diff --git a/lib/db/services.php b/lib/db/services.php index 381978e07ea..63c05221589 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -1207,6 +1207,7 @@ $services = array( 'core_user_add_user_private_files', 'mod_assign_view_grading_table', 'mod_scorm_view_scorm', + 'mod_scorm_get_scorm_scoes', 'mod_scorm_get_scorm_attempt_count', 'mod_page_view_page', 'mod_resource_view_resource', diff --git a/mod/scorm/classes/external.php b/mod/scorm/classes/external.php index 32f909ff68f..4b056418303 100644 --- a/mod/scorm/classes/external.php +++ b/mod/scorm/classes/external.php @@ -182,4 +182,87 @@ 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_scoes_parameters() { + return new external_function_parameters( + array( + 'scormid' => new external_value(PARAM_INT, 'scorm instance id'), + 'organization' => new external_value(PARAM_RAW, 'organization id', VALUE_DEFAULT, '') + ) + ); + } + + /** + * Returns a list containing all the scoes data related to the given scorm id + * + * @param int $scormid the scorm id + * @param string $organization the organization id + * @return array warnings and the scoes data + * @since Moodle 3.0 + */ + public static function get_scorm_scoes($scormid, $organization = '') { + global $DB; + + $params = self::validate_parameters(self::get_scorm_scoes_parameters(), + array('scormid' => $scormid, 'organization' => $organization)); + + $scoes = array(); + $warnings = array(); + + $scorm = $DB->get_record('scorm', array('id' => $params['scormid']), '*', MUST_EXIST); + $cm = get_coursemodule_from_instance('scorm', $scorm->id); + + $context = context_module::instance($cm->id); + self::validate_context($context); + + // Check settings / permissions to view the SCORM. + scorm_require_available($scorm, true, $context); + + if (!$scoes = scorm_get_scoes($scorm->id, $params['organization'])) { + // Function scorm_get_scoes return false, not an empty array. + $scoes = array(); + } + + $result = array(); + $result['scoes'] = $scoes; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Describes the get_scorm_scoes return value. + * + * @return external_single_structure + * @since Moodle 3.0 + */ + public static function get_scorm_scoes_returns() { + + return new external_single_structure( + array( + 'scoes' => new external_multiple_structure( + new external_single_structure( + array( + 'id' => new external_value(PARAM_INT, 'sco id'), + 'scorm' => new external_value(PARAM_INT, 'scorm id'), + 'manifest' => new external_value(PARAM_NOTAGS, 'manifest id'), + 'organization' => new external_value(PARAM_NOTAGS, 'organization id'), + 'parent' => new external_value(PARAM_NOTAGS, 'parent'), + 'identifier' => new external_value(PARAM_NOTAGS, 'identifier'), + 'launch' => new external_value(PARAM_NOTAGS, 'launch file'), + 'scormtype' => new external_value(PARAM_ALPHA, 'scorm type (asset, sco)'), + 'title' => new external_value(PARAM_NOTAGS, 'sco title'), + 'sortorder' => new external_value(PARAM_INT, 'sort order'), + ), 'SCORM SCO data' + ) + ), + 'warnings' => new external_warnings(), + ) + ); + } + } diff --git a/mod/scorm/db/services.php b/mod/scorm/db/services.php index a8f75dec90d..a72f5e98139 100644 --- a/mod/scorm/db/services.php +++ b/mod/scorm/db/services.php @@ -41,4 +41,12 @@ $functions = array( 'type' => 'read', 'capabilities' => '' ), + + 'mod_scorm_get_scorm_scoes' => array( + 'classname' => 'mod_scorm_external', + 'methodname' => 'get_scorm_scoes', + 'description' => 'Returns a list containing all the scoes data related to the given scorm id', + 'type' => 'read', + 'capabilities' => '' + ), ); diff --git a/mod/scorm/tests/externallib_test.php b/mod/scorm/tests/externallib_test.php index 017ab980f5c..25047617db6 100644 --- a/mod/scorm/tests/externallib_test.php +++ b/mod/scorm/tests/externallib_test.php @@ -201,4 +201,88 @@ class mod_scorm_external_testcase extends externallib_advanced_testcase { $this->setExpectedException('moodle_exception'); mod_scorm_external::get_scorm_attempt_count($this->scorm->id, -1); } + + /** + * Test get scorm scoes + */ + public function test_mod_scorm_get_scorm_scoes() { + global $DB; + + $this->resetAfterTest(true); + + // Create users. + $student = 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, dates restriction. + $record = new stdClass(); + $record->course = $course->id; + $record->timeopen = time() + DAYSECS; + $record->timeclose = $record->timeopen + DAYSECS; + $scorm = self::getDataGenerator()->create_module('scorm', $record); + + // Users enrolments. + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); + $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual'); + $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual'); + + // Retrieve my scoes, warning!. + try { + mod_scorm_external::get_scorm_scoes($scorm->id); + $this->fail('Exception expected due to invalid dates.'); + } catch (moodle_exception $e) { + $this->assertEquals('notopenyet', $e->errorcode); + } + + $scorm->timeopen = time() - DAYSECS; + $scorm->timeclose = time() - HOURSECS; + $DB->update_record('scorm', $scorm); + + try { + mod_scorm_external::get_scorm_scoes($scorm->id); + $this->fail('Exception expected due to invalid dates.'); + } catch (moodle_exception $e) { + $this->assertEquals('expired', $e->errorcode); + } + + // Retrieve my scoes, user with permission. + self::setUser($teacher); + $result = mod_scorm_external::get_scorm_scoes($scorm->id); + $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_scoes_returns(), $result); + $this->assertCount(2, $result['scoes']); + $this->assertCount(0, $result['warnings']); + + $scoes = scorm_get_scoes($scorm->id); + $sco = array_shift($scoes); + $this->assertEquals((array) $sco, $result['scoes'][0]); + + $sco = array_shift($scoes); + // Remove specific sco data. + unset($sco->isvisible); + unset($sco->parameters); + $this->assertEquals((array) $sco, $result['scoes'][1]); + + // Use organization. + $organization = 'golf_sample_default_org'; + $result = mod_scorm_external::get_scorm_scoes($scorm->id, $organization); + $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_scoes_returns(), $result); + $this->assertCount(1, $result['scoes']); + $this->assertEquals($organization, $result['scoes'][0]['organization']); + $this->assertCount(0, $result['warnings']); + + // Test invalid instance id. + try { + mod_scorm_external::get_scorm_scoes(0); + $this->fail('Exception expected due to invalid instance id.'); + } catch (moodle_exception $e) { + $this->assertEquals('invalidrecord', $e->errorcode); + } + } } diff --git a/mod/scorm/version.php b/mod/scorm/version.php index 067889d67d5..9ba9f136810 100644 --- a/mod/scorm/version.php +++ b/mod/scorm/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015091400; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2015091402; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2015050500; // Requires this Moodle version. $plugin->component = 'mod_scorm'; // Full name of the plugin (used for diagnostics). $plugin->cron = 300; diff --git a/version.php b/version.php index c4d349438a3..4d951627695 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2015091000.03; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2015091000.05; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.