lib.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. //adds gradeimporter to add new activity page
  3. function tool_devcourse_extend_navigation_course($navigation, $course, $coursecontext) {
  4. $url = new moodle_url('/admin/tool/devcourse/index.php');
  5. $devcoursenode = navigation_node::create('Development course', $url, navigation_node::TYPE_CUSTOM, 'Dev course', 'devcourse');
  6. $navigation->add_node($devcoursenode);
  7. }
  8. function gradeimporter_supports($feature) {
  9. switch($feature) {
  10. case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
  11. case FEATURE_GROUPS: return false;
  12. case FEATURE_GROUPINGS: return false;
  13. case FEATURE_MOD_INTRO: return true;
  14. case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
  15. case FEATURE_GRADE_HAS_GRADE: return false;
  16. case FEATURE_GRADE_OUTCOMES: return false;
  17. case FEATURE_BACKUP_MOODLE2: return true;
  18. default: return null;
  19. }
  20. }
  21. function gradeimporter_add_instance ($data, $mform) {
  22. /*
  23. *Given an object containing all the necessary data,
  24. *(defined by the form in mod_form.php) this function
  25. *creates a new instance and returns the id of this new
  26. *instance.
  27. *
  28. *@param $data: an object from the form in mod_form.php
  29. *@return int: the id of the newly inserted gradeimport record
  30. */
  31. global $DB;
  32. $data->timemodified = time();
  33. $data->id = $DB->insert_record("gradeimporter", $data);
  34. return $data->id;
  35. }
  36. function gradeimporter_update_instance ($data){
  37. /*
  38. *given an object containing all the necessary data,
  39. *(defined by the form in mod_form.php) this function
  40. *updates an existing instance with the new data.
  41. *
  42. *@param $data: an object from the form mod_form.php
  43. *@return boolean: if the record update was a success of fail
  44. */
  45. global $DB;
  46. $data->timemodified = time();
  47. $data->id = $data->instance;
  48. return $DB->update_record('gradeimporter', $data);
  49. }
  50. function gradeimporter_delete_instance($data){
  51. /*
  52. *Given an id of a gradeimporter instance,
  53. * this function permanently deletes the instance
  54. * and any data that depends on it.
  55. *
  56. *@param int $id: Id of the gradeimporter instance
  57. *@return boolean, if the deletion was a success or
  58. * a failure.
  59. */
  60. global $DB;
  61. if (!$data = $DB->get_record('gradeimporter', array('id'=>$id) ) ){
  62. return false;
  63. }
  64. $cm = get_coursemodule_from_instance('gradeimporter', $gradeimporter->id);
  65. $context = context_module::instance($cm->id);
  66. // Files
  67. $fs = get_file_storage();
  68. $fs->delete_area_files($context->id, 'mod_gradeimporter');
  69. //delete all files and submissions associated with this instance
  70. $DB->delete_records('gradeimporter_submission', array('gradeimporterid' => $gradeimporter->id));
  71. $DB->delete_records('gradeimporter_feedback', array('gradeimporterid' => $gradeimporter->id));
  72. //delete the instance itself
  73. $DB->delete_records('gradeimporter', array('id'=>$id) );
  74. return true;
  75. }
  76. function gradeimporter_check_for_zips ($context, $cm, $submission){
  77. global $DB;
  78. $fs = get_file_storage();
  79. $files = $fs->get_area_files($context->id, 'mod_gradeimporter', 'submission', $submission->id, "itemid, filepath, filename", false);
  80. echo "---- DEBUG ---- \n linha 96\n";
  81. foreach ($files as $storedfile) {
  82. if ($storedfile->get_mimetype() == 'application/zip') {
  83. echo "---- DEBUG ---- \n linha 99 - unpack zip \n";
  84. //unpack zip
  85. $packer = get_file_packer('application/zip');
  86. $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
  87. $storedfile->extract_to_storage($packer, $context->id, 'mod_gradeimporter', 'unpacktemp', 0, "item, filepath, filename", false);
  88. $tempfiles = $fs->get_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0, "itemid, filepath, filename", false);
  89. if (count($tempfiles) > 0) {
  90. echo "---- DEBUG ---- \n linha 105 - tempfiles > 0 \n";
  91. $storedfile->delete(); // delete the zip
  92. foreach ($tempfiles as $storedfile){
  93. //copy files to known directory
  94. echo $storedfile->get_filename();
  95. echo " mimetype ";
  96. echo $storedfile->get_mimetype();
  97. echo "file location";
  98. echo $storedfile->get_filepath(). ' \n ';
  99. }
  100. foreach ($tempfiles as $storedfile){
  101. if ($storedfile->get_mimetype() == 'text/csv'){
  102. echo "---- DEBUG ---- \n linha 110 - achou csv \n";
  103. $csvData = $storedfile->get_content();
  104. $csvLines = explode(PHP_EOL, $csvData);
  105. $csv = array();
  106. foreach($csvLines as $line){
  107. $csv[] = str_getcsv($line);
  108. }
  109. print_r($csv);
  110. //array_map('str_getcsv', $storedfile->get_content() );
  111. array_walk($csv, function(&$a) use ($csv) { //adds first row as keys
  112. $a = array_combine($csv[0], $a);
  113. });
  114. array_shift($csv); // remove column header
  115. print_r($csv);
  116. for ($i = 0; $i< sizeof($csv); $i++){
  117. $fileinfo = array(
  118. 'contextid' => $context->id,
  119. 'component' => 'mod_gradeimporter',
  120. 'filearea' => 'submission',
  121. 'itemid' => $submission->id,
  122. 'filepath' => '/',
  123. 'file' => $csv[$i]['file'],
  124. 'studentid' => $csv[$i]['id']
  125. );
  126. foreach ($tempfiles as $storedfile) {
  127. echo '---- DEBUG ---- \n linha 129 - file name'.$storedfile->get_filename();
  128. if ($storedfile->get_filename() == $csv[$i]['file']){
  129. $storedfile = $fs->create_file_from_storedfile($fileinfo, $storedfile);
  130. $dbentry = new stdClass();
  131. $dbentry->id = null;
  132. $dbentry->submissionid = $submission->id;
  133. $dbentry->studentid = $csv[$i]['id'];
  134. $dbentry->grade = $csv[$i]['grade'];
  135. $dbentry->comment = $csv[$i]['comment'];
  136. $dbentry->fileid = $storedfile;
  137. $dbentry->usermodified = 0;
  138. $dbentry->timecreated = time();
  139. $dbentry->timemodified = time();
  140. $dbentry->id = $DB->insert_record('gradeimporter_feedback', $dbentry);
  141. break;
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
  149. }
  150. }
  151. }