MDL-57762 mod_lesson: Implement the check_updates callback

This commit is contained in:
Juan Leyva 2017-01-25 16:04:50 +01:00
parent 87e472bd15
commit 635721f5c1
2 changed files with 151 additions and 0 deletions

View File

@ -1489,3 +1489,72 @@ function mod_lesson_get_fontawesome_icon_map() {
'mod_lesson:e/copy' => 'fa-clone',
];
}
/*
* Check if the module has any update that affects the current user since a given time.
*
* @param cm_info $cm course module data
* @param int $from the time to check updates from
* @param array $filter if we need to check only specific updates
* @return stdClass an object with the different type of areas indicating if they were updated or not
* @since Moodle 3.3
*/
function lesson_check_updates_since(cm_info $cm, $from, $filter = array()) {
global $DB, $USER;
$updates = course_check_module_updates_since($cm, $from, array(), $filter);
// Check if there are new pages or answers in the lesson.
$updates->pages = (object) array('updated' => false);
$updates->answers = (object) array('updated' => false);
$select = 'lessonid = ? AND (timecreated > ? OR timemodified > ?)';
$params = array($cm->instance, $USER->id, $from);
$pages = $DB->get_records_select('lesson_pages', $select, $params, '', 'id');
if (!empty($pages)) {
$updates->pages->updated = true;
$updates->pages->itemids = array_keys($pages);
}
$answers = $DB->get_records_select('lesson_answers', $select, $params, '', 'id');
if (!empty($answers)) {
$updates->answers->updated = true;
$updates->answers->itemids = array_keys($answers);
}
// Check for new question attempts, grades, pages viewed and timers.
$updates->questionattempts = (object) array('updated' => false);
$updates->grades = (object) array('updated' => false);
$updates->pagesviewed = (object) array('updated' => false);
$updates->timers = (object) array('updated' => false);
$select = 'lessonid = ? AND userid = ? AND timeseen > ?';
$params = array($cm->instance, $USER->id, $from);
$questionattempts = $DB->get_records_select('lesson_attempts', $select, $params, '', 'id');
if (!empty($questionattempts)) {
$updates->questionattempts->updated = true;
$updates->questionattempts->itemids = array_keys($questionattempts);
}
$pagesviewed = $DB->get_records_select('lesson_branch', $select, $params, '', 'id');
if (!empty($pagesviewed)) {
$updates->pagesviewed->updated = true;
$updates->pagesviewed->itemids = array_keys($pagesviewed);
}
$select = 'lessonid = ? AND userid = ? AND completed > ?';
$grades = $DB->get_records_select('lesson_grades', $select, $params, '', 'id');
if (!empty($grades)) {
$updates->grades->updated = true;
$updates->grades->itemids = array_keys($grades);
}
$select = 'lessonid = ? AND userid = ? AND (starttime > ? OR lessontime > ? OR timemodifiedoffline > ?)';
$params = array($cm->instance, $USER->id, $from, $from, $from);
$timers = $DB->get_records_select('lesson_timer', $select, $params, '', 'id');
if (!empty($timers)) {
$updates->timers->updated = true;
$updates->timers->itemids = array_keys($timers);
}
return $updates;
}

View File

@ -82,4 +82,86 @@ class mod_lesson_lib_testcase extends advanced_testcase {
$this->assertEquals(2, $closepriorities[$override1->deadline]);
$this->assertEquals(1, $closepriorities[$override2->deadline]);
}
/**
* Test check_updates_since callback.
*/
public function test_check_updates_since() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
// Create user.
$student = self::getDataGenerator()->create_user();
// User enrolment.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
$this->setCurrentTimeStart();
$record = array(
'course' => $course->id,
'custom' => 0,
'feedback' => 1,
);
$lessonmodule = $this->getDataGenerator()->create_module('lesson', $record);
// Convert to a lesson object.
$lesson = new lesson($lessonmodule);
$cm = $lesson->cm;
$cm = cm_info::create($cm);
// Check that upon creation, the updates are only about the new configuration created.
$onehourago = time() - HOURSECS;
$updates = lesson_check_updates_since($cm, $onehourago);
foreach ($updates as $el => $val) {
if ($el == 'configuration') {
$this->assertTrue($val->updated);
$this->assertTimeCurrent($val->timeupdated);
} else {
$this->assertFalse($val->updated);
}
}
// Set up a generator to create content.
$generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
$tfrecord = $generator->create_question_truefalse($lesson);
// Check now for pages and answers.
$updates = lesson_check_updates_since($cm, $onehourago);
$this->assertTrue($updates->pages->updated);
$this->assertCount(1, $updates->pages->itemids);
$this->assertTrue($updates->answers->updated);
$this->assertCount(2, $updates->answers->itemids);
// Now, do something in the lesson.
$this->setUser($student);
mod_lesson_external::launch_attempt($lesson->id);
$data = array(
array(
'name' => 'answerid',
'value' => $DB->get_field('lesson_answers', 'id', array('pageid' => $tfrecord->id, 'jumpto' => -1)),
),
array(
'name' => '_qf__lesson_display_answer_form_truefalse',
'value' => 1,
)
);
mod_lesson_external::process_page($lesson->id, $tfrecord->id, $data);
mod_lesson_external::finish_attempt($lesson->id);
$updates = lesson_check_updates_since($cm, $onehourago);
// Check question attempts, timers and new grades.
$this->assertTrue($updates->questionattempts->updated);
$this->assertCount(1, $updates->questionattempts->itemids);
$this->assertTrue($updates->grades->updated);
$this->assertCount(1, $updates->grades->itemids);
$this->assertTrue($updates->timers->updated);
$this->assertCount(1, $updates->timers->itemids);
}
}