mirror of
https://github.com/moodle/moodle.git
synced 2025-03-14 12:40:01 +01:00
MDL-49792 resource: New WS mod_resource_view_resource
This commit is contained in:
parent
537f7b38c8
commit
3c779961ad
@ -1141,6 +1141,7 @@ $services = array(
|
||||
'core_user_add_user_private_files',
|
||||
'mod_assign_view_grading_table',
|
||||
'mod_scorm_view_scorm',
|
||||
'mod_resource_view_resource',
|
||||
),
|
||||
'enabled' => 0,
|
||||
'restrictedusers' => 0,
|
||||
|
107
mod/resource/classes/external.php
Normal file
107
mod/resource/classes/external.php
Normal file
@ -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/>.
|
||||
|
||||
/**
|
||||
* Resource external API
|
||||
*
|
||||
* @package mod_resource
|
||||
* @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");
|
||||
|
||||
/**
|
||||
* Resource external functions
|
||||
*
|
||||
* @package mod_resource
|
||||
* @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_resource_external extends external_api {
|
||||
|
||||
/**
|
||||
* Returns description of method parameters
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
public static function view_resource_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'resourceid' => new external_value(PARAM_INT, 'resource instance id')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate the resource/view.php web interface page: trigger events, completion, etc...
|
||||
*
|
||||
* @param int $resourceid the resource instance id
|
||||
* @return array of warnings and status result
|
||||
* @since Moodle 3.0
|
||||
* @throws moodle_exception
|
||||
*/
|
||||
public static function view_resource($resourceid) {
|
||||
global $DB, $CFG;
|
||||
require_once($CFG->dirroot . "/mod/resource/lib.php");
|
||||
|
||||
$params = self::validate_parameters(self::view_resource_parameters(),
|
||||
array(
|
||||
'resourceid' => $resourceid
|
||||
));
|
||||
$warnings = array();
|
||||
|
||||
// Request and permission validation.
|
||||
$resource = $DB->get_record('resource', array('id' => $params['resourceid']), '*', MUST_EXIST);
|
||||
list($course, $cm) = get_course_and_cm_from_instance($resource, 'resource');
|
||||
|
||||
$context = context_module::instance($cm->id);
|
||||
self::validate_context($context);
|
||||
|
||||
require_capability('mod/resource:view', $context);
|
||||
|
||||
// Call the resource/lib API.
|
||||
resource_view($resource, $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_resource_returns() {
|
||||
return new external_single_structure(
|
||||
array(
|
||||
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
|
||||
'warnings' => new external_warnings()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
39
mod/resource/db/services.php
Normal file
39
mod/resource/db/services.php
Normal file
@ -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/>.
|
||||
|
||||
/**
|
||||
* Resource external functions and service definitions.
|
||||
*
|
||||
* @package mod_resource
|
||||
* @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_resource_view_resource' => array(
|
||||
'classname' => 'mod_resource_external',
|
||||
'methodname' => 'view_resource',
|
||||
'description' => 'Simulate the view.php web interface resource: trigger events, completion, etc...',
|
||||
'type' => 'write',
|
||||
'capabilities' => 'mod/resource:view'
|
||||
),
|
||||
|
||||
);
|
112
mod/resource/tests/externallib_test.php
Normal file
112
mod/resource/tests/externallib_test.php
Normal file
@ -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_resource functions unit tests
|
||||
*
|
||||
* @package mod_resource
|
||||
* @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_resource functions unit tests
|
||||
*
|
||||
* @package mod_resource
|
||||
* @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_resource_external_testcase extends externallib_advanced_testcase {
|
||||
|
||||
/**
|
||||
* Test view_resource
|
||||
*/
|
||||
public function test_view_resource() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$this->setAdminUser();
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id));
|
||||
$context = context_module::instance($resource->cmid);
|
||||
$cm = get_coursemodule_from_instance('resource', $resource->id);
|
||||
|
||||
// Test invalid instance id.
|
||||
try {
|
||||
mod_resource_external::view_resource(0);
|
||||
$this->fail('Exception expected due to invalid mod_resource 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_resource_external::view_resource($resource->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_resource_external::view_resource($resource->id);
|
||||
$result = external_api::clean_returnvalue(mod_resource_external::view_resource_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_resource\event\course_module_viewed', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
$moodleurl = new \moodle_url('/mod/resource/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/resource:view', CAP_PROHIBIT, $studentrole->id, $context->id);
|
||||
accesslib_clear_all_caches_for_unit_testing();
|
||||
|
||||
try {
|
||||
mod_resource_external::view_resource($resource->id);
|
||||
$this->fail('Exception expected due to missing capability.');
|
||||
} catch (moodle_exception $e) {
|
||||
$this->assertEquals('nopermissions', $e->errorcode);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -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_resource'; // Full name of the plugin (used for diagnostics)
|
||||
$plugin->cron = 0;
|
||||
|
@ -29,7 +29,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$version = 2015073000.00; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
$version = 2015073000.01; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
// RR = release increments - 00 in DEV branches.
|
||||
// .XX = incremental changes.
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user