MDL-65026 course: Fix modules intro formatting in WS
[moodle.git] / mod / page / classes / external.php
blob511d22285f57ae45af9570298b210e36c168377d
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Page external API
20 * @package mod_page
21 * @category external
22 * @copyright 2015 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 3.0
27 defined('MOODLE_INTERNAL') || die;
29 require_once("$CFG->libdir/externallib.php");
31 /**
32 * Page external functions
34 * @package mod_page
35 * @category external
36 * @copyright 2015 Juan Leyva <juan@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @since Moodle 3.0
40 class mod_page_external extends external_api {
42 /**
43 * Returns description of method parameters
45 * @return external_function_parameters
46 * @since Moodle 3.0
48 public static function view_page_parameters() {
49 return new external_function_parameters(
50 array(
51 'pageid' => new external_value(PARAM_INT, 'page instance id')
56 /**
57 * Simulate the page/view.php web interface page: trigger events, completion, etc...
59 * @param int $pageid the page instance id
60 * @return array of warnings and status result
61 * @since Moodle 3.0
62 * @throws moodle_exception
64 public static function view_page($pageid) {
65 global $DB, $CFG;
66 require_once($CFG->dirroot . "/mod/page/lib.php");
68 $params = self::validate_parameters(self::view_page_parameters(),
69 array(
70 'pageid' => $pageid
71 ));
72 $warnings = array();
74 // Request and permission validation.
75 $page = $DB->get_record('page', array('id' => $params['pageid']), '*', MUST_EXIST);
76 list($course, $cm) = get_course_and_cm_from_instance($page, 'page');
78 $context = context_module::instance($cm->id);
79 self::validate_context($context);
81 require_capability('mod/page:view', $context);
83 // Call the page/lib API.
84 page_view($page, $course, $cm, $context);
86 $result = array();
87 $result['status'] = true;
88 $result['warnings'] = $warnings;
89 return $result;
92 /**
93 * Returns description of method result value
95 * @return external_description
96 * @since Moodle 3.0
98 public static function view_page_returns() {
99 return new external_single_structure(
100 array(
101 'status' => new external_value(PARAM_BOOL, 'status: true if success'),
102 'warnings' => new external_warnings()
108 * Describes the parameters for get_pages_by_courses.
110 * @return external_function_parameters
111 * @since Moodle 3.3
113 public static function get_pages_by_courses_parameters() {
114 return new external_function_parameters (
115 array(
116 'courseids' => new external_multiple_structure(
117 new external_value(PARAM_INT, 'Course id'), 'Array of course ids', VALUE_DEFAULT, array()
124 * Returns a list of pages in a provided list of courses.
125 * If no list is provided all pages that the user can view will be returned.
127 * @param array $courseids course ids
128 * @return array of warnings and pages
129 * @since Moodle 3.3
131 public static function get_pages_by_courses($courseids = array()) {
133 $warnings = array();
134 $returnedpages = array();
136 $params = array(
137 'courseids' => $courseids,
139 $params = self::validate_parameters(self::get_pages_by_courses_parameters(), $params);
141 $mycourses = array();
142 if (empty($params['courseids'])) {
143 $mycourses = enrol_get_my_courses();
144 $params['courseids'] = array_keys($mycourses);
147 // Ensure there are courseids to loop through.
148 if (!empty($params['courseids'])) {
150 list($courses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses);
152 // Get the pages in this course, this function checks users visibility permissions.
153 // We can avoid then additional validate_context calls.
154 $pages = get_all_instances_in_courses("page", $courses);
155 foreach ($pages as $page) {
156 $context = context_module::instance($page->coursemodule);
157 // Entry to return.
158 $page->name = external_format_string($page->name, $context->id);
160 $options = array('noclean' => true);
161 list($page->intro, $page->introformat) =
162 external_format_text($page->intro, $page->introformat, $context->id, 'mod_page', 'intro', null, $options);
163 $page->introfiles = external_util::get_area_files($context->id, 'mod_page', 'intro', false, false);
165 $options = array('noclean' => true);
166 list($page->content, $page->contentformat) = external_format_text($page->content, $page->contentformat,
167 $context->id, 'mod_page', 'content', $page->revision, $options);
168 $page->contentfiles = external_util::get_area_files($context->id, 'mod_page', 'content');
170 $returnedpages[] = $page;
174 $result = array(
175 'pages' => $returnedpages,
176 'warnings' => $warnings
178 return $result;
182 * Describes the get_pages_by_courses return value.
184 * @return external_single_structure
185 * @since Moodle 3.3
187 public static function get_pages_by_courses_returns() {
188 return new external_single_structure(
189 array(
190 'pages' => new external_multiple_structure(
191 new external_single_structure(
192 array(
193 'id' => new external_value(PARAM_INT, 'Module id'),
194 'coursemodule' => new external_value(PARAM_INT, 'Course module id'),
195 'course' => new external_value(PARAM_INT, 'Course id'),
196 'name' => new external_value(PARAM_RAW, 'Page name'),
197 'intro' => new external_value(PARAM_RAW, 'Summary'),
198 'introformat' => new external_format_value('intro', 'Summary format'),
199 'introfiles' => new external_files('Files in the introduction text'),
200 'content' => new external_value(PARAM_RAW, 'Page content'),
201 'contentformat' => new external_format_value('content', 'Content format'),
202 'contentfiles' => new external_files('Files in the content'),
203 'legacyfiles' => new external_value(PARAM_INT, 'Legacy files flag'),
204 'legacyfileslast' => new external_value(PARAM_INT, 'Legacy files last control flag'),
205 'display' => new external_value(PARAM_INT, 'How to display the page'),
206 'displayoptions' => new external_value(PARAM_RAW, 'Display options (width, height)'),
207 'revision' => new external_value(PARAM_INT, 'Incremented when after each file changes, to avoid cache'),
208 'timemodified' => new external_value(PARAM_INT, 'Last time the page was modified'),
209 'section' => new external_value(PARAM_INT, 'Course section id'),
210 'visible' => new external_value(PARAM_INT, 'Module visibility'),
211 'groupmode' => new external_value(PARAM_INT, 'Group mode'),
212 'groupingid' => new external_value(PARAM_INT, 'Grouping id'),
216 'warnings' => new external_warnings(),