123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- // This file is part of
- //
- // Moodle is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // Moodle is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
- function create_submission($data, $gradeimporterid, $userid) {
- global $DB;
- $timenow = time();
- if (empty($data->id)) {
- // If is new db entry;
- $data->gradeimporterid = $gradeimporterid;
- $data->usermodified = $userid;
- $data->timecreated = $timenow;
- $isnewentry = true;
- } else {
- $isnewentry = false;
- }
- $data->descriptionformat = $data->description["format"];
- $data->description = $data->description["text"];
- $data->timemodified = $timenow;
- $data->position = -1; // Do later
- if ($isnewentry) {
- // If is new entry insert data into DB and gets id
- $data->id = $DB->insert_record('gradeimporter_submission', $data);
- }
- // If not new entry updates information
- // If new entry inserts id into DB
- $DB->update_record('gradeimporter_submission', $data);
- return $data;
- }
- function store_files($context, $cm, $data) {
- $fs = get_file_storage();
- $files = $fs->get_area_files($context->id, 'mod_gradeimporter',
- 'submissionfiles', $data->id
- );
- foreach ($files as $file) {
- $extension = explode(".", $file->get_filename())[1];
- if ($extension == 'csv') {
- read_csv($file->get_content(), $context, $data);
- return;
- }
- }
- }
- function read_csv($content, $context, $data) {
- $csv = prepare_csv($content);
- foreach ($csv as $feedback) {
- if ($feedback['file'] != "") {
- // If feedback has associated file, insert it into pluginfile
- $feedbackfilepath = store_feedback_file($feedback, $context, $data, $filepath);
- }
- echo "Before store_feedback</br>";
- store_feedback($feedback, $context->id, $data->id);
- echo "After store_feedback</br>";
- }
- }
- function prepare_csv($content) {
- $csvlines = explode(PHP_EOL, $content);
- $csv = array();
- foreach ($csvlines as $line) {
- $csv[] = str_getcsv($line);
- }
- $header = array_shift($csv);
- $outputcsv = array();
- $outputcsv = array_map(
- function($v)use($header){
- return array_combine($header, $v);
- },
- $csv
- );
- return $outputcsv;
- }
- function store_feedback($feedback, $contextid, $submissionid) {
- global $DB;
- // Prepare data to submit to gradeimporter_feedback table
- $entry = new stdClass();
- $entry->id = null;
- $entry->timecreated = time();
- $entry->timemodified = time();
- $entry->submissionid = $submissionid;
- $entry->studentid = $feedback['id'];
- $entry->grade = $feedback['grade'];
- $entry->comment = $feedback['comment'];
- $entry->name = $feedback['file'];
- $entry->fileid = 0;
- $entry->usermodified = 1;
- $entry->contextid = $contextid;
- // Insert data into gradeimporter_feedback table and gets ID
- $entry->id = $DB->insert_record('gradeimporter_feedback', $entry);
- }
- function store_feedback_file($feedback, $context, $data, $filepath) {
- // Prepare file
- $fs = get_file_storage();
- $fileinfo = array(
- 'contextid' => $context->id,
- 'component' => 'mod_gradeimporter',
- 'filearea' => 'submissionfiles',
- 'itemid' => 0,
- 'filepath' => "/$data->id/{$feedback[id]}/",
- 'filename' => $feedback['file'],
- 'timecreated' => time(),
- 'timemodified' => time()
- );
- // Move file from temp to pluginfile
- $fs->create_file_from_pathname($fileinfo, $filepath);
- return $fileinfo['filepath'];
- }
|