MDL-52556 mod_scorm: Return SCO extra data in get_scorm_scoes
authorJuan Leyva <juanleyvadelgado@gmail.com>
Fri, 18 Dec 2015 09:31:43 +0000 (10:31 +0100)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Mon, 21 Dec 2015 14:42:58 +0000 (15:42 +0100)
mod/scorm/classes/external.php
mod/scorm/tests/externallib_test.php
mod/scorm/tests/packages/complexscorm.zip [new file with mode: 0644]

index a67cb74..4ee409d 100644 (file)
@@ -226,6 +226,21 @@ class mod_scorm_external extends external_api {
         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 @@ class mod_scorm_external extends external_api {
                             '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'
                     )
                 ),
index d034c0c..4d3265a 100644 (file)
@@ -261,12 +261,27 @@ class mod_scorm_external_testcase extends externallib_advanced_testcase {
 
         $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 @@ class mod_scorm_external_testcase extends externallib_advanced_testcase {
         } 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 (file)
index 0000000..7c1eb9d
Binary files /dev/null and b/mod/scorm/tests/packages/complexscorm.zip differ