Skip to content

Commit

Permalink
MDL-59254 mod_workshop: Implement check_updates_since callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Oct 16, 2017
1 parent 9a316f3 commit 695b871
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 0 deletions.
112 changes: 112 additions & 0 deletions mod/workshop/lib.php
Expand Up @@ -1936,3 +1936,115 @@ function mod_workshop_get_fontawesome_icon_map() {
'mod_workshop:userplan/task-fail' => 'fa-remove text-danger',
];
}

/**
* 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.4
*/
function workshop_check_updates_since(cm_info $cm, $from, $filter = array()) {
global $DB, $USER;

$updates = course_check_module_updates_since($cm, $from, array('instructauthors', 'instructreviewers', 'conclusion'), $filter);

// Check if there are new submissions, assessments or assessments grades in the workshop.
$updates->submissions = (object) array('updated' => false);
$updates->assessments = (object) array('updated' => false);
$updates->assessmentgrades = (object) array('updated' => false);

$select = 'workshopid = ? AND authorid = ? AND (timecreated > ? OR timegraded > ? OR timemodified > ?)';
$params = array($cm->instance, $USER->id, $from, $from, $from);
$submissions = $DB->get_records_select('workshop_submissions', $select, $params, '', 'id');
if (!empty($submissions)) {
$updates->submissions->updated = true;
$updates->submissions->itemids = array_keys($submissions);
}

// Get assessments updates (both submissions reviewed by me or reviews by others).
$select = "SELECT a.id
FROM {workshop_assessments} a
JOIN {workshop_submissions} s ON a.submissionid = s.id
WHERE s.workshopid = ? AND (a.timecreated > ? OR a.timemodified > ?) AND (s.authorid = ? OR a.reviewerid = ?)";
$params = array($cm->instance, $from, $from, $USER->id, $USER->id);
$assessments = $DB->get_records_sql($select, $params);
if (!empty($assessments)) {
$updates->assessments->updated = true;
$updates->assessments->itemids = array_keys($assessments);
}
// Finally assessment aggregated grades.
$select = 'workshopid = ? AND userid = ? AND timegraded > ?';
$params = array($cm->instance, $USER->id, $from);
$assessmentgrades = $DB->get_records_select('workshop_aggregations', $select, $params, '', 'id');
if (!empty($assessmentgrades)) {
$updates->assessmentgrades->updated = true;
$updates->assessmentgrades->itemids = array_keys($assessmentgrades);
}

// Now, teachers should see other students updates.
$canviewallsubmissions = has_capability('mod/workshop:viewallsubmissions', $cm->context);
$canviewallassessments = has_capability('mod/workshop:viewallassessments', $cm->context);
if ($canviewallsubmissions || $canviewallassessments) {

$insql = '';
$inparams = array();
// To filter by users in my groups when separated groups are forced.
if (groups_get_activity_groupmode($cm) == SEPARATEGROUPS) {
$groupusers = array_keys(groups_get_activity_shared_group_members($cm));
if (empty($groupusers)) {
return $updates;
}
list($insql, $inparams) = $DB->get_in_or_equal($groupusers);
}

if ($canviewallsubmissions) {
$updates->usersubmissions = (object) array('updated' => false);
$select = 'workshopid = ? AND (timecreated > ? OR timegraded > ? OR timemodified > ?)';
$params = array($cm->instance, $from, $from, $from);
if (!empty($insql)) {
$select .= " AND authorid $insql";
$params = array_merge($params, $inparams);
}
$usersubmissions = $DB->get_records_select('workshop_submissions', $select, $params, '', 'id');
if (!empty($usersubmissions)) {
$updates->usersubmissions->updated = true;
$updates->usersubmissions->itemids = array_keys($usersubmissions);
}
}

if ($canviewallassessments) {
$updates->userassessments = (object) array('updated' => false);
$select = "SELECT a.id
FROM {workshop_assessments} a
JOIN {workshop_submissions} s ON a.submissionid = s.id
WHERE s.workshopid = ? AND (a.timecreated > ? OR a.timemodified > ?)";
$params = array($cm->instance, $from, $from);
if (!empty($insql)) {
$select .= " AND s.reviewerid $insql";
$params = array_merge($params, $inparams);
}
$userassessments = $DB->get_records_sql($select, $params);
if (!empty($userassessments)) {
$updates->userassessments->updated = true;
$updates->userassessments->itemids = array_keys($userassessments);
}

$updates->userassessmentgrades = (object) array('updated' => false);
$select = 'workshopid = ? AND timegraded > ?';
$params = array($cm->instance, $USER->id);
if (!empty($insql)) {
$select .= " AND userid $insql";
$params = array_merge($params, $inparams);
}
$userassessmentgrades = $DB->get_records_select('workshop_aggregations', $select, $params, '', 'id');
if (!empty($userassessmentgrades)) {
$updates->userassessmentgrades->updated = true;
$updates->userassessmentgrades->itemids = array_keys($userassessmentgrades);
}
}
}
return $updates;
}
96 changes: 96 additions & 0 deletions mod/workshop/tests/lib_test.php
Expand Up @@ -145,4 +145,100 @@ private function create_action_event($courseid, $instanceid, $eventtype) {

return calendar_event::create($event);
}

/**
* 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();
$teacher = 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');
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');

$this->setCurrentTimeStart();
$record = array(
'course' => $course->id,
'custom' => 0,
'feedback' => 1,
);
$workshop = $this->getDataGenerator()->create_module('workshop', $record);
$cm = get_coursemodule_from_instance('workshop', $workshop->id, $course->id);
$context = context_module::instance($cm->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 = workshop_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_workshop');
// Submission.
$submissionid = $generator->create_submission($workshop->id, $student->id, array(
'title' => 'My custom title',
));
// Now assessment.
$assessmentid = $generator->create_assessment($submissionid, $student->id, array(
'weight' => 3,
'grade' => 95.00000,
));
// Add files to one editor file area.
$fs = get_file_storage();
$filerecordinline = array(
'contextid' => $context->id,
'component' => 'mod_workshop',
'filearea' => 'instructauthors',
'itemid' => 0,
'filepath' => '/',
'filename' => 'image.png',
);
$instructauthorsfile = $fs->create_file_from_string($filerecordinline, 'image contents (not really)');

$updates = workshop_check_updates_since($cm, $onehourago);
$this->assertTrue($updates->submissions->updated);
$this->assertCount(1, $updates->submissions->itemids);
$this->assertEquals($submissionid, $updates->submissions->itemids[0]);
$this->assertTrue($updates->assessments->updated);
$this->assertCount(1, $updates->assessments->itemids);
$this->assertEquals($assessmentid, $updates->assessments->itemids[0]);
$this->assertTrue($updates->instructauthorsfiles->updated);
$this->assertCount(1, $updates->instructauthorsfiles->itemids);
$this->assertEquals($instructauthorsfile->get_id(), $updates->instructauthorsfiles->itemids[0]);

// Check I see the user updates as teacher.
$this->setUser($teacher);
$updates = workshop_check_updates_since($cm, $onehourago);
$this->assertTrue($updates->usersubmissions->updated);
$this->assertCount(1, $updates->usersubmissions->itemids);
$this->assertEquals($submissionid, $updates->usersubmissions->itemids[0]);
$this->assertTrue($updates->userassessments->updated);
$this->assertCount(1, $updates->userassessments->itemids);
$this->assertEquals($assessmentid, $updates->userassessments->itemids[0]);
$this->assertTrue($updates->instructauthorsfiles->updated);
$this->assertCount(1, $updates->instructauthorsfiles->itemids);
$this->assertEquals($instructauthorsfile->get_id(), $updates->instructauthorsfiles->itemids[0]);

// The teacher didn't do anything.
$this->assertFalse($updates->submissions->updated);
$this->assertFalse($updates->assessments->updated);
}
}

0 comments on commit 695b871

Please sign in to comment.