submission_form_functions.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. $fs = get_file_storage();
  43. $files = $fs->get_area_files($context->id, 'mod_gradeimporter',
  44. 'submissionfiles', $data->id
  45. );
  46. foreach ($files as $file) {
  47. $extension = explode(".", $file->get_filename())[1];
  48. if ($extension == 'csv') {
  49. read_csv($file->get_content(), $context, $data);
  50. return;
  51. }
  52. }
  53. }
  54. function read_csv($content, $context, $data) {
  55. $csv = prepare_csv($content);
  56. foreach ($csv as $feedback) {
  57. if ($feedback['file'] != "") {
  58. // If feedback has associated file, insert it into pluginfile
  59. $feedbackfilepath = store_feedback_file($feedback, $context, $data, $filepath);
  60. }
  61. echo "Before store_feedback</br>";
  62. store_feedback($feedback, $context->id, $data->id);
  63. echo "After store_feedback</br>";
  64. }
  65. }
  66. function prepare_csv($content) {
  67. $csvlines = explode(PHP_EOL, $content);
  68. $csv = array();
  69. foreach ($csvlines as $line) {
  70. $csv[] = str_getcsv($line);
  71. }
  72. $header = array_shift($csv);
  73. $outputcsv = array();
  74. $outputcsv = array_map(
  75. function($v)use($header){
  76. return array_combine($header, $v);
  77. },
  78. $csv
  79. );
  80. return $outputcsv;
  81. }
  82. function store_feedback($feedback, $contextid, $submissionid) {
  83. global $DB;
  84. // Prepare data to submit to gradeimporter_feedback table
  85. $entry = new stdClass();
  86. $entry->id = null;
  87. $entry->timecreated = time();
  88. $entry->timemodified = time();
  89. $entry->submissionid = $submissionid;
  90. $entry->studentid = $feedback['id'];
  91. $entry->grade = $feedback['grade'];
  92. $entry->comment = $feedback['comment'];
  93. $entry->name = $feedback['file'];
  94. $entry->fileid = 0;
  95. $entry->usermodified = 1;
  96. $entry->contextid = $contextid;
  97. // Insert data into gradeimporter_feedback table and gets ID
  98. $entry->id = $DB->insert_record('gradeimporter_feedback', $entry);
  99. }
  100. function store_feedback_file($feedback, $context, $data, $filepath) {
  101. // Prepare file
  102. $fs = get_file_storage();
  103. $fileinfo = array(
  104. 'contextid' => $context->id,
  105. 'component' => 'mod_gradeimporter',
  106. 'filearea' => 'submissionfiles',
  107. 'itemid' => 0,
  108. 'filepath' => "/$data->id/{$feedback[id]}/",
  109. 'filename' => $feedback['file'],
  110. 'timecreated' => time(),
  111. 'timemodified' => time()
  112. );
  113. // Move file from temp to pluginfile
  114. $fs->create_file_from_pathname($fileinfo, $filepath);
  115. return $fileinfo['filepath'];
  116. }