123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?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/>.
- // Adds gradeimporter to add new activity page
- function tool_devcourse_extend_navigation_course($navigation, $course, $coursecontext) {
- $url = new moodle_url('/admin/tool/devcourse/index.php');
- $devcoursenode = navigation_node::create('Development course', $url, navigation_node::TYPE_CUSTOM, 'Dev course', 'devcourse');
- $navigation->add_node($devcoursenode);
- }
- function gradeimporter_supports($feature) {
- switch ($feature) {
- case FEATURE_MOD_ARCHETYPE:
- return MOD_ARCHETYPE_RESOURCE;
- case FEATURE_GROUPS:
- return false;
- case FEATURE_GROUPINGS:
- return false;
- case FEATURE_MOD_INTRO:
- return true;
- case FEATURE_COMPLETION_TRACKS_VIEWS:
- return true;
- case FEATURE_GRADE_HAS_GRADE:
- return false;
- case FEATURE_GRADE_OUTCOMES:
- return false;
- case FEATURE_BACKUP_MOODLE2:
- return true;
- default:
- return null;
- }
- }
- function gradeimporter_add_instance($data, $mform) {
- /*
- *Given an object containing all the necessary data,
- *(defined by the form in mod_form.php) this function
- *creates a new instance and returns the id of this new
- *instance.
- *
- *@param $data: an object from the form in mod_form.php
- *@return int: the id of the newly inserted gradeimport record
- */
- global $DB;
- $data->timemodified = time();
- $data->id = $DB->insert_record("gradeimporter", $data);
- return $data->id;
- }
- function gradeimporter_update_instance($data) {
- /*
- *given an object containing all the necessary data,
- *(defined by the form in mod_form.php) this function
- *updates an existing instance with the new data.
- *
- *@param $data: an object from the form mod_form.php
- *@return boolean: if the record update was a success of fail
- */
- global $DB;
- $data->timemodified = time();
- $data->id = $data->instance;
- return $DB->update_record('gradeimporter', $data);
- }
- function gradeimporter_delete_instance($data) {
- /*
- *Given an id of a gradeimporter instance,
- * this function permanently deletes the instance
- * and any data that depends on it.
- *
- *@param int $id: Id of the gradeimporter instance
- *@return boolean, if the deletion was a success or
- * a failure.
- */
- global $DB;
- if (!$data = $DB->get_record('gradeimporter', array('id' => $id))) {
- return false;
- }
- $cm = get_coursemodule_from_instance('gradeimporter', $gradeimporter->id);
- $context = context_module::instance($cm->id);
- // Files
- $fs = get_file_storage();
- $fs->delete_area_files($context->id, 'mod_gradeimporter');
- // Delete all files and submissions associated with this instance
- $DB->delete_records('gradeimporter_submission', array('gradeimporterid' => $gradeimporter->id));
- $DB->delete_records('gradeimporter_feedback', array('gradeimporterid' => $gradeimporter->id));
- // Delete the instance itself
- $DB->delete_records('gradeimporter', array('id' => $id));
- return true;
- }
- function gradeimporter_check_for_zips($context, $cm, $submission) {
- global $DB;
- $fs = get_file_storage();
- $files = $fs->get_area_files($context->id, 'mod_gradeimporter',
- 'submission', $submission->id,
- "itemid, filepath, filename", false);
- foreach ($files as $storedfile) {
- if ($storedfile->get_mimetype() == 'application/zip') {
- // Unpack zip
- $packer = get_file_packer('application/zip');
- $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
- $storedfile->extract_to_storage($packer, $context->id, 'mod_gradeimporter',
- 'unpacktemp', 0, "item, filepath, filename", false
- );
- $tempfiles = $fs->get_area_files($context->id, 'mod_gradeimporter', 'unpacktemp',
- 0, "itemid, filepath, filename", false);
- if (count($tempfiles) > 0) {
- $storedfile->delete(); // Delete the zip
- foreach ($tempfiles as $storedfile) {
- if ($storedfile->get_mimetype() == 'text/csv') {
- $csvdata = $storedfile->get_content();
- $csvlines = explode(PHP_EOL, $csvdata);
- $csv = array();
- foreach ($csvlines as $line) {
- $csv[] = str_getcsv($line);
- }
- // array_map('str_getcsv', $storedfile->get_content() );
- array_walk($csv, function (&$a) use ($csv) { // Adds first row as keys
- $a = array_combine($csv[0], $a);
- });
- array_shift($csv); // Remove column header
- for ($i = 0; $i < count($csv); $i++) {
- foreach ($tempfiles as $storedfile) {
- if ($storedfile->get_filename() == $csv[$i]['file']) {
- $dbentry = new stdClass();
- $dbentry->id = null;
- $dbentry->submissionid = $submission->id;
- $dbentry->studentid = $csv[$i]['id'];
- $dbentry->grade = $csv[$i]['grade'];
- $dbentry->comment = $csv[$i]['comment'];
- $dbentry->fileid = $submission->id;
- $dbentry->usermodified = 0;
- $dbentry->timecreated = time();
- $dbentry->timemodified = time();
- $dbentry->contextid = $context->id;
- $dbentry->name = $csv[$i]['file'];
- $dbentry->id = $DB->insert_record('gradeimporter_feedback', $dbentry);
- $fileinfo = array(
- 'contextid' => $context->id,
- 'component' => 'mod_gradeimporter',
- 'filearea' => 'gradeimporter_feedback',
- 'itemid' => $dbentry->id,
- 'filepath' => '/',
- 'filename' => $csv[$i]['file']
- );
- $fs->create_file_from_storedfile($fileinfo, $storedfile);
- break;
- }
- }
- }
- }
- }
- }
- $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
- }
- }
- }
|