MDL-32235 execute scheduled allocation by cron

This commit is contained in:
David Mudrak 2012-03-30 12:57:43 +02:00
parent 782a35e225
commit 2a3aac40e4
4 changed files with 125 additions and 2 deletions

View File

@ -137,6 +137,13 @@ class workshop_allocation_result implements renderable {
return $this->message;
}
/**
* @return int|null the timestamp of when the final status was set
*/
public function get_timeend() {
return $this->timeend;
}
/**
* Appends a new message to the log
*

View File

@ -36,5 +36,12 @@ Internally, the random allocation method is executed with the parameters pre-def
Note that the scheduled allocation is *not* executed if you manually switch the workshop into the assessment phase before the submissions deadline. You have to allocate submissions yourself in that case. The scheduled allocation method is particularly useful when used together with the automatic phase switching feature.';
$string['nosubmissionend'] = 'You have to define the submissions deadline in the workshop settings in order to use scheduled allocation. Press the \'Continue\' button to edit the workshop settings.';
$string['pluginname'] = 'Scheduled allocation';
$string['resultdisabled'] = 'Scheduled allocation DISABLED';
$string['resultenabled'] = 'Scheduled allocation ENABLED';
$string['resultdisabled'] = 'Scheduled allocation disabled';
$string['resultenabled'] = 'Scheduled allocation enabled';
$string['resultfailed'] = 'Unable to automatically allocate submissions';
$string['resultfailedconfig'] = 'Scheduled allocation misconfigured';
$string['resultfaileddeadline'] = 'Workshop does not have the submissions deadline defined';
$string['resultfailedphase'] = 'Workshop not in the submission phase';
$string['resultvoid'] = 'No submissions were allocated';
$string['resultvoiddeadline'] = 'Not after the submissions deadline yet';
$string['resultvoidexecuted'] = 'The allocation has been already executed';

View File

@ -123,6 +123,78 @@ class workshop_scheduled_allocator implements workshop_allocator {
return $out;
}
/**
* Executes the allocation
*
* @return workshop_allocation_result
*/
public function execute() {
global $DB;
$result = new workshop_allocation_result($this);
// make sure the workshop itself is at the expected state
if ($this->workshop->phase != workshop::PHASE_SUBMISSION) {
$result->set_status(workshop_allocation_result::STATUS_FAILED,
get_string('resultfailedphase', 'workshopallocation_scheduled'));
return $result;
}
if (empty($this->workshop->submissionend)) {
$result->set_status(workshop_allocation_result::STATUS_FAILED,
get_string('resultfaileddeadline', 'workshopallocation_scheduled'));
return $result;
}
if ($this->workshop->submissionend > time()) {
$result->set_status(workshop_allocation_result::STATUS_VOID,
get_string('resultvoiddeadline', 'workshopallocation_scheduled'));
return $result;
}
$current = $DB->get_record('workshopallocation_scheduled',
array('workshopid' => $this->workshop->id, 'enabled' => 1), '*', IGNORE_MISSING);
if ($current === false) {
$result->set_status(workshop_allocation_result::STATUS_FAILED,
get_string('resultfailedconfig', 'workshopallocation_scheduled'));
return $result;
}
if (!$current->enabled) {
$result->set_status(workshop_allocation_result::STATUS_VOID,
get_string('resultdisabled', 'workshopallocation_scheduled'));
return $result;
}
if (!is_null($current->timeallocated) and $current->timeallocated >= $this->workshop->submissionend) {
$result->set_status(workshop_allocation_result::STATUS_VOID,
get_string('resultvoidexecuted', 'workshopallocation_scheduled'));
return $result;
}
// so now we know that we are after the submissions deadline and either the scheduled allocation was not
// executed yet or it was but the submissions deadline has been prolonged (and hence we should repeat the
// allocations)
$settings = workshop_random_allocator_setting::instance_from_text($current->settings);
$randomallocator = $this->workshop->allocator_instance('random');
$randomallocator->execute($settings, $result);
// store the result in the instance's table
$update = new stdClass();
$update->id = $current->id;
$update->timeallocated = $result->get_timeend();
$update->resultstatus = $result->get_status();
$update->resultmessage = $result->get_message();
$update->resultlog = json_encode($result->get_logs());
$DB->update_record('workshopallocation_scheduled', $update);
return $result;
}
/**
* Delete all data related to a given workshop module instance
*
@ -166,3 +238,39 @@ class workshop_scheduled_allocator implements workshop_allocator {
}
}
}
/**
* Regular jobs to execute via cron
*/
function workshopallocation_scheduled_cron() {
global $CFG, $DB;
$sql = "SELECT w.*
FROM {workshopallocation_scheduled} a
JOIN {workshop} w ON a.workshopid = w.id
WHERE a.enabled = 1
AND w.phase = 20
AND w.submissionend > 0
AND w.submissionend < ?
AND (a.timeallocated IS NULL OR a.timeallocated < w.submissionend)";
$workshops = $DB->get_records_sql($sql, array(time()));
if (empty($workshops)) {
mtrace('No workshops ready for scheduled allocation');
return;
}
// let's have some fun!
require_once($CFG->dirroot.'/mod/workshop/locallib.php');
foreach ($workshops as $workshop) {
$cm = get_coursemodule_from_instance('workshop', $workshop->id, $workshop->course, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$workshop = new workshop($workshop, $cm, $course);
$allocator = $workshop->allocator_instance('scheduled');
$result = $allocator->execute();
// todo inform the teachers about the results
}
}

View File

@ -33,3 +33,4 @@ $plugin->dependencies = array(
'workshopallocation_random' => 2012032800,
);
$plugin->maturity = MATURITY_ALPHA;
$plugin->cron = 60;