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