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 | ||
47d6e6a7 DW |
470 | /** |
471 | * Hide warning about changed grades during upgrade to 2.8. | |
472 | * | |
473 | * @param int $courseid The current course id. | |
474 | */ | |
475 | function hide_aggregatesubcats_upgrade_notice($courseid) { | |
476 | unset_config('show_aggregatesubcats_upgrade_' . $courseid); | |
477 | } | |
478 | ||
a9937aec DW |
479 | /** |
480 | * Print warning about changed grades during upgrade to 2.8. | |
481 | * | |
482 | * @param int $courseid The current course id. | |
483 | * @param context $context The course context. | |
eafab799 | 484 | * @param string $thispage The relative path for the current page. E.g. /grade/report/user/index.php |
a9937aec DW |
485 | * @param boolean $return return as string |
486 | * | |
487 | * @return nothing or string if $return true | |
488 | */ | |
eafab799 | 489 | function print_natural_aggregation_upgrade_notice($courseid, $context, $thispage, $return=false) { |
a9937aec DW |
490 | global $OUTPUT; |
491 | $html = ''; | |
a9937aec | 492 | |
eafab799 DW |
493 | $hidesubcatswarning = optional_param('seenaggregatesubcatsupgradedgrades', false, PARAM_BOOL) && confirm_sesskey(); |
494 | $showsubcatswarning = get_config('core', 'show_aggregatesubcats_upgrade_' . $courseid); | |
495 | $hidenaturalwarning = optional_param('seensumofgradesupgradedgrades', false, PARAM_BOOL) && confirm_sesskey(); | |
496 | $shownaturalwarning = get_config('core', 'show_sumofgrades_upgrade_' . $courseid); | |
497 | ||
498 | // Do not do anything if they are not a teacher. | |
499 | if ($hidesubcatswarning || $showsubcatswarning || $hidenaturalwarning || $shownaturalwarning) { | |
500 | if (!has_capability('moodle/grade:manage', $context)) { | |
501 | return ''; | |
502 | } | |
503 | } | |
504 | ||
505 | // Hide the warning if the user told it to go away. | |
506 | if ($hidenaturalwarning) { | |
507 | hide_natural_aggregation_upgrade_notice($courseid); | |
508 | } | |
509 | // Hide the warning if the user told it to go away. | |
510 | if ($hidesubcatswarning) { | |
511 | hide_aggregatesubcats_upgrade_notice($courseid); | |
512 | } | |
513 | ||
514 | if (!$hidenaturalwarning && $shownaturalwarning) { | |
a9937aec | 515 | $message = get_string('sumofgradesupgradedgrades', 'grades'); |
47d6e6a7 | 516 | $hidemessage = get_string('upgradedgradeshidemessage', 'grades'); |
a9937aec DW |
517 | $urlparams = array( 'id' => $courseid, |
518 | 'seensumofgradesupgradedgrades' => true, | |
519 | 'sesskey' => sesskey()); | |
eafab799 | 520 | $goawayurl = new moodle_url($thispage, $urlparams); |
a9937aec DW |
521 | $goawaybutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get'); |
522 | $html .= $OUTPUT->notification($message, 'notifysuccess'); | |
523 | $html .= $goawaybutton; | |
524 | } | |
525 | ||
eafab799 | 526 | if (!$hidesubcatswarning && $showsubcatswarning) { |
47d6e6a7 DW |
527 | $message = get_string('aggregatesubcatsupgradedgrades', 'grades'); |
528 | $hidemessage = get_string('upgradedgradeshidemessage', 'grades'); | |
529 | $urlparams = array( 'id' => $courseid, | |
530 | 'seenaggregatesubcatsupgradedgrades' => true, | |
531 | 'sesskey' => sesskey()); | |
eafab799 | 532 | $goawayurl = new moodle_url($thispage, $urlparams); |
47d6e6a7 DW |
533 | $goawaybutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get'); |
534 | $html .= $OUTPUT->notification($message, 'notifysuccess'); | |
535 | $html .= $goawaybutton; | |
536 | } | |
537 | ||
a9937aec DW |
538 | if ($return) { |
539 | return $html; | |
540 | } else { | |
541 | echo $html; | |
542 | } | |
543 | } | |
544 | ||
0610812a | 545 | /** |
546 | * Print grading plugin selection popup form. | |
547 | * | |
cf72e2dd | 548 | * @param array $plugin_info An array of plugins containing information for the selector |
0610812a | 549 | * @param boolean $return return as string |
cf72e2dd | 550 | * |
0610812a | 551 | * @return nothing or string if $return true |
552 | */ | |
f1a3e072 | 553 | function print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, $return=false) { |
714b4745 | 554 | global $CFG, $OUTPUT, $PAGE; |
dc482cfa | 555 | |
556 | $menu = array(); | |
557 | $count = 0; | |
558 | $active = ''; | |
559 | ||
560 | foreach ($plugin_info as $plugin_type => $plugins) { | |
561 | if ($plugin_type == 'strings') { | |
562 | continue; | |
563 | } | |
564 | ||
7981d537 | 565 | $first_plugin = reset($plugins); |
dc482cfa | 566 | |
f1a3e072 PS |
567 | $sectionname = $plugin_info['strings'][$plugin_type]; |
568 | $section = array(); | |
d4dcfc6b | 569 | |
f1a3e072 | 570 | foreach ($plugins as $plugin) { |
d4dcfc6b | 571 | $link = $plugin->link->out(false); |
f1a3e072 PS |
572 | $section[$link] = $plugin->string; |
573 | $count++; | |
574 | if ($plugin_type === $active_type and $plugin->id === $active_plugin) { | |
575 | $active = $link; | |
dc482cfa | 576 | } |
577 | } | |
f1a3e072 PS |
578 | |
579 | if ($section) { | |
580 | $menu[] = array($sectionname=>$section); | |
581 | } | |
dc482cfa | 582 | } |
583 | ||
cf72e2dd | 584 | // finally print/return the popup form |
dc482cfa | 585 | if ($count > 1) { |
f1a3e072 | 586 | $select = new url_select($menu, $active, null, 'choosepluginreport'); |
29bea634 | 587 | $select->set_label(get_string('gradereport', 'grades'), array('class' => 'accesshide')); |
7981d537 | 588 | if ($return) { |
f1a3e072 | 589 | return $OUTPUT->render($select); |
7981d537 | 590 | } else { |
f1a3e072 | 591 | echo $OUTPUT->render($select); |
7981d537 | 592 | } |
dc482cfa | 593 | } else { |
594 | // only one option - no plugin selector needed | |
595 | return ''; | |
596 | } | |
597 | } | |
598 | ||
599 | /** | |
600 | * Print grading plugin selection tab-based navigation. | |
601 | * | |
cf72e2dd | 602 | * @param string $active_type type of plugin on current page - import, export, report or edit |
603 | * @param string $active_plugin active plugin type - grader, user, cvs, ... | |
604 | * @param array $plugin_info Array of plugins | |
dc482cfa | 605 | * @param boolean $return return as string |
cf72e2dd | 606 | * |
dc482cfa | 607 | * @return nothing or string if $return true |
608 | */ | |
b827784e | 609 | function grade_print_tabs($active_type, $active_plugin, $plugin_info, $return=false) { |
dc482cfa | 610 | global $CFG, $COURSE; |
1c1f64a2 | 611 | |
e1513ca9 | 612 | if (!isset($currenttab)) { //TODO: this is weird |
dc482cfa | 613 | $currenttab = ''; |
614 | } | |
615 | ||
616 | $tabs = array(); | |
617 | $top_row = array(); | |
618 | $bottom_row = array(); | |
619 | $inactive = array($active_plugin); | |
f48d5209 | 620 | $activated = array($active_type); |
dc482cfa | 621 | |
622 | $count = 0; | |
623 | $active = ''; | |
624 | ||
625 | foreach ($plugin_info as $plugin_type => $plugins) { | |
626 | if ($plugin_type == 'strings') { | |
627 | continue; | |
628 | } | |
629 | ||
630 | // If $plugins is actually the definition of a child-less parent link: | |
cf72e2dd | 631 | if (!empty($plugins->id)) { |
632 | $string = $plugins->string; | |
633 | if (!empty($plugin_info[$active_type]->parent)) { | |
634 | $string = $plugin_info[$active_type]->parent->string; | |
27eef3bb | 635 | } |
636 | ||
cf72e2dd | 637 | $top_row[] = new tabobject($plugin_type, $plugins->link, $string); |
dc482cfa | 638 | continue; |
639 | } | |
640 | ||
641 | $first_plugin = reset($plugins); | |
cf72e2dd | 642 | $url = $first_plugin->link; |
dc482cfa | 643 | |
644 | if ($plugin_type == 'report') { | |
645 | $url = $CFG->wwwroot.'/grade/report/index.php?id='.$COURSE->id; | |
646 | } | |
647 | ||
648 | $top_row[] = new tabobject($plugin_type, $url, $plugin_info['strings'][$plugin_type]); | |
649 | ||
650 | if ($active_type == $plugin_type) { | |
651 | foreach ($plugins as $plugin) { | |
cf72e2dd | 652 | $bottom_row[] = new tabobject($plugin->id, $plugin->link, $plugin->string); |
653 | if ($plugin->id == $active_plugin) { | |
654 | $inactive = array($plugin->id); | |
dc482cfa | 655 | } |
656 | } | |
657 | } | |
658 | } | |
659 | ||
660 | $tabs[] = $top_row; | |
661 | $tabs[] = $bottom_row; | |
662 | ||
663 | if ($return) { | |
f48d5209 | 664 | return print_tabs($tabs, $active_plugin, $inactive, $activated, true); |
dc482cfa | 665 | } else { |
f48d5209 | 666 | print_tabs($tabs, $active_plugin, $inactive, $activated); |
dc482cfa | 667 | } |
668 | } | |
669 | ||
cf72e2dd | 670 | /** |
671 | * grade_get_plugin_info | |
672 | * | |
673 | * @param int $courseid The course id | |
674 | * @param string $active_type type of plugin on current page - import, export, report or edit | |
675 | * @param string $active_plugin active plugin type - grader, user, cvs, ... | |
676 | * | |
677 | * @return array | |
678 | */ | |
dc482cfa | 679 | function grade_get_plugin_info($courseid, $active_type, $active_plugin) { |
f7fcf4cd | 680 | global $CFG, $SITE; |
cbff94ba | 681 | |
d4060472 | 682 | $context = context_course::instance($courseid); |
cbff94ba | 683 | |
dc482cfa | 684 | $plugin_info = array(); |
6e2f3121 | 685 | $count = 0; |
3af29899 | 686 | $active = ''; |
dc482cfa | 687 | $url_prefix = $CFG->wwwroot . '/grade/'; |
688 | ||
689 | // Language strings | |
4d5059d4 | 690 | $plugin_info['strings'] = grade_helper::get_plugin_strings(); |
b827784e | 691 | |
4d5059d4 SH |
692 | if ($reports = grade_helper::get_plugins_reports($courseid)) { |
693 | $plugin_info['report'] = $reports; | |
b827784e | 694 | } |
c46aeeab | 695 | |
eb84b779 MG |
696 | if ($settings = grade_helper::get_info_manage_settings($courseid)) { |
697 | $plugin_info['settings'] = $settings; | |
698 | } | |
699 | ||
4d5059d4 SH |
700 | if ($scale = grade_helper::get_info_scales($courseid)) { |
701 | $plugin_info['scale'] = array('view'=>$scale); | |
cbff94ba | 702 | } |
dc482cfa | 703 | |
4d5059d4 SH |
704 | if ($outcomes = grade_helper::get_info_outcomes($courseid)) { |
705 | $plugin_info['outcome'] = $outcomes; | |
cbff94ba | 706 | } |
cbff94ba | 707 | |
4d5059d4 SH |
708 | if ($letters = grade_helper::get_info_letters($courseid)) { |
709 | $plugin_info['letter'] = $letters; | |
cbff94ba | 710 | } |
4d5059d4 SH |
711 | |
712 | if ($imports = grade_helper::get_plugins_import($courseid)) { | |
713 | $plugin_info['import'] = $imports; | |
281ffa4a | 714 | } |
c46aeeab | 715 | |
4d5059d4 SH |
716 | if ($exports = grade_helper::get_plugins_export($courseid)) { |
717 | $plugin_info['export'] = $exports; | |
281ffa4a | 718 | } |
281ffa4a | 719 | |
4d5059d4 SH |
720 | foreach ($plugin_info as $plugin_type => $plugins) { |
721 | if (!empty($plugins->id) && $active_plugin == $plugins->id) { | |
722 | $plugin_info['strings']['active_plugin_str'] = $plugins->string; | |
723 | break; | |
281ffa4a | 724 | } |
4d5059d4 SH |
725 | foreach ($plugins as $plugin) { |
726 | if (is_a($plugin, 'grade_plugin_info')) { | |
727 | if ($active_plugin == $plugin->id) { | |
728 | $plugin_info['strings']['active_plugin_str'] = $plugin->string; | |
729 | } | |
3af29899 | 730 | } |
281ffa4a | 731 | } |
cbff94ba | 732 | } |
c46aeeab | 733 | |
dc482cfa | 734 | foreach ($plugin_info as $plugin_type => $plugins) { |
cf72e2dd | 735 | if (!empty($plugins->id) && $active_plugin == $plugins->id) { |
736 | $plugin_info['strings']['active_plugin_str'] = $plugins->string; | |
dc482cfa | 737 | break; |
78ad5f3f | 738 | } |
dc482cfa | 739 | foreach ($plugins as $plugin) { |
cf72e2dd | 740 | if (is_a($plugin, 'grade_plugin_info')) { |
741 | if ($active_plugin == $plugin->id) { | |
742 | $plugin_info['strings']['active_plugin_str'] = $plugin->string; | |
743 | } | |
78ad5f3f | 744 | } |
78ad5f3f | 745 | } |
dc482cfa | 746 | } |
78ad5f3f | 747 | |
dc482cfa | 748 | return $plugin_info; |
749 | } | |
284abb09 | 750 | |
cf72e2dd | 751 | /** |
752 | * A simple class containing info about grade plugins. | |
753 | * Can be subclassed for special rules | |
754 | * | |
a153c9f2 | 755 | * @package core_grades |
cf72e2dd | 756 | * @copyright 2009 Nicolas Connault |
757 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
758 | */ | |
759 | class grade_plugin_info { | |
760 | /** | |
761 | * A unique id for this plugin | |
762 | * | |
763 | * @var mixed | |
764 | */ | |
765 | public $id; | |
766 | /** | |
767 | * A URL to access this plugin | |
768 | * | |
769 | * @var mixed | |
770 | */ | |
771 | public $link; | |
772 | /** | |
773 | * The name of this plugin | |
774 | * | |
775 | * @var mixed | |
776 | */ | |
777 | public $string; | |
778 | /** | |
779 | * Another grade_plugin_info object, parent of the current one | |
780 | * | |
781 | * @var mixed | |
782 | */ | |
783 | public $parent; | |
784 | ||
785 | /** | |
786 | * Constructor | |
787 | * | |
788 | * @param int $id A unique id for this plugin | |
789 | * @param string $link A URL to access this plugin | |
790 | * @param string $string The name of this plugin | |
791 | * @param object $parent Another grade_plugin_info object, parent of the current one | |
792 | * | |
793 | * @return void | |
794 | */ | |
795 | public function __construct($id, $link, $string, $parent=null) { | |
796 | $this->id = $id; | |
797 | $this->link = $link; | |
798 | $this->string = $string; | |
799 | $this->parent = $parent; | |
800 | } | |
801 | } | |
802 | ||
de0300ea | 803 | /** |
804 | * Prints the page headers, breadcrumb trail, page heading, (optional) dropdown navigation menu and | |
805 | * (optional) navigation tabs for any gradebook page. All gradebook pages MUST use these functions | |
806 | * in favour of the usual print_header(), print_header_simple(), print_heading() etc. | |
807 | * !IMPORTANT! Use of tabs.php file in gradebook pages is forbidden unless tabs are switched off at | |
808 | * the site level for the gradebook ($CFG->grade_navmethod = GRADE_NAVMETHOD_DROPDOWN). | |
809 | * | |
cf72e2dd | 810 | * @param int $courseid Course id |
811 | * @param string $active_type The type of the current page (report, settings, | |
812 | * import, export, scales, outcomes, letters) | |
813 | * @param string $active_plugin The plugin of the current page (grader, fullview etc...) | |
814 | * @param string $heading The heading of the page. Tries to guess if none is given | |
de0300ea | 815 | * @param boolean $return Whether to return (true) or echo (false) the HTML generated by this function |
cf72e2dd | 816 | * @param string $bodytags Additional attributes that will be added to the <body> tag |
817 | * @param string $buttons Additional buttons to display on the page | |
5625d33d | 818 | * @param boolean $shownavigation should the gradebook navigation drop down (or tabs) be shown? |
1464bcc8 AG |
819 | * @param string $headerhelpidentifier The help string identifier if required. |
820 | * @param string $headerhelpcomponent The component for the help string. | |
de0300ea | 821 | * |
822 | * @return string HTML code or nothing if $return == false | |
823 | */ | |
cf72e2dd | 824 | function print_grade_page_head($courseid, $active_type, $active_plugin=null, |
34a2777c | 825 | $heading = false, $return=false, |
1464bcc8 | 826 | $buttons=false, $shownavigation=true, $headerhelpidentifier = null, $headerhelpcomponent = null) { |
4d5059d4 | 827 | global $CFG, $OUTPUT, $PAGE; |
c46aeeab | 828 | |
eb84b779 MG |
829 | if ($active_type === 'preferences') { |
830 | // In Moodle 2.8 report preferences were moved under 'settings'. Allow backward compatibility for 3rd party grade reports. | |
831 | $active_type = 'settings'; | |
832 | } | |
833 | ||
dc482cfa | 834 | $plugin_info = grade_get_plugin_info($courseid, $active_type, $active_plugin); |
c46aeeab | 835 | |
dc482cfa | 836 | // Determine the string of the active plugin |
837 | $stractive_plugin = ($active_plugin) ? $plugin_info['strings']['active_plugin_str'] : $heading; | |
838 | $stractive_type = $plugin_info['strings'][$active_type]; | |
e0724506 | 839 | |
cf72e2dd | 840 | if (empty($plugin_info[$active_type]->id) || !empty($plugin_info[$active_type]->parent)) { |
4d5059d4 SH |
841 | $title = $PAGE->course->fullname.': ' . $stractive_type . ': ' . $stractive_plugin; |
842 | } else { | |
843 | $title = $PAGE->course->fullname.': ' . $stractive_plugin; | |
c4c97a6d | 844 | } |
845 | ||
367a75fa SH |
846 | if ($active_type == 'report') { |
847 | $PAGE->set_pagelayout('report'); | |
848 | } else { | |
849 | $PAGE->set_pagelayout('admin'); | |
850 | } | |
4d5059d4 | 851 | $PAGE->set_title(get_string('grades') . ': ' . $stractive_type); |
f3df5e14 | 852 | $PAGE->set_heading($title); |
4d5059d4 SH |
853 | if ($buttons instanceof single_button) { |
854 | $buttons = $OUTPUT->render($buttons); | |
855 | } | |
f3df5e14 | 856 | $PAGE->set_button($buttons); |
6bf696bb MG |
857 | if ($courseid != SITEID) { |
858 | grade_extend_settings($plugin_info, $courseid); | |
859 | } | |
4d5059d4 | 860 | |
f3df5e14 | 861 | $returnval = $OUTPUT->header(); |
1464bcc8 | 862 | |
f3df5e14 | 863 | if (!$return) { |
864 | echo $returnval; | |
865 | } | |
dc482cfa | 866 | |
867 | // Guess heading if not given explicitly | |
868 | if (!$heading) { | |
869 | $heading = $stractive_plugin; | |
870 | } | |
871 | ||
2821c495 | 872 | if ($shownavigation) { |
6bf696bb MG |
873 | if ($courseid != SITEID && |
874 | ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_DROPDOWN)) { | |
2821c495 AD |
875 | $returnval .= print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, $return); |
876 | } | |
5625d33d | 877 | |
1464bcc8 AG |
878 | $output = ''; |
879 | // Add a help dialogue box if provided. | |
880 | if (isset($headerhelpidentifier)) { | |
881 | $output = $OUTPUT->heading_with_help($heading, $headerhelpidentifier, $headerhelpcomponent); | |
882 | } else { | |
883 | $output = $OUTPUT->heading($heading); | |
884 | } | |
885 | ||
5625d33d | 886 | if ($return) { |
1464bcc8 | 887 | $returnval .= $output; |
5625d33d | 888 | } else { |
1464bcc8 | 889 | echo $output; |
5625d33d AD |
890 | } |
891 | ||
6bf696bb MG |
892 | if ($courseid != SITEID && |
893 | ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_TABS)) { | |
2821c495 AD |
894 | $returnval .= grade_print_tabs($active_type, $active_plugin, $plugin_info, $return); |
895 | } | |
8354e716 | 896 | } |
dc482cfa | 897 | |
eafab799 DW |
898 | $returnval .= print_natural_aggregation_upgrade_notice($courseid, |
899 | context_course::instance($courseid), | |
f01972d9 | 900 | $PAGE->url, |
eafab799 DW |
901 | $return); |
902 | ||
dc482cfa | 903 | if ($return) { |
904 | return $returnval; | |
6e2f3121 | 905 | } |
cbff94ba | 906 | } |
907 | ||
0610812a | 908 | /** |
7a6b7acf | 909 | * Utility class used for return tracking when using edit and other forms in grade plugins |
cf72e2dd | 910 | * |
a153c9f2 | 911 | * @package core_grades |
cf72e2dd | 912 | * @copyright 2009 Nicolas Connault |
913 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
0610812a | 914 | */ |
3af29899 | 915 | class grade_plugin_return { |
d24832f9 | 916 | public $type; |
917 | public $plugin; | |
918 | public $courseid; | |
919 | public $userid; | |
920 | public $page; | |
281ffa4a | 921 | |
0610812a | 922 | /** |
923 | * Constructor | |
cf72e2dd | 924 | * |
0610812a | 925 | * @param array $params - associative array with return parameters, if null parameter are taken from _GET or _POST |
926 | */ | |
cf72e2dd | 927 | public function grade_plugin_return($params = null) { |
3af29899 | 928 | if (empty($params)) { |
929 | $this->type = optional_param('gpr_type', null, PARAM_SAFEDIR); | |
aff24313 | 930 | $this->plugin = optional_param('gpr_plugin', null, PARAM_PLUGIN); |
3af29899 | 931 | $this->courseid = optional_param('gpr_courseid', null, PARAM_INT); |
932 | $this->userid = optional_param('gpr_userid', null, PARAM_INT); | |
933 | $this->page = optional_param('gpr_page', null, PARAM_INT); | |
a983b6ec | 934 | |
a983b6ec | 935 | } else { |
3af29899 | 936 | foreach ($params as $key=>$value) { |
4cc977a6 | 937 | if (property_exists($this, $key)) { |
3af29899 | 938 | $this->$key = $value; |
939 | } | |
cbff94ba | 940 | } |
941 | } | |
6cd8c592 | 942 | } |
943 | ||
0610812a | 944 | /** |
945 | * Returns return parameters as options array suitable for buttons. | |
946 | * @return array options | |
947 | */ | |
d24832f9 | 948 | public function get_options() { |
7a6b7acf | 949 | if (empty($this->type)) { |
3af29899 | 950 | return array(); |
865e9a82 | 951 | } |
6cd8c592 | 952 | |
3af29899 | 953 | $params = array(); |
6cd8c592 | 954 | |
7a6b7acf | 955 | if (!empty($this->plugin)) { |
956 | $params['plugin'] = $this->plugin; | |
957 | } | |
6cd8c592 | 958 | |
3af29899 | 959 | if (!empty($this->courseid)) { |
960 | $params['id'] = $this->courseid; | |
6cd8c592 | 961 | } |
9c61ba4d | 962 | |
3af29899 | 963 | if (!empty($this->userid)) { |
964 | $params['userid'] = $this->userid; | |
9c61ba4d | 965 | } |
9c61ba4d | 966 | |
3af29899 | 967 | if (!empty($this->page)) { |
968 | $params['page'] = $this->page; | |
cbff94ba | 969 | } |
865e9a82 | 970 | |
3af29899 | 971 | return $params; |
cbff94ba | 972 | } |
cbff94ba | 973 | |
0610812a | 974 | /** |
975 | * Returns return url | |
cf72e2dd | 976 | * |
0610812a | 977 | * @param string $default default url when params not set |
cf72e2dd | 978 | * @param array $extras Extra URL parameters |
979 | * | |
0610812a | 980 | * @return string url |
981 | */ | |
d24832f9 | 982 | public function get_return_url($default, $extras=null) { |
3af29899 | 983 | global $CFG; |
cbff94ba | 984 | |
3af29899 | 985 | if (empty($this->type) or empty($this->plugin)) { |
986 | return $default; | |
cbff94ba | 987 | } |
988 | ||
65dd61bd | 989 | $url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php'; |
990 | $glue = '?'; | |
cbff94ba | 991 | |
3af29899 | 992 | if (!empty($this->courseid)) { |
993 | $url .= $glue.'id='.$this->courseid; | |
994 | $glue = '&'; | |
cbff94ba | 995 | } |
cbff94ba | 996 | |
3af29899 | 997 | if (!empty($this->userid)) { |
998 | $url .= $glue.'userid='.$this->userid; | |
999 | $glue = '&'; | |
cbff94ba | 1000 | } |
7e2d7c92 | 1001 | |
3af29899 | 1002 | if (!empty($this->page)) { |
1003 | $url .= $glue.'page='.$this->page; | |
65dd61bd | 1004 | $glue = '&'; |
1005 | } | |
1006 | ||
1007 | if (!empty($extras)) { | |
cf72e2dd | 1008 | foreach ($extras as $key=>$value) { |
65dd61bd | 1009 | $url .= $glue.$key.'='.$value; |
1010 | $glue = '&'; | |
7a6b7acf | 1011 | } |
cbff94ba | 1012 | } |
cbff94ba | 1013 | |
3af29899 | 1014 | return $url; |
cbff94ba | 1015 | } |
cbff94ba | 1016 | |
0610812a | 1017 | /** |
1018 | * Returns string with hidden return tracking form elements. | |
1019 | * @return string | |
1020 | */ | |
d24832f9 | 1021 | public function get_form_fields() { |
7a6b7acf | 1022 | if (empty($this->type)) { |
3af29899 | 1023 | return ''; |
cbff94ba | 1024 | } |
cbff94ba | 1025 | |
3af29899 | 1026 | $result = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />'; |
7a6b7acf | 1027 | |
1028 | if (!empty($this->plugin)) { | |
1029 | $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />'; | |
1030 | } | |
0ca5abd6 | 1031 | |
3af29899 | 1032 | if (!empty($this->courseid)) { |
1033 | $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />'; | |
cbff94ba | 1034 | } |
cbff94ba | 1035 | |
3af29899 | 1036 | if (!empty($this->userid)) { |
1037 | $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />'; | |
cbff94ba | 1038 | } |
cbff94ba | 1039 | |
3af29899 | 1040 | if (!empty($this->page)) { |
1041 | $result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />'; | |
cbff94ba | 1042 | } |
1043 | } | |
cbff94ba | 1044 | |
0610812a | 1045 | /** |
1046 | * Add hidden elements into mform | |
cf72e2dd | 1047 | * |
1048 | * @param object &$mform moodle form object | |
1049 | * | |
0610812a | 1050 | * @return void |
1051 | */ | |
d24832f9 | 1052 | public function add_mform_elements(&$mform) { |
7a6b7acf | 1053 | if (empty($this->type)) { |
3af29899 | 1054 | return; |
cbff94ba | 1055 | } |
cbff94ba | 1056 | |
3af29899 | 1057 | $mform->addElement('hidden', 'gpr_type', $this->type); |
1058 | $mform->setType('gpr_type', PARAM_SAFEDIR); | |
cbff94ba | 1059 | |
7a6b7acf | 1060 | if (!empty($this->plugin)) { |
1061 | $mform->addElement('hidden', 'gpr_plugin', $this->plugin); | |
aff24313 | 1062 | $mform->setType('gpr_plugin', PARAM_PLUGIN); |
7a6b7acf | 1063 | } |
97033c86 | 1064 | |
3af29899 | 1065 | if (!empty($this->courseid)) { |
1066 | $mform->addElement('hidden', 'gpr_courseid', $this->courseid); | |
1067 | $mform->setType('gpr_courseid', PARAM_INT); | |
cbff94ba | 1068 | } |
cbff94ba | 1069 | |
3af29899 | 1070 | if (!empty($this->userid)) { |
1071 | $mform->addElement('hidden', 'gpr_userid', $this->userid); | |
1072 | $mform->setType('gpr_userid', PARAM_INT); | |
cbff94ba | 1073 | } |
cbff94ba | 1074 | |
3af29899 | 1075 | if (!empty($this->page)) { |
1076 | $mform->addElement('hidden', 'gpr_page', $this->page); | |
1077 | $mform->setType('gpr_page', PARAM_INT); | |
cbff94ba | 1078 | } |
1079 | } | |
281ffa4a | 1080 | |
0610812a | 1081 | /** |
1082 | * Add return tracking params into url | |
cf72e2dd | 1083 | * |
1c1f64a2 | 1084 | * @param moodle_url $url A URL |
cf72e2dd | 1085 | * |
6ef4878b | 1086 | * @return string $url with return tracking params |
0610812a | 1087 | */ |
1c1f64a2 | 1088 | public function add_url_params(moodle_url $url) { |
7a6b7acf | 1089 | if (empty($this->type)) { |
3af29899 | 1090 | return $url; |
cbff94ba | 1091 | } |
5609f9e6 | 1092 | |
1c1f64a2 | 1093 | $url->param('gpr_type', $this->type); |
cbff94ba | 1094 | |
7a6b7acf | 1095 | if (!empty($this->plugin)) { |
1c1f64a2 | 1096 | $url->param('gpr_plugin', $this->plugin); |
7a6b7acf | 1097 | } |
cbff94ba | 1098 | |
3af29899 | 1099 | if (!empty($this->courseid)) { |
1c1f64a2 | 1100 | $url->param('gpr_courseid' ,$this->courseid); |
cbff94ba | 1101 | } |
cbff94ba | 1102 | |
3af29899 | 1103 | if (!empty($this->userid)) { |
1c1f64a2 | 1104 | $url->param('gpr_userid', $this->userid); |
cbff94ba | 1105 | } |
0a8a95c9 | 1106 | |
3af29899 | 1107 | if (!empty($this->page)) { |
1c1f64a2 | 1108 | $url->param('gpr_page', $this->page); |
0a8a95c9 | 1109 | } |
5a412dbf | 1110 | |
3af29899 | 1111 | return $url; |
5a412dbf | 1112 | } |
5a412dbf | 1113 | } |
7a6b7acf | 1114 | |
826c5f86 | 1115 | /** |
1116 | * Function central to gradebook for building and printing the navigation (breadcrumb trail). | |
cf72e2dd | 1117 | * |
826c5f86 | 1118 | * @param string $path The path of the calling script (using __FILE__?) |
1119 | * @param string $pagename The language string to use as the last part of the navigation (non-link) | |
cf72e2dd | 1120 | * @param mixed $id Either a plain integer (assuming the key is 'id') or |
1121 | * an array of keys and values (e.g courseid => $courseid, itemid...) | |
1122 | * | |
826c5f86 | 1123 | * @return string |
1124 | */ | |
1125 | function grade_build_nav($path, $pagename=null, $id=null) { | |
f3df5e14 | 1126 | global $CFG, $COURSE, $PAGE; |
826c5f86 | 1127 | |
1128 | $strgrades = get_string('grades', 'grades'); | |
1129 | ||
1130 | // Parse the path and build navlinks from its elements | |
1131 | $dirroot_length = strlen($CFG->dirroot) + 1; // Add 1 for the first slash | |
1132 | $path = substr($path, $dirroot_length); | |
1133 | $path = str_replace('\\', '/', $path); | |
1134 | ||
1135 | $path_elements = explode('/', $path); | |
1136 | ||
1137 | $path_elements_count = count($path_elements); | |
1138 | ||
826c5f86 | 1139 | // First link is always 'grade' |
a6855934 | 1140 | $PAGE->navbar->add($strgrades, new moodle_url('/grade/index.php', array('id'=>$COURSE->id))); |
826c5f86 | 1141 | |
f3df5e14 | 1142 | $link = null; |
826c5f86 | 1143 | $numberofelements = 3; |
1144 | ||
1145 | // Prepare URL params string | |
f3df5e14 | 1146 | $linkparams = array(); |
826c5f86 | 1147 | if (!is_null($id)) { |
1148 | if (is_array($id)) { | |
1149 | foreach ($id as $idkey => $idvalue) { | |
f3df5e14 | 1150 | $linkparams[$idkey] = $idvalue; |
826c5f86 | 1151 | } |
1152 | } else { | |
f3df5e14 | 1153 | $linkparams['id'] = $id; |
826c5f86 | 1154 | } |
1155 | } | |
1156 | ||
1157 | $navlink4 = null; | |
1158 | ||
0f78c4de | 1159 | // Remove file extensions from filenames |
1160 | foreach ($path_elements as $key => $filename) { | |
1161 | $path_elements[$key] = str_replace('.php', '', $filename); | |
1162 | } | |
1163 | ||
826c5f86 | 1164 | // Second level links |
1165 | switch ($path_elements[1]) { | |
1166 | case 'edit': // No link | |
1167 | if ($path_elements[3] != 'index.php') { | |
1168 | $numberofelements = 4; | |
1169 | } | |
1170 | break; | |
1171 | case 'import': // No link | |
1172 | break; | |
1173 | case 'export': // No link | |
1174 | break; | |
1175 | case 'report': | |
1176 | // $id is required for this link. Do not print it if $id isn't given | |
1177 | if (!is_null($id)) { | |
a6855934 | 1178 | $link = new moodle_url('/grade/report/index.php', $linkparams); |
826c5f86 | 1179 | } |
1180 | ||
1181 | if ($path_elements[2] == 'grader') { | |
1182 | $numberofelements = 4; | |
1183 | } | |
1184 | break; | |
1185 | ||
1186 | default: | |
1187 | // If this element isn't among the ones already listed above, it isn't supported, throw an error. | |
cf72e2dd | 1188 | debugging("grade_build_nav() doesn't support ". $path_elements[1] . |
1189 | " as the second path element after 'grade'."); | |
826c5f86 | 1190 | return false; |
1191 | } | |
f3df5e14 | 1192 | $PAGE->navbar->add(get_string($path_elements[1], 'grades'), $link); |
826c5f86 | 1193 | |
1194 | // Third level links | |
1195 | if (empty($pagename)) { | |
1196 | $pagename = get_string($path_elements[2], 'grades'); | |
1197 | } | |
1198 | ||
1199 | switch ($numberofelements) { | |
1200 | case 3: | |
f3df5e14 | 1201 | $PAGE->navbar->add($pagename, $link); |
826c5f86 | 1202 | break; |
1203 | case 4: | |
826c5f86 | 1204 | if ($path_elements[2] == 'grader' AND $path_elements[3] != 'index.php') { |
b5e7b2bf | 1205 | $PAGE->navbar->add(get_string('pluginname', 'gradereport_grader'), new moodle_url('/grade/report/grader/index.php', $linkparams)); |
826c5f86 | 1206 | } |
f3df5e14 | 1207 | $PAGE->navbar->add($pagename); |
826c5f86 | 1208 | break; |
1209 | } | |
826c5f86 | 1210 | |
f3df5e14 | 1211 | return ''; |
d4795a07 | 1212 | } |
7a6b7acf | 1213 | |
e98871a2 | 1214 | /** |
6cc3e350 | 1215 | * General structure representing grade items in course |
cf72e2dd | 1216 | * |
a153c9f2 | 1217 | * @package core_grades |
cf72e2dd | 1218 | * @copyright 2009 Nicolas Connault |
1219 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
e98871a2 | 1220 | */ |
6cc3e350 | 1221 | class grade_structure { |
d24832f9 | 1222 | public $context; |
e98871a2 | 1223 | |
d24832f9 | 1224 | public $courseid; |
e98871a2 | 1225 | |
73ca5f01 | 1226 | /** |
1227 | * Reference to modinfo for current course (for performance, to save | |
1228 | * retrieving it from courseid every time). Not actually set except for | |
1229 | * the grade_tree type. | |
1230 | * @var course_modinfo | |
1231 | */ | |
1232 | public $modinfo; | |
1233 | ||
e98871a2 | 1234 | /** |
1235 | * 1D array of grade items only | |
1236 | */ | |
d24832f9 | 1237 | public $items; |
e98871a2 | 1238 | |
6391ebe7 | 1239 | /** |
6cc3e350 | 1240 | * Returns icon of element |
cf72e2dd | 1241 | * |
1242 | * @param array &$element An array representing an element in the grade_tree | |
1243 | * @param bool $spacerifnone return spacer if no icon found | |
1244 | * | |
6cc3e350 | 1245 | * @return string icon or spacer |
6391ebe7 | 1246 | */ |
d24832f9 | 1247 | public function get_element_icon(&$element, $spacerifnone=false) { |
6b608f8f | 1248 | global $CFG, $OUTPUT; |
559276b1 | 1249 | require_once $CFG->libdir.'/filelib.php'; |
6cc3e350 | 1250 | |
1251 | switch ($element['type']) { | |
1252 | case 'item': | |
1253 | case 'courseitem': | |
1254 | case 'categoryitem': | |
cf72e2dd | 1255 | $is_course = $element['object']->is_course_item(); |
1256 | $is_category = $element['object']->is_category_item(); | |
1257 | $is_scale = $element['object']->gradetype == GRADE_TYPE_SCALE; | |
1258 | $is_value = $element['object']->gradetype == GRADE_TYPE_VALUE; | |
0077f571 | 1259 | $is_outcome = !empty($element['object']->outcomeid); |
cf72e2dd | 1260 | |
6cc3e350 | 1261 | if ($element['object']->is_calculated()) { |
dc482cfa | 1262 | $strcalc = get_string('calculatedgrade', 'grades'); |
b5d0cafc | 1263 | return '<img src="'.$OUTPUT->pix_url('i/calc') . '" class="icon itemicon" title="'. |
cf72e2dd | 1264 | s($strcalc).'" alt="'.s($strcalc).'"/>'; |
6cc3e350 | 1265 | |
cf72e2dd | 1266 | } else if (($is_course or $is_category) and ($is_scale or $is_value)) { |
6cc3e350 | 1267 | if ($category = $element['object']->get_item_category()) { |
bfe969e8 DW |
1268 | $aggrstrings = grade_helper::get_aggregation_strings(); |
1269 | $stragg = $aggrstrings[$category->aggregation]; | |
6cc3e350 | 1270 | switch ($category->aggregation) { |
1271 | case GRADE_AGGREGATE_MEAN: | |
1272 | case GRADE_AGGREGATE_MEDIAN: | |
1273 | case GRADE_AGGREGATE_WEIGHTED_MEAN: | |
1426edac | 1274 | case GRADE_AGGREGATE_WEIGHTED_MEAN2: |
6cc3e350 | 1275 | case GRADE_AGGREGATE_EXTRACREDIT_MEAN: |
b5d0cafc | 1276 | return '<img src="'.$OUTPUT->pix_url('i/agg_mean') . '" ' . |
cf72e2dd | 1277 | 'class="icon itemicon" title="'.s($stragg).'" alt="'.s($stragg).'"/>'; |
0758a08e | 1278 | case GRADE_AGGREGATE_SUM: |
b5d0cafc | 1279 | return '<img src="'.$OUTPUT->pix_url('i/agg_sum') . '" ' . |
cf72e2dd | 1280 | 'class="icon itemicon" title="'.s($stragg).'" alt="'.s($stragg).'"/>'; |
9dd80882 DW |
1281 | default: |
1282 | return '<img src="'.$OUTPUT->pix_url('i/calc') . '" ' . | |
1283 | 'class="icon itemicon" title="'.s($stragg).'" alt="'.s($stragg).'"/>'; | |
6cc3e350 | 1284 | } |
1285 | } | |
1286 | ||
1287 | } else if ($element['object']->itemtype == 'mod') { | |
0077f571 AD |
1288 | //prevent outcomes being displaying the same icon as the activity they are attached to |
1289 | if ($is_outcome) { | |
1290 | $stroutcome = s(get_string('outcome', 'grades')); | |
1291 | return '<img src="'.$OUTPUT->pix_url('i/outcomes') . '" ' . | |
1292 | 'class="icon itemicon" title="'.$stroutcome. | |
1293 | '" alt="'.$stroutcome.'"/>'; | |
1294 | } else { | |
1295 | $strmodname = get_string('modulename', $element['object']->itemmodule); | |
1296 | return '<img src="'.$OUTPUT->pix_url('icon', | |
e63f88c9 | 1297 | $element['object']->itemmodule) . '" ' . |
cf72e2dd | 1298 | 'class="icon itemicon" title="' .s($strmodname). |
1299 | '" alt="' .s($strmodname).'"/>'; | |
0077f571 | 1300 | } |
6cc3e350 | 1301 | } else if ($element['object']->itemtype == 'manual') { |
1302 | if ($element['object']->is_outcome_item()) { | |
dc482cfa | 1303 | $stroutcome = get_string('outcome', 'grades'); |
b5d0cafc | 1304 | return '<img src="'.$OUTPUT->pix_url('i/outcomes') . '" ' . |
cf72e2dd | 1305 | 'class="icon itemicon" title="'.s($stroutcome). |
1306 | '" alt="'.s($stroutcome).'"/>'; | |
6cc3e350 | 1307 | } else { |
dc482cfa | 1308 | $strmanual = get_string('manualitem', 'grades'); |
0cddd851 | 1309 | return '<img src="'.$OUTPUT->pix_url('i/manual_item') . '" '. |
cf72e2dd | 1310 | 'class="icon itemicon" title="'.s($strmanual). |
1311 | '" alt="'.s($strmanual).'"/>'; | |
6cc3e350 | 1312 | } |
1313 | } | |
1314 | break; | |
1315 | ||
1316 | case 'category': | |
dc482cfa | 1317 | $strcat = get_string('category', 'grades'); |
0cddd851 | 1318 | return '<img src="'.$OUTPUT->pix_url('i/folder') . '" class="icon itemicon" ' . |
cf72e2dd | 1319 | 'title="'.s($strcat).'" alt="'.s($strcat).'" />'; |
6cc3e350 | 1320 | } |
1321 | ||
1322 | if ($spacerifnone) { | |
b11681e0 | 1323 | return $OUTPUT->spacer().' '; |
6cc3e350 | 1324 | } else { |
1325 | return ''; | |
1326 | } | |
1327 | } | |
6391ebe7 | 1328 | |
e98871a2 | 1329 | /** |
6cc3e350 | 1330 | * Returns name of element optionally with icon and link |
cf72e2dd | 1331 | * |
1332 | * @param array &$element An array representing an element in the grade_tree | |
1333 | * @param bool $withlink Whether or not this header has a link | |
1334 | * @param bool $icon Whether or not to display an icon with this header | |
1335 | * @param bool $spacerifnone return spacer if no icon found | |
65c2ac93 | 1336 | * @param bool $withdescription Show description if defined by this item. |
41e62a90 AN |
1337 | * @param bool $fulltotal If the item is a category total, returns $categoryname."total" |
1338 | * instead of "Category total" or "Course total" | |
cf72e2dd | 1339 | * |
1340 | * @return string header | |
e98871a2 | 1341 | */ |
41e62a90 AN |
1342 | public function get_element_header(&$element, $withlink = false, $icon = true, $spacerifnone = false, |
1343 | $withdescription = false, $fulltotal = false) { | |
6cc3e350 | 1344 | $header = ''; |
1345 | ||
1346 | if ($icon) { | |
1347 | $header .= $this->get_element_icon($element, $spacerifnone); | |
1348 | } | |
1349 | ||
41e62a90 | 1350 | $header .= $element['object']->get_name($fulltotal); |
6cc3e350 | 1351 | |
cf72e2dd | 1352 | if ($element['type'] != 'item' and $element['type'] != 'categoryitem' and |
1353 | $element['type'] != 'courseitem') { | |
6cc3e350 | 1354 | return $header; |
1355 | } | |
1356 | ||
43cd76e8 JC |
1357 | if ($withlink && $url = $this->get_activity_link($element)) { |
1358 | $a = new stdClass(); | |
1359 | $a->name = get_string('modulename', $element['object']->itemmodule); | |
1360 | $title = get_string('linktoactivity', 'grades', $a); | |
1361 | ||
1362 | $header = html_writer::link($url, $header, array('title' => $title)); | |
1363 | } else { | |
1364 | $header = html_writer::span($header); | |
6cc3e350 | 1365 | } |
1366 | ||
65c2ac93 DW |
1367 | if ($withdescription) { |
1368 | $desc = $element['object']->get_description(); | |
1369 | if (!empty($desc)) { | |
1370 | $header .= '<div class="gradeitemdescription">' . s($desc) . '</div><div class="gradeitemdescriptionfiller"></div>'; | |
1371 | } | |
1372 | } | |
1373 | ||
6cc3e350 | 1374 | return $header; |
1375 | } | |
1376 | ||
73ca5f01 | 1377 | private function get_activity_link($element) { |
1378 | global $CFG; | |
eff314b0 DM |
1379 | /** @var array static cache of the grade.php file existence flags */ |
1380 | static $hasgradephp = array(); | |
73ca5f01 | 1381 | |
1382 | $itemtype = $element['object']->itemtype; | |
1383 | $itemmodule = $element['object']->itemmodule; | |
1384 | $iteminstance = $element['object']->iteminstance; | |
25ab1d50 | 1385 | $itemnumber = $element['object']->itemnumber; |
73ca5f01 | 1386 | |
1387 | // Links only for module items that have valid instance, module and are | |
1388 | // called from grade_tree with valid modinfo | |
1389 | if ($itemtype != 'mod' || !$iteminstance || !$itemmodule || !$this->modinfo) { | |
1390 | return null; | |
1391 | } | |
1392 | ||
1393 | // Get $cm efficiently and with visibility information using modinfo | |
1394 | $instances = $this->modinfo->get_instances(); | |
1395 | if (empty($instances[$itemmodule][$iteminstance])) { | |
1396 | return null; | |
1397 | } | |
1398 | $cm = $instances[$itemmodule][$iteminstance]; | |
1399 | ||
1400 | // Do not add link if activity is not visible to the current user | |
1401 | if (!$cm->uservisible) { | |
1402 | return null; | |
1403 | } | |
1404 | ||
eff314b0 DM |
1405 | if (!array_key_exists($itemmodule, $hasgradephp)) { |
1406 | if (file_exists($CFG->dirroot . '/mod/' . $itemmodule . '/grade.php')) { | |
1407 | $hasgradephp[$itemmodule] = true; | |
1408 | } else { | |
1409 | $hasgradephp[$itemmodule] = false; | |
1410 | } | |
1411 | } | |
1412 | ||
73ca5f01 | 1413 | // If module has grade.php, link to that, otherwise view.php |
eff314b0 | 1414 | if ($hasgradephp[$itemmodule]) { |
c97933ff MG |
1415 | $args = array('id' => $cm->id, 'itemnumber' => $itemnumber); |
1416 | if (isset($element['userid'])) { | |
1417 | $args['userid'] = $element['userid']; | |
1418 | } | |
1419 | return new moodle_url('/mod/' . $itemmodule . '/grade.php', $args); | |
73ca5f01 | 1420 | } else { |
1421 | return new moodle_url('/mod/' . $itemmodule . '/view.php', array('id' => $cm->id)); | |
1422 | } | |
1423 | } | |
1424 | ||
bb17ac1e DM |
1425 | /** |
1426 | * Returns URL of a page that is supposed to contain detailed grade analysis | |
1427 | * | |
bb17ac1e DM |
1428 | * At the moment, only activity modules are supported. The method generates link |
1429 | * to the module's file grade.php with the parameters id (cmid), itemid, itemnumber, | |
eff314b0 | 1430 | * gradeid and userid. If the grade.php does not exist, null is returned. |
bb17ac1e DM |
1431 | * |
1432 | * @return moodle_url|null URL or null if unable to construct it | |
1433 | */ | |
1434 | public function get_grade_analysis_url(grade_grade $grade) { | |
eff314b0 DM |
1435 | global $CFG; |
1436 | /** @var array static cache of the grade.php file existence flags */ | |
1437 | static $hasgradephp = array(); | |
bb17ac1e DM |
1438 | |
1439 | if (empty($grade->grade_item) or !($grade->grade_item instanceof grade_item)) { | |
1440 | throw new coding_exception('Passed grade without the associated grade item'); | |
1441 | } | |
1442 | $item = $grade->grade_item; | |
1443 | ||
1444 | if (!$item->is_external_item()) { | |
1445 | // at the moment, only activity modules are supported | |
1446 | return null; | |
1447 | } | |
1448 | if ($item->itemtype !== 'mod') { | |
1449 | throw new coding_exception('Unknown external itemtype: '.$item->itemtype); | |
1450 | } | |
1451 | if (empty($item->iteminstance) or empty($item->itemmodule) or empty($this->modinfo)) { | |
1452 | return null; | |
1453 | } | |
1454 | ||
eff314b0 DM |
1455 | if (!array_key_exists($item->itemmodule, $hasgradephp)) { |
1456 | if (file_exists($CFG->dirroot . '/mod/' . $item->itemmodule . '/grade.php')) { | |
1457 | $hasgradephp[$item->itemmodule] = true; | |
1458 | } else { | |
1459 | $hasgradephp[$item->itemmodule] = false; | |
1460 | } | |
1461 | } | |
1462 | ||
1463 | if (!$hasgradephp[$item->itemmodule]) { | |
1464 | return null; | |
1465 | } | |
1466 | ||
bb17ac1e DM |
1467 | $instances = $this->modinfo->get_instances(); |
1468 | if (empty($instances[$item->itemmodule][$item->iteminstance])) { | |
1469 | return null; | |
1470 | } | |
1471 | $cm = $instances[$item->itemmodule][$item->iteminstance]; | |
1472 | if (!$cm->uservisible) { | |
1473 | return null; | |
1474 | } | |
1475 | ||
1476 | $url = new moodle_url('/mod/'.$item->itemmodule.'/grade.php', array( | |
1477 | 'id' => $cm->id, | |
1478 | 'itemid' => $item->id, | |
1479 | 'itemnumber' => $item->itemnumber, | |
1480 | 'gradeid' => $grade->id, | |
1481 | 'userid' => $grade->userid, | |
1482 | )); | |
1483 | ||
1484 | return $url; | |
1485 | } | |
1486 | ||
1487 | /** | |
1488 | * Returns an action icon leading to the grade analysis page | |
1489 | * | |
1490 | * @param grade_grade $grade | |
1491 | * @return string | |
1492 | */ | |
1493 | public function get_grade_analysis_icon(grade_grade $grade) { | |
1494 | global $OUTPUT; | |
1495 | ||
1496 | $url = $this->get_grade_analysis_url($grade); | |
1497 | if (is_null($url)) { | |
1498 | return ''; | |
1499 | } | |
1500 | ||
c97933ff | 1501 | return $OUTPUT->action_icon($url, new pix_icon('t/preview', |
bb17ac1e DM |
1502 | get_string('gradeanalysis', 'core_grades'))); |
1503 | } | |
1504 | ||
6cc3e350 | 1505 | /** |
1506 | * Returns the grade eid - the grade may not exist yet. | |
cf72e2dd | 1507 | * |
1508 | * @param grade_grade $grade_grade A grade_grade object | |
1509 | * | |
6cc3e350 | 1510 | * @return string eid |
1511 | */ | |
d24832f9 | 1512 | public function get_grade_eid($grade_grade) { |
6cc3e350 | 1513 | if (empty($grade_grade->id)) { |
1514 | return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid; | |
1515 | } else { | |
1516 | return 'g'.$grade_grade->id; | |
1517 | } | |
1518 | } | |
1519 | ||
1520 | /** | |
1521 | * Returns the grade_item eid | |
cf72e2dd | 1522 | * @param grade_item $grade_item A grade_item object |
6cc3e350 | 1523 | * @return string eid |
1524 | */ | |
d24832f9 | 1525 | public function get_item_eid($grade_item) { |
ddb9c95f | 1526 | return 'ig'.$grade_item->id; |
6cc3e350 | 1527 | } |
1528 | ||
cf72e2dd | 1529 | /** |
1530 | * Given a grade_tree element, returns an array of parameters | |
1531 | * used to build an icon for that element. | |
1532 | * | |
1533 | * @param array $element An array representing an element in the grade_tree | |
1534 | * | |
1535 | * @return array | |
1536 | */ | |
1537 | public function get_params_for_iconstr($element) { | |
9ecd4386 | 1538 | $strparams = new stdClass(); |
1539 | $strparams->category = ''; | |
1540 | $strparams->itemname = ''; | |
1541 | $strparams->itemmodule = ''; | |
cf72e2dd | 1542 | |
9ecd4386 | 1543 | if (!method_exists($element['object'], 'get_name')) { |
1544 | return $strparams; | |
1545 | } | |
345674ca | 1546 | |
b285c5aa | 1547 | $strparams->itemname = html_to_text($element['object']->get_name()); |
9ecd4386 | 1548 | |
1549 | // If element name is categorytotal, get the name of the parent category | |
1550 | if ($strparams->itemname == get_string('categorytotal', 'grades')) { | |
1551 | $parent = $element['object']->get_parent_category(); | |
1552 | $strparams->category = $parent->get_name() . ' '; | |
1553 | } else { | |
1554 | $strparams->category = ''; | |
1555 | } | |
1556 | ||
1557 | $strparams->itemmodule = null; | |
1558 | if (isset($element['object']->itemmodule)) { | |
1559 | $strparams->itemmodule = $element['object']->itemmodule; | |
345674ca | 1560 | } |
9ecd4386 | 1561 | return $strparams; |
1562 | } | |
1563 | ||
ae93f353 FM |
1564 | /** |
1565 | * Return a reset icon for the given element. | |
1566 | * | |
1567 | * @param array $element An array representing an element in the grade_tree | |
1568 | * @param object $gpr A grade_plugin_return object | |
079b2e52 MG |
1569 | * @param bool $returnactionmenulink return the instance of action_menu_link instead of string |
1570 | * @return string|action_menu_link | |
ae93f353 | 1571 | */ |
079b2e52 | 1572 | public function get_reset_icon($element, $gpr, $returnactionmenulink = false) { |
ae93f353 FM |
1573 | global $CFG, $OUTPUT; |
1574 | ||
1575 | // Limit to category items set to use the natural weights aggregation method, and users | |
1576 | // with the capability to manage grades. | |
1577 | if ($element['type'] != 'category' || $element['object']->aggregation != GRADE_AGGREGATE_SUM || | |
1578 | !has_capability('moodle/grade:manage', $this->context)) { | |
079b2e52 | 1579 | return $returnactionmenulink ? null : ''; |
ae93f353 FM |
1580 | } |
1581 | ||
1582 | $str = get_string('resetweights', 'grades', $this->get_params_for_iconstr($element)); | |
1583 | $url = new moodle_url('/grade/edit/tree/action.php', array( | |
1584 | 'id' => $this->courseid, | |
1585 | 'action' => 'resetweights', | |
1586 | 'eid' => $element['eid'], | |
1587 | 'sesskey' => sesskey(), | |
1588 | )); | |
1589 | ||
079b2e52 MG |
1590 | if ($returnactionmenulink) { |
1591 | return new action_menu_link_secondary($gpr->add_url_params($url), new pix_icon('t/reset', $str), | |
1592 | get_string('resetweightsshort', 'grades')); | |
1593 | } else { | |
1594 | return $OUTPUT->action_icon($gpr->add_url_params($url), new pix_icon('t/reset', $str)); | |
1595 | } | |
ae93f353 FM |
1596 | } |
1597 | ||
6cc3e350 | 1598 | /** |
1599 | * Return edit icon for give element | |
cf72e2dd | 1600 | * |
1601 | * @param array $element An array representing an element in the grade_tree | |
1602 | * @param object $gpr A grade_plugin_return object | |
079b2e52 MG |
1603 | * @param bool $returnactionmenulink return the instance of action_menu_link instead of string |
1604 | * @return string|action_menu_link | |
6cc3e350 | 1605 | */ |
079b2e52 | 1606 | public function get_edit_icon($element, $gpr, $returnactionmenulink = false) { |
6b608f8f | 1607 | global $CFG, $OUTPUT; |
6cc3e350 | 1608 | |
1609 | if (!has_capability('moodle/grade:manage', $this->context)) { | |
1610 | if ($element['type'] == 'grade' and has_capability('moodle/grade:edit', $this->context)) { | |
1611 | // oki - let them override grade | |
1612 | } else { | |
079b2e52 | 1613 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 1614 | } |
1615 | } | |
1616 | ||
05aba805 | 1617 | static $strfeedback = null; |
1618 | static $streditgrade = null; | |
1619 | if (is_null($streditgrade)) { | |
1620 | $streditgrade = get_string('editgrade', 'grades'); | |
1621 | $strfeedback = get_string('feedback'); | |
6cc3e350 | 1622 | } |
1623 | ||
9ecd4386 | 1624 | $strparams = $this->get_params_for_iconstr($element); |
6ef4878b | 1625 | |
6cc3e350 | 1626 | $object = $element['object']; |
6cc3e350 | 1627 | |
1628 | switch ($element['type']) { | |
1629 | case 'item': | |
1630 | case 'categoryitem': | |
1631 | case 'courseitem': | |
9ecd4386 | 1632 | $stredit = get_string('editverbose', 'grades', $strparams); |
6cc3e350 | 1633 | if (empty($object->outcomeid) || empty($CFG->enableoutcomes)) { |
a6855934 | 1634 | $url = new moodle_url('/grade/edit/tree/item.php', |
1c1f64a2 | 1635 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1636 | } else { |
a6855934 | 1637 | $url = new moodle_url('/grade/edit/tree/outcomeitem.php', |
1c1f64a2 | 1638 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1639 | } |
6cc3e350 | 1640 | break; |
1641 | ||
1642 | case 'category': | |
9ecd4386 | 1643 | $stredit = get_string('editverbose', 'grades', $strparams); |
a6855934 | 1644 | $url = new moodle_url('/grade/edit/tree/category.php', |
1c1f64a2 | 1645 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1646 | break; |
1647 | ||
1648 | case 'grade': | |
05aba805 | 1649 | $stredit = $streditgrade; |
6cc3e350 | 1650 | if (empty($object->id)) { |
a6855934 | 1651 | $url = new moodle_url('/grade/edit/tree/grade.php', |
1c1f64a2 | 1652 | array('courseid' => $this->courseid, 'itemid' => $object->itemid, 'userid' => $object->userid)); |
6cc3e350 | 1653 | } else { |
a6855934 | 1654 | $url = new moodle_url('/grade/edit/tree/grade.php', |
1c1f64a2 | 1655 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1656 | } |
6cc3e350 | 1657 | if (!empty($object->feedback)) { |
1658 | $feedback = addslashes_js(trim(format_string($object->feedback, $object->feedbackformat))); | |
6cc3e350 | 1659 | } |
1660 | break; | |
1661 | ||
1662 | default: | |
1663 | $url = null; | |
1664 | } | |
1665 | ||
1666 | if ($url) { | |
079b2e52 MG |
1667 | if ($returnactionmenulink) { |
1668 | return new action_menu_link_secondary($gpr->add_url_params($url), | |
1669 | new pix_icon('t/edit', $stredit), | |
1670 | get_string('editsettings')); | |
1671 | } else { | |
1672 | return $OUTPUT->action_icon($gpr->add_url_params($url), new pix_icon('t/edit', $stredit)); | |
1673 | } | |
6cc3e350 | 1674 | |
1675 | } else { | |
079b2e52 | 1676 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 1677 | } |
1678 | } | |
1679 | ||
1680 | /** | |
1681 | * Return hiding icon for give element | |
cf72e2dd | 1682 | * |
1683 | * @param array $element An array representing an element in the grade_tree | |
1684 | * @param object $gpr A grade_plugin_return object | |
079b2e52 MG |
1685 | * @param bool $returnactionmenulink return the instance of action_menu_link instead of string |
1686 | * @return string|action_menu_link | |
6cc3e350 | 1687 | */ |
079b2e52 | 1688 | public function get_hiding_icon($element, $gpr, $returnactionmenulink = false) { |
5d3b9994 | 1689 | global $CFG, $OUTPUT; |
6cc3e350 | 1690 | |
39873128 | 1691 | if (!$element['object']->can_control_visibility()) { |
079b2e52 | 1692 | return $returnactionmenulink ? null : ''; |
39873128 TH |
1693 | } |
1694 | ||
cf72e2dd | 1695 | if (!has_capability('moodle/grade:manage', $this->context) and |
1696 | !has_capability('moodle/grade:hide', $this->context)) { | |
079b2e52 | 1697 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 1698 | } |
1699 | ||
345674ca | 1700 | $strparams = $this->get_params_for_iconstr($element); |
9ecd4386 | 1701 | $strshow = get_string('showverbose', 'grades', $strparams); |
345674ca | 1702 | $strhide = get_string('hideverbose', 'grades', $strparams); |
6cc3e350 | 1703 | |
a6855934 | 1704 | $url = new moodle_url('/grade/edit/tree/action.php', array('id' => $this->courseid, 'sesskey' => sesskey(), 'eid' => $element['eid'])); |
8ae8bf8a | 1705 | $url = $gpr->add_url_params($url); |
1c1f64a2 | 1706 | |
6cc3e350 | 1707 | if ($element['object']->is_hidden()) { |
8ae8bf8a | 1708 | $type = 'show'; |
6cc3e350 | 1709 | $tooltip = $strshow; |
1710 | ||
cf72e2dd | 1711 | // Change the icon and add a tooltip showing the date |
1712 | if ($element['type'] != 'category' and $element['object']->get_hidden() > 1) { | |
8ae8bf8a | 1713 | $type = 'hiddenuntil'; |
cf72e2dd | 1714 | $tooltip = get_string('hiddenuntildate', 'grades', |
1715 | userdate($element['object']->get_hidden())); | |
6cc3e350 | 1716 | } |
1717 | ||
8ae8bf8a | 1718 | $url->param('action', 'show'); |
b9cc756e | 1719 | |
079b2e52 MG |
1720 | if ($returnactionmenulink) { |
1721 | $hideicon = new action_menu_link_secondary($url, new pix_icon('t/'.$type, $tooltip), get_string('show')); | |
1722 | } else { | |
1723 | $hideicon = $OUTPUT->action_icon($url, new pix_icon('t/'.$type, $tooltip, 'moodle', array('alt'=>$strshow, 'class'=>'smallicon'))); | |
1724 | } | |
6cc3e350 | 1725 | |
1726 | } else { | |
8ae8bf8a | 1727 | $url->param('action', 'hide'); |
079b2e52 MG |
1728 | if ($returnactionmenulink) { |
1729 | $hideicon = new action_menu_link_secondary($url, new pix_icon('t/hide', $strhide), get_string('hide')); | |
1730 | } else { | |
1731 | $hideicon = $OUTPUT->action_icon($url, new pix_icon('t/hide', $strhide)); | |
1732 | } | |
6cc3e350 | 1733 | } |
1c1f64a2 | 1734 | |
8ae8bf8a | 1735 | return $hideicon; |
6cc3e350 | 1736 | } |
1737 | ||
1738 | /** | |
2673c733 | 1739 | * Return locking icon for given element |
cf72e2dd | 1740 | * |
1741 | * @param array $element An array representing an element in the grade_tree | |
1742 | * @param object $gpr A grade_plugin_return object | |
1743 | * | |
6cc3e350 | 1744 | * @return string |
1745 | */ | |
d24832f9 | 1746 | public function get_locking_icon($element, $gpr) { |
6b608f8f | 1747 | global $CFG, $OUTPUT; |
6cc3e350 | 1748 | |
345674ca | 1749 | $strparams = $this->get_params_for_iconstr($element); |
9ecd4386 | 1750 | $strunlock = get_string('unlockverbose', 'grades', $strparams); |
1751 | $strlock = get_string('lockverbose', 'grades', $strparams); | |
d24832f9 | 1752 | |
a6855934 | 1753 | $url = new moodle_url('/grade/edit/tree/action.php', array('id' => $this->courseid, 'sesskey' => sesskey(), 'eid' => $element['eid'])); |
8ae8bf8a | 1754 | $url = $gpr->add_url_params($url); |
1c1f64a2 | 1755 | |
2673c733 | 1756 | // Don't allow an unlocking action for a grade whose grade item is locked: just print a state icon |
1757 | if ($element['type'] == 'grade' && $element['object']->grade_item->is_locked()) { | |
1758 | $strparamobj = new stdClass(); | |
1759 | $strparamobj->itemname = $element['object']->grade_item->itemname; | |
1760 | $strnonunlockable = get_string('nonunlockableverbose', 'grades', $strparamobj); | |
1c1f64a2 | 1761 | |
0cddd851 FM |
1762 | $action = html_writer::tag('span', $OUTPUT->pix_icon('t/locked', $strnonunlockable), |
1763 | array('class' => 'action-icon')); | |
8ae8bf8a | 1764 | |
cf72e2dd | 1765 | } else if ($element['object']->is_locked()) { |
8ae8bf8a | 1766 | $type = 'unlock'; |
6cc3e350 | 1767 | $tooltip = $strunlock; |
1768 | ||
cf72e2dd | 1769 | // Change the icon and add a tooltip showing the date |
1770 | if ($element['type'] != 'category' and $element['object']->get_locktime() > 1) { | |
8ae8bf8a | 1771 | $type = 'locktime'; |
cf72e2dd | 1772 | $tooltip = get_string('locktimedate', 'grades', |
1773 | userdate($element['object']->get_locktime())); | |
6cc3e350 | 1774 | } |
1775 | ||
8ae8bf8a PS |
1776 | if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:unlock', $this->context)) { |
1777 | $action = ''; | |
1778 | } else { | |
1779 | $url->param('action', 'unlock'); | |
c63923bd | 1780 | $action = $OUTPUT->action_icon($url, new pix_icon('t/'.$type, $tooltip, 'moodle', array('alt'=>$strunlock, 'class'=>'smallicon'))); |
6cc3e350 | 1781 | } |
6cc3e350 | 1782 | |
1783 | } else { | |
8ae8bf8a PS |
1784 | if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:lock', $this->context)) { |
1785 | $action = ''; | |
1786 | } else { | |
1787 | $url->param('action', 'lock'); | |
c63923bd | 1788 | $action = $OUTPUT->action_icon($url, new pix_icon('t/lock', $strlock)); |
6cc3e350 | 1789 | } |
6cc3e350 | 1790 | } |
8ae8bf8a | 1791 | |
6cc3e350 | 1792 | return $action; |
1793 | } | |
1794 | ||
1795 | /** | |
1796 | * Return calculation icon for given element | |
cf72e2dd | 1797 | * |
1798 | * @param array $element An array representing an element in the grade_tree | |
1799 | * @param object $gpr A grade_plugin_return object | |
079b2e52 MG |
1800 | * @param bool $returnactionmenulink return the instance of action_menu_link instead of string |
1801 | * @return string|action_menu_link | |
6cc3e350 | 1802 | */ |
079b2e52 | 1803 | public function get_calculation_icon($element, $gpr, $returnactionmenulink = false) { |
666e8458 | 1804 | global $CFG, $OUTPUT; |
6cc3e350 | 1805 | if (!has_capability('moodle/grade:manage', $this->context)) { |
079b2e52 | 1806 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 1807 | } |
1808 | ||
6cc3e350 | 1809 | $type = $element['type']; |
1810 | $object = $element['object']; | |
1811 | ||
1812 | if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') { | |
345674ca | 1813 | $strparams = $this->get_params_for_iconstr($element); |
9ecd4386 | 1814 | $streditcalculation = get_string('editcalculationverbose', 'grades', $strparams); |
6cc3e350 | 1815 | |
cf72e2dd | 1816 | $is_scale = $object->gradetype == GRADE_TYPE_SCALE; |
1817 | $is_value = $object->gradetype == GRADE_TYPE_VALUE; | |
1818 | ||
6cc3e350 | 1819 | // show calculation icon only when calculation possible |
cf72e2dd | 1820 | if (!$object->is_external_item() and ($is_scale or $is_value)) { |
6cc3e350 | 1821 | if ($object->is_calculated()) { |
666e8458 | 1822 | $icon = 't/calc'; |
6cc3e350 | 1823 | } else { |
666e8458 | 1824 | $icon = 't/calc_off'; |
6cc3e350 | 1825 | } |
cf72e2dd | 1826 | |
a6855934 | 1827 | $url = new moodle_url('/grade/edit/tree/calculation.php', array('courseid' => $this->courseid, 'id' => $object->id)); |
8ae8bf8a | 1828 | $url = $gpr->add_url_params($url); |
079b2e52 MG |
1829 | if ($returnactionmenulink) { |
1830 | return new action_menu_link_secondary($url, | |
1831 | new pix_icon($icon, $streditcalculation), | |
1832 | get_string('editcalculation', 'grades')); | |
1833 | } else { | |
1834 | return $OUTPUT->action_icon($url, new pix_icon($icon, $streditcalculation)); | |
1835 | } | |
6cc3e350 | 1836 | } |
1837 | } | |
1838 | ||
079b2e52 | 1839 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 1840 | } |
1841 | } | |
1842 | ||
1843 | /** | |
1844 | * Flat structure similar to grade tree. | |
cf72e2dd | 1845 | * |
1846 | * @uses grade_structure | |
a153c9f2 | 1847 | * @package core_grades |
cf72e2dd | 1848 | * @copyright 2009 Nicolas Connault |
1849 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
6cc3e350 | 1850 | */ |
1851 | class grade_seq extends grade_structure { | |
1852 | ||
6cc3e350 | 1853 | /** |
1854 | * 1D array of elements | |
1855 | */ | |
d24832f9 | 1856 | public $elements; |
e98871a2 | 1857 | |
1858 | /** | |
1859 | * Constructor, retrieves and stores array of all grade_category and grade_item | |
1860 | * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed. | |
cf72e2dd | 1861 | * |
1862 | * @param int $courseid The course id | |
1863 | * @param bool $category_grade_last category grade item is the last child | |
1864 | * @param bool $nooutcomes Whether or not outcomes should be included | |
e98871a2 | 1865 | */ |
d24832f9 | 1866 | public function grade_seq($courseid, $category_grade_last=false, $nooutcomes=false) { |
e98871a2 | 1867 | global $USER, $CFG; |
1868 | ||
1869 | $this->courseid = $courseid; | |
d4060472 | 1870 | $this->context = context_course::instance($courseid); |
e98871a2 | 1871 | |
1872 | // get course grade tree | |
1873 | $top_element = grade_category::fetch_course_tree($courseid, true); | |
1874 | ||
6391ebe7 | 1875 | $this->elements = grade_seq::flatten($top_element, $category_grade_last, $nooutcomes); |
1876 | ||
1877 | foreach ($this->elements as $key=>$unused) { | |
b89a70ce | 1878 | $this->items[$this->elements[$key]['object']->id] =& $this->elements[$key]['object']; |
6391ebe7 | 1879 | } |
e98871a2 | 1880 | } |
1881 | ||
1882 | /** | |
1883 | * Static recursive helper - makes the grade_item for category the last children | |
cf72e2dd | 1884 | * |
1885 | * @param array &$element The seed of the recursion | |
1886 | * @param bool $category_grade_last category grade item is the last child | |
1887 | * @param bool $nooutcomes Whether or not outcomes should be included | |
1888 | * | |
1889 | * @return array | |
e98871a2 | 1890 | */ |
d24832f9 | 1891 | public function flatten(&$element, $category_grade_last, $nooutcomes) { |
e98871a2 | 1892 | if (empty($element['children'])) { |
1893 | return array(); | |
1894 | } | |
1895 | $children = array(); | |
1896 | ||
1897 | foreach ($element['children'] as $sortorder=>$unused) { | |
cf72e2dd | 1898 | if ($nooutcomes and $element['type'] != 'category' and |
1899 | $element['children'][$sortorder]['object']->is_outcome_item()) { | |
e98871a2 | 1900 | continue; |
1901 | } | |
1902 | $children[] = $element['children'][$sortorder]; | |
1903 | } | |
1904 | unset($element['children']); | |
1905 | ||
1906 | if ($category_grade_last and count($children) > 1) { | |
1907 | $cat_item = array_shift($children); | |
1908 | array_push($children, $cat_item); | |
1909 | } | |
1910 | ||
1911 | $result = array(); | |
1912 | foreach ($children as $child) { | |
e0724506 | 1913 | if ($child['type'] == 'category') { |
6391ebe7 | 1914 | $result = $result + grade_seq::flatten($child, $category_grade_last, $nooutcomes); |
e98871a2 | 1915 | } else { |
1916 | $child['eid'] = 'i'.$child['object']->id; | |
6391ebe7 | 1917 | $result[$child['object']->id] = $child; |
e98871a2 | 1918 | } |
1919 | } | |
1920 | ||
1921 | return $result; | |
1922 | } | |
1923 | ||
1924 | /** | |
1925 | * Parses the array in search of a given eid and returns a element object with | |
1926 | * information about the element it has found. | |
cf72e2dd | 1927 | * |
1928 | * @param int $eid Gradetree Element ID | |
1929 | * | |
e98871a2 | 1930 | * @return object element |
1931 | */ | |
d24832f9 | 1932 | public function locate_element($eid) { |
e98871a2 | 1933 | // it is a grade - construct a new object |
1934 | if (strpos($eid, 'n') === 0) { | |
1935 | if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) { | |
1936 | return null; | |
1937 | } | |
1938 | ||
1939 | $itemid = $matches[1]; | |
1940 | $userid = $matches[2]; | |
1941 | ||
1942 | //extra security check - the grade item must be in this tree | |
ddb9c95f | 1943 | if (!$item_el = $this->locate_element('ig'.$itemid)) { |
e98871a2 | 1944 | return null; |
1945 | } | |
1946 | ||
1947 | // $gradea->id may be null - means does not exist yet | |
1948 | $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid)); | |
1949 | ||
1950 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
1951 | return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade'); | |
1952 | ||
1953 | } else if (strpos($eid, 'g') === 0) { | |
cf72e2dd | 1954 | $id = (int) substr($eid, 1); |
e98871a2 | 1955 | if (!$grade = grade_grade::fetch(array('id'=>$id))) { |
1956 | return null; | |
1957 | } | |
1958 | //extra security check - the grade item must be in this tree | |
ddb9c95f | 1959 | if (!$item_el = $this->locate_element('ig'.$grade->itemid)) { |
e98871a2 | 1960 | return null; |
1961 | } | |
1962 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
1963 | return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade'); | |
1964 | } | |
1965 | ||
1966 | // it is a category or item | |
6391ebe7 | 1967 | foreach ($this->elements as $element) { |
6cc3e350 | 1968 | if ($element['eid'] == $eid) { |
1969 | return $element; | |
1970 | } | |
e98871a2 | 1971 | } |
6cc3e350 | 1972 | |
1973 | return null; | |
e98871a2 | 1974 | } |
e98871a2 | 1975 | } |
1976 | ||
7a6b7acf | 1977 | /** |
1978 | * This class represents a complete tree of categories, grade_items and final grades, | |
1979 | * organises as an array primarily, but which can also be converted to other formats. | |
1980 | * It has simple method calls with complex implementations, allowing for easy insertion, | |
1981 | * deletion and moving of items and categories within the tree. | |
cf72e2dd | 1982 | * |
1983 | * @uses grade_structure | |
a153c9f2 | 1984 | * @package core_grades |
cf72e2dd | 1985 | * @copyright 2009 Nicolas Connault |
1986 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
7a6b7acf | 1987 | */ |
6cc3e350 | 1988 | class grade_tree extends grade_structure { |
7a6b7acf | 1989 | |
1990 | /** | |
1991 | * The basic representation of the tree as a hierarchical, 3-tiered array. | |
1992 | * @var object $top_element | |
1993 | */ | |
d24832f9 | 1994 | public $top_element; |
7a6b7acf | 1995 | |
7a6b7acf | 1996 | /** |
1997 | * 2D array of grade items and categories | |
cf72e2dd | 1998 | * @var array $levels |
7a6b7acf | 1999 | */ |
d24832f9 | 2000 | public $levels; |
7a6b7acf | 2001 | |
b89a70ce | 2002 | /** |
2003 | * Grade items | |
cf72e2dd | 2004 | * @var array $items |
b89a70ce | 2005 | */ |
d24832f9 | 2006 | public $items; |
b89a70ce | 2007 | |
7a6b7acf | 2008 | /** |
2009 | * Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item | |
e98871a2 | 2010 | * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed. |
cf72e2dd | 2011 | * |
2012 | * @param int $courseid The Course ID | |
2013 | * @param bool $fillers include fillers and colspans, make the levels var "rectangular" | |
2014 | * @param bool $category_grade_last category grade item is the last child | |
4faf5f99 | 2015 | * @param array $collapsed array of collapsed categories |
cf72e2dd | 2016 | * @param bool $nooutcomes Whether or not outcomes should be included |
7a6b7acf | 2017 | */ |
cf72e2dd | 2018 | public function grade_tree($courseid, $fillers=true, $category_grade_last=false, |
2019 | $collapsed=null, $nooutcomes=false) { | |
73ca5f01 | 2020 | global $USER, $CFG, $COURSE, $DB; |
7a6b7acf | 2021 | |
2022 | $this->courseid = $courseid; | |
7a6b7acf | 2023 | $this->levels = array(); |
d4060472 | 2024 | $this->context = context_course::instance($courseid); |
7a6b7acf | 2025 | |
73ca5f01 | 2026 | if (!empty($COURSE->id) && $COURSE->id == $this->courseid) { |
2027 | $course = $COURSE; | |
2028 | } else { | |
2029 | $course = $DB->get_record('course', array('id' => $this->courseid)); | |
2030 | } | |
2031 | $this->modinfo = get_fast_modinfo($course); | |
2032 | ||
7a6b7acf | 2033 | // get course grade tree |
2034 | $this->top_element = grade_category::fetch_course_tree($courseid, true); | |
2035 | ||
4faf5f99 | 2036 | // collapse the categories if requested |
2037 | if (!empty($collapsed)) { | |
2038 | grade_tree::category_collapse($this->top_element, $collapsed); | |
2039 | } | |
2040 | ||
aea4df41 | 2041 | // no otucomes if requested |
2042 | if (!empty($nooutcomes)) { | |
2043 | grade_tree::no_outcomes($this->top_element); | |
2044 | } | |
2045 | ||
4faf5f99 | 2046 | // move category item to last position in category |
7a6b7acf | 2047 | if ($category_grade_last) { |
2048 | grade_tree::category_grade_last($this->top_element); | |
2049 | } | |
2050 | ||
2051 | if ($fillers) { | |
2052 | // inject fake categories == fillers | |
2053 | grade_tree::inject_fillers($this->top_element, 0); | |
2054 | // add colspans to categories and fillers | |
2055 | grade_tree::inject_colspans($this->top_element); | |
2056 | } | |
2057 | ||
2058 | grade_tree::fill_levels($this->levels, $this->top_element, 0); | |
d297269d | 2059 | |
7a6b7acf | 2060 | } |
2061 | ||
4faf5f99 | 2062 | /** |
2063 | * Static recursive helper - removes items from collapsed categories | |
cf72e2dd | 2064 | * |
2065 | * @param array &$element The seed of the recursion | |
4faf5f99 | 2066 | * @param array $collapsed array of collapsed categories |
cf72e2dd | 2067 | * |
4faf5f99 | 2068 | * @return void |
2069 | */ | |
d24832f9 | 2070 | public function category_collapse(&$element, $collapsed) { |
4faf5f99 | 2071 | if ($element['type'] != 'category') { |
2072 | return; | |
2073 | } | |
2074 | if (empty($element['children']) or count($element['children']) < 2) { | |
2075 | return; | |
2076 | } | |
2077 | ||
384960dd | 2078 | if (in_array($element['object']->id, $collapsed['aggregatesonly'])) { |
4faf5f99 | 2079 | $category_item = reset($element['children']); //keep only category item |
2080 | $element['children'] = array(key($element['children'])=>$category_item); | |
2081 | ||
2082 | } else { | |
384960dd | 2083 | if (in_array($element['object']->id, $collapsed['gradesonly'])) { // Remove category item |
2084 | reset($element['children']); | |
2085 | $first_key = key($element['children']); | |
2086 | unset($element['children'][$first_key]); | |
2087 | } | |
2088 | foreach ($element['children'] as $sortorder=>$child) { // Recurse through the element's children | |
4faf5f99 | 2089 | grade_tree::category_collapse($element['children'][$sortorder], $collapsed); |
2090 | } | |
2091 | } | |
2092 | } | |
7a6b7acf | 2093 | |
aea4df41 | 2094 | /** |
2095 | * Static recursive helper - removes all outcomes | |
cf72e2dd | 2096 | * |
2097 | * @param array &$element The seed of the recursion | |
2098 | * | |
aea4df41 | 2099 | * @return void |
2100 | */ | |
d24832f9 | 2101 | public function no_outcomes(&$element) { |
aea4df41 | 2102 | if ($element['type'] != 'category') { |
2103 | return; | |
2104 | } | |
2105 | foreach ($element['children'] as $sortorder=>$child) { | |
2106 | if ($element['children'][$sortorder]['type'] == 'item' | |
2107 | and $element['children'][$sortorder]['object']->is_outcome_item()) { | |
2108 | unset($element['children'][$sortorder]); | |
2109 | ||
d4795a07 | 2110 | } else if ($element['children'][$sortorder]['type'] == 'category') { |
aea4df41 | 2111 | grade_tree::no_outcomes($element['children'][$sortorder]); |
2112 | } | |
2113 | } | |
2114 | } | |
2115 | ||
7a6b7acf | 2116 | /** |
2117 | * Static recursive helper - makes the grade_item for category the last children | |
cf72e2dd | 2118 | * |
2119 | * @param array &$element The seed of the recursion | |
2120 | * | |
7a6b7acf | 2121 | * @return void |
2122 | */ | |
d24832f9 | 2123 | public function category_grade_last(&$element) { |
7a6b7acf | 2124 | if (empty($element['children'])) { |
2125 | return; | |
2126 | } | |
2127 | if (count($element['children']) < 2) { | |
2128 | return; | |
2129 | } | |
3e0e2436 | 2130 | $first_item = reset($element['children']); |
4a3dfd9a | 2131 | if ($first_item['type'] == 'categoryitem' or $first_item['type'] == 'courseitem') { |
3e0e2436 | 2132 | // the category item might have been already removed |
2133 | $order = key($element['children']); | |
2134 | unset($element['children'][$order]); | |
2135 | $element['children'][$order] =& $first_item; | |
2136 | } | |
206f9953 | 2137 | foreach ($element['children'] as $sortorder => $child) { |
7a6b7acf | 2138 | grade_tree::category_grade_last($element['children'][$sortorder]); |
2139 | } | |
2140 | } | |
2141 | ||
2142 | /** | |
2143 | * Static recursive helper - fills the levels array, useful when accessing tree elements of one level | |
cf72e2dd | 2144 | * |
2145 | * @param array &$levels The levels of the grade tree through which to recurse | |
2146 | * @param array &$element The seed of the recursion | |
2147 | * @param int $depth How deep are we? | |
7a6b7acf | 2148 | * @return void |
2149 | */ | |
d24832f9 | 2150 | public function fill_levels(&$levels, &$element, $depth) { |
7a6b7acf | 2151 | if (!array_key_exists($depth, $levels)) { |
2152 | $levels[$depth] = array(); | |
2153 | } | |
2154 | ||
2155 | // prepare unique identifier | |
2156 | if ($element['type'] == 'category') { | |
ddb9c95f | 2157 | $element['eid'] = 'cg'.$element['object']->id; |
7a6b7acf | 2158 | } else if (in_array($element['type'], array('item', 'courseitem', 'categoryitem'))) { |
ddb9c95f | 2159 | $element['eid'] = 'ig'.$element['object']->id; |
b89a70ce | 2160 | $this->items[$element['object']->id] =& $element['object']; |
7a6b7acf | 2161 | } |
2162 | ||
2163 | $levels[$depth][] =& $element; | |
2164 | $depth++; | |
2165 | if (empty($element['children'])) { | |
2166 | return; | |
2167 | } | |
2168 | $prev = 0; | |
2169 | foreach ($element['children'] as $sortorder=>$child) { | |
2170 | grade_tree::fill_levels($levels, $element['children'][$sortorder], $depth); | |
2171 | $element['children'][$sortorder]['prev'] = $prev; | |
2172 | $element['children'][$sortorder]['next'] = 0; | |
2173 | if ($prev) { | |
2174 | $element['children'][$prev]['next'] = $sortorder; | |
2175 | } | |
2176 | $prev = $sortorder; | |
2177 | } | |
2178 | } | |
2179 | ||
2180 | /** | |
2181 | * Static recursive helper - makes full tree (all leafes are at the same level) | |
cf72e2dd | 2182 | * |
2183 | * @param array &$element The seed of the recursion | |
2184 | * @param int $depth How deep are we? | |
2185 | * | |
2186 | * @return int | |
7a6b7acf | 2187 | */ |
d24832f9 | 2188 | public function inject_fillers(&$element, $depth) { |
7a6b7acf | 2189 | $depth++; |
2190 | ||
2191 | if (empty($element['children'])) { | |
2192 | return $depth; | |
2193 | } | |
2194 | $chdepths = array(); | |
2195 | $chids = array_keys($element['children']); | |
2196 | $last_child = end($chids); | |
2197 | $first_child = reset($chids); | |
2198 | ||
2199 | foreach ($chids as $chid) { | |
2200 | $chdepths[$chid] = grade_tree::inject_fillers($element['children'][$chid], $depth); | |
2201 | } | |
2202 | arsort($chdepths); | |
2203 | ||
2204 | $maxdepth = reset($chdepths); | |
2205 | foreach ($chdepths as $chid=>$chd) { | |
2206 | if ($chd == $maxdepth) { | |
2207 | continue; | |
2208 | } | |
2209 | for ($i=0; $i < $maxdepth-$chd; $i++) { | |
2210 | if ($chid == $first_child) { | |
2211 | $type = 'fillerfirst'; | |
2212 | } else if ($chid == $last_child) { | |
2213 | $type = 'fillerlast'; | |
2214 | } else { | |
2215 | $type = 'filler'; | |
2216 | } | |
2217 | $oldchild =& $element['children'][$chid]; | |
cf72e2dd | 2218 | $element['children'][$chid] = array('object'=>'filler', 'type'=>$type, |
2219 | 'eid'=>'', 'depth'=>$element['object']->depth, | |
2220 | 'children'=>array($oldchild)); | |
7a6b7acf | 2221 | } |
2222 | } | |
2223 | ||
2224 | return $maxdepth; | |
2225 | } | |
2226 | ||
2227 | /** | |
2228 | * Static recursive helper - add colspan information into categories | |
cf72e2dd | 2229 | * |
2230 | * @param array &$element The seed of the recursion | |
2231 | * | |
2232 | * @return int | |
7a6b7acf | 2233 | */ |
d24832f9 | 2234 | public function inject_colspans(&$element) { |
7a6b7acf | 2235 | if (empty($element['children'])) { |
2236 | return 1; | |
2237 | } | |
2238 | $count = 0; | |
2239 | foreach ($element['children'] as $key=>$child) { | |
2240 | $count += grade_tree::inject_colspans($element['children'][$key]); | |
2241 | } | |
2242 | $element['colspan'] = $count; | |
2243 | return $count; | |
2244 | } | |
2245 | ||
2246 | /** | |
2247 | * Parses the array in search of a given eid and returns a element object with | |
2248 | * information about the element it has found. | |
cf72e2dd | 2249 | * @param int $eid Gradetree Element ID |
7a6b7acf | 2250 | * @return object element |
2251 | */ | |
d24832f9 | 2252 | public function locate_element($eid) { |
d3c3da1b | 2253 | // it is a grade - construct a new object |
2254 | if (strpos($eid, 'n') === 0) { | |
2255 | if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) { | |
2256 | return null; | |
2257 | } | |
2258 | ||
2259 | $itemid = $matches[1]; | |
2260 | $userid = $matches[2]; | |
2261 | ||
2262 | //extra security check - the grade item must be in this tree | |
ddb9c95f | 2263 | if (!$item_el = $this->locate_element('ig'.$itemid)) { |
d3c3da1b | 2264 | return null; |
2265 | } | |
2266 | ||
2267 | // $gradea->id may be null - means does not exist yet | |
2268 | $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid)); | |
2269 | ||
2270 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
2271 | return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade'); | |
2272 | ||
2273 | } else if (strpos($eid, 'g') === 0) { | |
cf72e2dd | 2274 | $id = (int) substr($eid, 1); |
7a6b7acf | 2275 | if (!$grade = grade_grade::fetch(array('id'=>$id))) { |
2276 | return null; | |
2277 | } | |
2278 | //extra security check - the grade item must be in this tree | |
ddb9c95f | 2279 | if (!$item_el = $this->locate_element('ig'.$grade->itemid)) { |
7a6b7acf | 2280 | return null; |
2281 | } | |
2282 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
2283 | return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade'); | |
2284 | } | |
2285 | ||
2286 | // it is a category or item | |
2287 | foreach ($this->levels as $row) { | |
2288 | foreach ($row as $element) { | |
2289 | if ($element['type'] == 'filler') { | |
2290 | continue; | |
2291 | } | |
2292 | if ($element['eid'] == $eid) { | |
2293 | return $element; | |
2294 | } | |
2295 | } | |
2296 | } | |
2297 | ||
2298 | return null; | |
2299 | } | |
d24832f9 | 2300 | |
b244b9b7 | 2301 | /** |
2302 | * Returns a well-formed XML representation of the grade-tree using recursion. | |
cf72e2dd | 2303 | * |
2304 | * @param array $root The current element in the recursion. If null, starts at the top of the tree. | |
2305 | * @param string $tabs The control character to use for tabs | |
2306 | * | |
b244b9b7 | 2307 | * @return string $xml |
2308 | */ | |
cf72e2dd | 2309 | public function exporttoxml($root=null, $tabs="\t") { |
b244b9b7 | 2310 | $xml = null; |
2311 | $first = false; | |
2312 | if (is_null($root)) { | |
2313 | $root = $this->top_element; | |
2314 | $xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n"; | |
2315 | $xml .= "<gradetree>\n"; | |
2316 | $first = true; | |
2317 | } | |
d24832f9 | 2318 | |
b244b9b7 | 2319 | $type = 'undefined'; |
2320 | if (strpos($root['object']->table, 'grade_categories') !== false) { | |
2321 | $type = 'category'; | |
cf72e2dd | 2322 | } else if (strpos($root['object']->table, 'grade_items') !== false) { |
b244b9b7 | 2323 | $type = 'item'; |
cf72e2dd | 2324 | } else if (strpos($root['object']->table, 'grade_outcomes') !== false) { |
b244b9b7 | 2325 | $type = 'outcome'; |
2326 | } | |
d24832f9 | 2327 | |
b244b9b7 | 2328 | $xml .= "$tabs<element type=\"$type\">\n"; |
2329 | foreach ($root['object'] as $var => $value) { | |
2330 | if (!is_object($value) && !is_array($value) && !empty($value)) { | |
2331 | $xml .= "$tabs\t<$var>$value</$var>\n"; | |
2332 | } | |
2333 | } | |
2334 | ||
2335 | if (!empty($root['children'])) { | |
2336 | $xml .= "$tabs\t<children>\n"; | |
2337 | foreach ($root['children'] as $sortorder => $child) { | |
2338 | $xml .= $this->exportToXML($child, $tabs."\t\t"); | |
2339 | } | |
2340 | $xml .= "$tabs\t</children>\n"; | |
2341 | } | |
d24832f9 | 2342 | |
b244b9b7 | 2343 | $xml .= "$tabs</element>\n"; |
2344 | ||
2345 | if ($first) { | |
2346 | $xml .= "</gradetree>"; | |
2347 | } | |
d24832f9 | 2348 | |
b244b9b7 | 2349 | return $xml; |
2350 | } | |
d24832f9 | 2351 | |
b244b9b7 | 2352 | /** |
2353 | * Returns a JSON representation of the grade-tree using recursion. | |
cf72e2dd | 2354 | * |
b244b9b7 | 2355 | * @param array $root The current element in the recursion. If null, starts at the top of the tree. |
2356 | * @param string $tabs Tab characters used to indent the string nicely for humans to enjoy | |
cf72e2dd | 2357 | * |
2358 | * @return string | |
b244b9b7 | 2359 | */ |
cf72e2dd | 2360 | public function exporttojson($root=null, $tabs="\t") { |
b244b9b7 | 2361 | $json = null; |
2362 | $first = false; | |
2363 | if (is_null($root)) { | |
2364 | $root = $this->top_element; | |
2365 | $first = true; | |
2366 | } | |
d24832f9 | 2367 | |
b244b9b7 | 2368 | $name = ''; |
2369 | ||
2370 | ||
2371 | if (strpos($root['object']->table, 'grade_categories') !== false) { | |
2372 | $name = $root['object']->fullname; | |
2373 | if ($name == '?') { | |
d24832f9 | 2374 | $name = $root['object']->get_name(); |
b244b9b7 | 2375 | } |
cf72e2dd | 2376 | } else if (strpos($root['object']->table, 'grade_items') !== false) { |
b244b9b7 | 2377 | $name = $root['object']->itemname; |
cf72e2dd | 2378 | } else if (strpos($root['object']->table, 'grade_outcomes') !== false) { |
b244b9b7 | 2379 | $name = $root['object']->itemname; |
2380 | } | |
d24832f9 | 2381 | |
b244b9b7 | 2382 | $json .= "$tabs {\n"; |
2383 | $json .= "$tabs\t \"type\": \"{$root['type']}\",\n"; | |
2384 | $json .= "$tabs\t \"name\": \"$name\",\n"; | |
2385 | ||
2386 | foreach ($root['object'] as $var => $value) { | |
2387 | if (!is_object($value) && !is_array($value) && !empty($value)) { | |
2388 | $json .= "$tabs\t \"$var\": \"$value\",\n"; | |
2389 | } | |
2390 | } | |
d24832f9 | 2391 | |
b244b9b7 | 2392 | $json = substr($json, 0, strrpos($json, ',')); |
d24832f9 | 2393 | |
b244b9b7 | 2394 | if (!empty($root['children'])) { |
2395 | $json .= ",\n$tabs\t\"children\": [\n"; | |
2396 | foreach ($root['children'] as $sortorder => $child) { | |
2397 | $json .= $this->exportToJSON($child, $tabs."\t\t"); | |
2398 | } | |
2399 | $json = substr($json, 0, strrpos($json, ',')); | |
2400 | $json .= "\n$tabs\t]\n"; | |
d24832f9 | 2401 | } |
b244b9b7 | 2402 | |
2403 | if ($first) { | |
2404 | $json .= "\n}"; | |
2405 | } else { | |
2406 | $json .= "\n$tabs},\n"; | |
2407 | } | |
d24832f9 | 2408 | |
b244b9b7 | 2409 | return $json; |
2410 | } | |
d24832f9 | 2411 | |
cf72e2dd | 2412 | /** |
2413 | * Returns the array of levels | |
2414 | * | |
2415 | * @return array | |
2416 | */ | |
d24832f9 | 2417 | public function get_levels() { |
2418 | return $this->levels; | |
2419 | } | |
2420 | ||
cf72e2dd | 2421 | /** |
2422 | * Returns the array of grade items | |
2423 | * | |
2424 | * @return array | |
2425 | */ | |
d24832f9 | 2426 | public function get_items() { |
2427 | return $this->items; | |
2428 | } | |
2429 | ||
cf72e2dd | 2430 | /** |
2431 | * Returns a specific Grade Item | |
2432 | * | |
2433 | * @param int $itemid The ID of the grade_item object | |
2434 | * | |
2435 | * @return grade_item | |
2436 | */ | |
d24832f9 | 2437 | public function get_item($itemid) { |
2438 | if (array_key_exists($itemid, $this->items)) { | |
2439 | return $this->items[$itemid]; | |
2440 | } else { | |
2441 | return false; | |
2442 | } | |
2443 | } | |
7a6b7acf | 2444 | } |
eef00ade | 2445 | |
2446 | /** | |
2447 | * Local shortcut function for creating an edit/delete button for a grade_* object. | |
4d27bc79 | 2448 | * @param string $type 'edit' or 'delete' |
eef00ade | 2449 | * @param int $courseid The Course ID |
2450 | * @param grade_* $object The grade_* object | |
2451 | * @return string html | |
2452 | */ | |
2453 | function grade_button($type, $courseid, $object) { | |
2454 | global $CFG, $OUTPUT; | |
2455 | if (preg_match('/grade_(.*)/', get_class($object), $matches)) { | |
2456 | $objectidstring = $matches[1] . 'id'; | |
2457 | } else { | |
2458 | throw new coding_exception('grade_button() only accepts grade_* objects as third parameter!'); | |
2459 | } | |
2460 | ||
2461 | $strdelete = get_string('delete'); | |
2462 | $stredit = get_string('edit'); | |
2463 | ||
eef00ade | 2464 | if ($type == 'delete') { |
8ae8bf8a | 2465 | $url = new moodle_url('index.php', array('id' => $courseid, $objectidstring => $object->id, 'action' => 'delete', 'sesskey' => sesskey())); |
eef00ade | 2466 | } else if ($type == 'edit') { |
8ae8bf8a | 2467 | $url = new moodle_url('edit.php', array('courseid' => $courseid, 'id' => $object->id)); |
eef00ade | 2468 | } |
2469 | ||
fa9c0aab | 2470 | return $OUTPUT->action_icon($url, new pix_icon('t/'.$type, ${'str'.$type}, '', array('class' => 'iconsmall'))); |
eef00ade | 2471 | |
2472 | } | |
4d5059d4 SH |
2473 | |
2474 | /** | |
2475 | * This method adds settings to the settings block for the grade system and its | |
2476 | * plugins | |
2477 | * | |
2478 | * @global moodle_page $PAGE | |
2479 | */ | |
2480 | function grade_extend_settings($plugininfo, $courseid) { | |
2481 | global $PAGE; | |
2482 | ||
2483 | $gradenode = $PAGE->settingsnav->prepend(get_string('gradeadministration', 'grades'), null, navigation_node::TYPE_CONTAINER); | |
2484 | ||
2485 | $strings = array_shift($plugininfo); | |
2486 | ||
2487 | if ($reports = grade_helper::get_plugins_reports($courseid)) { | |
2488 | foreach ($reports as $report) { | |
2489 | $gradenode->add($report->string, $report->link, navigation_node::TYPE_SETTING, null, $report->id, new pix_icon('i/report', '')); | |
2490 | } | |
2491 | } | |
2492 | ||
eb84b779 MG |
2493 | if ($settings = grade_helper::get_info_manage_settings($courseid)) { |
2494 | $settingsnode = $gradenode->add($strings['settings'], null, navigation_node::TYPE_CONTAINER); | |
2495 | foreach ($settings as $setting) { | |
2496 | $settingsnode->add($setting->string, $setting->link, navigation_node::TYPE_SETTING, null, $setting->id, new pix_icon('i/settings', '')); | |
2497 | } | |
2498 | } | |
2499 | ||
4d5059d4 SH |
2500 | if ($imports = grade_helper::get_plugins_import($courseid)) { |
2501 | $importnode = $gradenode->add($strings['import'], null, navigation_node::TYPE_CONTAINER); | |
2502 | foreach ($imports as $import) { | |
11f87187 | 2503 | $importnode->add($import->string, $import->link, navigation_node::TYPE_SETTING, null, $import->id, new pix_icon('i/import', '')); |
4d5059d4 SH |
2504 | } |
2505 | } | |
2506 | ||
2507 | if ($exports = grade_helper::get_plugins_export($courseid)) { | |
2508 | $exportnode = $gradenode->add($strings['export'], null, navigation_node::TYPE_CONTAINER); | |
2509 | foreach ($exports as $export) { | |
11f87187 | 2510 | $exportnode->add($export->string, $export->link, navigation_node::TYPE_SETTING, null, $export->id, new pix_icon('i/export', '')); |
4d5059d4 SH |
2511 | } |
2512 | } | |
2513 | ||
4d5059d4 SH |
2514 | if ($letters = grade_helper::get_info_letters($courseid)) { |
2515 | $letters = array_shift($letters); | |
2516 | $gradenode->add($strings['letter'], $letters->link, navigation_node::TYPE_SETTING, null, $letters->id, new pix_icon('i/settings', '')); | |
2517 | } | |
2518 | ||
2519 | if ($outcomes = grade_helper::get_info_outcomes($courseid)) { | |
2520 | $outcomes = array_shift($outcomes); | |
2521 | $gradenode->add($strings['outcome'], $outcomes->link, navigation_node::TYPE_SETTING, null, $outcomes->id, new pix_icon('i/outcomes', '')); | |
2522 | } | |
2523 | ||
2524 | if ($scales = grade_helper::get_info_scales($courseid)) { | |
2525 | $gradenode->add($strings['scale'], $scales->link, navigation_node::TYPE_SETTING, null, $scales->id, new pix_icon('i/scales', '')); | |
2526 | } | |
2527 | ||
4d5059d4 SH |
2528 | if ($gradenode->contains_active_node()) { |
2529 | // If the gradenode is active include the settings base node (gradeadministration) in | |
2530 | // the navbar, typcially this is ignored. | |
2531 | $PAGE->navbar->includesettingsbase = true; | |
2532 | ||
2533 | // If we can get the course admin node make sure it is closed by default | |
2534 | // as in this case the gradenode will be opened | |
2535 | if ($coursenode = $PAGE->settingsnav->get('courseadmin', navigation_node::TYPE_COURSE)){ | |
2536 | $coursenode->make_inactive(); | |
2537 | $coursenode->forceopen = false; | |
2538 | } | |
2539 | } | |
2540 | } | |
2541 | ||
2542 | /** | |
2543 | * Grade helper class | |
2544 | * | |
2545 | * This class provides several helpful functions that work irrespective of any | |
2546 | * current state. | |
2547 | * | |
2548 | * @copyright 2010 Sam Hemelryk | |
2549 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2550 | */ | |
2551 | abstract class grade_helper { | |
2552 | /** | |
2553 | * Cached manage settings info {@see get_info_settings} | |
2554 | * @var grade_plugin_info|false | |
2555 | */ | |
2556 | protected static $managesetting = null; | |
2557 | /** | |
2558 | * Cached grade report plugins {@see get_plugins_reports} | |
2559 | * @var array|false | |
2560 | */ | |
2561 | protected static $gradereports = null; | |
2562 | /** | |
2563 | * Cached grade report plugins preferences {@see get_info_scales} | |
2564 | * @var array|false | |
2565 | */ | |
2566 | protected static $gradereportpreferences = null; | |
2567 | /** | |
2568 | * Cached scale info {@see get_info_scales} | |
2569 | * @var grade_plugin_info|false | |
2570 | */ | |
2571 | protected static $scaleinfo = null; | |
2572 | /** | |
2573 | * Cached outcome info {@see get_info_outcomes} | |
2574 | * @var grade_plugin_info|false | |
2575 | */ | |
2576 | protected static $outcomeinfo = null; | |
4d5059d4 SH |
2577 | /** |
2578 | * Cached leftter info {@see get_info_letters} | |
2579 | * @var grade_plugin_info|false | |
2580 | */ | |
2581 | protected static $letterinfo = null; | |
2582 | /** | |
2583 | * Cached grade import plugins {@see get_plugins_import} | |
2584 | * @var array|false | |
2585 | */ | |
2586 | protected static $importplugins = null; | |
2587 | /** | |
2588 | * Cached grade export plugins {@see get_plugins_export} | |
2589 | * @var array|false | |
2590 | */ | |
2591 | protected static $exportplugins = null; | |
2592 | /** | |
2593 | * Cached grade plugin strings | |
2594 | * @var array | |
2595 | */ | |
2596 | protected static $pluginstrings = null; | |
bfe969e8 DW |
2597 | /** |
2598 | * Cached grade aggregation strings | |
2599 | * @var array | |
2600 | */ | |
2601 | protected static $aggregationstrings = null; | |
4d5059d4 SH |
2602 | |
2603 | /** | |
2604 | * Gets strings commonly used by the describe plugins | |
2605 | * | |
2606 | * report => get_string('view'), | |
4d5059d4 SH |
2607 | * scale => get_string('scales'), |
2608 | * outcome => get_string('outcomes', 'grades'), | |
2609 | * letter => get_string('letters', 'grades'), | |
2610 | * export => get_string('export', 'grades'), | |
2611 | * import => get_string('import'), | |
2612 | * preferences => get_string('mypreferences', 'grades'), | |
2613 | * settings => get_string('settings') | |
2614 | * | |
2615 | * @return array | |
2616 | */ | |
2617 | public static function get_plugin_strings() { | |
2618 | if (self::$pluginstrings === null) { | |
2619 | self::$pluginstrings = array( | |
2620 | 'report' => get_string('view'), | |
4d5059d4 SH |
2621 | 'scale' => get_string('scales'), |
2622 | 'outcome' => get_string('outcomes', 'grades'), | |
2623 | 'letter' => get_string('letters', 'grades'), | |
2624 | 'export' => get_string('export', 'grades'), | |
2625 | 'import' => get_string('import'), | |
eb84b779 | 2626 | 'settings' => get_string('edittree', 'grades') |
4d5059d4 SH |
2627 | ); |
2628 | } | |
2629 | return self::$pluginstrings; | |
2630 | } | |
bfe969e8 DW |
2631 | |
2632 | /** | |
2633 | * Gets strings describing the available aggregation methods. | |
2634 | * | |
2635 | * @return array | |
2636 | */ | |
2637 | public static function get_aggregation_strings() { | |
2638 | if (self::$aggregationstrings === null) { | |
2639 | self::$aggregationstrings = array( | |
2640 | GRADE_AGGREGATE_MEAN => get_string('aggregatemean', 'grades'), | |
2641 | GRADE_AGGREGATE_WEIGHTED_MEAN => get_string('aggregateweightedmean', 'grades'), | |
2642 | GRADE_AGGREGATE_WEIGHTED_MEAN2 => get_string('aggregateweightedmean2', 'grades'), | |
2643 | GRADE_AGGREGATE_EXTRACREDIT_MEAN => get_string('aggregateextracreditmean', 'grades'), | |
2644 | GRADE_AGGREGATE_MEDIAN => get_string('aggregatemedian', 'grades'), | |
2645 | GRADE_AGGREGATE_MIN => get_string('aggregatemin', 'grades'), | |
2646 | GRADE_AGGREGATE_MAX => get_string('aggregatemax', 'grades'), | |
2647 | GRADE_AGGREGATE_MODE => get_string('aggregatemode', 'grades'), | |
2648 | GRADE_AGGREGATE_SUM => get_string('aggregatesum', 'grades') | |
2649 | ); | |
2650 | } | |
2651 | return self::$aggregationstrings; | |
2652 | } | |
2653 | ||
4d5059d4 SH |
2654 | /** |
2655 | * Get grade_plugin_info object for managing settings if the user can | |
2656 | * | |
2657 | * @param int $courseid | |
8faa560c | 2658 | * @return grade_plugin_info[] |
4d5059d4 SH |
2659 | */ |
2660 | public static function get_info_manage_settings($courseid) { | |
2661 | if (self::$managesetting !== null) { | |
2662 | return self::$managesetting; | |
2663 | } | |
d4060472 | 2664 | $context = context_course::instance($courseid); |
8faa560c MG |
2665 | self::$managesetting = array(); |
2666 | if ($courseid != SITEID && has_capability('moodle/grade:manage', $context)) { | |
eb84b779 MG |
2667 | self::$managesetting['categoriesanditems'] = new grade_plugin_info('setup', |
2668 | new moodle_url('/grade/edit/tree/index.php', array('id' => $courseid)), | |
2669 | get_string('categoriesanditems', 'grades')); | |
8faa560c MG |
2670 | self::$managesetting['coursesettings'] = new grade_plugin_info('coursesettings', |
2671 | new moodle_url('/grade/edit/settings/index.php', array('id'=>$courseid)), | |
2672 | get_string('coursegradesettings', 'grades')); | |
eb84b779 MG |
2673 | } |
2674 | if (self::$gradereportpreferences === null) { | |
2675 | self::get_plugins_reports($courseid); | |
2676 | } | |
2677 | if (self::$gradereportpreferences) { | |
2678 | self::$managesetting = array_merge(self::$managesetting, self::$gradereportpreferences); | |
4d5059d4 SH |
2679 | } |
2680 | return self::$managesetting; | |
2681 | } | |
2682 | /** | |
2683 | * Returns an array of plugin reports as grade_plugin_info objects | |
2684 | * | |
2685 | * @param int $courseid | |
2686 | * @return array | |
2687 | */ | |
2688 | public static function get_plugins_reports($courseid) { | |
f7fcf4cd | 2689 | global $SITE; |
cf717dc2 | 2690 | |
4d5059d4 SH |
2691 | if (self::$gradereports !== null) { |
2692 | return self::$gradereports; | |
2693 | } | |
d4060472 | 2694 | $context = context_course::instance($courseid); |
4d5059d4 SH |
2695 | $gradereports = array(); |
2696 | $gradepreferences = array(); | |
bd3b3bba | 2697 | foreach (core_component::get_plugin_list('gradereport') as $plugin => $plugindir) { |
f7fcf4cd AD |
2698 | //some reports make no sense if we're not within a course |
2699 | if ($courseid==$SITE->id && ($plugin=='grader' || $plugin=='user')) { | |
2700 | continue; | |
2701 | } | |
2702 | ||
4d5059d4 SH |
2703 | // Remove ones we can't see |
2704 | if (!has_capability('gradereport/'.$plugin.':view', $context)) { | |
2705 | continue; | |
2706 | } | |
2707 | ||
b5e7b2bf | 2708 | $pluginstr = get_string('pluginname', 'gradereport_'.$plugin); |
4d5059d4 SH |
2709 | $url = new moodle_url('/grade/report/'.$plugin.'/index.php', array('id'=>$courseid)); |
2710 | $gradereports[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr); | |
2711 | ||
2712 | // Add link to preferences tab if such a page exists | |
2713 | if (file_exists($plugindir.'/preferences.php')) { | |
2714 | $url = new moodle_url('/grade/report/'.$plugin.'/preferences.php', array('id'=>$courseid)); | |
eb84b779 MG |
2715 | $gradepreferences[$plugin] = new grade_plugin_info($plugin, $url, |
2716 | get_string('mypreferences', 'grades') . ': ' . $pluginstr); | |
4d5059d4 SH |
2717 | } |
2718 | } | |
2719 | if (count($gradereports) == 0) { | |
2720 | $gradereports = false; | |
2721 | $gradepreferences = false; | |
2722 | } else if (count($gradepreferences) == 0) { | |
2723 | $gradepreferences = false; | |
2724 | asort($gradereports); | |
2725 | } else { | |
2726 | asort($gradereports); | |
2727 | asort($gradepreferences); | |
2728 | } | |
2729 | self::$gradereports = $gradereports; | |
2730 | self::$gradereportpreferences = $gradepreferences; | |
2731 | return self::$gradereports; | |
2732 | } | |
eb84b779 | 2733 | |
4d5059d4 SH |
2734 | /** |
2735 | * Get information on scales | |
2736 | * @param int $courseid | |
2737 | * @return grade_plugin_info | |
2738 | */ | |
2739 | public static function get_info_scales($courseid) { | |
2740 | if (self::$scaleinfo !== null) { | |
2741 | return self::$scaleinfo; | |
2742 | } | |
d4060472 | 2743 | if (has_capability('moodle/course:managescales', context_course::instance($courseid))) { |
4d5059d4 SH |
2744 | $url = new moodle_url('/grade/edit/scale/index.php', array('id'=>$courseid)); |
2745 | self::$scaleinfo = new grade_plugin_info('scale', $url, get_string('view')); | |
2746 | } else { | |
2747 | self::$scaleinfo = false; | |
2748 | } | |
2749 | return self::$scaleinfo; | |
2750 | } | |
2751 | /** | |
2752 | * Get information on outcomes | |
2753 | * @param int $courseid | |
2754 | * @return grade_plugin_info | |
2755 | */ | |
2756 | public static function get_info_outcomes($courseid) { | |
f7fcf4cd | 2757 | global $CFG, $SITE; |
4d5059d4 SH |
2758 | |
2759 | if (self::$outcomeinfo !== null) { | |
2760 | return self::$outcomeinfo; | |
2761 | } | |
d4060472 | 2762 | $context = context_course::instance($courseid); |
4d5059d4 SH |
2763 | $canmanage = has_capability('moodle/grade:manage', $context); |
2764 | $canupdate = has_capability('moodle/course:update', $context); | |
2765 | if (!empty($CFG->enableoutcomes) && ($canmanage || $canupdate)) { | |
2766 | $outcomes = array(); | |
2767 | if ($canupdate) { | |
f7fcf4cd AD |
2768 | if ($courseid!=$SITE->id) { |
2769 | $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid)); | |
2770 | $outcomes['course'] = new grade_plugin_info('course', $url, get_string('outcomescourse', 'grades')); | |
2771 | } | |
4d5059d4 SH |
2772 | $url = new moodle_url('/grade/edit/outcome/index.php', array('id'=>$courseid)); |
2773 | $outcomes['edit'] = new grade_plugin_info('edit', $url, get_string('editoutcomes', 'grades')); | |
c46aeeab DC |
2774 | $url = new moodle_url('/grade/edit/outcome/import.php', array('courseid'=>$courseid)); |
2775 | $outcomes['import'] = new grade_plugin_info('import', $url, get_string('importoutcomes', 'grades')); | |
4d5059d4 | 2776 | } else { |
f7fcf4cd AD |
2777 | if ($courseid!=$SITE->id) { |
2778 | $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid)); | |
2779 | $outcomes['edit'] = new grade_plugin_info('edit', $url, get_string('outcomescourse', 'grades')); | |
2780 | } | |
4d5059d4 SH |
2781 | } |
2782 | self::$outcomeinfo = $outcomes; | |
2783 | } else { | |
2784 | self::$outcomeinfo = false; | |
2785 | } | |
2786 | return self::$outcomeinfo; | |
2787 | } | |
4d5059d4 SH |
2788 | /** |
2789 | * Get information on letters | |
2790 | * @param int $courseid | |
4d27bc79 | 2791 | * @return array |
4d5059d4 SH |
2792 | */ |
2793 | public static function get_info_letters($courseid) { | |
a8dfc483 | 2794 | global $SITE; |
4d5059d4 SH |
2795 | if (self::$letterinfo !== null) { |
2796 | return self::$letterinfo; | |
2797 | } | |
d4060472 | 2798 | $context = context_course::instance($courseid); |
4d5059d4 SH |
2799 | $canmanage = has_capability('moodle/grade:manage', $context); |
2800 | $canmanageletters = has_capability('moodle/grade:manageletters', $context); | |
2801 | if ($canmanage || $canmanageletters) { | |
a8dfc483 AA |
2802 | // Redirect to system context when report is accessed from admin settings MDL-31633 |
2803 | if ($context->instanceid == $SITE->id) { | |
2804 | $param = array('edit' => 1); | |
2805 | } else { | |
2806 | $param = array('edit' => 1,'id' => $context->id); | |
2807 | } | |
4d5059d4 | 2808 | self::$letterinfo = array( |
54caa598 | 2809 | 'view' => new grade_plugin_info('view', new moodle_url('/grade/edit/letter/index.php', array('id'=>$context->id)), get_string('view')), |
a8dfc483 | 2810 | 'edit' => new grade_plugin_info('edit', new moodle_url('/grade/edit/letter/index.php', $param), get_string('edit')) |
4d5059d4 SH |
2811 | ); |
2812 | } else { | |
2813 | self::$letterinfo = false; | |
2814 | } | |
2815 | return self::$letterinfo; | |
2816 | } | |
2817 | /** | |
2818 | * Get information import plugins | |
2819 | * @param int $courseid | |
2820 | * @return array | |
2821 | */ | |
2822 | public static function get_plugins_import($courseid) { | |
2823 | global $CFG; | |
2824 | ||
2825 | if (self::$importplugins !== null) { | |
2826 | return self::$importplugins; | |
2827 | } | |
2828 | $importplugins = array(); | |
d4060472 | 2829 | $context = context_course::instance($courseid); |
4d5059d4 SH |
2830 | |
2831 | if (has_capability('moodle/grade:import', $context)) { | |
bd3b3bba | 2832 | foreach (core_component::get_plugin_list('gradeimport') as $plugin => $plugindir) { |
4d5059d4 SH |
2833 | if (!has_capability('gradeimport/'.$plugin.':view', $context)) { |
2834 | continue; | |
2835 | } | |
b5e7b2bf | 2836 | $pluginstr = get_string('pluginname', 'gradeimport_'.$plugin); |
4d5059d4 SH |
2837 | $url = new moodle_url('/grade/import/'.$plugin.'/index.php', array('id'=>$courseid)); |
2838 | $importplugins[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr); | |
2839 | } | |
2840 | ||
2841 | ||
2842 | if ($CFG->gradepublishing) { | |
2843 | $url = new moodle_url('/grade/import/keymanager.php', array('id'=>$courseid)); | |
2844 | $importplugins['keymanager'] = new grade_plugin_info('keymanager', $url, get_string('keymanager', 'grades')); | |
2845 | } | |
2846 | } | |
2847 | ||
2848 | if (count($importplugins) > 0) { | |
2849 | asort($importplugins); | |
2850 | self::$importplugins = $importplugins; | |
2851 | } else { | |
2852 | self::$importplugins = false; | |
2853 | } | |
2854 | return self::$importplugins; | |
2855 | } | |
2856 | /** | |
2857 | * Get information export plugins | |
2858 | * @param int $courseid | |
2859 | * @return array | |
2860 | */ | |
2861 | public static function get_plugins_export($courseid) { | |
2862 | global $CFG; | |
2863 | ||
2864 | if (self::$exportplugins !== null) { | |
2865 | return self::$exportplugins; | |
2866 | } | |
d4060472 | 2867 | $context = context_course::instance($courseid); |
4d5059d4 SH |
2868 | $exportplugins = array(); |
2869 | if (has_capability('moodle/grade:export', $context)) { | |
bd3b3bba | 2870 | foreach (core_component::get_plugin_list('gradeexport') as $plugin => $plugindir) { |
4d5059d4 SH |
2871 | if (!has_capability('gradeexport/'.$plugin.':view', $context)) { |
2872 | continue; | |
2873 | } | |
b5e7b2bf | 2874 | $pluginstr = get_string('pluginname', 'gradeexport_'.$plugin); |
4d5059d4 SH |
2875 | $url = new moodle_url('/grade/export/'.$plugin.'/index.php', array('id'=>$courseid)); |
2876 | $exportplugins[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr); | |
2877 | } | |
2878 | ||
2879 | if ($CFG->gradepublishing) { | |
2880 | $url = new moodle_url('/grade/export/keymanager.php', array('id'=>$courseid)); | |
2881 | $exportplugins['keymanager'] = new grade_plugin_info('keymanager', $url, get_string('keymanager', 'grades')); | |
2882 | } | |
2883 | } | |
2884 | if (count($exportplugins) > 0) { | |
2885 | asort($exportplugins); | |
2886 | self::$exportplugins = $exportplugins; | |
2887 | } else { | |
2888 | self::$exportplugins = false; | |
2889 | } | |
2890 | return self::$exportplugins; | |
2891 | } | |
293f42b6 | 2892 | |
61c8e0d7 FM |
2893 | /** |
2894 | * Returns the value of a field from a user record | |
2895 | * | |
2896 | * @param stdClass $user object | |
2897 | * @param stdClass $field object | |
2898 | * @return string value of the field | |
2899 | */ | |
2900 | public static function get_user_field_value($user, $field) { | |
2901 | if (!empty($field->customid)) { | |
2902 | $fieldname = 'customfield_' . $field->shortname; | |
2903 | if (!empty($user->{$fieldname}) || is_numeric($user->{$fieldname})) { | |
2904 | $fieldvalue = $user->{$fieldname}; | |
2905 | } else { | |
2906 | $fieldvalue = $field->default; | |
2907 | } | |
2908 | } else { | |
2909 | $fieldvalue = $user->{$field->shortname}; | |
2910 | } | |
2911 | return $fieldvalue; | |
2912 | } | |
2913 | ||
2914 | /** | |
2915 | * Returns an array of user profile fields to be included in export | |
2916 | * | |
2917 | * @param int $courseid | |
2918 | * @param bool $includecustomfields | |
2919 | * @return array An array of stdClass instances with customid, shortname, datatype, default and fullname fields | |
2920 | */ | |
2921 | public static function get_user_profile_fields($courseid, $includecustomfields = false) { | |
2922 | global $CFG, $DB; | |
2923 | ||
2924 | // Gets the fields that have to be hidden | |
2925 | $hiddenfields = array_map('trim', explode(',', $CFG->hiddenuserfields)); | |
2926 | $context = context_course::instance($courseid); | |
2927 | $canseehiddenfields = has_capability('moodle/course:viewhiddenuserfields', $context); | |
2928 | if ($canseehiddenfields) { | |
2929 | $hiddenfields = array(); | |
2930 | } | |
2931 | ||
2932 | $fields = array(); | |
2933 | require_once($CFG->dirroot.'/user/lib.php'); // Loads user_get_default_fields() | |
2934 | require_once($CFG->dirroot.'/user/profile/lib.php'); // Loads constants, such as PROFILE_VISIBLE_ALL | |
2935 | $userdefaultfields = user_get_default_fields(); | |
2936 | ||
2937 | // Sets the list of profile fields | |
2938 | $userprofilefields = array_map('trim', explode(',', $CFG->grade_export_userprofilefields)); | |
2939 | if (!empty($userprofilefields)) { | |
2940 | foreach ($userprofilefields as $field) { | |
2941 | $field = trim($field); | |
2942 | if (in_array($field, $hiddenfields) || !in_array($field, $userdefaultfields)) { | |
2943 | continue; | |
2944 | } | |
293f42b6 | 2945 | $obj = new stdClass(); |
61c8e0d7 | 2946 | $obj->customid = 0; |
293f42b6 | 2947 | $obj->shortname = $field; |
61c8e0d7 | 2948 | $obj->fullname = get_string($field); |
293f42b6 CF |
2949 | $fields[] = $obj; |
2950 | } | |
2951 | } | |
293f42b6 | 2952 | |
61c8e0d7 FM |
2953 | // Sets the list of custom profile fields |
2954 | $customprofilefields = array_map('trim', explode(',', $CFG->grade_export_customprofilefields)); | |
2955 | if ($includecustomfields && !empty($customprofilefields)) { | |
2956 | list($wherefields, $whereparams) = $DB->get_in_or_equal($customprofilefields); | |
2957 | $customfields = $DB->get_records_sql("SELECT f.* | |
2958 | FROM {user_info_field} f | |
2959 | JOIN {user_info_category} c ON f.categoryid=c.id | |
2960 | WHERE f.shortname $wherefields | |
2961 | ORDER BY c.sortorder ASC, f.sortorder ASC", $whereparams); | |
2962 | if (!is_array($customfields)) { | |
2963 | continue; | |
2964 | } | |
2965 | ||
2966 | foreach ($customfields as $field) { | |
2967 | // Make sure we can display this custom field | |
2968 | if (!in_array($field->shortname, $customprofilefields)) { | |
2969 | continue; | |
2970 | } else if (in_array($field->shortname, $hiddenfields)) { | |
2971 | continue; | |
2972 | } else if ($field->visible != PROFILE_VISIBLE_ALL && !$canseehiddenfields) { | |
2973 | continue; | |
2974 | } | |
2975 | ||
2976 | $obj = new stdClass(); | |
2977 | $obj->customid = $field->id; | |
2978 | $obj->shortname = $field->shortname; | |
2979 | $obj->fullname = format_string($field->name); | |
2980 | $obj->datatype = $field->datatype; | |
2981 | $obj->default = $field->defaultdata; | |
2982 | $fields[] = $obj; | |
2983 | } | |
2984 | } | |
2985 | ||
2986 | return $fields; | |
2987 | } | |
e61f25a4 DW |
2988 | |
2989 | /** | |
2990 | * This helper method gets a snapshot of all the weights for a course. | |
2991 | * It is used as a quick method to see if any wieghts have been automatically adjusted. | |
2992 | * @param int $courseid | |
2993 | * @return array of itemid -> aggregationcoef2 | |
2994 | */ | |
2995 | public static function fetch_all_natural_weights_for_course($courseid) { | |
2996 | global $DB; | |
2997 | $result = array(); | |
2998 | ||
2999 | $records = $DB->get_records('grade_items', array('courseid'=>$courseid), 'id', 'id, aggregationcoef2'); | |
3000 | foreach ($records as $record) { | |
3001 | $result[$record->id] = $record->aggregationcoef2; | |
3002 | } | |
3003 | return $result; | |
3004 | } | |
293f42b6 | 3005 | } |
61c8e0d7 | 3006 |