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 | |
1ef48469 JC |
1251 | $outputstr = ''; |
1252 | ||
1253 | // Object holding pix_icon information before instantiation. | |
1254 | $icon = new stdClass(); | |
1255 | $icon->attributes = array( | |
1256 | 'class' => 'item itemicon' | |
1257 | ); | |
1258 | $icon->component = 'moodle'; | |
1259 | ||
1260 | $none = true; | |
6cc3e350 | 1261 | switch ($element['type']) { |
1262 | case 'item': | |
1263 | case 'courseitem': | |
1264 | case 'categoryitem': | |
1ef48469 JC |
1265 | $none = false; |
1266 | ||
cf72e2dd | 1267 | $is_course = $element['object']->is_course_item(); |
1268 | $is_category = $element['object']->is_category_item(); | |
1269 | $is_scale = $element['object']->gradetype == GRADE_TYPE_SCALE; | |
1270 | $is_value = $element['object']->gradetype == GRADE_TYPE_VALUE; | |
0077f571 | 1271 | $is_outcome = !empty($element['object']->outcomeid); |
cf72e2dd | 1272 | |
6cc3e350 | 1273 | if ($element['object']->is_calculated()) { |
1ef48469 JC |
1274 | $icon->pix = 'i/calc'; |
1275 | $icon->title = s(get_string('calculatedgrade', 'grades')); | |
6cc3e350 | 1276 | |
cf72e2dd | 1277 | } else if (($is_course or $is_category) and ($is_scale or $is_value)) { |
6cc3e350 | 1278 | if ($category = $element['object']->get_item_category()) { |
bfe969e8 DW |
1279 | $aggrstrings = grade_helper::get_aggregation_strings(); |
1280 | $stragg = $aggrstrings[$category->aggregation]; | |
1ef48469 JC |
1281 | |
1282 | $icon->pix = 'i/calc'; | |
1283 | $icon->title = s($stragg); | |
1284 | ||
6cc3e350 | 1285 | switch ($category->aggregation) { |
1286 | case GRADE_AGGREGATE_MEAN: | |
1287 | case GRADE_AGGREGATE_MEDIAN: | |
1288 | case GRADE_AGGREGATE_WEIGHTED_MEAN: | |
1426edac | 1289 | case GRADE_AGGREGATE_WEIGHTED_MEAN2: |
6cc3e350 | 1290 | case GRADE_AGGREGATE_EXTRACREDIT_MEAN: |
1ef48469 JC |
1291 | $icon->pix = 'i/agg_mean'; |
1292 | break; | |
0758a08e | 1293 | case GRADE_AGGREGATE_SUM: |
1ef48469 JC |
1294 | $icon->pix = 'i/agg_sum'; |
1295 | break; | |
6cc3e350 | 1296 | } |
1297 | } | |
1298 | ||
1299 | } else if ($element['object']->itemtype == 'mod') { | |
1ef48469 | 1300 | // Prevent outcomes displaying the same icon as the activity they are attached to. |
0077f571 | 1301 | if ($is_outcome) { |
1ef48469 JC |
1302 | $icon->pix = 'i/outcomes'; |
1303 | $icon->title = s(get_string('outcome', 'grades')); | |
0077f571 | 1304 | } else { |
1ef48469 JC |
1305 | $icon->pix = 'icon'; |
1306 | $icon->component = $element['object']->itemmodule; | |
1307 | $icon->title = s(get_string('modulename', $element['object']->itemmodule)); | |
0077f571 | 1308 | } |
6cc3e350 | 1309 | } else if ($element['object']->itemtype == 'manual') { |
1310 | if ($element['object']->is_outcome_item()) { | |
1ef48469 JC |
1311 | $icon->pix = 'i/outcomes'; |
1312 | $icon->title = s(get_string('outcome', 'grades')); | |
6cc3e350 | 1313 | } else { |
1ef48469 JC |
1314 | $icon->pix = 'i/manual_item'; |
1315 | $icon->title = s(get_string('manualitem', 'grades')); | |
6cc3e350 | 1316 | } |
1317 | } | |
1318 | break; | |
1319 | ||
1320 | case 'category': | |
1ef48469 JC |
1321 | $none = false; |
1322 | $icon->pix = 'i/folder'; | |
1323 | $icon->title = s(get_string('category', 'grades')); | |
1324 | break; | |
6cc3e350 | 1325 | } |
1326 | ||
1ef48469 JC |
1327 | if ($none) { |
1328 | if ($spacerifnone) { | |
1329 | $outputstr = $OUTPUT->spacer() . ' '; | |
1330 | } | |
6cc3e350 | 1331 | } else { |
1ef48469 | 1332 | $outputstr = $OUTPUT->pix_icon($icon->pix, $icon->title, $icon->component, $icon->attributes); |
6cc3e350 | 1333 | } |
1ef48469 JC |
1334 | |
1335 | return $outputstr; | |
6cc3e350 | 1336 | } |
6391ebe7 | 1337 | |
e98871a2 | 1338 | /** |
6cc3e350 | 1339 | * Returns name of element optionally with icon and link |
cf72e2dd | 1340 | * |
1341 | * @param array &$element An array representing an element in the grade_tree | |
1342 | * @param bool $withlink Whether or not this header has a link | |
1343 | * @param bool $icon Whether or not to display an icon with this header | |
1344 | * @param bool $spacerifnone return spacer if no icon found | |
65c2ac93 | 1345 | * @param bool $withdescription Show description if defined by this item. |
41e62a90 AN |
1346 | * @param bool $fulltotal If the item is a category total, returns $categoryname."total" |
1347 | * instead of "Category total" or "Course total" | |
cf72e2dd | 1348 | * |
1349 | * @return string header | |
e98871a2 | 1350 | */ |
41e62a90 AN |
1351 | public function get_element_header(&$element, $withlink = false, $icon = true, $spacerifnone = false, |
1352 | $withdescription = false, $fulltotal = false) { | |
6cc3e350 | 1353 | $header = ''; |
1354 | ||
1355 | if ($icon) { | |
1356 | $header .= $this->get_element_icon($element, $spacerifnone); | |
1357 | } | |
1358 | ||
41e62a90 | 1359 | $header .= $element['object']->get_name($fulltotal); |
6cc3e350 | 1360 | |
cf72e2dd | 1361 | if ($element['type'] != 'item' and $element['type'] != 'categoryitem' and |
1362 | $element['type'] != 'courseitem') { | |
6cc3e350 | 1363 | return $header; |
1364 | } | |
1365 | ||
43cd76e8 JC |
1366 | if ($withlink && $url = $this->get_activity_link($element)) { |
1367 | $a = new stdClass(); | |
1368 | $a->name = get_string('modulename', $element['object']->itemmodule); | |
1369 | $title = get_string('linktoactivity', 'grades', $a); | |
1370 | ||
1371 | $header = html_writer::link($url, $header, array('title' => $title)); | |
1372 | } else { | |
1373 | $header = html_writer::span($header); | |
6cc3e350 | 1374 | } |
1375 | ||
65c2ac93 DW |
1376 | if ($withdescription) { |
1377 | $desc = $element['object']->get_description(); | |
1378 | if (!empty($desc)) { | |
1379 | $header .= '<div class="gradeitemdescription">' . s($desc) . '</div><div class="gradeitemdescriptionfiller"></div>'; | |
1380 | } | |
1381 | } | |
1382 | ||
6cc3e350 | 1383 | return $header; |
1384 | } | |
1385 | ||
73ca5f01 | 1386 | private function get_activity_link($element) { |
1387 | global $CFG; | |
eff314b0 DM |
1388 | /** @var array static cache of the grade.php file existence flags */ |
1389 | static $hasgradephp = array(); | |
73ca5f01 | 1390 | |
1391 | $itemtype = $element['object']->itemtype; | |
1392 | $itemmodule = $element['object']->itemmodule; | |
1393 | $iteminstance = $element['object']->iteminstance; | |
25ab1d50 | 1394 | $itemnumber = $element['object']->itemnumber; |
73ca5f01 | 1395 | |
1396 | // Links only for module items that have valid instance, module and are | |
1397 | // called from grade_tree with valid modinfo | |
1398 | if ($itemtype != 'mod' || !$iteminstance || !$itemmodule || !$this->modinfo) { | |
1399 | return null; | |
1400 | } | |
1401 | ||
1402 | // Get $cm efficiently and with visibility information using modinfo | |
1403 | $instances = $this->modinfo->get_instances(); | |
1404 | if (empty($instances[$itemmodule][$iteminstance])) { | |
1405 | return null; | |
1406 | } | |
1407 | $cm = $instances[$itemmodule][$iteminstance]; | |
1408 | ||
1409 | // Do not add link if activity is not visible to the current user | |
1410 | if (!$cm->uservisible) { | |
1411 | return null; | |
1412 | } | |
1413 | ||
eff314b0 DM |
1414 | if (!array_key_exists($itemmodule, $hasgradephp)) { |
1415 | if (file_exists($CFG->dirroot . '/mod/' . $itemmodule . '/grade.php')) { | |
1416 | $hasgradephp[$itemmodule] = true; | |
1417 | } else { | |
1418 | $hasgradephp[$itemmodule] = false; | |
1419 | } | |
1420 | } | |
1421 | ||
73ca5f01 | 1422 | // If module has grade.php, link to that, otherwise view.php |
eff314b0 | 1423 | if ($hasgradephp[$itemmodule]) { |
c97933ff MG |
1424 | $args = array('id' => $cm->id, 'itemnumber' => $itemnumber); |
1425 | if (isset($element['userid'])) { | |
1426 | $args['userid'] = $element['userid']; | |
1427 | } | |
1428 | return new moodle_url('/mod/' . $itemmodule . '/grade.php', $args); | |
73ca5f01 | 1429 | } else { |
1430 | return new moodle_url('/mod/' . $itemmodule . '/view.php', array('id' => $cm->id)); | |
1431 | } | |
1432 | } | |
1433 | ||
bb17ac1e DM |
1434 | /** |
1435 | * Returns URL of a page that is supposed to contain detailed grade analysis | |
1436 | * | |
bb17ac1e DM |
1437 | * At the moment, only activity modules are supported. The method generates link |
1438 | * to the module's file grade.php with the parameters id (cmid), itemid, itemnumber, | |
eff314b0 | 1439 | * gradeid and userid. If the grade.php does not exist, null is returned. |
bb17ac1e DM |
1440 | * |
1441 | * @return moodle_url|null URL or null if unable to construct it | |
1442 | */ | |
1443 | public function get_grade_analysis_url(grade_grade $grade) { | |
eff314b0 DM |
1444 | global $CFG; |
1445 | /** @var array static cache of the grade.php file existence flags */ | |
1446 | static $hasgradephp = array(); | |
bb17ac1e DM |
1447 | |
1448 | if (empty($grade->grade_item) or !($grade->grade_item instanceof grade_item)) { | |
1449 | throw new coding_exception('Passed grade without the associated grade item'); | |
1450 | } | |
1451 | $item = $grade->grade_item; | |
1452 | ||
1453 | if (!$item->is_external_item()) { | |
1454 | // at the moment, only activity modules are supported | |
1455 | return null; | |
1456 | } | |
1457 | if ($item->itemtype !== 'mod') { | |
1458 | throw new coding_exception('Unknown external itemtype: '.$item->itemtype); | |
1459 | } | |
1460 | if (empty($item->iteminstance) or empty($item->itemmodule) or empty($this->modinfo)) { | |
1461 | return null; | |
1462 | } | |
1463 | ||
eff314b0 DM |
1464 | if (!array_key_exists($item->itemmodule, $hasgradephp)) { |
1465 | if (file_exists($CFG->dirroot . '/mod/' . $item->itemmodule . '/grade.php')) { | |
1466 | $hasgradephp[$item->itemmodule] = true; | |
1467 | } else { | |
1468 | $hasgradephp[$item->itemmodule] = false; | |
1469 | } | |
1470 | } | |
1471 | ||
1472 | if (!$hasgradephp[$item->itemmodule]) { | |
1473 | return null; | |
1474 | } | |
1475 | ||
bb17ac1e DM |
1476 | $instances = $this->modinfo->get_instances(); |
1477 | if (empty($instances[$item->itemmodule][$item->iteminstance])) { | |
1478 | return null; | |
1479 | } | |
1480 | $cm = $instances[$item->itemmodule][$item->iteminstance]; | |
1481 | if (!$cm->uservisible) { | |
1482 | return null; | |
1483 | } | |
1484 | ||
1485 | $url = new moodle_url('/mod/'.$item->itemmodule.'/grade.php', array( | |
1486 | 'id' => $cm->id, | |
1487 | 'itemid' => $item->id, | |
1488 | 'itemnumber' => $item->itemnumber, | |
1489 | 'gradeid' => $grade->id, | |
1490 | 'userid' => $grade->userid, | |
1491 | )); | |
1492 | ||
1493 | return $url; | |
1494 | } | |
1495 | ||
1496 | /** | |
1497 | * Returns an action icon leading to the grade analysis page | |
1498 | * | |
1499 | * @param grade_grade $grade | |
1500 | * @return string | |
1501 | */ | |
1502 | public function get_grade_analysis_icon(grade_grade $grade) { | |
1503 | global $OUTPUT; | |
1504 | ||
1505 | $url = $this->get_grade_analysis_url($grade); | |
1506 | if (is_null($url)) { | |
1507 | return ''; | |
1508 | } | |
1509 | ||
c97933ff | 1510 | return $OUTPUT->action_icon($url, new pix_icon('t/preview', |
bb17ac1e DM |
1511 | get_string('gradeanalysis', 'core_grades'))); |
1512 | } | |
1513 | ||
6cc3e350 | 1514 | /** |
1515 | * Returns the grade eid - the grade may not exist yet. | |
cf72e2dd | 1516 | * |
1517 | * @param grade_grade $grade_grade A grade_grade object | |
1518 | * | |
6cc3e350 | 1519 | * @return string eid |
1520 | */ | |
d24832f9 | 1521 | public function get_grade_eid($grade_grade) { |
6cc3e350 | 1522 | if (empty($grade_grade->id)) { |
1523 | return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid; | |
1524 | } else { | |
1525 | return 'g'.$grade_grade->id; | |
1526 | } | |
1527 | } | |
1528 | ||
1529 | /** | |
1530 | * Returns the grade_item eid | |
cf72e2dd | 1531 | * @param grade_item $grade_item A grade_item object |
6cc3e350 | 1532 | * @return string eid |
1533 | */ | |
d24832f9 | 1534 | public function get_item_eid($grade_item) { |
ddb9c95f | 1535 | return 'ig'.$grade_item->id; |
6cc3e350 | 1536 | } |
1537 | ||
cf72e2dd | 1538 | /** |
1539 | * Given a grade_tree element, returns an array of parameters | |
1540 | * used to build an icon for that element. | |
1541 | * | |
1542 | * @param array $element An array representing an element in the grade_tree | |
1543 | * | |
1544 | * @return array | |
1545 | */ | |
1546 | public function get_params_for_iconstr($element) { | |
9ecd4386 | 1547 | $strparams = new stdClass(); |
1548 | $strparams->category = ''; | |
1549 | $strparams->itemname = ''; | |
1550 | $strparams->itemmodule = ''; | |
cf72e2dd | 1551 | |
9ecd4386 | 1552 | if (!method_exists($element['object'], 'get_name')) { |
1553 | return $strparams; | |
1554 | } | |
345674ca | 1555 | |
b285c5aa | 1556 | $strparams->itemname = html_to_text($element['object']->get_name()); |
9ecd4386 | 1557 | |
1558 | // If element name is categorytotal, get the name of the parent category | |
1559 | if ($strparams->itemname == get_string('categorytotal', 'grades')) { | |
1560 | $parent = $element['object']->get_parent_category(); | |
1561 | $strparams->category = $parent->get_name() . ' '; | |
1562 | } else { | |
1563 | $strparams->category = ''; | |
1564 | } | |
1565 | ||
1566 | $strparams->itemmodule = null; | |
1567 | if (isset($element['object']->itemmodule)) { | |
1568 | $strparams->itemmodule = $element['object']->itemmodule; | |
345674ca | 1569 | } |
9ecd4386 | 1570 | return $strparams; |
1571 | } | |
1572 | ||
ae93f353 FM |
1573 | /** |
1574 | * Return a reset icon for the given element. | |
1575 | * | |
1576 | * @param array $element An array representing an element in the grade_tree | |
1577 | * @param object $gpr A grade_plugin_return object | |
079b2e52 MG |
1578 | * @param bool $returnactionmenulink return the instance of action_menu_link instead of string |
1579 | * @return string|action_menu_link | |
ae93f353 | 1580 | */ |
079b2e52 | 1581 | public function get_reset_icon($element, $gpr, $returnactionmenulink = false) { |
ae93f353 FM |
1582 | global $CFG, $OUTPUT; |
1583 | ||
1584 | // Limit to category items set to use the natural weights aggregation method, and users | |
1585 | // with the capability to manage grades. | |
1586 | if ($element['type'] != 'category' || $element['object']->aggregation != GRADE_AGGREGATE_SUM || | |
1587 | !has_capability('moodle/grade:manage', $this->context)) { | |
079b2e52 | 1588 | return $returnactionmenulink ? null : ''; |
ae93f353 FM |
1589 | } |
1590 | ||
1591 | $str = get_string('resetweights', 'grades', $this->get_params_for_iconstr($element)); | |
1592 | $url = new moodle_url('/grade/edit/tree/action.php', array( | |
1593 | 'id' => $this->courseid, | |
1594 | 'action' => 'resetweights', | |
1595 | 'eid' => $element['eid'], | |
1596 | 'sesskey' => sesskey(), | |
1597 | )); | |
1598 | ||
079b2e52 MG |
1599 | if ($returnactionmenulink) { |
1600 | return new action_menu_link_secondary($gpr->add_url_params($url), new pix_icon('t/reset', $str), | |
1601 | get_string('resetweightsshort', 'grades')); | |
1602 | } else { | |
1603 | return $OUTPUT->action_icon($gpr->add_url_params($url), new pix_icon('t/reset', $str)); | |
1604 | } | |
ae93f353 FM |
1605 | } |
1606 | ||
6cc3e350 | 1607 | /** |
1608 | * Return edit icon for give element | |
cf72e2dd | 1609 | * |
1610 | * @param array $element An array representing an element in the grade_tree | |
1611 | * @param object $gpr A grade_plugin_return object | |
079b2e52 MG |
1612 | * @param bool $returnactionmenulink return the instance of action_menu_link instead of string |
1613 | * @return string|action_menu_link | |
6cc3e350 | 1614 | */ |
079b2e52 | 1615 | public function get_edit_icon($element, $gpr, $returnactionmenulink = false) { |
6b608f8f | 1616 | global $CFG, $OUTPUT; |
6cc3e350 | 1617 | |
1618 | if (!has_capability('moodle/grade:manage', $this->context)) { | |
1619 | if ($element['type'] == 'grade' and has_capability('moodle/grade:edit', $this->context)) { | |
1620 | // oki - let them override grade | |
1621 | } else { | |
079b2e52 | 1622 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 1623 | } |
1624 | } | |
1625 | ||
05aba805 | 1626 | static $strfeedback = null; |
1627 | static $streditgrade = null; | |
1628 | if (is_null($streditgrade)) { | |
1629 | $streditgrade = get_string('editgrade', 'grades'); | |
1630 | $strfeedback = get_string('feedback'); | |
6cc3e350 | 1631 | } |
1632 | ||
9ecd4386 | 1633 | $strparams = $this->get_params_for_iconstr($element); |
6ef4878b | 1634 | |
6cc3e350 | 1635 | $object = $element['object']; |
6cc3e350 | 1636 | |
1637 | switch ($element['type']) { | |
1638 | case 'item': | |
1639 | case 'categoryitem': | |
1640 | case 'courseitem': | |
9ecd4386 | 1641 | $stredit = get_string('editverbose', 'grades', $strparams); |
6cc3e350 | 1642 | if (empty($object->outcomeid) || empty($CFG->enableoutcomes)) { |
a6855934 | 1643 | $url = new moodle_url('/grade/edit/tree/item.php', |
1c1f64a2 | 1644 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1645 | } else { |
a6855934 | 1646 | $url = new moodle_url('/grade/edit/tree/outcomeitem.php', |
1c1f64a2 | 1647 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1648 | } |
6cc3e350 | 1649 | break; |
1650 | ||
1651 | case 'category': | |
9ecd4386 | 1652 | $stredit = get_string('editverbose', 'grades', $strparams); |
a6855934 | 1653 | $url = new moodle_url('/grade/edit/tree/category.php', |
1c1f64a2 | 1654 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1655 | break; |
1656 | ||
1657 | case 'grade': | |
05aba805 | 1658 | $stredit = $streditgrade; |
6cc3e350 | 1659 | if (empty($object->id)) { |
a6855934 | 1660 | $url = new moodle_url('/grade/edit/tree/grade.php', |
1c1f64a2 | 1661 | array('courseid' => $this->courseid, 'itemid' => $object->itemid, 'userid' => $object->userid)); |
6cc3e350 | 1662 | } else { |
a6855934 | 1663 | $url = new moodle_url('/grade/edit/tree/grade.php', |
1c1f64a2 | 1664 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1665 | } |
6cc3e350 | 1666 | if (!empty($object->feedback)) { |
1667 | $feedback = addslashes_js(trim(format_string($object->feedback, $object->feedbackformat))); | |
6cc3e350 | 1668 | } |
1669 | break; | |
1670 | ||
1671 | default: | |
1672 | $url = null; | |
1673 | } | |
1674 | ||
1675 | if ($url) { | |
079b2e52 MG |
1676 | if ($returnactionmenulink) { |
1677 | return new action_menu_link_secondary($gpr->add_url_params($url), | |
1678 | new pix_icon('t/edit', $stredit), | |
1679 | get_string('editsettings')); | |
1680 | } else { | |
1681 | return $OUTPUT->action_icon($gpr->add_url_params($url), new pix_icon('t/edit', $stredit)); | |
1682 | } | |
6cc3e350 | 1683 | |
1684 | } else { | |
079b2e52 | 1685 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 1686 | } |
1687 | } | |
1688 | ||
1689 | /** | |
1690 | * Return hiding icon for give element | |
cf72e2dd | 1691 | * |
1692 | * @param array $element An array representing an element in the grade_tree | |
1693 | * @param object $gpr A grade_plugin_return object | |
079b2e52 MG |
1694 | * @param bool $returnactionmenulink return the instance of action_menu_link instead of string |
1695 | * @return string|action_menu_link | |
6cc3e350 | 1696 | */ |
079b2e52 | 1697 | public function get_hiding_icon($element, $gpr, $returnactionmenulink = false) { |
5d3b9994 | 1698 | global $CFG, $OUTPUT; |
6cc3e350 | 1699 | |
39873128 | 1700 | if (!$element['object']->can_control_visibility()) { |
079b2e52 | 1701 | return $returnactionmenulink ? null : ''; |
39873128 TH |
1702 | } |
1703 | ||
cf72e2dd | 1704 | if (!has_capability('moodle/grade:manage', $this->context) and |
1705 | !has_capability('moodle/grade:hide', $this->context)) { | |
079b2e52 | 1706 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 1707 | } |
1708 | ||
345674ca | 1709 | $strparams = $this->get_params_for_iconstr($element); |
9ecd4386 | 1710 | $strshow = get_string('showverbose', 'grades', $strparams); |
345674ca | 1711 | $strhide = get_string('hideverbose', 'grades', $strparams); |
6cc3e350 | 1712 | |
a6855934 | 1713 | $url = new moodle_url('/grade/edit/tree/action.php', array('id' => $this->courseid, 'sesskey' => sesskey(), 'eid' => $element['eid'])); |
8ae8bf8a | 1714 | $url = $gpr->add_url_params($url); |
1c1f64a2 | 1715 | |
6cc3e350 | 1716 | if ($element['object']->is_hidden()) { |
8ae8bf8a | 1717 | $type = 'show'; |
6cc3e350 | 1718 | $tooltip = $strshow; |
1719 | ||
cf72e2dd | 1720 | // Change the icon and add a tooltip showing the date |
1721 | if ($element['type'] != 'category' and $element['object']->get_hidden() > 1) { | |
8ae8bf8a | 1722 | $type = 'hiddenuntil'; |
cf72e2dd | 1723 | $tooltip = get_string('hiddenuntildate', 'grades', |
1724 | userdate($element['object']->get_hidden())); | |
6cc3e350 | 1725 | } |
1726 | ||
8ae8bf8a | 1727 | $url->param('action', 'show'); |
b9cc756e | 1728 | |
079b2e52 MG |
1729 | if ($returnactionmenulink) { |
1730 | $hideicon = new action_menu_link_secondary($url, new pix_icon('t/'.$type, $tooltip), get_string('show')); | |
1731 | } else { | |
1732 | $hideicon = $OUTPUT->action_icon($url, new pix_icon('t/'.$type, $tooltip, 'moodle', array('alt'=>$strshow, 'class'=>'smallicon'))); | |
1733 | } | |
6cc3e350 | 1734 | |
1735 | } else { | |
8ae8bf8a | 1736 | $url->param('action', 'hide'); |
079b2e52 MG |
1737 | if ($returnactionmenulink) { |
1738 | $hideicon = new action_menu_link_secondary($url, new pix_icon('t/hide', $strhide), get_string('hide')); | |
1739 | } else { | |
1740 | $hideicon = $OUTPUT->action_icon($url, new pix_icon('t/hide', $strhide)); | |
1741 | } | |
6cc3e350 | 1742 | } |
1c1f64a2 | 1743 | |
8ae8bf8a | 1744 | return $hideicon; |
6cc3e350 | 1745 | } |
1746 | ||
1747 | /** | |
2673c733 | 1748 | * Return locking icon for given element |
cf72e2dd | 1749 | * |
1750 | * @param array $element An array representing an element in the grade_tree | |
1751 | * @param object $gpr A grade_plugin_return object | |
1752 | * | |
6cc3e350 | 1753 | * @return string |
1754 | */ | |
d24832f9 | 1755 | public function get_locking_icon($element, $gpr) { |
6b608f8f | 1756 | global $CFG, $OUTPUT; |
6cc3e350 | 1757 | |
345674ca | 1758 | $strparams = $this->get_params_for_iconstr($element); |
9ecd4386 | 1759 | $strunlock = get_string('unlockverbose', 'grades', $strparams); |
1760 | $strlock = get_string('lockverbose', 'grades', $strparams); | |
d24832f9 | 1761 | |
a6855934 | 1762 | $url = new moodle_url('/grade/edit/tree/action.php', array('id' => $this->courseid, 'sesskey' => sesskey(), 'eid' => $element['eid'])); |
8ae8bf8a | 1763 | $url = $gpr->add_url_params($url); |
1c1f64a2 | 1764 | |
2673c733 | 1765 | // Don't allow an unlocking action for a grade whose grade item is locked: just print a state icon |
1766 | if ($element['type'] == 'grade' && $element['object']->grade_item->is_locked()) { | |
1767 | $strparamobj = new stdClass(); | |
1768 | $strparamobj->itemname = $element['object']->grade_item->itemname; | |
1769 | $strnonunlockable = get_string('nonunlockableverbose', 'grades', $strparamobj); | |
1c1f64a2 | 1770 | |
0cddd851 FM |
1771 | $action = html_writer::tag('span', $OUTPUT->pix_icon('t/locked', $strnonunlockable), |
1772 | array('class' => 'action-icon')); | |
8ae8bf8a | 1773 | |
cf72e2dd | 1774 | } else if ($element['object']->is_locked()) { |
8ae8bf8a | 1775 | $type = 'unlock'; |
6cc3e350 | 1776 | $tooltip = $strunlock; |
1777 | ||
cf72e2dd | 1778 | // Change the icon and add a tooltip showing the date |
1779 | if ($element['type'] != 'category' and $element['object']->get_locktime() > 1) { | |
8ae8bf8a | 1780 | $type = 'locktime'; |
cf72e2dd | 1781 | $tooltip = get_string('locktimedate', 'grades', |
1782 | userdate($element['object']->get_locktime())); | |
6cc3e350 | 1783 | } |
1784 | ||
8ae8bf8a PS |
1785 | if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:unlock', $this->context)) { |
1786 | $action = ''; | |
1787 | } else { | |
1788 | $url->param('action', 'unlock'); | |
c63923bd | 1789 | $action = $OUTPUT->action_icon($url, new pix_icon('t/'.$type, $tooltip, 'moodle', array('alt'=>$strunlock, 'class'=>'smallicon'))); |
6cc3e350 | 1790 | } |
6cc3e350 | 1791 | |
1792 | } else { | |
8ae8bf8a PS |
1793 | if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:lock', $this->context)) { |
1794 | $action = ''; | |
1795 | } else { | |
1796 | $url->param('action', 'lock'); | |
c63923bd | 1797 | $action = $OUTPUT->action_icon($url, new pix_icon('t/lock', $strlock)); |
6cc3e350 | 1798 | } |
6cc3e350 | 1799 | } |
8ae8bf8a | 1800 | |
6cc3e350 | 1801 | return $action; |
1802 | } | |
1803 | ||
1804 | /** | |
1805 | * Return calculation icon for given element | |
cf72e2dd | 1806 | * |
1807 | * @param array $element An array representing an element in the grade_tree | |
1808 | * @param object $gpr A grade_plugin_return object | |
079b2e52 MG |
1809 | * @param bool $returnactionmenulink return the instance of action_menu_link instead of string |
1810 | * @return string|action_menu_link | |
6cc3e350 | 1811 | */ |
079b2e52 | 1812 | public function get_calculation_icon($element, $gpr, $returnactionmenulink = false) { |
666e8458 | 1813 | global $CFG, $OUTPUT; |
6cc3e350 | 1814 | if (!has_capability('moodle/grade:manage', $this->context)) { |
079b2e52 | 1815 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 1816 | } |
1817 | ||
6cc3e350 | 1818 | $type = $element['type']; |
1819 | $object = $element['object']; | |
1820 | ||
1821 | if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') { | |
345674ca | 1822 | $strparams = $this->get_params_for_iconstr($element); |
9ecd4386 | 1823 | $streditcalculation = get_string('editcalculationverbose', 'grades', $strparams); |
6cc3e350 | 1824 | |
cf72e2dd | 1825 | $is_scale = $object->gradetype == GRADE_TYPE_SCALE; |
1826 | $is_value = $object->gradetype == GRADE_TYPE_VALUE; | |
1827 | ||
6cc3e350 | 1828 | // show calculation icon only when calculation possible |
cf72e2dd | 1829 | if (!$object->is_external_item() and ($is_scale or $is_value)) { |
6cc3e350 | 1830 | if ($object->is_calculated()) { |
666e8458 | 1831 | $icon = 't/calc'; |
6cc3e350 | 1832 | } else { |
666e8458 | 1833 | $icon = 't/calc_off'; |
6cc3e350 | 1834 | } |
cf72e2dd | 1835 | |
a6855934 | 1836 | $url = new moodle_url('/grade/edit/tree/calculation.php', array('courseid' => $this->courseid, 'id' => $object->id)); |
8ae8bf8a | 1837 | $url = $gpr->add_url_params($url); |
079b2e52 MG |
1838 | if ($returnactionmenulink) { |
1839 | return new action_menu_link_secondary($url, | |
1840 | new pix_icon($icon, $streditcalculation), | |
1841 | get_string('editcalculation', 'grades')); | |
1842 | } else { | |
1843 | return $OUTPUT->action_icon($url, new pix_icon($icon, $streditcalculation)); | |
1844 | } | |
6cc3e350 | 1845 | } |
1846 | } | |
1847 | ||
079b2e52 | 1848 | return $returnactionmenulink ? null : ''; |
6cc3e350 | 1849 | } |
1850 | } | |
1851 | ||
1852 | /** | |
1853 | * Flat structure similar to grade tree. | |
cf72e2dd | 1854 | * |
1855 | * @uses grade_structure | |
a153c9f2 | 1856 | * @package core_grades |
cf72e2dd | 1857 | * @copyright 2009 Nicolas Connault |
1858 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
6cc3e350 | 1859 | */ |
1860 | class grade_seq extends grade_structure { | |
1861 | ||
6cc3e350 | 1862 | /** |
1863 | * 1D array of elements | |
1864 | */ | |
d24832f9 | 1865 | public $elements; |
e98871a2 | 1866 | |
1867 | /** | |
1868 | * Constructor, retrieves and stores array of all grade_category and grade_item | |
1869 | * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed. | |
cf72e2dd | 1870 | * |
1871 | * @param int $courseid The course id | |
1872 | * @param bool $category_grade_last category grade item is the last child | |
1873 | * @param bool $nooutcomes Whether or not outcomes should be included | |
e98871a2 | 1874 | */ |
d24832f9 | 1875 | public function grade_seq($courseid, $category_grade_last=false, $nooutcomes=false) { |
e98871a2 | 1876 | global $USER, $CFG; |
1877 | ||
1878 | $this->courseid = $courseid; | |
d4060472 | 1879 | $this->context = context_course::instance($courseid); |
e98871a2 | 1880 | |
1881 | // get course grade tree | |
1882 | $top_element = grade_category::fetch_course_tree($courseid, true); | |
1883 | ||
6391ebe7 | 1884 | $this->elements = grade_seq::flatten($top_element, $category_grade_last, $nooutcomes); |
1885 | ||
1886 | foreach ($this->elements as $key=>$unused) { | |
b89a70ce | 1887 | $this->items[$this->elements[$key]['object']->id] =& $this->elements[$key]['object']; |
6391ebe7 | 1888 | } |
e98871a2 | 1889 | } |
1890 | ||
1891 | /** | |
1892 | * Static recursive helper - makes the grade_item for category the last children | |
cf72e2dd | 1893 | * |
1894 | * @param array &$element The seed of the recursion | |
1895 | * @param bool $category_grade_last category grade item is the last child | |
1896 | * @param bool $nooutcomes Whether or not outcomes should be included | |
1897 | * | |
1898 | * @return array | |
e98871a2 | 1899 | */ |
d24832f9 | 1900 | public function flatten(&$element, $category_grade_last, $nooutcomes) { |
e98871a2 | 1901 | if (empty($element['children'])) { |
1902 | return array(); | |
1903 | } | |
1904 | $children = array(); | |
1905 | ||
1906 | foreach ($element['children'] as $sortorder=>$unused) { | |
cf72e2dd | 1907 | if ($nooutcomes and $element['type'] != 'category' and |
1908 | $element['children'][$sortorder]['object']->is_outcome_item()) { | |
e98871a2 | 1909 | continue; |
1910 | } | |
1911 | $children[] = $element['children'][$sortorder]; | |
1912 | } | |
1913 | unset($element['children']); | |
1914 | ||
1915 | if ($category_grade_last and count($children) > 1) { | |
1916 | $cat_item = array_shift($children); | |
1917 | array_push($children, $cat_item); | |
1918 | } | |
1919 | ||
1920 | $result = array(); | |
1921 | foreach ($children as $child) { | |
e0724506 | 1922 | if ($child['type'] == 'category') { |
6391ebe7 | 1923 | $result = $result + grade_seq::flatten($child, $category_grade_last, $nooutcomes); |
e98871a2 | 1924 | } else { |
1925 | $child['eid'] = 'i'.$child['object']->id; | |
6391ebe7 | 1926 | $result[$child['object']->id] = $child; |
e98871a2 | 1927 | } |
1928 | } | |
1929 | ||
1930 | return $result; | |
1931 | } | |
1932 | ||
1933 | /** | |
1934 | * Parses the array in search of a given eid and returns a element object with | |
1935 | * information about the element it has found. | |
cf72e2dd | 1936 | * |
1937 | * @param int $eid Gradetree Element ID | |
1938 | * | |
e98871a2 | 1939 | * @return object element |
1940 | */ | |
d24832f9 | 1941 | public function locate_element($eid) { |
e98871a2 | 1942 | // it is a grade - construct a new object |
1943 | if (strpos($eid, 'n') === 0) { | |
1944 | if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) { | |
1945 | return null; | |
1946 | } | |
1947 | ||
1948 | $itemid = $matches[1]; | |
1949 | $userid = $matches[2]; | |
1950 | ||
1951 | //extra security check - the grade item must be in this tree | |
ddb9c95f | 1952 | if (!$item_el = $this->locate_element('ig'.$itemid)) { |
e98871a2 | 1953 | return null; |
1954 | } | |
1955 | ||
1956 | // $gradea->id may be null - means does not exist yet | |
1957 | $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid)); | |
1958 | ||
1959 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
1960 | return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade'); | |
1961 | ||
1962 | } else if (strpos($eid, 'g') === 0) { | |
cf72e2dd | 1963 | $id = (int) substr($eid, 1); |
e98871a2 | 1964 | if (!$grade = grade_grade::fetch(array('id'=>$id))) { |
1965 | return null; | |
1966 | } | |
1967 | //extra security check - the grade item must be in this tree | |
ddb9c95f | 1968 | if (!$item_el = $this->locate_element('ig'.$grade->itemid)) { |
e98871a2 | 1969 | return null; |
1970 | } | |
1971 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
1972 | return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade'); | |
1973 | } | |
1974 | ||
1975 | // it is a category or item | |
6391ebe7 | 1976 | foreach ($this->elements as $element) { |
6cc3e350 | 1977 | if ($element['eid'] == $eid) { |
1978 | return $element; | |
1979 | } | |
e98871a2 | 1980 | } |
6cc3e350 | 1981 | |
1982 | return null; | |
e98871a2 | 1983 | } |
e98871a2 | 1984 | } |
1985 | ||
7a6b7acf | 1986 | /** |
1987 | * This class represents a complete tree of categories, grade_items and final grades, | |
1988 | * organises as an array primarily, but which can also be converted to other formats. | |
1989 | * It has simple method calls with complex implementations, allowing for easy insertion, | |
1990 | * deletion and moving of items and categories within the tree. | |
cf72e2dd | 1991 | * |
1992 | * @uses grade_structure | |
a153c9f2 | 1993 | * @package core_grades |
cf72e2dd | 1994 | * @copyright 2009 Nicolas Connault |
1995 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
7a6b7acf | 1996 | */ |
6cc3e350 | 1997 | class grade_tree extends grade_structure { |
7a6b7acf | 1998 | |
1999 | /** | |
2000 | * The basic representation of the tree as a hierarchical, 3-tiered array. | |
2001 | * @var object $top_element | |
2002 | */ | |
d24832f9 | 2003 | public $top_element; |
7a6b7acf | 2004 | |
7a6b7acf | 2005 | /** |
2006 | * 2D array of grade items and categories | |
cf72e2dd | 2007 | * @var array $levels |
7a6b7acf | 2008 | */ |
d24832f9 | 2009 | public $levels; |
7a6b7acf | 2010 | |
b89a70ce | 2011 | /** |
2012 | * Grade items | |
cf72e2dd | 2013 | * @var array $items |
b89a70ce | 2014 | */ |
d24832f9 | 2015 | public $items; |
b89a70ce | 2016 | |
7a6b7acf | 2017 | /** |
2018 | * Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item | |
e98871a2 | 2019 | * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed. |
cf72e2dd | 2020 | * |
2021 | * @param int $courseid The Course ID | |
2022 | * @param bool $fillers include fillers and colspans, make the levels var "rectangular" | |
2023 | * @param bool $category_grade_last category grade item is the last child | |
4faf5f99 | 2024 | * @param array $collapsed array of collapsed categories |
cf72e2dd | 2025 | * @param bool $nooutcomes Whether or not outcomes should be included |
7a6b7acf | 2026 | */ |
cf72e2dd | 2027 | public function grade_tree($courseid, $fillers=true, $category_grade_last=false, |
2028 | $collapsed=null, $nooutcomes=false) { | |
73ca5f01 | 2029 | global $USER, $CFG, $COURSE, $DB; |
7a6b7acf | 2030 | |
2031 | $this->courseid = $courseid; | |
7a6b7acf | 2032 | $this->levels = array(); |
d4060472 | 2033 | $this->context = context_course::instance($courseid); |
7a6b7acf | 2034 | |
73ca5f01 | 2035 | if (!empty($COURSE->id) && $COURSE->id == $this->courseid) { |
2036 | $course = $COURSE; | |
2037 | } else { | |
2038 | $course = $DB->get_record('course', array('id' => $this->courseid)); | |
2039 | } | |
2040 | $this->modinfo = get_fast_modinfo($course); | |
2041 | ||
7a6b7acf | 2042 | // get course grade tree |
2043 | $this->top_element = grade_category::fetch_course_tree($courseid, true); | |
2044 | ||
4faf5f99 | 2045 | // collapse the categories if requested |
2046 | if (!empty($collapsed)) { | |
2047 | grade_tree::category_collapse($this->top_element, $collapsed); | |
2048 | } | |
2049 | ||
aea4df41 | 2050 | // no otucomes if requested |
2051 | if (!empty($nooutcomes)) { | |
2052 | grade_tree::no_outcomes($this->top_element); | |
2053 | } | |
2054 | ||
4faf5f99 | 2055 | // move category item to last position in category |
7a6b7acf | 2056 | if ($category_grade_last) { |
2057 | grade_tree::category_grade_last($this->top_element); | |
2058 | } | |
2059 | ||
2060 | if ($fillers) { | |
2061 | // inject fake categories == fillers | |
2062 | grade_tree::inject_fillers($this->top_element, 0); | |
2063 | // add colspans to categories and fillers | |
2064 | grade_tree::inject_colspans($this->top_element); | |
2065 | } | |
2066 | ||
2067 | grade_tree::fill_levels($this->levels, $this->top_element, 0); | |
d297269d | 2068 | |
7a6b7acf | 2069 | } |
2070 | ||
4faf5f99 | 2071 | /** |
2072 | * Static recursive helper - removes items from collapsed categories | |
cf72e2dd | 2073 | * |
2074 | * @param array &$element The seed of the recursion | |
4faf5f99 | 2075 | * @param array $collapsed array of collapsed categories |
cf72e2dd | 2076 | * |
4faf5f99 | 2077 | * @return void |
2078 | */ | |
d24832f9 | 2079 | public function category_collapse(&$element, $collapsed) { |
4faf5f99 | 2080 | if ($element['type'] != 'category') { |
2081 | return; | |
2082 | } | |
2083 | if (empty($element['children']) or count($element['children']) < 2) { | |
2084 | return; | |
2085 | } | |
2086 | ||
384960dd | 2087 | if (in_array($element['object']->id, $collapsed['aggregatesonly'])) { |
4faf5f99 | 2088 | $category_item = reset($element['children']); //keep only category item |
2089 | $element['children'] = array(key($element['children'])=>$category_item); | |
2090 | ||
2091 | } else { | |
384960dd | 2092 | if (in_array($element['object']->id, $collapsed['gradesonly'])) { // Remove category item |
2093 | reset($element['children']); | |
2094 | $first_key = key($element['children']); | |
2095 | unset($element['children'][$first_key]); | |
2096 | } | |
2097 | foreach ($element['children'] as $sortorder=>$child) { // Recurse through the element's children | |
4faf5f99 | 2098 | grade_tree::category_collapse($element['children'][$sortorder], $collapsed); |
2099 | } | |
2100 | } | |
2101 | } | |
7a6b7acf | 2102 | |
aea4df41 | 2103 | /** |
2104 | * Static recursive helper - removes all outcomes | |
cf72e2dd | 2105 | * |
2106 | * @param array &$element The seed of the recursion | |
2107 | * | |
aea4df41 | 2108 | * @return void |
2109 | */ | |
d24832f9 | 2110 | public function no_outcomes(&$element) { |
aea4df41 | 2111 | if ($element['type'] != 'category') { |
2112 | return; | |
2113 | } | |
2114 | foreach ($element['children'] as $sortorder=>$child) { | |
2115 | if ($element['children'][$sortorder]['type'] == 'item' | |
2116 | and $element['children'][$sortorder]['object']->is_outcome_item()) { | |
2117 | unset($element['children'][$sortorder]); | |
2118 | ||
d4795a07 | 2119 | } else if ($element['children'][$sortorder]['type'] == 'category') { |
aea4df41 | 2120 | grade_tree::no_outcomes($element['children'][$sortorder]); |
2121 | } | |
2122 | } | |
2123 | } | |
2124 | ||
7a6b7acf | 2125 | /** |
2126 | * Static recursive helper - makes the grade_item for category the last children | |
cf72e2dd | 2127 | * |
2128 | * @param array &$element The seed of the recursion | |
2129 | * | |
7a6b7acf | 2130 | * @return void |
2131 | */ | |
d24832f9 | 2132 | public function category_grade_last(&$element) { |
7a6b7acf | 2133 | if (empty($element['children'])) { |
2134 | return; | |
2135 | } | |
2136 | if (count($element['children']) < 2) { | |
2137 | return; | |
2138 | } | |
3e0e2436 | 2139 | $first_item = reset($element['children']); |
4a3dfd9a | 2140 | if ($first_item['type'] == 'categoryitem' or $first_item['type'] == 'courseitem') { |
3e0e2436 | 2141 | // the category item might have been already removed |
2142 | $order = key($element['children']); | |
2143 | unset($element['children'][$order]); | |
2144 | $element['children'][$order] =& $first_item; | |
2145 | } | |
206f9953 | 2146 | foreach ($element['children'] as $sortorder => $child) { |
7a6b7acf | 2147 | grade_tree::category_grade_last($element['children'][$sortorder]); |
2148 | } | |
2149 | } | |
2150 | ||
2151 | /** | |
2152 | * Static recursive helper - fills the levels array, useful when accessing tree elements of one level | |
cf72e2dd | 2153 | * |
2154 | * @param array &$levels The levels of the grade tree through which to recurse | |
2155 | * @param array &$element The seed of the recursion | |
2156 | * @param int $depth How deep are we? | |
7a6b7acf | 2157 | * @return void |
2158 | */ | |
d24832f9 | 2159 | public function fill_levels(&$levels, &$element, $depth) { |
7a6b7acf | 2160 | if (!array_key_exists($depth, $levels)) { |
2161 | $levels[$depth] = array(); | |
2162 | } | |
2163 | ||
2164 | // prepare unique identifier | |
2165 | if ($element['type'] == 'category') { | |
ddb9c95f | 2166 | $element['eid'] = 'cg'.$element['object']->id; |
7a6b7acf | 2167 | } else if (in_array($element['type'], array('item', 'courseitem', 'categoryitem'))) { |
ddb9c95f | 2168 | $element['eid'] = 'ig'.$element['object']->id; |
b89a70ce | 2169 | $this->items[$element['object']->id] =& $element['object']; |
7a6b7acf | 2170 | } |
2171 | ||
2172 | $levels[$depth][] =& $element; | |
2173 | $depth++; | |
2174 | if (empty($element['children'])) { | |
2175 | return; | |
2176 | } | |
2177 | $prev = 0; | |
2178 | foreach ($element['children'] as $sortorder=>$child) { | |
2179 | grade_tree::fill_levels($levels, $element['children'][$sortorder], $depth); | |
2180 | $element['children'][$sortorder]['prev'] = $prev; | |
2181 | $element['children'][$sortorder]['next'] = 0; | |
2182 | if ($prev) { | |
2183 | $element['children'][$prev]['next'] = $sortorder; | |
2184 | } | |
2185 | $prev = $sortorder; | |
2186 | } | |
2187 | } | |
2188 | ||
2189 | /** | |
2190 | * Static recursive helper - makes full tree (all leafes are at the same level) | |
cf72e2dd | 2191 | * |
2192 | * @param array &$element The seed of the recursion | |
2193 | * @param int $depth How deep are we? | |
2194 | * | |
2195 | * @return int | |
7a6b7acf | 2196 | */ |
d24832f9 | 2197 | public function inject_fillers(&$element, $depth) { |
7a6b7acf | 2198 | $depth++; |
2199 | ||
2200 | if (empty($element['children'])) { | |
2201 | return $depth; | |
2202 | } | |
2203 | $chdepths = array(); | |
2204 | $chids = array_keys($element['children']); | |
2205 | $last_child = end($chids); | |
2206 | $first_child = reset($chids); | |
2207 | ||
2208 | foreach ($chids as $chid) { | |
2209 | $chdepths[$chid] = grade_tree::inject_fillers($element['children'][$chid], $depth); | |
2210 | } | |
2211 | arsort($chdepths); | |
2212 | ||
2213 | $maxdepth = reset($chdepths); | |
2214 | foreach ($chdepths as $chid=>$chd) { | |
2215 | if ($chd == $maxdepth) { | |
2216 | continue; | |
2217 | } | |
2218 | for ($i=0; $i < $maxdepth-$chd; $i++) { | |
2219 | if ($chid == $first_child) { | |
2220 | $type = 'fillerfirst'; | |
2221 | } else if ($chid == $last_child) { | |
2222 | $type = 'fillerlast'; | |
2223 | } else { | |
2224 | $type = 'filler'; | |
2225 | } | |
2226 | $oldchild =& $element['children'][$chid]; | |
cf72e2dd | 2227 | $element['children'][$chid] = array('object'=>'filler', 'type'=>$type, |
2228 | 'eid'=>'', 'depth'=>$element['object']->depth, | |
2229 | 'children'=>array($oldchild)); | |
7a6b7acf | 2230 | } |
2231 | } | |
2232 | ||
2233 | return $maxdepth; | |
2234 | } | |
2235 | ||
2236 | /** | |
2237 | * Static recursive helper - add colspan information into categories | |
cf72e2dd | 2238 | * |
2239 | * @param array &$element The seed of the recursion | |
2240 | * | |
2241 | * @return int | |
7a6b7acf | 2242 | */ |
d24832f9 | 2243 | public function inject_colspans(&$element) { |
7a6b7acf | 2244 | if (empty($element['children'])) { |
2245 | return 1; | |
2246 | } | |
2247 | $count = 0; | |
2248 | foreach ($element['children'] as $key=>$child) { | |
2249 | $count += grade_tree::inject_colspans($element['children'][$key]); | |
2250 | } | |
2251 | $element['colspan'] = $count; | |
2252 | return $count; | |
2253 | } | |
2254 | ||
2255 | /** | |
2256 | * Parses the array in search of a given eid and returns a element object with | |
2257 | * information about the element it has found. | |
cf72e2dd | 2258 | * @param int $eid Gradetree Element ID |
7a6b7acf | 2259 | * @return object element |
2260 | */ | |
d24832f9 | 2261 | public function locate_element($eid) { |
d3c3da1b | 2262 | // it is a grade - construct a new object |
2263 | if (strpos($eid, 'n') === 0) { | |
2264 | if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) { | |
2265 | return null; | |
2266 | } | |
2267 | ||
2268 | $itemid = $matches[1]; | |
2269 | $userid = $matches[2]; | |
2270 | ||
2271 | //extra security check - the grade item must be in this tree | |
ddb9c95f | 2272 | if (!$item_el = $this->locate_element('ig'.$itemid)) { |
d3c3da1b | 2273 | return null; |
2274 | } | |
2275 | ||
2276 | // $gradea->id may be null - means does not exist yet | |
2277 | $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid)); | |
2278 | ||
2279 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
2280 | return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade'); | |
2281 | ||
2282 | } else if (strpos($eid, 'g') === 0) { | |
cf72e2dd | 2283 | $id = (int) substr($eid, 1); |
7a6b7acf | 2284 | if (!$grade = grade_grade::fetch(array('id'=>$id))) { |
2285 | return null; | |
2286 | } | |
2287 | //extra security check - the grade item must be in this tree | |
ddb9c95f | 2288 | if (!$item_el = $this->locate_element('ig'.$grade->itemid)) { |
7a6b7acf | 2289 | return null; |
2290 | } | |
2291 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
2292 | return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade'); | |
2293 | } | |
2294 | ||
2295 | // it is a category or item | |
2296 | foreach ($this->levels as $row) { | |
2297 | foreach ($row as $element) { | |
2298 | if ($element['type'] == 'filler') { | |
2299 | continue; | |
2300 | } | |
2301 | if ($element['eid'] == $eid) { | |
2302 | return $element; | |
2303 | } | |
2304 | } | |
2305 | } | |
2306 | ||
2307 | return null; | |
2308 | } | |
d24832f9 | 2309 | |
b244b9b7 | 2310 | /** |
2311 | * Returns a well-formed XML representation of the grade-tree using recursion. | |
cf72e2dd | 2312 | * |
2313 | * @param array $root The current element in the recursion. If null, starts at the top of the tree. | |
2314 | * @param string $tabs The control character to use for tabs | |
2315 | * | |
b244b9b7 | 2316 | * @return string $xml |
2317 | */ | |
cf72e2dd | 2318 | public function exporttoxml($root=null, $tabs="\t") { |
b244b9b7 | 2319 | $xml = null; |
2320 | $first = false; | |
2321 | if (is_null($root)) { | |
2322 | $root = $this->top_element; | |
2323 | $xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n"; | |
2324 | $xml .= "<gradetree>\n"; | |
2325 | $first = true; | |
2326 | } | |
d24832f9 | 2327 | |
b244b9b7 | 2328 | $type = 'undefined'; |
2329 | if (strpos($root['object']->table, 'grade_categories') !== false) { | |
2330 | $type = 'category'; | |
cf72e2dd | 2331 | } else if (strpos($root['object']->table, 'grade_items') !== false) { |
b244b9b7 | 2332 | $type = 'item'; |
cf72e2dd | 2333 | } else if (strpos($root['object']->table, 'grade_outcomes') !== false) { |
b244b9b7 | 2334 | $type = 'outcome'; |
2335 | } | |
d24832f9 | 2336 | |
b244b9b7 | 2337 | $xml .= "$tabs<element type=\"$type\">\n"; |
2338 | foreach ($root['object'] as $var => $value) { | |
2339 | if (!is_object($value) && !is_array($value) && !empty($value)) { | |
2340 | $xml .= "$tabs\t<$var>$value</$var>\n"; | |
2341 | } | |
2342 | } | |
2343 | ||
2344 | if (!empty($root['children'])) { | |
2345 | $xml .= "$tabs\t<children>\n"; | |
2346 | foreach ($root['children'] as $sortorder => $child) { | |
2347 | $xml .= $this->exportToXML($child, $tabs."\t\t"); | |
2348 | } | |
2349 | $xml .= "$tabs\t</children>\n"; | |
2350 | } | |
d24832f9 | 2351 | |
b244b9b7 | 2352 | $xml .= "$tabs</element>\n"; |
2353 | ||
2354 | if ($first) { | |
2355 | $xml .= "</gradetree>"; | |
2356 | } | |
d24832f9 | 2357 | |
b244b9b7 | 2358 | return $xml; |
2359 | } | |
d24832f9 | 2360 | |
b244b9b7 | 2361 | /** |
2362 | * Returns a JSON representation of the grade-tree using recursion. | |
cf72e2dd | 2363 | * |
b244b9b7 | 2364 | * @param array $root The current element in the recursion. If null, starts at the top of the tree. |
2365 | * @param string $tabs Tab characters used to indent the string nicely for humans to enjoy | |
cf72e2dd | 2366 | * |
2367 | * @return string | |
b244b9b7 | 2368 | */ |
cf72e2dd | 2369 | public function exporttojson($root=null, $tabs="\t") { |
b244b9b7 | 2370 | $json = null; |
2371 | $first = false; | |
2372 | if (is_null($root)) { | |
2373 | $root = $this->top_element; | |
2374 | $first = true; | |
2375 | } | |
d24832f9 | 2376 | |
b244b9b7 | 2377 | $name = ''; |
2378 | ||
2379 | ||
2380 | if (strpos($root['object']->table, 'grade_categories') !== false) { | |
2381 | $name = $root['object']->fullname; | |
2382 | if ($name == '?') { | |
d24832f9 | 2383 | $name = $root['object']->get_name(); |
b244b9b7 | 2384 | } |
cf72e2dd | 2385 | } else if (strpos($root['object']->table, 'grade_items') !== false) { |
b244b9b7 | 2386 | $name = $root['object']->itemname; |
cf72e2dd | 2387 | } else if (strpos($root['object']->table, 'grade_outcomes') !== false) { |
b244b9b7 | 2388 | $name = $root['object']->itemname; |
2389 | } | |
d24832f9 | 2390 | |
b244b9b7 | 2391 | $json .= "$tabs {\n"; |
2392 | $json .= "$tabs\t \"type\": \"{$root['type']}\",\n"; | |
2393 | $json .= "$tabs\t \"name\": \"$name\",\n"; | |
2394 | ||
2395 | foreach ($root['object'] as $var => $value) { | |
2396 | if (!is_object($value) && !is_array($value) && !empty($value)) { | |
2397 | $json .= "$tabs\t \"$var\": \"$value\",\n"; | |
2398 | } | |
2399 | } | |
d24832f9 | 2400 | |
b244b9b7 | 2401 | $json = substr($json, 0, strrpos($json, ',')); |
d24832f9 | 2402 | |
b244b9b7 | 2403 | if (!empty($root['children'])) { |
2404 | $json .= ",\n$tabs\t\"children\": [\n"; | |
2405 | foreach ($root['children'] as $sortorder => $child) { | |
2406 | $json .= $this->exportToJSON($child, $tabs."\t\t"); | |
2407 | } | |
2408 | $json = substr($json, 0, strrpos($json, ',')); | |
2409 | $json .= "\n$tabs\t]\n"; | |
d24832f9 | 2410 | } |
b244b9b7 | 2411 | |
2412 | if ($first) { | |
2413 | $json .= "\n}"; | |
2414 | } else { | |
2415 | $json .= "\n$tabs},\n"; | |
2416 | } | |
d24832f9 | 2417 | |
b244b9b7 | 2418 | return $json; |
2419 | } | |
d24832f9 | 2420 | |
cf72e2dd | 2421 | /** |
2422 | * Returns the array of levels | |
2423 | * | |
2424 | * @return array | |
2425 | */ | |
d24832f9 | 2426 | public function get_levels() { |
2427 | return $this->levels; | |
2428 | } | |
2429 | ||
cf72e2dd | 2430 | /** |
2431 | * Returns the array of grade items | |
2432 | * | |
2433 | * @return array | |
2434 | */ | |
d24832f9 | 2435 | public function get_items() { |
2436 | return $this->items; | |
2437 | } | |
2438 | ||
cf72e2dd | 2439 | /** |
2440 | * Returns a specific Grade Item | |
2441 | * | |
2442 | * @param int $itemid The ID of the grade_item object | |
2443 | * | |
2444 | * @return grade_item | |
2445 | */ | |
d24832f9 | 2446 | public function get_item($itemid) { |
2447 | if (array_key_exists($itemid, $this->items)) { | |
2448 | return $this->items[$itemid]; | |
2449 | } else { | |
2450 | return false; | |
2451 | } | |
2452 | } | |
7a6b7acf | 2453 | } |
eef00ade | 2454 | |
2455 | /** | |
2456 | * Local shortcut function for creating an edit/delete button for a grade_* object. | |
4d27bc79 | 2457 | * @param string $type 'edit' or 'delete' |
eef00ade | 2458 | * @param int $courseid The Course ID |
2459 | * @param grade_* $object The grade_* object | |
2460 | * @return string html | |
2461 | */ | |
2462 | function grade_button($type, $courseid, $object) { | |
2463 | global $CFG, $OUTPUT; | |
2464 | if (preg_match('/grade_(.*)/', get_class($object), $matches)) { | |
2465 | $objectidstring = $matches[1] . 'id'; | |
2466 | } else { | |
2467 | throw new coding_exception('grade_button() only accepts grade_* objects as third parameter!'); | |
2468 | } | |
2469 | ||
2470 | $strdelete = get_string('delete'); | |
2471 | $stredit = get_string('edit'); | |
2472 | ||
eef00ade | 2473 | if ($type == 'delete') { |
8ae8bf8a | 2474 | $url = new moodle_url('index.php', array('id' => $courseid, $objectidstring => $object->id, 'action' => 'delete', 'sesskey' => sesskey())); |
eef00ade | 2475 | } else if ($type == 'edit') { |
8ae8bf8a | 2476 | $url = new moodle_url('edit.php', array('courseid' => $courseid, 'id' => $object->id)); |
eef00ade | 2477 | } |
2478 | ||
fa9c0aab | 2479 | return $OUTPUT->action_icon($url, new pix_icon('t/'.$type, ${'str'.$type}, '', array('class' => 'iconsmall'))); |
eef00ade | 2480 | |
2481 | } | |
4d5059d4 SH |
2482 | |
2483 | /** | |
2484 | * This method adds settings to the settings block for the grade system and its | |
2485 | * plugins | |
2486 | * | |
2487 | * @global moodle_page $PAGE | |
2488 | */ | |
2489 | function grade_extend_settings($plugininfo, $courseid) { | |
2490 | global $PAGE; | |
2491 | ||
2492 | $gradenode = $PAGE->settingsnav->prepend(get_string('gradeadministration', 'grades'), null, navigation_node::TYPE_CONTAINER); | |
2493 | ||
2494 | $strings = array_shift($plugininfo); | |
2495 | ||
2496 | if ($reports = grade_helper::get_plugins_reports($courseid)) { | |
2497 | foreach ($reports as $report) { | |
2498 | $gradenode->add($report->string, $report->link, navigation_node::TYPE_SETTING, null, $report->id, new pix_icon('i/report', '')); | |
2499 | } | |
2500 | } | |
2501 | ||
eb84b779 MG |
2502 | if ($settings = grade_helper::get_info_manage_settings($courseid)) { |
2503 | $settingsnode = $gradenode->add($strings['settings'], null, navigation_node::TYPE_CONTAINER); | |
2504 | foreach ($settings as $setting) { | |
2505 | $settingsnode->add($setting->string, $setting->link, navigation_node::TYPE_SETTING, null, $setting->id, new pix_icon('i/settings', '')); | |
2506 | } | |
2507 | } | |
2508 | ||
4d5059d4 SH |
2509 | if ($imports = grade_helper::get_plugins_import($courseid)) { |
2510 | $importnode = $gradenode->add($strings['import'], null, navigation_node::TYPE_CONTAINER); | |
2511 | foreach ($imports as $import) { | |
11f87187 | 2512 | $importnode->add($import->string, $import->link, navigation_node::TYPE_SETTING, null, $import->id, new pix_icon('i/import', '')); |
4d5059d4 SH |
2513 | } |
2514 | } | |
2515 | ||
2516 | if ($exports = grade_helper::get_plugins_export($courseid)) { | |
2517 | $exportnode = $gradenode->add($strings['export'], null, navigation_node::TYPE_CONTAINER); | |
2518 | foreach ($exports as $export) { | |
11f87187 | 2519 | $exportnode->add($export->string, $export->link, navigation_node::TYPE_SETTING, null, $export->id, new pix_icon('i/export', '')); |
4d5059d4 SH |
2520 | } |
2521 | } | |
2522 | ||
4d5059d4 SH |
2523 | if ($letters = grade_helper::get_info_letters($courseid)) { |
2524 | $letters = array_shift($letters); | |
2525 | $gradenode->add($strings['letter'], $letters->link, navigation_node::TYPE_SETTING, null, $letters->id, new pix_icon('i/settings', '')); | |
2526 | } | |
2527 | ||
2528 | if ($outcomes = grade_helper::get_info_outcomes($courseid)) { | |
2529 | $outcomes = array_shift($outcomes); | |
2530 | $gradenode->add($strings['outcome'], $outcomes->link, navigation_node::TYPE_SETTING, null, $outcomes->id, new pix_icon('i/outcomes', '')); | |
2531 | } | |
2532 | ||
2533 | if ($scales = grade_helper::get_info_scales($courseid)) { | |
2534 | $gradenode->add($strings['scale'], $scales->link, navigation_node::TYPE_SETTING, null, $scales->id, new pix_icon('i/scales', '')); | |
2535 | } | |
2536 | ||
4d5059d4 SH |
2537 | if ($gradenode->contains_active_node()) { |
2538 | // If the gradenode is active include the settings base node (gradeadministration) in | |
2539 | // the navbar, typcially this is ignored. | |
2540 | $PAGE->navbar->includesettingsbase = true; | |
2541 | ||
2542 | // If we can get the course admin node make sure it is closed by default | |
2543 | // as in this case the gradenode will be opened | |
2544 | if ($coursenode = $PAGE->settingsnav->get('courseadmin', navigation_node::TYPE_COURSE)){ | |
2545 | $coursenode->make_inactive(); | |
2546 | $coursenode->forceopen = false; | |
2547 | } | |
2548 | } | |
2549 | } | |
2550 | ||
2551 | /** | |
2552 | * Grade helper class | |
2553 | * | |
2554 | * This class provides several helpful functions that work irrespective of any | |
2555 | * current state. | |
2556 | * | |
2557 | * @copyright 2010 Sam Hemelryk | |
2558 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2559 | */ | |
2560 | abstract class grade_helper { | |
2561 | /** | |
2562 | * Cached manage settings info {@see get_info_settings} | |
2563 | * @var grade_plugin_info|false | |
2564 | */ | |
2565 | protected static $managesetting = null; | |
2566 | /** | |
2567 | * Cached grade report plugins {@see get_plugins_reports} | |
2568 | * @var array|false | |
2569 | */ | |
2570 | protected static $gradereports = null; | |
2571 | /** | |
2572 | * Cached grade report plugins preferences {@see get_info_scales} | |
2573 | * @var array|false | |
2574 | */ | |
2575 | protected static $gradereportpreferences = null; | |
2576 | /** | |
2577 | * Cached scale info {@see get_info_scales} | |
2578 | * @var grade_plugin_info|false | |
2579 | */ | |
2580 | protected static $scaleinfo = null; | |
2581 | /** | |
2582 | * Cached outcome info {@see get_info_outcomes} | |
2583 | * @var grade_plugin_info|false | |
2584 | */ | |
2585 | protected static $outcomeinfo = null; | |
4d5059d4 SH |
2586 | /** |
2587 | * Cached leftter info {@see get_info_letters} | |
2588 | * @var grade_plugin_info|false | |
2589 | */ | |
2590 | protected static $letterinfo = null; | |
2591 | /** | |
2592 | * Cached grade import plugins {@see get_plugins_import} | |
2593 | * @var array|false | |
2594 | */ | |
2595 | protected static $importplugins = null; | |
2596 | /** | |
2597 | * Cached grade export plugins {@see get_plugins_export} | |
2598 | * @var array|false | |
2599 | */ | |
2600 | protected static $exportplugins = null; | |
2601 | /** | |
2602 | * Cached grade plugin strings | |
2603 | * @var array | |
2604 | */ | |
2605 | protected static $pluginstrings = null; | |
bfe969e8 DW |
2606 | /** |
2607 | * Cached grade aggregation strings | |
2608 | * @var array | |
2609 | */ | |
2610 | protected static $aggregationstrings = null; | |
4d5059d4 SH |
2611 | |
2612 | /** | |
2613 | * Gets strings commonly used by the describe plugins | |
2614 | * | |
2615 | * report => get_string('view'), | |
4d5059d4 SH |
2616 | * scale => get_string('scales'), |
2617 | * outcome => get_string('outcomes', 'grades'), | |
2618 | * letter => get_string('letters', 'grades'), | |
2619 | * export => get_string('export', 'grades'), | |
2620 | * import => get_string('import'), | |
2621 | * preferences => get_string('mypreferences', 'grades'), | |
2622 | * settings => get_string('settings') | |
2623 | * | |
2624 | * @return array | |
2625 | */ | |
2626 | public static function get_plugin_strings() { | |
2627 | if (self::$pluginstrings === null) { | |
2628 | self::$pluginstrings = array( | |
2629 | 'report' => get_string('view'), | |
4d5059d4 SH |
2630 | 'scale' => get_string('scales'), |
2631 | 'outcome' => get_string('outcomes', 'grades'), | |
2632 | 'letter' => get_string('letters', 'grades'), | |
2633 | 'export' => get_string('export', 'grades'), | |
2634 | 'import' => get_string('import'), | |
eb84b779 | 2635 | 'settings' => get_string('edittree', 'grades') |
4d5059d4 SH |
2636 | ); |
2637 | } | |
2638 | return self::$pluginstrings; | |
2639 | } | |
bfe969e8 DW |
2640 | |
2641 | /** | |
2642 | * Gets strings describing the available aggregation methods. | |
2643 | * | |
2644 | * @return array | |
2645 | */ | |
2646 | public static function get_aggregation_strings() { | |
2647 | if (self::$aggregationstrings === null) { | |
2648 | self::$aggregationstrings = array( | |
2649 | GRADE_AGGREGATE_MEAN => get_string('aggregatemean', 'grades'), | |
2650 | GRADE_AGGREGATE_WEIGHTED_MEAN => get_string('aggregateweightedmean', 'grades'), | |
2651 | GRADE_AGGREGATE_WEIGHTED_MEAN2 => get_string('aggregateweightedmean2', 'grades'), | |
2652 | GRADE_AGGREGATE_EXTRACREDIT_MEAN => get_string('aggregateextracreditmean', 'grades'), | |
2653 | GRADE_AGGREGATE_MEDIAN => get_string('aggregatemedian', 'grades'), | |
2654 | GRADE_AGGREGATE_MIN => get_string('aggregatemin', 'grades'), | |
2655 | GRADE_AGGREGATE_MAX => get_string('aggregatemax', 'grades'), | |
2656 | GRADE_AGGREGATE_MODE => get_string('aggregatemode', 'grades'), | |
2657 | GRADE_AGGREGATE_SUM => get_string('aggregatesum', 'grades') | |
2658 | ); | |
2659 | } | |
2660 | return self::$aggregationstrings; | |
2661 | } | |
2662 | ||
4d5059d4 SH |
2663 | /** |
2664 | * Get grade_plugin_info object for managing settings if the user can | |
2665 | * | |
2666 | * @param int $courseid | |
8faa560c | 2667 | * @return grade_plugin_info[] |
4d5059d4 SH |
2668 | */ |
2669 | public static function get_info_manage_settings($courseid) { | |
2670 | if (self::$managesetting !== null) { | |
2671 | return self::$managesetting; | |
2672 | } | |
d4060472 | 2673 | $context = context_course::instance($courseid); |
8faa560c MG |
2674 | self::$managesetting = array(); |
2675 | if ($courseid != SITEID && has_capability('moodle/grade:manage', $context)) { | |
eb84b779 MG |
2676 | self::$managesetting['categoriesanditems'] = new grade_plugin_info('setup', |
2677 | new moodle_url('/grade/edit/tree/index.php', array('id' => $courseid)), | |
2678 | get_string('categoriesanditems', 'grades')); | |
8faa560c MG |
2679 | self::$managesetting['coursesettings'] = new grade_plugin_info('coursesettings', |
2680 | new moodle_url('/grade/edit/settings/index.php', array('id'=>$courseid)), | |
2681 | get_string('coursegradesettings', 'grades')); | |
eb84b779 MG |
2682 | } |
2683 | if (self::$gradereportpreferences === null) { | |
2684 | self::get_plugins_reports($courseid); | |
2685 | } | |
2686 | if (self::$gradereportpreferences) { | |
2687 | self::$managesetting = array_merge(self::$managesetting, self::$gradereportpreferences); | |
4d5059d4 SH |
2688 | } |
2689 | return self::$managesetting; | |
2690 | } | |
2691 | /** | |
2692 | * Returns an array of plugin reports as grade_plugin_info objects | |
2693 | * | |
2694 | * @param int $courseid | |
2695 | * @return array | |
2696 | */ | |
2697 | public static function get_plugins_reports($courseid) { | |
f7fcf4cd | 2698 | global $SITE; |
cf717dc2 | 2699 | |
4d5059d4 SH |
2700 | if (self::$gradereports !== null) { |
2701 | return self::$gradereports; | |
2702 | } | |
d4060472 | 2703 | $context = context_course::instance($courseid); |
4d5059d4 SH |
2704 | $gradereports = array(); |
2705 | $gradepreferences = array(); | |
bd3b3bba | 2706 | foreach (core_component::get_plugin_list('gradereport') as $plugin => $plugindir) { |
f7fcf4cd AD |
2707 | //some reports make no sense if we're not within a course |
2708 | if ($courseid==$SITE->id && ($plugin=='grader' || $plugin=='user')) { | |
2709 | continue; | |
2710 | } | |
2711 | ||
4d5059d4 SH |
2712 | // Remove ones we can't see |
2713 | if (!has_capability('gradereport/'.$plugin.':view', $context)) { | |
2714 | continue; | |
2715 | } | |
2716 | ||
b5e7b2bf | 2717 | $pluginstr = get_string('pluginname', 'gradereport_'.$plugin); |
4d5059d4 SH |
2718 | $url = new moodle_url('/grade/report/'.$plugin.'/index.php', array('id'=>$courseid)); |
2719 | $gradereports[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr); | |
2720 | ||
2721 | // Add link to preferences tab if such a page exists | |
2722 | if (file_exists($plugindir.'/preferences.php')) { | |
2723 | $url = new moodle_url('/grade/report/'.$plugin.'/preferences.php', array('id'=>$courseid)); | |
eb84b779 MG |
2724 | $gradepreferences[$plugin] = new grade_plugin_info($plugin, $url, |
2725 | get_string('mypreferences', 'grades') . ': ' . $pluginstr); | |
4d5059d4 SH |
2726 | } |
2727 | } | |
2728 | if (count($gradereports) == 0) { | |
2729 | $gradereports = false; | |
2730 | $gradepreferences = false; | |
2731 | } else if (count($gradepreferences) == 0) { | |
2732 | $gradepreferences = false; | |
2733 | asort($gradereports); | |
2734 | } else { | |
2735 | asort($gradereports); | |
2736 | asort($gradepreferences); | |
2737 | } | |
2738 | self::$gradereports = $gradereports; | |
2739 | self::$gradereportpreferences = $gradepreferences; | |
2740 | return self::$gradereports; | |
2741 | } | |
eb84b779 | 2742 | |
4d5059d4 SH |
2743 | /** |
2744 | * Get information on scales | |
2745 | * @param int $courseid | |
2746 | * @return grade_plugin_info | |
2747 | */ | |
2748 | public static function get_info_scales($courseid) { | |
2749 | if (self::$scaleinfo !== null) { | |
2750 | return self::$scaleinfo; | |
2751 | } | |
d4060472 | 2752 | if (has_capability('moodle/course:managescales', context_course::instance($courseid))) { |
4d5059d4 SH |
2753 | $url = new moodle_url('/grade/edit/scale/index.php', array('id'=>$courseid)); |
2754 | self::$scaleinfo = new grade_plugin_info('scale', $url, get_string('view')); | |
2755 | } else { | |
2756 | self::$scaleinfo = false; | |
2757 | } | |
2758 | return self::$scaleinfo; | |
2759 | } | |
2760 | /** | |
2761 | * Get information on outcomes | |
2762 | * @param int $courseid | |
2763 | * @return grade_plugin_info | |
2764 | */ | |
2765 | public static function get_info_outcomes($courseid) { | |
f7fcf4cd | 2766 | global $CFG, $SITE; |
4d5059d4 SH |
2767 | |
2768 | if (self::$outcomeinfo !== null) { | |
2769 | return self::$outcomeinfo; | |
2770 | } | |
d4060472 | 2771 | $context = context_course::instance($courseid); |
4d5059d4 SH |
2772 | $canmanage = has_capability('moodle/grade:manage', $context); |
2773 | $canupdate = has_capability('moodle/course:update', $context); | |
2774 | if (!empty($CFG->enableoutcomes) && ($canmanage || $canupdate)) { | |
2775 | $outcomes = array(); | |
2776 | if ($canupdate) { | |
f7fcf4cd AD |
2777 | if ($courseid!=$SITE->id) { |
2778 | $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid)); | |
2779 | $outcomes['course'] = new grade_plugin_info('course', $url, get_string('outcomescourse', 'grades')); | |
2780 | } | |
4d5059d4 SH |
2781 | $url = new moodle_url('/grade/edit/outcome/index.php', array('id'=>$courseid)); |
2782 | $outcomes['edit'] = new grade_plugin_info('edit', $url, get_string('editoutcomes', 'grades')); | |
c46aeeab DC |
2783 | $url = new moodle_url('/grade/edit/outcome/import.php', array('courseid'=>$courseid)); |
2784 | $outcomes['import'] = new grade_plugin_info('import', $url, get_string('importoutcomes', 'grades')); | |
4d5059d4 | 2785 | } else { |
f7fcf4cd AD |
2786 | if ($courseid!=$SITE->id) { |
2787 | $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid)); | |
2788 | $outcomes['edit'] = new grade_plugin_info('edit', $url, get_string('outcomescourse', 'grades')); | |
2789 | } | |
4d5059d4 SH |
2790 | } |
2791 | self::$outcomeinfo = $outcomes; | |
2792 | } else { | |
2793 | self::$outcomeinfo = false; | |
2794 | } | |
2795 | return self::$outcomeinfo; | |
2796 | } | |
4d5059d4 SH |
2797 | /** |
2798 | * Get information on letters | |
2799 | * @param int $courseid | |
4d27bc79 | 2800 | * @return array |
4d5059d4 SH |
2801 | */ |
2802 | public static function get_info_letters($courseid) { | |
a8dfc483 | 2803 | global $SITE; |
4d5059d4 SH |
2804 | if (self::$letterinfo !== null) { |
2805 | return self::$letterinfo; | |
2806 | } | |
d4060472 | 2807 | $context = context_course::instance($courseid); |
4d5059d4 SH |
2808 | $canmanage = has_capability('moodle/grade:manage', $context); |
2809 | $canmanageletters = has_capability('moodle/grade:manageletters', $context); | |
2810 | if ($canmanage || $canmanageletters) { | |
a8dfc483 AA |
2811 | // Redirect to system context when report is accessed from admin settings MDL-31633 |
2812 | if ($context->instanceid == $SITE->id) { | |
2813 | $param = array('edit' => 1); | |
2814 | } else { | |
2815 | $param = array('edit' => 1,'id' => $context->id); | |
2816 | } | |
4d5059d4 | 2817 | self::$letterinfo = array( |
54caa598 | 2818 | 'view' => new grade_plugin_info('view', new moodle_url('/grade/edit/letter/index.php', array('id'=>$context->id)), get_string('view')), |
a8dfc483 | 2819 | 'edit' => new grade_plugin_info('edit', new moodle_url('/grade/edit/letter/index.php', $param), get_string('edit')) |
4d5059d4 SH |
2820 | ); |
2821 | } else { | |
2822 | self::$letterinfo = false; | |
2823 | } | |
2824 | return self::$letterinfo; | |
2825 | } | |
2826 | /** | |
2827 | * Get information import plugins | |
2828 | * @param int $courseid | |
2829 | * @return array | |
2830 | */ | |
2831 | public static function get_plugins_import($courseid) { | |
2832 | global $CFG; | |
2833 | ||
2834 | if (self::$importplugins !== null) { | |
2835 | return self::$importplugins; | |
2836 | } | |
2837 | $importplugins = array(); | |
d4060472 | 2838 | $context = context_course::instance($courseid); |
4d5059d4 SH |
2839 | |
2840 | if (has_capability('moodle/grade:import', $context)) { | |
bd3b3bba | 2841 | foreach (core_component::get_plugin_list('gradeimport') as $plugin => $plugindir) { |
4d5059d4 SH |
2842 | if (!has_capability('gradeimport/'.$plugin.':view', $context)) { |
2843 | continue; | |
2844 | } | |
b5e7b2bf | 2845 | $pluginstr = get_string('pluginname', 'gradeimport_'.$plugin); |
4d5059d4 SH |
2846 | $url = new moodle_url('/grade/import/'.$plugin.'/index.php', array('id'=>$courseid)); |
2847 | $importplugins[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr); | |
2848 | } | |
2849 | ||
ead4d41e SL |
2850 | // Show key manager if grade publishing is enabled and the user has xml publishing capability. |
2851 | // XML is the only grade import plugin that has publishing feature. | |
2852 | if ($CFG->gradepublishing && has_capability('gradeimport/xml:publish', $context)) { | |
4d5059d4 SH |
2853 | $url = new moodle_url('/grade/import/keymanager.php', array('id'=>$courseid)); |
2854 | $importplugins['keymanager'] = new grade_plugin_info('keymanager', $url, get_string('keymanager', 'grades')); | |
2855 | } | |
2856 | } | |
2857 | ||
2858 | if (count($importplugins) > 0) { | |
2859 | asort($importplugins); | |
2860 | self::$importplugins = $importplugins; | |
2861 | } else { | |
2862 | self::$importplugins = false; | |
2863 | } | |
2864 | return self::$importplugins; | |
2865 | } | |
2866 | /** | |
2867 | * Get information export plugins | |
2868 | * @param int $courseid | |
2869 | * @return array | |
2870 | */ | |
2871 | public static function get_plugins_export($courseid) { | |
2872 | global $CFG; | |
2873 | ||
2874 | if (self::$exportplugins !== null) { | |
2875 | return self::$exportplugins; | |
2876 | } | |
d4060472 | 2877 | $context = context_course::instance($courseid); |
4d5059d4 | 2878 | $exportplugins = array(); |
ead4d41e | 2879 | $canpublishgrades = 0; |
4d5059d4 | 2880 | if (has_capability('moodle/grade:export', $context)) { |
bd3b3bba | 2881 | foreach (core_component::get_plugin_list('gradeexport') as $plugin => $plugindir) { |
4d5059d4 SH |
2882 | if (!has_capability('gradeexport/'.$plugin.':view', $context)) { |
2883 | continue; | |
2884 | } | |
ead4d41e SL |
2885 | // All the grade export plugins has grade publishing capabilities. |
2886 | if (has_capability('gradeexport/'.$plugin.':publish', $context)) { | |
2887 | $canpublishgrades++; | |
2888 | } | |
2889 | ||
b5e7b2bf | 2890 | $pluginstr = get_string('pluginname', 'gradeexport_'.$plugin); |
4d5059d4 SH |
2891 | $url = new moodle_url('/grade/export/'.$plugin.'/index.php', array('id'=>$courseid)); |
2892 | $exportplugins[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr); | |
2893 | } | |
2894 | ||
ead4d41e SL |
2895 | // Show key manager if grade publishing is enabled and the user has at least one grade publishing capability. |
2896 | if ($CFG->gradepublishing && $canpublishgrades != 0) { | |
4d5059d4 SH |
2897 | $url = new moodle_url('/grade/export/keymanager.php', array('id'=>$courseid)); |
2898 | $exportplugins['keymanager'] = new grade_plugin_info('keymanager', $url, get_string('keymanager', 'grades')); | |
2899 | } | |
2900 | } | |
2901 | if (count($exportplugins) > 0) { | |
2902 | asort($exportplugins); | |
2903 | self::$exportplugins = $exportplugins; | |
2904 | } else { | |
2905 | self::$exportplugins = false; | |
2906 | } | |
2907 | return self::$exportplugins; | |
2908 | } | |
293f42b6 | 2909 | |
61c8e0d7 FM |
2910 | /** |
2911 | * Returns the value of a field from a user record | |
2912 | * | |
2913 | * @param stdClass $user object | |
2914 | * @param stdClass $field object | |
2915 | * @return string value of the field | |
2916 | */ | |
2917 | public static function get_user_field_value($user, $field) { | |
2918 | if (!empty($field->customid)) { | |
2919 | $fieldname = 'customfield_' . $field->shortname; | |
2920 | if (!empty($user->{$fieldname}) || is_numeric($user->{$fieldname})) { | |
2921 | $fieldvalue = $user->{$fieldname}; | |
2922 | } else { | |
2923 | $fieldvalue = $field->default; | |
2924 | } | |
2925 | } else { | |
2926 | $fieldvalue = $user->{$field->shortname}; | |
2927 | } | |
2928 | return $fieldvalue; | |
2929 | } | |
2930 | ||
2931 | /** | |
2932 | * Returns an array of user profile fields to be included in export | |
2933 | * | |
2934 | * @param int $courseid | |
2935 | * @param bool $includecustomfields | |
2936 | * @return array An array of stdClass instances with customid, shortname, datatype, default and fullname fields | |
2937 | */ | |
2938 | public static function get_user_profile_fields($courseid, $includecustomfields = false) { | |
2939 | global $CFG, $DB; | |
2940 | ||
2941 | // Gets the fields that have to be hidden | |
2942 | $hiddenfields = array_map('trim', explode(',', $CFG->hiddenuserfields)); | |
2943 | $context = context_course::instance($courseid); | |
2944 | $canseehiddenfields = has_capability('moodle/course:viewhiddenuserfields', $context); | |
2945 | if ($canseehiddenfields) { | |
2946 | $hiddenfields = array(); | |
2947 | } | |
2948 | ||
2949 | $fields = array(); | |
2950 | require_once($CFG->dirroot.'/user/lib.php'); // Loads user_get_default_fields() | |
2951 | require_once($CFG->dirroot.'/user/profile/lib.php'); // Loads constants, such as PROFILE_VISIBLE_ALL | |
2952 | $userdefaultfields = user_get_default_fields(); | |
2953 | ||
2954 | // Sets the list of profile fields | |
2955 | $userprofilefields = array_map('trim', explode(',', $CFG->grade_export_userprofilefields)); | |
2956 | if (!empty($userprofilefields)) { | |
2957 | foreach ($userprofilefields as $field) { | |
2958 | $field = trim($field); | |
2959 | if (in_array($field, $hiddenfields) || !in_array($field, $userdefaultfields)) { | |
2960 | continue; | |
2961 | } | |
293f42b6 | 2962 | $obj = new stdClass(); |
61c8e0d7 | 2963 | $obj->customid = 0; |
293f42b6 | 2964 | $obj->shortname = $field; |
61c8e0d7 | 2965 | $obj->fullname = get_string($field); |
293f42b6 CF |
2966 | $fields[] = $obj; |
2967 | } | |
2968 | } | |
293f42b6 | 2969 | |
61c8e0d7 FM |
2970 | // Sets the list of custom profile fields |
2971 | $customprofilefields = array_map('trim', explode(',', $CFG->grade_export_customprofilefields)); | |
2972 | if ($includecustomfields && !empty($customprofilefields)) { | |
2973 | list($wherefields, $whereparams) = $DB->get_in_or_equal($customprofilefields); | |
2974 | $customfields = $DB->get_records_sql("SELECT f.* | |
2975 | FROM {user_info_field} f | |
2976 | JOIN {user_info_category} c ON f.categoryid=c.id | |
2977 | WHERE f.shortname $wherefields | |
2978 | ORDER BY c.sortorder ASC, f.sortorder ASC", $whereparams); | |
2979 | if (!is_array($customfields)) { | |
2980 | continue; | |
2981 | } | |
2982 | ||
2983 | foreach ($customfields as $field) { | |
2984 | // Make sure we can display this custom field | |
2985 | if (!in_array($field->shortname, $customprofilefields)) { | |
2986 | continue; | |
2987 | } else if (in_array($field->shortname, $hiddenfields)) { | |
2988 | continue; | |
2989 | } else if ($field->visible != PROFILE_VISIBLE_ALL && !$canseehiddenfields) { | |
2990 | continue; | |
2991 | } | |
2992 | ||
2993 | $obj = new stdClass(); | |
2994 | $obj->customid = $field->id; | |
2995 | $obj->shortname = $field->shortname; | |
2996 | $obj->fullname = format_string($field->name); | |
2997 | $obj->datatype = $field->datatype; | |
2998 | $obj->default = $field->defaultdata; | |
2999 | $fields[] = $obj; | |
3000 | } | |
3001 | } | |
3002 | ||
3003 | return $fields; | |
3004 | } | |
e61f25a4 DW |
3005 | |
3006 | /** | |
3007 | * This helper method gets a snapshot of all the weights for a course. | |
3008 | * It is used as a quick method to see if any wieghts have been automatically adjusted. | |
3009 | * @param int $courseid | |
3010 | * @return array of itemid -> aggregationcoef2 | |
3011 | */ | |
3012 | public static function fetch_all_natural_weights_for_course($courseid) { | |
3013 | global $DB; | |
3014 | $result = array(); | |
3015 | ||
3016 | $records = $DB->get_records('grade_items', array('courseid'=>$courseid), 'id', 'id, aggregationcoef2'); | |
3017 | foreach ($records as $record) { | |
3018 | $result[$record->id] = $record->aggregationcoef2; | |
3019 | } | |
3020 | return $result; | |
3021 | } | |
293f42b6 | 3022 | } |
61c8e0d7 | 3023 |