Merge branch 'MDL-50557-master' of git://github.com/jleyva/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2015-09-16 23:24:39 +02:00
commit 115c5270cc
4 changed files with 198 additions and 0 deletions

View File

@ -1210,6 +1210,7 @@ $services = array(
'mod_scorm_get_scorm_scoes',
'mod_scorm_get_scorm_user_data',
'mod_scorm_insert_scorm_tracks',
'mod_scorm_get_scorm_sco_tracks',
'mod_scorm_get_scorm_attempt_count',
'mod_page_view_page',
'mod_resource_view_resource',

View File

@ -490,4 +490,107 @@ class mod_scorm_external extends external_api {
)
);
}
/**
* Describes the parameters for get_scorms_by_courses.
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function get_scorm_sco_tracks_parameters() {
return new external_function_parameters(
array(
'scoid' => new external_value(PARAM_INT, 'sco id'),
'userid' => new external_value(PARAM_INT, 'user id'),
'attempt' => new external_value(PARAM_INT, 'attempt number (0 for last attempt)', VALUE_DEFAULT, 0)
)
);
}
/**
* Retrieves SCO tracking data for the given user id and attempt number
*
* @param int $scoid the sco id
* @param int $userid the user id
* @param int $attempt the attempt number
* @return array warnings and the scoes data
* @since Moodle 3.0
*/
public static function get_scorm_sco_tracks($scoid, $userid, $attempt = 0) {
global $USER, $DB;
$params = self::validate_parameters(self::get_scorm_sco_tracks_parameters(),
array('scoid' => $scoid, 'userid' => $userid, 'attempt' => $attempt));
$tracks = array();
$warnings = array();
$sco = scorm_get_sco($params['scoid'], SCO_ONLY);
if (!$sco) {
throw new moodle_exception('cannotfindsco', 'scorm');
}
$scorm = $DB->get_record('scorm', array('id' => $sco->scorm), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('scorm', $scorm->id);
$context = context_module::instance($cm->id);
self::validate_context($context);
// Validate the user obtaining the context, it will fail if the user doesn't exists or have been deleted.
context_user::instance($params['userid']);
// Extra checks so only users with permissions can view other users attempts.
if ($USER->id != $params['userid']) {
require_capability('mod/scorm:viewreport', $context);
}
scorm_require_available($scorm, true, $context);
if (empty($params['attempt'])) {
$params['attempt'] = scorm_get_last_attempt($scorm->id, $params['userid']);
}
if ($scormtracks = scorm_get_tracks($sco->id, $params['userid'], $params['attempt'])) {
foreach ($scormtracks as $element => $value) {
$tracks[] = array(
'element' => $element,
'value' => $value,
);
}
}
$result = array();
$result['data']['attempt'] = $params['attempt'];
$result['data']['tracks'] = $tracks;
$result['warnings'] = $warnings;
return $result;
}
/**
* Describes the get_scorm_sco_tracks return value.
*
* @return external_single_structure
* @since Moodle 3.0
*/
public static function get_scorm_sco_tracks_returns() {
return new external_single_structure(
array(
'data' => new external_single_structure(
array(
'attempt' => new external_value(PARAM_INT, 'Attempt number'),
'tracks' => new external_multiple_structure(
new external_single_structure(
array(
'element' => new external_value(PARAM_RAW, 'Element name'),
'value' => new external_value(PARAM_RAW, 'Element value')
), 'Tracks data'
)
),
), 'SCO data'
),
'warnings' => new external_warnings(),
)
);
}
}

View File

@ -68,4 +68,12 @@ $functions = array(
'type' => 'write',
'capabilities' => 'mod/scorm:savetrack'
),
'mod_scorm_get_scorm_sco_tracks' => array(
'classname' => 'mod_scorm_external',
'methodname' => 'get_scorm_sco_tracks',
'description' => 'Retrieves SCO tracking data for the given user id and attempt number',
'type' => 'read',
'capabilities' => ''
),
);

View File

@ -433,4 +433,90 @@ class mod_scorm_external_testcase extends externallib_advanced_testcase {
$expectedkeys = array_keys($trackids);
$this->assertEquals(asort($expectedkeys), asort($result['trackids']));
}
/**
* Test get scorm sco tracks
*/
public function test_mod_scorm_get_scorm_sco_tracks() {
global $DB;
$this->resetAfterTest(true);
// Create users.
$student = self::getDataGenerator()->create_user();
$otherstudent = self::getDataGenerator()->create_user();
$teacher = self::getDataGenerator()->create_user();
// Set to the student user.
self::setUser($student);
// Create courses to add the modules.
$course = self::getDataGenerator()->create_course();
// First scorm.
$record = new stdClass();
$record->course = $course->id;
$scorm = self::getDataGenerator()->create_module('scorm', $record);
// Users enrolments.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
// Create attempts.
$scoes = scorm_get_scoes($scorm->id);
$sco = array_shift($scoes);
scorm_insert_track($student->id, $scorm->id, $sco->id, 1, 'cmi.core.lesson_status', 'completed');
scorm_insert_track($student->id, $scorm->id, $sco->id, 1, 'cmi.core.score.raw', '80');
scorm_insert_track($student->id, $scorm->id, $sco->id, 2, 'cmi.core.lesson_status', 'completed');
$result = mod_scorm_external::get_scorm_sco_tracks($sco->id, $student->id, 1);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_sco_tracks_returns(), $result);
// 7 default elements + 2 custom ones.
$this->assertCount(9, $result['data']['tracks']);
$this->assertEquals(1, $result['data']['attempt']);
// Find our tracking data.
$found = 0;
foreach ($result['data']['tracks'] as $userdata) {
if ($userdata['element'] == 'cmi.core.lesson_status' and $userdata['value'] == 'completed') {
$found++;
}
if ($userdata['element'] == 'cmi.core.score.raw' and $userdata['value'] == '80') {
$found++;
}
}
$this->assertEquals(2, $found);
// Capabilities check.
try {
mod_scorm_external::get_scorm_sco_tracks($sco->id, $otherstudent->id);
$this->fail('Exception expected due to invalid instance id.');
} catch (required_capability_exception $e) {
$this->assertEquals('nopermissions', $e->errorcode);
}
self::setUser($teacher);
// Ommit the attempt parameter, the function should calculate the last attempt.
$result = mod_scorm_external::get_scorm_sco_tracks($sco->id, $student->id);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_sco_tracks_returns(), $result);
// 7 default elements + 1 custom one.
$this->assertCount(8, $result['data']['tracks']);
$this->assertEquals(2, $result['data']['attempt']);
// Test invalid instance id.
try {
mod_scorm_external::get_scorm_sco_tracks(0, 1);
$this->fail('Exception expected due to invalid instance id.');
} catch (moodle_exception $e) {
$this->assertEquals('cannotfindsco', $e->errorcode);
}
// Invalid user.
try {
mod_scorm_external::get_scorm_sco_tracks($sco->id, 0);
$this->fail('Exception expected due to invalid instance id.');
} catch (moodle_exception $e) {
$this->assertEquals('invaliduser', $e->errorcode);
}
}
}