Commit | Line | Data |
---|---|---|
cf72e2dd | 1 | <?php |
cf72e2dd | 2 | // This file is part of Moodle - http://moodle.org/ |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
cf72e2dd | 17 | /** |
18 | * Functions used by gradebook plugins and reports. | |
19 | * | |
a153c9f2 | 20 | * @package core_grades |
cf72e2dd | 21 | * @copyright 2009 Petr Skoda and Nicolas Connault |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
8ad36f4c | 24 | |
63d2081e MN |
25 | require_once($CFG->libdir . '/gradelib.php'); |
26 | require_once($CFG->dirroot . '/grade/export/lib.php'); | |
7a6b7acf | 27 | |
0f5660f7 | 28 | /** |
29 | * This class iterates over all users that are graded in a course. | |
b9f49659 | 30 | * Returns detailed info about users and their grades. |
cf72e2dd | 31 | * |
32 | * @author Petr Skoda <skodak@moodle.org> | |
33 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
0f5660f7 | 34 | */ |
35 | class graded_users_iterator { | |
728ff21b AD |
36 | |
37 | /** | |
38 | * The couse whose users we are interested in | |
39 | */ | |
40 | protected $course; | |
41 | ||
42 | /** | |
43 | * An array of grade items or null if only user data was requested | |
44 | */ | |
45 | protected $grade_items; | |
46 | ||
47 | /** | |
48 | * The group ID we are interested in. 0 means all groups. | |
49 | */ | |
50 | protected $groupid; | |
51 | ||
52 | /** | |
53 | * A recordset of graded users | |
54 | */ | |
55 | protected $users_rs; | |
56 | ||
57 | /** | |
58 | * A recordset of user grades (grade_grade instances) | |
59 | */ | |
60 | protected $grades_rs; | |
61 | ||
62 | /** | |
63 | * Array used when moving to next user while iterating through the grades recordset | |
64 | */ | |
65 | protected $gradestack; | |
66 | ||
67 | /** | |
68 | * The first field of the users table by which the array of users will be sorted | |
69 | */ | |
70 | protected $sortfield1; | |
71 | ||
72 | /** | |
73 | * Should sortfield1 be ASC or DESC | |
74 | */ | |
75 | protected $sortorder1; | |
76 | ||
77 | /** | |
78 | * The second field of the users table by which the array of users will be sorted | |
79 | */ | |
80 | protected $sortfield2; | |
81 | ||
82 | /** | |
83 | * Should sortfield2 be ASC or DESC | |
84 | */ | |
85 | protected $sortorder2; | |
0f5660f7 | 86 | |
78ab98bc AD |
87 | /** |
88 | * Should users whose enrolment has been suspended be ignored? | |
89 | */ | |
90 | protected $onlyactive = false; | |
91 | ||
61c8e0d7 FM |
92 | /** |
93 | * Enable user custom fields | |
94 | */ | |
95 | protected $allowusercustomfields = false; | |
96 | ||
38c1dd19 RT |
97 | /** |
98 | * List of suspended users in course. This includes users whose enrolment status is suspended | |
99 | * or enrolment has expired or not started. | |
100 | */ | |
101 | protected $suspendedusers = array(); | |
102 | ||
0f5660f7 | 103 | /** |
104 | * Constructor | |
cf72e2dd | 105 | * |
106 | * @param object $course A course object | |
107 | * @param array $grade_items array of grade items, if not specified only user info returned | |
108 | * @param int $groupid iterate only group users if present | |
d08bba83 | 109 | * @param string $sortfield1 The first field of the users table by which the array of users will be sorted |
110 | * @param string $sortorder1 The order in which the first sorting field will be sorted (ASC or DESC) | |
111 | * @param string $sortfield2 The second field of the users table by which the array of users will be sorted | |
112 | * @param string $sortorder2 The order in which the second sorting field will be sorted (ASC or DESC) | |
0f5660f7 | 113 | */ |
728ff21b | 114 | public function __construct($course, $grade_items=null, $groupid=0, |
cf72e2dd | 115 | $sortfield1='lastname', $sortorder1='ASC', |
116 | $sortfield2='firstname', $sortorder2='ASC') { | |
0f5660f7 | 117 | $this->course = $course; |
118 | $this->grade_items = $grade_items; | |
119 | $this->groupid = $groupid; | |
d08bba83 | 120 | $this->sortfield1 = $sortfield1; |
121 | $this->sortorder1 = $sortorder1; | |
122 | $this->sortfield2 = $sortfield2; | |
123 | $this->sortorder2 = $sortorder2; | |
0f5660f7 | 124 | |
125 | $this->gradestack = array(); | |
126 | } | |
127 | ||
128 | /** | |
129 | * Initialise the iterator | |
728ff21b | 130 | * |
0f5660f7 | 131 | * @return boolean success |
132 | */ | |
d24832f9 | 133 | public function init() { |
134 | global $CFG, $DB; | |
0f5660f7 | 135 | |
136 | $this->close(); | |
137 | ||
63d2081e | 138 | export_verify_grades($this->course->id); |
0f5660f7 | 139 | $course_item = grade_item::fetch_course_item($this->course->id); |
140 | if ($course_item->needsupdate) { | |
4e829d48 | 141 | // Can not calculate all final grades - sorry. |
0f5660f7 | 142 | return false; |
143 | } | |
144 | ||
d4060472 | 145 | $coursecontext = context_course::instance($this->course->id); |
656d17c2 | 146 | |
4e829d48 MN |
147 | list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx'); |
148 | list($gradebookroles_sql, $params) = $DB->get_in_or_equal(explode(',', $CFG->gradebookroles), SQL_PARAMS_NAMED, 'grbr'); | |
78ab98bc | 149 | list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, '', 0, $this->onlyactive); |
656d17c2 | 150 | |
4e829d48 | 151 | $params = array_merge($params, $enrolledparams, $relatedctxparams); |
0f5660f7 | 152 | |
153 | if ($this->groupid) { | |
d24832f9 | 154 | $groupsql = "INNER JOIN {groups_members} gm ON gm.userid = u.id"; |
e58fcb35 | 155 | $groupwheresql = "AND gm.groupid = :groupid"; |
d24832f9 | 156 | // $params contents: gradebookroles |
e58fcb35 | 157 | $params['groupid'] = $this->groupid; |
0f5660f7 | 158 | } else { |
159 | $groupsql = ""; | |
160 | $groupwheresql = ""; | |
161 | } | |
162 | ||
345674ca | 163 | if (empty($this->sortfield1)) { |
4e829d48 | 164 | // We must do some sorting even if not specified. |
345674ca | 165 | $ofields = ", u.id AS usrt"; |
166 | $order = "usrt ASC"; | |
167 | ||
168 | } else { | |
169 | $ofields = ", u.$this->sortfield1 AS usrt1"; | |
170 | $order = "usrt1 $this->sortorder1"; | |
171 | if (!empty($this->sortfield2)) { | |
0febb12d | 172 | $ofields .= ", u.$this->sortfield2 AS usrt2"; |
345674ca | 173 | $order .= ", usrt2 $this->sortorder2"; |
174 | } | |
175 | if ($this->sortfield1 != 'id' and $this->sortfield2 != 'id') { | |
4e829d48 MN |
176 | // User order MUST be the same in both queries, |
177 | // must include the only unique user->id if not already present. | |
345674ca | 178 | $ofields .= ", u.id AS usrt"; |
179 | $order .= ", usrt ASC"; | |
180 | } | |
181 | } | |
182 | ||
293f42b6 CF |
183 | $userfields = 'u.*'; |
184 | $customfieldssql = ''; | |
61c8e0d7 FM |
185 | if ($this->allowusercustomfields && !empty($CFG->grade_export_customprofilefields)) { |
186 | $customfieldscount = 0; | |
187 | $customfieldsarray = grade_helper::get_user_profile_fields($this->course->id, $this->allowusercustomfields); | |
188 | foreach ($customfieldsarray as $field) { | |
189 | if (!empty($field->customid)) { | |
293f42b6 | 190 | $customfieldssql .= " |
61c8e0d7 FM |
191 | LEFT JOIN (SELECT * FROM {user_info_data} |
192 | WHERE fieldid = :cf$customfieldscount) cf$customfieldscount | |
193 | ON u.id = cf$customfieldscount.userid"; | |
c1735baf | 194 | $userfields .= ", cf$customfieldscount.data AS customfield_{$field->shortname}"; |
61c8e0d7 FM |
195 | $params['cf'.$customfieldscount] = $field->customid; |
196 | $customfieldscount++; | |
293f42b6 CF |
197 | } |
198 | } | |
199 | } | |
200 | ||
293f42b6 | 201 | $users_sql = "SELECT $userfields $ofields |
d24832f9 | 202 | FROM {user} u |
656d17c2 | 203 | JOIN ($enrolledsql) je ON je.id = u.id |
293f42b6 | 204 | $groupsql $customfieldssql |
656d17c2 AD |
205 | JOIN ( |
206 | SELECT DISTINCT ra.userid | |
207 | FROM {role_assignments} ra | |
208 | WHERE ra.roleid $gradebookroles_sql | |
4e829d48 | 209 | AND ra.contextid $relatedctxsql |
656d17c2 AD |
210 | ) rainner ON rainner.userid = u.id |
211 | WHERE u.deleted = 0 | |
345674ca | 212 | $groupwheresql |
213 | ORDER BY $order"; | |
d24832f9 | 214 | $this->users_rs = $DB->get_recordset_sql($users_sql, $params); |
0f5660f7 | 215 | |
38c1dd19 RT |
216 | if (!$this->onlyactive) { |
217 | $context = context_course::instance($this->course->id); | |
218 | $this->suspendedusers = get_suspended_userids($context); | |
219 | } else { | |
220 | $this->suspendedusers = array(); | |
221 | } | |
222 | ||
0f5660f7 | 223 | if (!empty($this->grade_items)) { |
224 | $itemids = array_keys($this->grade_items); | |
cf717dc2 | 225 | list($itemidsql, $grades_params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED, 'items'); |
d24832f9 | 226 | $params = array_merge($params, $grades_params); |
0f5660f7 | 227 | |
345674ca | 228 | $grades_sql = "SELECT g.* $ofields |
d24832f9 | 229 | FROM {grade_grades} g |
656d17c2 AD |
230 | JOIN {user} u ON g.userid = u.id |
231 | JOIN ($enrolledsql) je ON je.id = u.id | |
0f5660f7 | 232 | $groupsql |
656d17c2 AD |
233 | JOIN ( |
234 | SELECT DISTINCT ra.userid | |
235 | FROM {role_assignments} ra | |
236 | WHERE ra.roleid $gradebookroles_sql | |
4e829d48 | 237 | AND ra.contextid $relatedctxsql |
4f82032a | 238 | ) rainner ON rainner.userid = u.id |
656d17c2 AD |
239 | WHERE u.deleted = 0 |
240 | AND g.itemid $itemidsql | |
241 | $groupwheresql | |
345674ca | 242 | ORDER BY $order, g.itemid ASC"; |
d24832f9 | 243 | $this->grades_rs = $DB->get_recordset_sql($grades_sql, $params); |
345674ca | 244 | } else { |
245 | $this->grades_rs = false; | |
0f5660f7 | 246 | } |
345674ca | 247 | |
0f5660f7 | 248 | return true; |
249 | } | |
250 | ||
251 | /** | |
252 | * Returns information about the next user | |
253 | * @return mixed array of user info, all grades and feedback or null when no more users found | |
254 | */ | |
728ff21b | 255 | public function next_user() { |
03cedd62 | 256 | if (!$this->users_rs) { |
0f5660f7 | 257 | return false; // no users present |
258 | } | |
259 | ||
5c75a0a3 | 260 | if (!$this->users_rs->valid()) { |
345674ca | 261 | if ($current = $this->_pop()) { |
262 | // this is not good - user or grades updated between the two reads above :-( | |
263 | } | |
264 | ||
0f5660f7 | 265 | return false; // no more users |
5c75a0a3 | 266 | } else { |
267 | $user = $this->users_rs->current(); | |
268 | $this->users_rs->next(); | |
0f5660f7 | 269 | } |
270 | ||
345674ca | 271 | // find grades of this user |
0f5660f7 | 272 | $grade_records = array(); |
273 | while (true) { | |
274 | if (!$current = $this->_pop()) { | |
275 | break; // no more grades | |
276 | } | |
277 | ||
5c75a0a3 | 278 | if (empty($current->userid)) { |
279 | break; | |
280 | } | |
281 | ||
345674ca | 282 | if ($current->userid != $user->id) { |
283 | // grade of the next user, we have all for this user | |
0f5660f7 | 284 | $this->_push($current); |
285 | break; | |
286 | } | |
287 | ||
288 | $grade_records[$current->itemid] = $current; | |
289 | } | |
290 | ||
291 | $grades = array(); | |
292 | $feedbacks = array(); | |
293 | ||
d08bba83 | 294 | if (!empty($this->grade_items)) { |
345674ca | 295 | foreach ($this->grade_items as $grade_item) { |
6189c7b4 FM |
296 | if (!isset($feedbacks[$grade_item->id])) { |
297 | $feedbacks[$grade_item->id] = new stdClass(); | |
298 | } | |
345674ca | 299 | if (array_key_exists($grade_item->id, $grade_records)) { |
300 | $feedbacks[$grade_item->id]->feedback = $grade_records[$grade_item->id]->feedback; | |
301 | $feedbacks[$grade_item->id]->feedbackformat = $grade_records[$grade_item->id]->feedbackformat; | |
302 | unset($grade_records[$grade_item->id]->feedback); | |
303 | unset($grade_records[$grade_item->id]->feedbackformat); | |
304 | $grades[$grade_item->id] = new grade_grade($grade_records[$grade_item->id], false); | |
305 | } else { | |
306 | $feedbacks[$grade_item->id]->feedback = ''; | |
307 | $feedbacks[$grade_item->id]->feedbackformat = FORMAT_MOODLE; | |
cf72e2dd | 308 | $grades[$grade_item->id] = |
309 | new grade_grade(array('userid'=>$user->id, 'itemid'=>$grade_item->id), false); | |
345674ca | 310 | } |
0f5660f7 | 311 | } |
312 | } | |
313 | ||
38c1dd19 RT |
314 | // Set user suspended status. |
315 | $user->suspendedenrolment = isset($this->suspendedusers[$user->id]); | |
ace9051c | 316 | $result = new stdClass(); |
0f5660f7 | 317 | $result->user = $user; |
318 | $result->grades = $grades; | |
319 | $result->feedbacks = $feedbacks; | |
0f5660f7 | 320 | return $result; |
321 | } | |
322 | ||
323 | /** | |
728ff21b | 324 | * Close the iterator, do not forget to call this function |
0f5660f7 | 325 | */ |
728ff21b | 326 | public function close() { |
caffc55a | 327 | if ($this->users_rs) { |
d24832f9 | 328 | $this->users_rs->close(); |
caffc55a | 329 | $this->users_rs = null; |
0f5660f7 | 330 | } |
caffc55a | 331 | if ($this->grades_rs) { |
d24832f9 | 332 | $this->grades_rs->close(); |
caffc55a | 333 | $this->grades_rs = null; |
0f5660f7 | 334 | } |
335 | $this->gradestack = array(); | |
336 | } | |
337 | ||
78ab98bc AD |
338 | /** |
339 | * Should all enrolled users be exported or just those with an active enrolment? | |
340 | * | |
341 | * @param bool $onlyactive True to limit the export to users with an active enrolment | |
342 | */ | |
343 | public function require_active_enrolment($onlyactive = true) { | |
344 | if (!empty($this->users_rs)) { | |
345 | debugging('Calling require_active_enrolment() has no effect unless you call init() again', DEBUG_DEVELOPER); | |
346 | } | |
347 | $this->onlyactive = $onlyactive; | |
348 | } | |
349 | ||
61c8e0d7 FM |
350 | /** |
351 | * Allow custom fields to be included | |
352 | * | |
353 | * @param bool $allow Whether to allow custom fields or not | |
354 | * @return void | |
355 | */ | |
356 | public function allow_user_custom_fields($allow = true) { | |
357 | if ($allow) { | |
358 | $this->allowusercustomfields = true; | |
359 | } else { | |
360 | $this->allowusercustomfields = false; | |
361 | } | |
362 | } | |
cf72e2dd | 363 | |
0f5660f7 | 364 | /** |
728ff21b | 365 | * Add a grade_grade instance to the grade stack |
cf72e2dd | 366 | * |
367 | * @param grade_grade $grade Grade object | |
368 | * | |
369 | * @return void | |
0f5660f7 | 370 | */ |
728ff21b | 371 | private function _push($grade) { |
0f5660f7 | 372 | array_push($this->gradestack, $grade); |
373 | } | |
374 | ||
cf72e2dd | 375 | |
0f5660f7 | 376 | /** |
728ff21b | 377 | * Remove a grade_grade instance from the grade stack |
cf72e2dd | 378 | * |
728ff21b | 379 | * @return grade_grade current grade object |
0f5660f7 | 380 | */ |
728ff21b | 381 | private function _pop() { |
d24832f9 | 382 | global $DB; |
0f5660f7 | 383 | if (empty($this->gradestack)) { |
8de7c8a7 | 384 | if (empty($this->grades_rs) || !$this->grades_rs->valid()) { |
cf72e2dd | 385 | return null; // no grades present |
0f5660f7 | 386 | } |
387 | ||
7ce5df86 AD |
388 | $current = $this->grades_rs->current(); |
389 | ||
390 | $this->grades_rs->next(); | |
0f5660f7 | 391 | |
7ce5df86 | 392 | return $current; |
0f5660f7 | 393 | } else { |
394 | return array_pop($this->gradestack); | |
395 | } | |
396 | } | |
397 | } | |
398 | ||
d08bba83 | 399 | /** |
400 | * Print a selection popup form of the graded users in a course. | |
401 | * | |
4d5059d4 SH |
402 | * @deprecated since 2.0 |
403 | * | |
cf72e2dd | 404 | * @param int $course id of the course |
d08bba83 | 405 | * @param string $actionpage The page receiving the data from the popoup form |
cf72e2dd | 406 | * @param int $userid id of the currently selected user (or 'all' if they are all selected) |
407 | * @param int $groupid id of requested group, 0 means all | |
408 | * @param int $includeall bool include all option | |
409 | * @param bool $return If true, will return the HTML, otherwise, will print directly | |
d08bba83 | 410 | * @return null |
411 | */ | |
7ac88172 | 412 | function print_graded_users_selector($course, $actionpage, $userid=0, $groupid=0, $includeall=true, $return=false) { |
714b4745 | 413 | global $CFG, $USER, $OUTPUT; |
4d5059d4 SH |
414 | return $OUTPUT->render(grade_get_graded_users_select(substr($actionpage, 0, strpos($actionpage, '/')), $course, $userid, $groupid, $includeall)); |
415 | } | |
345674ca | 416 | |
4d5059d4 | 417 | function grade_get_graded_users_select($report, $course, $userid, $groupid, $includeall) { |
4e829d48 | 418 | global $USER, $CFG; |
f9510667 | 419 | |
879c99bb | 420 | if (is_null($userid)) { |
421 | $userid = $USER->id; | |
422 | } | |
38c1dd19 RT |
423 | $coursecontext = context_course::instance($course->id); |
424 | $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol); | |
425 | $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol); | |
426 | $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $coursecontext); | |
d08bba83 | 427 | $menu = array(); // Will be a list of userid => user name |
38c1dd19 | 428 | $menususpendedusers = array(); // Suspended users go to a separate optgroup. |
772229f3 | 429 | $gui = new graded_users_iterator($course, null, $groupid); |
38c1dd19 | 430 | $gui->require_active_enrolment($showonlyactiveenrol); |
d08bba83 | 431 | $gui->init(); |
10f5c046 | 432 | $label = get_string('selectauser', 'grades'); |
7ac88172 | 433 | if ($includeall) { |
879c99bb | 434 | $menu[0] = get_string('allusers', 'grades'); |
10f5c046 | 435 | $label = get_string('selectalloroneuser', 'grades'); |
d08bba83 | 436 | } |
d08bba83 | 437 | while ($userdata = $gui->next_user()) { |
438 | $user = $userdata->user; | |
38c1dd19 RT |
439 | $userfullname = fullname($user); |
440 | if ($user->suspendedenrolment) { | |
441 | $menususpendedusers[$user->id] = $userfullname; | |
442 | } else { | |
443 | $menu[$user->id] = $userfullname; | |
444 | } | |
d08bba83 | 445 | } |
d08bba83 | 446 | $gui->close(); |
447 | ||
7ac88172 | 448 | if ($includeall) { |
38c1dd19 RT |
449 | $menu[0] .= " (" . (count($menu) + count($menususpendedusers) - 1) . ")"; |
450 | } | |
451 | ||
452 | if (!empty($menususpendedusers)) { | |
453 | $menu[] = array(get_string('suspendedusers') => $menususpendedusers); | |
879c99bb | 454 | } |
4d5059d4 | 455 | $select = new single_select(new moodle_url('/grade/report/'.$report.'/index.php', array('id'=>$course->id)), 'userid', $menu, $userid); |
f8dab966 PS |
456 | $select->label = $label; |
457 | $select->formid = 'choosegradeuser'; | |
4d5059d4 | 458 | return $select; |
d08bba83 | 459 | } |
460 | ||
a9937aec DW |
461 | /** |
462 | * Hide warning about changed grades during upgrade to 2.8. | |
463 | * | |
464 | * @param int $courseid The current course id. | |
465 | */ | |
466 | function hide_natural_aggregation_upgrade_notice($courseid) { | |
c8eb1fae | 467 | unset_config('show_sumofgrades_upgrade_' . $courseid); |
a9937aec DW |
468 | } |
469 | ||
c07775df EM |
470 | /** |
471 | * Hide warning about changed grades during upgrade from 2.8.0-2.8.6 and 2.9.0. | |
472 | * | |
473 | * @param int $courseid The current course id. | |
474 | */ | |
ebea19cb | 475 | function grade_hide_min_max_grade_upgrade_notice($courseid) { |
c07775df EM |
476 | unset_config('show_min_max_grades_changed_' . $courseid); |
477 | } | |
478 | ||
479 | /** | |
ebea19cb FM |
480 | * Use the grade min and max from the grade_grade. |
481 | * | |
482 | * This is reserved for core use after an upgrade. | |
c07775df EM |
483 | * |
484 | * @param int $courseid The current course id. | |
485 | */ | |
ebea19cb FM |
486 | function grade_upgrade_use_min_max_from_grade_grade($courseid) { |
487 | grade_set_setting($courseid, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_GRADE); | |
c07775df EM |
488 | |
489 | grade_force_full_regrading($courseid); | |
490 | // Do this now, because it probably happened to late in the page load to be happen automatically. | |
491 | grade_regrade_final_grades($courseid); | |
492 | } | |
493 | ||
494 | /** | |
ebea19cb FM |
495 | * Use the grade min and max from the grade_item. |
496 | * | |
497 | * This is reserved for core use after an upgrade. | |
c07775df EM |
498 | * |
499 | * @param int $courseid The current course id. | |
500 | */ | |
ebea19cb FM |
501 | function grade_upgrade_use_min_max_from_grade_item($courseid) { |
502 | grade_set_setting($courseid, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_ITEM); | |
c07775df EM |
503 | |
504 | grade_force_full_regrading($courseid); | |
505 | // Do this now, because it probably happened to late in the page load to be happen automatically. | |
506 | grade_regrade_final_grades($courseid); | |
507 | } | |
508 | ||
47d6e6a7 DW |
509 | /** |
510 | * Hide warning about changed grades during upgrade to 2.8. | |
511 | * | |
512 | * @param int $courseid The current course id. | |
513 | */ | |
514 | function hide_aggregatesubcats_upgrade_notice($courseid) { | |
515 | unset_config('show_aggregatesubcats_upgrade_' . $courseid); | |
516 | } | |
517 | ||
deb3d5ed MG |
518 | /** |
519 | * Hide warning about changed grades due to bug fixes | |
520 | * | |
521 | * @param int $courseid The current course id. | |
522 | */ | |
523 | function hide_gradebook_calculations_freeze_notice($courseid) { | |
524 | unset_config('gradebook_calculations_freeze_' . $courseid); | |
525 | } | |
526 | ||
a9937aec DW |
527 | /** |
528 | * Print warning about changed grades during upgrade to 2.8. | |
529 | * | |
530 | * @param int $courseid The current course id. | |
531 | * @param context $context The course context. | |
eafab799 | 532 | * @param string $thispage The relative path for the current page. E.g. /grade/report/user/index.php |
a9937aec DW |
533 | * @param boolean $return return as string |
534 | * | |
535 | * @return nothing or string if $return true | |
536 | */ | |
eafab799 | 537 | function print_natural_aggregation_upgrade_notice($courseid, $context, $thispage, $return=false) { |
ebea19cb | 538 | global $CFG, $OUTPUT; |
a9937aec | 539 | $html = ''; |
a9937aec | 540 | |
ebea19cb FM |
541 | // Do not do anything if they cannot manage the grades of this course. |
542 | if (!has_capability('moodle/grade:manage', $context)) { | |
543 | return $html; | |
544 | } | |
545 | ||
eafab799 DW |
546 | $hidesubcatswarning = optional_param('seenaggregatesubcatsupgradedgrades', false, PARAM_BOOL) && confirm_sesskey(); |
547 | $showsubcatswarning = get_config('core', 'show_aggregatesubcats_upgrade_' . $courseid); | |
548 | $hidenaturalwarning = optional_param('seensumofgradesupgradedgrades', false, PARAM_BOOL) && confirm_sesskey(); | |
549 | $shownaturalwarning = get_config('core', 'show_sumofgrades_upgrade_' . $courseid); | |
ebea19cb | 550 | |
c07775df EM |
551 | $hideminmaxwarning = optional_param('seenminmaxupgradedgrades', false, PARAM_BOOL) && confirm_sesskey(); |
552 | $showminmaxwarning = get_config('core', 'show_min_max_grades_changed_' . $courseid); | |
eafab799 | 553 | |
ebea19cb FM |
554 | $useminmaxfromgradeitem = optional_param('useminmaxfromgradeitem', false, PARAM_BOOL) && confirm_sesskey(); |
555 | $useminmaxfromgradegrade = optional_param('useminmaxfromgradegrade', false, PARAM_BOOL) && confirm_sesskey(); | |
556 | ||
557 | $minmaxtouse = grade_get_setting($courseid, 'minmaxtouse', $CFG->grade_minmaxtouse); | |
eafab799 | 558 | |
deb3d5ed MG |
559 | $gradebookcalculationsfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid); |
560 | $acceptgradebookchanges = optional_param('acceptgradebookchanges', false, PARAM_BOOL) && confirm_sesskey(); | |
561 | ||
eafab799 DW |
562 | // Hide the warning if the user told it to go away. |
563 | if ($hidenaturalwarning) { | |
564 | hide_natural_aggregation_upgrade_notice($courseid); | |
565 | } | |
566 | // Hide the warning if the user told it to go away. | |
567 | if ($hidesubcatswarning) { | |
568 | hide_aggregatesubcats_upgrade_notice($courseid); | |
569 | } | |
570 | ||
ebea19cb | 571 | // Hide the min/max warning if the user told it to go away. |
c07775df | 572 | if ($hideminmaxwarning) { |
ebea19cb FM |
573 | grade_hide_min_max_grade_upgrade_notice($courseid); |
574 | $showminmaxwarning = false; | |
c07775df EM |
575 | } |
576 | ||
ebea19cb FM |
577 | if ($useminmaxfromgradegrade) { |
578 | // Revert to the new behaviour, we now use the grade_grade for min/max. | |
579 | grade_upgrade_use_min_max_from_grade_grade($courseid); | |
580 | grade_hide_min_max_grade_upgrade_notice($courseid); | |
581 | $showminmaxwarning = false; | |
582 | ||
583 | } else if ($useminmaxfromgradeitem) { | |
584 | // Apply the new logic, we now use the grade_item for min/max. | |
585 | grade_upgrade_use_min_max_from_grade_item($courseid); | |
586 | grade_hide_min_max_grade_upgrade_notice($courseid); | |
587 | $showminmaxwarning = false; | |
c07775df EM |
588 | } |
589 | ||
590 | ||
eafab799 | 591 | if (!$hidenaturalwarning && $shownaturalwarning) { |
a9937aec | 592 | $message = get_string('sumofgradesupgradedgrades', 'grades'); |
47d6e6a7 | 593 | $hidemessage = get_string('upgradedgradeshidemessage', 'grades'); |
a9937aec DW |
594 | $urlparams = array( 'id' => $courseid, |
595 | 'seensumofgradesupgradedgrades' => true, | |
596 | 'sesskey' => sesskey()); | |
eafab799 | 597 | $goawayurl = new moodle_url($thispage, $urlparams); |
a9937aec DW |
598 | $goawaybutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get'); |
599 | $html .= $OUTPUT->notification($message, 'notifysuccess'); | |
600 | $html .= $goawaybutton; | |
601 | } | |
602 | ||
eafab799 | 603 | if (!$hidesubcatswarning && $showsubcatswarning) { |
47d6e6a7 DW |
604 | $message = get_string('aggregatesubcatsupgradedgrades', 'grades'); |
605 | $hidemessage = get_string('upgradedgradeshidemessage', 'grades'); | |
606 | $urlparams = array( 'id' => $courseid, | |
607 | 'seenaggregatesubcatsupgradedgrades' => true, | |
608 | 'sesskey' => sesskey()); | |
eafab799 | 609 | $goawayurl = new moodle_url($thispage, $urlparams); |
47d6e6a7 DW |
610 | $goawaybutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get'); |
611 | $html .= $OUTPUT->notification($message, 'notifysuccess'); | |
612 | $html .= $goawaybutton; | |
613 | } | |
614 | ||
ebea19cb | 615 | if ($showminmaxwarning) { |
c07775df EM |
616 | $hidemessage = get_string('upgradedgradeshidemessage', 'grades'); |
617 | $urlparams = array( 'id' => $courseid, | |
618 | 'seenminmaxupgradedgrades' => true, | |
619 | 'sesskey' => sesskey()); | |
ebea19cb | 620 | |
c07775df | 621 | $goawayurl = new moodle_url($thispage, $urlparams); |
ebea19cb FM |
622 | $hideminmaxbutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get'); |
623 | $moreinfo = html_writer::link(get_docs_url(get_string('minmaxtouse_link', 'grades')), get_string('moreinfo'), | |
624 | array('target' => '_blank')); | |
625 | ||
626 | if ($minmaxtouse == GRADE_MIN_MAX_FROM_GRADE_ITEM) { | |
627 | // Show the message that there were min/max issues that have been resolved. | |
628 | $message = get_string('minmaxupgradedgrades', 'grades') . ' ' . $moreinfo; | |
629 | ||
630 | $revertmessage = get_string('upgradedminmaxrevertmessage', 'grades'); | |
631 | $urlparams = array('id' => $courseid, | |
632 | 'useminmaxfromgradegrade' => true, | |
633 | 'sesskey' => sesskey()); | |
634 | $reverturl = new moodle_url($thispage, $urlparams); | |
635 | $revertbutton = $OUTPUT->single_button($reverturl, $revertmessage, 'get'); | |
636 | ||
f677ece5 | 637 | $html .= $OUTPUT->notification($message); |
ebea19cb FM |
638 | $html .= $revertbutton . $hideminmaxbutton; |
639 | ||
640 | } else if ($minmaxtouse == GRADE_MIN_MAX_FROM_GRADE_GRADE) { | |
641 | // Show the warning that there are min/max issues that have not be resolved. | |
642 | $message = get_string('minmaxupgradewarning', 'grades') . ' ' . $moreinfo; | |
643 | ||
644 | $fixmessage = get_string('minmaxupgradefixbutton', 'grades'); | |
645 | $urlparams = array('id' => $courseid, | |
646 | 'useminmaxfromgradeitem' => true, | |
647 | 'sesskey' => sesskey()); | |
648 | $fixurl = new moodle_url($thispage, $urlparams); | |
649 | $fixbutton = $OUTPUT->single_button($fixurl, $fixmessage, 'get'); | |
650 | ||
f677ece5 | 651 | $html .= $OUTPUT->notification($message); |
ebea19cb FM |
652 | $html .= $fixbutton . $hideminmaxbutton; |
653 | } | |
c07775df EM |
654 | } |
655 | ||
deb3d5ed MG |
656 | if ($gradebookcalculationsfreeze) { |
657 | if ($acceptgradebookchanges) { | |
658 | // Accept potential changes in grades caused by extra credit bug MDL-49257. | |
659 | hide_gradebook_calculations_freeze_notice($courseid); | |
660 | $courseitem = grade_item::fetch_course_item($courseid); | |
661 | $courseitem->force_regrading(); | |
662 | grade_regrade_final_grades($courseid); | |
663 | ||
664 | $html .= $OUTPUT->notification(get_string('gradebookcalculationsuptodate', 'grades'), 'notifysuccess'); | |
665 | } else { | |
666 | // Show the warning that there may be extra credit weights problems. | |
667 | $a = new stdClass(); | |
668 | $a->gradebookversion = $gradebookcalculationsfreeze; | |
669 | if (preg_match('/(\d{8,})/', $CFG->release, $matches)) { | |
670 | $a->currentversion = $matches[1]; | |
671 | } else { | |
672 | $a->currentversion = $CFG->release; | |
673 | } | |
6e4b8381 | 674 | $a->url = get_docs_url('Gradebook_calculation_changes'); |
deb3d5ed MG |
675 | $message = get_string('gradebookcalculationswarning', 'grades', $a); |
676 | ||
677 | $fixmessage = get_string('gradebookcalculationsfixbutton', 'grades'); | |
678 | $urlparams = array('id' => $courseid, | |
679 | 'acceptgradebookchanges' => true, | |
680 | 'sesskey' => sesskey()); | |
681 | $fixurl = new moodle_url($thispage, $urlparams); | |
682 | $fixbutton = $OUTPUT->single_button($fixurl, $fixmessage, 'get'); | |
683 | ||
f677ece5 | 684 | $html .= $OUTPUT->notification($message); |
deb3d5ed MG |
685 | $html .= $fixbutton; |
686 | } | |
687 | } | |
688 | ||
ebea19cb FM |
689 | if (!empty($html)) { |
690 | $html = html_writer::tag('div', $html, array('class' => 'core_grades_notices')); | |
c07775df EM |
691 | } |
692 | ||
a9937aec DW |
693 | if ($return) { |
694 | return $html; | |
695 | } else { | |
696 | echo $html; | |
697 | } | |
698 | } | |
699 | ||
0610812a | 700 | /** |
701 | * Print grading plugin selection popup form. | |
702 | * | |
cf72e2dd | 703 | * @param array $plugin_info An array of plugins containing information for the selector |
0610812a | 704 | * @param boolean $return return as string |
cf72e2dd | 705 | * |
0610812a | 706 | * @return nothing or string if $return true |
707 | */ | |
f1a3e072 | 708 | function print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, $return=false) { |
714b4745 | 709 | global $CFG, $OUTPUT, $PAGE; |
dc482cfa | 710 | |
711 | $menu = array(); | |
712 | $count = 0; | |
713 | $active = ''; | |
714 | ||
715 | foreach ($plugin_info as $plugin_type => $plugins) { | |
716 | if ($plugin_type == 'strings') { | |
717 | continue; | |
718 | } | |
719 | ||
7981d537 | 720 | $first_plugin = reset($plugins); |
dc482cfa | 721 | |
f1a3e072 PS |
722 | $sectionname = $plugin_info['strings'][$plugin_type]; |
723 | $section = array(); | |
d4dcfc6b | 724 | |
f1a3e072 | 725 | foreach ($plugins as $plugin) { |
d4dcfc6b | 726 | $link = $plugin->link->out(false); |
f1a3e072 PS |
727 | $section[$link] = $plugin->string; |
728 | $count++; | |
729 | if ($plugin_type === $active_type and $plugin->id === $active_plugin) { | |
730 | $active = $link; | |
dc482cfa | 731 | } |
732 | } | |
f1a3e072 PS |
733 | |
734 | if ($section) { | |
735 | $menu[] = array($sectionname=>$section); | |
736 | } | |
dc482cfa | 737 | } |
738 | ||
cf72e2dd | 739 | // finally print/return the popup form |
dc482cfa | 740 | if ($count > 1) { |
f1a3e072 | 741 | $select = new url_select($menu, $active, null, 'choosepluginreport'); |
29bea634 | 742 | $select->set_label(get_string('gradereport', 'grades'), array('class' => 'accesshide')); |
7981d537 | 743 | if ($return) { |
f1a3e072 | 744 | return $OUTPUT->render($select); |
7981d537 | 745 | } else { |
f1a3e072 | 746 | echo $OUTPUT->render($select); |
7981d537 | 747 | } |
dc482cfa | 748 | } else { |
749 | // only one option - no plugin selector needed | |
750 | return ''; | |
751 | } | |
752 | } | |
753 | ||
754 | /** | |
755 | * Print grading plugin selection tab-based navigation. | |
756 | * | |
cf72e2dd | 757 | * @param string $active_type type of plugin on current page - import, export, report or edit |
758 | * @param string $active_plugin active plugin type - grader, user, cvs, ... | |
759 | * @param array $plugin_info Array of plugins | |
dc482cfa | 760 | * @param boolean $return return as string |
cf72e2dd | 761 | * |
dc482cfa | 762 | * @return nothing or string if $return true |
763 | */ | |
b827784e | 764 | function grade_print_tabs($active_type, $active_plugin, $plugin_info, $return=false) { |
dc482cfa | 765 | global $CFG, $COURSE; |
1c1f64a2 | 766 | |
e1513ca9 | 767 | if (!isset($currenttab)) { //TODO: this is weird |
dc482cfa | 768 | $currenttab = ''; |
769 | } | |
770 | ||
771 | $tabs = array(); | |
772 | $top_row = array(); | |
773 | $bottom_row = array(); | |
774 | $inactive = array($active_plugin); | |
f48d5209 | 775 | $activated = array($active_type); |
dc482cfa | 776 | |
777 | $count = 0; | |
778 | $active = ''; | |
779 | ||
780 | foreach ($plugin_info as $plugin_type => $plugins) { | |
781 | if ($plugin_type == 'strings') { | |
782 | continue; | |
783 | } | |
784 | ||
785 | // If $plugins is actually the definition of a child-less parent link: | |
cf72e2dd | 786 | if (!empty($plugins->id)) { |
787 | $string = $plugins->string; | |
788 | if (!empty($plugin_info[$active_type]->parent)) { | |
789 | $string = $plugin_info[$active_type]->parent->string; | |
27eef3bb | 790 | } |
791 | ||
cf72e2dd | 792 | $top_row[] = new tabobject($plugin_type, $plugins->link, $string); |
dc482cfa | 793 | continue; |
794 | } | |
795 | ||
796 | $first_plugin = reset($plugins); | |
cf72e2dd | 797 | $url = $first_plugin->link; |
dc482cfa | 798 | |
799 | if ($plugin_type == 'report') { | |
800 | $url = $CFG->wwwroot.'/grade/report/index.php?id='.$COURSE->id; | |
801 | } | |
802 | ||
803 | $top_row[] = new tabobject($plugin_type, $url, $plugin_info['strings'][$plugin_type]); | |
804 | ||
805 | if ($active_type == $plugin_type) { | |
806 | foreach ($plugins as $plugin) { | |
cf72e2dd | 807 | $bottom_row[] = new tabobject($plugin->id, $plugin->link, $plugin->string); |
808 | if ($plugin->id == $active_plugin) { | |
809 | $inactive = array($plugin->id); | |
dc482cfa | 810 | } |
811 | } | |
812 | } | |
813 | } | |
814 | ||
815 | $tabs[] = $top_row; | |
816 | $tabs[] = $bottom_row; | |
817 | ||
818 | if ($return) { | |
f48d5209 | 819 | return print_tabs($tabs, $active_plugin, $inactive, $activated, true); |
dc482cfa | 820 | } else { |
f48d5209 | 821 | print_tabs($tabs, $active_plugin, $inactive, $activated); |
dc482cfa | 822 | } |
823 | } | |
824 | ||
cf72e2dd | 825 | /** |
826 | * grade_get_plugin_info | |
827 | * | |
828 | * @param int $courseid The course id | |
829 | * @param string $active_type type of plugin on current page - import, export, report or edit | |
830 | * @param string $active_plugin active plugin type - grader, user, cvs, ... | |
831 | * | |
832 | * @return array | |
833 | */ | |
dc482cfa | 834 | function grade_get_plugin_info($courseid, $active_type, $active_plugin) { |
f7fcf4cd | 835 | global $CFG, $SITE; |
cbff94ba | 836 | |
d4060472 | 837 | $context = context_course::instance($courseid); |
cbff94ba | 838 | |
dc482cfa | 839 | $plugin_info = array(); |
6e2f3121 | 840 | $count = 0; |
3af29899 | 841 | $active = ''; |
dc482cfa | 842 | $url_prefix = $CFG->wwwroot . '/grade/'; |
843 | ||
844 | // Language strings | |
4d5059d4 | 845 | $plugin_info['strings'] = grade_helper::get_plugin_strings(); |
b827784e | 846 | |
4d5059d4 SH |
847 | if ($reports = grade_helper::get_plugins_reports($courseid)) { |
848 | $plugin_info['report'] = $reports; | |
b827784e | 849 | } |
c46aeeab | 850 | |
eb84b779 MG |
851 | if ($settings = grade_helper::get_info_manage_settings($courseid)) { |
852 | $plugin_info['settings'] = $settings; | |
853 | } | |
854 | ||
4d5059d4 SH |
855 | if ($scale = grade_helper::get_info_scales($courseid)) { |
856 | $plugin_info['scale'] = array('view'=>$scale); | |
cbff94ba | 857 | } |
dc482cfa | 858 | |
4d5059d4 SH |
859 | if ($outcomes = grade_helper::get_info_outcomes($courseid)) { |
860 | $plugin_info['outcome'] = $outcomes; | |
cbff94ba | 861 | } |
cbff94ba | 862 | |
4d5059d4 SH |
863 | if ($letters = grade_helper::get_info_letters($courseid)) { |
864 | $plugin_info['letter'] = $letters; | |
cbff94ba | 865 | } |
4d5059d4 SH |
866 | |
867 | if ($imports = grade_helper::get_plugins_import($courseid)) { | |
868 | $plugin_info['import'] = $imports; | |
281ffa4a | 869 | } |
c46aeeab | 870 | |
4d5059d4 SH |
871 | if ($exports = grade_helper::get_plugins_export($courseid)) { |
872 | $plugin_info['export'] = $exports; | |
281ffa4a | 873 | } |
281ffa4a | 874 | |
4d5059d4 SH |
875 | foreach ($plugin_info as $plugin_type => $plugins) { |
876 | if (!empty($plugins->id) && $active_plugin == $plugins->id) { | |
877 | $plugin_info['strings']['active_plugin_str'] = $plugins->string; | |
878 | break; | |
281ffa4a | 879 | } |
4d5059d4 SH |
880 | foreach ($plugins as $plugin) { |
881 | if (is_a($plugin, 'grade_plugin_info')) { | |
882 | if ($active_plugin == $plugin->id) { | |
883 | $plugin_info['strings']['active_plugin_str'] = $plugin->string; | |
884 | } | |
3af29899 | 885 | } |
281ffa4a | 886 | } |
cbff94ba | 887 | } |
c46aeeab | 888 | |
dc482cfa | 889 | foreach ($plugin_info as $plugin_type => $plugins) { |
cf72e2dd | 890 | if (!empty($plugins->id) && $active_plugin == $plugins->id) { |
891 | $plugin_info['strings']['active_plugin_str'] = $plugins->string; | |
dc482cfa | 892 | break; |
78ad5f3f | 893 | } |
dc482cfa | 894 | foreach ($plugins as $plugin) { |
cf72e2dd | 895 | if (is_a($plugin, 'grade_plugin_info')) { |
896 | if ($active_plugin == $plugin->id) { | |
897 | $plugin_info['strings']['active_plugin_str'] = $plugin->string; | |
898 | } | |
78ad5f3f | 899 | } |
78ad5f3f | 900 | } |
dc482cfa | 901 | } |
78ad5f3f | 902 | |
dc482cfa | 903 | return $plugin_info; |
904 | } | |
284abb09 | 905 | |
cf72e2dd | 906 | /** |
907 | * A simple class containing info about grade plugins. | |
908 | * Can be subclassed for special rules | |
909 | * | |
a153c9f2 | 910 | * @package core_grades |
cf72e2dd | 911 | * @copyright 2009 Nicolas Connault |
912 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
913 | */ | |
914 | class grade_plugin_info { | |
915 | /** | |
916 | * A unique id for this plugin | |
917 | * | |
918 | * @var mixed | |
919 | */ | |
920 | public $id; | |
921 | /** | |
922 | * A URL to access this plugin | |
923 | * | |
924 | * @var mixed | |
925 | */ | |
926 | public $link; | |
927 | /** | |
928 | * The name of this plugin | |
929 | * | |
930 | * @var mixed | |
931 | */ | |
932 | public $string; | |
933 | /** | |
934 | * Another grade_plugin_info object, parent of the current one | |
935 | * | |
936 | * @var mixed | |
937 | */ | |
938 | public $parent; | |
939 | ||
940 | /** | |
941 | * Constructor | |
942 | * | |
943 | * @param int $id A unique id for this plugin | |
944 | * @param string $link A URL to access this plugin | |
945 | * @param string $string The name of this plugin | |
946 | * @param object $parent Another grade_plugin_info object, parent of the current one | |
947 | * | |
948 | * @return void | |
949 | */ | |
950 | public function __construct($id, $link, $string, $parent=null) { | |
951 | $this->id = $id; | |
952 | $this->link = $link; | |
953 | $this->string = $string; | |
954 | $this->parent = $parent; | |
955 | } | |
956 | } | |
957 | ||
de0300ea | 958 | /** |
959 | * Prints the page headers, breadcrumb trail, page heading, (optional) dropdown navigation menu and | |
960 | * (optional) navigation tabs for any gradebook page. All gradebook pages MUST use these functions | |
961 | * in favour of the usual print_header(), print_header_simple(), print_heading() etc. | |
962 | * !IMPORTANT! Use of tabs.php file in gradebook pages is forbidden unless tabs are switched off at | |
963 | * the site level for the gradebook ($CFG->grade_navmethod = GRADE_NAVMETHOD_DROPDOWN). | |
964 | * | |
cf72e2dd | 965 | * @param int $courseid Course id |
966 | * @param string $active_type The type of the current page (report, settings, | |
967 | * import, export, scales, outcomes, letters) | |
968 | * @param string $active_plugin The plugin of the current page (grader, fullview etc...) | |
969 | * @param string $heading The heading of the page. Tries to guess if none is given | |
de0300ea | 970 | * @param boolean $return Whether to return (true) or echo (false) the HTML generated by this function |
cf72e2dd | 971 | * @param string $bodytags Additional attributes that will be added to the <body> tag |
972 | * @param string $buttons Additional buttons to display on the page | |
5625d33d | 973 | * @param boolean $shownavigation should the gradebook navigation drop down (or tabs) be shown? |
1464bcc8 AG |
974 | * @param string $headerhelpidentifier The help string identifier if required. |
975 | * @param string $headerhelpcomponent The component for the help string. | |
1acbd1f9 | 976 | * @param stdClass $user The user object for use with the user context header. |
de0300ea | 977 | * |
978 | * @return string HTML code or nothing if $return == false | |
979 | */ | |
cf72e2dd | 980 | function print_grade_page_head($courseid, $active_type, $active_plugin=null, |
34a2777c | 981 | $heading = false, $return=false, |
1acbd1f9 AG |
982 | $buttons=false, $shownavigation=true, $headerhelpidentifier = null, $headerhelpcomponent = null, |
983 | $user = null) { | |
4d5059d4 | 984 | global $CFG, $OUTPUT, $PAGE; |
c46aeeab | 985 | |
eb84b779 MG |
986 | if ($active_type === 'preferences') { |
987 | // In Moodle 2.8 report preferences were moved under 'settings'. Allow backward compatibility for 3rd party grade reports. | |
988 | $active_type = 'settings'; | |
989 | } | |
990 | ||
dc482cfa | 991 | $plugin_info = grade_get_plugin_info($courseid, $active_type, $active_plugin); |
c46aeeab | 992 | |
dc482cfa | 993 | // Determine the string of the active plugin |
994 | $stractive_plugin = ($active_plugin) ? $plugin_info['strings']['active_plugin_str'] : $heading; | |
995 | $stractive_type = $plugin_info['strings'][$active_type]; | |
e0724506 | 996 | |
cf72e2dd | 997 | if (empty($plugin_info[$active_type]->id) || !empty($plugin_info[$active_type]->parent)) { |
4d5059d4 SH |
998 | $title = $PAGE->course->fullname.': ' . $stractive_type . ': ' . $stractive_plugin; |
999 | } else { | |
1000 | $title = $PAGE->course->fullname.': ' . $stractive_plugin; | |
c4c97a6d | 1001 | } |
1002 | ||
367a75fa SH |
1003 | if ($active_type == 'report') { |
1004 | $PAGE->set_pagelayout('report'); | |
1005 | } else { | |
1006 | $PAGE->set_pagelayout('admin'); | |
1007 | } | |
4d5059d4 | 1008 | $PAGE->set_title(get_string('grades') . ': ' . $stractive_type); |
f3df5e14 | 1009 | $PAGE->set_heading($title); |
4d5059d4 SH |
1010 | if ($buttons instanceof single_button) { |
1011 | $buttons = $OUTPUT->render($buttons); | |
1012 | } | |
f3df5e14 | 1013 | $PAGE->set_button($buttons); |
6bf696bb MG |
1014 | if ($courseid != SITEID) { |
1015 | grade_extend_settings($plugin_info, $courseid); | |
1016 | } | |
4d5059d4 | 1017 | |
f3df5e14 | 1018 | $returnval = $OUTPUT->header(); |
1464bcc8 | 1019 | |
f3df5e14 | 1020 | if (!$return) { |
1021 | echo $returnval; | |
1022 | } | |
dc482cfa | 1023 | |
1024 | // Guess heading if not given explicitly | |
1025 | if (!$heading) { | |
1026 | $heading = $stractive_plugin; | |
1027 | } | |
1028 | ||
2821c495 | 1029 | if ($shownavigation) { |
1acbd1f9 | 1030 | $navselector = null; |
6bf696bb MG |
1031 | if ($courseid != SITEID && |
1032 | ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_DROPDOWN)) { | |
1acbd1f9 AG |
1033 | // It's absolutely essential that this grade plugin selector is shown after the user header. Just ask Fred. |
1034 | $navselector = print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, true); | |
1035 | if ($return) { | |
1036 | $returnval .= $navselector; | |
1037 | } else if (!isset($user)) { | |
1038 | echo $navselector; | |
1039 | } | |
2821c495 | 1040 | } |
5625d33d | 1041 | |
1464bcc8 AG |
1042 | $output = ''; |
1043 | // Add a help dialogue box if provided. | |
1044 | if (isset($headerhelpidentifier)) { | |
1045 | $output = $OUTPUT->heading_with_help($heading, $headerhelpidentifier, $headerhelpcomponent); | |
1046 | } else { | |
1acbd1f9 AG |
1047 | if (isset($user)) { |
1048 | $output = $OUTPUT->context_header( | |
1049 | array( | |
1050 | 'heading' => fullname($user), | |
1051 | 'user' => $user, | |
1052 | 'usercontext' => context_user::instance($user->id) | |
1053 | ), 2 | |
1054 | ) . $navselector; | |
1055 | } else { | |
1056 | $output = $OUTPUT->heading($heading); | |
1057 | } | |
1464bcc8 AG |
1058 | } |
1059 | ||
5625d33d | 1060 | if ($return) { |
1464bcc8 | 1061 | $returnval .= $output; |
5625d33d | 1062 | } else { |
1464bcc8 | 1063 | echo $output; |
5625d33d AD |
1064 | } |
1065 | ||
6bf696bb MG |
1066 | if ($courseid != SITEID && |
1067 | ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_TABS)) { | |
2821c495 AD |
1068 | $returnval .= grade_print_tabs($active_type, $active_plugin, $plugin_info, $return); |
1069 | } | |
8354e716 | 1070 | } |
dc482cfa | 1071 | |
eafab799 DW |
1072 | $returnval .= print_natural_aggregation_upgrade_notice($courseid, |
1073 | context_course::instance($courseid), | |
f01972d9 | 1074 | $PAGE->url, |
eafab799 DW |
1075 | $return); |
1076 | ||
dc482cfa | 1077 | if ($return) { |
1078 | return $returnval; | |
6e2f3121 | 1079 | } |
cbff94ba | 1080 | } |
1081 | ||
0610812a | 1082 | /** |
7a6b7acf | 1083 | * Utility class used for return tracking when using edit and other forms in grade plugins |
cf72e2dd | 1084 | * |
a153c9f2 | 1085 | * @package core_grades |
cf72e2dd | 1086 | * @copyright 2009 Nicolas Connault |
1087 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
0610812a | 1088 | */ |
3af29899 | 1089 | class grade_plugin_return { |
d24832f9 | 1090 | public $type; |
1091 | public $plugin; | |
1092 | public $courseid; | |
1093 | public $userid; | |
1094 | public $page; | |
281ffa4a | 1095 | |
0610812a | 1096 | /** |
1097 | * Constructor | |
cf72e2dd | 1098 | * |
0610812a | 1099 | * @param array $params - associative array with return parameters, if null parameter are taken from _GET or _POST |
1100 | */ | |
cf72e2dd | 1101 | public function grade_plugin_return($params = null) { |
3af29899 | 1102 | if (empty($params)) { |
1103 | $this->type = optional_param('gpr_type', null, PARAM_SAFEDIR); | |
aff24313 | 1104 | $this->plugin = optional_param('gpr_plugin', null, PARAM_PLUGIN); |
3af29899 | 1105 | $this->courseid = optional_param('gpr_courseid', null, PARAM_INT); |
1106 | $this->userid = optional_param('gpr_userid', null, PARAM_INT); | |
1107 | $this->page = optional_param('gpr_page', null, PARAM_INT); | |
a983b6ec | 1108 | |
a983b6ec | 1109 | } else { |
3af29899 | 1110 | foreach ($params as $key=>$value) { |
4cc977a6 | 1111 | if (property_exists($this, $key)) { |
3af29899 | 1112 | $this->$key = $value; |
1113 | } | |
cbff94ba | 1114 | } |
1115 | } | |
6cd8c592 | 1116 | } |
1117 | ||
0610812a | 1118 | /** |
1119 | * Returns return parameters as options array suitable for buttons. | |
1120 | * @return array options | |
1121 | */ | |
d24832f9 | 1122 | public function get_options() { |
7a6b7acf | 1123 | if (empty($this->type)) { |
3af29899 | 1124 | return array(); |
865e9a82 | 1125 | } |
6cd8c592 | 1126 | |
3af29899 | 1127 | $params = array(); |
6cd8c592 | 1128 | |
7a6b7acf | 1129 | if (!empty($this->plugin)) { |
1130 | $params['plugin'] = $this->plugin; | |
1131 | } | |
6cd8c592 | 1132 | |
3af29899 | 1133 | if (!empty($this->courseid)) { |
1134 | $params['id'] = $this->courseid; | |
6cd8c592 | 1135 | } |
9c61ba4d | 1136 | |
3af29899 | 1137 | if (!empty($this->userid)) { |
1138 | $params['userid'] = $this->userid; | |
9c61ba4d | 1139 | } |
9c61ba4d | 1140 | |
3af29899 | 1141 | if (!empty($this->page)) { |
1142 | $params['page'] = $this->page; | |
cbff94ba | 1143 | } |
865e9a82 | 1144 | |
3af29899 | 1145 | return $params; |
cbff94ba | 1146 | } |
cbff94ba | 1147 | |
0610812a | 1148 | /** |
1149 | * Returns return url | |
cf72e2dd | 1150 | * |
0610812a | 1151 | * @param string $default default url when params not set |
cf72e2dd | 1152 | * @param array $extras Extra URL parameters |
1153 | * | |
0610812a | 1154 | * @return string url |
1155 | */ | |
d24832f9 | 1156 | public function get_return_url($default, $extras=null) { |
3af29899 | 1157 | global $CFG; |
cbff94ba | 1158 | |
3af29899 | 1159 | if (empty($this->type) or empty($this->plugin)) { |
1160 | return $default; | |
cbff94ba | 1161 | } |
1162 | ||
65dd61bd | 1163 | $url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php'; |
1164 | $glue = '?'; | |
cbff94ba | 1165 | |
3af29899 | 1166 | if (!empty($this->courseid)) { |
1167 | $url .= $glue.'id='.$this->courseid; | |
1168 | $glue = '&'; | |
cbff94ba | 1169 | } |
cbff94ba | 1170 | |
3af29899 | 1171 | if (!empty($this->userid)) { |
1172 | $url .= $glue.'userid='.$this->userid; | |
1173 | $glue = '&'; | |
cbff94ba | 1174 | } |
7e2d7c92 | 1175 | |
3af29899 | 1176 | if (!empty($this->page)) { |
1177 | $url .= $glue.'page='.$this->page; | |
65dd61bd | 1178 | $glue = '&'; |
1179 | } | |
1180 | ||
1181 | if (!empty($extras)) { | |
cf72e2dd | 1182 | foreach ($extras as $key=>$value) { |
65dd61bd | 1183 | $url .= $glue.$key.'='.$value; |
1184 | $glue = '&'; | |
7a6b7acf | 1185 | } |
cbff94ba | 1186 | } |
cbff94ba | 1187 | |
3af29899 | 1188 | return $url; |
cbff94ba | 1189 | } |
cbff94ba | 1190 | |
0610812a | 1191 | /** |
1192 | * Returns string with hidden return tracking form elements. | |
1193 | * @return string | |
1194 | */ | |
d24832f9 | 1195 | public function get_form_fields() { |
7a6b7acf | 1196 | if (empty($this->type)) { |
3af29899 | 1197 | return ''; |
cbff94ba | 1198 | } |
cbff94ba | 1199 | |
3af29899 | 1200 | $result = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />'; |
7a6b7acf | 1201 | |
1202 | if (!empty($this->plugin)) { | |
1203 | $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />'; | |
1204 | } | |
0ca5abd6 | 1205 | |
3af29899 | 1206 | if (!empty($this->courseid)) { |
1207 | $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />'; | |
cbff94ba | 1208 | } |
cbff94ba | 1209 | |
3af29899 | 1210 | if (!empty($this->userid)) { |
1211 | $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />'; | |
cbff94ba | 1212 | } |
cbff94ba | 1213 | |
3af29899 | 1214 | if (!empty($this->page)) { |
1215 | $result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />'; | |
cbff94ba | 1216 | } |
1217 | } | |
cbff94ba | 1218 | |
0610812a | 1219 | /** |
1220 | * Add hidden elements into mform | |
cf72e2dd | 1221 | * |
1222 | * @param object &$mform moodle form object | |
1223 | * | |
0610812a | 1224 | * @return void |
1225 | */ | |
d24832f9 | 1226 | public function add_mform_elements(&$mform) { |
7a6b7acf | 1227 | if (empty($this->type)) { |
3af29899 | 1228 | return; |
cbff94ba | 1229 | } |
cbff94ba | 1230 | |
3af29899 | 1231 | $mform->addElement('hidden', 'gpr_type', $this->type); |
1232 | $mform->setType('gpr_type', PARAM_SAFEDIR); | |
cbff94ba | 1233 | |
7a6b7acf | 1234 | if (!empty($this->plugin)) { |
1235 | $mform->addElement('hidden', 'gpr_plugin', $this->plugin); | |
aff24313 | 1236 | $mform->setType('gpr_plugin', PARAM_PLUGIN); |
7a6b7acf | 1237 | } |
97033c86 | 1238 | |
3af29899 | 1239 | if (!empty($this->courseid)) { |
1240 | $mform->addElement('hidden', 'gpr_courseid', $this->courseid); | |
1241 | $mform->setType('gpr_courseid', PARAM_INT); | |
cbff94ba | 1242 | } |
cbff94ba | 1243 | |
3af29899 | 1244 | if (!empty($this->userid)) { |
1245 | $mform->addElement('hidden', 'gpr_userid', $this->userid); | |
1246 | $mform->setType('gpr_userid', PARAM_INT); | |
cbff94ba | 1247 | } |
cbff94ba | 1248 | |
3af29899 | 1249 | if (!empty($this->page)) { |
1250 | $mform->addElement('hidden', 'gpr_page', $this->page); | |
1251 | $mform->setType('gpr_page', PARAM_INT); | |
cbff94ba | 1252 | } |
1253 | } | |
281ffa4a | 1254 | |
0610812a | 1255 | /** |
1256 | * Add return tracking params into url | |
cf72e2dd | 1257 | * |
1c1f64a2 | 1258 | * @param moodle_url $url A URL |
cf72e2dd | 1259 | * |
6ef4878b | 1260 | * @return string $url with return tracking params |
0610812a | 1261 | */ |
1c1f64a2 | 1262 | public function add_url_params(moodle_url $url) { |
7a6b7acf | 1263 | if (empty($this->type)) { |
3af29899 | 1264 | return $url; |
cbff94ba | 1265 | } |
5609f9e6 | 1266 | |
1c1f64a2 | 1267 | $url->param('gpr_type', $this->type); |
cbff94ba | 1268 | |
7a6b7acf | 1269 | if (!empty($this->plugin)) { |
1c1f64a2 | 1270 | $url->param('gpr_plugin', $this->plugin); |
7a6b7acf | 1271 | } |
cbff94ba | 1272 | |
3af29899 | 1273 | if (!empty($this->courseid)) { |
1c1f64a2 | 1274 | $url->param('gpr_courseid' ,$this->courseid); |
cbff94ba | 1275 | } |
cbff94ba | 1276 | |
3af29899 | 1277 | if (!empty($this->userid)) { |
1c1f64a2 | 1278 | $url->param('gpr_userid', $this->userid); |
cbff94ba | 1279 | } |
0a8a95c9 | 1280 | |
3af29899 | 1281 | if (!empty($this->page)) { |
1c1f64a2 | 1282 | $url->param('gpr_page', $this->page); |
0a8a95c9 | 1283 | } |
5a412dbf | 1284 | |
3af29899 | 1285 | return $url; |
5a412dbf | 1286 | } |
5a412dbf | 1287 | } |
7a6b7acf | 1288 | |
826c5f86 | 1289 | /** |
1290 | * Function central to gradebook for building and printing the navigation (breadcrumb trail). | |
cf72e2dd | 1291 | * |
826c5f86 | 1292 | * @param string $path The path of the calling script (using __FILE__?) |
1293 | * @param string $pagename The language string to use as the last part of the navigation (non-link) | |
cf72e2dd | 1294 | * @param mixed $id Either a plain integer (assuming the key is 'id') or |
1295 | * an array of keys and values (e.g courseid => $courseid, itemid...) | |
1296 | * | |
826c5f86 | 1297 | * @return string |
1298 | */ | |
1299 | function grade_build_nav($path, $pagename=null, $id=null) { | |
f3df5e14 | 1300 | global $CFG, $COURSE, $PAGE; |
826c5f86 | 1301 | |
1302 | $strgrades = get_string('grades', 'grades'); | |
1303 | ||
1304 | // Parse the path and build navlinks from its elements | |
1305 | $dirroot_length = strlen($CFG->dirroot) + 1; // Add 1 for the first slash | |
1306 | $path = substr($path, $dirroot_length); | |
1307 | $path = str_replace('\\', '/', $path); | |
1308 | ||
1309 | $path_elements = explode('/', $path); | |
1310 | ||
1311 | $path_elements_count = count($path_elements); | |
1312 | ||
826c5f86 | 1313 | // First link is always 'grade' |
a6855934 | 1314 | $PAGE->navbar->add($strgrades, new moodle_url('/grade/index.php', array('id'=>$COURSE->id))); |
826c5f86 | 1315 | |
f3df5e14 | 1316 | $link = null; |
826c5f86 | 1317 | $numberofelements = 3; |
1318 | ||
1319 | // Prepare URL params string | |
f3df5e14 | 1320 | $linkparams = array(); |
826c5f86 | 1321 | if (!is_null($id)) { |
1322 | if (is_array($id)) { | |
1323 | foreach ($id as $idkey => $idvalue) { | |
f3df5e14 | 1324 | $linkparams[$idkey] = $idvalue; |
826c5f86 | 1325 | } |
1326 | } else { | |
f3df5e14 | 1327 | $linkparams['id'] = $id; |
826c5f86 | 1328 | } |
1329 | } | |
1330 | ||
1331 | $navlink4 = null; | |
1332 | ||
0f78c4de | 1333 | // Remove file extensions from filenames |
1334 | foreach ($path_elements as $key => $filename) { | |
1335 | $path_elements[$key] = str_replace('.php', '', $filename); | |
1336 | } | |
1337 | ||
826c5f86 | 1338 | // Second level links |
1339 | switch ($path_elements[1]) { | |
1340 | case 'edit': // No link | |
1341 | if ($path_elements[3] != 'index.php') { | |
1342 | $numberofelements = 4; | |
1343 | } | |
1344 | break; | |
1345 | case 'import': // No link | |
1346 | break; | |
1347 | case 'export': // No link | |
1348 | break; | |
1349 | case 'report': | |
1350 | // $id is required for this link. Do not print it if $id isn't given | |
1351 | if (!is_null($id)) { | |
a6855934 | 1352 | $link = new moodle_url('/grade/report/index.php', $linkparams); |
826c5f86 | 1353 | } |
1354 | ||
1355 | if ($path_elements[2] == 'grader') { | |
1356 | $numberofelements = 4; | |
1357 | } | |
1358 | break; | |
1359 | ||
1360 | default: | |
1361 | // If this element isn't among the ones already listed above, it isn't supported, throw an error. | |
cf72e2dd | 1362 | debugging("grade_build_nav() doesn't support ". $path_elements[1] . |
1363 | " as the second path element after 'grade'."); | |
826c5f86 | 1364 | return false; |
1365 | } | |
f3df5e14 | 1366 | $PAGE->navbar->add(get_string($path_elements[1], 'grades'), $link); |
826c5f86 | 1367 | |
1368 | // Third level links | |
1369 | if (empty($pagename)) { | |
1370 | $pagename = get_string($path_elements[2], 'grades'); | |
1371 | } | |
1372 | ||
1373 | switch ($numberofelements) { | |
1374 | case 3: | |
f3df5e14 | 1375 | $PAGE->navbar->add($pagename, $link); |
826c5f86 | 1376 | break; |
1377 | case 4: | |
826c5f86 | 1378 | if ($path_elements[2] == 'grader' AND $path_elements[3] != 'index.php') { |
b5e7b2bf | 1379 | $PAGE->navbar->add(get_string('pluginname', 'gradereport_grader'), new moodle_url('/grade/report/grader/index.php', $linkparams)); |
826c5f86 | 1380 | } |
f3df5e14 | 1381 | $PAGE->navbar->add($pagename); |
826c5f86 | 1382 | break; |
1383 | } | |
826c5f86 | 1384 | |
f3df5e14 | 1385 | return ''; |
d4795a07 | 1386 | } |
7a6b7acf | 1387 | |
e98871a2 | 1388 | /** |
6cc3e350 | 1389 | * General structure representing grade items in course |
cf72e2dd | 1390 | * |
a153c9f2 | 1391 | * @package core_grades |
cf72e2dd | 1392 | * @copyright 2009 Nicolas Connault |
1393 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
e98871a2 | 1394 | */ |
6cc3e350 | 1395 | class grade_structure { |
d24832f9 | 1396 | public $context; |
e98871a2 | 1397 | |
d24832f9 | 1398 | public $courseid; |
e98871a2 | 1399 | |
73ca5f01 | 1400 | /** |
1401 | * Reference to modinfo for current course (for performance, to save | |
1402 | * retrieving it from courseid every time). Not actually set except for | |
1403 | * the grade_tree type. | |
1404 | * @var course_modinfo | |
1405 | */ | |
1406 | public $modinfo; | |
1407 | ||
e98871a2 | 1408 | /** |
1409 | * 1D array of grade items only | |
1410 | */ | |
d24832f9 | 1411 | public $items; |
e98871a2 | 1412 | |
6391ebe7 | 1413 | /** |
6cc3e350 | 1414 | * Returns icon of element |
cf72e2dd | 1415 | * |
1416 | * @param array &$element An array representing an element in the grade_tree | |
1417 | * @param bool $spacerifnone return spacer if no icon found | |
1418 | * | |
6cc3e350 | 1419 | * @return string icon or spacer |
6391ebe7 | 1420 | */ |
d24832f9 | 1421 | public function get_element_icon(&$element, $spacerifnone=false) { |
6b608f8f | 1422 | global $CFG, $OUTPUT; |
559276b1 | 1423 | require_once $CFG->libdir.'/filelib.php'; |
6cc3e350 | 1424 | |
1ef48469 JC |
1425 | $outputstr = ''; |
1426 | ||
1427 | // Object holding pix_icon information before instantiation. | |
1428 | $icon = new stdClass(); | |
1429 | $icon->attributes = array( | |
09235e24 | 1430 | 'class' => 'icon itemicon' |
1ef48469 JC |
1431 | ); |
1432 | $icon->component = 'moodle'; | |
1433 | ||
1434 | $none = true; | |
6cc3e350 | 1435 | switch ($element['type']) { |
1436 | case 'item': | |
1437 | case 'courseitem': | |
1438 | case 'categoryitem': | |
1ef48469 JC |
1439 | $none = false; |
1440 | ||
cf72e2dd | 1441 | $is_course = $element['object']->is_course_item(); |
1442 | $is_category = $element['object']->is_category_item(); | |
1443 | $is_scale = $element['object']->gradetype == GRADE_TYPE_SCALE; | |
1444 | $is_value = $element['object']->gradetype == GRADE_TYPE_VALUE; | |
0077f571 | 1445 | $is_outcome = !empty($element['object']->outcomeid); |
cf72e2dd | 1446 | |
6cc3e350 | 1447 | if ($element['object']->is_calculated()) { |
1ef48469 JC |
1448 | $icon->pix = 'i/calc'; |
1449 | $icon->title = s(get_string('calculatedgrade', 'grades')); | |
6cc3e350 | 1450 | |
cf72e2dd | 1451 | } else if (($is_course or $is_category) and ($is_scale or $is_value)) { |
6cc3e350 | 1452 | if ($category = $element['object']->get_item_category()) { |
bfe969e8 DW |
1453 | $aggrstrings = grade_helper::get_aggregation_strings(); |
1454 | $stragg = $aggrstrings[$category->aggregation]; | |
1ef48469 JC |
1455 | |
1456 | $icon->pix = 'i/calc'; | |
1457 | $icon->title = s($stragg); | |
1458 | ||
6cc3e350 | 1459 | switch ($category->aggregation) { |
1460 | case GRADE_AGGREGATE_MEAN: | |
1461 | case GRADE_AGGREGATE_MEDIAN: | |
1462 | case GRADE_AGGREGATE_WEIGHTED_MEAN: | |
1426edac | 1463 | case GRADE_AGGREGATE_WEIGHTED_MEAN2: |
6cc3e350 | 1464 | case GRADE_AGGREGATE_EXTRACREDIT_MEAN: |
1ef48469 JC |
1465 | $icon->pix = 'i/agg_mean'; |
1466 | break; | |
0758a08e | 1467 | case GRADE_AGGREGATE_SUM: |
1ef48469 JC |
1468 | $icon->pix = 'i/agg_sum'; |
1469 | break; | |
6cc3e350 | 1470 | } |
1471 | } | |
1472 | ||
1473 | } else if ($element['object']->itemtype == 'mod') { | |
1ef48469 | 1474 | // Prevent outcomes displaying the same icon as the activity they are attached to. |
0077f571 | 1475 | if ($is_outcome) { |
1ef48469 JC |
1476 | $icon->pix = 'i/outcomes'; |
1477 | $icon->title = s(get_string('outcome', 'grades')); | |
0077f571 | 1478 | } else { |
1ef48469 JC |
1479 | $icon->pix = 'icon'; |
1480 | $icon->component = $element['object']->itemmodule; | |
1481 | $icon->title = s(get_string('modulename', $element['object']->itemmodule)); | |
0077f571 | 1482 | } |
6cc3e350 | 1483 | } else if ($element['object']->itemtype == 'manual') { |
1484 | if ($element['object']->is_outcome_item()) { | |
1ef48469 JC |
1485 | $icon->pix = 'i/outcomes'; |
1486 | $icon->title = s(get_string('outcome', 'grades')); | |
6cc3e350 | 1487 | } else { |
1ef48469 JC |
1488 | $icon->pix = 'i/manual_item'; |
1489 | $icon->title = s(get_string('manualitem', 'grades')); | |
6cc3e350 | 1490 | } |
1491 | } | |
1492 | break; | |
1493 | ||
1494 | case 'category': | |
1ef48469 JC |
1495 | $none = false; |
1496 | $icon->pix = 'i/folder'; | |
1497 | $icon->title = s(get_string('category', 'grades')); | |
1498 | break; | |
6cc3e350 | 1499 | } |
1500 | ||
1ef48469 JC |
1501 | if ($none) { |
1502 | if ($spacerifnone) { | |
1503 | $outputstr = $OUTPUT->spacer() . ' '; | |
1504 | } | |
6cc3e350 | 1505 | } else { |
1ef48469 | 1506 | $outputstr = $OUTPUT->pix_icon($icon->pix, $icon->title, $icon->component, $icon->attributes); |
6cc3e350 | 1507 | } |
1ef48469 JC |
1508 | |
1509 | return $outputstr; | |
6cc3e350 | 1510 | } |
6391ebe7 | 1511 | |
e98871a2 | 1512 | /** |
6cc3e350 | 1513 | * Returns name of element optionally with icon and link |
cf72e2dd | 1514 | * |
1515 | * @param array &$element An array representing an element in the grade_tree | |
1516 | * @param bool $withlink Whether or not this header has a link | |
1517 | * @param bool $icon Whether or not to display an icon with this header | |
1518 | * @param bool $spacerifnone return spacer if no icon found | |
65c2ac93 | 1519 | * @param bool $withdescription Show description if defined by this item. |
41e62a90 AN |
1520 | * @param bool $fulltotal If the item is a category total, returns $categoryname."total" |
1521 | * instead of "Category total" or "Course total" | |
cf72e2dd | 1522 | * |
1523 | * @return string header | |
e98871a2 | 1524 | */ |
41e62a90 AN |
1525 | public function get_element_header(&$element, $withlink = false, $icon = true, $spacerifnone = false, |
1526 | $withdescription = false, $fulltotal = false) { | |
6cc3e350 | 1527 | $header = ''; |
1528 | ||
1529 | if ($icon) { | |
1530 | $header .= $this->get_element_icon($element, $spacerifnone); | |
1531 | } | |
1532 | ||
41e62a90 | 1533 | $header .= $element['object']->get_name($fulltotal); |
6cc3e350 | 1534 | |
cf72e2dd | 1535 | if ($element['type'] != 'item' and $element['type'] != 'categoryitem' and |
1536 | $element['type'] != 'courseitem') { | |
6cc3e350 | 1537 | return $header; |
1538 | } | |
1539 | ||
43cd76e8 JC |
1540 | if ($withlink && $url = $this->get_activity_link($element)) { |
1541 | $a = new stdClass(); | |
1542 | $a->name = get_string('modulename', $element['object']->itemmodule); | |
1543 | $title = get_string('linktoactivity', 'grades', $a); | |
1544 | ||
1545 | $header = html_writer::link($url, $header, array('title' => $title)); | |
1546 | } else { | |
1547 | $header = html_writer::span($header); | |
6cc3e350 | 1548 | } |
1549 | ||
65c2ac93 DW |
1550 | if ($withdescription) { |
1551 | $desc = $element['object']->get_description(); | |
1552 | if (!empty($desc)) { | |
1553 | $header .= '<div class="gradeitemdescription">' . s($desc) . '</div><div class="gradeitemdescriptionfiller"></div>'; | |
1554 | } | |
1555 | } | |
1556 | ||
6cc3e350 | 1557 | return $header; |
1558 | } | |
1559 | ||
73ca5f01 | 1560 | private function get_activity_link($element) { |
1561 | global $CFG; | |
eff314b0 DM |
1562 | /** @var array static cache of the grade.php file existence flags */ |
1563 | static $hasgradephp = array(); | |
73ca5f01 | 1564 | |
1565 | $itemtype = $element['object']->itemtype; | |
1566 | $itemmodule = $element['object']->itemmodule; | |
1567 | $iteminstance = $element['object']->iteminstance; | |
25ab1d50 | 1568 | $itemnumber = $element['object']->itemnumber; |
73ca5f01 | 1569 | |
1570 | // Links only for module items that have valid instance, module and are | |
1571 | // called from grade_tree with valid modinfo | |
1572 | if ($itemtype != 'mod' || !$iteminstance || !$itemmodule || !$this->modinfo) { | |
1573 | return null; | |
1574 | } | |
1575 | ||
1576 | // Get $cm efficiently and with visibility information using modinfo | |
1577 | $instances = $this->modinfo->get_instances(); | |
1578 | if (empty($instances[$itemmodule][$iteminstance])) { | |
1579 | return null; | |
1580 | } | |
1581 | $cm = $instances[$itemmodule][$iteminstance]; | |
1582 | ||
1583 | // Do not add link if activity is not visible to the current user | |
1584 | if (!$cm->uservisible) { | |
1585 | return null; | |
1586 | } | |
1587 | ||
eff314b0 DM |
1588 | if (!array_key_exists($itemmodule, $hasgradephp)) { |
1589 | if (file_exists($CFG->dirroot . '/mod/' . $itemmodule . '/grade.php')) { | |
1590 | $hasgradephp[$itemmodule] = true; | |
1591 | } else { | |
1592 | $hasgradephp[$itemmodule] = false; | |
1593 | } | |
1594 | } | |
1595 | ||
73ca5f01 | 1596 | // If module has grade.php, link to that, otherwise view.php |
eff314b0 | 1597 | if ($hasgradephp[$itemmodule]) { |
c97933ff MG |
1598 | $args = array('id' => $cm->id, 'itemnumber' => $itemnumber); |
1599 | if (isset($element['userid'])) { | |
1600 | $args['userid'] = $element['userid']; | |
1601 | } | |
1602 | return new moodle_url('/mod/' . $itemmodule . '/grade.php', $args); | |
73ca5f01 | 1603 | } else { |
1604 | return new moodle_url('/mod/' . $itemmodule . '/view.php', array('id' => $cm->id)); | |
1605 | } | |
1606 | } | |
1607 | ||
bb17ac1e DM |
1608 | /** |
1609 | * Returns URL of a page that is supposed to contain detailed grade analysis | |
1610 | * | |
bb17ac1e DM |
1611 | * At the moment, only activity modules are supported. The method generates link |
1612 | * to the module's file grade.php with the parameters id (cmid), itemid, itemnumber, | |
eff314b0 | 1613 | * gradeid and userid. If the grade.php does not exist, null is returned. |
bb17ac1e DM |
1614 | * |
1615 | * @return moodle_url|null URL or null if unable to construct it | |
1616 | */ | |
1617 | public function get_grade_analysis_url(grade_grade $grade) { | |
eff314b0 DM |
1618 | global $CFG; |
1619 | /** @var array static cache of the grade.php file existence flags */ | |
1620 | static $hasgradephp = array(); | |
bb17ac1e DM |
1621 | |
1622 | if (empty($grade->grade_item) or !($grade->grade_item instanceof grade_item)) { | |
1623 | throw new coding_exception('Passed grade without the associated grade item'); | |
1624 | } | |
1625 | $item = $grade->grade_item; | |
1626 | ||
1627 | if (!$item->is_external_item()) { | |
1628 | // at the moment, only activity modules are supported | |
1629 | return null; | |
1630 | } | |
1631 | if ($item->itemtype !== 'mod') { | |
1632 | throw new coding_exception('Unknown external itemtype: '.$item->itemtype); | |
1633 | } | |
1634 | if (empty($item->iteminstance) or empty($item->itemmodule) or empty($this->modinfo)) { | |
1635 | return null; | |
1636 | } | |
1637 | ||
eff314b0 DM |
1638 | if (!array_key_exists($item->itemmodule, $hasgradephp)) { |
1639 | if (file_exists($CFG->dirroot . '/mod/' . $item->itemmodule . '/grade.php')) { | |
1640 | $hasgradephp[$item->itemmodule] = true; | |
1641 | } else { | |
1642 | $hasgradephp[$item->itemmodule] = false; | |
1643 | } | |
1644 | } | |
1645 | ||
1646 | if (!$hasgradephp[$item->itemmodule]) { | |
1647 | return null; | |
1648 | } | |
1649 | ||
bb17ac1e DM |
1650 | $instances = $this->modinfo->get_instances(); |
1651 | if (empty($instances[$item->itemmodule][$item->iteminstance])) { | |
1652 | return null; | |
1653 | } | |
1654 | $cm = $instances[$item->itemmodule][$item->iteminstance]; | |
1655 | if (!$cm->uservisible) { | |
1656 | return null; | |
1657 | } | |
1658 | ||
1659 | $url = new moodle_url('/mod/'.$item->itemmodule.'/grade.php', array( | |
1660 | 'id' => $cm->id, | |
1661 | 'itemid' => $item->id, | |
1662 | 'itemnumber' => $item->itemnumber, | |
1663 | 'gradeid' => $grade->id, | |
1664 | 'userid' => $grade->userid, | |
1665 | )); | |
1666 | ||
1667 | return $url; | |
1668 | } | |
1669 | ||
1670 | /** | |
1671 | * Returns an action icon leading to the grade analysis page | |
1672 | * | |
1673 | * @param grade_grade $grade | |
1674 | * @return string | |
1675 | */ | |
1676 | public function get_grade_analysis_icon(grade_grade $grade) { | |
1677 | global $OUTPUT; | |
1678 | ||
1679 | $url = $this->get_grade_analysis_url($grade); | |
1680 | if (is_null($url)) { | |
1681 | return ''; | |
1682 | } | |
1683 | ||
c97933ff | 1684 | return $OUTPUT->action_icon($url, new pix_icon('t/preview', |
bb17ac1e DM |
1685 | get_string('gradeanalysis', 'core_grades'))); |
1686 | } | |
1687 | ||
6cc3e350 | 1688 | /** |
1689 | * Returns the grade eid - the grade may not exist yet. | |
cf72e2dd | 1690 | * |
1691 | * @param grade_grade $grade_grade A grade_grade object | |
1692 | * | |
6cc3e350 | 1693 | * @return string eid |
1694 | */ | |
d24832f9 | 1695 | public function get_grade_eid($grade_grade) { |
6cc3e350 | 1696 | if (empty($grade_grade->id)) { |
1697 | return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid; | |
1698 | } else { | |
1699 | return 'g'.$grade_grade->id; | |
1700 | } | |
1701 | } | |
1702 | ||
1703 | /** | |
1704 | * Returns the grade_item eid | |
cf72e2dd | 1705 | * @param grade_item $grade_item A grade_item object |
6cc3e350 | 1706 | * @return string eid |
1707 | */ | |
d24832f9 | 1708 | public function get_item_eid($grade_item) { |
ddb9c95f | 1709 | return 'ig'.$grade_item->id; |
6cc3e350 | 1710 | } |
1711 | ||
cf72e2dd | 1712 | /** |
1713 | * Given a grade_tree element, returns an array of parameters | |
1714 | * used to build an icon for that element. | |
1715 | * | |
1716 | * @param array $element An array representing an element in the grade_tree | |
1717 | * | |
1718 | * @return array | |
1719 | */ | |
1720 | public function get_params_for_iconstr($element) { | |
9ecd4386 | 1721 | $strparams = new stdClass(); |
1722 | $strparams->category = ''; | |
1723 | $strparams->itemname = ''; | |
1724 | $strparams->itemmodule = ''; | |
cf72e2dd | 1725 | |
9ecd4386 | 1726 | if (!method_exists($element['object'], 'get_name')) { |
1727 | return $strparams; | |
1728 | } | |
345674ca | 1729 | |
b285c5aa | 1730 | $strparams->itemname = html_to_text($element['object']->get_name()); |
9ecd4386 | 1731 | |
1732 | // If element name is categorytotal, get the name of the parent category | |
1733 | if ($strparams->itemname == get_string('categorytotal', 'grades')) { | |
1734 | $parent = $element['object']->get_parent_category(); | |
1735 | $strparams->category = $parent->get_name() . ' '; | |
1736 | } else { | |
1737 | $strparams->category = ''; | |
1738 | } | |
1739 | ||
1740 | $strparams->itemmodule = null; | |
1741 | if (isset($element['object']->itemmodule)) { | |
1742 | $strparams->itemmodule = $element['object']->itemmodule; | |
345674ca | 1743 | } |
9ecd4386 | 1744 | return $strparams; |
1745 | } | |
1746 | ||
ae93f353 FM |
1747 | /** |
1748 | * Return a reset icon for the given element. | |
1749 | * | |
1750 | * @param array $element An array representing an element in the grade_tree | |
1751 | * @param object $gpr A grade_plugin_return object | |
079b2e52 MG |
1752 | * @param bool $returnactionmenulink return the instance of action_menu_link instead of string |
1753 | * @return string|action_menu_link | |
ae93f353 | 1754 | */ |
079b2e52 | 1755 | public function get_reset_icon($element, $gpr, $returnactionmenulink = false) { |
ae93f353 FM |
1756 | global $CFG, $OUTPUT; |
1757 | ||
1758 | // Limit to category items set to use the natural weights aggregation method, and users | |
1759 | // with the capability to manage grades. | |
1760 | if ($element['type'] != 'category' || $element['object']->aggregation != GRADE_AGGREGATE_SUM || | |
1761 | !has_capability('moodle/grade:manage', $this->context)) { | |
079b2e52 | 1762 | return $returnactionmenulink ? null : ''; |
ae93f353 FM |
1763 | } |
1764 | ||
1765 | $str = get_string('resetweights', 'grades', $this->get_params_for_iconstr($element)); | |
1766 | $url = new moodle_url('/grade/edit/tree/action.php', array( | |
1767 | 'id' => $this->courseid, | |
1768 | 'action' => 'resetweights', | |
1769 | 'eid' => $element['eid'], | |
1770 | 'sesskey' => sesskey(), | |
1771 | )); | |
1772 | ||
079b2e52 MG |
1773 | if ($returnactionmenulink) { |
1774 | return new action_menu_link_secondary($gpr->add_url_params($url), new pix_icon('t/reset', $str), | |
1775 | get_string('resetweightsshort', 'grades')); | |
1776 | } else { | |
1777 | return $OUTPUT->action_icon($gpr->add_url_params($url), new pix_icon('t/reset', $str)); | |
1778 | } | |
ae93f353 FM |
1779 | } |
1780 | ||
6cc3e350 | 1781 | /** |
1782 | * Return edit icon for give element | |
cf72e2dd | 1783 | * |
1784 | * @param array $element An array representing an element in the grade_tree | |
1785 | * @param object $gpr A grade_plugin_return object | |
079b2e52 MG |
1786 | * @param bool $returnactionmenulink return the instance of action_menu_link instead of string |
1787 | * @return string|action_menu_link | |
6cc3e350 | 1788 | */ |
079b2e52 | 1789 | public function get_edit_icon($element, $gpr, $returnactionmenulink = false) { |
6b608f8f | 1790 | global $CFG, $OUTPUT; |
6cc3e350 | 1791 | |
1792 | if (!has_capability('moodle/grade:manage', $this->context)) { | |
1793 | if ($element['type'] == 'grade' and has_capability('moodle/grade:edit', $this->context)) { | |
1794 | // oki - let them override grade | |
1795 | } else { | |
079b2e52 | 1796 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 1797 | } |
1798 | } | |
1799 | ||
05aba805 | 1800 | static $strfeedback = null; |
1801 | static $streditgrade = null; | |
1802 | if (is_null($streditgrade)) { | |
1803 | $streditgrade = get_string('editgrade', 'grades'); | |
1804 | $strfeedback = get_string('feedback'); | |
6cc3e350 | 1805 | } |
1806 | ||
9ecd4386 | 1807 | $strparams = $this->get_params_for_iconstr($element); |
6ef4878b | 1808 | |
6cc3e350 | 1809 | $object = $element['object']; |
6cc3e350 | 1810 | |
1811 | switch ($element['type']) { | |
1812 | case 'item': | |
1813 | case 'categoryitem': | |
1814 | case 'courseitem': | |
9ecd4386 | 1815 | $stredit = get_string('editverbose', 'grades', $strparams); |
6cc3e350 | 1816 | if (empty($object->outcomeid) || empty($CFG->enableoutcomes)) { |
a6855934 | 1817 | $url = new moodle_url('/grade/edit/tree/item.php', |
1c1f64a2 | 1818 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1819 | } else { |
a6855934 | 1820 | $url = new moodle_url('/grade/edit/tree/outcomeitem.php', |
1c1f64a2 | 1821 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1822 | } |
6cc3e350 | 1823 | break; |
1824 | ||
1825 | case 'category': | |
9ecd4386 | 1826 | $stredit = get_string('editverbose', 'grades', $strparams); |
a6855934 | 1827 | $url = new moodle_url('/grade/edit/tree/category.php', |
1c1f64a2 | 1828 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1829 | break; |
1830 | ||
1831 | case 'grade': | |
05aba805 | 1832 | $stredit = $streditgrade; |
6cc3e350 | 1833 | if (empty($object->id)) { |
a6855934 | 1834 | $url = new moodle_url('/grade/edit/tree/grade.php', |
1c1f64a2 | 1835 | array('courseid' => $this->courseid, 'itemid' => $object->itemid, 'userid' => $object->userid)); |
6cc3e350 | 1836 | } else { |
a6855934 | 1837 | $url = new moodle_url('/grade/edit/tree/grade.php', |
1c1f64a2 | 1838 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1839 | } |
6cc3e350 | 1840 | if (!empty($object->feedback)) { |
1841 | $feedback = addslashes_js(trim(format_string($object->feedback, $object->feedbackformat))); | |
6cc3e350 | 1842 | } |
1843 | break; | |
1844 | ||
1845 | default: | |
1846 | $url = null; | |
1847 | } | |
1848 | ||
1849 | if ($url) { | |
079b2e52 MG |
1850 | if ($returnactionmenulink) { |
1851 | return new action_menu_link_secondary($gpr->add_url_params($url), | |
1852 | new pix_icon('t/edit', $stredit), | |
1853 | get_string('editsettings')); | |
1854 | } else { | |
1855 | return $OUTPUT->action_icon($gpr->add_url_params($url), new pix_icon('t/edit', $stredit)); | |
1856 | } | |
6cc3e350 | 1857 | |
1858 | } else { | |
079b2e52 | 1859 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 1860 | } |
1861 | } | |
1862 | ||
1863 | /** | |
1864 | * Return hiding icon for give element | |
cf72e2dd | 1865 | * |
1866 | * @param array $element An array representing an element in the grade_tree | |
1867 | * @param object $gpr A grade_plugin_return object | |
079b2e52 MG |
1868 | * @param bool $returnactionmenulink return the instance of action_menu_link instead of string |
1869 | * @return string|action_menu_link | |
6cc3e350 | 1870 | */ |
079b2e52 | 1871 | public function get_hiding_icon($element, $gpr, $returnactionmenulink = false) { |
5d3b9994 | 1872 | global $CFG, $OUTPUT; |
6cc3e350 | 1873 | |
39873128 | 1874 | if (!$element['object']->can_control_visibility()) { |
079b2e52 | 1875 | return $returnactionmenulink ? null : ''; |
39873128 TH |
1876 | } |
1877 | ||
cf72e2dd | 1878 | if (!has_capability('moodle/grade:manage', $this->context) and |
1879 | !has_capability('moodle/grade:hide', $this->context)) { | |
079b2e52 | 1880 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 1881 | } |
1882 | ||
345674ca | 1883 | $strparams = $this->get_params_for_iconstr($element); |
9ecd4386 | 1884 | $strshow = get_string('showverbose', 'grades', $strparams); |
345674ca | 1885 | $strhide = get_string('hideverbose', 'grades', $strparams); |
6cc3e350 | 1886 | |
a6855934 | 1887 | $url = new moodle_url('/grade/edit/tree/action.php', array('id' => $this->courseid, 'sesskey' => sesskey(), 'eid' => $element['eid'])); |
8ae8bf8a | 1888 | $url = $gpr->add_url_params($url); |
1c1f64a2 | 1889 | |
6cc3e350 | 1890 | if ($element['object']->is_hidden()) { |
8ae8bf8a | 1891 | $type = 'show'; |
6cc3e350 | 1892 | $tooltip = $strshow; |
1893 | ||
cf72e2dd | 1894 | // Change the icon and add a tooltip showing the date |
1895 | if ($element['type'] != 'category' and $element['object']->get_hidden() > 1) { | |
8ae8bf8a | 1896 | $type = 'hiddenuntil'; |
cf72e2dd | 1897 | $tooltip = get_string('hiddenuntildate', 'grades', |
1898 | userdate($element['object']->get_hidden())); | |
6cc3e350 | 1899 | } |
1900 | ||
8ae8bf8a | 1901 | $url->param('action', 'show'); |
b9cc756e | 1902 | |
079b2e52 MG |
1903 | if ($returnactionmenulink) { |
1904 | $hideicon = new action_menu_link_secondary($url, new pix_icon('t/'.$type, $tooltip), get_string('show')); | |
1905 | } else { | |
1906 | $hideicon = $OUTPUT->action_icon($url, new pix_icon('t/'.$type, $tooltip, 'moodle', array('alt'=>$strshow, 'class'=>'smallicon'))); | |
1907 | } | |
6cc3e350 | 1908 | |
1909 | } else { | |
8ae8bf8a | 1910 | $url->param('action', 'hide'); |
079b2e52 MG |
1911 | if ($returnactionmenulink) { |
1912 | $hideicon = new action_menu_link_secondary($url, new pix_icon('t/hide', $strhide), get_string('hide')); | |
1913 | } else { | |
1914 | $hideicon = $OUTPUT->action_icon($url, new pix_icon('t/hide', $strhide)); | |
1915 | } | |
6cc3e350 | 1916 | } |
1c1f64a2 | 1917 | |
8ae8bf8a | 1918 | return $hideicon; |
6cc3e350 | 1919 | } |
1920 | ||
1921 | /** | |
2673c733 | 1922 | * Return locking icon for given element |
cf72e2dd | 1923 | * |
1924 | * @param array $element An array representing an element in the grade_tree | |
1925 | * @param object $gpr A grade_plugin_return object | |
1926 | * | |
6cc3e350 | 1927 | * @return string |
1928 | */ | |
d24832f9 | 1929 | public function get_locking_icon($element, $gpr) { |
6b608f8f | 1930 | global $CFG, $OUTPUT; |
6cc3e350 | 1931 | |
345674ca | 1932 | $strparams = $this->get_params_for_iconstr($element); |
9ecd4386 | 1933 | $strunlock = get_string('unlockverbose', 'grades', $strparams); |
1934 | $strlock = get_string('lockverbose', 'grades', $strparams); | |
d24832f9 | 1935 | |
a6855934 | 1936 | $url = new moodle_url('/grade/edit/tree/action.php', array('id' => $this->courseid, 'sesskey' => sesskey(), 'eid' => $element['eid'])); |
8ae8bf8a | 1937 | $url = $gpr->add_url_params($url); |
1c1f64a2 | 1938 | |
2673c733 | 1939 | // Don't allow an unlocking action for a grade whose grade item is locked: just print a state icon |
1940 | if ($element['type'] == 'grade' && $element['object']->grade_item->is_locked()) { | |
1941 | $strparamobj = new stdClass(); | |
1942 | $strparamobj->itemname = $element['object']->grade_item->itemname; | |
1943 | $strnonunlockable = get_string('nonunlockableverbose', 'grades', $strparamobj); | |
1c1f64a2 | 1944 | |
0cddd851 FM |
1945 | $action = html_writer::tag('span', $OUTPUT->pix_icon('t/locked', $strnonunlockable), |
1946 | array('class' => 'action-icon')); | |
8ae8bf8a | 1947 | |
cf72e2dd | 1948 | } else if ($element['object']->is_locked()) { |
8ae8bf8a | 1949 | $type = 'unlock'; |
6cc3e350 | 1950 | $tooltip = $strunlock; |
1951 | ||
cf72e2dd | 1952 | // Change the icon and add a tooltip showing the date |
1953 | if ($element['type'] != 'category' and $element['object']->get_locktime() > 1) { | |
8ae8bf8a | 1954 | $type = 'locktime'; |
cf72e2dd | 1955 | $tooltip = get_string('locktimedate', 'grades', |
1956 | userdate($element['object']->get_locktime())); | |
6cc3e350 | 1957 | } |
1958 | ||
8ae8bf8a PS |
1959 | if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:unlock', $this->context)) { |
1960 | $action = ''; | |
1961 | } else { | |
1962 | $url->param('action', 'unlock'); | |
c63923bd | 1963 | $action = $OUTPUT->action_icon($url, new pix_icon('t/'.$type, $tooltip, 'moodle', array('alt'=>$strunlock, 'class'=>'smallicon'))); |
6cc3e350 | 1964 | } |
6cc3e350 | 1965 | |
1966 | } else { | |
8ae8bf8a PS |
1967 | if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:lock', $this->context)) { |
1968 | $action = ''; | |
1969 | } else { | |
1970 | $url->param('action', 'lock'); | |
c63923bd | 1971 | $action = $OUTPUT->action_icon($url, new pix_icon('t/lock', $strlock)); |
6cc3e350 | 1972 | } |
6cc3e350 | 1973 | } |
8ae8bf8a | 1974 | |
6cc3e350 | 1975 | return $action; |
1976 | } | |
1977 | ||
1978 | /** | |
1979 | * Return calculation icon for given element | |
cf72e2dd | 1980 | * |
1981 | * @param array $element An array representing an element in the grade_tree | |
1982 | * @param object $gpr A grade_plugin_return object | |
079b2e52 MG |
1983 | * @param bool $returnactionmenulink return the instance of action_menu_link instead of string |
1984 | * @return string|action_menu_link | |
6cc3e350 | 1985 | */ |
079b2e52 | 1986 | public function get_calculation_icon($element, $gpr, $returnactionmenulink = false) { |
666e8458 | 1987 | global $CFG, $OUTPUT; |
6cc3e350 | 1988 | if (!has_capability('moodle/grade:manage', $this->context)) { |
079b2e52 | 1989 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 1990 | } |
1991 | ||
6cc3e350 | 1992 | $type = $element['type']; |
1993 | $object = $element['object']; | |
1994 | ||
1995 | if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') { | |
345674ca | 1996 | $strparams = $this->get_params_for_iconstr($element); |
9ecd4386 | 1997 | $streditcalculation = get_string('editcalculationverbose', 'grades', $strparams); |
6cc3e350 | 1998 | |
cf72e2dd | 1999 | $is_scale = $object->gradetype == GRADE_TYPE_SCALE; |
2000 | $is_value = $object->gradetype == GRADE_TYPE_VALUE; | |
2001 | ||
6cc3e350 | 2002 | // show calculation icon only when calculation possible |
cf72e2dd | 2003 | if (!$object->is_external_item() and ($is_scale or $is_value)) { |
6cc3e350 | 2004 | if ($object->is_calculated()) { |
666e8458 | 2005 | $icon = 't/calc'; |
6cc3e350 | 2006 | } else { |
666e8458 | 2007 | $icon = 't/calc_off'; |
6cc3e350 | 2008 | } |
cf72e2dd | 2009 | |
a6855934 | 2010 | $url = new moodle_url('/grade/edit/tree/calculation.php', array('courseid' => $this->courseid, 'id' => $object->id)); |
8ae8bf8a | 2011 | $url = $gpr->add_url_params($url); |
079b2e52 MG |
2012 | if ($returnactionmenulink) { |
2013 | return new action_menu_link_secondary($url, | |
2014 | new pix_icon($icon, $streditcalculation), | |
2015 | get_string('editcalculation', 'grades')); | |
2016 | } else { | |
2017 | return $OUTPUT->action_icon($url, new pix_icon($icon, $streditcalculation)); | |
2018 | } | |
6cc3e350 | 2019 | } |
2020 | } | |
2021 | ||
079b2e52 | 2022 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 2023 | } |
2024 | } | |
2025 | ||
2026 | /** | |
2027 | * Flat structure similar to grade tree. | |
cf72e2dd | 2028 | * |
2029 | * @uses grade_structure | |
a153c9f2 | 2030 | * @package core_grades |
cf72e2dd | 2031 | * @copyright 2009 Nicolas Connault |
2032 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
6cc3e350 | 2033 | */ |
2034 | class grade_seq extends grade_structure { | |
2035 | ||
6cc3e350 | 2036 | /** |
2037 | * 1D array of elements | |
2038 | */ | |
d24832f9 | 2039 | public $elements; |
e98871a2 | 2040 | |
2041 | /** | |
2042 | * Constructor, retrieves and stores array of all grade_category and grade_item | |
2043 | * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed. | |
cf72e2dd | 2044 | * |
2045 | * @param int $courseid The course id | |
2046 | * @param bool $category_grade_last category grade item is the last child | |
2047 | * @param bool $nooutcomes Whether or not outcomes should be included | |
e98871a2 | 2048 | */ |
d24832f9 | 2049 | public function grade_seq($courseid, $category_grade_last=false, $nooutcomes=false) { |
e98871a2 | 2050 | global $USER, $CFG; |
2051 | ||
2052 | $this->courseid = $courseid; | |
d4060472 | 2053 | $this->context = context_course::instance($courseid); |
e98871a2 | 2054 | |
2055 | // get course grade tree | |
2056 | $top_element = grade_category::fetch_course_tree($courseid, true); | |
2057 | ||
6391ebe7 | 2058 | $this->elements = grade_seq::flatten($top_element, $category_grade_last, $nooutcomes); |
2059 | ||
2060 | foreach ($this->elements as $key=>$unused) { | |
b89a70ce | 2061 | $this->items[$this->elements[$key]['object']->id] =& $this->elements[$key]['object']; |
6391ebe7 | 2062 | } |
e98871a2 | 2063 | } |
2064 | ||
2065 | /** | |
2066 | * Static recursive helper - makes the grade_item for category the last children | |
cf72e2dd | 2067 | * |
2068 | * @param array &$element The seed of the recursion | |
2069 | * @param bool $category_grade_last category grade item is the last child | |
2070 | * @param bool $nooutcomes Whether or not outcomes should be included | |
2071 | * | |
2072 | * @return array | |
e98871a2 | 2073 | */ |
d24832f9 | 2074 | public function flatten(&$element, $category_grade_last, $nooutcomes) { |
e98871a2 | 2075 | if (empty($element['children'])) { |
2076 | return array(); | |
2077 | } | |
2078 | $children = array(); | |
2079 | ||
2080 | foreach ($element['children'] as $sortorder=>$unused) { | |
cf72e2dd | 2081 | if ($nooutcomes and $element['type'] != 'category' and |
2082 | $element['children'][$sortorder]['object']->is_outcome_item()) { | |
e98871a2 | 2083 | continue; |
2084 | } | |
2085 | $children[] = $element['children'][$sortorder]; | |
2086 | } | |
2087 | unset($element['children']); | |
2088 | ||
2089 | if ($category_grade_last and count($children) > 1) { | |
2090 | $cat_item = array_shift($children); | |
2091 | array_push($children, $cat_item); | |
2092 | } | |
2093 | ||
2094 | $result = array(); | |
2095 | foreach ($children as $child) { | |
e0724506 | 2096 | if ($child['type'] == 'category') { |
6391ebe7 | 2097 | $result = $result + grade_seq::flatten($child, $category_grade_last, $nooutcomes); |
e98871a2 | 2098 | } else { |
2099 | $child['eid'] = 'i'.$child['object']->id; | |
6391ebe7 | 2100 | $result[$child['object']->id] = $child; |
e98871a2 | 2101 | } |
2102 | } | |
2103 | ||
2104 | return $result; | |
2105 | } | |
2106 | ||
2107 | /** | |
2108 | * Parses the array in search of a given eid and returns a element object with | |
2109 | * information about the element it has found. | |
cf72e2dd | 2110 | * |
2111 | * @param int $eid Gradetree Element ID | |
2112 | * | |
e98871a2 | 2113 | * @return object element |
2114 | */ | |
d24832f9 | 2115 | public function locate_element($eid) { |
e98871a2 | 2116 | // it is a grade - construct a new object |
2117 | if (strpos($eid, 'n') === 0) { | |
2118 | if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) { | |
2119 | return null; | |
2120 | } | |
2121 | ||
2122 | $itemid = $matches[1]; | |
2123 | $userid = $matches[2]; | |
2124 | ||
2125 | //extra security check - the grade item must be in this tree | |
ddb9c95f | 2126 | if (!$item_el = $this->locate_element('ig'.$itemid)) { |
e98871a2 | 2127 | return null; |
2128 | } | |
2129 | ||
2130 | // $gradea->id may be null - means does not exist yet | |
2131 | $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid)); | |
2132 | ||
2133 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
2134 | return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade'); | |
2135 | ||
2136 | } else if (strpos($eid, 'g') === 0) { | |
cf72e2dd | 2137 | $id = (int) substr($eid, 1); |
e98871a2 | 2138 | if (!$grade = grade_grade::fetch(array('id'=>$id))) { |
2139 | return null; | |
2140 | } | |
2141 | //extra security check - the grade item must be in this tree | |
ddb9c95f | 2142 | if (!$item_el = $this->locate_element('ig'.$grade->itemid)) { |
e98871a2 | 2143 | return null; |
2144 | } | |
2145 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
2146 | return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade'); | |
2147 | } | |
2148 | ||
2149 | // it is a category or item | |
6391ebe7 | 2150 | foreach ($this->elements as $element) { |
6cc3e350 | 2151 | if ($element['eid'] == $eid) { |
2152 | return $element; | |
2153 | } | |
e98871a2 | 2154 | } |
6cc3e350 | 2155 | |
2156 | return null; | |
e98871a2 | 2157 | } |
e98871a2 | 2158 | } |
2159 | ||
7a6b7acf | 2160 | /** |
2161 | * This class represents a complete tree of categories, grade_items and final grades, | |
2162 | * organises as an array primarily, but which can also be converted to other formats. | |
2163 | * It has simple method calls with complex implementations, allowing for easy insertion, | |
2164 | * deletion and moving of items and categories within the tree. | |
cf72e2dd | 2165 | * |
2166 | * @uses grade_structure | |
a153c9f2 | 2167 | * @package core_grades |
cf72e2dd | 2168 | * @copyright 2009 Nicolas Connault |
2169 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
7a6b7acf | 2170 | */ |
6cc3e350 | 2171 | class grade_tree extends grade_structure { |
7a6b7acf | 2172 | |
2173 | /** | |
2174 | * The basic representation of the tree as a hierarchical, 3-tiered array. | |
2175 | * @var object $top_element | |
2176 | */ | |
d24832f9 | 2177 | public $top_element; |
7a6b7acf | 2178 | |
7a6b7acf | 2179 | /** |
2180 | * 2D array of grade items and categories | |
cf72e2dd | 2181 | * @var array $levels |
7a6b7acf | 2182 | */ |
d24832f9 | 2183 | public $levels; |
7a6b7acf | 2184 | |
b89a70ce | 2185 | /** |
2186 | * Grade items | |
cf72e2dd | 2187 | * @var array $items |
b89a70ce | 2188 | */ |
d24832f9 | 2189 | public $items; |
b89a70ce | 2190 | |
7a6b7acf | 2191 | /** |
2192 | * Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item | |
e98871a2 | 2193 | * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed. |
cf72e2dd | 2194 | * |
2195 | * @param int $courseid The Course ID | |
2196 | * @param bool $fillers include fillers and colspans, make the levels var "rectangular" | |
2197 | * @param bool $category_grade_last category grade item is the last child | |
4faf5f99 | 2198 | * @param array $collapsed array of collapsed categories |
cf72e2dd | 2199 | * @param bool $nooutcomes Whether or not outcomes should be included |
7a6b7acf | 2200 | */ |
cf72e2dd | 2201 | public function grade_tree($courseid, $fillers=true, $category_grade_last=false, |
2202 | $collapsed=null, $nooutcomes=false) { | |
73ca5f01 | 2203 | global $USER, $CFG, $COURSE, $DB; |
7a6b7acf | 2204 | |
2205 | $this->courseid = $courseid; | |
7a6b7acf | 2206 | $this->levels = array(); |
d4060472 | 2207 | $this->context = context_course::instance($courseid); |
7a6b7acf | 2208 | |
73ca5f01 | 2209 | if (!empty($COURSE->id) && $COURSE->id == $this->courseid) { |
2210 | $course = $COURSE; | |
2211 | } else { | |
2212 | $course = $DB->get_record('course', array('id' => $this->courseid)); | |
2213 | } | |
2214 | $this->modinfo = get_fast_modinfo($course); | |
2215 | ||
7a6b7acf | 2216 | // get course grade tree |
2217 | $this->top_element = grade_category::fetch_course_tree($courseid, true); | |
2218 | ||
4faf5f99 | 2219 | // collapse the categories if requested |
2220 | if (!empty($collapsed)) { | |
2221 | grade_tree::category_collapse($this->top_element, $collapsed); | |
2222 | } | |
2223 | ||
aea4df41 | 2224 | // no otucomes if requested |
2225 | if (!empty($nooutcomes)) { | |
2226 | grade_tree::no_outcomes($this->top_element); | |
2227 | } | |
2228 | ||
4faf5f99 | 2229 | // move category item to last position in category |
7a6b7acf | 2230 | if ($category_grade_last) { |
2231 | grade_tree::category_grade_last($this->top_element); | |
2232 | } | |
2233 | ||
2234 | if ($fillers) { | |
2235 | // inject fake categories == fillers | |
2236 | grade_tree::inject_fillers($this->top_element, 0); | |
2237 | // add colspans to categories and fillers | |
2238 | grade_tree::inject_colspans($this->top_element); | |
2239 | } | |
2240 | ||
2241 | grade_tree::fill_levels($this->levels, $this->top_element, 0); | |
d297269d | 2242 | |
7a6b7acf | 2243 | } |
2244 | ||
4faf5f99 | 2245 | /** |
2246 | * Static recursive helper - removes items from collapsed categories | |
cf72e2dd | 2247 | * |
2248 | * @param array &$element The seed of the recursion | |
4faf5f99 | 2249 | * @param array $collapsed array of collapsed categories |
cf72e2dd | 2250 | * |
4faf5f99 | 2251 | * @return void |
2252 | */ | |
d24832f9 | 2253 | public function category_collapse(&$element, $collapsed) { |
4faf5f99 | 2254 | if ($element['type'] != 'category') { |
2255 | return; | |
2256 | } | |
2257 | if (empty($element['children']) or count($element['children']) < 2) { | |
2258 | return; | |
2259 | } | |
2260 | ||
384960dd | 2261 | if (in_array($element['object']->id, $collapsed['aggregatesonly'])) { |
4faf5f99 | 2262 | $category_item = reset($element['children']); //keep only category item |
2263 | $element['children'] = array(key($element['children'])=>$category_item); | |
2264 | ||
2265 | } else { | |
384960dd | 2266 | if (in_array($element['object']->id, $collapsed['gradesonly'])) { // Remove category item |
2267 | reset($element['children']); | |
2268 | $first_key = key($element['children']); | |
2269 | unset($element['children'][$first_key]); | |
2270 | } | |
2271 | foreach ($element['children'] as $sortorder=>$child) { // Recurse through the element's children | |
4faf5f99 | 2272 | grade_tree::category_collapse($element['children'][$sortorder], $collapsed); |
2273 | } | |
2274 | } | |
2275 | } | |
7a6b7acf | 2276 | |
aea4df41 | 2277 | /** |
2278 | * Static recursive helper - removes all outcomes | |
cf72e2dd | 2279 | * |
2280 | * @param array &$element The seed of the recursion | |
2281 | * | |
aea4df41 | 2282 | * @return void |
2283 | */ | |
d24832f9 | 2284 | public function no_outcomes(&$element) { |
aea4df41 | 2285 | if ($element['type'] != 'category') { |
2286 | return; | |
2287 | } | |
2288 | foreach ($element['children'] as $sortorder=>$child) { | |
2289 | if ($element['children'][$sortorder]['type'] == 'item' | |
2290 | and $element['children'][$sortorder]['object']->is_outcome_item()) { | |
2291 | unset($element['children'][$sortorder]); | |
2292 | ||
d4795a07 | 2293 | } else if ($element['children'][$sortorder]['type'] == 'category') { |
aea4df41 | 2294 | grade_tree::no_outcomes($element['children'][$sortorder]); |
2295 | } | |
2296 | } | |
2297 | } | |
2298 | ||
7a6b7acf | 2299 | /** |
2300 | * Static recursive helper - makes the grade_item for category the last children | |
cf72e2dd | 2301 | * |
2302 | * @param array &$element The seed of the recursion | |
2303 | * | |
7a6b7acf | 2304 | * @return void |
2305 | */ | |
d24832f9 | 2306 | public function category_grade_last(&$element) { |
7a6b7acf | 2307 | if (empty($element['children'])) { |
2308 | return; | |
2309 | } | |
2310 | if (count($element['children']) < 2) { | |
2311 | return; | |
2312 | } | |
3e0e2436 | 2313 | $first_item = reset($element['children']); |
4a3dfd9a | 2314 | if ($first_item['type'] == 'categoryitem' or $first_item['type'] == 'courseitem') { |
3e0e2436 | 2315 | // the category item might have been already removed |
2316 | $order = key($element['children']); | |
2317 | unset($element['children'][$order]); | |
2318 | $element['children'][$order] =& $first_item; | |
2319 | } | |
206f9953 | 2320 | foreach ($element['children'] as $sortorder => $child) { |
7a6b7acf | 2321 | grade_tree::category_grade_last($element['children'][$sortorder]); |
2322 | } | |
2323 | } | |
2324 | ||
2325 | /** | |
2326 | * Static recursive helper - fills the levels array, useful when accessing tree elements of one level | |
cf72e2dd | 2327 | * |
2328 | * @param array &$levels The levels of the grade tree through which to recurse | |
2329 | * @param array &$element The seed of the recursion | |
2330 | * @param int $depth How deep are we? | |
7a6b7acf | 2331 | * @return void |
2332 | */ | |
d24832f9 | 2333 | public function fill_levels(&$levels, &$element, $depth) { |
7a6b7acf | 2334 | if (!array_key_exists($depth, $levels)) { |
2335 | $levels[$depth] = array(); | |
2336 | } | |
2337 | ||
2338 | // prepare unique identifier | |
2339 | if ($element['type'] == 'category') { | |
ddb9c95f | 2340 | $element['eid'] = 'cg'.$element['object']->id; |
7a6b7acf | 2341 | } else if (in_array($element['type'], array('item', 'courseitem', 'categoryitem'))) { |
ddb9c95f | 2342 | $element['eid'] = 'ig'.$element['object']->id; |
b89a70ce | 2343 | $this->items[$element['object']->id] =& $element['object']; |
7a6b7acf | 2344 | } |
2345 | ||
2346 | $levels[$depth][] =& $element; | |
2347 | $depth++; | |
2348 | if (empty($element['children'])) { | |
2349 | return; | |
2350 | } | |
2351 | $prev = 0; | |
2352 | foreach ($element['children'] as $sortorder=>$child) { | |
2353 | grade_tree::fill_levels($levels, $element['children'][$sortorder], $depth); | |
2354 | $element['children'][$sortorder]['prev'] = $prev; | |
2355 | $element['children'][$sortorder]['next'] = 0; | |
2356 | if ($prev) { | |
2357 | $element['children'][$prev]['next'] = $sortorder; | |
2358 | } | |
2359 | $prev = $sortorder; | |
2360 | } | |
2361 | } | |
2362 | ||
2363 | /** | |
2364 | * Static recursive helper - makes full tree (all leafes are at the same level) | |
cf72e2dd | 2365 | * |
2366 | * @param array &$element The seed of the recursion | |
2367 | * @param int $depth How deep are we? | |
2368 | * | |
2369 | * @return int | |
7a6b7acf | 2370 | */ |
d24832f9 | 2371 | public function inject_fillers(&$element, $depth) { |
7a6b7acf | 2372 | $depth++; |
2373 | ||
2374 | if (empty($element['children'])) { | |
2375 | return $depth; | |
2376 | } | |
2377 | $chdepths = array(); | |
2378 | $chids = array_keys($element['children']); | |
2379 | $last_child = end($chids); | |
2380 | $first_child = reset($chids); | |
2381 | ||
2382 | foreach ($chids as $chid) { | |
2383 | $chdepths[$chid] = grade_tree::inject_fillers($element['children'][$chid], $depth); | |
2384 | } | |
2385 | arsort($chdepths); | |
2386 | ||
2387 | $maxdepth = reset($chdepths); | |
2388 | foreach ($chdepths as $chid=>$chd) { | |
2389 | if ($chd == $maxdepth) { | |
2390 | continue; | |
2391 | } | |
2392 | for ($i=0; $i < $maxdepth-$chd; $i++) { | |
2393 | if ($chid == $first_child) { | |
2394 | $type = 'fillerfirst'; | |
2395 | } else if ($chid == $last_child) { | |
2396 | $type = 'fillerlast'; | |
2397 | } else { | |
2398 | $type = 'filler'; | |
2399 | } | |
2400 | $oldchild =& $element['children'][$chid]; | |
cf72e2dd | 2401 | $element['children'][$chid] = array('object'=>'filler', 'type'=>$type, |
2402 | 'eid'=>'', 'depth'=>$element['object']->depth, | |
2403 | 'children'=>array($oldchild)); | |
7a6b7acf | 2404 | } |
2405 | } | |
2406 | ||
2407 | return $maxdepth; | |
2408 | } | |
2409 | ||
2410 | /** | |
2411 | * Static recursive helper - add colspan information into categories | |
cf72e2dd | 2412 | * |
2413 | * @param array &$element The seed of the recursion | |
2414 | * | |
2415 | * @return int | |
7a6b7acf | 2416 | */ |
d24832f9 | 2417 | public function inject_colspans(&$element) { |
7a6b7acf | 2418 | if (empty($element['children'])) { |
2419 | return 1; | |
2420 | } | |
2421 | $count = 0; | |
2422 | foreach ($element['children'] as $key=>$child) { | |
2423 | $count += grade_tree::inject_colspans($element['children'][$key]); | |
2424 | } | |
2425 | $element['colspan'] = $count; | |
2426 | return $count; | |
2427 | } | |
2428 | ||
2429 | /** | |
2430 | * Parses the array in search of a given eid and returns a element object with | |
2431 | * information about the element it has found. | |
cf72e2dd | 2432 | * @param int $eid Gradetree Element ID |
7a6b7acf | 2433 | * @return object element |
2434 | */ | |
d24832f9 | 2435 | public function locate_element($eid) { |
d3c3da1b | 2436 | // it is a grade - construct a new object |
2437 | if (strpos($eid, 'n') === 0) { | |
2438 | if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) { | |
2439 | return null; | |
2440 | } | |
2441 | ||
2442 | $itemid = $matches[1]; | |
2443 | $userid = $matches[2]; | |
2444 | ||
2445 | //extra security check - the grade item must be in this tree | |
ddb9c95f | 2446 | if (!$item_el = $this->locate_element('ig'.$itemid)) { |
d3c3da1b | 2447 | return null; |
2448 | } | |
2449 | ||
2450 | // $gradea->id may be null - means does not exist yet | |
2451 | $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid)); | |
2452 | ||
2453 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
2454 | return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade'); | |
2455 | ||
2456 | } else if (strpos($eid, 'g') === 0) { | |
cf72e2dd | 2457 | $id = (int) substr($eid, 1); |
7a6b7acf | 2458 | if (!$grade = grade_grade::fetch(array('id'=>$id))) { |
2459 | return null; | |
2460 | } | |
2461 | //extra security check - the grade item must be in this tree | |
ddb9c95f | 2462 | if (!$item_el = $this->locate_element('ig'.$grade->itemid)) { |
7a6b7acf | 2463 | return null; |
2464 | } | |
2465 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
2466 | return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade'); | |
2467 | } | |
2468 | ||
2469 | // it is a category or item | |
2470 | foreach ($this->levels as $row) { | |
2471 | foreach ($row as $element) { | |
2472 | if ($element['type'] == 'filler') { | |
2473 | continue; | |
2474 | } | |
2475 | if ($element['eid'] == $eid) { | |
2476 | return $element; | |
2477 | } | |
2478 | } | |
2479 | } | |
2480 | ||
2481 | return null; | |
2482 | } | |
d24832f9 | 2483 | |
b244b9b7 | 2484 | /** |
2485 | * Returns a well-formed XML representation of the grade-tree using recursion. | |
cf72e2dd | 2486 | * |
2487 | * @param array $root The current element in the recursion. If null, starts at the top of the tree. | |
2488 | * @param string $tabs The control character to use for tabs | |
2489 | * | |
b244b9b7 | 2490 | * @return string $xml |
2491 | */ | |
cf72e2dd | 2492 | public function exporttoxml($root=null, $tabs="\t") { |
b244b9b7 | 2493 | $xml = null; |
2494 | $first = false; | |
2495 | if (is_null($root)) { | |
2496 | $root = $this->top_element; | |
2497 | $xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n"; | |
2498 | $xml .= "<gradetree>\n"; | |
2499 | $first = true; | |
2500 | } | |
d24832f9 | 2501 | |
b244b9b7 | 2502 | $type = 'undefined'; |
2503 | if (strpos($root['object']->table, 'grade_categories') !== false) { | |
2504 | $type = 'category'; | |
cf72e2dd | 2505 | } else if (strpos($root['object']->table, 'grade_items') !== false) { |
b244b9b7 | 2506 | $type = 'item'; |
cf72e2dd | 2507 | } else if (strpos($root['object']->table, 'grade_outcomes') !== false) { |
b244b9b7 | 2508 | $type = 'outcome'; |
2509 | } | |
d24832f9 | 2510 | |
b244b9b7 | 2511 | $xml .= "$tabs<element type=\"$type\">\n"; |
2512 | foreach ($root['object'] as $var => $value) { | |
2513 | if (!is_object($value) && !is_array($value) && !empty($value)) { | |
2514 | $xml .= "$tabs\t<$var>$value</$var>\n"; | |
2515 | } | |
2516 | } | |
2517 | ||
2518 | if (!empty($root['children'])) { | |
2519 | $xml .= "$tabs\t<children>\n"; | |
2520 | foreach ($root['children'] as $sortorder => $child) { | |
2521 | $xml .= $this->exportToXML($child, $tabs."\t\t"); | |
2522 | } | |
2523 | $xml .= "$tabs\t</children>\n"; | |
2524 | } | |
d24832f9 | 2525 | |
b244b9b7 | 2526 | $xml .= "$tabs</element>\n"; |
2527 | ||
2528 | if ($first) { | |
2529 | $xml .= "</gradetree>"; | |
2530 | } | |
d24832f9 | 2531 | |
b244b9b7 | 2532 | return $xml; |
2533 | } | |
d24832f9 | 2534 | |
b244b9b7 | 2535 | /** |
2536 | * Returns a JSON representation of the grade-tree using recursion. | |
cf72e2dd | 2537 | * |
b244b9b7 | 2538 | * @param array $root The current element in the recursion. If null, starts at the top of the tree. |
2539 | * @param string $tabs Tab characters used to indent the string nicely for humans to enjoy | |
cf72e2dd | 2540 | * |
2541 | * @return string | |
b244b9b7 | 2542 | */ |
cf72e2dd | 2543 | public function exporttojson($root=null, $tabs="\t") { |
b244b9b7 | 2544 | $json = null; |
2545 | $first = false; | |
2546 | if (is_null($root)) { | |
2547 | $root = $this->top_element; | |
2548 | $first = true; | |
2549 | } | |
d24832f9 | 2550 | |
b244b9b7 | 2551 | $name = ''; |
2552 | ||
2553 | ||
2554 | if (strpos($root['object']->table, 'grade_categories') !== false) { | |
2555 | $name = $root['object']->fullname; | |
2556 | if ($name == '?') { | |
d24832f9 | 2557 | $name = $root['object']->get_name(); |
b244b9b7 | 2558 | } |
cf72e2dd | 2559 | } else if (strpos($root['object']->table, 'grade_items') !== false) { |
b244b9b7 | 2560 | $name = $root['object']->itemname; |
cf72e2dd | 2561 | } else if (strpos($root['object']->table, 'grade_outcomes') !== false) { |
b244b9b7 | 2562 | $name = $root['object']->itemname; |
2563 | } | |
d24832f9 | 2564 | |
b244b9b7 | 2565 | $json .= "$tabs {\n"; |
2566 | $json .= "$tabs\t \"type\": \"{$root['type']}\",\n"; | |
2567 | $json .= "$tabs\t \"name\": \"$name\",\n"; | |
2568 | ||
2569 | foreach ($root['object'] as $var => $value) { | |
2570 | if (!is_object($value) && !is_array($value) && !empty($value)) { | |
2571 | $json .= "$tabs\t \"$var\": \"$value\",\n"; | |
2572 | } | |
2573 | } | |
d24832f9 | 2574 | |
b244b9b7 | 2575 | $json = substr($json, 0, strrpos($json, ',')); |
d24832f9 | 2576 | |
b244b9b7 | 2577 | if (!empty($root['children'])) { |
2578 | $json .= ",\n$tabs\t\"children\": [\n"; | |
2579 | foreach ($root['children'] as $sortorder => $child) { | |
2580 | $json .= $this->exportToJSON($child, $tabs."\t\t"); | |
2581 | } | |
2582 | $json = substr($json, 0, strrpos($json, ',')); | |
2583 | $json .= "\n$tabs\t]\n"; | |
d24832f9 | 2584 | } |
b244b9b7 | 2585 | |
2586 | if ($first) { | |
2587 | $json .= "\n}"; | |
2588 | } else { | |
2589 | $json .= "\n$tabs},\n"; | |
2590 | } | |
d24832f9 | 2591 | |
b244b9b7 | 2592 | return $json; |
2593 | } | |
d24832f9 | 2594 | |
cf72e2dd | 2595 | /** |
2596 | * Returns the array of levels | |
2597 | * | |
2598 | * @return array | |
2599 | */ | |
d24832f9 | 2600 | public function get_levels() { |
2601 | return $this->levels; | |
2602 | } | |
2603 | ||
cf72e2dd | 2604 | /** |
2605 | * Returns the array of grade items | |
2606 | * | |
2607 | * @return array | |
2608 | */ | |
d24832f9 | 2609 | public function get_items() { |
2610 | return $this->items; | |
2611 | } | |
2612 | ||
cf72e2dd | 2613 | /** |
2614 | * Returns a specific Grade Item | |
2615 | * | |
2616 | * @param int $itemid The ID of the grade_item object | |
2617 | * | |
2618 | * @return grade_item | |
2619 | */ | |
d24832f9 | 2620 | public function get_item($itemid) { |
2621 | if (array_key_exists($itemid, $this->items)) { | |
2622 | return $this->items[$itemid]; | |
2623 | } else { | |
2624 | return false; | |
2625 | } | |
2626 | } | |
7a6b7acf | 2627 | } |
eef00ade | 2628 | |
2629 | /** | |
2630 | * Local shortcut function for creating an edit/delete button for a grade_* object. | |
4d27bc79 | 2631 | * @param string $type 'edit' or 'delete' |
eef00ade | 2632 | * @param int $courseid The Course ID |
2633 | * @param grade_* $object The grade_* object | |
2634 | * @return string html | |
2635 | */ | |
2636 | function grade_button($type, $courseid, $object) { | |
2637 | global $CFG, $OUTPUT; | |
2638 | if (preg_match('/grade_(.*)/', get_class($object), $matches)) { | |
2639 | $objectidstring = $matches[1] . 'id'; | |
2640 | } else { | |
2641 | throw new coding_exception('grade_button() only accepts grade_* objects as third parameter!'); | |
2642 | } | |
2643 | ||
2644 | $strdelete = get_string('delete'); | |
2645 | $stredit = get_string('edit'); | |
2646 | ||
eef00ade | 2647 | if ($type == 'delete') { |
8ae8bf8a | 2648 | $url = new moodle_url('index.php', array('id' => $courseid, $objectidstring => $object->id, 'action' => 'delete', 'sesskey' => sesskey())); |
eef00ade | 2649 | } else if ($type == 'edit') { |
8ae8bf8a | 2650 | $url = new moodle_url('edit.php', array('courseid' => $courseid, 'id' => $object->id)); |
eef00ade | 2651 | } |
2652 | ||
fa9c0aab | 2653 | return $OUTPUT->action_icon($url, new pix_icon('t/'.$type, ${'str'.$type}, '', array('class' => 'iconsmall'))); |
eef00ade | 2654 | |
2655 | } | |
4d5059d4 SH |
2656 | |
2657 | /** | |
2658 | * This method adds settings to the settings block for the grade system and its | |
2659 | * plugins | |
2660 | * | |
2661 | * @global moodle_page $PAGE | |
2662 | */ | |
2663 | function grade_extend_settings($plugininfo, $courseid) { | |
2664 | global $PAGE; | |
2665 | ||
2666 | $gradenode = $PAGE->settingsnav->prepend(get_string('gradeadministration', 'grades'), null, navigation_node::TYPE_CONTAINER); | |
2667 | ||
2668 | $strings = array_shift($plugininfo); | |
2669 | ||
2670 | if ($reports = grade_helper::get_plugins_reports($courseid)) { | |
2671 | foreach ($reports as $report) { | |
2672 | $gradenode->add($report->string, $report->link, navigation_node::TYPE_SETTING, null, $report->id, new pix_icon('i/report', '')); | |
2673 | } | |
2674 | } | |
2675 | ||
eb84b779 MG |
2676 | if ($settings = grade_helper::get_info_manage_settings($courseid)) { |
2677 | $settingsnode = $gradenode->add($strings['settings'], null, navigation_node::TYPE_CONTAINER); | |
2678 | foreach ($settings as $setting) { | |
2679 | $settingsnode->add($setting->string, $setting->link, navigation_node::TYPE_SETTING, null, $setting->id, new pix_icon('i/settings', '')); | |
2680 | } | |
2681 | } | |
2682 | ||
4d5059d4 SH |
2683 | if ($imports = grade_helper::get_plugins_import($courseid)) { |
2684 | $importnode = $gradenode->add($strings['import'], null, navigation_node::TYPE_CONTAINER); | |
2685 | foreach ($imports as $import) { | |
11f87187 | 2686 | $importnode->add($import->string, $import->link, navigation_node::TYPE_SETTING, null, $import->id, new pix_icon('i/import', '')); |
4d5059d4 SH |
2687 | } |
2688 | } | |
2689 | ||
2690 | if ($exports = grade_helper::get_plugins_export($courseid)) { | |
2691 | $exportnode = $gradenode->add($strings['export'], null, navigation_node::TYPE_CONTAINER); | |
2692 | foreach ($exports as $export) { | |
11f87187 | 2693 | $exportnode->add($export->string, $export->link, navigation_node::TYPE_SETTING, null, $export->id, new pix_icon('i/export', '')); |
4d5059d4 SH |
2694 | } |
2695 | } | |
2696 | ||
4d5059d4 SH |
2697 | if ($letters = grade_helper::get_info_letters($courseid)) { |
2698 | $letters = array_shift($letters); | |
2699 | $gradenode->add($strings['letter'], $letters->link, navigation_node::TYPE_SETTING, null, $letters->id, new pix_icon('i/settings', '')); | |
2700 | } | |
2701 | ||
2702 | if ($outcomes = grade_helper::get_info_outcomes($courseid)) { | |
2703 | $outcomes = array_shift($outcomes); | |
2704 | $gradenode->add($strings['outcome'], $outcomes->link, navigation_node::TYPE_SETTING, null, $outcomes->id, new pix_icon('i/outcomes', '')); | |
2705 | } | |
2706 | ||
2707 | if ($scales = grade_helper::get_info_scales($courseid)) { | |
2708 | $gradenode->add($strings['scale'], $scales->link, navigation_node::TYPE_SETTING, null, $scales->id, new pix_icon('i/scales', '')); | |
2709 | } | |
2710 | ||
4d5059d4 SH |
2711 | if ($gradenode->contains_active_node()) { |
2712 | // If the gradenode is active include the settings base node (gradeadministration) in | |
2713 | // the navbar, typcially this is ignored. | |
2714 | $PAGE->navbar->includesettingsbase = true; | |
2715 | ||
2716 | // If we can get the course admin node make sure it is closed by default | |
2717 | // as in this case the gradenode will be opened | |
2718 | if ($coursenode = $PAGE->settingsnav->get('courseadmin', navigation_node::TYPE_COURSE)){ | |
2719 | $coursenode->make_inactive(); | |
2720 | $coursenode->forceopen = false; | |
2721 | } | |
2722 | } | |
2723 | } | |
2724 | ||
2725 | /** | |
2726 | * Grade helper class | |
2727 | * | |
2728 | * This class provides several helpful functions that work irrespective of any | |
2729 | * current state. | |
2730 | * | |
2731 | * @copyright 2010 Sam Hemelryk | |
2732 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2733 | */ | |
2734 | abstract class grade_helper { | |
2735 | /** | |
2736 | * Cached manage settings info {@see get_info_settings} | |
2737 | * @var grade_plugin_info|false | |
2738 | */ | |
2739 | protected static $managesetting = null; | |
2740 | /** | |
2741 | * Cached grade report plugins {@see get_plugins_reports} | |
2742 | * @var array|false | |
2743 | */ | |
2744 | protected static $gradereports = null; | |
2745 | /** | |
2746 | * Cached grade report plugins preferences {@see get_info_scales} | |
2747 | * @var array|false | |
2748 | */ | |
2749 | protected static $gradereportpreferences = null; | |
2750 | /** | |
2751 | * Cached scale info {@see get_info_scales} | |
2752 | * @var grade_plugin_info|false | |
2753 | */ | |
2754 | protected static $scaleinfo = null; | |
2755 | /** | |
2756 | * Cached outcome info {@see get_info_outcomes} | |
2757 | * @var grade_plugin_info|false | |
2758 | */ | |
2759 | protected static $outcomeinfo = null; | |
4d5059d4 SH |
2760 | /** |
2761 | * Cached leftter info {@see get_info_letters} | |
2762 | * @var grade_plugin_info|false | |
2763 | */ | |
2764 | protected static $letterinfo = null; | |
2765 | /** | |
2766 | * Cached grade import plugins {@see get_plugins_import} | |
2767 | * @var array|false | |
2768 | */ | |
2769 | protected static $importplugins = null; | |
2770 | /** | |
2771 | * Cached grade export plugins {@see get_plugins_export} | |
2772 | * @var array|false | |
2773 | */ | |
2774 | protected static $exportplugins = null; | |
2775 | /** | |
2776 | * Cached grade plugin strings | |
2777 | * @var array | |
2778 | */ | |
2779 | protected static $pluginstrings = null; | |
bfe969e8 DW |
2780 | /** |
2781 | * Cached grade aggregation strings | |
2782 | * @var array | |
2783 | */ | |
2784 | protected static $aggregationstrings = null; | |
4d5059d4 SH |
2785 | |
2786 | /** | |
2787 | * Gets strings commonly used by the describe plugins | |
2788 | * | |
2789 | * report => get_string('view'), | |
4d5059d4 SH |
2790 | * scale => get_string('scales'), |
2791 | * outcome => get_string('outcomes', 'grades'), | |
2792 | * letter => get_string('letters', 'grades'), | |
2793 | * export => get_string('export', 'grades'), | |
2794 | * import => get_string('import'), | |
4d5059d4 SH |
2795 | * settings => get_string('settings') |
2796 | * | |
2797 | * @return array | |
2798 | */ | |
2799 | public static function get_plugin_strings() { | |
2800 | if (self::$pluginstrings === null) { | |
2801 | self::$pluginstrings = array( | |
2802 | 'report' => get_string('view'), | |
4d5059d4 SH |
2803 | 'scale' => get_string('scales'), |
2804 | 'outcome' => get_string('outcomes', 'grades'), | |
2805 | 'letter' => get_string('letters', 'grades'), | |
2806 | 'export' => get_string('export', 'grades'), | |
2807 | 'import' => get_string('import'), | |
eb84b779 | 2808 | 'settings' => get_string('edittree', 'grades') |
4d5059d4 SH |
2809 | ); |
2810 | } | |
2811 | return self::$pluginstrings; | |
2812 | } | |
bfe969e8 DW |
2813 | |
2814 | /** | |
2815 | * Gets strings describing the available aggregation methods. | |
2816 | * | |
2817 | * @return array | |
2818 | */ | |
2819 | public static function get_aggregation_strings() { | |
2820 | if (self::$aggregationstrings === null) { | |
2821 | self::$aggregationstrings = array( | |
2822 | GRADE_AGGREGATE_MEAN => get_string('aggregatemean', 'grades'), | |
2823 | GRADE_AGGREGATE_WEIGHTED_MEAN => get_string('aggregateweightedmean', 'grades'), | |
2824 | GRADE_AGGREGATE_WEIGHTED_MEAN2 => get_string('aggregateweightedmean2', 'grades'), | |
2825 | GRADE_AGGREGATE_EXTRACREDIT_MEAN => get_string('aggregateextracreditmean', 'grades'), | |
2826 | GRADE_AGGREGATE_MEDIAN => get_string('aggregatemedian', 'grades'), | |
2827 | GRADE_AGGREGATE_MIN => get_string('aggregatemin', 'grades'), | |
2828 | GRADE_AGGREGATE_MAX => get_string('aggregatemax', 'grades'), | |
2829 | GRADE_AGGREGATE_MODE => get_string('aggregatemode', 'grades'), | |
2830 | GRADE_AGGREGATE_SUM => get_string('aggregatesum', 'grades') | |
2831 | ); | |
2832 | } | |
2833 | return self::$aggregationstrings; | |
2834 | } | |
2835 | ||
4d5059d4 SH |
2836 | /** |
2837 | * Get grade_plugin_info object for managing settings if the user can | |
2838 | * | |
2839 | * @param int $courseid | |
8faa560c | 2840 | * @return grade_plugin_info[] |
4d5059d4 SH |
2841 | */ |
2842 | public static function get_info_manage_settings($courseid) { | |
2843 | if (self::$managesetting !== null) { | |
2844 | return self::$managesetting; | |
2845 | } | |
d4060472 | 2846 | $context = context_course::instance($courseid); |
8faa560c MG |
2847 | self::$managesetting = array(); |
2848 | if ($courseid != SITEID && has_capability('moodle/grade:manage', $context)) { | |
eb84b779 MG |
2849 | self::$managesetting['categoriesanditems'] = new grade_plugin_info('setup', |
2850 | new moodle_url('/grade/edit/tree/index.php', array('id' => $courseid)), | |
2851 | get_string('categoriesanditems', 'grades')); | |
8faa560c MG |
2852 | self::$managesetting['coursesettings'] = new grade_plugin_info('coursesettings', |
2853 | new moodle_url('/grade/edit/settings/index.php', array('id'=>$courseid)), | |
2854 | get_string('coursegradesettings', 'grades')); | |
eb84b779 MG |
2855 | } |
2856 | if (self::$gradereportpreferences === null) { | |
2857 | self::get_plugins_reports($courseid); | |
2858 | } | |
2859 | if (self::$gradereportpreferences) { | |
2860 | self::$managesetting = array_merge(self::$managesetting, self::$gradereportpreferences); | |
4d5059d4 SH |
2861 | } |
2862 | return self::$managesetting; | |
2863 | } | |
2864 | /** | |
2865 | * Returns an array of plugin reports as grade_plugin_info objects | |
2866 | * | |
2867 | * @param int $courseid | |
2868 | * @return array | |
2869 | */ | |
2870 | public static function get_plugins_reports($courseid) { | |
f7fcf4cd | 2871 | global $SITE; |
cf717dc2 | 2872 | |
4d5059d4 SH |
2873 | if (self::$gradereports !== null) { |
2874 | return self::$gradereports; | |
2875 | } | |
d4060472 | 2876 | $context = context_course::instance($courseid); |
4d5059d4 SH |
2877 | $gradereports = array(); |
2878 | $gradepreferences = array(); | |
bd3b3bba | 2879 | foreach (core_component::get_plugin_list('gradereport') as $plugin => $plugindir) { |
f7fcf4cd AD |
2880 | //some reports make no sense if we're not within a course |
2881 | if ($courseid==$SITE->id && ($plugin=='grader' || $plugin=='user')) { | |
2882 | continue; | |
2883 | } | |
2884 | ||
4d5059d4 SH |
2885 | // Remove ones we can't see |
2886 | if (!has_capability('gradereport/'.$plugin.':view', $context)) { | |
2887 | continue; | |
2888 | } | |
2889 | ||
4ae71e93 DP |
2890 | // Singleview doesn't doesn't accomodate for all cap combos yet, so this is hardcoded.. |
2891 | if ($plugin === 'singleview' && !has_all_capabilities(array('moodle/grade:viewall', | |
2892 | 'moodle/grade:edit'), $context)) { | |
2893 | continue; | |
2894 | } | |
2895 | ||
b5e7b2bf | 2896 | $pluginstr = get_string('pluginname', 'gradereport_'.$plugin); |
4d5059d4 SH |
2897 | $url = new moodle_url('/grade/report/'.$plugin.'/index.php', array('id'=>$courseid)); |
2898 | $gradereports[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr); | |
2899 | ||
2900 | // Add link to preferences tab if such a page exists | |
2901 | if (file_exists($plugindir.'/preferences.php')) { | |
2902 | $url = new moodle_url('/grade/report/'.$plugin.'/preferences.php', array('id'=>$courseid)); | |
eb84b779 | 2903 | $gradepreferences[$plugin] = new grade_plugin_info($plugin, $url, |
4887d152 | 2904 | get_string('preferences', 'grades') . ': ' . $pluginstr); |
4d5059d4 SH |
2905 | } |
2906 | } | |
2907 | if (count($gradereports) == 0) { | |
2908 | $gradereports = false; | |
2909 | $gradepreferences = false; | |
2910 | } else if (count($gradepreferences) == 0) { | |
2911 | $gradepreferences = false; | |
2912 | asort($gradereports); | |
2913 | } else { | |
2914 | asort($gradereports); | |
2915 | asort($gradepreferences); | |
2916 | } | |
2917 | self::$gradereports = $gradereports; | |
2918 | self::$gradereportpreferences = $gradepreferences; | |
2919 | return self::$gradereports; | |
2920 | } | |
eb84b779 | 2921 | |
4d5059d4 SH |
2922 | /** |
2923 | * Get information on scales | |
2924 | * @param int $courseid | |
2925 | * @return grade_plugin_info | |
2926 | */ | |
2927 | public static function get_info_scales($courseid) { | |
2928 | if (self::$scaleinfo !== null) { | |
2929 | return self::$scaleinfo; | |
2930 | } | |
d4060472 | 2931 | if (has_capability('moodle/course:managescales', context_course::instance($courseid))) { |
4d5059d4 SH |
2932 | $url = new moodle_url('/grade/edit/scale/index.php', array('id'=>$courseid)); |
2933 | self::$scaleinfo = new grade_plugin_info('scale', $url, get_string('view')); | |
2934 | } else { | |
2935 | self::$scaleinfo = false; | |
2936 | } | |
2937 | return self::$scaleinfo; | |
2938 | } | |
2939 | /** | |
2940 | * Get information on outcomes | |
2941 | * @param int $courseid | |
2942 | * @return grade_plugin_info | |
2943 | */ | |
2944 | public static function get_info_outcomes($courseid) { | |
f7fcf4cd | 2945 | global $CFG, $SITE; |
4d5059d4 SH |
2946 | |
2947 | if (self::$outcomeinfo !== null) { | |
2948 | return self::$outcomeinfo; | |
2949 | } | |
d4060472 | 2950 | $context = context_course::instance($courseid); |
4d5059d4 SH |
2951 | $canmanage = has_capability('moodle/grade:manage', $context); |
2952 | $canupdate = has_capability('moodle/course:update', $context); | |
2953 | if (!empty($CFG->enableoutcomes) && ($canmanage || $canupdate)) { | |
2954 | $outcomes = array(); | |
2955 | if ($canupdate) { | |
f7fcf4cd AD |
2956 | if ($courseid!=$SITE->id) { |
2957 | $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid)); | |
2958 | $outcomes['course'] = new grade_plugin_info('course', $url, get_string('outcomescourse', 'grades')); | |
2959 | } | |
4d5059d4 SH |
2960 | $url = new moodle_url('/grade/edit/outcome/index.php', array('id'=>$courseid)); |
2961 | $outcomes['edit'] = new grade_plugin_info('edit', $url, get_string('editoutcomes', 'grades')); | |
c46aeeab DC |
2962 | $url = new moodle_url('/grade/edit/outcome/import.php', array('courseid'=>$courseid)); |
2963 | $outcomes['import'] = new grade_plugin_info('import', $url, get_string('importoutcomes', 'grades')); | |
4d5059d4 | 2964 | } else { |
f7fcf4cd AD |
2965 | if ($courseid!=$SITE->id) { |
2966 | $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid)); | |
2967 | $outcomes['edit'] = new grade_plugin_info('edit', $url, get_string('outcomescourse', 'grades')); | |
2968 | } | |
4d5059d4 SH |
2969 | } |
2970 | self::$outcomeinfo = $outcomes; | |
2971 | } else { | |
2972 | self::$outcomeinfo = false; | |
2973 | } | |
2974 | return self::$outcomeinfo; | |
2975 | } | |
4d5059d4 SH |
2976 | /** |
2977 | * Get information on letters | |
2978 | * @param int $courseid | |
4d27bc79 | 2979 | * @return array |
4d5059d4 SH |
2980 | */ |
2981 | public static function get_info_letters($courseid) { | |
a8dfc483 | 2982 | global $SITE; |
4d5059d4 SH |
2983 | if (self::$letterinfo !== null) { |
2984 | return self::$letterinfo; | |
2985 | } | |
d4060472 | 2986 | $context = context_course::instance($courseid); |
4d5059d4 SH |
2987 | $canmanage = has_capability('moodle/grade:manage', $context); |
2988 | $canmanageletters = has_capability('moodle/grade:manageletters', $context); | |
2989 | if ($canmanage || $canmanageletters) { | |
a8dfc483 AA |
2990 | // Redirect to system context when report is accessed from admin settings MDL-31633 |
2991 | if ($context->instanceid == $SITE->id) { | |
2992 | $param = array('edit' => 1); | |
2993 | } else { | |
2994 | $param = array('edit' => 1,'id' => $context->id); | |
2995 | } | |
4d5059d4 | 2996 | self::$letterinfo = array( |
54caa598 | 2997 | 'view' => new grade_plugin_info('view', new moodle_url('/grade/edit/letter/index.php', array('id'=>$context->id)), get_string('view')), |
a8dfc483 | 2998 | 'edit' => new grade_plugin_info('edit', new moodle_url('/grade/edit/letter/index.php', $param), get_string('edit')) |
4d5059d4 SH |
2999 | ); |
3000 | } else { | |
3001 | self::$letterinfo = false; | |
3002 | } | |
3003 | return self::$letterinfo; | |
3004 | } | |
3005 | /** | |
3006 | * Get information import plugins | |
3007 | * @param int $courseid | |
3008 | * @return array | |
3009 | */ | |
3010 | public static function get_plugins_import($courseid) { | |
3011 | global $CFG; | |
3012 | ||
3013 | if (self::$importplugins !== null) { | |
3014 | return self::$importplugins; | |
3015 | } | |
3016 | $importplugins = array(); | |
d4060472 | 3017 | $context = context_course::instance($courseid); |
4d5059d4 SH |
3018 | |
3019 | if (has_capability('moodle/grade:import', $context)) { | |
bd3b3bba | 3020 | foreach (core_component::get_plugin_list('gradeimport') as $plugin => $plugindir) { |
4d5059d4 SH |
3021 | if (!has_capability('gradeimport/'.$plugin.':view', $context)) { |
3022 | continue; | |
3023 | } | |
b5e7b2bf | 3024 | $pluginstr = get_string('pluginname', 'gradeimport_'.$plugin); |
4d5059d4 SH |
3025 | $url = new moodle_url('/grade/import/'.$plugin.'/index.php', array('id'=>$courseid)); |
3026 | $importplugins[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr); | |
3027 | } | |
3028 | ||
ead4d41e SL |
3029 | // Show key manager if grade publishing is enabled and the user has xml publishing capability. |
3030 | // XML is the only grade import plugin that has publishing feature. | |
3031 | if ($CFG->gradepublishing && has_capability('gradeimport/xml:publish', $context)) { | |
4d5059d4 SH |
3032 | $url = new moodle_url('/grade/import/keymanager.php', array('id'=>$courseid)); |
3033 | $importplugins['keymanager'] = new grade_plugin_info('keymanager', $url, get_string('keymanager', 'grades')); | |
3034 | } | |
3035 | } | |
3036 | ||
3037 | if (count($importplugins) > 0) { | |
3038 | asort($importplugins); | |
3039 | self::$importplugins = $importplugins; | |
3040 | } else { | |
3041 | self::$importplugins = false; | |
3042 | } | |
3043 | return self::$importplugins; | |
3044 | } | |
3045 | /** | |
3046 | * Get information export plugins | |
3047 | * @param int $courseid | |
3048 | * @return array | |
3049 | */ | |
3050 | public static function get_plugins_export($courseid) { | |
3051 | global $CFG; | |
3052 | ||
3053 | if (self::$exportplugins !== null) { | |
3054 | return self::$exportplugins; | |
3055 | } | |
d4060472 | 3056 | $context = context_course::instance($courseid); |
4d5059d4 | 3057 | $exportplugins = array(); |
ead4d41e | 3058 | $canpublishgrades = 0; |
4d5059d4 | 3059 | if (has_capability('moodle/grade:export', $context)) { |
bd3b3bba | 3060 | foreach (core_component::get_plugin_list('gradeexport') as $plugin => $plugindir) { |
4d5059d4 SH |
3061 | if (!has_capability('gradeexport/'.$plugin.':view', $context)) { |
3062 | continue; | |
3063 | } | |
ead4d41e SL |
3064 | // All the grade export plugins has grade publishing capabilities. |
3065 | if (has_capability('gradeexport/'.$plugin.':publish', $context)) { | |
3066 | $canpublishgrades++; | |
3067 | } | |
3068 | ||
b5e7b2bf | 3069 | $pluginstr = get_string('pluginname', 'gradeexport_'.$plugin); |
4d5059d4 SH |
3070 | $url = new moodle_url('/grade/export/'.$plugin.'/index.php', array('id'=>$courseid)); |
3071 | $exportplugins[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr); | |
3072 | } | |
3073 | ||
ead4d41e SL |
3074 | // Show key manager if grade publishing is enabled and the user has at least one grade publishing capability. |
3075 | if ($CFG->gradepublishing && $canpublishgrades != 0) { | |
4d5059d4 SH |
3076 | $url = new moodle_url('/grade/export/keymanager.php', array('id'=>$courseid)); |
3077 | $exportplugins['keymanager'] = new grade_plugin_info('keymanager', $url, get_string('keymanager', 'grades')); | |
3078 | } | |
3079 | } | |
3080 | if (count($exportplugins) > 0) { | |
3081 | asort($exportplugins); | |
3082 | self::$exportplugins = $exportplugins; | |
3083 | } else { | |
3084 | self::$exportplugins = false; | |
3085 | } | |
3086 | return self::$exportplugins; | |
3087 | } | |
293f42b6 | 3088 | |
61c8e0d7 FM |
3089 | /** |
3090 | * Returns the value of a field from a user record | |
3091 | * | |
3092 | * @param stdClass $user object | |
3093 | * @param stdClass $field object | |
3094 | * @return string value of the field | |
3095 | */ | |
3096 | public static function get_user_field_value($user, $field) { | |
3097 | if (!empty($field->customid)) { | |
3098 | $fieldname = 'customfield_' . $field->shortname; | |
3099 | if (!empty($user->{$fieldname}) || is_numeric($user->{$fieldname})) { | |
3100 | $fieldvalue = $user->{$fieldname}; | |
3101 | } else { | |
3102 | $fieldvalue = $field->default; | |
3103 | } | |
3104 | } else { | |
3105 | $fieldvalue = $user->{$field->shortname}; | |
3106 | } | |
3107 | return $fieldvalue; | |
3108 | } | |
3109 | ||
3110 | /** | |
3111 | * Returns an array of user profile fields to be included in export | |
3112 | * | |
3113 | * @param int $courseid | |
3114 | * @param bool $includecustomfields | |
3115 | * @return array An array of stdClass instances with customid, shortname, datatype, default and fullname fields | |
3116 | */ | |
3117 | public static function get_user_profile_fields($courseid, $includecustomfields = false) { | |
3118 | global $CFG, $DB; | |
3119 | ||
3120 | // Gets the fields that have to be hidden | |
3121 | $hiddenfields = array_map('trim', explode(',', $CFG->hiddenuserfields)); | |
3122 | $context = context_course::instance($courseid); | |
3123 | $canseehiddenfields = has_capability('moodle/course:viewhiddenuserfields', $context); | |
3124 | if ($canseehiddenfields) { | |
3125 | $hiddenfields = array(); | |
3126 | } | |
3127 | ||
3128 | $fields = array(); | |
3129 | require_once($CFG->dirroot.'/user/lib.php'); // Loads user_get_default_fields() | |
3130 | require_once($CFG->dirroot.'/user/profile/lib.php'); // Loads constants, such as PROFILE_VISIBLE_ALL | |
3131 | $userdefaultfields = user_get_default_fields(); | |
3132 | ||
3133 | // Sets the list of profile fields | |
3134 | $userprofilefields = array_map('trim', explode(',', $CFG->grade_export_userprofilefields)); | |
3135 | if (!empty($userprofilefields)) { | |
3136 | foreach ($userprofilefields as $field) { | |
3137 | $field = trim($field); | |
3138 | if (in_array($field, $hiddenfields) || !in_array($field, $userdefaultfields)) { | |
3139 | continue; | |
3140 | } | |
293f42b6 | 3141 | $obj = new stdClass(); |
61c8e0d7 | 3142 | $obj->customid = 0; |
293f42b6 | 3143 | $obj->shortname = $field; |
61c8e0d7 | 3144 | $obj->fullname = get_string($field); |
293f42b6 CF |
3145 | $fields[] = $obj; |
3146 | } | |
3147 | } | |
293f42b6 | 3148 | |
61c8e0d7 FM |
3149 | // Sets the list of custom profile fields |
3150 | $customprofilefields = array_map('trim', explode(',', $CFG->grade_export_customprofilefields)); | |
3151 | if ($includecustomfields && !empty($customprofilefields)) { | |
3152 | list($wherefields, $whereparams) = $DB->get_in_or_equal($customprofilefields); | |
3153 | $customfields = $DB->get_records_sql("SELECT f.* | |
3154 | FROM {user_info_field} f | |
3155 | JOIN {user_info_category} c ON f.categoryid=c.id | |
3156 | WHERE f.shortname $wherefields | |
3157 | ORDER BY c.sortorder ASC, f.sortorder ASC", $whereparams); | |
3158 | if (!is_array($customfields)) { | |
3159 | continue; | |
3160 | } | |
3161 | ||
3162 | foreach ($customfields as $field) { | |
3163 | // Make sure we can display this custom field | |
3164 | if (!in_array($field->shortname, $customprofilefields)) { | |
3165 | continue; | |
3166 | } else if (in_array($field->shortname, $hiddenfields)) { | |
3167 | continue; | |
3168 | } else if ($field->visible != PROFILE_VISIBLE_ALL && !$canseehiddenfields) { | |
3169 | continue; | |
3170 | } | |
3171 | ||
3172 | $obj = new stdClass(); | |
3173 | $obj->customid = $field->id; | |
3174 | $obj->shortname = $field->shortname; | |
3175 | $obj->fullname = format_string($field->name); | |
3176 | $obj->datatype = $field->datatype; | |
3177 | $obj->default = $field->defaultdata; | |
3178 | $fields[] = $obj; | |
3179 | } | |
3180 | } | |
3181 | ||
3182 | return $fields; | |
3183 | } | |
e61f25a4 DW |
3184 | |
3185 | /** | |
3186 | * This helper method gets a snapshot of all the weights for a course. | |
3187 | * It is used as a quick method to see if any wieghts have been automatically adjusted. | |
3188 | * @param int $courseid | |
3189 | * @return array of itemid -> aggregationcoef2 | |
3190 | */ | |
3191 | public static function fetch_all_natural_weights_for_course($courseid) { | |
3192 | global $DB; | |
3193 | $result = array(); | |
3194 | ||
3195 | $records = $DB->get_records('grade_items', array('courseid'=>$courseid), 'id', 'id, aggregationcoef2'); | |
3196 | foreach ($records as $record) { | |
3197 | $result[$record->id] = $record->aggregationcoef2; | |
3198 | } | |
3199 | return $result; | |
3200 | } | |
293f42b6 | 3201 | } |
61c8e0d7 | 3202 |