mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 17:06:53 +02:00
MDL-55415 course: New WS core_course_get_user_administration_options
This commit is contained in:
parent
b2392037e7
commit
b9050b106e
3 changed files with 134 additions and 0 deletions
|
@ -2701,4 +2701,70 @@ class core_course_external extends external_api {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns description of method parameters
|
||||||
|
*
|
||||||
|
* @return external_function_parameters
|
||||||
|
* @since Moodle 3.2
|
||||||
|
*/
|
||||||
|
public static function get_user_administration_options_parameters() {
|
||||||
|
return new external_function_parameters(
|
||||||
|
array(
|
||||||
|
'courseids' => new external_multiple_structure(new external_value(PARAM_INT, 'Course id.')),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a list of administration options in a set of courses that are available or not for the current user.
|
||||||
|
*
|
||||||
|
* @param array $courseids a list of course ids
|
||||||
|
* @return array of warnings and the options availability
|
||||||
|
* @since Moodle 3.2
|
||||||
|
* @throws moodle_exception
|
||||||
|
*/
|
||||||
|
public static function get_user_administration_options($courseids) {
|
||||||
|
global $CFG;
|
||||||
|
require_once($CFG->dirroot . '/course/lib.php');
|
||||||
|
|
||||||
|
// Parameter validation.
|
||||||
|
$params = self::validate_parameters(self::get_user_administration_options_parameters(), array('courseids' => $courseids));
|
||||||
|
$courseoptions = array();
|
||||||
|
|
||||||
|
list($courses, $warnings) = external_util::validate_courses($params['courseids'], array(), true);
|
||||||
|
|
||||||
|
if (!empty($courses)) {
|
||||||
|
foreach ($courses as $course) {
|
||||||
|
$adminoptions = course_get_user_administration_options($course, $course->context);
|
||||||
|
$options = array();
|
||||||
|
foreach ($adminoptions as $name => $available) {
|
||||||
|
$options[] = array(
|
||||||
|
'name' => $name,
|
||||||
|
'available' => $available,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$courseoptions[] = array(
|
||||||
|
'id' => $course->id,
|
||||||
|
'options' => $options
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = array(
|
||||||
|
'courses' => $courseoptions,
|
||||||
|
'warnings' => $warnings
|
||||||
|
);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns description of method result value
|
||||||
|
*
|
||||||
|
* @return external_description
|
||||||
|
* @since Moodle 3.2
|
||||||
|
*/
|
||||||
|
public static function get_user_administration_options_returns() {
|
||||||
|
return self::get_user_navigation_options_returns();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1803,4 +1803,63 @@ class core_course_externallib_testcase extends externallib_advanced_testcase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test get_user_administration_options
|
||||||
|
*/
|
||||||
|
public function test_get_user_administration_options() {
|
||||||
|
global $USER;
|
||||||
|
|
||||||
|
$this->resetAfterTest();
|
||||||
|
$course1 = self::getDataGenerator()->create_course();
|
||||||
|
$course2 = self::getDataGenerator()->create_course();
|
||||||
|
|
||||||
|
// Create a viewer user.
|
||||||
|
$viewer = self::getDataGenerator()->create_user();
|
||||||
|
$this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
|
||||||
|
$this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
|
||||||
|
|
||||||
|
$this->setUser($viewer->id);
|
||||||
|
$courses = array($course1->id , $course2->id, SITEID);
|
||||||
|
|
||||||
|
$result = core_course_external::get_user_administration_options($courses);
|
||||||
|
$result = external_api::clean_returnvalue(core_course_external::get_user_administration_options_returns(), $result);
|
||||||
|
|
||||||
|
$this->assertCount(0, $result['warnings']);
|
||||||
|
$this->assertCount(3, $result['courses']);
|
||||||
|
|
||||||
|
foreach ($result['courses'] as $course) {
|
||||||
|
$adminoptions = new stdClass;
|
||||||
|
foreach ($course['options'] as $option) {
|
||||||
|
$adminoptions->{$option['name']} = $option['available'];
|
||||||
|
}
|
||||||
|
if ($course['id'] == SITEID) {
|
||||||
|
$this->assertCount(6, $course['options']);
|
||||||
|
$this->assertFalse($adminoptions->update);
|
||||||
|
$this->assertFalse($adminoptions->filters);
|
||||||
|
$this->assertFalse($adminoptions->reports);
|
||||||
|
$this->assertFalse($adminoptions->backup);
|
||||||
|
$this->assertFalse($adminoptions->restore);
|
||||||
|
$this->assertFalse($adminoptions->files);
|
||||||
|
$this->assertTrue(!isset($adminoptions->tags));
|
||||||
|
} else {
|
||||||
|
$this->assertCount(15, $course['options']);
|
||||||
|
$this->assertFalse($adminoptions->update);
|
||||||
|
$this->assertFalse($adminoptions->filters);
|
||||||
|
$this->assertFalse($adminoptions->reports);
|
||||||
|
$this->assertFalse($adminoptions->backup);
|
||||||
|
$this->assertFalse($adminoptions->restore);
|
||||||
|
$this->assertFalse($adminoptions->files);
|
||||||
|
$this->assertFalse($adminoptions->tags);
|
||||||
|
$this->assertFalse($adminoptions->gradebook);
|
||||||
|
$this->assertFalse($adminoptions->outcomes);
|
||||||
|
$this->assertTrue($adminoptions->badges);
|
||||||
|
$this->assertFalse($adminoptions->import);
|
||||||
|
$this->assertFalse($adminoptions->publish);
|
||||||
|
$this->assertFalse($adminoptions->reset);
|
||||||
|
$this->assertFalse($adminoptions->roles);
|
||||||
|
$this->assertTrue($adminoptions->grades);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -310,6 +310,15 @@ $functions = array(
|
||||||
'type' => 'read',
|
'type' => 'read',
|
||||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||||
),
|
),
|
||||||
|
'core_course_get_user_administration_options' => array(
|
||||||
|
'classname' => 'core_course_external',
|
||||||
|
'methodname' => 'get_user_administration_options',
|
||||||
|
'classpath' => 'course/externallib.php',
|
||||||
|
'description' => 'Return a list of administration options in a set of courses that are avaialable or not for the current
|
||||||
|
user.',
|
||||||
|
'type' => 'read',
|
||||||
|
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||||
|
),
|
||||||
'core_enrol_get_course_enrolment_methods' => array(
|
'core_enrol_get_course_enrolment_methods' => array(
|
||||||
'classname' => 'core_enrol_external',
|
'classname' => 'core_enrol_external',
|
||||||
'methodname' => 'get_course_enrolment_methods',
|
'methodname' => 'get_course_enrolment_methods',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue