/** * Serves seplment submissions and other files. * * @param mixed $course course or id of the course * @param mixed $cm course module or id of the course module * @param context $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return bool false if file not found, does not return if found - just send the file */ function seplsubmission_onlinetext_pluginfile($course, $cm, context $context, $filearea, $args, $forcedownload) { global $DB, $CFG; if ($context->contextlevel != CONTEXT_MODULE) { return false; } require_login($course, false, $cm); $itemid = (int) array_shift($args); $record = $DB->get_record('sepl_submission', array('id' => $itemid), 'userid, seplment, groupid', MUST_EXIST); $userid = $record->userid; $groupid = $record->groupid; require_once $CFG->dirroot . '/mod/sepl/locallib.php'; $sepl = new sepl($context, $cm, $course); if ($sepl->get_instance()->id != $record->seplment) { return false; } if ($sepl->get_instance()->teamsubmission && !$sepl->can_view_group_submission($groupid)) { return false; } if (!$sepl->get_instance()->teamsubmission && !$sepl->can_view_submission($userid)) { return false; } $relativepath = implode('/', $args); $fullpath = "/{$context->id}/seplsubmission_onlinetext/{$filearea}/{$itemid}/{$relativepath}"; $fs = get_file_storage(); if (!($file = $fs->get_file_by_hash(sha1($fullpath))) || $file->is_directory()) { return false; } // Download MUST be forced - security! send_stored_file($file, 0, 0, true); }
/** * Get a configuration value for this plugin * * @param mixed $setting The config key (string) or null * @return mixed string | false */ public final function get_config($setting = null) { global $DB; if ($setting) { if (!$this->seplment->has_instance()) { return false; } $seplment = $this->seplment->get_instance(); if ($seplment) { $dbparams = array('seplment' => $seplment->id, 'subtype' => $this->get_subtype(), 'plugin' => $this->get_type(), 'name' => $setting); $result = $DB->get_record('sepl_plugin_config', $dbparams, '*', IGNORE_MISSING); if ($result) { return $result->value; } } return false; } $dbparams = array('seplment' => $this->seplment->get_instance()->id, 'subtype' => $this->get_subtype(), 'plugin' => $this->get_type()); $results = $DB->get_records('sepl_plugin_config', $dbparams); $config = new stdClass(); if (is_array($results)) { foreach ($results as $setting) { $name = $setting->name; $config->{$name} = $setting->value; } } return $config; }
/** * Create instance of event. * * @since Moodle 2.7 * * @param \sepl $sepl * @param \stdClass $submission * @return submission_status_updated */ public static function create_from_submission(\sepl $sepl, \stdClass $submission) { $data = array('context' => $sepl->get_context(), 'objectid' => $submission->id, 'relateduserid' => $sepl->get_instance()->teamsubmission ? null : $submission->userid, 'other' => array('newstatus' => $submission->status)); /** @var submission_status_updated $event */ $event = self::create($data); $event->set_sepl($sepl); $event->add_record_snapshot('sepl_submission', $submission); return $event; }
/** * Create instance of event. * * @param \sepl $sepl * @param \stdClass $grade * @return feedback_viewed */ public static function create_from_grade(\sepl $sepl, \stdClass $grade) { $data = array('objectid' => $grade->id, 'relateduserid' => $grade->userid, 'context' => $sepl->get_context(), 'other' => array('seplid' => $sepl->get_instance()->id)); /** @var feedback_viewed $event */ $event = self::create($data); $event->set_sepl($sepl); $event->add_record_snapshot('sepl_grades', $grade); return $event; }
/** * Create instance of event. * * @param \sepl $sepl * @return reveal_identities_confirmation_page_viewed */ public static function create_from_sepl(\sepl $sepl) { $data = array('context' => $sepl->get_context(), 'other' => array('seplid' => $sepl->get_instance()->id)); self::$preventcreatecall = false; /** @var reveal_identities_confirmation_page_viewed $event */ $event = self::create($data); self::$preventcreatecall = true; $event->set_sepl($sepl); return $event; }
/** * Create instance of event. * * @since Moodle 2.7 * * @param \sepl $sepl * @return identities_revealed */ public static function create_from_sepl(\sepl $sepl) { $data = array('context' => $sepl->get_context(), 'objectid' => $sepl->get_instance()->id); self::$preventcreatecall = false; /** @var identities_revealed $event */ $event = self::create($data); self::$preventcreatecall = true; $event->set_sepl($sepl); return $event; }
/** * Create instance of event. * * @since Moodle 2.7 * * @param \sepl $sepl * @param int $userid * @return extension_granted */ public static function create_from_sepl(\sepl $sepl, $userid) { $data = array('context' => $sepl->get_context(), 'objectid' => $sepl->get_instance()->id, 'relateduserid' => $userid); self::$preventcreatecall = false; /** @var extension_granted $event */ $event = self::create($data); self::$preventcreatecall = true; $event->set_sepl($sepl); return $event; }
/** * Create instance of event. * * @since Moodle 2.7 * * @param \sepl $sepl * @param \stdClass $user * @return submission_unlocked */ public static function create_from_user(\sepl $sepl, \stdClass $user) { $data = array('context' => $sepl->get_context(), 'objectid' => $sepl->get_instance()->id, 'relateduserid' => $user->id); self::$preventcreatecall = false; /** @var submission_unlocked $event */ $event = self::create($data); self::$preventcreatecall = true; $event->set_sepl($sepl); $event->add_record_snapshot('user', $user); return $event; }
/** * Create instance of event. * * @param \sepl $sepl * @param \stdClass $user * @return grading_form_viewed */ public static function create_from_user(\sepl $sepl, \stdClass $user) { $data = array('relateduserid' => $user->id, 'context' => $sepl->get_context(), 'other' => array('seplid' => $sepl->get_instance()->id)); self::$preventcreatecall = false; /** @var grading_form_viewed $event */ $event = self::create($data); self::$preventcreatecall = true; $event->set_sepl($sepl); $event->add_record_snapshot('user', $user); return $event; }
/** * Save multiple student grades for a single seplment. * * @param int $seplmentid The id of the seplment * @param boolean $applytoall If set to true and this is a team seplment, * apply the grade to all members of the group * @param array $grades grade data for one or more students that includes * userid - The id of the student being graded * grade - The grade (ignored if the seplment uses advanced grading) * attemptnumber - The attempt number * addattempt - Allow another attempt * workflowstate - New workflow state * plugindata - Custom data used by plugins * advancedgradingdata - Optional Advanced grading data * @throws invalid_parameter_exception if multiple grades are supplied for * a team seplment that has $applytoall set to true * @return null * @since Moodle 2.7 */ public static function save_grades($seplmentid, $applytoall = false, $grades) { global $CFG, $USER; require_once "{$CFG->dirroot}/mod/sepl/locallib.php"; $params = self::validate_parameters(self::save_grades_parameters(), array('seplmentid' => $seplmentid, 'applytoall' => $applytoall, 'grades' => $grades)); $cm = get_coursemodule_from_instance('sepl', $params['seplmentid'], 0, false, MUST_EXIST); $context = context_module::instance($cm->id); self::validate_context($context); $seplment = new sepl($context, $cm, null); if ($seplment->get_instance()->teamsubmission && $params['applytoall']) { // Check that only 1 user per submission group is provided. $groupids = array(); foreach ($params['grades'] as $gradeinfo) { $group = $seplment->get_submission_group($gradeinfo['userid']); if (in_array($group->id, $groupids)) { throw new invalid_parameter_exception('Multiple grades for the same team have been supplied ' . ' this is not permitted when the applytoall flag is set'); } else { $groupids[] = $group->id; } } } foreach ($params['grades'] as $gradeinfo) { $gradedata = (object) $gradeinfo['plugindata']; $gradedata->addattempt = $gradeinfo['addattempt']; $gradedata->attemptnumber = $gradeinfo['attemptnumber']; $gradedata->workflowstate = $gradeinfo['workflowstate']; $gradedata->applytoall = $params['applytoall']; $gradedata->grade = $gradeinfo['grade']; if (!empty($gradeinfo['advancedgradingdata'])) { $advancedgrading = array(); $criteria = reset($gradeinfo['advancedgradingdata']); foreach ($criteria as $key => $criterion) { $details = array(); foreach ($criterion as $value) { foreach ($value['fillings'] as $filling) { $details[$value['criterionid']] = $filling; } } $advancedgrading[$key] = $details; } $gradedata->advancedgrading = $advancedgrading; } $seplment->save_grade($gradeinfo['userid'], $gradedata); } return null; }
/** * This function converts all of the base settings for an instance of * the old seplment to the new format. Then it calls each of the plugins * to see if they can help upgrade this seplment. * @param int $oldseplmentid (don't rely on the old seplment type even being installed) * @param string $log This string gets appended to during the conversion process * @return bool true or false */ public function upgrade_seplment($oldseplmentid, &$log) { global $DB, $CFG, $USER; // Steps to upgrade an seplment. core_php_time_limit::raise(ASSIGN_MAX_UPGRADE_TIME_SECS); // Get the module details. $oldmodule = $DB->get_record('modules', array('name' => 'seplment'), '*', MUST_EXIST); $params = array('module' => $oldmodule->id, 'instance' => $oldseplmentid); $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_seplment module. // Also note that we do not check for any mod_seplment capabilities, because they can // be removed so that users don't add new instances of the broken old thing. if (!has_capability('mod/sepl:addinstance', $oldcontext)) { $log = get_string('couldnotcreatenewseplmentinstance', 'mod_sepl'); return false; } // First insert an sepl instance to get the id. $oldseplment = $DB->get_record('seplment', array('id' => $oldseplmentid), '*', MUST_EXIST); $oldversion = get_config('seplment_' . $oldseplment->seplmenttype, 'version'); $data = new stdClass(); $data->course = $oldseplment->course; $data->name = $oldseplment->name; $data->intro = $oldseplment->intro; $data->introformat = $oldseplment->introformat; $data->alwaysshowdescription = 1; $data->sendnotifications = $oldseplment->emailteachers; $data->sendlatenotifications = $oldseplment->emailteachers; $data->duedate = $oldseplment->timedue; $data->allowsubmissionsfromdate = $oldseplment->timeavailable; $data->grade = $oldseplment->grade; $data->submissiondrafts = $oldseplment->resubmit; $data->requiresubmissionstatement = 0; $data->markingworkflow = 0; $data->markingallocation = 0; $data->cutoffdate = 0; // New way to specify no late submissions. if ($oldseplment->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; $newseplment = new sepl(null, null, null); if (!$newseplment->add_instance($data, false)) { $log = get_string('couldnotcreatenewseplmentinstance', 'mod_sepl'); return false; } // Now create a new coursemodule from the old one. $newmodule = $DB->get_record('modules', array('name' => 'sepl'), '*', MUST_EXIST); $newcoursemodule = $this->duplicate_course_module($oldcoursemodule, $newmodule->id, $newseplment->get_instance()->id); if (!$newcoursemodule) { $log = get_string('couldnotcreatenewcoursemodule', 'mod_sepl'); return false; } // Convert the base database tables (seplment, 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 { $newseplment->set_context(context_module::instance($newcoursemodule->id)); // The course module has now been created - time to update the core tables. // Copy intro files. $newseplment->copy_area_files_for_upgrade($oldcontext->id, 'mod_seplment', 'intro', 0, $newseplment->get_context()->id, 'mod_sepl', 'intro', 0); // Get the plugins to do their bit. foreach ($newseplment->get_submission_plugins() as $plugin) { if ($plugin->can_upgrade($oldseplment->seplmenttype, $oldversion)) { $plugin->enable(); if (!$plugin->upgrade_settings($oldcontext, $oldseplment, $log)) { $rollback = true; } } else { $plugin->disable(); } } foreach ($newseplment->get_feedback_plugins() as $plugin) { if ($plugin->can_upgrade($oldseplment->seplmenttype, $oldversion)) { $plugin->enable(); if (!$plugin->upgrade_settings($oldcontext, $oldseplment, $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' => $newseplment->get_context()->id, 'component' => 'mod_sepl', '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 = 'sepl'; $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', 'sepl', $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('seplment_submissions', array('seplment' => $oldseplmentid)); foreach ($oldsubmissions as $oldsubmission) { $submission = new stdClass(); $submission->seplment = $newseplment->get_instance()->id; $submission->userid = $oldsubmission->userid; $submission->timecreated = $oldsubmission->timecreated; $submission->timemodified = $oldsubmission->timemodified; $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; // Because in mod_seplment there could only be one submission per student, it is always the latest. $submission->latest = 1; $submission->id = $DB->insert_record('sepl_submission', $submission); if (!$submission->id) { $log .= get_string('couldnotinsertsubmission', 'mod_sepl', $submission->userid); $rollback = true; } foreach ($newseplment->get_submission_plugins() as $plugin) { if ($plugin->can_upgrade($oldseplment->seplmenttype, $oldversion)) { if (!$plugin->upgrade($oldcontext, $oldseplment, $oldsubmission, $submission, $log)) { $rollback = true; } } } if ($oldsubmission->timemarked) { // Submission has been graded - create a grade record. $grade = new stdClass(); $grade->seplment = $newseplment->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->seplment = $newseplment->get_instance()->id; $flags->mailed = 1; $DB->insert_record('sepl_user_flags', $flags); } $grade->id = $DB->insert_record('sepl_grades', $grade); if (!$grade->id) { $log .= get_string('couldnotinsertgrade', 'mod_sepl', $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 ($newseplment->get_feedback_plugins() as $plugin) { if ($plugin->can_upgrade($oldseplment->seplmenttype, $oldversion)) { if (!$plugin->upgrade($oldcontext, $oldseplment, $oldsubmission, $grade, $log)) { $rollback = true; } } } } } $newseplment->update_calendar($newcoursemodule->id); // Reassociate grade_items from the old seplment instance to the new sepl instance. // This includes outcome linked grade_items. $params = array('sepl', $newseplment->get_instance()->id, 'seplment', $oldseplment->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 seplment. $mapping = new stdClass(); $mapping->oldcmid = $oldcoursemodule->id; $mapping->oldinstance = $oldseplment->id; $mapping->newcmid = $newcoursemodule->id; $mapping->newinstance = $newseplment->get_instance()->id; $mapping->timecreated = time(); $DB->insert_record('seplment_upgrade', $mapping); $gradesdone = true; } catch (Exception $exception) { $rollback = true; $log .= get_string('conversionexception', 'mod_sepl', $exception->getMessage()); } if ($rollback) { // Roll back the grades changes. if ($gradesdone) { // Reassociate grade_items from the new sepl instance to the old seplment instance. $params = array('seplment', $oldseplment->id, 'sepl', $newseplment->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 = 'seplment'; $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', 'seplment', $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_seplment', 'areaname' => 'submission'); $DB->update_record('grading_areas', $params); } $newseplment->delete_instance(); return false; } // Delete the old seplment (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; }
/** * Obtains the automatic completion state for this module based on any conditions * in sepl settings. * * @param object $course Course * @param object $cm Course-module * @param int $userid User ID * @param bool $type Type of comparison (or/and; can be used as return value if no conditions) * @return bool True if completed, false if not, $type if conditions not set. */ function sepl_get_completion_state($course, $cm, $userid, $type) { global $CFG, $DB; require_once $CFG->dirroot . '/mod/sepl/locallib.php'; $sepl = new sepl(null, $cm, $course); // If completion option is enabled, evaluate it and return true/false. if ($sepl->get_instance()->completionsubmit) { $submission = $sepl->get_user_submission($userid, false); return $submission && $submission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED; } else { // Completion option is not enabled so just return $type. return $type; } }
/** * Callback called by comment::get_comments() and comment::add(). Gives an opportunity to enforce blind-marking. * * @param array $comments * @param stdClass $options * @return array * @throws comment_exception */ function seplsubmission_comments_comment_display($comments, $options) { global $CFG, $DB, $USER; if ($options->commentarea != 'submission_comments' && $options->commentarea != 'submission_comments_upgrade') { throw new comment_exception('invalidcommentarea'); } if (!($submission = $DB->get_record('sepl_submission', array('id' => $options->itemid)))) { throw new comment_exception('invalidcommentitemid'); } $context = $options->context; $cm = $options->cm; $course = $options->courseid; require_once $CFG->dirroot . '/mod/sepl/locallib.php'; $seplment = new sepl($context, $cm, $course); if ($seplment->get_instance()->id != $submission->seplment) { throw new comment_exception('invalidcontext'); } if ($seplment->is_blind_marking() && !empty($comments)) { // Blind marking is being used, may need to map unique anonymous ids to the comments. $usermappings = array(); $hiddenuserstr = trim(get_string('hiddenuser', 'sepl')); $guestuser = guest_user(); foreach ($comments as $comment) { // Anonymize the comments. if (empty($usermappings[$comment->userid])) { // The blind-marking information for this commenter has not been generated; do so now. $anonid = $seplment->get_uniqueid_for_user($comment->userid); $commenter = new stdClass(); $commenter->firstname = $hiddenuserstr; $commenter->lastname = $anonid; $commenter->picture = 0; $commenter->id = $guestuser->id; $commenter->email = $guestuser->email; $commenter->imagealt = $guestuser->imagealt; // Temporarily store blind-marking information for use in later comments if necessary. $usermappings[$comment->userid]->fullname = fullname($commenter); $usermappings[$comment->userid]->avatar = $seplment->get_renderer()->user_picture($commenter, array('size' => 18, 'link' => false)); } // Set blind-marking information for this comment. $comment->fullname = $usermappings[$comment->userid]->fullname; $comment->avatar = $usermappings[$comment->userid]->avatar; $comment->profileurl = null; } } return $comments; }
/** * Return things to the renderer. * * @return string the seplment name */ public function get_seplment_name() { return $this->seplment->get_instance()->name; }