diff --git a/lib/db/services.php b/lib/db/services.php index 3bf4a0748d8..2d3e9de53da 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -1269,6 +1269,7 @@ $services = array( 'mod_lti_view_lti', 'mod_imscp_view_imscp', 'mod_imscp_get_imscps_by_courses', + 'mod_wiki_get_wikis_by_courses', ), 'enabled' => 0, 'restrictedusers' => 0, diff --git a/mod/wiki/classes/external.php b/mod/wiki/classes/external.php new file mode 100644 index 00000000000..847f7888fb6 --- /dev/null +++ b/mod/wiki/classes/external.php @@ -0,0 +1,177 @@ +. + +/** + * Wiki module external API. + * + * @package mod_wiki + * @category external + * @copyright 2015 Dani Palou + * @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'); +require_once($CFG->dirroot . '/mod/wiki/lib.php'); +require_once($CFG->dirroot . '/mod/wiki/locallib.php'); + +/** + * Wiki module external functions. + * + * @package mod_wiki + * @category external + * @copyright 2015 Dani Palou + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.1 + */ +class mod_wiki_external extends external_api { + + /** + * Describes the parameters for get_wikis_by_courses. + * + * @return external_function_parameters + * @since Moodle 3.1 + */ + public static function get_wikis_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 wikis in a provided list of courses, + * if no list is provided all wikis that the user can view will be returned. + * + * @param array $courseids The courses IDs. + * @return array Containing a list of warnings and a list of wikis. + * @since Moodle 3.1 + */ + public static function get_wikis_by_courses($courseids = array()) { + + $returnedwikis = array(); + $warnings = array(); + + $params = self::validate_parameters(self::get_wikis_by_courses_parameters(), array('courseids' => $courseids)); + + $mycourses = array(); + if (empty($params['courseids'])) { + $mycourses = enrol_get_my_courses(); + $params['courseids'] = array_keys($mycourses); + } + + // Ensure there are courseids to loop through. + if (!empty($params['courseids'])) { + + list($courses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses); + + // Get the wikis in this course, this function checks users visibility permissions. + // We can avoid then additional validate_context calls. + $wikis = get_all_instances_in_courses('wiki', $courses); + + foreach ($wikis as $wiki) { + + $context = context_module::instance($wiki->coursemodule); + + // Entry to return. + $module = array(); + + // First, we return information that any user can see in (or can deduce from) the web interface. + $module['id'] = $wiki->id; + $module['coursemodule'] = $wiki->coursemodule; + $module['course'] = $wiki->course; + $module['name'] = external_format_string($wiki->name, $context->id); + + $viewablefields = []; + if (has_capability('mod/wiki:viewpage', $context)) { + list($module['intro'], $module['introformat']) = + external_format_text($wiki->intro, $wiki->introformat, $context->id, 'mod_wiki', 'intro', $wiki->id); + + $viewablefields = array('firstpagetitle', 'wikimode', 'defaultformat', 'forceformat', 'editbegin', 'editend', + 'section', 'visible', 'groupmode', 'groupingid'); + } + + // Check additional permissions for returning optional private settings. + if (has_capability('moodle/course:manageactivities', $context)) { + $additionalfields = array('timecreated', 'timemodified'); + $viewablefields = array_merge($viewablefields, $additionalfields); + } + + foreach ($viewablefields as $field) { + $module[$field] = $wiki->{$field}; + } + + // Check if user can add new pages. + $module['cancreatepages'] = wiki_can_create_pages($context); + + $returnedwikis[] = $module; + } + } + + $result = array(); + $result['wikis'] = $returnedwikis; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Describes the get_wikis_by_courses return value. + * + * @return external_single_structure + * @since Moodle 3.1 + */ + public static function get_wikis_by_courses_returns() { + + return new external_single_structure( + array( + 'wikis' => new external_multiple_structure( + new external_single_structure( + array( + 'id' => new external_value(PARAM_INT, 'Wiki ID.'), + 'coursemodule' => new external_value(PARAM_INT, 'Course module ID.'), + 'course' => new external_value(PARAM_INT, 'Course ID.'), + 'name' => new external_value(PARAM_RAW, 'Wiki name.'), + 'intro' => new external_value(PARAM_RAW, 'Wiki intro.', VALUE_OPTIONAL), + 'introformat' => new external_format_value('Wiki intro format.', VALUE_OPTIONAL), + 'timecreated' => new external_value(PARAM_INT, 'Time of creation.', VALUE_OPTIONAL), + 'timemodified' => new external_value(PARAM_INT, 'Time of last modification.', VALUE_OPTIONAL), + 'firstpagetitle' => new external_value(PARAM_RAW, 'First page title.', VALUE_OPTIONAL), + 'wikimode' => new external_value(PARAM_TEXT, 'Wiki mode (individual, collaborative).', VALUE_OPTIONAL), + 'defaultformat' => new external_value(PARAM_TEXT, 'Wiki\'s default format (html, creole, nwiki).', + VALUE_OPTIONAL), + 'forceformat' => new external_value(PARAM_INT, '1 if format is forced, 0 otherwise.', + VALUE_OPTIONAL), + 'editbegin' => new external_value(PARAM_INT, 'Edit begin.', VALUE_OPTIONAL), + 'editend' => new external_value(PARAM_INT, 'Edit end.', VALUE_OPTIONAL), + 'section' => new external_value(PARAM_INT, 'Course section ID.', VALUE_OPTIONAL), + 'visible' => new external_value(PARAM_INT, '1 if visible, 0 otherwise.', VALUE_OPTIONAL), + 'groupmode' => new external_value(PARAM_INT, 'Group mode.', VALUE_OPTIONAL), + 'groupingid' => new external_value(PARAM_INT, 'Group ID.', VALUE_OPTIONAL), + 'cancreatepages' => new external_value(PARAM_BOOL, 'True if user can create pages.'), + ), 'Wikis' + ) + ), + 'warnings' => new external_warnings(), + ) + ); + } + +} diff --git a/mod/wiki/db/services.php b/mod/wiki/db/services.php new file mode 100644 index 00000000000..89c59804800 --- /dev/null +++ b/mod/wiki/db/services.php @@ -0,0 +1,37 @@ +. + +/** + * Wiki external functions and service definitions. + * + * @package mod_wiki + * @category external + * @copyright 2015 Dani Palou + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.1 + */ + +$functions = array( + + 'mod_wiki_get_wikis_by_courses' => array( + 'classname' => 'mod_wiki_external', + 'methodname' => 'get_wikis_by_courses', + 'description' => 'Returns a list of wiki instances in a provided set of courses, if ' . + 'no courses are provided then all the wiki instances the user has access to will be returned.', + 'type' => 'read', + 'capabilities' => 'mod/wiki:viewpage' + ) +); diff --git a/mod/wiki/lib.php b/mod/wiki/lib.php index ddd785c1c19..4e0d4a640c9 100644 --- a/mod/wiki/lib.php +++ b/mod/wiki/lib.php @@ -495,7 +495,7 @@ function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) $pageid = $page->id; } - if (has_capability('mod/wiki:createpage', $context)) { + if (wiki_can_create_pages($context)) { $link = new moodle_url('/mod/wiki/create.php', array('action' => 'new', 'swid' => $swid)); $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING); } diff --git a/mod/wiki/locallib.php b/mod/wiki/locallib.php index 9a085a4cdef..e2395888b4f 100644 --- a/mod/wiki/locallib.php +++ b/mod/wiki/locallib.php @@ -1566,3 +1566,14 @@ function wiki_get_updated_pages_by_subwiki($swid) { ORDER BY timemodified DESC"; return $DB->get_records_sql($sql, array($swid, $USER->lastlogin)); } + +/** + * Check if the user can create pages in a certain wiki. + * @param context $context Wiki's context. + * @param integer|stdClass $user A user id or object. By default (null) checks the permissions of the current user. + * @return bool True if user can create pages, false otherwise. + * @since Moodle 3.1 + */ +function wiki_can_create_pages($context, $user = null) { + return has_capability('mod/wiki:createpage', $context, $user); +} diff --git a/mod/wiki/version.php b/mod/wiki/version.php index d493a4641ff..2fa3c25b296 100644 --- a/mod/wiki/version.php +++ b/mod/wiki/version.php @@ -33,7 +33,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_wiki'; // Full name of the plugin (used for diagnostics) $plugin->cron = 0; diff --git a/version.php b/version.php index b7bfddf0b34..535c1476dc0 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.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.