Merge branch 'MDL-50015-master' of git://github.com/jleyva/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Wed, 9 Sep 2015 23:42:06 +0000 (01:42 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Wed, 9 Sep 2015 23:42:06 +0000 (01:42 +0200)
lib/db/services.php
mod/imscp/classes/external.php [new file with mode: 0644]
mod/imscp/db/services.php [new file with mode: 0644]
mod/imscp/lib.php
mod/imscp/tests/externallib_test.php [new file with mode: 0644]
mod/imscp/tests/lib_test.php
mod/imscp/version.php
mod/imscp/view.php
version.php

index aed15b9..ba37842 100644 (file)
@@ -1172,6 +1172,7 @@ $services = array(
             'mod_choice_get_choice_options',
             'mod_choice_submit_choice_response',
             'mod_choice_view_choice',
+            'mod_imscp_view_imscp',
             ),
         'enabled' => 0,
         'restrictedusers' => 0,
diff --git a/mod/imscp/classes/external.php b/mod/imscp/classes/external.php
new file mode 100644 (file)
index 0000000..16cc25b
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * IMSCP external API
+ *
+ * @package    mod_imscp
+ * @category   external
+ * @copyright  2015 Juan Leyva <juan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since      Moodle 3.0
+ */
+
+defined('MOODLE_INTERNAL') || die;
+
+require_once("$CFG->libdir/externallib.php");
+
+/**
+ * IMSCP external functions
+ *
+ * @package    mod_imscp
+ * @category   external
+ * @copyright  2015 Juan Leyva <juan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since      Moodle 3.0
+ */
+class mod_imscp_external extends external_api {
+
+    /**
+     * Returns description of method parameters
+     *
+     * @return external_function_parameters
+     * @since Moodle 3.0
+     */
+    public static function view_imscp_parameters() {
+        return new external_function_parameters(
+            array(
+                'imscpid' => new external_value(PARAM_INT, 'imscp instance id')
+            )
+        );
+    }
+
+    /**
+     * Simulate the imscp/view.php web interface page: trigger events, completion, etc...
+     *
+     * @param int $imscpid the imscp instance id
+     * @return array of warnings and status result
+     * @since Moodle 3.0
+     * @throws moodle_exception
+     */
+    public static function view_imscp($imscpid) {
+        global $DB, $CFG;
+        require_once($CFG->dirroot . "/mod/imscp/lib.php");
+
+        $params = self::validate_parameters(self::view_imscp_parameters(),
+                                            array(
+                                                'imscpid' => $imscpid
+                                            ));
+        $warnings = array();
+
+        // Request and permission validation.
+        $imscp = $DB->get_record('imscp', array('id' => $params['imscpid']), '*', MUST_EXIST);
+        list($course, $cm) = get_course_and_cm_from_instance($imscp, 'imscp');
+
+        $context = context_module::instance($cm->id);
+        self::validate_context($context);
+
+        require_capability('mod/imscp:view', $context);
+
+        // Call the imscp/lib API.
+        imscp_view($imscp, $course, $cm, $context);
+
+        $result = array();
+        $result['status'] = true;
+        $result['warnings'] = $warnings;
+        return $result;
+    }
+
+    /**
+     * Returns description of method result value
+     *
+     * @return external_description
+     * @since Moodle 3.0
+     */
+    public static function view_imscp_returns() {
+        return new external_single_structure(
+            array(
+                'status' => new external_value(PARAM_BOOL, 'status: true if success'),
+                'warnings' => new external_warnings()
+            )
+        );
+    }
+
+}
diff --git a/mod/imscp/db/services.php b/mod/imscp/db/services.php
new file mode 100644 (file)
index 0000000..9eaff89
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * IMSCP external functions and service definitions.
+ *
+ * @package    mod_imscp
+ * @category   external
+ * @copyright  2015 Juan Leyva <juan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since      Moodle 3.0
+ */
+
+defined('MOODLE_INTERNAL') || die;
+
+$functions = array(
+
+    'mod_imscp_view_imscp' => array(
+        'classname'     => 'mod_imscp_external',
+        'methodname'    => 'view_imscp',
+        'description'   => 'Simulate the view.php web interface imscp: trigger events, completion, etc...',
+        'type'          => 'write',
+        'capabilities'  => 'mod/imscp:view'
+    ),
+
+);
index 2304398..973ef14 100644 (file)
@@ -416,3 +416,31 @@ function imscp_export_contents($cm, $baseurl) {
 
     return $contents;
 }
+
+/**
+ * Mark the activity completed (if required) and trigger the course_module_viewed event.
+ *
+ * @param  stdClass $imscp   imscp object
+ * @param  stdClass $course     course object
+ * @param  stdClass $cm         course module object
+ * @param  stdClass $context    context object
+ * @since Moodle 3.0
+ */
+function imscp_view($imscp, $course, $cm, $context) {
+
+    // Trigger course_module_viewed event.
+    $params = array(
+        'context' => $context,
+        'objectid' => $imscp->id
+    );
+
+    $event = \mod_imscp\event\course_module_viewed::create($params);
+    $event->add_record_snapshot('course_modules', $cm);
+    $event->add_record_snapshot('course', $course);
+    $event->add_record_snapshot('imscp', $imscp);
+    $event->trigger();
+
+    // Completion.
+    $completion = new completion_info($course);
+    $completion->set_module_viewed($cm);
+}
diff --git a/mod/imscp/tests/externallib_test.php b/mod/imscp/tests/externallib_test.php
new file mode 100644 (file)
index 0000000..b02560d
--- /dev/null
@@ -0,0 +1,112 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * External mod_imscp functions unit tests
+ *
+ * @package    mod_imscp
+ * @category   external
+ * @copyright  2015 Juan Leyva <juan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since      Moodle 3.0
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+global $CFG;
+
+require_once($CFG->dirroot . '/webservice/tests/helpers.php');
+
+/**
+ * External mod_imscp functions unit tests
+ *
+ * @package    mod_imscp
+ * @category   external
+ * @copyright  2015 Juan Leyva <juan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since      Moodle 3.0
+ */
+class mod_imscp_external_testcase extends externallib_advanced_testcase {
+
+    /**
+     * Test view_imscp
+     */
+    public function test_view_imscp() {
+        global $DB;
+
+        $this->resetAfterTest(true);
+
+        $this->setAdminUser();
+        // Setup test data.
+        $course = $this->getDataGenerator()->create_course();
+        $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
+        $context = context_module::instance($imscp->cmid);
+        $cm = get_coursemodule_from_instance('imscp', $imscp->id);
+
+        // Test invalid instance id.
+        try {
+            mod_imscp_external::view_imscp(0);
+            $this->fail('Exception expected due to invalid mod_imscp instance id.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('invalidrecord', $e->errorcode);
+        }
+
+        // Test not-enrolled user.
+        $user = self::getDataGenerator()->create_user();
+        $this->setUser($user);
+        try {
+            mod_imscp_external::view_imscp($imscp->id);
+            $this->fail('Exception expected due to not enrolled user.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('requireloginerror', $e->errorcode);
+        }
+
+        // Test user with full capabilities.
+        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
+        $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+
+        $result = mod_imscp_external::view_imscp($imscp->id);
+        $result = external_api::clean_returnvalue(mod_imscp_external::view_imscp_returns(), $result);
+
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = array_shift($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_imscp\event\course_module_viewed', $event);
+        $this->assertEquals($context, $event->get_context());
+        $moodleurl = new \moodle_url('/mod/imscp/view.php', array('id' => $cm->id));
+        $this->assertEquals($moodleurl, $event->get_url());
+        $this->assertEventContextNotUsed($event);
+        $this->assertNotEmpty($event->get_name());
+
+        // Test user with no capabilities.
+        // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
+        assign_capability('mod/imscp:view', CAP_PROHIBIT, $studentrole->id, $context->id);
+        accesslib_clear_all_caches_for_unit_testing();
+
+        try {
+            mod_imscp_external::view_imscp($imscp->id);
+            $this->fail('Exception expected due to missing capability.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('nopermissions', $e->errorcode);
+        }
+
+    }
+}
index a28f7eb..bffbb65 100644 (file)
@@ -64,4 +64,47 @@ class mod_imscp_lib_testcase extends advanced_testcase {
         $this->assertEquals(json_encode(unserialize($imscp->structure)), $contents[0]['content']);
 
     }
+
+    /**
+     * Test imscp_view
+     * @return void
+     */
+    public function test_imscp_view() {
+        global $CFG;
+
+        $CFG->enablecompletion = 1;
+        $this->resetAfterTest();
+
+        $this->setAdminUser();
+        // Setup test data.
+        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
+        $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id),
+                                                            array('completion' => 2, 'completionview' => 1));
+        $context = context_module::instance($imscp->cmid);
+        $cm = get_coursemodule_from_instance('imscp', $imscp->id);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+
+        imscp_view($imscp, $course, $cm, $context);
+
+        $events = $sink->get_events();
+        // 2 additional events thanks to completion.
+        $this->assertCount(3, $events);
+        $event = array_shift($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_imscp\event\course_module_viewed', $event);
+        $this->assertEquals($context, $event->get_context());
+        $moodleurl = new \moodle_url('/mod/imscp/view.php', array('id' => $cm->id));
+        $this->assertEquals($moodleurl, $event->get_url());
+        $this->assertEventContextNotUsed($event);
+        $this->assertNotEmpty($event->get_name());
+
+        // Check completion status.
+        $completion = new completion_info($course);
+        $completiondata = $completion->get_data($cm);
+        $this->assertEquals(1, $completiondata->completionstate);
+
+    }
 }
index 714beab..ad04e01 100644 (file)
@@ -24,7 +24,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2015051100;  // The current module version (Date: YYYYMMDDXX).
+$plugin->version   = 2015051101;  // The current module version (Date: YYYYMMDDXX).
 $plugin->requires  = 2015050500;  // Requires this Moodle version.
 $plugin->component = 'mod_imscp'; // Full name of the plugin (used for diagnostics).
 $plugin->cron      = 0;
index ebd105a..e3c2867 100644 (file)
@@ -23,6 +23,7 @@
  */
 
 require('../../config.php');
+require_once($CFG->dirroot . '/mod/imscp/lib.php');
 require_once("$CFG->dirroot/mod/imscp/locallib.php");
 require_once($CFG->libdir . '/completionlib.php');
 
@@ -44,17 +45,8 @@ require_course_login($course, true, $cm);
 $context = context_module::instance($cm->id);
 require_capability('mod/imscp:view', $context);
 
-$params = array(
-    'context' => $context,
-    'objectid' => $imscp->id
-);
-$event = \mod_imscp\event\course_module_viewed::create($params);
-$event->add_record_snapshot('imscp', $imscp);
-$event->trigger();
-
-// Update 'viewed' state if required by completion system.
-$completion = new completion_info($course);
-$completion->set_module_viewed($cm);
+// Completion and trigger events.
+imscp_view($imscp, $course, $cm, $context);
 
 $PAGE->set_url('/mod/imscp/view.php', array('id' => $cm->id));
 $PAGE->requires->js('/mod/imscp/dummyapi.js', true);
index 7abced9..155a368 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2015090902.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2015090903.00;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.