Commit e45fc71e authored by Juan Leyva's avatar Juan Leyva
Browse files

MDL-57392 course: Return course filters status via WS

Return active filters in core_course_get_courses_by_field
parent c4cf1c60
......@@ -2404,6 +2404,16 @@ class core_course_external extends external_api {
'timemodified' => new external_value(PARAM_INT, 'Last time the course was updated', VALUE_OPTIONAL),
'requested' => new external_value(PARAM_INT, 'If is a requested course', VALUE_OPTIONAL),
'cacherev' => new external_value(PARAM_INT, 'Cache revision number', VALUE_OPTIONAL),
'filters' => new external_multiple_structure(
new external_single_structure(
array(
'filter' => new external_value(PARAM_PLUGIN, 'Filter plugin name'),
'localstate' => new external_value(PARAM_INT, 'Filter state: 1 for on, -1 for off, 0 if inherit'),
'inheritedstate' => new external_value(PARAM_INT, '1 or 0 to use when localstate is set to inherit'),
)
),
'Course filters', VALUE_OPTIONAL
),
);
$coursestructure = array_merge($coursestructure, $extra);
}
......@@ -2929,6 +2939,7 @@ class core_course_external extends external_api {
public static function get_courses_by_field($field = '', $value = '') {
global $DB, $CFG;
require_once($CFG->libdir . '/coursecatlib.php');
require_once($CFG->libdir . '/filterlib.php');
$params = self::validate_parameters(self::get_courses_by_field_parameters(),
array(
......@@ -2991,6 +3002,9 @@ class core_course_external extends external_api {
'groupmode', 'groupmodeforce', 'defaultgroupingid', 'enablecompletion', 'completionnotify', 'lang', 'theme',
'sortorder', 'marker');
// Course filters.
$coursesdata[$course->id]['filters'] = filter_get_available_in_context($context);
// Information for managers only.
if ($canupdatecourse) {
$managerfields = array('idnumber', 'legacyfiles', 'calendartype', 'timecreated', 'timemodified', 'requested',
......
......@@ -1965,16 +1965,16 @@ class core_course_externallib_testcase extends externallib_advanced_testcase {
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
$this->assertCount(3, $result['courses']);
// Expect to receive all the fields.
$this->assertCount(35, $result['courses'][0]);
$this->assertCount(35, $result['courses'][1]);
$this->assertCount(35, $result['courses'][2]);
$this->assertCount(36, $result['courses'][0]);
$this->assertCount(36, $result['courses'][1]);
$this->assertCount(36, $result['courses'][2]);
$result = core_course_external::get_courses_by_field('id', $course1->id);
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
$this->assertCount(1, $result['courses']);
$this->assertEquals($course1->id, $result['courses'][0]['id']);
// Expect to receive all the fields.
$this->assertCount(35, $result['courses'][0]);
$this->assertCount(36, $result['courses'][0]);
$result = core_course_external::get_courses_by_field('id', $course2->id);
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
......@@ -1985,6 +1985,10 @@ class core_course_externallib_testcase extends externallib_advanced_testcase {
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
$this->assertCount(2, $result['courses']);
// Check default filters.
$this->assertCount(3, $result['courses'][0]['filters']);
$this->assertCount(3, $result['courses'][1]['filters']);
$result = core_course_external::get_courses_by_field('category', $category1->id);
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
$this->assertCount(1, $result['courses']);
......@@ -2004,20 +2008,34 @@ class core_course_externallib_testcase extends externallib_advanced_testcase {
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
$this->assertCount(0, $result['courses']);
// Change filter value.
filter_set_local_state('mediaplugin', context_course::instance($course1->id)->id, TEXTFILTER_OFF);
self::setUser($student1);
// All visible courses (including front page) for normal student.
$result = core_course_external::get_courses_by_field();
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
$this->assertCount(2, $result['courses']);
$this->assertCount(28, $result['courses'][0]);
$this->assertCount(28, $result['courses'][1]);
$this->assertCount(29, $result['courses'][0]);
$this->assertCount(29, $result['courses'][1]);
$result = core_course_external::get_courses_by_field('id', $course1->id);
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
$this->assertCount(1, $result['courses']);
$this->assertEquals($course1->id, $result['courses'][0]['id']);
// Expect to receive all the files that a student can see.
$this->assertCount(28, $result['courses'][0]);
$this->assertCount(29, $result['courses'][0]);
// Check default filters.
$filters = $result['courses'][0]['filters'];
$this->assertCount(3, $filters);
$found = false;
foreach ($filters as $filter) {
if ($filter['filter'] == 'mediaplugin' and $filter['localstate'] == TEXTFILTER_OFF) {
$found = true;
}
}
$this->assertTrue($found);
// Course 2 is not visible.
$result = core_course_external::get_courses_by_field('id', $course2->id);
......@@ -2051,7 +2069,7 @@ class core_course_externallib_testcase extends externallib_advanced_testcase {
$result = core_course_external::get_courses_by_field();
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
$this->assertCount(2, $result['courses']);
$this->assertCount(28, $result['courses'][0]); // Site course.
$this->assertCount(29, $result['courses'][0]); // Site course.
$this->assertCount(12, $result['courses'][1]); // Only public information, not enrolled.
$result = core_course_external::get_courses_by_field('id', $course1->id);
......
This files describes API changes in /course/*,
information provided here is intended especially for developers.
=== 3.3 ===
* External function core_course_external::get_courses_by_field now returns the course filters list and status.
=== 3.2 ===
* External function core_course_external::get_course_contents now returns the section's number in the course (new section field).
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment