submission_form_functions.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. }
  119. function store_feedback_file ($feedback, $context, $submission, $filetostore, $feedbackid) {
  120. // Prepare file
  121. $fs = get_file_storage();
  122. $fileinfo = array(
  123. 'contextid' => $context->id,
  124. 'component' => 'mod_gradeimporter',
  125. 'filearea' => 'submissionfiles',
  126. 'itemid' => 0,
  127. 'filepath' => "/",
  128. 'filename' => $filetostore->get_filename(),
  129. 'timecreated' => time(), 'timemodified' => time()
  130. );
  131. $fileinfo = $fs->create_file_from_storedfile($fileinfo, $filetostore);
  132. return $fileinfo;
  133. }
  134. function read_zip ($file, $context, $fs) {
  135. global $CFG;
  136. // Get file packer to unpack zip
  137. $packer = get_file_packer('application/zip');
  138. // Clear area_files if it has been used before
  139. $fs->delete_area_files($context->id,
  140. 'mod_gradeimporter',
  141. 'unpacktemp'
  142. );
  143. // Extract to temp areafiles
  144. $file->extract_to_storage($packer,
  145. $context->id,
  146. 'mod_gradeimporter',
  147. 'unpacktemp',
  148. 0,
  149. $CFG->tempdir,
  150. false
  151. );
  152. // Get extracted files from unpacktemp area file
  153. $tempfiles = $fs->get_area_files($context->id,
  154. 'mod_gradeimporter',
  155. 'unpacktemp'
  156. );
  157. // Returns an array of files object
  158. return $tempfiles;
  159. }
  160. function get_types_array($gradeimporterid) {
  161. global $DB, $CFG;
  162. // Gets moodle table prefix, usually mdl_
  163. $tp = $CFG->prefix;
  164. $query = "select id, name from {$tp}gradeimporter_submissiontype where gradeimporterid = $gradeimporterid";
  165. return $DB->get_records_sql($query);
  166. }