diff --git a/mod/scorm/classes/external.php b/mod/scorm/classes/external.php index df9db5a1a4a1c..990c15f7fa5cd 100644 --- a/mod/scorm/classes/external.php +++ b/mod/scorm/classes/external.php @@ -226,6 +226,21 @@ public static function get_scorm_scoes($scormid, $organization = '') { if (!$scoes = scorm_get_scoes($scorm->id, $params['organization'])) { // Function scorm_get_scoes return false, not an empty array. $scoes = array(); + } else { + $scoreturnstructure = self::get_scorm_scoes_returns(); + foreach ($scoes as $sco) { + $extradata = array(); + foreach ($sco as $element => $value) { + // Check if the element is extra data (not a basic SCO element). + if (!isset($scoreturnstructure->keys['scoes']->content->keys[$element])) { + $extradata[] = array( + 'element' => $element, + 'value' => $value + ); + } + } + $sco->extradata = $extradata; + } } $result = array(); @@ -257,6 +272,14 @@ public static function get_scorm_scoes_returns() { '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'), + 'extradata' => 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') + ) + ), 'Additional SCO data', VALUE_OPTIONAL + ) ), 'SCORM SCO data' ) ), diff --git a/mod/scorm/tests/externallib_test.php b/mod/scorm/tests/externallib_test.php index dcddafb3897a8..39dc0fdca54d0 100644 --- a/mod/scorm/tests/externallib_test.php +++ b/mod/scorm/tests/externallib_test.php @@ -261,12 +261,27 @@ public function test_mod_scorm_get_scorm_scoes() { $scoes = scorm_get_scoes($scorm->id); $sco = array_shift($scoes); + $sco->extradata = array(); $this->assertEquals((array) $sco, $result['scoes'][0]); $sco = array_shift($scoes); - // Remove specific sco data. + $sco->extradata = array(); + $sco->extradata[] = array( + 'element' => 'isvisible', + 'value' => $sco->isvisible + ); + $sco->extradata[] = array( + 'element' => 'parameters', + 'value' => $sco->parameters + ); unset($sco->isvisible); unset($sco->parameters); + + // Sort the array (if we don't sort tests will fails for Postgres). + usort($result['scoes'][1]['extradata'], function($a, $b) { + return strcmp($a['element'], $b['element']); + }); + $this->assertEquals((array) $sco, $result['scoes'][1]); // Use organization. @@ -284,6 +299,47 @@ public function test_mod_scorm_get_scorm_scoes() { } catch (moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } + + } + + /** + * Test get scorm scoes (with a complex SCORM package) + */ + public function test_mod_scorm_get_scorm_scoes_complex_package() { + global $CFG; + + // As student. + self::setUser($this->student); + + $record = new stdClass(); + $record->course = $this->course->id; + $record->packagefilepath = $CFG->dirroot.'/mod/scorm/tests/packages/complexscorm.zip'; + $scorm = self::getDataGenerator()->create_module('scorm', $record); + + $result = mod_scorm_external::get_scorm_scoes($scorm->id); + $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_scoes_returns(), $result); + $this->assertCount(9, $result['scoes']); + $this->assertCount(0, $result['warnings']); + + $expectedscoes = array(); + $scoreturnstructure = mod_scorm_external::get_scorm_scoes_returns(); + $scoes = scorm_get_scoes($scorm->id); + foreach ($scoes as $sco) { + $sco->extradata = array(); + foreach ($sco as $element => $value) { + // Add the extra data to the extradata array and remove the object element. + if (!isset($scoreturnstructure->keys['scoes']->content->keys[$element])) { + $sco->extradata[] = array( + 'element' => $element, + 'value' => $value + ); + unset($sco->{$element}); + } + } + $expectedscoes[] = (array) $sco; + } + + $this->assertEquals($expectedscoes, $result['scoes']); } /* diff --git a/mod/scorm/tests/packages/complexscorm.zip b/mod/scorm/tests/packages/complexscorm.zip new file mode 100644 index 0000000000000..7c1eb9daf7cde Binary files /dev/null and b/mod/scorm/tests/packages/complexscorm.zip differ