mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
MDL-57915 mod_data: New Web Service mod_data_view_database
This commit is contained in:
parent
023ffa656d
commit
9fac7c8640
4 changed files with 161 additions and 2 deletions
|
@ -27,6 +27,7 @@
|
|||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
require_once("$CFG->libdir/externallib.php");
|
||||
require_once($CFG->dirroot . "/mod/data/locallib.php");
|
||||
|
||||
use mod_data\external\database_summary_exporter;
|
||||
|
||||
|
@ -149,4 +150,65 @@ class mod_data_external extends external_api {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public static function view_database_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'databaseid' => new external_value(PARAM_INT, 'data instance id')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate the data/view.php web interface page: trigger events, completion, etc...
|
||||
*
|
||||
* @param int $databaseid the data instance id
|
||||
* @return array of warnings and status result
|
||||
* @since Moodle 3.3
|
||||
* @throws moodle_exception
|
||||
*/
|
||||
public static function view_database($databaseid) {
|
||||
global $DB;
|
||||
|
||||
$params = self::validate_parameters(self::view_database_parameters(), array('databaseid' => $databaseid));
|
||||
$warnings = array();
|
||||
|
||||
// Request and permission validation.
|
||||
$data = $DB->get_record('data', array('id' => $params['databaseid']), '*', MUST_EXIST);
|
||||
list($course, $cm) = get_course_and_cm_from_instance($data, 'data');
|
||||
|
||||
$context = context_module::instance($cm->id);
|
||||
self::validate_context($context);
|
||||
|
||||
require_capability('mod/data:viewentry', $context);
|
||||
|
||||
// Call the data/lib API.
|
||||
data_view($data, $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.3
|
||||
*/
|
||||
public static function view_database_returns() {
|
||||
return new external_single_structure(
|
||||
array(
|
||||
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
|
||||
'warnings' => new external_warnings()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -34,5 +34,13 @@ $functions = array(
|
|||
'type' => 'read',
|
||||
'capabilities' => 'mod/data:viewentry',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
|
||||
)
|
||||
),
|
||||
'mod_data_view_database' => array(
|
||||
'classname' => 'mod_data_external',
|
||||
'methodname' => 'view_database',
|
||||
'description' => 'Simulate the view.php web interface data: trigger events, completion, etc...',
|
||||
'type' => 'write',
|
||||
'capabilities' => 'mod/data:viewentry',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
|
||||
),
|
||||
);
|
||||
|
|
|
@ -41,6 +41,33 @@ require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
|||
*/
|
||||
class mod_data_external_testcase extends externallib_advanced_testcase {
|
||||
|
||||
/**
|
||||
* Set up for every test
|
||||
*/
|
||||
public function setUp() {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
// Setup test data.
|
||||
$this->course = $this->getDataGenerator()->create_course();
|
||||
$this->data = $this->getDataGenerator()->create_module('data', array('course' => $this->course->id));
|
||||
$this->context = context_module::instance($this->data->cmid);
|
||||
$this->cm = get_coursemodule_from_instance('data', $this->data->id);
|
||||
|
||||
// Create users.
|
||||
$this->student1 = self::getDataGenerator()->create_user();
|
||||
$this->student2 = self::getDataGenerator()->create_user();
|
||||
$this->teacher = self::getDataGenerator()->create_user();
|
||||
|
||||
// Users enrolments.
|
||||
$this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
||||
$this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
|
||||
$this->getDataGenerator()->enrol_user($this->student1->id, $this->course->id, $this->studentrole->id, 'manual');
|
||||
$this->getDataGenerator()->enrol_user($this->student2->id, $this->course->id, $this->studentrole->id, 'manual');
|
||||
$this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get databases by courses
|
||||
*/
|
||||
|
@ -168,4 +195,66 @@ class mod_data_external_testcase extends externallib_advanced_testcase {
|
|||
$result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
|
||||
$this->assertEquals($expecteddatabases, $result['databases']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test view_database invalid id.
|
||||
*/
|
||||
public function test_view_database_invalid_id() {
|
||||
|
||||
// Test invalid instance id.
|
||||
$this->setExpectedException('moodle_exception');
|
||||
mod_data_external::view_database(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test view_database not enrolled user.
|
||||
*/
|
||||
public function test_view_database_not_enrolled_user() {
|
||||
|
||||
$usernotenrolled = self::getDataGenerator()->create_user();
|
||||
$this->setUser($usernotenrolled);
|
||||
|
||||
$this->setExpectedException('moodle_exception');
|
||||
mod_data_external::view_database(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test view_database no capabilities.
|
||||
*/
|
||||
public function test_view_database_no_capabilities() {
|
||||
// Test user with no capabilities.
|
||||
// We need a explicit prohibit since this capability is allowed for students by default.
|
||||
assign_capability('mod/data:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
|
||||
accesslib_clear_all_caches_for_unit_testing();
|
||||
|
||||
$this->setExpectedException('moodle_exception');
|
||||
mod_data_external::view_database(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test view_database.
|
||||
*/
|
||||
public function test_view_database() {
|
||||
|
||||
// Test user with full capabilities.
|
||||
$this->setUser($this->student1);
|
||||
|
||||
// Trigger and capture the event.
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
$result = mod_data_external::view_database($this->data->id);
|
||||
$result = external_api::clean_returnvalue(mod_data_external::view_database_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_data\event\course_module_viewed', $event);
|
||||
$this->assertEquals($this->context, $event->get_context());
|
||||
$moodledata = new \moodle_url('/mod/data/view.php', array('id' => $this->cm->id));
|
||||
$this->assertEquals($moodledata, $event->get_url());
|
||||
$this->assertEventContextNotUsed($event);
|
||||
$this->assertNotEmpty($event->get_name());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$plugin->version = 2016120500; // The current module version (Date: YYYYMMDDXX)
|
||||
$plugin->version = 2016120501; // The current module version (Date: YYYYMMDDXX)
|
||||
$plugin->requires = 2016112900; // Requires this Moodle version
|
||||
$plugin->component = 'mod_data'; // Full name of the plugin (used for diagnostics)
|
||||
$plugin->cron = 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue