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;
|
|
|
18 |
|
|
|
19 |
use required_capability_exception;
|
|
|
20 |
use tool_courserating\local\models\rating;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Permission checks
|
|
|
24 |
*
|
|
|
25 |
* @package tool_courserating
|
|
|
26 |
* @copyright 2022 Marina Glancy <marina.glancy@gmail.com>
|
|
|
27 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
class permission {
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* User can view rating for the course (ratings are enabled for this course)
|
|
|
33 |
*
|
|
|
34 |
* @param int $courseid
|
|
|
35 |
* @return bool
|
|
|
36 |
*/
|
|
|
37 |
public static function can_view_ratings(int $courseid): bool {
|
|
|
38 |
global $USER;
|
|
|
39 |
if (helper::get_course_rating_mode($courseid) == constants::RATEBY_NOONE) {
|
|
|
40 |
return false;
|
|
|
41 |
}
|
|
|
42 |
$course = get_course($courseid);
|
|
|
43 |
$context = \context_course::instance($courseid);
|
|
|
44 |
return \core_course_category::can_view_course_info($course) ||
|
|
|
45 |
is_enrolled($context, $USER, '', true);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Can the current user add a rating for the specified course
|
|
|
50 |
*
|
|
|
51 |
* Example of checking last access:
|
|
|
52 |
* $lastaccess = $DB->get_field('user_lastaccess', 'timeaccess', ['userid' => $USER->id, 'courseid' => $courseid]);
|
|
|
53 |
*
|
|
|
54 |
* @param int $courseid
|
|
|
55 |
* @return bool
|
|
|
56 |
* @throws \coding_exception
|
|
|
57 |
*/
|
|
|
58 |
public static function can_add_rating(int $courseid): bool {
|
|
|
59 |
global $CFG, $USER;
|
|
|
60 |
if (!has_capability('tool/courserating:rate', \context_course::instance($courseid))) {
|
|
|
61 |
return false;
|
|
|
62 |
}
|
|
|
63 |
$courseratingmode = helper::get_course_rating_mode($courseid);
|
|
|
64 |
if ($courseratingmode == constants::RATEBY_NOONE) {
|
|
|
65 |
return false;
|
|
|
66 |
}
|
|
|
67 |
if ($courseratingmode == constants::RATEBY_COMPLETED) {
|
|
|
68 |
require_once($CFG->dirroot.'/completion/completion_completion.php');
|
|
|
69 |
// The course is supposed to be marked as completed at $timeend.
|
|
|
70 |
$ccompletion = new \completion_completion(['userid' => $USER->id, 'course' => $courseid]);
|
|
|
71 |
return $ccompletion->is_complete();
|
|
|
72 |
}
|
|
|
73 |
return true;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Does current user have capability to delete ratings
|
|
|
78 |
*
|
|
|
79 |
* @param int $ratingid
|
|
|
80 |
* @param int|null $courseid
|
|
|
81 |
* @return bool
|
|
|
82 |
*/
|
|
|
83 |
public static function can_delete_rating(int $ratingid, ?int $courseid = null): bool {
|
|
|
84 |
if (!$courseid) {
|
|
|
85 |
$courseid = (new rating($ratingid))->get('courseid');
|
|
|
86 |
}
|
|
|
87 |
return has_capability('tool/courserating:delete', \context_course::instance($courseid));
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Can current user flag the rating
|
|
|
92 |
*
|
|
|
93 |
* @param int $ratingid
|
|
|
94 |
* @param int|null $courseid course id if known (saves a DB query)
|
|
|
95 |
* @return bool
|
|
|
96 |
*/
|
|
|
97 |
public static function can_flag_rating(int $ratingid, ?int $courseid = null): bool {
|
|
|
98 |
if (!isloggedin() || isguestuser()) {
|
|
|
99 |
return false;
|
|
|
100 |
}
|
|
|
101 |
if (!$courseid) {
|
|
|
102 |
$courseid = (new rating($ratingid))->get('courseid');
|
|
|
103 |
}
|
|
|
104 |
return self::can_view_ratings($courseid);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* User can view the 'Course ratings' item in the course administration
|
|
|
109 |
*
|
|
|
110 |
* @param int $courseid
|
|
|
111 |
* @return bool
|
|
|
112 |
*/
|
|
|
113 |
public static function can_view_report(int $courseid): bool {
|
|
|
114 |
if (!helper::course_ratings_enabled_anywhere()) {
|
|
|
115 |
return false;
|
|
|
116 |
}
|
|
|
117 |
$context = \context_course::instance($courseid);
|
|
|
118 |
return has_capability('tool/courserating:reports', $context);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Check that user can view rating or throw exception
|
|
|
123 |
*
|
|
|
124 |
* @param int $courseid
|
|
|
125 |
* @throws \moodle_exception
|
|
|
126 |
*/
|
|
|
127 |
public static function require_can_view_ratings(int $courseid): void {
|
|
|
128 |
if (!self::can_view_ratings($courseid)) {
|
|
|
129 |
throw new \moodle_exception('cannotview', 'tool_courserating');
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Check that user can add/change rating or throw exception
|
|
|
135 |
*
|
|
|
136 |
* @param int $courseid
|
|
|
137 |
* @throws \moodle_exception
|
|
|
138 |
*/
|
|
|
139 |
public static function require_can_add_rating(int $courseid): void {
|
|
|
140 |
if (!self::can_add_rating($courseid)) {
|
|
|
141 |
throw new \moodle_exception('cannotrate', 'tool_courserating');
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* Check that user can delete rating or throw exception
|
|
|
147 |
*
|
|
|
148 |
* @param int $ratingid
|
|
|
149 |
* @param int|null $courseid
|
|
|
150 |
* @throws required_capability_exception
|
|
|
151 |
*/
|
|
|
152 |
public static function require_can_delete_rating(int $ratingid, ?int $courseid = null): void {
|
|
|
153 |
if (!$courseid) {
|
|
|
154 |
$courseid = (new rating($ratingid))->get('courseid');
|
|
|
155 |
}
|
|
|
156 |
if (!self::can_delete_rating($ratingid, $courseid)) {
|
|
|
157 |
throw new required_capability_exception(\context_course::instance($courseid),
|
|
|
158 |
'tool/courserating:delete', 'nopermissions', '');
|
|
|
159 |
}
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* Check that user can flag rating or throw exception
|
|
|
164 |
*
|
|
|
165 |
* @param int $ratingid
|
|
|
166 |
* @param int|null $courseid
|
|
|
167 |
* @throws \moodle_exception
|
|
|
168 |
*/
|
|
|
169 |
public static function require_can_flag_rating(int $ratingid, ?int $courseid = null): void {
|
|
|
170 |
if (!self::can_flag_rating($ratingid, $courseid)) {
|
|
|
171 |
throw new \moodle_exception('cannotview', 'tool_courserating');
|
|
|
172 |
}
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
/**
|
|
|
176 |
* Check that user can view rating or throw exception
|
|
|
177 |
*
|
|
|
178 |
* @param int $courseid
|
|
|
179 |
* @throws \moodle_exception
|
|
|
180 |
*/
|
|
|
181 |
public static function require_can_view_reports(int $courseid): void {
|
|
|
182 |
if (!\tool_courserating\helper::course_ratings_enabled_anywhere()) {
|
|
|
183 |
// TODO create a new string, maybe link to settings for admins?
|
|
|
184 |
throw new \moodle_exception('ratebynoone', 'tool_courserating');
|
|
|
185 |
}
|
|
|
186 |
if (!self::can_view_report($courseid)) {
|
|
|
187 |
throw new required_capability_exception(\context_course::instance($courseid),
|
|
|
188 |
'tool/courserating:reports', 'nopermissions', '');
|
|
|
189 |
}
|
|
|
190 |
}
|
|
|
191 |
}
|