MDL-64252 mod_scorm: New WS mod_scorm_get_scorm_access_information

This commit is contained in:
Juan Leyva 2019-02-06 15:11:13 +01:00
parent ec819146cc
commit b0cf7af4b7
4 changed files with 130 additions and 1 deletions

View File

@ -906,4 +906,74 @@ class mod_scorm_external extends external_api {
)
);
}
/**
* Describes the parameters for get_scorm_access_information.
*
* @return external_external_function_parameters
* @since Moodle 3.7
*/
public static function get_scorm_access_information_parameters() {
return new external_function_parameters (
array(
'scormid' => new external_value(PARAM_INT, 'scorm instance id.')
)
);
}
/**
* Return access information for a given scorm.
*
* @param int $scormid scorm instance id
* @return array of warnings and the access information
* @since Moodle 3.7
* @throws moodle_exception
*/
public static function get_scorm_access_information($scormid) {
global $DB;
$params = self::validate_parameters(self::get_scorm_access_information_parameters(), array('scormid' => $scormid));
// Request and permission validation.
$scorm = $DB->get_record('scorm', array('id' => $params['scormid']), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($scorm, 'scorm');
$context = context_module::instance($cm->id);
self::validate_context($context);
$result = array();
// Return all the available capabilities.
$capabilities = load_capability_def('mod_scorm');
foreach ($capabilities as $capname => $capdata) {
// Get fields like cansubmit so it is consistent with the access_information function implemented in other modules.
$field = 'can' . str_replace('mod/scorm:', '', $capname);
$result[$field] = has_capability($capname, $context);
}
$result['warnings'] = array();
return $result;
}
/**
* Describes the get_scorm_access_information return value.
*
* @return external_single_structure
* @since Moodle 3.7
*/
public static function get_scorm_access_information_returns() {
$structure = array(
'warnings' => new external_warnings()
);
$capabilities = load_capability_def('mod_scorm');
foreach ($capabilities as $capname => $capdata) {
// Get fields like cansubmit so it is consistent with the access_information function implemented in other modules.
$field = 'can' . str_replace('mod/scorm:', '', $capname);
$structure[$field] = new external_value(PARAM_BOOL, 'Whether the user has the capability ' . $capname . ' allowed.',
VALUE_OPTIONAL);
}
return new external_single_structure($structure);
}
}

View File

@ -101,4 +101,12 @@ $functions = array(
'capabilities' => '',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
'mod_scorm_get_scorm_access_information' => array(
'classname' => 'mod_scorm_external',
'methodname' => 'get_scorm_access_information',
'description' => 'Return capabilities information for a given scorm.',
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
);

View File

@ -868,4 +868,55 @@ class mod_scorm_external_testcase extends externallib_advanced_testcase {
$this->assertEquals('cannotfindsco', $e->errorcode);
}
}
/**
* Test mod_scorm_get_scorm_access_information.
*/
public function test_mod_scorm_get_scorm_access_information() {
global $DB;
$this->resetAfterTest(true);
$student = self::getDataGenerator()->create_user();
$course = self::getDataGenerator()->create_course();
// Create the scorm.
$record = new stdClass();
$record->course = $course->id;
$scorm = self::getDataGenerator()->create_module('scorm', $record);
$context = context_module::instance($scorm->cmid);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
self::setUser($student);
$result = mod_scorm_external::get_scorm_access_information($scorm->id);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_access_information_returns(), $result);
// Check default values for capabilities.
$enabledcaps = array('canskipview', 'cansavetrack', 'canviewscores');
unset($result['warnings']);
foreach ($result as $capname => $capvalue) {
if (in_array($capname, $enabledcaps)) {
$this->assertTrue($capvalue);
} else {
$this->assertFalse($capvalue);
}
}
// Now, unassign one capability.
unassign_capability('mod/scorm:viewscores', $studentrole->id);
array_pop($enabledcaps);
accesslib_clear_all_caches_for_unit_testing();
$result = mod_scorm_external::get_scorm_access_information($scorm->id);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_access_information_returns(), $result);
unset($result['warnings']);
foreach ($result as $capname => $capvalue) {
if (in_array($capname, $enabledcaps)) {
$this->assertTrue($capvalue);
} else {
$this->assertFalse($capvalue);
}
}
}
}

View File

@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2018123100; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2018123101; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2018112800; // Requires this Moodle version.
$plugin->component = 'mod_scorm'; // Full name of the plugin (used for diagnostics).