submission_functions.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. function create_submission ($data, $gradeimporterid, $userid) {
  17. global $DB;
  18. $timenow = time();
  19. if (empty($data->id)) {
  20. // If is new db entry;
  21. $data->gradeimporterid = $gradeimporterid;
  22. $data->usermodified = $userid;
  23. $data->timecreated = $timenow;
  24. $isnewentry = true;
  25. } else {
  26. $isnewentry = false;
  27. }
  28. $data->introformat = $data->intro["format"];
  29. $data->intro = $data->intro["text"];
  30. $data->timemodified = $timenow;
  31. $data->position = -1; // Do later
  32. if ($isnewentry) {
  33. // If is new entry insert data into DB and gets id
  34. $data->id = $DB->insert_record('gradeimporter_submission', $data);
  35. }
  36. // If not new entry updates information
  37. // If new entry inserts id into DB
  38. $DB->update_record('gradeimporter_submission', $data);
  39. return $data;
  40. }
  41. function store_files ($context, $cm, $data) {
  42. global $CFG;
  43. $fs = get_file_storage();
  44. $files = $fs->get_area_files($context->id, 'mod_gradeimporter',
  45. 'submissionfiles', $data->id
  46. );
  47. foreach ($files as $file) {
  48. if ($file->get_mimetype() == 'text/csv') {
  49. read_csv($file->get_content(), $context, $data);
  50. return;
  51. }
  52. if ($file->get_mimetype() == 'application/zip') {
  53. // If it is a .zip file, extract files from the zip
  54. $zipfiles = read_zip($file, $context, $fs);
  55. // Search a .csv file
  56. foreach ($zipfiles as $file) {
  57. if ($file->get_mimetype() == 'text/csv') {
  58. // If csv is found, pass it to read_csv()
  59. read_csv($file->get_content(), $context, $data, $zipfiles);
  60. // Only expects one csv so doesn't look for another
  61. }
  62. }
  63. // Finished processing files from zip, so delete temp copies
  64. $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
  65. }
  66. }
  67. }
  68. function read_csv ($content, $context, $data, $zipfiles = null) {
  69. $csv = prepare_csv($content);
  70. foreach ($csv as $line => $feedback) {
  71. $feedback = fix_feedback($feedback, $line); // Check if data is correct
  72. $feedbackid = store_feedback($feedback, $context->id, $data->id);
  73. gradeimporter_update_grades($data, $feedback['id']);
  74. if ($feedback['file'] != "") {
  75. // If feedback has associated file, insert it into pluginfile
  76. foreach ($zipfiles as $file) {
  77. // Search in zipfiles to find same name then feedback['file']
  78. if ($feedback['file'] == $file->get_filename()) {
  79. $feedbackfileinfo = store_feedback_file($feedback, $context, $data, $file, $feedbackid);
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. }
  86. function prepare_csv ($content) {
  87. $csvlines = explode(PHP_EOL, $content);
  88. $csv = array();
  89. foreach ($csvlines as $line) {
  90. $csv[] = str_getcsv($line);
  91. }
  92. $header = array_shift($csv);
  93. $outputcsv = array();
  94. $outputcsv = array_map(
  95. function($v)use($header){
  96. return array_combine($header, $v);
  97. },
  98. $csv
  99. );
  100. return $outputcsv;
  101. }
  102. function store_feedback ($feedback, $contextid, $submissionid) {
  103. global $DB, $USER;
  104. // Prepare data to submit to gradeimporter_feedback table
  105. $entry = feedback_exists($feedback, $submissionid);
  106. $isnewentry = false;
  107. if (!$entry) {
  108. $entry = new stdClass();
  109. $entry->id = null;
  110. $entry->timecreated = time();
  111. $entry->submissionid = $submissionid;
  112. $entry->studentid = $feedback['id'];
  113. $entry->contextid = $contextid;
  114. $isnewentry = true;
  115. } else {
  116. // If already had a feedback
  117. // Deletes its files and remove its entry on DB
  118. delete_feedback_files($entry);
  119. $DB->delete_records('gradeimporter_feedback',
  120. array('submissionid' => $submissionid,
  121. 'studentid' => $feedback['id'])
  122. );
  123. }
  124. $entry->timemodified = time();
  125. $entry->grade = $feedback['grade'];
  126. $entry->comment = $feedback['comment'];
  127. $entry->filename = $feedback['file'];
  128. $entry->usermodified = $USER->id;
  129. // If is not a new feedback update previous record and return id
  130. return $DB->insert_record('gradeimporter_feedback', $entry);
  131. }
  132. function store_feedback_file ($feedback, $context, $submission, $filetostore, $feedbackid) {
  133. // Prepare file
  134. $fs = get_file_storage();
  135. // Create file information needed
  136. $fileinfo = array(
  137. 'contextid' => $context->id,
  138. 'component' => 'mod_gradeimporter',
  139. 'filearea' => 'submissionfiles',
  140. 'itemid' => $feedbackid,
  141. 'filepath' => "/",
  142. 'filename' => $filetostore->get_filename(),
  143. 'timecreated' => time(), 'timemodified' => time()
  144. );
  145. // Store files in correct area
  146. return $fs->create_file_from_storedfile($fileinfo, $filetostore);
  147. }
  148. function read_zip ($file, $context, $fs) {
  149. global $CFG;
  150. // Get file packer to unpack zip
  151. $packer = get_file_packer('application/zip');
  152. // Clear area_files if it has been used before
  153. $fs->delete_area_files($context->id,
  154. 'mod_gradeimporter',
  155. 'unpacktemp'
  156. );
  157. // Extract to temp areafiles
  158. $file->extract_to_storage($packer,
  159. $context->id,
  160. 'mod_gradeimporter',
  161. 'unpacktemp',
  162. 0,
  163. $CFG->tempdir,
  164. false
  165. );
  166. // Get extracted files from unpacktemp area file
  167. $tempfiles = $fs->get_area_files($context->id,
  168. 'mod_gradeimporter',
  169. 'unpacktemp'
  170. );
  171. // Returns an array of files object
  172. return $tempfiles;
  173. }
  174. function get_types_array($gradeimporterid) {
  175. global $DB, $CFG;
  176. // Gets moodle table prefix, usually mdl_
  177. $tp = $CFG->prefix;
  178. $query = "select id, name from {$tp}gradeimporter_submissiontype where gradeimporterid = $gradeimporterid";
  179. return $DB->get_records_sql($query);
  180. }
  181. function validate_formdata($data) {
  182. if ($data->type == -1) {
  183. throw new moodle_exception(get_string('invalidtype', 'gradeimporter'));
  184. }
  185. }
  186. function feedback_exists($feedback, $submissionid) {
  187. global $DB;
  188. $feedback = $DB->get_record('gradeimporter_feedback',
  189. array('submissionid' => $submissionid,
  190. 'studentid' => $feedback['id']),
  191. );
  192. return $feedback;
  193. }
  194. /**
  195. * Searches if a feedback has a file when updating it
  196. * If already has file then delete it to open space for a new file
  197. * Or if updated feedback doesnt have associated file
  198. * @param $feedback - record of already submited feedback
  199. * @return void
  200. */
  201. function delete_feedback_files($feedback) {
  202. $fs = get_file_storage();
  203. $file = $fs->get_file($feedback->contextid, 'mod_gradeimporter', 'submissionfiles', $feedback->id, '/', $feedback->filename);
  204. if ($file) {
  205. $file->delete();
  206. }
  207. }
  208. /**
  209. * Checks if $feedback csv line is correct
  210. * if id is missing throw error
  211. * if grade is missing change it to 0
  212. * @param array $feedback - line with feedback data for a student
  213. * @param int $line - which line of the csv file the feedback is
  214. * @return array $feedback - Either empty grade changed to 0 or the same as param
  215. */
  216. function fix_feedback ($feedback, $line) {
  217. if ($feedback['id'] == "") {
  218. throw new moodle_exception(get_string('studentidmissing', 'gradeimporter'), $line);
  219. }
  220. if ($feedback['grade'] == "") {
  221. $feedback['grade'] = 0;
  222. }
  223. return $feedback;
  224. }