MDL-51629 mod_survey: New WS mod_survey_get_surveys_by_courses
[moodle.git] / mod / survey / classes / external.php
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.
13 //
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  * Survey external API
19  *
20  * @package    mod_survey
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
25  */
27 defined('MOODLE_INTERNAL') || die;
29 require_once("$CFG->libdir/externallib.php");
31 /**
32  * Survey external functions
33  *
34  * @package    mod_survey
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
39  */
40 class mod_survey_external extends external_api {
42     /**
43      * Describes the parameters for get_surveys_by_courses.
44      *
45      * @return external_external_function_parameters
46      * @since Moodle 3.0
47      */
48     public static function get_surveys_by_courses_parameters() {
49         return new external_function_parameters (
50             array(
51                 'courseids' => new external_multiple_structure(
52                     new external_value(PARAM_INT, 'course id'), 'Array of course ids', VALUE_DEFAULT, array()
53                 ),
54             )
55         );
56     }
58     /**
59      * Returns a list of surveys in a provided list of courses,
60      * if no list is provided all surveys that the user can view will be returned.
61      *
62      * @param array $courseids the course ids
63      * @return array of surveys details
64      * @since Moodle 3.0
65      */
66     public static function get_surveys_by_courses($courseids = array()) {
67         global $CFG, $USER, $DB;
69         $returnedsurveys = array();
70         $warnings = array();
72         $params = self::validate_parameters(self::get_surveys_by_courses_parameters(), array('courseids' => $courseids));
74         if (empty($params['courseids'])) {
75             $params['courseids'] = array_keys(enrol_get_my_courses());
76         }
78         // Ensure there are courseids to loop through.
79         if (!empty($params['courseids'])) {
81             list($courses, $warnings) = external_util::validate_courses($params['courseids']);
83             // Get the surveys in this course, this function checks users visibility permissions.
84             // We can avoid then additional validate_context calls.
85             $surveys = get_all_instances_in_courses("survey", $courses);
86             foreach ($surveys as $survey) {
87                 $context = context_module::instance($survey->coursemodule);
88                 // Entry to return.
89                 $surveydetails = array();
90                 // First, we return information that any user can see in the web interface.
91                 $surveydetails['id'] = $survey->id;
92                 $surveydetails['coursemodule']      = $survey->coursemodule;
93                 $surveydetails['course']            = $survey->course;
94                 $surveydetails['name']              = external_format_string($survey->name, $context->id);
96                 if (has_capability('mod/survey:participate', $context)) {
97                     $trimmedintro = trim($survey->intro);
98                     if (empty($trimmedintro)) {
99                         $tempo = $DB->get_field("survey", "intro", array("id" => $survey->template));
100                         $survey->intro = get_string($tempo, "survey");
101                     }
103                     // Format intro.
104                     list($surveydetails['intro'], $surveydetails['introformat']) =
105                         external_format_text($survey->intro, $survey->introformat, $context->id, 'mod_survey', 'intro', null);
107                     $surveydetails['template']  = $survey->template;
108                     $surveydetails['days']      = $survey->days;
109                     $surveydetails['questions'] = $survey->questions;
110                     $surveydetails['surveydone'] = survey_already_done($survey->id, $USER->id) ? 1 : 0;
112                 }
114                 if (has_capability('moodle/course:manageactivities', $context)) {
115                     $surveydetails['timecreated']   = $survey->timecreated;
116                     $surveydetails['timemodified']  = $survey->timemodified;
117                     $surveydetails['section']       = $survey->section;
118                     $surveydetails['visible']       = $survey->visible;
119                     $surveydetails['groupmode']     = $survey->groupmode;
120                     $surveydetails['groupingid']    = $survey->groupingid;
121                 }
122                 $returnedsurveys[] = $surveydetails;
123             }
124         }
125         $result = array();
126         $result['surveys'] = $returnedsurveys;
127         $result['warnings'] = $warnings;
128         return $result;
129     }
131     /**
132      * Describes the get_surveys_by_courses return value.
133      *
134      * @return external_single_structure
135      * @since Moodle 3.0
136      */
137     public static function get_surveys_by_courses_returns() {
138         return new external_single_structure(
139             array(
140                 'surveys' => new external_multiple_structure(
141                     new external_single_structure(
142                         array(
143                             'id' => new external_value(PARAM_INT, 'Survey id'),
144                             'coursemodule' => new external_value(PARAM_INT, 'Course module id'),
145                             'course' => new external_value(PARAM_INT, 'Course id'),
146                             'name' => new external_value(PARAM_RAW, 'Survey name'),
147                             'intro' => new external_value(PARAM_RAW, 'The Survey intro', VALUE_OPTIONAL),
148                             'introformat' => new external_format_value('intro', VALUE_OPTIONAL),
149                             'template' => new external_value(PARAM_INT, 'Survey type', VALUE_OPTIONAL),
150                             'days' => new external_value(PARAM_INT, 'Days', VALUE_OPTIONAL),
151                             'questions' => new external_value(PARAM_RAW, 'Question ids', VALUE_OPTIONAL),
152                             'surveydone' => new external_value(PARAM_INT, 'Did I finish the survey?', VALUE_OPTIONAL),
153                             'timecreated' => new external_value(PARAM_INT, 'Time of creation', VALUE_OPTIONAL),
154                             'timemodified' => new external_value(PARAM_INT, 'Time of last modification', VALUE_OPTIONAL),
155                             'section' => new external_value(PARAM_INT, 'Course section id', VALUE_OPTIONAL),
156                             'visible' => new external_value(PARAM_INT, 'Visible', VALUE_OPTIONAL),
157                             'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL),
158                             'groupingid' => new external_value(PARAM_INT, 'Group id', VALUE_OPTIONAL),
159                         ), 'Surveys'
160                     )
161                 ),
162                 'warnings' => new external_warnings(),
163             )
164         );
165     }