Commit 501ed0c4 authored by Eloy Lafuente's avatar Eloy Lafuente
Browse files

Merge branch 'MDL-57915-master' of git://github.com/jleyva/moodle

parents 815c6b20 9fac7c86
......@@ -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)
),
);
......@@ -4101,3 +4101,33 @@ function data_set_config(&$database, $key, $value) {
$DB->set_field('data', 'config', $database->config, ['id' => $database->id]);
}
}
/**
* Mark the activity completed (if required) and trigger the course_module_viewed event.
*
* @param stdClass $data data object
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @since Moodle 3.3
*/
function data_view($data, $course, $cm, $context) {
global $CFG;
require_once($CFG->libdir . '/completionlib.php');
// Trigger course_module_viewed event.
$params = array(
'context' => $context,
'objectid' => $data->id
);
$event = \mod_data\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('data', $data);
$event->trigger();
// Completion.
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
}
......@@ -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());
}
}
......@@ -879,4 +879,46 @@ class mod_data_lib_testcase extends advanced_testcase {
$config = json_decode($database->config);
$this->assertEquals($value, $config->$key);
}
/**
* Test data_view
* @return void
*/
public function test_data_view() {
global $CFG;
$CFG->enablecompletion = 1;
$this->resetAfterTest();
$this->setAdminUser();
// Setup test data.
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
array('completion' => 2, 'completionview' => 1));
$context = context_module::instance($data->cmid);
$cm = get_coursemodule_from_instance('data', $data->id);
// Trigger and capture the event.
$sink = $this->redirectEvents();
data_view($data, $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_data\event\course_module_viewed', $event);
$this->assertEquals($context, $event->get_context());
$moodleurl = new \moodle_url('/mod/data/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);
}
}
......@@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2016120500; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2016120502; // 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;
......@@ -25,7 +25,6 @@
require_once(__DIR__ . '/../../config.php');
require_once($CFG->dirroot . '/mod/data/lib.php');
require_once($CFG->libdir . '/rsslib.php');
require_once($CFG->libdir . '/completionlib.php');
/// One of these is necessary!
$id = optional_param('id', 0, PARAM_INT); // course module id
......@@ -255,15 +254,8 @@
set_user_preference('data_perpage_'.$data->id, $perpage);
}
$params = array(
'context' => $context,
'objectid' => $data->id
);
$event = \mod_data\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('data', $data);
$event->trigger();
// Completion and trigger events.
data_view($data, $course, $cm, $context);
$urlparams = array('d' => $data->id);
if ($record) {
......@@ -300,10 +292,6 @@
$PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
}
// Mark as viewed
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
/// Print the page header
// Note: MDL-19010 there will be further changes to printing header and blocks.
// The code will be much nicer than this eventually.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment