MDL-50352 scorm: New Web Service mod_scorm_get_scorm_user_data
authorJuan Leyva <juanleyvadelgado@gmail.com>
Wed, 10 Jun 2015 14:55:50 +0000 (16:55 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Wed, 16 Sep 2015 17:25:19 +0000 (19:25 +0200)
lib/db/services.php
mod/scorm/classes/external.php
mod/scorm/db/services.php
mod/scorm/tests/externallib_test.php

index 63c0522..538d945 100644 (file)
@@ -1208,6 +1208,7 @@ $services = array(
             'mod_assign_view_grading_table',
             'mod_scorm_view_scorm',
             'mod_scorm_get_scorm_scoes',
+            'mod_scorm_get_scorm_user_data',
             'mod_scorm_get_scorm_attempt_count',
             'mod_page_view_page',
             'mod_resource_view_resource',
index 4b05641..03bbe17 100644 (file)
@@ -265,4 +265,128 @@ class mod_scorm_external extends external_api {
         );
     }
 
+    /**
+     * Describes the parameters for get_scorm_user_data.
+     *
+     * @return external_function_parameters
+     * @since Moodle 3.0
+     */
+    public static function get_scorm_user_data_parameters() {
+        return new external_function_parameters(
+            array(
+                'scormid' => new external_value(PARAM_INT, 'scorm instance id'),
+                'attempt' => new external_value(PARAM_INT, 'attempt number')
+            )
+        );
+    }
+
+    /**
+     * Retrieves user tracking and SCO data and default SCORM values
+     *
+     * @param int $scormid the scorm id
+     * @param int $attempt the attempt number
+     * @return array warnings and the scoes data
+     * @throws  moodle_exception
+     * @since Moodle 3.0
+     */
+    public static function get_scorm_user_data($scormid, $attempt) {
+        global $CFG, $DB;
+
+        $params = self::validate_parameters(self::get_scorm_user_data_parameters(),
+                                            array('scormid' => $scormid, 'attempt' => $attempt));
+
+        $data = 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);
+
+        scorm_require_available($scorm, true, $context);
+
+        $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR));
+        if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
+            $scorm->version = 'scorm_12';
+        }
+        require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
+
+        if ($scoes = scorm_get_scoes($scorm->id)) {
+            $def = new stdClass();
+            $user = new stdClass();
+
+            foreach ($scoes as $sco) {
+                $def->{$sco->id} = new stdClass();
+                $user->{$sco->id} = new stdClass();
+                // We force mode normal, this can be override by the client at any time.
+                $def->{$sco->id} = get_scorm_default($user->{$sco->id}, $scorm, $sco->id, $params['attempt'], 'normal');
+
+                $userdata = array();
+                $defaultdata = array();
+
+                foreach ((array) $user->{$sco->id} as $key => $val) {
+                    $userdata[] = array(
+                        'element' => $key,
+                        'value' => $val
+                    );
+                }
+                foreach ($def->{$sco->id} as $key => $val) {
+                    $defaultdata[] = array(
+                        'element' => $key,
+                        'value' => $val
+                    );
+                }
+
+                $data[] = array(
+                    'scoid' => $sco->id,
+                    'userdata' => $userdata,
+                    'defaultdata' => $defaultdata,
+                );
+            }
+        }
+
+        $result = array();
+        $result['data'] = $data;
+        $result['warnings'] = $warnings;
+        return $result;
+    }
+
+    /**
+     * Describes the get_scorm_user_data return value.
+     *
+     * @return external_single_structure
+     * @since Moodle 3.0
+     */
+    public static function get_scorm_user_data_returns() {
+
+        return new external_single_structure(
+            array(
+                'data' => new external_multiple_structure(
+                    new external_single_structure(
+                        array(
+                            'scoid' => new external_value(PARAM_INT, 'sco id'),
+                            'userdata' => 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')
+                                                )
+                                            )
+                                          ),
+                            'defaultdata' => 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')
+                                                    )
+                                                )
+                                             ),
+                        ), 'SCO data'
+                    )
+                ),
+                'warnings' => new external_warnings(),
+            )
+        );
+    }
 }
index a72f5e9..540518a 100644 (file)
@@ -49,4 +49,12 @@ $functions = array(
         'type' => 'read',
         'capabilities' => ''
     ),
+
+    'mod_scorm_get_scorm_user_data' => array(
+        'classname' => 'mod_scorm_external',
+        'methodname' => 'get_scorm_user_data',
+        'description' => 'Retrieves user tracking and SCO data and default SCORM values',
+        'type' => 'read',
+        'capabilities' => ''
+    ),
 );
index 2504761..7bc9f8a 100644 (file)
@@ -285,4 +285,66 @@ class mod_scorm_external_testcase extends externallib_advanced_testcase {
             $this->assertEquals('invalidrecord', $e->errorcode);
         }
     }
+
+    /*
+     * Test get scorm user data
+     */
+    public function test_mod_scorm_get_scorm_user_data() {
+        global $DB;
+
+        $this->resetAfterTest(true);
+
+        // Create users.
+        $student1 = self::getDataGenerator()->create_user();
+        $teacher = self::getDataGenerator()->create_user();
+
+        // Set to the student user.
+        self::setUser($student1);
+
+        // 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($student1->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($student1->id, $scorm->id, $sco->id, 1, 'cmi.core.lesson_status', 'completed');
+        scorm_insert_track($student1->id, $scorm->id, $sco->id, 1, 'cmi.core.score.raw', '80');
+        scorm_insert_track($student1->id, $scorm->id, $sco->id, 2, 'cmi.core.lesson_status', 'completed');
+
+        $result = mod_scorm_external::get_scorm_user_data($scorm->id, 1);
+        $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_user_data_returns(), $result);
+        $this->assertCount(2, $result['data']);
+        // Find our tracking data.
+        $found = 0;
+        foreach ($result['data'] as $scodata) {
+            foreach ($scodata['userdata'] 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);
+
+        // Test invalid instance id.
+        try {
+             mod_scorm_external::get_scorm_user_data(0, 1);
+            $this->fail('Exception expected due to invalid instance id.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('invalidrecord', $e->errorcode);
+        }
+    }
 }