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