/** * Triggered when the '\mod_teamwork\event\course_module_viewed' event is triggered. * * This does the same job as {@link teamworkallocation_scheduled_cron()} but for the * single teamwork. The idea is that we do not need to wait for cron to execute. * Displaying the teamwork main view.php can trigger the scheduled allocation, too. * * @param \mod_teamwork\event\course_module_viewed $event * @return bool */ public static function teamwork_viewed($event) { global $DB, $CFG; require_once $CFG->dirroot . '/mod/teamwork/locallib.php'; $teamwork = $event->get_record_snapshot('teamwork', $event->objectid); $course = $event->get_record_snapshot('course', $event->courseid); $cm = $event->get_record_snapshot('course_modules', $event->contextinstanceid); $teamwork = new \teamwork($teamwork, $cm, $course); $now = time(); // Non-expensive check to see if the scheduled allocation can even happen. if ($teamwork->phase == \teamwork::PHASE_SUBMISSION and $teamwork->submissionend > 0 and $teamwork->submissionend < $now) { // Make sure the scheduled allocation has been configured for this teamwork, that it has not // been executed yet and that the passed teamwork record is still valid. $sql = "SELECT a.id\n FROM {teamworkallocation_scheduled} a\n JOIN {teamwork} w ON a.teamworkid = w.id\n WHERE w.id = :teamworkid\n AND a.enabled = 1\n AND w.phase = :phase\n AND w.submissionend > 0\n AND w.submissionend < :now\n AND (a.timeallocated IS NULL OR a.timeallocated < w.submissionend)"; $params = array('teamworkid' => $teamwork->id, 'phase' => \teamwork::PHASE_SUBMISSION, 'now' => $now); if ($DB->record_exists_sql($sql, $params)) { // Allocate submissions for assessments. $allocator = $teamwork->allocator_instance('scheduled'); $result = $allocator->execute(); // Todo inform the teachers about the results. } } return true; }
/** * Regular jobs to execute via cron */ function teamworkallocation_scheduled_cron() { global $CFG, $DB; $sql = "SELECT w.*\n FROM {teamworkallocation_scheduled} a\n JOIN {teamwork} w ON a.teamworkid = w.id\n WHERE a.enabled = 1\n AND w.phase = 20\n AND w.submissionend > 0\n AND w.submissionend < ?\n AND (a.timeallocated IS NULL OR a.timeallocated < w.submissionend)"; $teamworks = $DB->get_records_sql($sql, array(time())); if (empty($teamworks)) { mtrace('... no teamworks awaiting scheduled allocation. ', ''); return; } mtrace('... executing scheduled allocation in ' . count($teamworks) . ' teamwork(s) ... ', ''); // let's have some fun! require_once $CFG->dirroot . '/mod/teamwork/locallib.php'; foreach ($teamworks as $teamwork) { $cm = get_coursemodule_from_instance('teamwork', $teamwork->id, $teamwork->course, false, MUST_EXIST); $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); $teamwork = new teamwork($teamwork, $cm, $course); $allocator = $teamwork->allocator_instance('scheduled'); $result = $allocator->execute(); // todo inform the teachers about the results } }
$cmid = required_param('cmid', PARAM_INT); // course module $method = optional_param('method', 'manual', PARAM_ALPHA); // method to use $cm = get_coursemodule_from_id('teamwork', $cmid, 0, false, MUST_EXIST); $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); $teamwork = $DB->get_record('teamwork', array('id' => $cm->instance), '*', MUST_EXIST); $teamwork = new teamwork($teamwork, $cm, $course); $PAGE->set_url($teamwork->allocation_url($method)); require_login($course, false, $cm); $context = $PAGE->context; require_capability('mod/teamwork:allocate', $context); $PAGE->set_title($teamwork->name); $PAGE->set_heading($course->fullname); $PAGE->navbar->add(get_string('allocation', 'teamwork')); $allocator = $teamwork->allocator_instance($method); $initresult = $allocator->init(); // // Output starts here // $output = $PAGE->get_renderer('mod_teamwork'); echo $output->header(); echo $OUTPUT->heading(format_string($teamwork->name)); $allocators = teamwork::installed_allocators(); if (!empty($allocators)) { $tabs = array(); $row = array(); $inactive = array(); $activated = array(); foreach ($allocators as $methodid => $methodname) { $row[] = new tabobject($methodid, $teamwork->allocation_url($methodid)->out(), $methodname);