Skip to content

Commit

Permalink
MDL-64123 webservices: Add valueused to completion data
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Johnson committed Dec 19, 2018
1 parent 33a388e commit 7672471
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
5 changes: 4 additions & 1 deletion completion/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ public static function get_activities_completion_status($courseid, $userid) {
'state' => $activitycompletiondata->completionstate,
'timecompleted' => $activitycompletiondata->timemodified,
'tracking' => $activity->completion,
'overrideby' => $activitycompletiondata->overrideby
'overrideby' => $activitycompletiondata->overrideby,
'valueused' => core_availability\info::completion_value_used($course, $activity->id)
);
}

Expand Down Expand Up @@ -302,6 +303,8 @@ public static function get_activities_completion_status_returns() {
0 means none, 1 manual, 2 automatic'),
'overrideby' => new external_value(PARAM_INT, 'The user id who has overriden the status, or null',
VALUE_OPTIONAL),
'valueused' => new external_value(PARAM_BOOL, 'Whether the completion status affects the availability
of another activity.', VALUE_OPTIONAL),
), 'Activity'
), 'List of activities status'
),
Expand Down
6 changes: 5 additions & 1 deletion completion/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ public function test_get_activities_completion_status() {
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1,
'groupmode' => SEPARATEGROUPS,
'groupmodeforce' => 1));
availability_completion\condition::wipe_static_cache();

$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
array('completion' => 1));
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
array('completion' => 1));
$assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
$availability = '{"op":"&","c":[{"type":"completion","cm":' . $forum->cmid .',"e":1}],"showc":[true]}';
$assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id], ['availability' => $availability]);
$page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
array('completion' => 1, 'visible' => 0));

Expand Down Expand Up @@ -146,10 +148,12 @@ public function test_get_activities_completion_status() {
$activitiesfound++;
$this->assertEquals(COMPLETION_COMPLETE, $status['state']);
$this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
$this->assertTrue($status['valueused']);
} else if ($status['cmid'] == $data->cmid and $status['modname'] == 'data' and $status['instance'] == $data->id) {
$activitiesfound++;
$this->assertEquals(COMPLETION_INCOMPLETE, $status['state']);
$this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
$this->assertFalse($status['valueused']);
}
}
$this->assertEquals(2, $activitiesfound);
Expand Down
4 changes: 4 additions & 0 deletions completion/upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
This files describes API changes in /completion/* - completion,
information provided here is intended especially for developers.

=== 3.7 ===
* External function core_completion_external::get_activities_completion_status new returns the following additional field:
- valueused (indicates whether the completion state affects the availability of other content)

=== 2.9 ===

* A completed and failed activity counts as a completed activity for
Expand Down
5 changes: 4 additions & 1 deletion course/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ public static function get_course_contents($courseid, $options = array()) {
$module['completiondata'] = array(
'state' => $completiondata->completionstate,
'timecompleted' => $completiondata->timemodified,
'overrideby' => $completiondata->overrideby
'overrideby' => $completiondata->overrideby,
'valueused' => core_availability\info::completion_value_used($course, $cm->id)
);
}

Expand Down Expand Up @@ -440,6 +441,8 @@ public static function get_course_contents_returns() {
'timecompleted' => new external_value(PARAM_INT, 'Timestamp for completion status.'),
'overrideby' => new external_value(PARAM_INT, 'The user id who has overriden the
status.'),
'valueused' => new external_value(PARAM_BOOL, 'Whether the completion status affects
the availability of another activity.', VALUE_OPTIONAL),
), 'Module completion data.', VALUE_OPTIONAL
),
'contents' => new external_multiple_structure(
Expand Down
24 changes: 21 additions & 3 deletions course/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -833,14 +833,16 @@ private function prepare_get_course_contents_test() {
$labeldescription = 'This is a very long label to test if more than 50 characters are returned.
So bla bla bla bla <b>bold bold bold</b> bla bla bla bla.';
$label = $this->getDataGenerator()->create_module('label', array('course' => $course->id,
'intro' => $labeldescription));
'intro' => $labeldescription, 'completion' => COMPLETION_TRACKING_MANUAL));
$labelcm = get_coursemodule_from_instance('label', $label->id);
$tomorrow = time() + DAYSECS;
// Module with availability restrictions not met.
$availability = '{"op":"&","c":[{"type":"date","d":">=","t":' . $tomorrow . '},'
.'{"type":"completion","cm":' . $label->cmid .',"e":1}],"showc":[true,true]}';
$url = $this->getDataGenerator()->create_module('url',
array('course' => $course->id, 'name' => 'URL: % & $ ../', 'section' => 2, 'display' => RESOURCELIB_DISPLAY_POPUP,
'popupwidth' => 100, 'popupheight' => 100),
array('availability' => '{"op":"&","c":[{"type":"date","d":">=","t":' . $tomorrow . '}],"showc":[true]}'));
array('availability' => $availability));
$urlcm = get_coursemodule_from_instance('url', $url->id);
// Module for the last section.
$this->getDataGenerator()->create_module('url',
Expand Down Expand Up @@ -1152,6 +1154,7 @@ public function test_get_course_contents_completion() {
$this->resetAfterTest(true);

list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
availability_completion\condition::wipe_static_cache();

// Test activity not completed yet.
$result = core_course_external::get_course_contents($course->id, array(
Expand All @@ -1165,6 +1168,7 @@ public function test_get_course_contents_completion() {
$this->assertEquals(0, $result[0]['modules'][0]["completiondata"]['state']);
$this->assertEquals(0, $result[0]['modules'][0]["completiondata"]['timecompleted']);
$this->assertEmpty($result[0]['modules'][0]["completiondata"]['overrideby']);
$this->assertFalse($result[0]['modules'][0]["completiondata"]['valueused']);

// Set activity completed.
core_completion_external::update_activity_completion_status_manually($forumcm->id, true);
Expand All @@ -1178,6 +1182,20 @@ public function test_get_course_contents_completion() {
$this->assertNotEmpty($result[0]['modules'][0]["completiondata"]['timecompleted']);
$this->assertEmpty($result[0]['modules'][0]["completiondata"]['overrideby']);

// Test activity with completion value that is used in an availability condition.
$result = core_course_external::get_course_contents($course->id, array(
array("name" => "modname", "value" => "label"), array("name" => "modid", "value" => $labelcm->instance)));
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $result);

$this->assertCount(1, $result[0]['modules']);
$this->assertEquals("label", $result[0]['modules'][0]["modname"]);
$this->assertEquals(COMPLETION_TRACKING_MANUAL, $result[0]['modules'][0]["completion"]);
$this->assertEquals(0, $result[0]['modules'][0]["completiondata"]['state']);
$this->assertEquals(0, $result[0]['modules'][0]["completiondata"]['timecompleted']);
$this->assertEmpty($result[0]['modules'][0]["completiondata"]['overrideby']);
$this->assertTrue($result[0]['modules'][0]["completiondata"]['valueused']);

// Disable completion.
$CFG->enablecompletion = 0;
$result = core_course_external::get_course_contents($course->id, array(
Expand Down Expand Up @@ -2826,4 +2844,4 @@ public function test_get_recent_courses() {
$this->assertCount(1, $result);
$this->assertEquals($courses[0]->id, array_shift($result)->id);
}
}
}
4 changes: 4 additions & 0 deletions course/upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
This files describes API changes in /course/*,
information provided here is intended especially for developers.

=== 3.7 ===
* External function core_course_external::get_course_contents new returns the following additional completiondata field:
- valueused (indicates whether the completion state affects the availability of other content)

=== 3.6 ===

* External function core_course_external::get_course_public_information now returns the roles and the primary role of course
Expand Down

0 comments on commit 7672471

Please sign in to comment.