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 |
namespace tool_courserating\output;
|
|
|
18 |
|
|
|
19 |
use plugin_renderer_base;
|
|
|
20 |
use tool_courserating\api;
|
|
|
21 |
use tool_courserating\constants;
|
|
|
22 |
use tool_courserating\external\summary_exporter;
|
|
|
23 |
use tool_courserating\external\ratings_list_exporter;
|
|
|
24 |
use tool_courserating\helper;
|
|
|
25 |
use tool_courserating\local\models\rating;
|
|
|
26 |
use tool_courserating\local\models\summary;
|
|
|
27 |
use tool_courserating\permission;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Renderer
|
|
|
31 |
*
|
|
|
32 |
* @package tool_courserating
|
|
|
33 |
* @copyright 2022 Marina Glancy <marina.glancy@gmail.com>
|
|
|
34 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class renderer extends plugin_renderer_base {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Reads contents of a custom field and displays it
|
|
|
40 |
*
|
|
|
41 |
* @param int $courseid
|
|
|
42 |
* @return string
|
|
|
43 |
*/
|
|
|
44 |
public function cfield(int $courseid): string {
|
|
|
45 |
$summary = summary::get_for_course($courseid);
|
|
|
46 |
$data = (new summary_exporter(0, $summary))->export($this);
|
|
|
47 |
return $this->render_from_template('tool_courserating/summary_for_cfield', $data);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Content of a course rating summary popup
|
|
|
52 |
*
|
|
|
53 |
* @param int $courseid
|
|
|
54 |
* @return string
|
|
|
55 |
*/
|
|
|
56 |
public function course_ratings_popup(int $courseid): string {
|
|
|
57 |
global $USER;
|
|
|
58 |
$data1 = (new summary_exporter($courseid))->export($this);
|
|
|
59 |
$data2 = (new ratings_list_exporter(['courseid' => $courseid]))->export($this);
|
|
|
60 |
$data = (array)$data1 + (array)$data2;
|
|
|
61 |
$data['canrate'] = permission::can_add_rating($courseid);
|
|
|
62 |
$data['hasrating'] = $data['canrate'] && rating::get_record(['userid' => $USER->id, 'courseid' => $courseid]);
|
|
|
63 |
$this->page->requires->js_call_amd('tool_courserating/rating', 'setupViewRatingsPopup', []);
|
|
|
64 |
return $this->render_from_template('tool_courserating/course_ratings_popup', $data);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Course review widget to be added to the course page
|
|
|
69 |
*
|
|
|
70 |
* @param int $courseid
|
|
|
71 |
* @return string
|
|
|
72 |
*/
|
|
|
73 |
public function course_rating_block(int $courseid): string {
|
|
|
74 |
global $CFG, $USER;
|
|
|
75 |
if (!permission::can_view_ratings($courseid)) {
|
|
|
76 |
return '';
|
|
|
77 |
}
|
|
|
78 |
$summary = summary::get_for_course($courseid);
|
|
|
79 |
$canrate = permission::can_add_rating($courseid);
|
|
|
80 |
$data = (new summary_exporter(0, $summary, $canrate))->export($this);
|
|
|
81 |
$data->canrate = $canrate;
|
|
|
82 |
$data->hasrating = $canrate && rating::get_record(['userid' => $USER->id, 'courseid' => $courseid]);
|
|
|
83 |
|
|
|
84 |
$branch = $CFG->branch ?? '';
|
|
|
85 |
if ($parentcss = helper::get_setting(constants::SETTING_PARENTCSS)) {
|
|
|
86 |
$data->parentelement = $parentcss;
|
|
|
87 |
} else if ("{$branch}" === '311') {
|
|
|
88 |
$data->parentelement = '#page-header .card-body, #page-header #course-header, #page-header';
|
|
|
89 |
} else if ("{$branch}" >= '400') {
|
|
|
90 |
$data->parentelement = '#page-header';
|
|
|
91 |
$data->extraclasses = 'pb-2';
|
|
|
92 |
}
|
|
|
93 |
return $this->render_from_template('tool_courserating/course_rating_block', $data);
|
|
|
94 |
}
|
|
|
95 |
}
|