1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Plugin callbacks
|
|
|
19 |
*
|
|
|
20 |
* @package tool_courserating
|
|
|
21 |
* @copyright 2022 Marina Glancy <marina.glancy@gmail.com>
|
|
|
22 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
use tool_courserating\external\ratings_list_exporter;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Callback allowing to add js to $PAGE->requires
|
|
|
29 |
*/
|
|
|
30 |
function tool_courserating_before_http_headers() {
|
|
|
31 |
// This is an implementation of a legacy callback that will only be called in older Moodle versions.
|
|
|
32 |
// It will not be called in Moodle versions that contain the hook core\hook\output\before_http_headers,
|
|
|
33 |
// instead, the callback tool_courserating\local\hooks\output\before_http_headers::callback will be executed.
|
|
|
34 |
|
|
|
35 |
global $PAGE, $CFG;
|
|
|
36 |
if (\tool_courserating\helper::course_ratings_enabled_anywhere() &&
|
|
|
37 |
!in_array($PAGE->pagelayout, ['redirect', 'embedded'])) {
|
|
|
38 |
// Add JS to all pages, the course ratings can be displayed on any page (for example course listings).
|
|
|
39 |
$branch = $CFG->branch ?? '';
|
|
|
40 |
$PAGE->requires->js_call_amd('tool_courserating/rating', 'init',
|
|
|
41 |
[context_system::instance()->id, "{$branch}" < "400"]);
|
|
|
42 |
if (\tool_courserating\helper::is_course_edit_page()) {
|
|
|
43 |
$field = \tool_courserating\helper::get_course_rating_field();
|
|
|
44 |
$PAGE->requires->js_call_amd('tool_courserating/rating', 'hideEditField',
|
|
|
45 |
[$field->get('shortname')]);
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
return null;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Callback allowing to add contetnt inside the region-main, in the very end
|
|
|
53 |
*
|
|
|
54 |
* @return string
|
|
|
55 |
*/
|
|
|
56 |
function tool_courserating_before_footer() {
|
|
|
57 |
// This is an implementation of a legacy callback that will only be called in older Moodle versions.
|
|
|
58 |
// It will not be called in Moodle versions that contain the hook core\hook\output\before_footer_html_generation,
|
|
|
59 |
// instead, the callback tool_courserating\local\hooks\output\before_footer_html_generation::callback will be executed.
|
|
|
60 |
|
|
|
61 |
global $PAGE;
|
|
|
62 |
$res = '';
|
|
|
63 |
if (\tool_courserating\helper::course_ratings_enabled_anywhere()) {
|
|
|
64 |
/** @var tool_courserating\output\renderer $output */
|
|
|
65 |
$output = $PAGE->get_renderer('tool_courserating');
|
|
|
66 |
if (($courseid = \tool_courserating\helper::is_course_page()) ||
|
|
|
67 |
($courseid = \tool_courserating\helper::is_single_activity_course_page())) {
|
|
|
68 |
$res .= $output->course_rating_block($courseid);
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
return $res;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Callback allowing to add to <head> of the page
|
|
|
76 |
*
|
|
|
77 |
* @return string
|
|
|
78 |
*/
|
|
|
79 |
function tool_courserating_before_standard_html_head() {
|
|
|
80 |
// This is an implementation of a legacy callback that will only be called in older Moodle versions.
|
|
|
81 |
// It will not be called in Moodle versions that contain the hook core\hook\output\before_standard_head_html_generation,
|
|
|
82 |
// instead, the callback tool_courserating\local\hooks\output\before_standard_head_html_generation::callback will be executed.
|
|
|
83 |
|
|
|
84 |
$res = '';
|
|
|
85 |
if (\tool_courserating\helper::course_ratings_enabled_anywhere()) {
|
|
|
86 |
// Add CSS to all pages, the course ratings can be displayed on any page (for example course listings).
|
|
|
87 |
$res .= '<style>' . \tool_courserating\helper::get_rating_colour_css() . '</style>';
|
|
|
88 |
}
|
|
|
89 |
return $res;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
// @codingStandardsIgnoreStart
|
|
|
93 |
/* More callbacks that can be implemented
|
|
|
94 |
|
|
|
95 |
function tool_courserating_render_navbar_output() {
|
|
|
96 |
// Added to the top navbar after messaging icon before the user picture/menu.
|
|
|
97 |
return '';
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
function tool_courserating_add_htmlattributes() {
|
|
|
101 |
// <html {{{ output.htmlattributes }}}>
|
|
|
102 |
return [];
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
function tool_courserating_standard_after_main_region_html() {
|
|
|
106 |
// Added in the very end of the page, must be floating element or otherwise it messes up layout
|
|
|
107 |
return '';
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
function tool_courserating_standard_footer_html() {
|
|
|
111 |
// Added after the "Reset user tour on this page" link in the popup footer
|
|
|
112 |
return '';
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
function tool_courserating_before_standard_top_of_body_html() {
|
|
|
116 |
// added before the <nav> element (top navbar)
|
|
|
117 |
return '';
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
|
|
|
121 |
function tool_courserating_after_config() {
|
|
|
122 |
return null;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
function tool_courserating_after_require_login() {
|
|
|
126 |
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
function tool_courserating_extend_navigation_user($usernode, $user, $usercontext, $course, $coursecontext) {
|
|
|
130 |
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
function tool_courserating_extend_navigation_course($coursenode, $course, $coursecontext) {
|
|
|
134 |
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
function tool_courserating_extend_navigation_user_settings($usersetting, $user, $usercontext, $course, $coursecontext) {
|
|
|
138 |
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
function tool_courserating_extend_navigation_category_settings($categorynode, $catcontext) {
|
|
|
142 |
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
function tool_courserating_extend_navigation_frontpage($frontpage, $course, $coursecontext) {
|
|
|
146 |
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
function tool_courserating_user_preferences() {
|
|
|
150 |
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
function tool_courserating_get_course_category_contents($coursecat) {
|
|
|
154 |
// To display what this category contains (on category deletion)
|
|
|
155 |
return '';
|
|
|
156 |
}
|
|
|
157 |
*/
|
|
|
158 |
// @codingStandardsIgnoreEnd
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Fragment API callback
|
|
|
162 |
*
|
|
|
163 |
* @param array $args
|
|
|
164 |
* @return string
|
|
|
165 |
*/
|
|
|
166 |
function tool_courserating_output_fragment_course_ratings_popup($args) {
|
|
|
167 |
global $PAGE;
|
|
|
168 |
if (!$courseid = clean_param($args['courseid'] ?? 0, PARAM_INT)) {
|
|
|
169 |
throw new moodle_exception('missingparam', '', '', 'courseid');
|
|
|
170 |
}
|
|
|
171 |
\tool_courserating\permission::require_can_view_ratings($courseid);
|
|
|
172 |
/** @var tool_courserating\output\renderer $output */
|
|
|
173 |
$output = $PAGE->get_renderer('tool_courserating');
|
|
|
174 |
return $output->course_ratings_popup($courseid);
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* Fragment API callback
|
|
|
179 |
*
|
|
|
180 |
* @param array $args
|
|
|
181 |
* @return string
|
|
|
182 |
*/
|
|
|
183 |
function tool_courserating_output_fragment_cfield($args) {
|
|
|
184 |
global $PAGE;
|
|
|
185 |
if (!$courseid = clean_param($args['courseid'] ?? 0, PARAM_INT)) {
|
|
|
186 |
throw new moodle_exception('missingparam', '', '', 'courseid');
|
|
|
187 |
}
|
|
|
188 |
\tool_courserating\permission::require_can_view_ratings($courseid);
|
|
|
189 |
/** @var tool_courserating\output\renderer $output */
|
|
|
190 |
$output = $PAGE->get_renderer('tool_courserating');
|
|
|
191 |
return $output->cfield($courseid);
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* Fragment API callback
|
|
|
196 |
*
|
|
|
197 |
* @param array $args
|
|
|
198 |
* @return string
|
|
|
199 |
*/
|
|
|
200 |
function tool_courserating_output_fragment_course_ratings_summary($args) {
|
|
|
201 |
global $PAGE;
|
|
|
202 |
if (!$courseid = clean_param($args['courseid'] ?? 0, PARAM_INT)) {
|
|
|
203 |
throw new moodle_exception('missingparam', '', '', 'courseid');
|
|
|
204 |
}
|
|
|
205 |
\tool_courserating\permission::require_can_view_ratings($courseid);
|
|
|
206 |
/** @var tool_courserating\output\renderer $output */
|
|
|
207 |
$output = $PAGE->get_renderer('tool_courserating');
|
|
|
208 |
$data = (new \tool_courserating\external\summary_exporter($courseid))->export($output);
|
|
|
209 |
return $output->render_from_template('tool_courserating/course_ratings_summary', $data);
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
/**
|
|
|
213 |
* Fragment API callback
|
|
|
214 |
*
|
|
|
215 |
* @param array $args
|
|
|
216 |
* @return string
|
|
|
217 |
*/
|
|
|
218 |
function tool_courserating_output_fragment_rating_flag($args) {
|
|
|
219 |
global $PAGE;
|
|
|
220 |
/** @var tool_courserating\output\renderer $output */
|
|
|
221 |
$output = $PAGE->get_renderer('tool_courserating');
|
|
|
222 |
|
|
|
223 |
if (!$ratingid = clean_param($args['ratingid'] ?? 0, PARAM_INT)) {
|
|
|
224 |
throw new moodle_exception('missingparam', '', '', 'ratingid');
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
$rating = new \tool_courserating\local\models\rating($args['ratingid']);
|
|
|
228 |
\tool_courserating\permission::require_can_view_ratings($rating->get('courseid'));
|
|
|
229 |
$data = (array)(new \tool_courserating\external\rating_exporter($rating))->export($output);
|
|
|
230 |
return $output->render_from_template('tool_courserating/rating_flag', $data['ratingflag']);
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
/**
|
|
|
234 |
* Map icons for font-awesome themes.
|
|
|
235 |
*/
|
|
|
236 |
function tool_courserating_get_fontawesome_icon_map() {
|
|
|
237 |
return [
|
|
|
238 |
'tool_courserating:star' => 'fa-star',
|
|
|
239 |
'tool_courserating:star-o' => 'fa-star-o',
|
|
|
240 |
'tool_courserating:star-half' => 'fa-star-half-full',
|
|
|
241 |
];
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
/**
|
|
|
245 |
* Implements callback inplace_editable() allowing to edit values in-place
|
|
|
246 |
*
|
|
|
247 |
* @param string $itemtype
|
|
|
248 |
* @param int $itemid
|
|
|
249 |
* @param mixed $newvalue
|
|
|
250 |
* @return \core\output\inplace_editable|void
|
|
|
251 |
*/
|
|
|
252 |
function tool_courserating_inplace_editable($itemtype, $itemid, $newvalue) {
|
|
|
253 |
global $CFG;
|
|
|
254 |
require_once($CFG->dirroot . '/lib/externallib.php');
|
|
|
255 |
\external_api::validate_context(context_system::instance());
|
|
|
256 |
if ($itemtype === 'flag') {
|
|
|
257 |
\tool_courserating\permission::require_can_flag_rating($itemid);
|
|
|
258 |
if ($newvalue) {
|
|
|
259 |
\tool_courserating\api::flag_review($itemid);
|
|
|
260 |
} else {
|
|
|
261 |
\tool_courserating\api::revoke_review_flag($itemid);
|
|
|
262 |
}
|
|
|
263 |
return \tool_courserating\api::get_flag_inplace_editable($itemid);
|
|
|
264 |
}
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
/**
|
|
|
268 |
* Fragment API callback
|
|
|
269 |
*
|
|
|
270 |
* @param array $args
|
|
|
271 |
* @return string
|
|
|
272 |
*/
|
|
|
273 |
function tool_courserating_output_fragment_course_reviews($args) {
|
|
|
274 |
global $PAGE;
|
|
|
275 |
$args = [
|
|
|
276 |
'courseid' => clean_param($args['courseid'] ?? 0, PARAM_INT),
|
|
|
277 |
'offset' => clean_param($args['offset'] ?? 0, PARAM_INT),
|
|
|
278 |
'withrating' => clean_param($args['withrating'] ?? 0, PARAM_INT),
|
|
|
279 |
];
|
|
|
280 |
if (!$args['courseid']) {
|
|
|
281 |
throw new moodle_exception('missingparam', '', '', 'courseid');
|
|
|
282 |
}
|
|
|
283 |
\tool_courserating\permission::require_can_view_ratings($args['courseid']);
|
|
|
284 |
/** @var tool_courserating\output\renderer $output */
|
|
|
285 |
$output = $PAGE->get_renderer('tool_courserating');
|
|
|
286 |
$data = (new ratings_list_exporter($args))->export($output);
|
|
|
287 |
return $output->render_from_template('tool_courserating/course_ratings_popup_reviews', $data);
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
/**
|
|
|
291 |
* Serves the files.
|
|
|
292 |
*
|
|
|
293 |
* @param stdClass $course course object
|
|
|
294 |
* @param stdClass $cm course module
|
|
|
295 |
* @param context $context context object
|
|
|
296 |
* @param string $filearea file area
|
|
|
297 |
* @param array $args extra arguments
|
|
|
298 |
* @param bool $forcedownload whether or not force download
|
|
|
299 |
* @param array $options additional options affecting the file serving
|
|
|
300 |
* @return bool|void false if file not found, does not return if found - just send the file
|
|
|
301 |
*/
|
|
|
302 |
function tool_courserating_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = []) {
|
|
|
303 |
if (!\tool_courserating\helper::get_setting(\tool_courserating\constants::SETTING_USEHTML)) {
|
|
|
304 |
return false;
|
|
|
305 |
}
|
|
|
306 |
if ($context->contextlevel != CONTEXT_COURSE) {
|
|
|
307 |
return false;
|
|
|
308 |
}
|
|
|
309 |
\tool_courserating\permission::require_can_view_ratings($context->instanceid);
|
|
|
310 |
|
|
|
311 |
if ($filearea !== 'review') {
|
|
|
312 |
return false;
|
|
|
313 |
}
|
|
|
314 |
$itemid = array_shift($args);
|
|
|
315 |
|
|
|
316 |
$rating = \tool_courserating\local\models\rating::get_record(['courseid' => $context->instanceid, 'id' => $itemid]);
|
|
|
317 |
if (!$rating) {
|
|
|
318 |
return false;
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
$fs = get_file_storage();
|
|
|
322 |
$relativepath = implode('/', $args);
|
|
|
323 |
$fullpath = "/$context->id/tool_courserating/$filearea/$itemid/$relativepath";
|
|
|
324 |
if (!($file = $fs->get_file_by_hash(sha1($fullpath))) || $file->is_directory()) {
|
|
|
325 |
return false;
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
// Set security posture for in-browser display.
|
|
|
329 |
if (!$forcedownload) {
|
|
|
330 |
header("Content-Security-Policy: default-src 'none'; img-src 'self'");
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
// Finally send the file.
|
|
|
334 |
send_stored_file($file, 0, 0, $forcedownload, $options);
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
/**
|
|
|
338 |
* Add 'Course ratings' to the course administration menu
|
|
|
339 |
*
|
|
|
340 |
* @param navigation_node $navigation The navigation node to extend
|
|
|
341 |
* @param stdClass $course The course to object for the report
|
|
|
342 |
* @param context $context The context of the course
|
|
|
343 |
*/
|
|
|
344 |
function tool_courserating_extend_navigation_course(\navigation_node $navigation, \stdClass $course, \context $context) {
|
|
|
345 |
if (!\tool_courserating\permission::can_view_report($course->id)) {
|
|
|
346 |
return;
|
|
|
347 |
}
|
|
|
348 |
$url = new moodle_url('/admin/tool/courserating/index.php', ['id' => $course->id]);
|
|
|
349 |
$navigation->add(
|
|
|
350 |
get_string('pluginname', 'tool_courserating'),
|
|
|
351 |
$url,
|
|
|
352 |
navigation_node::TYPE_SETTING,
|
|
|
353 |
null,
|
|
|
354 |
null,
|
|
|
355 |
new pix_icon('i/report', '')
|
|
|
356 |
);
|
|
|
357 |
}
|