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