Commit | Line | Data |
---|---|---|
de811c0c | 1 | <?php |
53fad4b9 DM |
2 | |
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
de811c0c DM |
5 | // Moodle is free software: you can redistribute it and/or modify |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
53fad4b9 | 14 | // |
de811c0c DM |
15 | // You should have received a copy of the GNU General Public License |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
53fad4b9 | 17 | |
de811c0c | 18 | /** |
6e309973 | 19 | * Library of internal classes and functions for module workshop |
de811c0c | 20 | * |
53fad4b9 | 21 | * All the workshop specific functions, needed to implement the module |
6e309973 | 22 | * logic, should go to here. Instead of having bunch of function named |
53fad4b9 | 23 | * workshop_something() taking the workshop instance as the first |
a39d7d87 | 24 | * parameter, we use a class workshop that provides all methods. |
53fad4b9 | 25 | * |
de811c0c DM |
26 | * @package mod-workshop |
27 | * @copyright 2009 David Mudrak <david.mudrak@gmail.com> | |
28 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
29 | */ | |
30 | ||
31 | defined('MOODLE_INTERNAL') || die(); | |
32 | ||
6e309973 | 33 | require_once(dirname(__FILE__).'/lib.php'); // we extend this library here |
0968b1a3 | 34 | |
ed597c77 DM |
35 | define('WORKSHOP_ALLOCATION_EXISTS', -1); // return status of {@link add_allocation} |
36 | define('WORKSHOP_ALLOCATION_ERROR', -2); // can be passed to a workshop renderer method | |
6e309973 DM |
37 | |
38 | /** | |
39 | * Full-featured workshop API | |
40 | * | |
a39d7d87 | 41 | * This wraps the workshop database record with a set of methods that are called |
6e309973 | 42 | * from the module itself. The class should be initialized right after you get |
a39d7d87 | 43 | * $workshop, $cm and $course records at the begining of the script. |
6e309973 | 44 | */ |
a39d7d87 DM |
45 | class workshop { |
46 | ||
65ba104c | 47 | /** @var stdClass course module record */ |
a39d7d87 DM |
48 | public $cm = null; |
49 | ||
65ba104c | 50 | /** @var stdClass course record */ |
a39d7d87 | 51 | public $course = null; |
6e309973 DM |
52 | |
53 | /** grading strategy instance */ | |
3fd2b0e1 | 54 | private $strategyinstance = null; |
6e309973 DM |
55 | |
56 | /** | |
65ba104c | 57 | * Initializes the workshop API instance using the data from DB |
a39d7d87 DM |
58 | * |
59 | * Makes deep copy of all passed records properties. Replaces integer $course attribute | |
60 | * with a full database record (course should not be stored in instances table anyway). | |
6e309973 | 61 | * |
06d73dd5 DM |
62 | * @param stdClass $instance Workshop instance data from {workshop} table |
63 | * @param stdClass $cm Course module record as returned by {@link get_coursemodule_from_id()} | |
64 | * @param stdClass $course Course record from {course} table | |
0dc47fb9 | 65 | */ |
06d73dd5 | 66 | public function __construct(stdClass $instance, stdClass $cm, stdClass $course) { |
a39d7d87 DM |
67 | foreach ($instance as $key => $val) { |
68 | if (is_object($val) || (is_array($val))) { | |
69 | // this should not happen if the $instance is really just the record returned by $DB | |
70 | $this->{$key} = unserialize(serialize($val)); // makes deep copy of referenced variables | |
71 | } else { | |
72 | $this->{$key} = $val; | |
73 | } | |
74 | } | |
75 | $this->cm = unserialize(serialize($cm)); | |
76 | $this->course = unserialize(serialize($course)); | |
6e309973 DM |
77 | } |
78 | ||
6e309973 DM |
79 | /** |
80 | * Fetches all users with the capability mod/workshop:submit in the current context | |
81 | * | |
3d2924e9 | 82 | * The returned objects contain id, lastname and firstname properties and are ordered by lastname,firstname |
53fad4b9 DM |
83 | * |
84 | * @param bool $musthavesubmission If true, return only users who have already submitted. All possible authors otherwise. | |
65ba104c | 85 | * @return array array[userid] => stdClass{->id ->lastname ->firstname} |
6e309973 | 86 | */ |
53fad4b9 | 87 | public function get_peer_authors($musthavesubmission=true) { |
66c9894d | 88 | global $DB; |
6e309973 | 89 | |
3d2924e9 DM |
90 | $context = get_context_instance(CONTEXT_MODULE, $this->cm->id); |
91 | $users = get_users_by_capability($context, 'mod/workshop:submit', | |
235b31c8 | 92 | 'u.id, u.lastname, u.firstname', 'u.lastname,u.firstname', '', '', '', '', false, false, true); |
66c9894d | 93 | |
3d2924e9 | 94 | if ($musthavesubmission) { |
a39d7d87 | 95 | $userswithsubmission = array(); |
235b31c8 | 96 | $submissions = $DB->get_records_list('workshop_submissions', 'userid', array_keys($users),'', 'id,userid'); |
b8ead2e6 DM |
97 | foreach ($submissions as $submission) { |
98 | $userswithsubmission[$submission->userid] = null; | |
99 | } | |
66c9894d DM |
100 | $userswithsubmission = array_intersect_key($users, $userswithsubmission); |
101 | } | |
102 | ||
103 | if ($musthavesubmission) { | |
104 | return $userswithsubmission; | |
105 | } else { | |
106 | return $users; | |
107 | } | |
6e309973 DM |
108 | } |
109 | ||
6e309973 DM |
110 | /** |
111 | * Fetches all users with the capability mod/workshop:peerassess in the current context | |
112 | * | |
113 | * Static variable used to cache the results. The returned objects contain id, lastname | |
114 | * and firstname properties and are ordered by lastname,firstname | |
53fad4b9 DM |
115 | * |
116 | * @param bool $musthavesubmission If true, return only users who have already submitted. All possible users otherwise. | |
b8ead2e6 | 117 | * @see get_super_reviewers() |
65ba104c | 118 | * @return array array[userid] => stdClass{->id ->lastname ->firstname} |
6e309973 | 119 | */ |
53fad4b9 | 120 | public function get_peer_reviewers($musthavesubmission=false) { |
6e309973 | 121 | global $DB; |
3d2924e9 DM |
122 | |
123 | $context = get_context_instance(CONTEXT_MODULE, $this->cm->id); | |
124 | $users = get_users_by_capability($context, 'mod/workshop:peerassess', | |
235b31c8 | 125 | 'u.id, u.lastname, u.firstname', 'u.lastname,u.firstname', '', '', '', '', false, false, true); |
3d2924e9 DM |
126 | |
127 | if ($musthavesubmission) { | |
128 | // users without their own submission can not be reviewers | |
235b31c8 | 129 | $submissions = $DB->get_records_list('workshop_submissions', 'userid', array_keys($users),'', 'id,userid'); |
3d2924e9 DM |
130 | foreach ($submissions as $submission) { |
131 | $userswithsubmission[$submission->userid] = null; | |
6e309973 | 132 | } |
3d2924e9 | 133 | $userswithsubmission = array_intersect_key($users, $userswithsubmission); |
0968b1a3 | 134 | } |
3d2924e9 | 135 | |
53fad4b9 DM |
136 | if ($musthavesubmission) { |
137 | return $userswithsubmission; | |
138 | } else { | |
139 | return $users; | |
140 | } | |
0968b1a3 DM |
141 | } |
142 | ||
53fad4b9 | 143 | /** |
b8ead2e6 | 144 | * Fetches all users with the capability mod/workshop:assessallsubmissions in the current context |
53fad4b9 | 145 | * |
b8ead2e6 DM |
146 | * Static variable used to cache the results. The returned objects contain id, lastname |
147 | * and firstname properties and are ordered by lastname,firstname | |
53fad4b9 DM |
148 | * |
149 | * @param bool $musthavesubmission If true, return only users who have already submitted. All possible users otherwise. | |
b8ead2e6 | 150 | * @see get_peer_reviewers() |
65ba104c | 151 | * @return array array[userid] => stdClass{->id ->lastname ->firstname} |
b8ead2e6 DM |
152 | */ |
153 | public function get_super_reviewers() { | |
154 | global $DB; | |
b8ead2e6 | 155 | |
3d2924e9 DM |
156 | $context = get_context_instance(CONTEXT_MODULE, $this->cm->id); |
157 | $users = get_users_by_capability($context, 'mod/workshop:assessallsubmissions', | |
235b31c8 | 158 | 'u.id, u.lastname, u.firstname', 'u.lastname,u.firstname', '', '', '', '', false, false, true); |
3d2924e9 | 159 | |
b8ead2e6 DM |
160 | return $users; |
161 | } | |
162 | ||
163 | /** | |
164 | * Groups the given users by the group membership | |
165 | * | |
166 | * This takes the module grouping settings into account. If "Available for group members only" | |
167 | * is set, returns only groups withing the course module grouping. Always returns group [0] with | |
168 | * all the given users. | |
169 | * | |
65ba104c DM |
170 | * @param array $users array[userid] => stdClass{->id ->lastname ->firstname} |
171 | * @return array array[groupid][userid] => stdClass{->id ->lastname ->firstname} | |
53fad4b9 | 172 | */ |
3d2924e9 | 173 | public function get_grouped($users) { |
53fad4b9 | 174 | global $DB; |
3d2924e9 | 175 | global $CFG; |
53fad4b9 | 176 | |
b8ead2e6 DM |
177 | $grouped = array(); // grouped users to be returned |
178 | if (empty($users)) { | |
179 | return $grouped; | |
a7c5b918 | 180 | } |
3d2924e9 | 181 | if (!empty($CFG->enablegroupings) and $this->cm->groupmembersonly) { |
53fad4b9 DM |
182 | // Available for group members only - the workshop is available only |
183 | // to users assigned to groups within the selected grouping, or to | |
184 | // any group if no grouping is selected. | |
185 | $groupingid = $this->cm->groupingid; | |
b8ead2e6 | 186 | // All users that are members of at least one group will be |
53fad4b9 | 187 | // added into a virtual group id 0 |
b8ead2e6 | 188 | $grouped[0] = array(); |
53fad4b9 DM |
189 | } else { |
190 | $groupingid = 0; | |
b8ead2e6 DM |
191 | // there is no need to be member of a group so $grouped[0] will contain |
192 | // all users | |
193 | $grouped[0] = $users; | |
53fad4b9 | 194 | } |
b8ead2e6 | 195 | $gmemberships = groups_get_all_groups($this->cm->course, array_keys($users), $groupingid, |
53fad4b9 DM |
196 | 'gm.id,gm.groupid,gm.userid'); |
197 | foreach ($gmemberships as $gmembership) { | |
b8ead2e6 DM |
198 | if (!isset($grouped[$gmembership->groupid])) { |
199 | $grouped[$gmembership->groupid] = array(); | |
53fad4b9 | 200 | } |
b8ead2e6 DM |
201 | $grouped[$gmembership->groupid][$gmembership->userid] = $users[$gmembership->userid]; |
202 | $grouped[0][$gmembership->userid] = $users[$gmembership->userid]; | |
53fad4b9 | 203 | } |
b8ead2e6 | 204 | return $grouped; |
53fad4b9 | 205 | } |
6e309973 DM |
206 | |
207 | /** | |
208 | * Returns submissions from this workshop | |
209 | * | |
210 | * Fetches data from {workshop_submissions} and adds some useful information from other | |
211 | * tables. | |
53fad4b9 DM |
212 | * |
213 | * @param mixed $userid int|array|'all' If set to [array of] integer, return submission[s] of the given user[s] only | |
214 | * @param mixed $examples false|true|'all' Only regular submissions, only examples, all submissions | |
65ba104c | 215 | * @return stdClass moodle_recordset |
6e309973 | 216 | */ |
66c9894d | 217 | public function get_submissions_recordset($userid='all', $examples=false) { |
6e309973 DM |
218 | global $DB; |
219 | ||
235b31c8 | 220 | $sql = 'SELECT s.*, u.lastname AS authorlastname, u.firstname AS authorfirstname |
3d2924e9 DM |
221 | FROM {workshop_submissions} s |
222 | INNER JOIN {user} u ON (s.userid = u.id) | |
235b31c8 | 223 | WHERE s.workshopid = :workshopid'; |
3d2924e9 | 224 | $params = array('workshopid' => $this->id); |
6e309973 | 225 | |
6e309973 | 226 | if ($examples === true) { |
235b31c8 | 227 | $sql .= ' AND example = 1'; |
3d2924e9 | 228 | } else { |
235b31c8 | 229 | $sql .= ' AND example = 0'; |
6e309973 | 230 | } |
3d2924e9 DM |
231 | |
232 | if ('all' === $userid) { | |
233 | // no additional conditions | |
234 | } elseif (is_array($userid)) { | |
235 | list($usql, $uparams) = $DB->get_in_or_equal($userid, SQL_PARAMS_NAMED); | |
236 | $sql .= " AND userid $usql"; | |
6e309973 | 237 | $params = array_merge($params, $uparams); |
3d2924e9 | 238 | } else { |
235b31c8 | 239 | $sql .= ' AND userid = :userid'; |
3d2924e9 | 240 | $params['userid'] = $userid; |
6e309973 DM |
241 | } |
242 | ||
243 | return $DB->get_recordset_sql($sql, $params); | |
244 | } | |
245 | ||
53fad4b9 DM |
246 | /** |
247 | * Returns a submission submitted by the given author or authors. | |
248 | * | |
249 | * This is intended for regular workshop participants, not for example submissions by teachers. | |
250 | * If an array of authors is provided, returns array of stripped submission records so they do not | |
251 | * include text fields (to prevent possible memory-lack issues). | |
252 | * | |
253 | * @param mixed $id integer|array author ID or IDs | |
65ba104c | 254 | * @return mixed false if not found, stdClass if $id is int, array if $id is array |
53fad4b9 DM |
255 | */ |
256 | public function get_submission_by_author($id) { | |
257 | if (empty($id)) { | |
258 | return false; | |
259 | } | |
260 | $rs = $this->get_submissions_recordset($id, false); | |
3d2924e9 | 261 | if (is_array($id)) { |
53fad4b9 DM |
262 | $submissions = array(); |
263 | foreach ($rs as $submission) { | |
65ba104c | 264 | $submissions[$submission->id] = new stdClass(); |
53fad4b9 DM |
265 | foreach ($submission as $property => $value) { |
266 | // we do not want text fields here to prevent possible memory issues | |
267 | if (in_array($property, array('id', 'workshopid', 'example', 'userid', 'authorlastname', 'authorfirstname', | |
268 | 'timecreated', 'timemodified', 'grade', 'gradeover', 'gradeoverby', 'gradinggrade'))) { | |
269 | $submissions[$submission->id]->{$property} = $value; | |
270 | } | |
271 | } | |
272 | } | |
273 | return $submissions; | |
274 | } else { | |
3d2924e9 DM |
275 | $submission = $rs->current(); |
276 | $rs->close(); | |
277 | if (empty($submission->id)) { | |
278 | return false; | |
279 | } else { | |
280 | return $submission; | |
281 | } | |
53fad4b9 DM |
282 | } |
283 | } | |
6e309973 DM |
284 | |
285 | /** | |
286 | * Returns the list of assessments with some data added | |
287 | * | |
288 | * Fetches data from {workshop_assessments} and adds some useful information from other | |
289 | * tables. | |
290 | * | |
291 | * @param mixed $reviewerid 'all'|int|array User ID of the reviewer | |
292 | * @param mixed $id 'all'|int Assessment ID | |
65ba104c | 293 | * @return stdClass moodle_recordset |
6e309973 | 294 | */ |
66c9894d | 295 | public function get_assessments_recordset($reviewerid='all', $id='all') { |
6e309973 | 296 | global $DB; |
53fad4b9 | 297 | |
235b31c8 | 298 | $sql = 'SELECT a.*, |
3d2924e9 DM |
299 | reviewer.id AS reviewerid,reviewer.firstname AS reviewerfirstname,reviewer.lastname as reviewerlastname, |
300 | s.title, | |
301 | author.id AS authorid, author.firstname AS authorfirstname,author.lastname as authorlastname | |
302 | FROM {workshop_assessments} a | |
303 | INNER JOIN {user} reviewer ON (a.userid = reviewer.id) | |
304 | INNER JOIN {workshop_submissions} s ON (a.submissionid = s.id) | |
305 | INNER JOIN {user} author ON (s.userid = author.id) | |
235b31c8 | 306 | WHERE s.workshopid = :workshopid'; |
3d2924e9 DM |
307 | $params = array('workshopid' => $this->id); |
308 | ||
309 | if ('all' === $reviewerid) { | |
310 | // no additional conditions | |
311 | } elseif (is_array($reviewerid)) { | |
312 | list($usql, $uparams) = $DB->get_in_or_equal($reviewerid, SQL_PARAMS_NAMED); | |
313 | $sql .= " AND reviewer.id $usql"; | |
6e309973 | 314 | $params = array_merge($params, $uparams); |
3d2924e9 | 315 | } else { |
235b31c8 | 316 | $sql .= ' AND reviewer.id = :reviewerid'; |
3d2924e9 | 317 | $params['reviewerid'] = $reviewerid; |
6e309973 | 318 | } |
3d2924e9 DM |
319 | |
320 | if ('all' === $id) { | |
321 | // no additional conditions | |
322 | } else { | |
235b31c8 | 323 | $sql .= ' AND a.id = :assessmentid'; |
3d2924e9 | 324 | $params['assessmentid'] = $id; |
6e309973 DM |
325 | } |
326 | ||
327 | return $DB->get_recordset_sql($sql, $params); | |
328 | } | |
329 | ||
53fad4b9 DM |
330 | /** |
331 | * Returns the list of assessments with some data added | |
332 | * | |
333 | * Fetches data from {workshop_assessments} and adds some useful information from other | |
334 | * tables. The returned objects are lightweight version of those returned by get_assessments_recordset(), | |
335 | * mainly they do not contain text fields. | |
336 | * | |
337 | * @param mixed $reviewerid 'all'|int|array User ID of the reviewer | |
b8ead2e6 | 338 | * @param mixed $id 'all'|int Assessment ID |
65ba104c | 339 | * @return array [assessmentid] => assessment stdClass |
a39d7d87 | 340 | * @see workshop::get_assessments_recordset() for the structure of returned objects |
53fad4b9 | 341 | */ |
b8ead2e6 DM |
342 | public function get_assessments($reviewerid='all', $id='all') { |
343 | $rs = $this->get_assessments_recordset($reviewerid, $id); | |
53fad4b9 DM |
344 | $assessments = array(); |
345 | foreach ($rs as $assessment) { | |
346 | // copy selected properties into the array to be returned. This is here mainly in order not | |
347 | // to include text comments. | |
65ba104c | 348 | $assessments[$assessment->id] = new stdClass(); |
53fad4b9 DM |
349 | foreach ($assessment as $property => $value) { |
350 | if (in_array($property, array('id', 'submissionid', 'userid', 'timecreated', 'timemodified', | |
351 | 'timeagreed', 'grade', 'gradinggrade', 'gradinggradeover', 'gradinggradeoverby', | |
352 | 'reviewerid', 'reviewerfirstname', 'reviewerlastname', 'title', 'authorid', | |
353 | 'authorfirstname', 'authorlastname'))) { | |
354 | $assessments[$assessment->id]->{$property} = $value; | |
355 | } | |
356 | } | |
357 | } | |
358 | $rs->close(); | |
359 | return $assessments; | |
360 | } | |
361 | ||
362 | /** | |
363 | * Get the information about the given assessment | |
364 | * | |
365 | * @param int $id Assessment ID | |
a39d7d87 | 366 | * @see workshop::get_assessments_recordset() for the structure of data returned |
65ba104c | 367 | * @return mixed false if not found, stdClass otherwise |
53fad4b9 DM |
368 | */ |
369 | public function get_assessment_by_id($id) { | |
370 | $rs = $this->get_assessments_recordset('all', $id); | |
371 | $assessment = $rs->current(); | |
372 | $rs->close(); | |
373 | if (empty($assessment->id)) { | |
374 | return false; | |
375 | } else { | |
376 | return $assessment; | |
377 | } | |
378 | } | |
379 | ||
380 | /** | |
381 | * Get the information about all assessments assigned to the given reviewer | |
382 | * | |
383 | * @param int $id Reviewer ID | |
a39d7d87 | 384 | * @see workshop::get_assessments_recordset() for the structure of data returned |
53fad4b9 DM |
385 | * @return array array of objects |
386 | */ | |
387 | public function get_assessments_by_reviewer($id) { | |
388 | $rs = $this->get_assessments_recordset($id); | |
389 | $assessments = array(); | |
390 | foreach ($rs as $assessment) { | |
391 | $assessments[$assessment->id] = $assessment; | |
392 | } | |
393 | $rs->close(); | |
394 | return $assessment; | |
395 | } | |
6e309973 DM |
396 | |
397 | /** | |
398 | * Returns the list of allocations in the workshop | |
399 | * | |
400 | * This returns the list of all users who can submit their work or review submissions (or both | |
401 | * which is the common case). So basically this is to return list of all students participating | |
402 | * in the workshop. For every participant, it adds information about their submission and their | |
53fad4b9 | 403 | * reviews. |
6e309973 DM |
404 | * |
405 | * The returned structure is recordset of objects with following properties: | |
406 | * [authorid] [authorfirstname] [authorlastname] [authorpicture] [authorimagealt] | |
407 | * [submissionid] [submissiontitle] [submissiongrade] [assessmentid] | |
53fad4b9 | 408 | * [timeallocated] [reviewerid] [reviewerfirstname] [reviewerlastname] |
6e309973 DM |
409 | * [reviewerpicture] [reviewerimagealt] |
410 | * | |
3d2924e9 | 411 | * TODO This should be refactored when capability handling proposed by Petr is implemented so that |
6e309973 | 412 | * we can check capabilities directly in SQL joins. |
53fad4b9 DM |
413 | * Note that the returned recordset includes participants without submission as well as those |
414 | * without any review allocated yet. | |
6e309973 | 415 | * |
65ba104c | 416 | * @return stdClass moodle_recordset |
6e309973 | 417 | */ |
66c9894d | 418 | public function get_allocations_recordset() { |
6e309973 | 419 | global $DB; |
6e309973 | 420 | |
3d2924e9 DM |
421 | $context = get_context_instance(CONTEXT_MODULE, $this->cm->id); |
422 | $users = get_users_by_capability($context, array('mod/workshop:submit', 'mod/workshop:peerassess'), | |
235b31c8 | 423 | 'u.id', 'u.lastname,u.firstname', '', '', '', '', false, false, true); |
3d2924e9 DM |
424 | |
425 | list($usql, $params) = $DB->get_in_or_equal(array_keys($users), SQL_PARAMS_NAMED); | |
426 | $params['workshopid'] = $this->id; | |
427 | ||
1dbbccb7 | 428 | $sql = "SELECT author.id AS authorid, author.firstname AS authorfirstname, author.lastname AS authorlastname, |
3d2924e9 DM |
429 | author.picture AS authorpicture, author.imagealt AS authorimagealt, |
430 | s.id AS submissionid, s.title AS submissiontitle, s.grade AS submissiongrade, | |
431 | a.id AS assessmentid, a.timecreated AS timeallocated, a.userid AS reviewerid, | |
432 | reviewer.firstname AS reviewerfirstname, reviewer.lastname AS reviewerlastname, | |
433 | reviewer.picture as reviewerpicture, reviewer.imagealt AS reviewerimagealt | |
434 | FROM {user} author | |
435 | LEFT JOIN {workshop_submissions} s ON (s.userid = author.id) | |
436 | LEFT JOIN {workshop_assessments} a ON (s.id = a.submissionid) | |
437 | LEFT JOIN {user} reviewer ON (a.userid = reviewer.id) | |
438 | WHERE author.id $usql AND s.workshopid = :workshopid | |
1dbbccb7 | 439 | ORDER BY author.lastname,author.firstname,reviewer.lastname,reviewer.firstname"; |
3d2924e9 | 440 | |
6e309973 DM |
441 | return $DB->get_recordset_sql($sql, $params); |
442 | } | |
443 | ||
6e309973 DM |
444 | /** |
445 | * Allocate a submission to a user for review | |
53fad4b9 | 446 | * |
65ba104c | 447 | * @param stdClass $submission Submission record |
6e309973 | 448 | * @param int $reviewerid User ID |
53fad4b9 | 449 | * @param bool $bulk repeated inserts into DB expected |
6e309973 DM |
450 | * @return int ID of the new assessment or an error code |
451 | */ | |
65ba104c | 452 | public function add_allocation(stdClass $submission, $reviewerid, $bulk=false) { |
6e309973 DM |
453 | global $DB; |
454 | ||
455 | if ($DB->record_exists('workshop_assessments', array('submissionid' => $submission->id, 'userid' => $reviewerid))) { | |
456 | return WORKSHOP_ALLOCATION_EXISTS; | |
457 | } | |
458 | ||
6e309973 | 459 | $now = time(); |
65ba104c | 460 | $assessment = new stdClass(); |
53fad4b9 | 461 | $assessment->submissionid = $submission->id; |
6e309973 DM |
462 | $assessment->userid = $reviewerid; |
463 | $assessment->timecreated = $now; | |
464 | $assessment->timemodified = $now; | |
465 | ||
235b31c8 | 466 | return $DB->insert_record('workshop_assessments', $assessment, true, $bulk); |
6e309973 DM |
467 | } |
468 | ||
6e309973 | 469 | /** |
53fad4b9 | 470 | * Delete assessment record or records |
6e309973 | 471 | * |
53fad4b9 DM |
472 | * @param mixed $id int|array assessment id or array of assessments ids |
473 | * @return bool false if $id not a valid parameter, true otherwise | |
6e309973 DM |
474 | */ |
475 | public function delete_assessment($id) { | |
476 | global $DB; | |
477 | ||
478 | // todo remove all given grades from workshop_grades; | |
6e309973 | 479 | |
53fad4b9 | 480 | if (is_array($id)) { |
235b31c8 | 481 | return $DB->delete_records_list('workshop_assessments', 'id', $id); |
3d2924e9 | 482 | } else { |
235b31c8 | 483 | return $DB->delete_records('workshop_assessments', array('id' => $id)); |
53fad4b9 | 484 | } |
53fad4b9 | 485 | } |
6e309973 DM |
486 | |
487 | /** | |
488 | * Returns instance of grading strategy class | |
53fad4b9 | 489 | * |
65ba104c | 490 | * @return stdClass Instance of a grading strategy |
6e309973 DM |
491 | */ |
492 | public function grading_strategy_instance() { | |
3d2924e9 DM |
493 | global $CFG; // because we require other libs here |
494 | ||
3fd2b0e1 | 495 | if (is_null($this->strategyinstance)) { |
0dc47fb9 | 496 | $strategylib = dirname(__FILE__) . '/grading/' . $this->strategy . '/strategy.php'; |
6e309973 DM |
497 | if (is_readable($strategylib)) { |
498 | require_once($strategylib); | |
499 | } else { | |
a39d7d87 | 500 | throw new moodle_exception('missingstrategy', 'workshop'); |
6e309973 | 501 | } |
0dc47fb9 | 502 | $classname = 'workshop_' . $this->strategy . '_strategy'; |
3fd2b0e1 DM |
503 | $this->strategyinstance = new $classname($this); |
504 | if (!in_array('workshop_strategy', class_implements($this->strategyinstance))) { | |
a39d7d87 | 505 | throw new moodle_exception('strategynotimplemented', 'workshop'); |
6e309973 DM |
506 | } |
507 | } | |
3fd2b0e1 | 508 | return $this->strategyinstance; |
6e309973 DM |
509 | } |
510 | ||
66c9894d DM |
511 | /** |
512 | * Return list of available allocation methods | |
513 | * | |
514 | * @return array Array ['string' => 'string'] of localized allocation method names | |
515 | */ | |
516 | public function installed_allocators() { | |
ed597c77 | 517 | $installed = get_plugin_list('workshopallocation'); |
66c9894d | 518 | $forms = array(); |
ed597c77 DM |
519 | foreach ($installed as $allocation => $allocationpath) { |
520 | if (file_exists($allocationpath . '/allocator.php')) { | |
521 | $forms[$allocation] = get_string('pluginname', 'workshopallocation_' . $allocation); | |
522 | } | |
66c9894d DM |
523 | } |
524 | // usability - make sure that manual allocation appears the first | |
525 | if (isset($forms['manual'])) { | |
526 | $m = array('manual' => $forms['manual']); | |
527 | unset($forms['manual']); | |
528 | $forms = array_merge($m, $forms); | |
529 | } | |
530 | return $forms; | |
531 | } | |
0968b1a3 | 532 | |
66c9894d DM |
533 | /** |
534 | * Returns instance of submissions allocator | |
53fad4b9 | 535 | * |
65ba104c DM |
536 | * @param stdClass $method The name of the allocation method, must be PARAM_ALPHA |
537 | * @return stdClass Instance of submissions allocator | |
66c9894d DM |
538 | */ |
539 | public function allocator_instance($method) { | |
3d2924e9 DM |
540 | global $CFG; // because we require other libs here |
541 | ||
66c9894d DM |
542 | $allocationlib = dirname(__FILE__) . '/allocation/' . $method . '/allocator.php'; |
543 | if (is_readable($allocationlib)) { | |
544 | require_once($allocationlib); | |
545 | } else { | |
a39d7d87 | 546 | throw new coding_exception('Unable to find allocator.php'); |
66c9894d DM |
547 | } |
548 | $classname = 'workshop_' . $method . '_allocator'; | |
549 | return new $classname($this); | |
550 | } | |
551 | ||
b8ead2e6 | 552 | /** |
65ba104c | 553 | * @return stdClass {@link moodle_url} the URL of this workshop's view page |
b8ead2e6 DM |
554 | */ |
555 | public function view_url() { | |
556 | global $CFG; | |
557 | return new moodle_url($CFG->wwwroot . '/mod/workshop/view.php', array('id' => $this->cm->id)); | |
558 | } | |
559 | ||
560 | /** | |
65ba104c | 561 | * @return stdClass {@link moodle_url} the URL of the page for editing this workshop's grading form |
b8ead2e6 DM |
562 | */ |
563 | public function editform_url() { | |
564 | global $CFG; | |
565 | return new moodle_url($CFG->wwwroot . '/mod/workshop/editform.php', array('cmid' => $this->cm->id)); | |
566 | } | |
567 | ||
568 | /** | |
65ba104c | 569 | * @return stdClass {@link moodle_url} the URL of the page for previewing this workshop's grading form |
b8ead2e6 DM |
570 | */ |
571 | public function previewform_url() { | |
572 | global $CFG; | |
573 | return new moodle_url($CFG->wwwroot . '/mod/workshop/assessment.php', array('preview' => $this->cm->id)); | |
574 | } | |
575 | ||
576 | /** | |
577 | * @param int $assessmentid The ID of assessment record | |
65ba104c | 578 | * @return stdClass {@link moodle_url} the URL of the assessment page |
b8ead2e6 | 579 | */ |
a39d7d87 | 580 | public function assess_url($assessmentid) { |
b8ead2e6 | 581 | global $CFG; |
a39d7d87 | 582 | return new moodle_url($CFG->wwwroot . '/mod/workshop/assessment.php', array('asid' => $assessmentid)); |
b8ead2e6 DM |
583 | } |
584 | ||
39861053 DM |
585 | /** |
586 | * @return stdClass {@link moodle_url} the URL of the page to view own submission | |
587 | */ | |
588 | public function submission_url() { | |
589 | global $CFG; | |
590 | return new moodle_url($CFG->wwwroot . '/mod/workshop/submission.php', array('cmid' => $this->cm->id)); | |
591 | } | |
592 | ||
b8ead2e6 DM |
593 | /** |
594 | * Returns an object containing all data to display the user's full name and picture | |
595 | * | |
596 | * @param int $id optional user id, defaults to the current user | |
65ba104c | 597 | * @return stdClass containing properties lastname, firstname, picture and imagealt |
b8ead2e6 DM |
598 | */ |
599 | public function user_info($id=null) { | |
600 | global $USER, $DB; | |
601 | ||
602 | if (is_null($id) || ($id == $USER->id)) { | |
603 | return $USER; | |
604 | } else { | |
605 | return $DB->get_record('user', array('id' => $id), 'id,lastname,firstname,picture,imagealt', MUST_EXIST); | |
606 | } | |
607 | } | |
608 | ||
c1e883bb DM |
609 | /** |
610 | * Are users alloed to create/edit their submissions? | |
611 | * | |
612 | * TODO: this depends on the workshop phase, phase deadlines, submitting after deadlines possibility | |
613 | * | |
614 | * @return bool | |
615 | */ | |
616 | public function submitting_allowed() { | |
617 | return true; | |
618 | } | |
619 | ||
66c9894d | 620 | } |