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

Conflicts:
	lib/db/services.php
This commit is contained in:
David Monllao
2015-03-24 17:55:42 +08:00
5 changed files with 432 additions and 1 deletions
+1
View File
@@ -1023,6 +1023,7 @@ $services = array(
'core_user_remove_user_device',
'core_course_get_courses',
'core_completion_update_activity_completion_status_manually',
'mod_data_get_databases_by_courses'
),
'enabled' => 0,
'restrictedusers' => 0,
+239
View File
@@ -0,0 +1,239 @@
<?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/>.
/**
* Database module external API
*
* @package mod_data
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
defined('MOODLE_INTERNAL') || die;
require_once("$CFG->libdir/externallib.php");
/**
* Database module external functions
*
* @package mod_data
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
class mod_data_external extends external_api {
/**
* Describes the parameters for get_databases_by_courses.
*
* @return external_external_function_parameters
* @since Moodle 2.9
*/
public static function get_databases_by_courses_parameters() {
return new external_function_parameters (
array(
'courseids' => new external_multiple_structure(
new external_value(PARAM_INT, 'course id', VALUE_REQUIRED),
'Array of course ids', VALUE_DEFAULT, array()
),
)
);
}
/**
* Returns a list of databases in a provided list of courses,
* if no list is provided all databases that the user can view will be returned.
*
* @param array $courseids the course ids
* @return array the database details
* @since Moodle 2.9
*/
public static function get_databases_by_courses($courseids = array()) {
global $CFG;
$params = self::validate_parameters(self::get_databases_by_courses_parameters(), array('courseids' => $courseids));
$warnings = array();
$courses = enrol_get_my_courses();
// Used to test for ids that have been requested but can't be returned.
if (count($params['courseids']) > 0) {
$courseids = array();
foreach ($params['courseids'] as $courseid) {
if (!in_array($courseid, array_keys($courses))) {
$warnings[] = array(
'item' => 'course',
'itemid' => $courseid,
'warningcode' => '2',
'message' => 'User is not enrolled or does not have requested capability'
);
} else {
$courseids[] = $courseid;
}
}
} else {
$courseids = array_keys($courses);
}
// Array to store the databases to return.
$arrdatabases = array();
// Ensure there are courseids to loop through.
if (!empty($courseids)) {
// Array of the courses we are going to retrieve the databases from.
$dbcourses = array();
// Go through the courseids.
foreach ($courseids as $cid) {
$context = context_course::instance($cid);
// Check the user can function in this context.
try {
self::validate_context($context);
$dbcourses[$cid] = $courses[$cid];
} catch (Exception $e) {
$warnings[] = array(
'item' => 'course',
'itemid' => $cid,
'warningcode' => '1',
'message' => 'No access rights in course context '.$e->getMessage().$e->getTraceAsString()
);
}
}
// Get the databases in this course, this function checks users visibility permissions.
// We can avoid then additional validate_context calls.
$databases = get_all_instances_in_courses("data", $dbcourses);
foreach ($databases as $database) {
$datacontext = context_module::instance($database->coursemodule);
// Entry to return.
$newdb = array();
// First, we return information that any user can see in the web interface.
$newdb['id'] = $database->id;
$newdb['coursemodule'] = $database->coursemodule;
$newdb['course'] = $database->course;
$newdb['name'] = $database->name;
// Format intro.
list($newdb['intro'], $newdb['introformat']) =
external_format_text($database->intro, $database->introformat,
$datacontext->id, 'mod_data', 'intro', $database->id);
// This information should be only available if the user can see the database entries.
if (has_capability('mod/data:viewentry', $datacontext)) {
$viewablefields = array('comments', 'timeavailablefrom', 'timeavailableto', 'timeviewfrom',
'timeviewto', 'requiredentries', 'requiredentriestoview');
// This is for avoid a long repetitive list and for
// checking that we are retrieving all the required fields.
foreach ($viewablefields as $field) {
// We do not use isset because it won't work for existing null values.
if (!property_exists($database, $field)) {
throw new invalid_response_exception('Missing database module required field: ' . $field);
}
$newdb[$field] = $database->{$field};
}
}
// Check additional permissions for returning optional private settings.
// I avoid intentionally to use can_[add|update]_moduleinfo.
if (has_capability('moodle/course:manageactivities', $context)) {
$additionalfields = array('maxentries', 'rssarticles', 'singletemplate', 'listtemplate',
'listtemplateheader', 'listtemplatefooter', 'addtemplate', 'rsstemplate', 'rsstitletemplate',
'csstemplate', 'jstemplate', 'asearchtemplate', 'approval', 'scale', 'assessed', 'assesstimestart',
'assesstimefinish', 'defaultsort', 'defaultsortdir', 'editany', 'notification');
// This is for avoid a long repetitive list.
foreach ($additionalfields as $field) {
if (property_exists($database, $field)) {
$newdb[$field] = $database->{$field};
}
}
}
$arrdatabases[] = $newdb;
}
}
$result = array();
$result['databases'] = $arrdatabases;
$result['warnings'] = $warnings;
return $result;
}
/**
* Describes the get_databases_by_courses return value.
*
* @return external_single_structure
* @since Moodle 2.9
*/
public static function get_databases_by_courses_returns() {
return new external_single_structure(
array(
'databases' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Database id'),
'coursemodule' => new external_value(PARAM_INT, 'Course module id'),
'course' => new external_value(PARAM_TEXT, 'Course id'),
'name' => new external_value(PARAM_TEXT, 'Database name'),
'intro' => new external_value(PARAM_RAW, 'The Database intro'),
'introformat' => new external_format_value('intro'),
'comments' => new external_value(PARAM_BOOL, 'comments enabled'),
'timeavailablefrom' => new external_value(PARAM_INT, 'timeavailablefrom field'),
'timeavailableto' => new external_value(PARAM_INT, 'timeavailableto field'),
'timeviewfrom' => new external_value(PARAM_INT, 'timeviewfrom field'),
'timeviewto' => new external_value(PARAM_INT, 'timeviewto field'),
'requiredentries' => new external_value(PARAM_INT, 'requiredentries field'),
'requiredentriestoview' => new external_value(PARAM_INT, 'requiredentriestoview field'),
'maxentries' => new external_value(PARAM_INT, 'maxentries field', VALUE_OPTIONAL),
'rssarticles' => new external_value(PARAM_INT, 'rssarticles field', VALUE_OPTIONAL),
'singletemplate' => new external_value(PARAM_RAW, 'singletemplate field', VALUE_OPTIONAL),
'listtemplate' => new external_value(PARAM_RAW, 'listtemplate field', VALUE_OPTIONAL),
'listtemplateheader' => new external_value(PARAM_RAW, 'listtemplateheader field', VALUE_OPTIONAL),
'listtemplatefooter' => new external_value(PARAM_RAW, 'listtemplatefooter field', VALUE_OPTIONAL),
'addtemplate' => new external_value(PARAM_RAW, 'addtemplate field', VALUE_OPTIONAL),
'rsstemplate' => new external_value(PARAM_RAW, 'rsstemplate field', VALUE_OPTIONAL),
'rsstitletemplate' => new external_value(PARAM_RAW, 'rsstitletemplate field', VALUE_OPTIONAL),
'csstemplate' => new external_value(PARAM_RAW, 'csstemplate field', VALUE_OPTIONAL),
'jstemplate' => new external_value(PARAM_RAW, 'jstemplate field', VALUE_OPTIONAL),
'asearchtemplate' => new external_value(PARAM_RAW, 'asearchtemplate field', VALUE_OPTIONAL),
'approval' => new external_value(PARAM_BOOL, 'approval field', VALUE_OPTIONAL),
'scale' => new external_value(PARAM_INT, 'scale field', VALUE_OPTIONAL),
'assessed' => new external_value(PARAM_INT, 'assessed field', VALUE_OPTIONAL),
'assesstimestart' => new external_value(PARAM_INT, 'assesstimestart field', VALUE_OPTIONAL),
'assesstimefinish' => new external_value(PARAM_INT, 'assesstimefinish field', VALUE_OPTIONAL),
'defaultsort' => new external_value(PARAM_INT, 'defaultsort field', VALUE_OPTIONAL),
'defaultsortdir' => new external_value(PARAM_INT, 'defaultsortdir field', VALUE_OPTIONAL),
'editany' => new external_value(PARAM_BOOL, 'editany field', VALUE_OPTIONAL),
'notification' => new external_value(PARAM_INT, 'notification field', VALUE_OPTIONAL)
), 'Database'
)
),
'warnings' => new external_warnings(),
)
);
}
}
+37
View File
@@ -0,0 +1,37 @@
<?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/>.
/**
* Database external functions and service definitions.
*
* @package mod_data
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
$functions = array(
'mod_data_get_databases_by_courses' => array(
'classname' => 'mod_data_external',
'methodname' => 'get_databases_by_courses',
'description' => 'Returns a list of database instances in a provided set of courses, if
no courses are provided then all the database instances the user has access to will be returned.',
'type' => 'read',
'capabilities' => 'mod/data:viewentry'
)
);
+154
View File
@@ -0,0 +1,154 @@
<?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/>.
/**
* Database module external functions tests
*
* @package mod_data
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* Database module external functions tests
*
* @package mod_data
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
class mod_data_external_testcase extends externallib_advanced_testcase {
/**
* Test get databases by courses
*/
public function test_mod_data_get_databases_by_courses() {
global $DB;
$this->resetAfterTest(true);
// Create users.
$student = self::getDataGenerator()->create_user();
$teacher = self::getDataGenerator()->create_user();
// Set to the student user.
self::setUser($student);
// Create courses to add the modules.
$course1 = self::getDataGenerator()->create_course();
$course2 = self::getDataGenerator()->create_course();
// First database.
$record = new stdClass();
$record->introformat = FORMAT_HTML;
$record->course = $course1->id;
$database1 = self::getDataGenerator()->create_module('data', $record);
// Second database.
$record = new stdClass();
$record->introformat = FORMAT_HTML;
$record->course = $course2->id;
$database2 = self::getDataGenerator()->create_module('data', $record);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
// Users enrolments.
$this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($teacher->id, $course1->id, $teacherrole->id, 'manual');
// Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course2->id, true);
foreach ($enrolinstances as $courseenrolinstance) {
if ($courseenrolinstance->enrol == "manual") {
$instance2 = $courseenrolinstance;
break;
}
}
$enrol->enrol_user($instance2, $student->id, $studentrole->id);
// Create what we expect to be returned when querying the two courses.
// First for the student user.
$expectedfields = array('id', 'coursemodule', 'course', 'name', 'comments', 'timeavailablefrom',
'timeavailableto', 'timeviewfrom', 'timeviewto', 'requiredentries', 'requiredentriestoview',
'intro', 'introformat');
// Add expected coursemodule.
$database1->coursemodule = $database1->cmid;
$database2->coursemodule = $database2->cmid;
$expected1 = array();
$expected2 = array();
foreach ($expectedfields as $field) {
$expected1[$field] = $database1->{$field};
$expected2[$field] = $database2->{$field};
}
$expecteddatabases = array();
$expecteddatabases[] = $expected2;
$expecteddatabases[] = $expected1;
// Call the external function passing course ids.
$result = mod_data_external::get_databases_by_courses(array($course2->id, $course1->id));
external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
$this->assertEquals($expecteddatabases, $result['databases']);
// Call the external function without passing course id.
$result = mod_data_external::get_databases_by_courses();
external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
$this->assertEquals($expecteddatabases, $result['databases']);
// Unenrol user from second course and alter expected databases.
$enrol->unenrol_user($instance2, $student->id);
array_shift($expecteddatabases);
// Call the external function without passing course id.
$result = mod_data_external::get_databases_by_courses();
external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
$this->assertEquals($expecteddatabases, $result['databases']);
// Call for the second course we unenrolled the user from, expected warning.
$result = mod_data_external::get_databases_by_courses(array($course2->id));
$this->assertCount(1, $result['warnings']);
$this->assertEquals('2', $result['warnings'][0]['warningcode']);
$this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
// Now, try as a teacher for getting all the additional fields.
self::setUser($teacher);
$additionalfields = array('maxentries', 'rssarticles', 'singletemplate', 'listtemplate',
'listtemplateheader', 'listtemplatefooter', 'addtemplate', 'rsstemplate', 'rsstitletemplate',
'csstemplate', 'jstemplate', 'asearchtemplate', 'approval', 'scale', 'assessed', 'assesstimestart',
'assesstimefinish', 'defaultsort', 'defaultsortdir', 'editany', 'notification');
foreach ($additionalfields as $field) {
$expecteddatabases[0][$field] = $database1->{$field};
}
$result = mod_data_external::get_databases_by_courses();
external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result);
$this->assertEquals($expecteddatabases, $result['databases']);
}
}
+1 -1
View File
@@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2015030900; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2015030901; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2014110400; // Requires this Moodle version
$plugin->component = 'mod_data'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;