lib.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. // This file is part of
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. // Adds gradeimporter to add new activity page
  17. function tool_devcourse_extend_navigation_course($navigation, $course, $coursecontext) {
  18. $url = new moodle_url('/admin/tool/devcourse/index.php');
  19. $devcoursenode = navigation_node::create('Development course', $url, navigation_node::TYPE_CUSTOM, 'Dev course', 'devcourse');
  20. $navigation->add_node($devcoursenode);
  21. }
  22. function gradeimporter_supports($feature) {
  23. switch ($feature) {
  24. case FEATURE_MOD_ARCHETYPE:
  25. return MOD_ARCHETYPE_RESOURCE;
  26. case FEATURE_GROUPS:
  27. return false;
  28. case FEATURE_GROUPINGS:
  29. return false;
  30. case FEATURE_MOD_INTRO:
  31. return true;
  32. case FEATURE_COMPLETION_TRACKS_VIEWS:
  33. return true;
  34. case FEATURE_GRADE_HAS_GRADE:
  35. return false;
  36. case FEATURE_GRADE_OUTCOMES:
  37. return false;
  38. case FEATURE_BACKUP_MOODLE2:
  39. return true;
  40. default:
  41. return null;
  42. }
  43. }
  44. function gradeimporter_add_instance($data, $mform) {
  45. /*
  46. *Given an object containing all the necessary data,
  47. *(defined by the form in mod_form.php) this function
  48. *creates a new instance and returns the id of this new
  49. *instance.
  50. *
  51. *@param $data: an object from the form in mod_form.php
  52. *@return int: the id of the newly inserted gradeimport record
  53. */
  54. global $DB;
  55. $data->timemodified = time();
  56. $data->id = $DB->insert_record("gradeimporter", $data);
  57. return $data->id;
  58. }
  59. function gradeimporter_update_instance($data) {
  60. /*
  61. *given an object containing all the necessary data,
  62. *(defined by the form in mod_form.php) this function
  63. *updates an existing instance with the new data.
  64. *
  65. *@param $data: an object from the form mod_form.php
  66. *@return boolean: if the record update was a success of fail
  67. */
  68. global $DB;
  69. $data->timemodified = time();
  70. $data->id = $data->instance;
  71. return $DB->update_record('gradeimporter', $data);
  72. }
  73. function gradeimporter_delete_instance($data) {
  74. /*
  75. *Given an id of a gradeimporter instance,
  76. * this function permanently deletes the instance
  77. * and any data that depends on it.
  78. *
  79. *@param int $id: Id of the gradeimporter instance
  80. *@return boolean, if the deletion was a success or
  81. * a failure.
  82. */
  83. global $DB;
  84. if (!$data = $DB->get_record('gradeimporter', array('id' => $id))) {
  85. return false;
  86. }
  87. $cm = get_coursemodule_from_instance('gradeimporter', $gradeimporter->id);
  88. $context = context_module::instance($cm->id);
  89. // Files
  90. $fs = get_file_storage();
  91. $fs->delete_area_files($context->id, 'mod_gradeimporter');
  92. // Delete all files and submissions associated with this instance
  93. $DB->delete_records('gradeimporter_submission', array('gradeimporterid' => $gradeimporter->id));
  94. $DB->delete_records('gradeimporter_feedback', array('gradeimporterid' => $gradeimporter->id));
  95. // Delete the instance itself
  96. $DB->delete_records('gradeimporter', array('id' => $id));
  97. return true;
  98. }
  99. function gradeimporter_check_for_zips($context, $cm, $submission) {
  100. global $DB;
  101. $fs = get_file_storage();
  102. $files = $fs->get_area_files($context->id, 'mod_gradeimporter',
  103. 'submission', $submission->id,
  104. "itemid, filepath, filename", false);
  105. foreach ($files as $storedfile) {
  106. if ($storedfile->get_mimetype() == 'application/zip') {
  107. // Unpack zip
  108. $packer = get_file_packer('application/zip');
  109. $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
  110. $storedfile->extract_to_storage($packer, $context->id, 'mod_gradeimporter',
  111. 'unpacktemp', 0, "item, filepath, filename", false
  112. );
  113. $tempfiles = $fs->get_area_files($context->id, 'mod_gradeimporter', 'unpacktemp',
  114. 0, "itemid, filepath, filename", false);
  115. if (count($tempfiles) > 0) {
  116. $storedfile->delete(); // Delete the zip
  117. foreach ($tempfiles as $storedfile) {
  118. if ($storedfile->get_mimetype() == 'text/csv') {
  119. $csvdata = $storedfile->get_content();
  120. $csvlines = explode(PHP_EOL, $csvdata);
  121. $csv = array();
  122. foreach ($csvlines as $line) {
  123. $csv[] = str_getcsv($line);
  124. }
  125. // array_map('str_getcsv', $storedfile->get_content() );
  126. array_walk($csv, function (&$a) use ($csv) { // Adds first row as keys
  127. $a = array_combine($csv[0], $a);
  128. });
  129. array_shift($csv); // Remove column header
  130. for ($i = 0; $i < count($csv); $i++) {
  131. foreach ($tempfiles as $storedfile) {
  132. if ($storedfile->get_filename() == $csv[$i]['file']) {
  133. $dbentry = new stdClass();
  134. $dbentry->id = null;
  135. $dbentry->submissionid = $submission->id;
  136. $dbentry->studentid = $csv[$i]['id'];
  137. $dbentry->grade = $csv[$i]['grade'];
  138. $dbentry->comment = $csv[$i]['comment'];
  139. $dbentry->fileid = $submission->id;
  140. $dbentry->usermodified = 0;
  141. $dbentry->timecreated = time();
  142. $dbentry->timemodified = time();
  143. $dbentry->contextid = $context->id;
  144. $dbentry->name = $csv[$i]['file'];
  145. $dbentry->id = $DB->insert_record('gradeimporter_feedback', $dbentry);
  146. $fileinfo = array(
  147. 'contextid' => $context->id,
  148. 'component' => 'mod_gradeimporter',
  149. 'filearea' => 'gradeimporter_feedback',
  150. 'itemid' => $dbentry->id,
  151. 'filepath' => '/',
  152. 'filename' => $csv[$i]['file']
  153. );
  154. $fs->create_file_from_storedfile($fileinfo, $storedfile);
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }
  162. $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
  163. }
  164. }
  165. }