Esempio n. 1
0
/**
 * This standard function will check all instances of this module
 * and make sure there are up-to-date events created for each of them.
 * If courseid = 0, then every seplment event in the site is checked, else
 * only seplment events belonging to the course specified are checked.
 *
 * @param int $courseid
 * @return bool
 */
function sepl_refresh_events($courseid = 0)
{
    global $CFG, $DB;
    require_once $CFG->dirroot . '/mod/sepl/locallib.php';
    if ($courseid) {
        // Make sure that the course id is numeric.
        if (!is_numeric($courseid)) {
            return false;
        }
        if (!($sepls = $DB->get_records('sepl', array('course' => $courseid)))) {
            return false;
        }
        // Get course from courseid parameter.
        if (!($course = $DB->get_record('course', array('id' => $courseid), '*'))) {
            return false;
        }
    } else {
        if (!($sepls = $DB->get_records('sepl'))) {
            return false;
        }
    }
    foreach ($sepls as $sepl) {
        // Use seplment's course column if courseid parameter is not given.
        if (!$courseid) {
            $courseid = $sepl->course;
            if (!($course = $DB->get_record('course', array('id' => $courseid), '*'))) {
                continue;
            }
        }
        if (!($cm = get_coursemodule_from_instance('sepl', $sepl->id, $courseid, false))) {
            continue;
        }
        $context = context_module::instance($cm->id);
        $seplment = new sepl($context, $cm, $course);
        $seplment->update_calendar($cm->id);
    }
    return true;
}
 /**
  * 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;
 }
Esempio n. 3
0
 /**
  * Finds all seplment notifications that have yet to be mailed out, and mails them.
  *
  * Cron function to be run periodically according to the moodle cron.
  *
  * @return bool
  */
 public static function cron()
 {
     global $DB;
     // Only ever send a max of one days worth of updates.
     $yesterday = time() - 24 * 3600;
     $timenow = time();
     $lastcron = $DB->get_field('modules', 'lastcron', array('name' => 'sepl'));
     // Collect all submissions that require mailing.
     // Submissions are included if all are true:
     //   - The seplment is visible in the gradebook.
     //   - No previous notification has been sent.
     //   - If marking workflow is not enabled, the grade was updated in the past 24 hours, or
     //     if marking workflow is enabled, the workflow state is at 'released'.
     $sql = "SELECT g.id as gradeid, a.course, a.name, a.blindmarking, a.revealidentities,\n                       g.*, g.timemodified as lastmodified, cm.id as cmid\n                 FROM {sepl} a\n                 JOIN {sepl_grades} g ON g.seplment = a.id\n            LEFT JOIN {sepl_user_flags} uf ON uf.seplment = a.id AND uf.userid = g.userid\n                 JOIN {course_modules} cm ON cm.course = a.course AND cm.instance = a.id\n                 JOIN {modules} md ON md.id = cm.module AND md.name = 'sepl'\n                 JOIN {grade_items} gri ON gri.iteminstance = a.id AND gri.courseid = a.course AND gri.itemmodule = md.name\n                 WHERE ((a.markingworkflow = 0 AND g.timemodified >= :yesterday AND g.timemodified <= :today) OR\n                        (a.markingworkflow = 1 AND uf.workflowstate = :wfreleased)) AND\n                       uf.mailed = 0 AND gri.hidden = 0\n              ORDER BY a.course, cm.id";
     $params = array('yesterday' => $yesterday, 'today' => $timenow, 'wfreleased' => ASSIGN_MARKING_WORKFLOW_STATE_RELEASED);
     $submissions = $DB->get_records_sql($sql, $params);
     if (!empty($submissions)) {
         mtrace('Processing ' . count($submissions) . ' seplment submissions ...');
         // Preload courses we are going to need those.
         $courseids = array();
         foreach ($submissions as $submission) {
             $courseids[] = $submission->course;
         }
         // Filter out duplicates.
         $courseids = array_unique($courseids);
         $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
         list($courseidsql, $params) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED);
         $sql = 'SELECT c.*, ' . $ctxselect . ' FROM {course} c
              LEFT JOIN {context} ctx ON ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel
                  WHERE c.id ' . $courseidsql;
         $params['contextlevel'] = CONTEXT_COURSE;
         $courses = $DB->get_records_sql($sql, $params);
         // Clean up... this could go on for a while.
         unset($courseids);
         unset($ctxselect);
         unset($courseidsql);
         unset($params);
         // Message students about new feedback.
         foreach ($submissions as $submission) {
             mtrace("Processing seplment submission {$submission->id} ...");
             // Do not cache user lookups - could be too many.
             if (!($user = $DB->get_record('user', array('id' => $submission->userid)))) {
                 mtrace('Could not find user ' . $submission->userid);
                 continue;
             }
             // Use a cache to prevent the same DB queries happening over and over.
             if (!array_key_exists($submission->course, $courses)) {
                 mtrace('Could not find course ' . $submission->course);
                 continue;
             }
             $course = $courses[$submission->course];
             if (isset($course->ctxid)) {
                 // Context has not yet been preloaded. Do so now.
                 context_helper::preload_from_record($course);
             }
             // Override the language and timezone of the "current" user, so that
             // mail is customised for the receiver.
             cron_setup_user($user, $course);
             // Context lookups are already cached.
             $coursecontext = context_course::instance($course->id);
             if (!is_enrolled($coursecontext, $user->id)) {
                 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
                 mtrace(fullname($user) . ' not an active participant in ' . $courseshortname);
                 continue;
             }
             if (!($grader = $DB->get_record('user', array('id' => $submission->grader)))) {
                 mtrace('Could not find grader ' . $submission->grader);
                 continue;
             }
             $modinfo = get_fast_modinfo($course, $user->id);
             $cm = $modinfo->get_cm($submission->cmid);
             // Context lookups are already cached.
             $contextmodule = context_module::instance($cm->id);
             if (!$cm->uservisible) {
                 // Hold mail notification for seplments the user cannot access until later.
                 continue;
             }
             // Need to send this to the student.
             $messagetype = 'feedbackavailable';
             $eventtype = 'sepl_notification';
             $updatetime = $submission->lastmodified;
             $modulename = get_string('modulename', 'sepl');
             $uniqueid = 0;
             if ($submission->blindmarking && !$submission->revealidentities) {
                 $uniqueid = self::get_uniqueid_for_user_static($submission->seplment, $user->id);
             }
             $showusers = $submission->blindmarking && !$submission->revealidentities;
             self::send_seplment_notification($grader, $user, $messagetype, $eventtype, $updatetime, $cm, $contextmodule, $course, $modulename, $submission->name, $showusers, $uniqueid);
             $flags = $DB->get_record('sepl_user_flags', array('userid' => $user->id, 'seplment' => $submission->seplment));
             if ($flags) {
                 $flags->mailed = 1;
                 $DB->update_record('sepl_user_flags', $flags);
             } else {
                 $flags = new stdClass();
                 $flags->userid = $user->id;
                 $flags->seplment = $submission->seplment;
                 $flags->mailed = 1;
                 $DB->insert_record('sepl_user_flags', $flags);
             }
             mtrace('Done');
         }
         mtrace('Done processing ' . count($submissions) . ' seplment submissions');
         cron_setup_user();
         // Free up memory just to be sure.
         unset($courses);
     }
     // Update calendar events to provide a description.
     $sql = 'SELECT id
                 FROM {sepl}
                 WHERE
                     allowsubmissionsfromdate >= :lastcron AND
                     allowsubmissionsfromdate <= :timenow AND
                     alwaysshowdescription = 0';
     $params = array('lastcron' => $lastcron, 'timenow' => $timenow);
     $newlyavailable = $DB->get_records_sql($sql, $params);
     foreach ($newlyavailable as $record) {
         $cm = get_coursemodule_from_instance('sepl', $record->id, 0, false, MUST_EXIST);
         $context = context_module::instance($cm->id);
         $seplment = new sepl($context, null, null);
         $seplment->update_calendar($cm->id);
     }
     return true;
 }