Skip to content

Commit

Permalink
MDL-61312 completion: Return completion for teachers via WS
Browse files Browse the repository at this point in the history
core_completion_get_activities_completion_status was not returning
completion information for users with the teacher role only (when it
wasn’t a role allowed to track completion).
  • Loading branch information
jleyva committed Jan 30, 2018
1 parent 51e05c0 commit 444b165
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
21 changes: 5 additions & 16 deletions completion/classes/external.php
Expand Up @@ -249,8 +249,6 @@ public static function get_activities_completion_status($courseid, $userid) {

$completion = new completion_info($course);
$activities = $completion->get_activities();
$progresses = $completion->get_progress_all('u.id = :uid', ['uid' => $params['userid']]);
$userprogress = $progresses[$user->id];

$results = array();
foreach ($activities as $activity) {
Expand All @@ -260,26 +258,17 @@ public static function get_activities_completion_status($courseid, $userid) {
continue;
}

// Get progress information and state.
if (array_key_exists($activity->id, $userprogress->progress)) {
$thisprogress = $userprogress->progress[$activity->id];
$state = $thisprogress->completionstate;
$timecompleted = $thisprogress->timemodified;
$overrideby = $thisprogress->overrideby;
} else {
$state = COMPLETION_INCOMPLETE;
$timecompleted = 0;
$overrideby = null;
}
// Get progress information and state (we must use get_data because it works for all user roles in course).
$activitycompletiondata = $completion->get_data($activity, true, $user->id);

$results[] = array(
'cmid' => $activity->id,
'modname' => $activity->modname,
'instance' => $activity->instance,
'state' => $state,
'timecompleted' => $timecompleted,
'state' => $activitycompletiondata->completionstate,
'timecompleted' => $activitycompletiondata->timemodified,
'tracking' => $activity->completion,
'overrideby' => $overrideby
'overrideby' => $activitycompletiondata->overrideby
);
}

Expand Down
28 changes: 28 additions & 0 deletions completion/tests/externallib_test.php
Expand Up @@ -182,6 +182,34 @@ public function test_get_activities_completion_status() {
}
}

// Teacher should see his own completion status.

// Forum complete for teacher.
$completion = new completion_info($course);
$completion->update_state($cmforum, COMPLETION_COMPLETE);

$result = core_completion_external::get_activities_completion_status($course->id, $teacher->id);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::get_activities_completion_status_returns(), $result);

// We added 4 activities, but only 3 with completion enabled (one of those is hidden but the teacher can see it).
$this->assertCount(3, $result['statuses']);

$activitiesfound = 0;
foreach ($result['statuses'] as $status) {
if ($status['cmid'] == $forum->cmid and $status['modname'] == 'forum' and $status['instance'] == $forum->id) {
$activitiesfound++;
$this->assertEquals(COMPLETION_COMPLETE, $status['state']);
$this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
} else {
$activitiesfound++;
$this->assertEquals(COMPLETION_INCOMPLETE, $status['state']);
$this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
}
}
$this->assertEquals(3, $activitiesfound);

// Change teacher role capabilities (disable access all groups).
$context = context_course::instance($course->id);
assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $context);
Expand Down

0 comments on commit 444b165

Please sign in to comment.