MDL-57925 mod_data: Implement check_updates_since callback

This commit is contained in:
Juan Leyva 2017-03-24 10:22:04 +01:00 committed by Eloy Lafuente (stronk7)
parent 01b7ec36d1
commit 57ab070e01
2 changed files with 111 additions and 0 deletions

View File

@ -4174,3 +4174,41 @@ function mod_data_get_fontawesome_icon_map() {
'mod_data:field/url' => 'fa-link',
];
}
/*
* 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.2
*/
function data_check_updates_since(cm_info $cm, $from, $filter = array()) {
global $DB, $CFG;
require_once($CFG->dirroot . '/mod/data/locallib.php');
$updates = course_check_module_updates_since($cm, $from, array(), $filter);
// Check for new entries.
$updates->entries = (object) array('updated' => false);
$data = $DB->get_record('data', array('id' => $cm->instance), '*', MUST_EXIST);
$searcharray = [];
$searcharray[DATA_TIMEMODIFIED] = new stdClass();
$searcharray[DATA_TIMEMODIFIED]->sql = '';
$searcharray[DATA_TIMEMODIFIED]->params = array();
$searcharray[DATA_TIMEMODIFIED]->field = 'r.timemodified';
$searcharray[DATA_TIMEMODIFIED]->data = $from;
$currentgroup = groups_get_activity_group($cm);
list($entries, $maxcount, $totalcount, $page, $nowperpage, $sort, $mode) =
data_search_entries($data, $cm, $cm->context, 'list', $currentgroup, '', null, null, 0, 0, true, $searcharray);
if (!empty($entries)) {
$updates->entries->updated = true;
$updates->entries->itemids = array_keys($entries);
}
return $updates;
}

View File

@ -921,4 +921,77 @@ class mod_data_lib_testcase extends advanced_testcase {
$completiondata = $completion->get_data($cm);
$this->assertEquals(1, $completiondata->completionstate);
}
/**
* 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,
);
$data = $this->getDataGenerator()->create_module('data', $record);
$cm = get_coursemodule_from_instance('data', $data->id, $course->id);
$cm = cm_info::create($cm);
$this->setUser($student);
// Check that upon creation, the updates are only about the new configuration created.
$onehourago = time() - HOURSECS;
$updates = data_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);
}
}
// Add a couple of entries.
$datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
$fieldtypes = array('checkbox', 'date');
$count = 1;
// Creating test Fields with default parameter values.
foreach ($fieldtypes as $fieldtype) {
// Creating variables dynamically.
$fieldname = 'field-' . $count;
$record = new StdClass();
$record->name = $fieldname;
$record->type = $fieldtype;
$record->required = 1;
${$fieldname} = $datagenerator->create_field($record, $data);
$count++;
}
$fields = $DB->get_records('data_fields', array('dataid' => $data->id), 'id');
$contents = array();
$contents[] = array('opt1', 'opt2', 'opt3', 'opt4');
$contents[] = '01-01-2037'; // It should be lower than 2038, to avoid failing on 32-bit windows.
$count = 0;
$fieldcontents = array();
foreach ($fields as $fieldrecord) {
$fieldcontents[$fieldrecord->id] = $contents[$count++];
}
$datarecor1did = $datagenerator->create_entry($data, $fieldcontents);
$datarecor2did = $datagenerator->create_entry($data, $fieldcontents);
$records = $DB->get_records('data_records', array('dataid' => $data->id));
$this->assertCount(2, $records);
// Check we received the entries updated.
$updates = data_check_updates_since($cm, $onehourago);
$this->assertTrue($updates->entries->updated);
$this->assertEquals([$datarecor1did, $datarecor2did], $updates->entries->itemids, '', 0, 10, true);
}
}