From f3fe1b8b35d4e085cedb59e951d794d61ba2f97d Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Fri, 17 Apr 2015 14:02:18 +0200 Subject: [PATCH 1/2] MDL-49793 url: Move logging and completion to new API --- mod/url/lib.php | 28 +++++++++++++++++++++++ mod/url/tests/lib_test.php | 46 +++++++++++++++++++++++++++++++++++++- mod/url/view.php | 16 +++---------- 3 files changed, 76 insertions(+), 14 deletions(-) diff --git a/mod/url/lib.php b/mod/url/lib.php index 03fedbbc551..527b394c70b 100644 --- a/mod/url/lib.php +++ b/mod/url/lib.php @@ -323,3 +323,31 @@ function url_dndupload_handle($uploadinfo) { return url_add_instance($data, null); } + +/** + * Mark the activity completed (if required) and trigger the course_module_viewed event. + * + * @param stdClass $url url object + * @param stdClass $course course object + * @param stdClass $cm course module object + * @param stdClass $context context object + * @since Moodle 3.0 + */ +function url_view($url, $course, $cm, $context) { + + // Trigger course_module_viewed event. + $params = array( + 'context' => $context, + 'objectid' => $url->id + ); + + $event = \mod_url\event\course_module_viewed::create($params); + $event->add_record_snapshot('course_modules', $cm); + $event->add_record_snapshot('course', $course); + $event->add_record_snapshot('url', $url); + $event->trigger(); + + // Completion. + $completion = new completion_info($course); + $completion->set_module_viewed($cm); +} diff --git a/mod/url/tests/lib_test.php b/mod/url/tests/lib_test.php index 406405a484b..229bf6fcadd 100644 --- a/mod/url/tests/lib_test.php +++ b/mod/url/tests/lib_test.php @@ -34,7 +34,7 @@ defined('MOODLE_INTERNAL') || die(); * @copyright 2011 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_url_lib_testcase extends basic_testcase { +class mod_url_lib_testcase extends advanced_testcase { /** * Prepares things before this test case is initialised @@ -42,6 +42,7 @@ class mod_url_lib_testcase extends basic_testcase { */ public static function setUpBeforeClass() { global $CFG; + require_once($CFG->dirroot . '/mod/url/lib.php'); require_once($CFG->dirroot . '/mod/url/locallib.php'); } @@ -72,4 +73,47 @@ class mod_url_lib_testcase extends basic_testcase { $this->assertTrue(url_appears_valid_url('lalala://@:@/')); } + + /** + * Test url_view + * @return void + */ + public function test_url_view() { + global $CFG; + + $CFG->enablecompletion = 1; + $this->resetAfterTest(); + + // Setup test data. + $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); + $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id), + array('completion' => 2, 'completionview' => 1)); + $context = context_module::instance($url->cmid); + $cm = get_coursemodule_from_instance('url', $url->id); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + + $this->setAdminUser(); + url_view($url, $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_url\event\course_module_viewed', $event); + $this->assertEquals($context, $event->get_context()); + $url = new \moodle_url('/mod/url/view.php', array('id' => $cm->id)); + $this->assertEquals($url, $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); + + } } \ No newline at end of file diff --git a/mod/url/view.php b/mod/url/view.php index a4600f4281f..03ad7471395 100644 --- a/mod/url/view.php +++ b/mod/url/view.php @@ -24,6 +24,7 @@ */ require('../../config.php'); +require_once("$CFG->dirroot/mod/url/lib.php"); require_once("$CFG->dirroot/mod/url/locallib.php"); require_once($CFG->libdir . '/completionlib.php'); @@ -46,19 +47,8 @@ require_course_login($course, true, $cm); $context = context_module::instance($cm->id); require_capability('mod/url:view', $context); -$params = array( - 'context' => $context, - 'objectid' => $url->id -); -$event = \mod_url\event\course_module_viewed::create($params); -$event->add_record_snapshot('course_modules', $cm); -$event->add_record_snapshot('course', $course); -$event->add_record_snapshot('url', $url); -$event->trigger(); - -// Update 'viewed' state if required by completion system -$completion = new completion_info($course); -$completion->set_module_viewed($cm); +// Completion and trigger events. +url_view($url, $course, $cm, $context); $PAGE->set_url('/mod/url/view.php', array('id' => $cm->id)); From 752036b826dbbcee25ac341aa7082a300e9f3226 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Fri, 17 Apr 2015 14:02:49 +0200 Subject: [PATCH 2/2] MDL-49793 url: New Web Service mod_url_view_url --- lib/db/services.php | 1 + mod/url/classes/external.php | 107 +++++++++++++++++++++++++++ mod/url/db/services.php | 39 ++++++++++ mod/url/tests/externallib_test.php | 111 +++++++++++++++++++++++++++++ mod/url/version.php | 2 +- version.php | 2 +- 6 files changed, 260 insertions(+), 2 deletions(-) create mode 100644 mod/url/classes/external.php create mode 100644 mod/url/db/services.php create mode 100644 mod/url/tests/externallib_test.php diff --git a/lib/db/services.php b/lib/db/services.php index abbb1b7ab3a..7d725ff0220 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -1127,6 +1127,7 @@ $services = array( 'core_user_view_user_profile', 'gradereport_user_view_grade_report', 'core_rating_get_item_ratings', + 'mod_url_view_url', ), 'enabled' => 0, 'restrictedusers' => 0, diff --git a/mod/url/classes/external.php b/mod/url/classes/external.php new file mode 100644 index 00000000000..e9285716505 --- /dev/null +++ b/mod/url/classes/external.php @@ -0,0 +1,107 @@ +. + +/** + * URL external API + * + * @package mod_url + * @category external + * @copyright 2015 Juan Leyva + * @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"); + +/** + * URL external functions + * + * @package mod_url + * @category external + * @copyright 2015 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.0 + */ +class mod_url_external extends external_api { + + /** + * Returns description of method parameters + * + * @return external_function_parameters + * @since Moodle 3.0 + */ + public static function view_url_parameters() { + return new external_function_parameters( + array( + 'urlid' => new external_value(PARAM_INT, 'url instance id') + ) + ); + } + + /** + * Simulate the url/view.php web interface page: trigger events, completion, etc... + * + * @param int $urlid the url instance id + * @return array of warnings and status result + * @since Moodle 3.0 + * @throws moodle_exception + */ + public static function view_url($urlid) { + global $DB, $CFG; + require_once($CFG->dirroot . "/mod/url/lib.php"); + + $params = self::validate_parameters(self::view_url_parameters(), + array( + 'urlid' => $urlid + )); + $warnings = array(); + + // Request and permission validation. + $url = $DB->get_record('url', array('id' => $params['urlid']), '*', MUST_EXIST); + list($course, $cm) = get_course_and_cm_from_instance($url, 'url'); + + $context = context_module::instance($cm->id); + self::validate_context($context); + + require_capability('mod/url:view', $context); + + // Call the url/lib API. + url_view($url, $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_url_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/url/db/services.php b/mod/url/db/services.php new file mode 100644 index 00000000000..c5b320b78c2 --- /dev/null +++ b/mod/url/db/services.php @@ -0,0 +1,39 @@ +. + +/** + * URL external functions and service definitions. + * + * @package mod_url + * @category external + * @copyright 2015 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.0 + */ + +defined('MOODLE_INTERNAL') || die; + +$functions = array( + + 'mod_url_view_url' => array( + 'classname' => 'mod_url_external', + 'methodname' => 'view_url', + 'description' => 'Simulate the view.php web interface url: trigger events, completion, etc...', + 'type' => 'write', + 'capabilities' => 'mod/url:view' + ), + +); diff --git a/mod/url/tests/externallib_test.php b/mod/url/tests/externallib_test.php new file mode 100644 index 00000000000..dd8cd6e1990 --- /dev/null +++ b/mod/url/tests/externallib_test.php @@ -0,0 +1,111 @@ +. + +/** + * External mod_url functions unit tests + * + * @package mod_url + * @category external + * @copyright 2015 Juan Leyva + * @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_url functions unit tests + * + * @package mod_url + * @category external + * @copyright 2015 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.0 + */ +class mod_url_external_testcase extends externallib_advanced_testcase { + + /** + * Test view_url + */ + public function test_view_url() { + global $DB; + + $this->resetAfterTest(true); + + // Setup test data. + $course = $this->getDataGenerator()->create_course(); + $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id)); + $context = context_module::instance($url->cmid); + $cm = get_coursemodule_from_instance('url', $url->id); + + // Test invalid instance id. + try { + mod_url_external::view_url(0); + $this->fail('Exception expected due to invalid mod_url 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_url_external::view_url($url->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_url_external::view_url($url->id); + $result = external_api::clean_returnvalue(mod_url_external::view_url_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_url\event\course_module_viewed', $event); + $this->assertEquals($context, $event->get_context()); + $moodleurl = new \moodle_url('/mod/url/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/url:view', CAP_PROHIBIT, $studentrole->id, $context->id); + accesslib_clear_all_caches_for_unit_testing(); + + try { + mod_url_external::view_url($url->id); + $this->fail('Exception expected due to missing capability.'); + } catch (moodle_exception $e) { + $this->assertEquals('nopermissions', $e->errorcode); + } + + } +} diff --git a/mod/url/version.php b/mod/url/version.php index b9fe91bf327..99f7a1cbb63 100644 --- a/mod/url/version.php +++ b/mod/url/version.php @@ -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_url'; // Full name of the plugin (used for diagnostics) $plugin->cron = 0; diff --git a/version.php b/version.php index 64501029acd..bc623fbb7ee 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2015051100.03; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2015051100.04; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.