+<?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/>.
+
+/**
+ * Wiki module external API.
+ *
+ * @package mod_wiki
+ * @category external
+ * @copyright 2015 Dani Palou <dani@moodle.com>
+ * @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 <dani@moodle.com>
+ * @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(),
+ )
+ );
+ }
+
+}