-
Notifications
You must be signed in to change notification settings - Fork 0
/
externallib.php
159 lines (149 loc) · 5.63 KB
/
externallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?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/>.
/**
* feedbackbox module external API
*
* @package mod_feedbackbox
* @category external
* @copyright 2018 Igor Sazonov <sovletig@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/
use mod_feedbackbox\feedbackbox;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->dirroot . '/mod/feedbackbox/lib.php');
/**
* feedbackbox module external functions
*
* @package mod_feedbackbox
* @category external
* @copyright 2018 Igor Sazonov <sovletig@yandex.ru>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/
class mod_feedbackbox_external extends external_api {
/**
* @return external_function_parameters
* @noinspection PhpUnused
*/
public static function get_chartdata_single_parameters() {
return new external_function_parameters(
[
'feedbackboxid' => new external_value(PARAM_INT, 'feedbackbox id'),
'turnus' => new external_value(PARAM_INT, 'Course module id', VALUE_OPTIONAL),
]
);
}
/**
* @param $feedbackboxid
* @param $turnus
* @return stdClass
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
* @noinspection PhpUnused
*/
public static function get_chartdata_single($feedbackboxid, $turnus) {
global $DB, $USER;
$result = new stdClass();
if (!$cm = get_coursemodule_from_instance('feedbackbox', $feedbackboxid)) {
throw new \moodle_exception('invalidcoursemodule', 'error');
}
if (!has_capability('mod/feedbackbox:manage', context_module::instance($cm->id), $USER->id)) {
throw new \moodle_exception('nopermissions', 'error');
}
$course = $DB->get_record("course", ["id" => $cm->course]);
$feedbackbox = $DB->get_record('feedbackbox', ['id' => $feedbackboxid]);
$feedbackbox = new feedbackbox($feedbackboxid, $feedbackbox, $course, $cm);
$data = $feedbackbox->get_turnus_responses($turnus);
$result->data = $data->rating;
$result->labels = $data->ratinglabel;
return $result;
}
/**
* @return external_single_structure
* @noinspection PhpUnused
*/
public static function get_chartdata_single_returns() {
return new external_single_structure(
[
'data' => new external_multiple_structure(
new external_value(PARAM_FLOAT, 'data', VALUE_OPTIONAL)
),
'labels' => new external_multiple_structure(
new external_value(PARAM_TEXT, 'label', VALUE_OPTIONAL)
)
]
);
}
/**
* @return external_function_parameters
* @noinspection PhpUnused
*/
public static function get_chartdata_multiple_parameters() {
return new external_function_parameters(
[
'feedbackboxid' => new external_value(PARAM_INT, 'feedbackbox id'),
'turnus' => new external_value(PARAM_INT, 'Course module id', VALUE_OPTIONAL),
]
);
}
/**
* @param $feedbackboxid
* @return stdClass
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
* @noinspection PhpUnused
*/
public static function get_chartdata_multiple($feedbackboxid) {
global $DB, $USER;
$result = new stdClass();
if (!$cm = get_coursemodule_from_instance('feedbackbox', $feedbackboxid)) {
throw new \moodle_exception('invalidcoursemodule', 'error');
}
if (!has_capability('mod/feedbackbox:manage', context_module::instance($cm->id), $USER->id)) {
throw new \moodle_exception('nopermissions', 'error');
}
$course = $DB->get_record("course", ["id" => $cm->course]);
$feedbackbox = $DB->get_record('feedbackbox', ['id' => $feedbackboxid]);
$feedbackbox = new feedbackbox($feedbackboxid, $feedbackbox, $course, $cm);
$data = null;
$data = $feedbackbox->get_feedback_responses();
$result->data = [];
foreach ($data->zones as $entry) { // round
$result->data[] = (object) ['rating' => round($entry->rating, 2), 'participants' => $entry->participants];
}
return $result;
}
/**
* @return external_single_structure
* @noinspection PhpUnused
*/
public static function get_chartdata_multiple_returns() {
return new external_single_structure(
[
'data' => new external_multiple_structure(
new external_single_structure([
'rating' => new external_value(PARAM_FLOAT, 'rating', VALUE_OPTIONAL),
'participants' => new external_value(PARAM_INT, 'participants', VALUE_OPTIONAL),
])
)
]
);
}
}