submission_form_functions.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // Already used all files, so delete extra copies
  82. }
  83. }
  84. }
  85. function prepare_csv ($content) {
  86. $csvlines = explode(PHP_EOL, $content);
  87. $csv = array();
  88. foreach ($csvlines as $line) {
  89. $csv[] = str_getcsv($line);
  90. }
  91. $header = array_shift($csv);
  92. $outputcsv = array();
  93. $outputcsv = array_map(
  94. function($v)use($header){
  95. return array_combine($header, $v);
  96. },
  97. $csv
  98. );
  99. return $outputcsv;
  100. }
  101. function store_feedback ($feedback, $contextid, $submissionid) {
  102. global $DB;
  103. // Prepare data to submit to gradeimporter_feedback table
  104. $entry = new stdClass();
  105. $entry->id = null;
  106. $entry->timecreated = time();
  107. $entry->timemodified = time();
  108. $entry->submissionid = $submissionid;
  109. $entry->studentid = $feedback['id'];
  110. $entry->grade = $feedback['grade'];
  111. $entry->comment = $feedback['comment'];
  112. $entry->name = $feedback['file'];
  113. $entry->fileid = 0;
  114. $entry->usermodified = 1;
  115. $entry->contextid = $contextid;
  116. // Insert data into gradeimporter_feedback table and gets ID
  117. $entry->id = $DB->insert_record('gradeimporter_feedback', $entry);
  118. return $entry->id;
  119. }
  120. function store_feedback_file ($feedback, $context, $submission, $filetostore, $feedbackid) {
  121. // Prepare file
  122. $fs = get_file_storage();
  123. $fileinfo = array(
  124. 'contextid' => $context->id,
  125. 'component' => 'mod_gradeimporter',
  126. 'filearea' => 'submissionfiles',
  127. 'itemid' => $feedbackid,
  128. 'filepath' => "/",
  129. 'filename' => $filetostore->get_filename(),
  130. 'timecreated' => time(), 'timemodified' => time()
  131. );
  132. $fileinfo = $fs->create_file_from_storedfile($fileinfo, $filetostore);
  133. return $fileinfo;
  134. }
  135. function read_zip ($file, $context, $fs) {
  136. global $CFG;
  137. // Get file packer to unpack zip
  138. $packer = get_file_packer('application/zip');
  139. // Clear area_files if it has been used before
  140. $fs->delete_area_files($context->id,
  141. 'mod_gradeimporter',
  142. 'unpacktemp'
  143. );
  144. // Extract to temp areafiles
  145. $file->extract_to_storage($packer,
  146. $context->id,
  147. 'mod_gradeimporter',
  148. 'unpacktemp',
  149. 0,
  150. $CFG->tempdir,
  151. false
  152. );
  153. // Get extracted files from unpacktemp area file
  154. $tempfiles = $fs->get_area_files($context->id,
  155. 'mod_gradeimporter',
  156. 'unpacktemp'
  157. );
  158. // Returns an array of files object
  159. return $tempfiles;
  160. }
  161. function get_types_array($gradeimporterid) {
  162. global $DB, $CFG;
  163. // Gets moodle table prefix, usually mdl_
  164. $tp = $CFG->prefix;
  165. $query = "select id, name from {$tp}gradeimporter_submissiontype where gradeimporterid = $gradeimporterid";
  166. return $DB->get_records_sql($query);
  167. }
  168. function validate_formdata($data) {
  169. if ($data->type == -1) {
  170. throw new moodle_exception(get_string('invalidtype', 'gradeimporter'));
  171. }
  172. }