/** * Default implementation of file_get_info for plugins. * This is used by the filebrowser to browse a plugins file areas. * * This implementation should work for most plugins but can be overridden if required. * @param file_browser $browser * @param string $filearea * @param int $itemid * @param string $filepath * @param string $filename * @return file_info_stored */ public function get_file_info($browser, $filearea, $itemid, $filepath, $filename) { global $CFG, $DB, $USER; $urlbase = $CFG->wwwroot . '/pluginfile.php'; // Permission check on the itemid. if ($this->get_subtype() == 'setasksubmission') { if ($itemid) { $record = $DB->get_record('setask_submission', array('id' => $itemid), 'userid', IGNORE_MISSING); if (!$record) { return null; } if (!$this->setaskment->can_view_submission($record->userid)) { return null; } } } else { // Not supported for feedback plugins. return null; } $fs = get_file_storage(); $filepath = is_null($filepath) ? '/' : $filepath; $filename = is_null($filename) ? '.' : $filename; if (!($storedfile = $fs->get_file($this->setaskment->get_context()->id, $this->get_subtype() . '_' . $this->get_type(), $filearea, $itemid, $filepath, $filename))) { return null; } return new file_info_stored($browser, $this->setaskment->get_context(), $storedfile, $urlbase, $filearea, $itemid, true, true, false); }
/** * Create instance of event. * * @since Moodle 2.7 * * @param \setask $setask * @param \stdClass $submission * @return submission_status_updated */ public static function create_from_submission(\setask $setask, \stdClass $submission) { $data = array('context' => $setask->get_context(), 'objectid' => $submission->id, 'relateduserid' => $setask->get_instance()->teamsubmission ? null : $submission->userid, 'other' => array('newstatus' => $submission->status)); /** @var submission_status_updated $event */ $event = self::create($data); $event->set_setask($setask); $event->add_record_snapshot('setask_submission', $submission); return $event; }
/** * Create instance of event. * * @param \setask $setask * @param \stdClass $grade * @return feedback_viewed */ public static function create_from_grade(\setask $setask, \stdClass $grade) { $data = array('objectid' => $grade->id, 'relateduserid' => $grade->userid, 'context' => $setask->get_context(), 'other' => array('setaskid' => $setask->get_instance()->id)); /** @var feedback_viewed $event */ $event = self::create($data); $event->set_setask($setask); $event->add_record_snapshot('setask_grades', $grade); return $event; }
/** * Create instance of event. * * @param \setask $setask * @return grading_table_viewed */ public static function create_from_setask(\setask $setask) { $data = array('context' => $setask->get_context(), 'other' => array('setaskid' => $setask->get_instance()->id)); self::$preventcreatecall = false; /** @var grading_table_viewed $event */ $event = self::create($data); self::$preventcreatecall = true; $event->set_setask($setask); return $event; }
/** * Create instance of event. * * @since Moodle 2.7 * * @param \setask $setask * @return all_submissions_downloaded */ public static function create_from_setask(\setask $setask) { $data = array('context' => $setask->get_context(), 'objectid' => $setask->get_instance()->id); self::$preventcreatecall = false; /** @var submission_graded $event */ $event = self::create($data); self::$preventcreatecall = true; $event->set_setask($setask); return $event; }
/** * Set setask instance for this event. * @param \setask $setask * @throws \coding_exception */ public function set_setask(\setask $setask) { if ($this->is_triggered()) { throw new \coding_exception('set_setask() must be done before triggerring of event'); } if ($setask->get_context()->id != $this->get_context()->id) { throw new \coding_exception('Invalid setask isntance supplied!'); } $this->setask = $setask; }
/** * Create instance of event. * * @since Moodle 2.7 * * @param \setask $setask * @param \stdClass $submission * @return submission_duplicated */ public static function create_from_submission(\setask $setask, \stdClass $submission) { $data = array('objectid' => $submission->id, 'context' => $setask->get_context()); self::$preventcreatecall = false; /** @var submission_duplicated $event */ $event = self::create($data); self::$preventcreatecall = true; $event->set_setask($setask); $event->add_record_snapshot('setask_submission', $submission); return $event; }
/** * Create instance of event. * * @since Moodle 2.7 * * @param \setask $setask * @param \stdClass $user * @param string $state * @return workflow_state_updated */ public static function create_from_user(\setask $setask, \stdClass $user, $state) { $data = array('context' => $setask->get_context(), 'objectid' => $setask->get_instance()->id, 'relateduserid' => $user->id, 'other' => array('newstate' => $state)); self::$preventcreatecall = false; /** @var workflow_state_updated $event */ $event = self::create($data); self::$preventcreatecall = true; $event->set_setask($setask); $event->add_record_snapshot('user', $user); return $event; }
/** * Create instance of event. * * @since Moodle 2.7 * * @param \setask $setask * @param \stdClass $grade * @return submission_graded */ public static function create_from_grade(\setask $setask, \stdClass $grade) { $data = array('context' => $setask->get_context(), 'objectid' => $grade->id, 'relateduserid' => $grade->userid); self::$preventcreatecall = false; /** @var submission_graded $event */ $event = self::create($data); self::$preventcreatecall = true; $event->set_setask($setask); $event->add_record_snapshot('setask_grades', $grade); return $event; }
/** * Create instance of event. * * @param \setask $setask * @param \stdClass $user * @return grading_form_viewed */ public static function create_from_user(\setask $setask, \stdClass $user) { $data = array('relateduserid' => $user->id, 'context' => $setask->get_context(), 'other' => array('setaskid' => $setask->get_instance()->id)); self::$preventcreatecall = false; /** @var grading_form_viewed $event */ $event = self::create($data); self::$preventcreatecall = true; $event->set_setask($setask); $event->add_record_snapshot('user', $user); return $event; }
/** * Create instance of event. * * @since Moodle 2.7 * * @param \setask $setask * @param \stdClass $submission * @param bool $editable * @return assessable_submitted */ public static function create_from_submission(\setask $setask, \stdClass $submission, $editable) { global $USER; $data = array('context' => $setask->get_context(), 'objectid' => $submission->id, 'other' => array('submission_editable' => $editable)); if (!empty($submission->userid) && $submission->userid != $USER->id) { $data['relateduserid'] = $submission->userid; } /** @var assessable_submitted $event */ $event = self::create($data); $event->set_setask($setask); $event->add_record_snapshot('setask_submission', $submission); return $event; }
/** * list current marker * * @param stdClass $row - The row of data * @return id the user->id of the marker. */ public function col_allocatedmarker(stdClass $row) { static $markers = null; static $markerlist = array(); if ($markers === null) { list($sort, $params) = users_order_by_sql(); $markers = get_users_by_capability($this->setaskment->get_context(), 'mod/setask:grade', '', $sort); $markerlist[0] = get_string('choosemarker', 'setask'); foreach ($markers as $marker) { $markerlist[$marker->id] = fullname($marker); } } if (empty($markerlist)) { // TODO: add some form of notification here that no markers are available. return ''; } if ($this->is_downloading()) { if (isset($markers[$row->allocatedmarker])) { return fullname($markers[$row->allocatedmarker]); } else { return ''; } } if ($this->quickgrading && has_capability('mod/setask:manageallocations', $this->setaskment->get_context()) && (empty($row->workflowstate) || $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_INMARKING || $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED)) { $name = 'quickgrade_' . $row->id . '_allocatedmarker'; return html_writer::select($markerlist, $name, $row->allocatedmarker, false); } else { if (!empty($row->allocatedmarker)) { $output = ''; if ($this->quickgrading) { // Add hidden field for quickgrading page. $name = 'quickgrade_' . $row->id . '_allocatedmarker'; $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $name, 'value' => $row->allocatedmarker)); } $output .= $markerlist[$row->allocatedmarker]; return $output; } } }
/** * Returns submissions for the requested setaskment ids * * @param int[] $setaskmentids * @param string $status only return submissions with this status * @param int $since only return submissions with timemodified >= since * @param int $before only return submissions with timemodified <= before * @return array of submissions for each requested setaskment * @since Moodle 2.5 */ public static function get_submissions($setaskmentids, $status = '', $since = 0, $before = 0) { global $DB, $CFG; require_once "{$CFG->dirroot}/mod/setask/locallib.php"; $params = self::validate_parameters(self::get_submissions_parameters(), array('setaskmentids' => $setaskmentids, 'status' => $status, 'since' => $since, 'before' => $before)); $warnings = array(); $setaskments = array(); // Check the user is allowed to get the submissions for the setaskments requested. $placeholders = array(); list($inorequalsql, $placeholders) = $DB->get_in_or_equal($params['setaskmentids'], SQL_PARAMS_NAMED); $sql = "SELECT cm.id, cm.instance FROM {course_modules} cm JOIN {modules} md ON md.id = cm.module " . "WHERE md.name = :modname AND cm.instance " . $inorequalsql; $placeholders['modname'] = 'setask'; $cms = $DB->get_records_sql($sql, $placeholders); $setasks = array(); foreach ($cms as $cm) { try { $context = context_module::instance($cm->id); self::validate_context($context); require_capability('mod/setask:grade', $context); $setask = new setask($context, null, null); $setasks[] = $setask; } catch (Exception $e) { $warnings[] = array('item' => 'setaskment', 'itemid' => $cm->instance, 'warningcode' => '1', 'message' => 'No access rights in module context'); } } foreach ($setasks as $setask) { $submissions = array(); $submissionplugins = $setask->get_submission_plugins(); $placeholders = array('setaskid1' => $setask->get_instance()->id, 'setaskid2' => $setask->get_instance()->id); $submissionmaxattempt = 'SELECT mxs.userid, MAX(mxs.attemptnumber) AS maxattempt FROM {setask_submission} mxs WHERE mxs.setaskment = :setaskid1 GROUP BY mxs.userid'; $sql = "SELECT mas.id, mas.setaskment,mas.userid," . "mas.timecreated,mas.timemodified,mas.status,mas.groupid,mas.attemptnumber " . "FROM {setask_submission} mas " . "JOIN ( " . $submissionmaxattempt . " ) smx ON mas.userid = smx.userid " . "WHERE mas.setaskment = :setaskid2 AND mas.attemptnumber = smx.maxattempt"; if (!empty($params['status'])) { $placeholders['status'] = $params['status']; $sql = $sql . " AND mas.status = :status"; } if (!empty($params['before'])) { $placeholders['since'] = $params['since']; $placeholders['before'] = $params['before']; $sql = $sql . " AND mas.timemodified BETWEEN :since AND :before"; } else { $placeholders['since'] = $params['since']; $sql = $sql . " AND mas.timemodified >= :since"; } $submissionrecords = $DB->get_records_sql($sql, $placeholders); if (!empty($submissionrecords)) { $fs = get_file_storage(); foreach ($submissionrecords as $submissionrecord) { $submission = array('id' => $submissionrecord->id, 'userid' => $submissionrecord->userid, 'timecreated' => $submissionrecord->timecreated, 'timemodified' => $submissionrecord->timemodified, 'status' => $submissionrecord->status, 'attemptnumber' => $submissionrecord->attemptnumber, 'groupid' => $submissionrecord->groupid); foreach ($submissionplugins as $submissionplugin) { $plugin = array('name' => $submissionplugin->get_name(), 'type' => $submissionplugin->get_type()); // Subtype is 'setasksubmission', type is currently 'file' or 'onlinetext'. $component = $submissionplugin->get_subtype() . '_' . $submissionplugin->get_type(); $fileareas = $submissionplugin->get_file_areas(); foreach ($fileareas as $filearea => $name) { $fileareainfo = array('area' => $filearea); $files = $fs->get_area_files($setask->get_context()->id, $component, $filearea, $submissionrecord->id, "timemodified", false); foreach ($files as $file) { $filepath = $file->get_filepath() . $file->get_filename(); $fileurl = file_encode_url($CFG->wwwroot . '/webservice/pluginfile.php', '/' . $setask->get_context()->id . '/' . $component . '/' . $filearea . '/' . $submissionrecord->id . $filepath); $fileinfo = array('filepath' => $filepath, 'fileurl' => $fileurl); $fileareainfo['files'][] = $fileinfo; } $plugin['fileareas'][] = $fileareainfo; } $editorfields = $submissionplugin->get_editor_fields(); foreach ($editorfields as $name => $description) { $editorfieldinfo = array('name' => $name, 'description' => $description, 'text' => $submissionplugin->get_editor_text($name, $submissionrecord->id), 'format' => $submissionplugin->get_editor_format($name, $submissionrecord->id)); $plugin['editorfields'][] = $editorfieldinfo; } $submission['plugins'][] = $plugin; } $submissions[] = $submission; } } else { $warnings[] = array('item' => 'module', 'itemid' => $setask->get_instance()->id, 'warningcode' => '3', 'message' => 'No submissions found'); } $setaskments[] = array('setaskmentid' => $setask->get_instance()->id, 'submissions' => $submissions); } $result = array('setaskments' => $setaskments, 'warnings' => $warnings); return $result; }
/** * File browsing support for setask module. * * @param file_browser $browser * @param object $areas * @param object $course * @param object $cm * @param object $context * @param string $filearea * @param int $itemid * @param string $filepath * @param string $filename * @return object file_info instance or null if not found */ function setask_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) { global $CFG; require_once $CFG->dirroot . '/mod/setask/locallib.php'; if ($context->contextlevel != CONTEXT_MODULE) { return null; } $urlbase = $CFG->wwwroot . '/pluginfile.php'; $fs = get_file_storage(); $filepath = is_null($filepath) ? '/' : $filepath; $filename = is_null($filename) ? '.' : $filename; // Need to find where this belongs to. $setaskment = new setask($context, $cm, $course); if ($filearea === ASSIGN_INTROATTACHMENT_FILEAREA) { if (!has_capability('moodle/course:managefiles', $context)) { // Students can not peak here! return null; } if (!($storedfile = $fs->get_file($setaskment->get_context()->id, 'mod_setask', $filearea, 0, $filepath, $filename))) { return null; } return new file_info_stored($browser, $setaskment->get_context(), $storedfile, $urlbase, $filearea, $itemid, true, true, false); } $pluginowner = null; foreach ($setaskment->get_submission_plugins() as $plugin) { if ($plugin->is_visible()) { $pluginareas = $plugin->get_file_areas(); if (array_key_exists($filearea, $pluginareas)) { $pluginowner = $plugin; break; } } } if (!$pluginowner) { foreach ($setaskment->get_feedback_plugins() as $plugin) { if ($plugin->is_visible()) { $pluginareas = $plugin->get_file_areas(); if (array_key_exists($filearea, $pluginareas)) { $pluginowner = $plugin; break; } } } } if (!$pluginowner) { return null; } $result = $pluginowner->get_file_info($browser, $filearea, $itemid, $filepath, $filename); return $result; }
/** * Copy annotations, comments, pages, and other required content from the source user to the current group member * being procssed when using applytoall. * * @param int|\setask $setaskment * @param stdClass $grade * @param int $sourceuserid * @return bool */ public static function copy_drafts_from_to($setaskment, $grade, $sourceuserid) { global $DB; // Delete any existing annotations and comments from current user. $DB->delete_records('setaskfeedback_editpdf_annot', array('gradeid' => $grade->id)); $DB->delete_records('setaskfeedback_editpdf_cmnt', array('gradeid' => $grade->id)); // Get gradeid, annotations and comments from sourceuserid. $sourceusergrade = $setaskment->get_user_grade($sourceuserid, true, $grade->attemptnumber); $annotations = $DB->get_records('setaskfeedback_editpdf_annot', array('gradeid' => $sourceusergrade->id, 'draft' => 1)); $comments = $DB->get_records('setaskfeedback_editpdf_cmnt', array('gradeid' => $sourceusergrade->id, 'draft' => 1)); $contextid = $setaskment->get_context()->id; $sourceitemid = $sourceusergrade->id; // Add annotations and comments to current user to generate feedback file. foreach ($annotations as $annotation) { $annotation->gradeid = $grade->id; $DB->insert_record('setaskfeedback_editpdf_annot', $annotation); } foreach ($comments as $comment) { $comment->gradeid = $grade->id; $DB->insert_record('setaskfeedback_editpdf_cmnt', $comment); } $fs = get_file_storage(); // Copy the stamp files. self::replace_files_from_to($fs, $contextid, $sourceitemid, $grade->id, document_services::STAMPS_FILEAREA, true); // Copy the PAGE_IMAGE_FILEAREA files. self::replace_files_from_to($fs, $contextid, $sourceitemid, $grade->id, document_services::PAGE_IMAGE_FILEAREA); return true; }
/** * This function converts all of the base settings for an instance of * the old setaskment to the new format. Then it calls each of the plugins * to see if they can help upgrade this setaskment. * @param int $oldsetaskmentid (don't rely on the old setaskment type even being installed) * @param string $log This string gets appended to during the conversion process * @return bool true or false */ public function upgrade_setaskment($oldsetaskmentid, &$log) { global $DB, $CFG, $USER; // Steps to upgrade an setaskment. core_php_time_limit::raise(ASSIGN_MAX_UPGRADE_TIME_SECS); // Get the module details. $oldmodule = $DB->get_record('modules', array('name' => 'setaskment'), '*', MUST_EXIST); $params = array('module' => $oldmodule->id, 'instance' => $oldsetaskmentid); $oldcoursemodule = $DB->get_record('course_modules', $params, '*', MUST_EXIST); $oldcontext = context_module::instance($oldcoursemodule->id); // We used to check for admin capability, but since Moodle 2.7 this is called // during restore of a mod_setaskment module. // Also note that we do not check for any mod_setaskment capabilities, because they can // be removed so that users don't add new instances of the broken old thing. if (!has_capability('mod/setask:addinstance', $oldcontext)) { $log = get_string('couldnotcreatenewsetaskmentinstance', 'mod_setask'); return false; } // First insert an setask instance to get the id. $oldsetaskment = $DB->get_record('setaskment', array('id' => $oldsetaskmentid), '*', MUST_EXIST); $oldversion = get_config('setaskment_' . $oldsetaskment->setaskmenttype, 'version'); $data = new stdClass(); $data->course = $oldsetaskment->course; $data->name = $oldsetaskment->name; $data->intro = $oldsetaskment->intro; $data->introformat = $oldsetaskment->introformat; $data->alwaysshowdescription = 1; $data->sendnotifications = $oldsetaskment->emailteachers; $data->sendlatenotifications = $oldsetaskment->emailteachers; $data->duedate = $oldsetaskment->timedue; $data->allowsubmissionsfromdate = $oldsetaskment->timeavailable; $data->grade = $oldsetaskment->grade; $data->submissiondrafts = $oldsetaskment->resubmit; $data->requiresubmissionstatement = 0; $data->markingworkflow = 0; $data->markingallocation = 0; $data->cutoffdate = 0; // New way to specify no late submissions. if ($oldsetaskment->preventlate) { $data->cutoffdate = $data->duedate; } $data->teamsubmission = 0; $data->requireallteammemberssubmit = 0; $data->teamsubmissiongroupingid = 0; $data->blindmarking = 0; $data->attemptreopenmethod = 'none'; $data->maxattempts = ASSIGN_UNLIMITED_ATTEMPTS; $newsetaskment = new setask(null, null, null); if (!$newsetaskment->add_instance($data, false)) { $log = get_string('couldnotcreatenewsetaskmentinstance', 'mod_setask'); return false; } // Now create a new coursemodule from the old one. $newmodule = $DB->get_record('modules', array('name' => 'setask'), '*', MUST_EXIST); $newcoursemodule = $this->duplicate_course_module($oldcoursemodule, $newmodule->id, $newsetaskment->get_instance()->id); if (!$newcoursemodule) { $log = get_string('couldnotcreatenewcoursemodule', 'mod_setask'); return false; } // Convert the base database tables (setaskment, submission, grade). // These are used to store information in case a rollback is required. $gradingarea = null; $gradingdefinitions = null; $gradeidmap = array(); $completiondone = false; $gradesdone = false; // From this point we want to rollback on failure. $rollback = false; try { $newsetaskment->set_context(context_module::instance($newcoursemodule->id)); // The course module has now been created - time to update the core tables. // Copy intro files. $newsetaskment->copy_area_files_for_upgrade($oldcontext->id, 'mod_setaskment', 'intro', 0, $newsetaskment->get_context()->id, 'mod_setask', 'intro', 0); // Get the plugins to do their bit. foreach ($newsetaskment->get_submission_plugins() as $plugin) { if ($plugin->can_upgrade($oldsetaskment->setaskmenttype, $oldversion)) { $plugin->enable(); if (!$plugin->upgrade_settings($oldcontext, $oldsetaskment, $log)) { $rollback = true; } } else { $plugin->disable(); } } foreach ($newsetaskment->get_feedback_plugins() as $plugin) { if ($plugin->can_upgrade($oldsetaskment->setaskmenttype, $oldversion)) { $plugin->enable(); if (!$plugin->upgrade_settings($oldcontext, $oldsetaskment, $log)) { $rollback = true; } } else { $plugin->disable(); } } // See if there is advanced grading upgrades required. $gradingarea = $DB->get_record('grading_areas', array('contextid' => $oldcontext->id, 'areaname' => 'submission'), '*', IGNORE_MISSING); if ($gradingarea) { $params = array('id' => $gradingarea->id, 'contextid' => $newsetaskment->get_context()->id, 'component' => 'mod_setask', 'areaname' => 'submissions'); $DB->update_record('grading_areas', $params); $gradingdefinitions = $DB->get_records('grading_definitions', array('areaid' => $gradingarea->id)); } // Upgrade availability data. \core_availability\info::update_dependency_id_across_course($newcoursemodule->course, 'course_modules', $oldcoursemodule->id, $newcoursemodule->id); // Upgrade completion data. $DB->set_field('course_modules_completion', 'coursemoduleid', $newcoursemodule->id, array('coursemoduleid' => $oldcoursemodule->id)); $allcriteria = $DB->get_records('course_completion_criteria', array('moduleinstance' => $oldcoursemodule->id)); foreach ($allcriteria as $criteria) { $criteria->module = 'setask'; $criteria->moduleinstance = $newcoursemodule->id; $DB->update_record('course_completion_criteria', $criteria); } $completiondone = true; // Migrate log entries so we don't lose them. $logparams = array('cmid' => $oldcoursemodule->id, 'course' => $oldcoursemodule->course); $DB->set_field('log', 'module', 'setask', $logparams); $DB->set_field('log', 'cmid', $newcoursemodule->id, $logparams); // Copy all the submission data (and get plugins to do their bit). $oldsubmissions = $DB->get_records('setaskment_submissions', array('setaskment' => $oldsetaskmentid)); foreach ($oldsubmissions as $oldsubmission) { $submission = new stdClass(); $submission->setaskment = $newsetaskment->get_instance()->id; $submission->userid = $oldsubmission->userid; $submission->timecreated = $oldsubmission->timecreated; $submission->timemodified = $oldsubmission->timemodified; $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; // Because in mod_setaskment there could only be one submission per student, it is always the latest. $submission->latest = 1; $submission->id = $DB->insert_record('setask_submission', $submission); if (!$submission->id) { $log .= get_string('couldnotinsertsubmission', 'mod_setask', $submission->userid); $rollback = true; } foreach ($newsetaskment->get_submission_plugins() as $plugin) { if ($plugin->can_upgrade($oldsetaskment->setaskmenttype, $oldversion)) { if (!$plugin->upgrade($oldcontext, $oldsetaskment, $oldsubmission, $submission, $log)) { $rollback = true; } } } if ($oldsubmission->timemarked) { // Submission has been graded - create a grade record. $grade = new stdClass(); $grade->setaskment = $newsetaskment->get_instance()->id; $grade->userid = $oldsubmission->userid; $grade->grader = $oldsubmission->teacher; $grade->timemodified = $oldsubmission->timemarked; $grade->timecreated = $oldsubmission->timecreated; $grade->grade = $oldsubmission->grade; if ($oldsubmission->mailed) { // The mailed flag goes in the flags table. $flags = new stdClass(); $flags->userid = $oldsubmission->userid; $flags->setaskment = $newsetaskment->get_instance()->id; $flags->mailed = 1; $DB->insert_record('setask_user_flags', $flags); } $grade->id = $DB->insert_record('setask_grades', $grade); if (!$grade->id) { $log .= get_string('couldnotinsertgrade', 'mod_setask', $grade->userid); $rollback = true; } // Copy any grading instances. if ($gradingarea) { $gradeidmap[$grade->id] = $oldsubmission->id; foreach ($gradingdefinitions as $definition) { $params = array('definitionid' => $definition->id, 'itemid' => $oldsubmission->id); $DB->set_field('grading_instances', 'itemid', $grade->id, $params); } } foreach ($newsetaskment->get_feedback_plugins() as $plugin) { if ($plugin->can_upgrade($oldsetaskment->setaskmenttype, $oldversion)) { if (!$plugin->upgrade($oldcontext, $oldsetaskment, $oldsubmission, $grade, $log)) { $rollback = true; } } } } } $newsetaskment->update_calendar($newcoursemodule->id); // Reassociate grade_items from the old setaskment instance to the new setask instance. // This includes outcome linked grade_items. $params = array('setask', $newsetaskment->get_instance()->id, 'setaskment', $oldsetaskment->id); $sql = 'UPDATE {grade_items} SET itemmodule = ?, iteminstance = ? WHERE itemmodule = ? AND iteminstance = ?'; $DB->execute($sql, $params); // Create a mapping record to map urls from the old to the new setaskment. $mapping = new stdClass(); $mapping->oldcmid = $oldcoursemodule->id; $mapping->oldinstance = $oldsetaskment->id; $mapping->newcmid = $newcoursemodule->id; $mapping->newinstance = $newsetaskment->get_instance()->id; $mapping->timecreated = time(); $DB->insert_record('setaskment_upgrade', $mapping); $gradesdone = true; } catch (Exception $exception) { $rollback = true; $log .= get_string('conversionexception', 'mod_setask', $exception->getMessage()); } if ($rollback) { // Roll back the grades changes. if ($gradesdone) { // Reassociate grade_items from the new setask instance to the old setaskment instance. $params = array('setaskment', $oldsetaskment->id, 'setask', $newsetaskment->get_instance()->id); $sql = 'UPDATE {grade_items} SET itemmodule = ?, iteminstance = ? WHERE itemmodule = ? AND iteminstance = ?'; $DB->execute($sql, $params); } // Roll back the completion changes. if ($completiondone) { $DB->set_field('course_modules_completion', 'coursemoduleid', $oldcoursemodule->id, array('coursemoduleid' => $newcoursemodule->id)); $allcriteria = $DB->get_records('course_completion_criteria', array('moduleinstance' => $newcoursemodule->id)); foreach ($allcriteria as $criteria) { $criteria->module = 'setaskment'; $criteria->moduleinstance = $oldcoursemodule->id; $DB->update_record('course_completion_criteria', $criteria); } } // Roll back the log changes. $logparams = array('cmid' => $newcoursemodule->id, 'course' => $newcoursemodule->course); $DB->set_field('log', 'module', 'setaskment', $logparams); $DB->set_field('log', 'cmid', $oldcoursemodule->id, $logparams); // Roll back the advanced grading update. if ($gradingarea) { foreach ($gradeidmap as $newgradeid => $oldsubmissionid) { foreach ($gradingdefinitions as $definition) { $DB->set_field('grading_instances', 'itemid', $oldsubmissionid, array('definitionid' => $definition->id, 'itemid' => $newgradeid)); } } $params = array('id' => $gradingarea->id, 'contextid' => $oldcontext->id, 'component' => 'mod_setaskment', 'areaname' => 'submission'); $DB->update_record('grading_areas', $params); } $newsetaskment->delete_instance(); return false; } // Delete the old setaskment (use object delete). $cm = get_coursemodule_from_id('', $oldcoursemodule->id, $oldcoursemodule->course); if ($cm) { course_delete_module($cm->id); } rebuild_course_cache($oldcoursemodule->course); return true; }
/** * Process an uploaded zip file * * @param setask $setaskment - The setaskment instance * @param setask_feedback_file $fileplugin - The file feedback plugin * @return string - The html response */ public function import_zip_files($setaskment, $fileplugin) { global $CFG, $PAGE, $DB; core_php_time_limit::raise(ASSIGNFEEDBACK_FILE_MAXFILEUNZIPTIME); $packer = get_file_packer('application/zip'); $feedbackfilesupdated = 0; $feedbackfilesadded = 0; $userswithnewfeedback = array(); $contextid = $setaskment->get_context()->id; $fs = get_file_storage(); $files = $this->get_import_files($contextid); $currentgroup = groups_get_activity_group($setaskment->get_course_module(), true); $allusers = $setaskment->list_participants($currentgroup, false); $participants = array(); foreach ($allusers as $user) { $participants[$setaskment->get_uniqueid_for_user($user->id)] = $user; } foreach ($files as $unzippedfile) { // Set the timeout for unzipping each file. $user = null; $plugin = null; $filename = ''; if ($this->is_valid_filename_for_import($setaskment, $unzippedfile, $participants, $user, $plugin, $filename)) { if ($this->is_file_modified($setaskment, $user, $plugin, $filename, $unzippedfile)) { $grade = $setaskment->get_user_grade($user->id, true); if ($oldfile = $fs->get_file($contextid, 'setaskfeedback_file', ASSIGNFEEDBACK_FILE_FILEAREA, $grade->id, '/', $filename)) { // Update existing feedback file. $oldfile->replace_file_with($unzippedfile); $feedbackfilesupdated++; } else { // Create a new feedback file. $newfilerecord = new stdClass(); $newfilerecord->contextid = $contextid; $newfilerecord->component = 'setaskfeedback_file'; $newfilerecord->filearea = ASSIGNFEEDBACK_FILE_FILEAREA; $newfilerecord->filename = $filename; $newfilerecord->filepath = '/'; $newfilerecord->itemid = $grade->id; $fs->create_file_from_storedfile($newfilerecord, $unzippedfile); $feedbackfilesadded++; } $userswithnewfeedback[$user->id] = 1; // Update the number of feedback files for this user. $fileplugin->update_file_count($grade); // Update the last modified time on the grade which will trigger student notifications. $setaskment->notify_grade_modified($grade); } } } require_once $CFG->dirroot . '/mod/setask/feedback/file/renderable.php'; $importsummary = new setaskfeedback_file_import_summary($setaskment->get_course_module()->id, count($userswithnewfeedback), $feedbackfilesadded, $feedbackfilesupdated); $setaskrenderer = $setaskment->get_renderer(); $renderer = $PAGE->get_renderer('setaskfeedback_file'); $o = ''; $o .= $setaskrenderer->render(new setask_header($setaskment->get_instance(), $setaskment->get_context(), false, $setaskment->get_course_module()->id, get_string('uploadzipsummary', 'setaskfeedback_file'))); $o .= $renderer->render($importsummary); $o .= $setaskrenderer->render_footer(); return $o; }