2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
22 * @copyright 2017 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die;
29 require_once("$CFG->libdir/externallib.php");
32 * Label external functions
36 * @copyright 2017 Juan Leyva <juan@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class mod_label_external extends external_api {
43 * Describes the parameters for get_labels_by_courses.
45 * @return external_function_parameters
48 public static function get_labels_by_courses_parameters() {
49 return new external_function_parameters (
51 'courseids' => new external_multiple_structure(
52 new external_value(PARAM_INT, 'Course id'), 'Array of course ids', VALUE_DEFAULT, array()
59 * Returns a list of labels in a provided list of courses.
60 * If no list is provided all labels that the user can view will be returned.
62 * @param array $courseids course ids
63 * @return array of warnings and labels
66 public static function get_labels_by_courses($courseids = array()) {
69 $returnedlabels = array();
72 'courseids' => $courseids,
74 $params = self::validate_parameters(self::get_labels_by_courses_parameters(), $params);
77 if (empty($params['courseids'])) {
78 $mycourses = enrol_get_my_courses();
79 $params['courseids'] = array_keys($mycourses);
82 // Ensure there are courseids to loop through.
83 if (!empty($params['courseids'])) {
85 list($courses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses);
87 // Get the labels in this course, this function checks users visibility permissions.
88 // We can avoid then additional validate_context calls.
89 $labels = get_all_instances_in_courses("label", $courses);
90 foreach ($labels as $label) {
91 $context = context_module::instance($label->coursemodule);
93 $label->name = external_format_string($label->name, $context->id);
95 list($label->intro, $label->introformat) = external_format_text($label->intro,
96 $label->introformat, $context->id, 'mod_label', 'intro', null);
97 $label->introfiles = external_util::get_area_files($context->id, 'mod_label', 'intro', false, false);
99 $returnedlabels[] = $label;
104 'labels' => $returnedlabels,
105 'warnings' => $warnings
111 * Describes the get_labels_by_courses return value.
113 * @return external_single_structure
116 public static function get_labels_by_courses_returns() {
117 return new external_single_structure(
119 'labels' => new external_multiple_structure(
120 new external_single_structure(
122 'id' => new external_value(PARAM_INT, 'Module id'),
123 'course' => new external_value(PARAM_INT, 'Course id'),
124 'name' => new external_value(PARAM_RAW, 'Label name'),
125 'intro' => new external_value(PARAM_RAW, 'Label contents'),
126 'introformat' => new external_format_value('intro', 'Content format'),
127 'introfiles' => new external_files('Files in the introduction text'),
128 'timemodified' => new external_value(PARAM_INT, 'Last time the label was modified'),
129 'section' => new external_value(PARAM_INT, 'Course section id'),
130 'visible' => new external_value(PARAM_INT, 'Module visibility'),
131 'groupmode' => new external_value(PARAM_INT, 'Group mode'),
132 'groupingid' => new external_value(PARAM_INT, 'Grouping id'),
136 'warnings' => new external_warnings(),