diff --git a/lib/db/services.php b/lib/db/services.php index 3bf4a0748d8..61b5752734e 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -1132,7 +1132,7 @@ $functions = array( ), // Tag functions. - 'core_tag_update_tags' => array( + 'core_tag_update_tags' => array( 'classname' => 'core_tag_external', 'methodname' => 'update_tags', 'description' => 'Updates tags', @@ -1269,6 +1269,7 @@ $services = array( 'mod_lti_view_lti', 'mod_imscp_view_imscp', 'mod_imscp_get_imscps_by_courses', + 'mod_glossary_get_glossaries_by_courses', ), 'enabled' => 0, 'restrictedusers' => 0, diff --git a/mod/glossary/classes/external.php b/mod/glossary/classes/external.php new file mode 100644 index 00000000000..65c8bf3f044 --- /dev/null +++ b/mod/glossary/classes/external.php @@ -0,0 +1,160 @@ +. + +/** + * Glossary module external API. + * + * @package mod_glossary + * @category external + * @copyright 2015 Costantino Cito + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.1 + */ +defined('MOODLE_INTERNAL') || die; +require_once($CFG->libdir . '/externallib.php'); + +/** + * Glossary module external functions. + * + * @package mod_glossary + * @category external + * @copyright 2015 Costantino Cito + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.1 + */ +class mod_glossary_external extends external_api { + + /** + * Describes the parameters for get_glossaries_by_courses. + * + * @return external_external_function_parameters + * @since Moodle 3.1 + */ + public static function get_glossaries_by_courses_parameters() { + return new external_function_parameters ( + array( + 'courseids' => new external_multiple_structure( + new external_value(PARAM_INT, 'course id'), + 'Array of course IDs', VALUE_DEFAULT, array() + ), + ) + ); + } + + /** + * Returns a list of glossaries in a provided list of courses. + * + * If no list is provided all glossaries that the user can view will be returned. + * + * @param array $courseids the course IDs. + * @return array of glossaries + * @since Moodle 3.1 + */ + public static function get_glossaries_by_courses($courseids = array()) { + $params = self::validate_parameters(self::get_glossaries_by_courses_parameters(), array('courseids' => $courseids)); + + $warnings = array(); + $courses = array(); + $courseids = $params['courseids']; + + if (empty($courseids)) { + $courses = enrol_get_my_courses(); + $courseids = array_keys($courses); + } + + // Array to store the glossaries to return. + $glossaries = array(); + + // Ensure there are courseids to loop through. + if (!empty($courseids)) { + list($courses, $warnings) = external_util::validate_courses($courseids, $courses); + + // Get the glossaries in these courses, this function checks users visibility permissions. + $glossaries = get_all_instances_in_courses('glossary', $courses); + foreach ($glossaries as $glossary) { + $context = context_module::instance($glossary->coursemodule); + $glossary->name = external_format_string($glossary->name, $context->id); + list($glossary->intro, $glossary->introformat) = external_format_text($glossary->intro, $glossary->introformat, + $context->id, 'mod_glossary', 'intro', null); + } + } + + $result = array(); + $result['glossaries'] = $glossaries; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Describes the get_glossaries_by_courses return value. + * + * @return external_single_structure + * @since Moodle 3.1 + */ + public static function get_glossaries_by_courses_returns() { + return new external_single_structure(array( + 'glossaries' => new external_multiple_structure( + new external_single_structure(array( + 'id' => new external_value(PARAM_INT, 'Glossary id'), + 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), + 'course' => new external_value(PARAM_INT, 'Course id'), + 'name' => new external_value(PARAM_RAW, 'Glossary name'), + 'intro' => new external_value(PARAM_RAW, 'The Glossary intro'), + 'introformat' => new external_format_value('intro'), + 'allowduplicatedentries' => new external_value(PARAM_INT, 'If enabled, multiple entries can have the' . + ' same concept name'), + 'displayformat' => new external_value(PARAM_TEXT, 'Display format type'), + 'mainglossary' => new external_value(PARAM_INT, 'If enabled this glossary is a main glossary.'), + 'showspecial' => new external_value(PARAM_INT, 'If enabled, participants can browse the glossary by' . + ' special characters, such as @ and #'), + 'showalphabet' => new external_value(PARAM_INT, 'If enabled, participants can browse the glossary by' . + ' letters of the alphabet'), + 'showall' => new external_value(PARAM_INT, 'If enabled, participants can browse all entries at once'), + 'allowcomments' => new external_value(PARAM_INT, 'If enabled, all participants with permission to' . + ' create comments will be able to add comments to glossary entries'), + 'allowprintview' => new external_value(PARAM_INT, 'If enabled, students are provided with a link to a' . + ' printer-friendly version of the glossary. The link is always available to teachers'), + 'usedynalink' => new external_value(PARAM_INT, 'If site-wide glossary auto-linking has been enabled' . + ' by an administrator and this checkbox is ticked, the entry will be automatically linked' . + ' wherever the concept words and phrases appear throughout the rest of the course.'), + 'defaultapproval' => new external_value(PARAM_INT, 'If set to no, entries require approving by a' . + ' teacher before they are viewable by everyone.'), + 'approvaldisplayformat' => new external_value(PARAM_TEXT, 'When approving glossary items you may wish' . + ' to use a different display format'), + 'globalglossary' => new external_value(PARAM_INT, ''), + 'entbypage' => new external_value(PARAM_INT, 'Entries shown per page'), + 'editalways' => new external_value(PARAM_INT, 'Always allow editing'), + 'rsstype' => new external_value(PARAM_INT, 'To enable the RSS feed for this activity, select either' . + ' concepts with author or concepts without author to be included in the feed'), + 'rssarticles' => new external_value(PARAM_INT, 'This setting specifies the number of glossary entry' . + ' concepts to include in the RSS feed. Between 5 and 20 generally acceptable'), + 'assessed' => new external_value(PARAM_INT, 'Aggregate type'), + 'assesstimestart' => new external_value(PARAM_INT, 'Restrict rating to items created after this'), + 'assesstimefinish' => new external_value(PARAM_INT, 'Restrict rating to items created before this'), + 'scale' => new external_value(PARAM_INT, 'Scale ID'), + 'timecreated' => new external_value(PARAM_INT, 'Time created'), + 'timemodified' => new external_value(PARAM_INT, 'Time modified'), + 'completionentries' => new external_value(PARAM_INT, 'Number of entries to complete'), + 'section' => new external_value(PARAM_INT, 'Section'), + 'visible' => new external_value(PARAM_INT, 'Visible'), + 'groupmode' => new external_value(PARAM_INT, 'Group mode'), + 'groupingid' => new external_value(PARAM_INT, 'Grouping ID'), + ), 'Glossaries') + ), + 'warnings' => new external_warnings()) + ); + } +} diff --git a/mod/glossary/db/services.php b/mod/glossary/db/services.php new file mode 100644 index 00000000000..fe26d6cd5b4 --- /dev/null +++ b/mod/glossary/db/services.php @@ -0,0 +1,38 @@ +. + +/** + * Glossary module external functions. + * + * @package mod_glossary + * @category external + * @copyright 2015 Frédéric Massart - FMCorz.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$functions = array( + + 'mod_glossary_get_glossaries_by_courses' => array( + 'classname' => 'mod_glossary_external', + 'methodname' => 'get_glossaries_by_courses', + 'description' => 'Retrieve a list of glossaries from several courses.', + 'type' => 'read', + 'capabilities' => 'mod/glossary:view' + ), + +); diff --git a/mod/glossary/tests/external_test.php b/mod/glossary/tests/external_test.php new file mode 100644 index 00000000000..405ba93fa17 --- /dev/null +++ b/mod/glossary/tests/external_test.php @@ -0,0 +1,88 @@ +. + +/** + * External glossary functions unit tests + * + * @package mod_glossary + * @category external + * @copyright 2015 Costantino Cito + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); +global $CFG; +require_once($CFG->dirroot . '/webservice/tests/helpers.php'); + +/** + * External glossary functions unit tests + * + * @package mod_glossary + * @category external + * @copyright 2015 Costantino Cito + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_glossary_external_testcase extends externallib_advanced_testcase { + + /** + * Test get_glossaries_by_courses + */ + public function test_get_glossaries_by_courses() { + $this->resetAfterTest(true); + + // As admin. + $this->setAdminUser(); + $c1 = self::getDataGenerator()->create_course(); + $c2 = self::getDataGenerator()->create_course(); + $g1 = self::getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'name' => 'First Glossary')); + $g2 = self::getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'name' => 'Second Glossary')); + $g3 = self::getDataGenerator()->create_module('glossary', array('course' => $c2->id, 'name' => 'Third Glossary')); + + $s1 = $this->getDataGenerator()->create_user(); + self::getDataGenerator()->enrol_user($s1->id, $c1->id); + + // Check results where student is enrolled. + $this->setUser($s1); + $glossaries = mod_glossary_external::get_glossaries_by_courses(array()); + $glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries); + + $this->assertCount(2, $glossaries['glossaries']); + $this->assertEquals('First Glossary', $glossaries['glossaries'][0]['name']); + $this->assertEquals('Second Glossary', $glossaries['glossaries'][1]['name']); + + // Check results with specific course IDs. + $glossaries = mod_glossary_external::get_glossaries_by_courses(array($c1->id, $c2->id)); + $glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries); + + $this->assertCount(2, $glossaries['glossaries']); + $this->assertEquals('First Glossary', $glossaries['glossaries'][0]['name']); + $this->assertEquals('Second Glossary', $glossaries['glossaries'][1]['name']); + + $this->assertEquals('course', $glossaries['warnings'][0]['item']); + $this->assertEquals($c2->id, $glossaries['warnings'][0]['itemid']); + $this->assertEquals('1', $glossaries['warnings'][0]['warningcode']); + + // Now as admin. + $this->setAdminUser(); + + $glossaries = mod_glossary_external::get_glossaries_by_courses(array($c2->id)); + $glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries); + + $this->assertCount(1, $glossaries['glossaries']); + $this->assertEquals('Third Glossary', $glossaries['glossaries'][0]['name']); + } + +} diff --git a/mod/glossary/version.php b/mod/glossary/version.php index b776316c910..66ae9ad42ab 100644 --- a/mod/glossary/version.php +++ b/mod/glossary/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015111600; // The current module version (Date: YYYYMMDDXX) +$plugin->version = 2015111601; // The current module version (Date: YYYYMMDDXX) $plugin->requires = 2015111000; // Requires this Moodle version $plugin->component = 'mod_glossary'; // Full name of the plugin (used for diagnostics) $plugin->cron = 0; diff --git a/version.php b/version.php index b7bfddf0b34..3b24633f374 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2015120400.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2015120400.02; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.