1 |
efrain |
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/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Questionnaire module external API
|
|
|
19 |
*
|
|
|
20 |
* @package mod_questionnaire
|
|
|
21 |
* @category external
|
|
|
22 |
* @copyright 2018 Igor Sazonov <sovletig@gmail.com>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
* @since Moodle 3.0
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die;
|
|
|
28 |
require_once($CFG->libdir . '/externallib.php');
|
|
|
29 |
require_once($CFG->dirroot . '/mod/questionnaire/lib.php');
|
|
|
30 |
require_once($CFG->dirroot . '/mod/questionnaire/questionnaire.class.php');
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Questionnaire module external functions
|
|
|
34 |
*
|
|
|
35 |
* @package mod_questionnaire
|
|
|
36 |
* @category external
|
|
|
37 |
* @copyright 2018 Igor Sazonov <sovletig@yandex.ru>
|
|
|
38 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
39 |
* @since Moodle 3.0
|
|
|
40 |
*/
|
|
|
41 |
class mod_questionnaire_external extends \external_api {
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Describes the parameters for submit_questionnaire_response.
|
|
|
45 |
*
|
|
|
46 |
* @return external_function_parameters
|
|
|
47 |
* @since Moodle 3.0
|
|
|
48 |
*/
|
|
|
49 |
public static function submit_questionnaire_response_parameters() {
|
|
|
50 |
return new \external_function_parameters(
|
|
|
51 |
[
|
|
|
52 |
'questionnaireid' => new \external_value(PARAM_INT, 'Questionnaire instance id'),
|
|
|
53 |
'surveyid' => new \external_value(PARAM_INT, 'Survey id'),
|
|
|
54 |
'userid' => new \external_value(PARAM_INT, 'User id'),
|
|
|
55 |
'cmid' => new \external_value(PARAM_INT, 'Course module id'),
|
|
|
56 |
'sec' => new \external_value(PARAM_INT, 'Section number'),
|
|
|
57 |
'completed' => new \external_value(PARAM_INT, 'Completed survey or not'),
|
|
|
58 |
'rid' => new \external_value(PARAM_INT, 'Existing response id'),
|
|
|
59 |
'submit' => new \external_value(PARAM_INT, 'Submit survey or not'),
|
|
|
60 |
'action' => new \external_value(PARAM_ALPHA, 'Page action'),
|
|
|
61 |
'responses' => new \external_multiple_structure(
|
|
|
62 |
new \external_single_structure(
|
|
|
63 |
[
|
|
|
64 |
'name' => new \external_value(PARAM_RAW, 'data key'),
|
|
|
65 |
'value' => new \external_value(PARAM_RAW, 'data value')
|
|
|
66 |
]
|
|
|
67 |
),
|
|
|
68 |
'The data to be saved', VALUE_DEFAULT, []
|
|
|
69 |
)
|
|
|
70 |
]
|
|
|
71 |
);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Submit questionnaire responses
|
|
|
76 |
*
|
|
|
77 |
* @param int $questionnaireid the questionnaire instance id
|
|
|
78 |
* @param int $surveyid Survey id
|
|
|
79 |
* @param int $userid User id
|
|
|
80 |
* @param int $cmid Course module id
|
|
|
81 |
* @param int $sec Section number
|
|
|
82 |
* @param int $completed Completed survey 1/0
|
|
|
83 |
* @param int $rid Already in progress response id.
|
|
|
84 |
* @param int $submit Submit survey?
|
|
|
85 |
* @param string $action
|
|
|
86 |
* @param array $responses the response ids
|
|
|
87 |
* @return array answers information and warnings
|
|
|
88 |
*/
|
|
|
89 |
public static function submit_questionnaire_response($questionnaireid, $surveyid, $userid, $cmid, $sec, $completed, $rid,
|
|
|
90 |
$submit, $action, $responses) {
|
|
|
91 |
self::validate_parameters(self::submit_questionnaire_response_parameters(),
|
|
|
92 |
[
|
|
|
93 |
'questionnaireid' => $questionnaireid,
|
|
|
94 |
'surveyid' => $surveyid,
|
|
|
95 |
'userid' => $userid,
|
|
|
96 |
'cmid' => $cmid,
|
|
|
97 |
'sec' => $sec,
|
|
|
98 |
'completed' => $completed,
|
|
|
99 |
'rid' => $rid,
|
|
|
100 |
'submit' => $submit,
|
|
|
101 |
'action' => $action,
|
|
|
102 |
'responses' => $responses
|
|
|
103 |
]
|
|
|
104 |
);
|
|
|
105 |
|
|
|
106 |
list($cm, $course, $questionnaire) = questionnaire_get_standard_page_items($cmid);
|
|
|
107 |
$questionnaire = new \questionnaire($course, $cm, 0, $questionnaire);
|
|
|
108 |
|
|
|
109 |
$context = \context_module::instance($cm->id);
|
|
|
110 |
self::validate_context($context);
|
|
|
111 |
|
|
|
112 |
require_capability('mod/questionnaire:submit', $context);
|
|
|
113 |
|
|
|
114 |
$result = $questionnaire->save_mobile_data($userid, $sec, $completed, $rid, $submit, $action, $responses);
|
|
|
115 |
$result['submitted'] = true;
|
|
|
116 |
if (isset($result['warnings']) && !empty($result['warnings'])) {
|
|
|
117 |
unset($result['responses']);
|
|
|
118 |
$result['submitted'] = false;
|
|
|
119 |
}
|
|
|
120 |
$result['warnings'] = [];
|
|
|
121 |
return $result;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Describes the submit_questionnaire_response return value.
|
|
|
126 |
*
|
|
|
127 |
* @return external_single_structure
|
|
|
128 |
* @since Moodle 3.0
|
|
|
129 |
*/
|
|
|
130 |
public static function submit_questionnaire_response_returns() {
|
|
|
131 |
return new \external_single_structure(
|
|
|
132 |
[
|
|
|
133 |
'submitted' => new \external_value(PARAM_BOOL, 'submitted', true, false, false),
|
|
|
134 |
'warnings' => new \external_warnings()
|
|
|
135 |
]
|
|
|
136 |
);
|
|
|
137 |
}
|
|
|
138 |
}
|