submission_form_functions.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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->descriptionformat = $data->description["format"];
  29. $data->description = $data->description["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 $feedback) {
  71. $feedbackid = store_feedback($feedback, $context->id, $data->id);
  72. if ($feedback['file'] != "") {
  73. // If feedback has associated file, insert it into pluginfile
  74. foreach ($zipfiles as $file) {
  75. // Search in zipfiles to find same name then feedback['file']
  76. if ($feedback['file'] == $file->get_filename()) {
  77. $feedbackfileinfo = store_feedback_file($feedback, $context, $data, $file, $feedbackid);
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. }
  84. function prepare_csv ($content) {
  85. $csvlines = explode(PHP_EOL, $content);
  86. $csv = array();
  87. foreach ($csvlines as $line) {
  88. $csv[] = str_getcsv($line);
  89. }
  90. $header = array_shift($csv);
  91. $outputcsv = array();
  92. $outputcsv = array_map(
  93. function($v)use($header){
  94. return array_combine($header, $v);
  95. },
  96. $csv
  97. );
  98. return $outputcsv;
  99. }
  100. function store_feedback ($feedback, $contextid, $submissionid) {
  101. global $DB, $USER;
  102. // Prepare data to submit to gradeimporter_feedback table
  103. $entry = feedback_exists($feedback, $submissionid);
  104. $isnewentry = false;
  105. if (!$entry) {
  106. $entry = new stdClass();
  107. $entry->id = null;
  108. $entry->timecreated = time();
  109. $entry->submissionid = $submissionid;
  110. $entry->studentid = $feedback['id'];
  111. $entry->contextid = $contextid;
  112. $isnewentry = true;
  113. } else {
  114. // If already had a feedback
  115. // Deletes its files and remove its entry on DB
  116. delete_feedback_files($entry);
  117. $DB->delete_records('gradeimporter_feedback',
  118. array('submissionid' => $submissionid,
  119. 'studentid' => $feedback['id'])
  120. );
  121. }
  122. $entry->timemodified = time();
  123. $entry->grade = $feedback['grade'];
  124. $entry->comment = $feedback['comment'];
  125. $entry->name = $feedback['file'];
  126. $entry->fileid = 0; // To be removed fileid is now feedbackid
  127. $entry->usermodified = $USER->id;
  128. // If is not a new feedback update previous record and return id
  129. return $DB->insert_record('gradeimporter_feedback', $entry);
  130. }
  131. function store_feedback_file ($feedback, $context, $submission, $filetostore, $feedbackid) {
  132. // Prepare file
  133. $fs = get_file_storage();
  134. // Create file information needed
  135. $fileinfo = array(
  136. 'contextid' => $context->id,
  137. 'component' => 'mod_gradeimporter',
  138. 'filearea' => 'submissionfiles',
  139. 'itemid' => $feedbackid,
  140. 'filepath' => "/",
  141. 'filename' => $filetostore->get_filename(),
  142. 'timecreated' => time(), 'timemodified' => time()
  143. );
  144. // store files in correct area
  145. return $fs->create_file_from_storedfile($fileinfo, $filetostore);
  146. }
  147. function read_zip ($file, $context, $fs) {
  148. global $CFG;
  149. // Get file packer to unpack zip
  150. $packer = get_file_packer('application/zip');
  151. // Clear area_files if it has been used before
  152. $fs->delete_area_files($context->id,
  153. 'mod_gradeimporter',
  154. 'unpacktemp'
  155. );
  156. // Extract to temp areafiles
  157. $file->extract_to_storage($packer,
  158. $context->id,
  159. 'mod_gradeimporter',
  160. 'unpacktemp',
  161. 0,
  162. $CFG->tempdir,
  163. false
  164. );
  165. // Get extracted files from unpacktemp area file
  166. $tempfiles = $fs->get_area_files($context->id,
  167. 'mod_gradeimporter',
  168. 'unpacktemp'
  169. );
  170. // Returns an array of files object
  171. return $tempfiles;
  172. }
  173. function get_types_array($gradeimporterid) {
  174. global $DB, $CFG;
  175. // Gets moodle table prefix, usually mdl_
  176. $tp = $CFG->prefix;
  177. $query = "select id, name from {$tp}gradeimporter_submissiontype where gradeimporterid = $gradeimporterid";
  178. return $DB->get_records_sql($query);
  179. }
  180. function validate_formdata($data) {
  181. if ($data->type == -1) {
  182. throw new moodle_exception(get_string('invalidtype', 'gradeimporter'));
  183. }
  184. }
  185. function feedback_exists($feedback, $submissionid) {
  186. global $DB;
  187. $feedback = $DB->get_record('gradeimporter_feedback',
  188. array('submissionid' => $submissionid,
  189. 'studentid' => $feedback['id']),
  190. );
  191. return $feedback;
  192. }
  193. /**
  194. * Searches if a feedback has a file when updating it
  195. * If already has file then delete it to open space for a new file
  196. * Or if updated feedback doesnt have associated file
  197. * @param $feedback - record of already submited feedback
  198. * @return void
  199. */
  200. function delete_feedback_files($feedback) {
  201. $fs = get_file_storage();
  202. $file = $fs->get_file($feedback->contextid, 'mod_gradeimporter', 'submissionfiles', $feedback->id, '/', $feedback->name);
  203. if ($file) {
  204. $file->delete();
  205. }
  206. }