4 |
ariadna |
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 |
* Point of View external lib
|
|
|
19 |
*
|
|
|
20 |
* @package block_point_view
|
|
|
21 |
* @copyright 2020 Quentin Fombaron, 2021 Astor Bizard
|
|
|
22 |
* @author Quentin Fombaron <q.fombaron@outlook.fr>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
global $CFG;
|
|
|
29 |
require_once($CFG->libdir . '/externallib.php');
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Class block_point_view_external
|
|
|
33 |
*
|
|
|
34 |
* @package block_point_view
|
|
|
35 |
* @copyright 2020 Quentin Fombaron
|
|
|
36 |
* @author Quentin Fombaron <q.fombaron@outlook.fr>
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
*/
|
|
|
39 |
class block_point_view_external extends external_api {
|
|
|
40 |
/**
|
|
|
41 |
* Parameters definition for update_db.
|
|
|
42 |
*/
|
|
|
43 |
public static function update_db_parameters() {
|
|
|
44 |
return new external_function_parameters(
|
|
|
45 |
[
|
|
|
46 |
'func' => new external_value(PARAM_TEXT, 'function name to call', VALUE_REQUIRED),
|
|
|
47 |
'courseid' => new external_value(PARAM_INT, 'id of course', VALUE_REQUIRED),
|
|
|
48 |
'cmid' => new external_value(PARAM_INT, 'id of course module', VALUE_DEFAULT, 0),
|
|
|
49 |
'vote' => new external_value(PARAM_INT, 'id of vote', VALUE_DEFAULT, 0),
|
|
|
50 |
]
|
|
|
51 |
);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Update the database after added, updated or removed vote on a course module, or a full reset of course reactions.
|
|
|
56 |
*
|
|
|
57 |
* @param string $func Function name of database update ('update' or 'reset').
|
|
|
58 |
* @param int $courseid Course ID
|
|
|
59 |
* @param int $cmid Course Module ID
|
|
|
60 |
* @param int $vote Vote ID
|
|
|
61 |
* @return string Log message
|
|
|
62 |
*/
|
|
|
63 |
public static function update_db(string $func, int $courseid, int $cmid, int $vote) {
|
|
|
64 |
global $DB, $USER;
|
|
|
65 |
|
|
|
66 |
$params = self::validate_parameters(self::update_db_parameters(), [
|
|
|
67 |
'func' => $func,
|
|
|
68 |
'courseid' => $courseid,
|
|
|
69 |
'cmid' => $cmid,
|
|
|
70 |
'vote' => $vote,
|
|
|
71 |
]
|
|
|
72 |
);
|
|
|
73 |
|
|
|
74 |
$table = 'block_point_view';
|
|
|
75 |
|
|
|
76 |
$coursecontext = context_course::instance($courseid);
|
|
|
77 |
|
|
|
78 |
switch ($params['func']) {
|
|
|
79 |
case 'update':
|
|
|
80 |
|
|
|
81 |
$blockrecord = $DB->get_record('block_instances',
|
|
|
82 |
[ 'blockname' => 'point_view', 'parentcontextid' => $coursecontext->id ], '*', MUST_EXIST);
|
|
|
83 |
$blockinstance = block_instance('point_view', $blockrecord);
|
|
|
84 |
|
|
|
85 |
$canreact = isset($blockinstance->config->enable_point_views)
|
|
|
86 |
&& $blockinstance->config->enable_point_views
|
|
|
87 |
&& isset($blockinstance->config->{'moduleselectm' . $params['cmid']})
|
|
|
88 |
&& $blockinstance->config->{'moduleselectm' . $params['cmid']}
|
|
|
89 |
&& get_fast_modinfo($params['courseid'], $USER->id)->cms[$params['cmid']]->uservisible;
|
|
|
90 |
|
|
|
91 |
if (!$canreact) {
|
|
|
92 |
throw new moodle_exception('reactionsunavailable', 'block_point_view');
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
$dbparams = [
|
|
|
96 |
'userid' => $USER->id,
|
|
|
97 |
'courseid' => $params['courseid'],
|
|
|
98 |
'cmid' => $params['cmid'],
|
|
|
99 |
];
|
|
|
100 |
|
|
|
101 |
if ($params['vote'] === 0) {
|
|
|
102 |
$DB->delete_records($table, $dbparams);
|
|
|
103 |
} else {
|
|
|
104 |
$currentvote = $DB->get_record($table, $dbparams);
|
|
|
105 |
if ($currentvote === false) {
|
|
|
106 |
$dbparams['vote'] = $params['vote'];
|
|
|
107 |
$DB->insert_record($table, $dbparams);
|
|
|
108 |
} else {
|
|
|
109 |
$currentvote->vote = $params['vote'];
|
|
|
110 |
$DB->update_record($table, $currentvote);
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
break;
|
|
|
115 |
case 'reset':
|
|
|
116 |
// Reset all reactions of course or module.
|
|
|
117 |
require_capability('moodle/site:manageblocks', $coursecontext);
|
|
|
118 |
if ($params['cmid'] == 0) {
|
|
|
119 |
// Delete for all the course.
|
|
|
120 |
$DB->delete_records($table, [ 'courseid' => $params['courseid'] ]);
|
|
|
121 |
} else {
|
|
|
122 |
// Delete only for a module.
|
|
|
123 |
$DB->delete_records($table, [ 'courseid' => $params['courseid'], 'cmid' => $params['cmid'] ]);
|
|
|
124 |
}
|
|
|
125 |
break;
|
|
|
126 |
case 'cleanup':
|
|
|
127 |
// Cleanup reactions of users no longer enrolled in course.
|
|
|
128 |
require_capability('moodle/site:manageblocks', $coursecontext);
|
|
|
129 |
$users = get_enrolled_users($coursecontext, '', 0, 'u.id');
|
|
|
130 |
list($notin, $sqlparams) = $DB->get_in_or_equal(array_column($users, 'id'), SQL_PARAMS_QM, '', false);
|
|
|
131 |
$sqlparams[] = $params['courseid'];
|
|
|
132 |
$DB->delete_records_select($table, "userid $notin AND courseid = ?", $sqlparams);
|
|
|
133 |
break;
|
|
|
134 |
default:
|
|
|
135 |
break;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
return '';
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Returns a log message
|
|
|
143 |
*
|
|
|
144 |
* @return external_description
|
|
|
145 |
*/
|
|
|
146 |
public static function update_db_returns() {
|
|
|
147 |
return new external_value(PARAM_TEXT, 'Log message');
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Parameters definition for delete_custom_pix.
|
|
|
153 |
*/
|
|
|
154 |
public static function delete_custom_pix_parameters() {
|
|
|
155 |
return new external_function_parameters(
|
|
|
156 |
[
|
|
|
157 |
'contextid' => new external_value(PARAM_INT, 'id of context', VALUE_REQUIRED),
|
|
|
158 |
'courseid' => new external_value(PARAM_INT, 'id of course', VALUE_REQUIRED),
|
|
|
159 |
'draftitemid' => new external_value(PARAM_INT, 'id of draft file area', VALUE_REQUIRED),
|
|
|
160 |
]
|
|
|
161 |
);
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
/**
|
|
|
165 |
* Delete custom emoji for a block instance.
|
|
|
166 |
*
|
|
|
167 |
* @param int $contextid Context in which files are stored.
|
|
|
168 |
* @param int $courseid Course id.
|
|
|
169 |
* @param int $draftitemid Draft area id.
|
|
|
170 |
* @return boolean true on success
|
|
|
171 |
*/
|
|
|
172 |
public static function delete_custom_pix($contextid, $courseid, $draftitemid) {
|
|
|
173 |
global $USER;
|
|
|
174 |
require_capability('moodle/site:manageblocks', context_course::instance($courseid));
|
|
|
175 |
$fs = get_file_storage();
|
|
|
176 |
$success = $fs->delete_area_files($contextid, 'block_point_view', 'point_views_pix');
|
|
|
177 |
$success = $success && $fs->delete_area_files(context_user::instance($USER->id)->id, 'user', 'draft', $draftitemid);
|
|
|
178 |
return $success;
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* Return true on success
|
|
|
183 |
*/
|
|
|
184 |
public static function delete_custom_pix_returns() {
|
|
|
185 |
return new external_value(PARAM_BOOL, 'Whether operation was successful', VALUE_REQUIRED);
|
|
|
186 |
}
|
|
|
187 |
}
|