Exemplo n.º 1
0
/**
 * Adds an setaskment instance
 *
 * This is done by calling the add_instance() method of the setaskment type class
 * @param stdClass $data
 * @param mod_setask_mod_form $form
 * @return int The instance id of the new setaskment
 */
function setask_add_instance(stdClass $data, mod_setask_mod_form $form = null)
{
    global $CFG;
    require_once $CFG->dirroot . '/mod/setask/locallib.php';
    $setaskment = new setask(context_module::instance($data->coursemodule), null, null);
    return $setaskment->add_instance($data, true);
}
Exemplo n.º 2
0
 /**
  * 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;
 }