MDL-57664 mod_lesson: New Ws mod_lesson_get_content_pages_viewed
authorJuan Leyva <juanleyvadelgado@gmail.com>
Mon, 16 Jan 2017 17:06:05 +0000 (18:06 +0100)
committerDavid Monllao <david.monllao@gmail.com>
Mon, 20 Mar 2017 10:16:55 +0000 (11:16 +0100)
mod/lesson/classes/external.php
mod/lesson/db/services.php
mod/lesson/tests/external_test.php
mod/lesson/version.php

index 5958153..05f4ca1 100644 (file)
@@ -746,4 +746,90 @@ class mod_lesson_external extends external_api {
             )
         );
     }
+
+    /**
+     * Describes the parameters for get_content_pages_viewed.
+     *
+     * @return external_external_function_parameters
+     * @since Moodle 3.3
+     */
+    public static function get_content_pages_viewed_parameters() {
+        return new external_function_parameters (
+            array(
+                'lessonid' => new external_value(PARAM_INT, 'lesson instance id'),
+                'lessonattempt' => new external_value(PARAM_INT, 'lesson attempt number'),
+                'userid' => new external_value(PARAM_INT, 'the user id (empty for current user)', VALUE_DEFAULT, null),
+            )
+        );
+    }
+
+    /**
+     * Return the list of content pages viewed by a user during a lesson attempt.
+     *
+     * @param int $lessonid lesson instance id
+     * @param int $lessonattempt lesson attempt number
+     * @param int $userid only fetch attempts of the given user
+     * @return array of warnings and page attempts
+     * @since Moodle 3.3
+     * @throws moodle_exception
+     */
+    public static function get_content_pages_viewed($lessonid, $lessonattempt, $userid = null) {
+        global $USER;
+
+        $params = array(
+            'lessonid' => $lessonid,
+            'lessonattempt' => $lessonattempt,
+            'userid' => $userid,
+        );
+        $params = self::validate_parameters(self::get_content_pages_viewed_parameters(), $params);
+        $warnings = array();
+
+        list($lesson, $course, $cm, $context) = self::validate_lesson($params['lessonid']);
+
+        // Default value for userid.
+        if (empty($params['userid'])) {
+            $params['userid'] = $USER->id;
+        }
+
+        // Extra checks so only users with permissions can view other users attempts.
+        if ($USER->id != $params['userid']) {
+            self::check_can_view_user_data($params['userid'], $course, $cm, $context);
+        }
+
+        $pages = $lesson->get_content_pages_viewed($params['lessonattempt'], $params['userid']);
+
+        $result = array();
+        $result['pages'] = $pages;
+        $result['warnings'] = $warnings;
+        return $result;
+    }
+
+    /**
+     * Describes the get_content_pages_viewed return value.
+     *
+     * @return external_single_structure
+     * @since Moodle 3.3
+     */
+    public static function get_content_pages_viewed_returns() {
+        return new external_single_structure(
+            array(
+                'pages' => new external_multiple_structure(
+                    new external_single_structure(
+                        array(
+                            'id' => new external_value(PARAM_INT, 'The attempt id.'),
+                            'lessonid' => new external_value(PARAM_INT, 'The lesson id.'),
+                            'pageid' => new external_value(PARAM_INT, 'The page id.'),
+                            'userid' => new external_value(PARAM_INT, 'The user who viewed the page.'),
+                            'retry' => new external_value(PARAM_INT, 'The lesson attempt number.'),
+                            'flag' => new external_value(PARAM_INT, '1 if the next page was calculated randomly.'),
+                            'timeseen' => new external_value(PARAM_INT, 'The time the page was seen.'),
+                            'nextpageid' => new external_value(PARAM_INT, 'The next page chosen id.'),
+                        ),
+                        'The content pages viewed.'
+                    )
+                ),
+                'warnings' => new external_warnings(),
+            )
+        );
+    }
 }
index ec6f325..b6bc6aa 100644 (file)
@@ -76,4 +76,12 @@ $functions = array(
         'capabilities'  => 'mod/lesson:view',
         'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
     ),
+    'mod_lesson_get_content_pages_viewed' => array(
+        'classname'     => 'mod_lesson_external',
+        'methodname'    => 'get_content_pages_viewed',
+        'description'   => 'Return the list of content pages viewed by a user during a lesson attempt.',
+        'type'          => 'read',
+        'capabilities'  => 'mod/lesson:view',
+        'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
+    ),
 );
index 57db5de..5168a57 100644 (file)
@@ -579,4 +579,54 @@ class mod_lesson_external_testcase extends externallib_advanced_testcase {
         $this->assertEquals(0, $result['nmanual']);
         $this->assertEquals(0, $result['manualpoints']);
     }
+
+    /**
+     * Test get_content_pages_viewed
+     */
+    public function test_get_content_pages_viewed() {
+        global $DB;
+
+        // Create another content pages.
+        $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
+        $page3 = $lessongenerator->create_content($this->lesson);
+
+        $branch1 = new stdClass;
+        $branch1->lessonid = $this->lesson->id;
+        $branch1->userid = $this->student->id;
+        $branch1->pageid = $this->page1->id;
+        $branch1->retry = 1;
+        $branch1->flag = 0;
+        $branch1->timeseen = time();
+        $branch1->nextpageid = $page3->id;
+        $branch1->id = $DB->insert_record("lesson_branch", $branch1);
+
+        $branch2 = new stdClass;
+        $branch2->lessonid = $this->lesson->id;
+        $branch2->userid = $this->student->id;
+        $branch2->pageid = $page3->id;
+        $branch2->retry = 1;
+        $branch2->flag = 0;
+        $branch2->timeseen = time() + 1;
+        $branch2->nextpageid = 0;
+        $branch2->id = $DB->insert_record("lesson_branch", $branch2);
+
+        // Test first attempt.
+        $result = mod_lesson_external::get_content_pages_viewed($this->lesson->id, 1, $this->student->id);
+        $result = external_api::clean_returnvalue(mod_lesson_external::get_content_pages_viewed_returns(), $result);
+        $this->assertCount(0, $result['warnings']);
+        $this->assertCount(2, $result['pages']);
+        foreach ($result['pages'] as $page) {
+            if ($page['id'] == $branch1->id) {
+                $this->assertEquals($branch1, (object) $page);
+            } else {
+                $this->assertEquals($branch2, (object) $page);
+            }
+        }
+
+        // Attempt without pages viewed.
+        $result = mod_lesson_external::get_content_pages_viewed($this->lesson->id, 3, $this->student->id);
+        $result = external_api::clean_returnvalue(mod_lesson_external::get_content_pages_viewed_returns(), $result);
+        $this->assertCount(0, $result['warnings']);
+        $this->assertCount(0, $result['pages']);
+    }
 }
index ec17282..7162569 100644 (file)
@@ -24,7 +24,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2016120505;     // The current module version (Date: YYYYMMDDXX)
+$plugin->version   = 2016120506;     // The current module version (Date: YYYYMMDDXX)
 $plugin->requires  = 2016112900;    // Requires this Moodle version
 $plugin->component = 'mod_lesson'; // Full name of the plugin (used for diagnostics)
 $plugin->cron      = 0;