Skip to content

Commit

Permalink
Attempting to add cron updates
Browse files Browse the repository at this point in the history
  • Loading branch information
davosmith committed Feb 9, 2011
1 parent c0e0fab commit bd94070
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 44 deletions.
51 changes: 10 additions & 41 deletions autoupdate.php
@@ -1,49 +1,10 @@
<?php

/*
To make this work, you need to open up the following files:
* moodle/lib/datalib.php
Find the function 'add_to_log', then add these lines to the end of it:
require_once($CFG->dirroot.'/mod/checklist/autoupdate.php');
checklist_autoupdate($courseid, $module, $action, $cm, $userid);
* moodle/mod/quiz/lib.php
Find the function 'quiz_grade_item_update', then add these lines just before the final 'return' line:
// Inserted to allow autoupdating items in checklist
require_once($CFG->dirroot.'/mod/checklist/autoupdate.php');
checklist_autoupdate_score('quiz', $quiz->course, $quiz->id, $grades);
// Inserted to allow autoupdating items in checklist
* moodle/mod/forum/lib.php
Find the function 'forum_grade_item_update', then add these lines just before the final 'return' line:
// Inserted to allow autoupdating items in checklist
require_once($CFG->dirroot.'/mod/checklist/autoupdate.php');
checklist_autoupdate_score('forum', $forum->course, $forum->id, $grades);
// Inserted to allow autoupdating items in checklist
* moodle/mod/assignment/lib.php
Find the function 'assignment_grade_item_update', then add these lines just before the final 'return' line:
// Inserted to allow autoupdating items in checklist
require_once($CFG->dirroot.'/mod/checklist/autoupdate.php');
checklist_autoupdate_score('assignment', $assignment->courseid, $assignment->id, $grades);
// Inserted to allow autoupdating items in checklist
WARNING: This will slow your Moodle site down very slightly.
However, the difference is unlikely to be noticable.
*/

function checklist_autoupdate($courseid, $module, $action, $cm, $userid) {
global $CFG;

echo "checking module: $module; action: $action";

if ($module == 'course') { return; }
if ($module == 'user') { return; }
if ($module == 'role') { return; }
Expand All @@ -58,6 +19,7 @@ function checklist_autoupdate($courseid, $module, $action, $cm, $userid) {
if ($module == 'library') { return; }
if ($module == 'upload') { return; }

echo 'passed first test';

if (
(($module == 'survey') && ($action == 'submit'))
Expand All @@ -81,11 +43,15 @@ function checklist_autoupdate($courseid, $module, $action, $cm, $userid) {
|| (($module == 'feedback') && ($action == 'submit'))
) {

echo 'something to update';

$checklists = get_records_sql("SELECT * FROM {$CFG->prefix}checklist WHERE course = $courseid AND autoupdate > 0");
if (!$checklists) {
return;
}

echo 'found checklists';

// Find all checklist_item records which are related to these $checklists which have a moduleid matching $module
// and do not have a related checklist_check record that is filled in
$checklistids = '('.implode(',', array_keys($checklists)).')';
Expand All @@ -96,11 +62,14 @@ function checklist_autoupdate($courseid, $module, $action, $cm, $userid) {
return;
}

echo 'found items';

$updategrades = false;
foreach ($items as $item) {
$updategrades = checklist_set_check($item->id, $userid, true) || $updategrades;
}
if ($updategrades) {
echo 'grades updated';
require_once($CFG->dirroot.'/mod/checklist/lib.php');
foreach ($checklists as $checklist) {
checklist_update_grades($checklist, $userid);
Expand Down
24 changes: 23 additions & 1 deletion lib.php
Expand Up @@ -373,7 +373,29 @@ function checklist_print_overview($courses, &$htmlarray) {
* @todo Finish documenting this function
**/
function checklist_cron () {
return true;
global $CFG;

$lastcron = get_field('modules', 'lastcron', 'name', 'checklist');
if (!$lastcron) {
// First time run - checklists will take care of any updates before now
return true;
}

require_once($CFG->dirroot.'/mod/checklist/autoupdate.php');

$lastlogtime = $lastcron - 5; // Subtract 5 seconds just in case a log slipped through during the last cron update

$logs = get_logs("l.time >= $lastlogtime", 'l.time ASC', '', '', $totalcount);
if ($logs) {
foreach ($logs as $log) {
echo "\n";
checklist_autoupdate($log->course, $log->module, $log->action, $log->cmid, $log->userid);
}
}

// Get all grade changes since last update

return false;
}


Expand Down
4 changes: 2 additions & 2 deletions version.php
Expand Up @@ -8,7 +8,7 @@
* @package mod/checklist
*/

$module->version = 2010121100; // The current module version (Date: YYYYMMDDXX)
$module->cron = 0; // Period for cron to check this module (secs)
$module->version = 2011020900; // The current module version (Date: YYYYMMDDXX)
$module->cron = 60; // Period for cron to check this module (secs)

?>

0 comments on commit bd94070

Please sign in to comment.