Merge branch 'MDL-26914-lang-grand-parents' of git://github.com/mudrd8mz/moodle
[moodle.git] / grade / lib.php
CommitLineData
cf72e2dd 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
cf72e2dd 18/**
19 * Functions used by gradebook plugins and reports.
20 *
0287fe7e 21 * @package moodlecore
cf72e2dd 22 * @copyright 2009 Petr Skoda and Nicolas Connault
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
8ad36f4c 25
7a6b7acf 26require_once $CFG->libdir.'/gradelib.php';
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 */
35class graded_users_iterator {
d24832f9 36 public $course;
37 public $grade_items;
38 public $groupid;
39 public $users_rs;
40 public $grades_rs;
41 public $gradestack;
42 public $sortfield1;
43 public $sortorder1;
44 public $sortfield2;
45 public $sortorder2;
0f5660f7 46
47 /**
48 * Constructor
cf72e2dd 49 *
50 * @param object $course A course object
51 * @param array $grade_items array of grade items, if not specified only user info returned
52 * @param int $groupid iterate only group users if present
d08bba83 53 * @param string $sortfield1 The first field of the users table by which the array of users will be sorted
54 * @param string $sortorder1 The order in which the first sorting field will be sorted (ASC or DESC)
55 * @param string $sortfield2 The second field of the users table by which the array of users will be sorted
56 * @param string $sortorder2 The order in which the second sorting field will be sorted (ASC or DESC)
0f5660f7 57 */
cf72e2dd 58 public function graded_users_iterator($course, $grade_items=null, $groupid=0,
59 $sortfield1='lastname', $sortorder1='ASC',
60 $sortfield2='firstname', $sortorder2='ASC') {
0f5660f7 61 $this->course = $course;
62 $this->grade_items = $grade_items;
63 $this->groupid = $groupid;
d08bba83 64 $this->sortfield1 = $sortfield1;
65 $this->sortorder1 = $sortorder1;
66 $this->sortfield2 = $sortfield2;
67 $this->sortorder2 = $sortorder2;
0f5660f7 68
69 $this->gradestack = array();
70 }
71
72 /**
73 * Initialise the iterator
74 * @return boolean success
75 */
d24832f9 76 public function init() {
77 global $CFG, $DB;
0f5660f7 78
79 $this->close();
80
81 grade_regrade_final_grades($this->course->id);
82 $course_item = grade_item::fetch_course_item($this->course->id);
83 if ($course_item->needsupdate) {
84 // can not calculate all final grades - sorry
85 return false;
86 }
87
cf72e2dd 88 list($gradebookroles_sql, $params) =
89 $DB->get_in_or_equal(explode(',', $CFG->gradebookroles), SQL_PARAMS_NAMED, 'grbr0');
0f5660f7 90
91 $relatedcontexts = get_related_contexts_string(get_context_instance(CONTEXT_COURSE, $this->course->id));
92
93 if ($this->groupid) {
d24832f9 94 $groupsql = "INNER JOIN {groups_members} gm ON gm.userid = u.id";
e58fcb35 95 $groupwheresql = "AND gm.groupid = :groupid";
d24832f9 96 // $params contents: gradebookroles
e58fcb35 97 $params['groupid'] = $this->groupid;
0f5660f7 98 } else {
99 $groupsql = "";
100 $groupwheresql = "";
101 }
102
345674ca 103 if (empty($this->sortfield1)) {
104 // we must do some sorting even if not specified
105 $ofields = ", u.id AS usrt";
106 $order = "usrt ASC";
107
108 } else {
109 $ofields = ", u.$this->sortfield1 AS usrt1";
110 $order = "usrt1 $this->sortorder1";
111 if (!empty($this->sortfield2)) {
0febb12d 112 $ofields .= ", u.$this->sortfield2 AS usrt2";
345674ca 113 $order .= ", usrt2 $this->sortorder2";
114 }
115 if ($this->sortfield1 != 'id' and $this->sortfield2 != 'id') {
cf72e2dd 116 // user order MUST be the same in both queries,
117 // must include the only unique user->id if not already present
345674ca 118 $ofields .= ", u.id AS usrt";
119 $order .= ", usrt ASC";
120 }
121 }
122
d24832f9 123 // $params contents: gradebookroles and groupid (for $groupwheresql)
345674ca 124 $users_sql = "SELECT u.* $ofields
d24832f9 125 FROM {user} u
126 INNER JOIN {role_assignments} ra ON u.id = ra.userid
0f5660f7 127 $groupsql
b593bf4d
AD
128 WHERE u.deleted=0
129 AND ra.roleid $gradebookroles_sql
0f5660f7 130 AND ra.contextid $relatedcontexts
345674ca 131 $groupwheresql
132 ORDER BY $order";
d08bba83 133
d24832f9 134 $this->users_rs = $DB->get_recordset_sql($users_sql, $params);
0f5660f7 135
136 if (!empty($this->grade_items)) {
137 $itemids = array_keys($this->grade_items);
e58fcb35 138 list($itemidsql, $grades_params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED, 'items0');
d24832f9 139 $params = array_merge($params, $grades_params);
0f5660f7 140
d24832f9 141 // $params contents: gradebookroles, groupid (for $groupwheresql) and itemids
345674ca 142 $grades_sql = "SELECT g.* $ofields
d24832f9 143 FROM {grade_grades} g
144 INNER JOIN {user} u ON g.userid = u.id
145 INNER JOIN {role_assignments} ra ON u.id = ra.userid
0f5660f7 146 $groupsql
d24832f9 147 WHERE ra.roleid $gradebookroles_sql
0f5660f7 148 AND ra.contextid $relatedcontexts
0f5660f7 149 $groupwheresql
d24832f9 150 AND g.itemid $itemidsql
345674ca 151 ORDER BY $order, g.itemid ASC";
d24832f9 152 $this->grades_rs = $DB->get_recordset_sql($grades_sql, $params);
345674ca 153 } else {
154 $this->grades_rs = false;
0f5660f7 155 }
345674ca 156
0f5660f7 157 return true;
158 }
159
160 /**
161 * Returns information about the next user
162 * @return mixed array of user info, all grades and feedback or null when no more users found
163 */
164 function next_user() {
03cedd62 165 if (!$this->users_rs) {
0f5660f7 166 return false; // no users present
167 }
168
5c75a0a3 169 if (!$this->users_rs->valid()) {
345674ca 170 if ($current = $this->_pop()) {
171 // this is not good - user or grades updated between the two reads above :-(
172 }
173
0f5660f7 174 return false; // no more users
5c75a0a3 175 } else {
176 $user = $this->users_rs->current();
177 $this->users_rs->next();
0f5660f7 178 }
179
345674ca 180 // find grades of this user
0f5660f7 181 $grade_records = array();
182 while (true) {
183 if (!$current = $this->_pop()) {
184 break; // no more grades
185 }
186
5c75a0a3 187 if (empty($current->userid)) {
188 break;
189 }
190
345674ca 191 if ($current->userid != $user->id) {
192 // grade of the next user, we have all for this user
0f5660f7 193 $this->_push($current);
194 break;
195 }
196
197 $grade_records[$current->itemid] = $current;
198 }
199
200 $grades = array();
201 $feedbacks = array();
202
d08bba83 203 if (!empty($this->grade_items)) {
345674ca 204 foreach ($this->grade_items as $grade_item) {
205 if (array_key_exists($grade_item->id, $grade_records)) {
206 $feedbacks[$grade_item->id]->feedback = $grade_records[$grade_item->id]->feedback;
207 $feedbacks[$grade_item->id]->feedbackformat = $grade_records[$grade_item->id]->feedbackformat;
208 unset($grade_records[$grade_item->id]->feedback);
209 unset($grade_records[$grade_item->id]->feedbackformat);
210 $grades[$grade_item->id] = new grade_grade($grade_records[$grade_item->id], false);
211 } else {
212 $feedbacks[$grade_item->id]->feedback = '';
213 $feedbacks[$grade_item->id]->feedbackformat = FORMAT_MOODLE;
cf72e2dd 214 $grades[$grade_item->id] =
215 new grade_grade(array('userid'=>$user->id, 'itemid'=>$grade_item->id), false);
345674ca 216 }
0f5660f7 217 }
218 }
219
ace9051c 220 $result = new stdClass();
0f5660f7 221 $result->user = $user;
222 $result->grades = $grades;
223 $result->feedbacks = $feedbacks;
224
225 return $result;
226 }
227
228 /**
229 * Close the iterator, do not forget to call this function.
230 * @return void
231 */
232 function close() {
caffc55a 233 if ($this->users_rs) {
d24832f9 234 $this->users_rs->close();
caffc55a 235 $this->users_rs = null;
0f5660f7 236 }
caffc55a 237 if ($this->grades_rs) {
d24832f9 238 $this->grades_rs->close();
caffc55a 239 $this->grades_rs = null;
0f5660f7 240 }
241 $this->gradestack = array();
242 }
243
cf72e2dd 244
0f5660f7 245 /**
cf72e2dd 246 * _push
247 *
248 * @param grade_grade $grade Grade object
249 *
250 * @return void
0f5660f7 251 */
252 function _push($grade) {
253 array_push($this->gradestack, $grade);
254 }
255
cf72e2dd 256
0f5660f7 257 /**
cf72e2dd 258 * _pop
259 *
260 * @return void
0f5660f7 261 */
262 function _pop() {
d24832f9 263 global $DB;
0f5660f7 264 if (empty($this->gradestack)) {
03cedd62 265 if (!$this->grades_rs) {
cf72e2dd 266 return null; // no grades present
0f5660f7 267 }
268
5c75a0a3 269 if ($this->grades_rs->next()) {
cf72e2dd 270 return null; // no more grades
0f5660f7 271 }
272
5c75a0a3 273 return $this->grades_rs->current();
0f5660f7 274 } else {
275 return array_pop($this->gradestack);
276 }
277 }
278}
279
d08bba83 280/**
281 * Print a selection popup form of the graded users in a course.
282 *
4d5059d4
SH
283 * @deprecated since 2.0
284 *
cf72e2dd 285 * @param int $course id of the course
d08bba83 286 * @param string $actionpage The page receiving the data from the popoup form
cf72e2dd 287 * @param int $userid id of the currently selected user (or 'all' if they are all selected)
288 * @param int $groupid id of requested group, 0 means all
289 * @param int $includeall bool include all option
290 * @param bool $return If true, will return the HTML, otherwise, will print directly
d08bba83 291 * @return null
292 */
7ac88172 293function print_graded_users_selector($course, $actionpage, $userid=0, $groupid=0, $includeall=true, $return=false) {
714b4745 294 global $CFG, $USER, $OUTPUT;
4d5059d4
SH
295 return $OUTPUT->render(grade_get_graded_users_select(substr($actionpage, 0, strpos($actionpage, '/')), $course, $userid, $groupid, $includeall));
296}
345674ca 297
4d5059d4 298function grade_get_graded_users_select($report, $course, $userid, $groupid, $includeall) {
f9510667
PS
299 global $USER;
300
879c99bb 301 if (is_null($userid)) {
302 $userid = $USER->id;
303 }
d08bba83 304
d08bba83 305 $menu = array(); // Will be a list of userid => user name
772229f3 306 $gui = new graded_users_iterator($course, null, $groupid);
d08bba83 307 $gui->init();
10f5c046 308 $label = get_string('selectauser', 'grades');
7ac88172 309 if ($includeall) {
879c99bb 310 $menu[0] = get_string('allusers', 'grades');
10f5c046 311 $label = get_string('selectalloroneuser', 'grades');
d08bba83 312 }
d08bba83 313 while ($userdata = $gui->next_user()) {
314 $user = $userdata->user;
315 $menu[$user->id] = fullname($user);
316 }
d08bba83 317 $gui->close();
318
7ac88172 319 if ($includeall) {
879c99bb 320 $menu[0] .= " (" . (count($menu) - 1) . ")";
321 }
4d5059d4 322 $select = new single_select(new moodle_url('/grade/report/'.$report.'/index.php', array('id'=>$course->id)), 'userid', $menu, $userid);
f8dab966
PS
323 $select->label = $label;
324 $select->formid = 'choosegradeuser';
4d5059d4 325 return $select;
d08bba83 326}
327
0610812a 328/**
329 * Print grading plugin selection popup form.
330 *
cf72e2dd 331 * @param array $plugin_info An array of plugins containing information for the selector
0610812a 332 * @param boolean $return return as string
cf72e2dd 333 *
0610812a 334 * @return nothing or string if $return true
335 */
f1a3e072 336function print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, $return=false) {
714b4745 337 global $CFG, $OUTPUT, $PAGE;
dc482cfa 338
339 $menu = array();
340 $count = 0;
341 $active = '';
342
343 foreach ($plugin_info as $plugin_type => $plugins) {
344 if ($plugin_type == 'strings') {
345 continue;
346 }
347
7981d537 348 $first_plugin = reset($plugins);
dc482cfa 349
f1a3e072
PS
350 $sectionname = $plugin_info['strings'][$plugin_type];
351 $section = array();
d4dcfc6b 352
f1a3e072 353 foreach ($plugins as $plugin) {
d4dcfc6b 354 $link = $plugin->link->out(false);
f1a3e072
PS
355 $section[$link] = $plugin->string;
356 $count++;
357 if ($plugin_type === $active_type and $plugin->id === $active_plugin) {
358 $active = $link;
dc482cfa 359 }
360 }
f1a3e072
PS
361
362 if ($section) {
363 $menu[] = array($sectionname=>$section);
364 }
dc482cfa 365 }
366
cf72e2dd 367 // finally print/return the popup form
dc482cfa 368 if ($count > 1) {
f1a3e072 369 $select = new url_select($menu, $active, null, 'choosepluginreport');
1c1f64a2 370
7981d537 371 if ($return) {
f1a3e072 372 return $OUTPUT->render($select);
7981d537 373 } else {
f1a3e072 374 echo $OUTPUT->render($select);
7981d537 375 }
dc482cfa 376 } else {
377 // only one option - no plugin selector needed
378 return '';
379 }
380}
381
382/**
383 * Print grading plugin selection tab-based navigation.
384 *
cf72e2dd 385 * @param string $active_type type of plugin on current page - import, export, report or edit
386 * @param string $active_plugin active plugin type - grader, user, cvs, ...
387 * @param array $plugin_info Array of plugins
dc482cfa 388 * @param boolean $return return as string
cf72e2dd 389 *
dc482cfa 390 * @return nothing or string if $return true
391 */
b827784e 392function grade_print_tabs($active_type, $active_plugin, $plugin_info, $return=false) {
dc482cfa 393 global $CFG, $COURSE;
1c1f64a2 394
e1513ca9 395 if (!isset($currenttab)) { //TODO: this is weird
dc482cfa 396 $currenttab = '';
397 }
398
399 $tabs = array();
400 $top_row = array();
401 $bottom_row = array();
402 $inactive = array($active_plugin);
403 $activated = array();
404
405 $count = 0;
406 $active = '';
407
408 foreach ($plugin_info as $plugin_type => $plugins) {
409 if ($plugin_type == 'strings') {
410 continue;
411 }
412
413 // If $plugins is actually the definition of a child-less parent link:
cf72e2dd 414 if (!empty($plugins->id)) {
415 $string = $plugins->string;
416 if (!empty($plugin_info[$active_type]->parent)) {
417 $string = $plugin_info[$active_type]->parent->string;
27eef3bb 418 }
419
cf72e2dd 420 $top_row[] = new tabobject($plugin_type, $plugins->link, $string);
dc482cfa 421 continue;
422 }
423
424 $first_plugin = reset($plugins);
cf72e2dd 425 $url = $first_plugin->link;
dc482cfa 426
427 if ($plugin_type == 'report') {
428 $url = $CFG->wwwroot.'/grade/report/index.php?id='.$COURSE->id;
429 }
430
431 $top_row[] = new tabobject($plugin_type, $url, $plugin_info['strings'][$plugin_type]);
432
433 if ($active_type == $plugin_type) {
434 foreach ($plugins as $plugin) {
cf72e2dd 435 $bottom_row[] = new tabobject($plugin->id, $plugin->link, $plugin->string);
436 if ($plugin->id == $active_plugin) {
437 $inactive = array($plugin->id);
dc482cfa 438 }
439 }
440 }
441 }
442
443 $tabs[] = $top_row;
444 $tabs[] = $bottom_row;
445
446 if ($return) {
447 return print_tabs($tabs, $active_type, $inactive, $activated, true);
448 } else {
449 print_tabs($tabs, $active_type, $inactive, $activated);
450 }
451}
452
cf72e2dd 453/**
454 * grade_get_plugin_info
455 *
456 * @param int $courseid The course id
457 * @param string $active_type type of plugin on current page - import, export, report or edit
458 * @param string $active_plugin active plugin type - grader, user, cvs, ...
459 *
460 * @return array
461 */
dc482cfa 462function grade_get_plugin_info($courseid, $active_type, $active_plugin) {
f7fcf4cd 463 global $CFG, $SITE;
cbff94ba 464
3af29899 465 $context = get_context_instance(CONTEXT_COURSE, $courseid);
cbff94ba 466
dc482cfa 467 $plugin_info = array();
6e2f3121 468 $count = 0;
3af29899 469 $active = '';
dc482cfa 470 $url_prefix = $CFG->wwwroot . '/grade/';
471
472 // Language strings
4d5059d4 473 $plugin_info['strings'] = grade_helper::get_plugin_strings();
b827784e 474
4d5059d4
SH
475 if ($reports = grade_helper::get_plugins_reports($courseid)) {
476 $plugin_info['report'] = $reports;
b827784e 477 }
c46aeeab 478
f7fcf4cd
AD
479 //showing grade categories and items make no sense if we're not within a course
480 if ($courseid!=$SITE->id) {
481 if ($edittree = grade_helper::get_info_edit_structure($courseid)) {
482 $plugin_info['edittree'] = $edittree;
483 }
04678d8e 484 }
b827784e 485
4d5059d4
SH
486 if ($scale = grade_helper::get_info_scales($courseid)) {
487 $plugin_info['scale'] = array('view'=>$scale);
cbff94ba 488 }
dc482cfa 489
4d5059d4
SH
490 if ($outcomes = grade_helper::get_info_outcomes($courseid)) {
491 $plugin_info['outcome'] = $outcomes;
cbff94ba 492 }
cbff94ba 493
4d5059d4
SH
494 if ($letters = grade_helper::get_info_letters($courseid)) {
495 $plugin_info['letter'] = $letters;
cbff94ba 496 }
4d5059d4
SH
497
498 if ($imports = grade_helper::get_plugins_import($courseid)) {
499 $plugin_info['import'] = $imports;
281ffa4a 500 }
c46aeeab 501
4d5059d4
SH
502 if ($exports = grade_helper::get_plugins_export($courseid)) {
503 $plugin_info['export'] = $exports;
281ffa4a 504 }
281ffa4a 505
4d5059d4
SH
506 foreach ($plugin_info as $plugin_type => $plugins) {
507 if (!empty($plugins->id) && $active_plugin == $plugins->id) {
508 $plugin_info['strings']['active_plugin_str'] = $plugins->string;
509 break;
281ffa4a 510 }
4d5059d4
SH
511 foreach ($plugins as $plugin) {
512 if (is_a($plugin, 'grade_plugin_info')) {
513 if ($active_plugin == $plugin->id) {
514 $plugin_info['strings']['active_plugin_str'] = $plugin->string;
515 }
3af29899 516 }
281ffa4a 517 }
cbff94ba 518 }
c46aeeab 519
f7fcf4cd
AD
520 //hide course settings if we're not in a course
521 if ($courseid!=$SITE->id) {
522 if ($setting = grade_helper::get_info_manage_settings($courseid)) {
523 $plugin_info['settings'] = array('course'=>$setting);
524 }
281ffa4a 525 }
cbff94ba 526
4d5059d4
SH
527 // Put preferences last
528 if ($preferences = grade_helper::get_plugins_report_preferences($courseid)) {
529 $plugin_info['preferences'] = $preferences;
27eef3bb 530 }
531
dc482cfa 532 foreach ($plugin_info as $plugin_type => $plugins) {
cf72e2dd 533 if (!empty($plugins->id) && $active_plugin == $plugins->id) {
534 $plugin_info['strings']['active_plugin_str'] = $plugins->string;
dc482cfa 535 break;
78ad5f3f 536 }
dc482cfa 537 foreach ($plugins as $plugin) {
cf72e2dd 538 if (is_a($plugin, 'grade_plugin_info')) {
539 if ($active_plugin == $plugin->id) {
540 $plugin_info['strings']['active_plugin_str'] = $plugin->string;
541 }
78ad5f3f 542 }
78ad5f3f 543 }
dc482cfa 544 }
78ad5f3f 545
dc482cfa 546 return $plugin_info;
547}
284abb09 548
cf72e2dd 549/**
550 * A simple class containing info about grade plugins.
551 * Can be subclassed for special rules
552 *
553 * @package moodlecore
554 * @copyright 2009 Nicolas Connault
555 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
556 */
557class grade_plugin_info {
558 /**
559 * A unique id for this plugin
560 *
561 * @var mixed
562 */
563 public $id;
564 /**
565 * A URL to access this plugin
566 *
567 * @var mixed
568 */
569 public $link;
570 /**
571 * The name of this plugin
572 *
573 * @var mixed
574 */
575 public $string;
576 /**
577 * Another grade_plugin_info object, parent of the current one
578 *
579 * @var mixed
580 */
581 public $parent;
582
583 /**
584 * Constructor
585 *
586 * @param int $id A unique id for this plugin
587 * @param string $link A URL to access this plugin
588 * @param string $string The name of this plugin
589 * @param object $parent Another grade_plugin_info object, parent of the current one
590 *
591 * @return void
592 */
593 public function __construct($id, $link, $string, $parent=null) {
594 $this->id = $id;
595 $this->link = $link;
596 $this->string = $string;
597 $this->parent = $parent;
598 }
599}
600
de0300ea 601/**
602 * Prints the page headers, breadcrumb trail, page heading, (optional) dropdown navigation menu and
603 * (optional) navigation tabs for any gradebook page. All gradebook pages MUST use these functions
604 * in favour of the usual print_header(), print_header_simple(), print_heading() etc.
605 * !IMPORTANT! Use of tabs.php file in gradebook pages is forbidden unless tabs are switched off at
606 * the site level for the gradebook ($CFG->grade_navmethod = GRADE_NAVMETHOD_DROPDOWN).
607 *
cf72e2dd 608 * @param int $courseid Course id
609 * @param string $active_type The type of the current page (report, settings,
610 * import, export, scales, outcomes, letters)
611 * @param string $active_plugin The plugin of the current page (grader, fullview etc...)
612 * @param string $heading The heading of the page. Tries to guess if none is given
de0300ea 613 * @param boolean $return Whether to return (true) or echo (false) the HTML generated by this function
cf72e2dd 614 * @param string $bodytags Additional attributes that will be added to the <body> tag
615 * @param string $buttons Additional buttons to display on the page
de0300ea 616 *
617 * @return string HTML code or nothing if $return == false
618 */
cf72e2dd 619function print_grade_page_head($courseid, $active_type, $active_plugin=null,
34a2777c 620 $heading = false, $return=false,
2821c495 621 $buttons=false, $shownavigation=true) {
4d5059d4 622 global $CFG, $OUTPUT, $PAGE;
c46aeeab 623
dc482cfa 624 $plugin_info = grade_get_plugin_info($courseid, $active_type, $active_plugin);
c46aeeab 625
dc482cfa 626 // Determine the string of the active plugin
627 $stractive_plugin = ($active_plugin) ? $plugin_info['strings']['active_plugin_str'] : $heading;
628 $stractive_type = $plugin_info['strings'][$active_type];
e0724506 629
cf72e2dd 630 if (empty($plugin_info[$active_type]->id) || !empty($plugin_info[$active_type]->parent)) {
4d5059d4
SH
631 $title = $PAGE->course->fullname.': ' . $stractive_type . ': ' . $stractive_plugin;
632 } else {
633 $title = $PAGE->course->fullname.': ' . $stractive_plugin;
c4c97a6d 634 }
635
367a75fa
SH
636 if ($active_type == 'report') {
637 $PAGE->set_pagelayout('report');
638 } else {
639 $PAGE->set_pagelayout('admin');
640 }
4d5059d4 641 $PAGE->set_title(get_string('grades') . ': ' . $stractive_type);
f3df5e14 642 $PAGE->set_heading($title);
4d5059d4
SH
643 if ($buttons instanceof single_button) {
644 $buttons = $OUTPUT->render($buttons);
645 }
f3df5e14 646 $PAGE->set_button($buttons);
4d5059d4
SH
647 grade_extend_settings($plugin_info, $courseid);
648
f3df5e14 649 $returnval = $OUTPUT->header();
650 if (!$return) {
651 echo $returnval;
652 }
dc482cfa 653
654 // Guess heading if not given explicitly
655 if (!$heading) {
656 $heading = $stractive_plugin;
657 }
658
2821c495
AD
659 if ($shownavigation) {
660 if ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_DROPDOWN) {
661 $returnval .= print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, $return);
662 }
663 $returnval .= $OUTPUT->heading($heading);
664 if ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_TABS) {
665 $returnval .= grade_print_tabs($active_type, $active_plugin, $plugin_info, $return);
666 }
8354e716 667 }
dc482cfa 668
669 if ($return) {
670 return $returnval;
6e2f3121 671 }
cbff94ba 672}
673
0610812a 674/**
7a6b7acf 675 * Utility class used for return tracking when using edit and other forms in grade plugins
cf72e2dd 676 *
677 * @package moodlecore
678 * @copyright 2009 Nicolas Connault
679 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0610812a 680 */
3af29899 681class grade_plugin_return {
d24832f9 682 public $type;
683 public $plugin;
684 public $courseid;
685 public $userid;
686 public $page;
281ffa4a 687
0610812a 688 /**
689 * Constructor
cf72e2dd 690 *
0610812a 691 * @param array $params - associative array with return parameters, if null parameter are taken from _GET or _POST
692 */
cf72e2dd 693 public function grade_plugin_return($params = null) {
3af29899 694 if (empty($params)) {
695 $this->type = optional_param('gpr_type', null, PARAM_SAFEDIR);
696 $this->plugin = optional_param('gpr_plugin', null, PARAM_SAFEDIR);
697 $this->courseid = optional_param('gpr_courseid', null, PARAM_INT);
698 $this->userid = optional_param('gpr_userid', null, PARAM_INT);
699 $this->page = optional_param('gpr_page', null, PARAM_INT);
a983b6ec 700
a983b6ec 701 } else {
3af29899 702 foreach ($params as $key=>$value) {
4cc977a6 703 if (property_exists($this, $key)) {
3af29899 704 $this->$key = $value;
705 }
cbff94ba 706 }
707 }
6cd8c592 708 }
709
0610812a 710 /**
711 * Returns return parameters as options array suitable for buttons.
712 * @return array options
713 */
d24832f9 714 public function get_options() {
7a6b7acf 715 if (empty($this->type)) {
3af29899 716 return array();
865e9a82 717 }
6cd8c592 718
3af29899 719 $params = array();
6cd8c592 720
7a6b7acf 721 if (!empty($this->plugin)) {
722 $params['plugin'] = $this->plugin;
723 }
6cd8c592 724
3af29899 725 if (!empty($this->courseid)) {
726 $params['id'] = $this->courseid;
6cd8c592 727 }
9c61ba4d 728
3af29899 729 if (!empty($this->userid)) {
730 $params['userid'] = $this->userid;
9c61ba4d 731 }
9c61ba4d 732
3af29899 733 if (!empty($this->page)) {
734 $params['page'] = $this->page;
cbff94ba 735 }
865e9a82 736
3af29899 737 return $params;
cbff94ba 738 }
cbff94ba 739
0610812a 740 /**
741 * Returns return url
cf72e2dd 742 *
0610812a 743 * @param string $default default url when params not set
cf72e2dd 744 * @param array $extras Extra URL parameters
745 *
0610812a 746 * @return string url
747 */
d24832f9 748 public function get_return_url($default, $extras=null) {
3af29899 749 global $CFG;
cbff94ba 750
3af29899 751 if (empty($this->type) or empty($this->plugin)) {
752 return $default;
cbff94ba 753 }
754
65dd61bd 755 $url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php';
756 $glue = '?';
cbff94ba 757
3af29899 758 if (!empty($this->courseid)) {
759 $url .= $glue.'id='.$this->courseid;
760 $glue = '&amp;';
cbff94ba 761 }
cbff94ba 762
3af29899 763 if (!empty($this->userid)) {
764 $url .= $glue.'userid='.$this->userid;
765 $glue = '&amp;';
cbff94ba 766 }
7e2d7c92 767
3af29899 768 if (!empty($this->page)) {
769 $url .= $glue.'page='.$this->page;
65dd61bd 770 $glue = '&amp;';
771 }
772
773 if (!empty($extras)) {
cf72e2dd 774 foreach ($extras as $key=>$value) {
65dd61bd 775 $url .= $glue.$key.'='.$value;
776 $glue = '&amp;';
7a6b7acf 777 }
cbff94ba 778 }
cbff94ba 779
3af29899 780 return $url;
cbff94ba 781 }
cbff94ba 782
0610812a 783 /**
784 * Returns string with hidden return tracking form elements.
785 * @return string
786 */
d24832f9 787 public function get_form_fields() {
7a6b7acf 788 if (empty($this->type)) {
3af29899 789 return '';
cbff94ba 790 }
cbff94ba 791
3af29899 792 $result = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />';
7a6b7acf 793
794 if (!empty($this->plugin)) {
795 $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />';
796 }
0ca5abd6 797
3af29899 798 if (!empty($this->courseid)) {
799 $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />';
cbff94ba 800 }
cbff94ba 801
3af29899 802 if (!empty($this->userid)) {
803 $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />';
cbff94ba 804 }
cbff94ba 805
3af29899 806 if (!empty($this->page)) {
807 $result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />';
cbff94ba 808 }
809 }
cbff94ba 810
0610812a 811 /**
812 * Add hidden elements into mform
cf72e2dd 813 *
814 * @param object &$mform moodle form object
815 *
0610812a 816 * @return void
817 */
d24832f9 818 public function add_mform_elements(&$mform) {
7a6b7acf 819 if (empty($this->type)) {
3af29899 820 return;
cbff94ba 821 }
cbff94ba 822
3af29899 823 $mform->addElement('hidden', 'gpr_type', $this->type);
824 $mform->setType('gpr_type', PARAM_SAFEDIR);
cbff94ba 825
7a6b7acf 826 if (!empty($this->plugin)) {
827 $mform->addElement('hidden', 'gpr_plugin', $this->plugin);
828 $mform->setType('gpr_plugin', PARAM_SAFEDIR);
829 }
97033c86 830
3af29899 831 if (!empty($this->courseid)) {
832 $mform->addElement('hidden', 'gpr_courseid', $this->courseid);
833 $mform->setType('gpr_courseid', PARAM_INT);
cbff94ba 834 }
cbff94ba 835
3af29899 836 if (!empty($this->userid)) {
837 $mform->addElement('hidden', 'gpr_userid', $this->userid);
838 $mform->setType('gpr_userid', PARAM_INT);
cbff94ba 839 }
cbff94ba 840
3af29899 841 if (!empty($this->page)) {
842 $mform->addElement('hidden', 'gpr_page', $this->page);
843 $mform->setType('gpr_page', PARAM_INT);
cbff94ba 844 }
845 }
281ffa4a 846
0610812a 847 /**
848 * Add return tracking params into url
cf72e2dd 849 *
1c1f64a2 850 * @param moodle_url $url A URL
cf72e2dd 851 *
6ef4878b 852 * @return string $url with return tracking params
0610812a 853 */
1c1f64a2 854 public function add_url_params(moodle_url $url) {
7a6b7acf 855 if (empty($this->type)) {
3af29899 856 return $url;
cbff94ba 857 }
5609f9e6 858
1c1f64a2 859 $url->param('gpr_type', $this->type);
cbff94ba 860
7a6b7acf 861 if (!empty($this->plugin)) {
1c1f64a2 862 $url->param('gpr_plugin', $this->plugin);
7a6b7acf 863 }
cbff94ba 864
3af29899 865 if (!empty($this->courseid)) {
1c1f64a2 866 $url->param('gpr_courseid' ,$this->courseid);
cbff94ba 867 }
cbff94ba 868
3af29899 869 if (!empty($this->userid)) {
1c1f64a2 870 $url->param('gpr_userid', $this->userid);
cbff94ba 871 }
0a8a95c9 872
3af29899 873 if (!empty($this->page)) {
1c1f64a2 874 $url->param('gpr_page', $this->page);
0a8a95c9 875 }
5a412dbf 876
3af29899 877 return $url;
5a412dbf 878 }
5a412dbf 879}
7a6b7acf 880
826c5f86 881/**
882 * Function central to gradebook for building and printing the navigation (breadcrumb trail).
cf72e2dd 883 *
826c5f86 884 * @param string $path The path of the calling script (using __FILE__?)
885 * @param string $pagename The language string to use as the last part of the navigation (non-link)
cf72e2dd 886 * @param mixed $id Either a plain integer (assuming the key is 'id') or
887 * an array of keys and values (e.g courseid => $courseid, itemid...)
888 *
826c5f86 889 * @return string
890 */
891function grade_build_nav($path, $pagename=null, $id=null) {
f3df5e14 892 global $CFG, $COURSE, $PAGE;
826c5f86 893
894 $strgrades = get_string('grades', 'grades');
895
896 // Parse the path and build navlinks from its elements
897 $dirroot_length = strlen($CFG->dirroot) + 1; // Add 1 for the first slash
898 $path = substr($path, $dirroot_length);
899 $path = str_replace('\\', '/', $path);
900
901 $path_elements = explode('/', $path);
902
903 $path_elements_count = count($path_elements);
904
826c5f86 905 // First link is always 'grade'
a6855934 906 $PAGE->navbar->add($strgrades, new moodle_url('/grade/index.php', array('id'=>$COURSE->id)));
826c5f86 907
f3df5e14 908 $link = null;
826c5f86 909 $numberofelements = 3;
910
911 // Prepare URL params string
f3df5e14 912 $linkparams = array();
826c5f86 913 if (!is_null($id)) {
914 if (is_array($id)) {
915 foreach ($id as $idkey => $idvalue) {
f3df5e14 916 $linkparams[$idkey] = $idvalue;
826c5f86 917 }
918 } else {
f3df5e14 919 $linkparams['id'] = $id;
826c5f86 920 }
921 }
922
923 $navlink4 = null;
924
0f78c4de 925 // Remove file extensions from filenames
926 foreach ($path_elements as $key => $filename) {
927 $path_elements[$key] = str_replace('.php', '', $filename);
928 }
929
826c5f86 930 // Second level links
931 switch ($path_elements[1]) {
932 case 'edit': // No link
933 if ($path_elements[3] != 'index.php') {
934 $numberofelements = 4;
935 }
936 break;
937 case 'import': // No link
938 break;
939 case 'export': // No link
940 break;
941 case 'report':
942 // $id is required for this link. Do not print it if $id isn't given
943 if (!is_null($id)) {
a6855934 944 $link = new moodle_url('/grade/report/index.php', $linkparams);
826c5f86 945 }
946
947 if ($path_elements[2] == 'grader') {
948 $numberofelements = 4;
949 }
950 break;
951
952 default:
953 // If this element isn't among the ones already listed above, it isn't supported, throw an error.
cf72e2dd 954 debugging("grade_build_nav() doesn't support ". $path_elements[1] .
955 " as the second path element after 'grade'.");
826c5f86 956 return false;
957 }
f3df5e14 958 $PAGE->navbar->add(get_string($path_elements[1], 'grades'), $link);
826c5f86 959
960 // Third level links
961 if (empty($pagename)) {
962 $pagename = get_string($path_elements[2], 'grades');
963 }
964
965 switch ($numberofelements) {
966 case 3:
f3df5e14 967 $PAGE->navbar->add($pagename, $link);
826c5f86 968 break;
969 case 4:
826c5f86 970 if ($path_elements[2] == 'grader' AND $path_elements[3] != 'index.php') {
b5e7b2bf 971 $PAGE->navbar->add(get_string('pluginname', 'gradereport_grader'), new moodle_url('/grade/report/grader/index.php', $linkparams));
826c5f86 972 }
f3df5e14 973 $PAGE->navbar->add($pagename);
826c5f86 974 break;
975 }
826c5f86 976
f3df5e14 977 return '';
d4795a07 978}
7a6b7acf 979
e98871a2 980/**
6cc3e350 981 * General structure representing grade items in course
cf72e2dd 982 *
983 * @package moodlecore
984 * @copyright 2009 Nicolas Connault
985 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
e98871a2 986 */
6cc3e350 987class grade_structure {
d24832f9 988 public $context;
e98871a2 989
d24832f9 990 public $courseid;
e98871a2 991
992 /**
993 * 1D array of grade items only
994 */
d24832f9 995 public $items;
e98871a2 996
6391ebe7 997 /**
6cc3e350 998 * Returns icon of element
cf72e2dd 999 *
1000 * @param array &$element An array representing an element in the grade_tree
1001 * @param bool $spacerifnone return spacer if no icon found
1002 *
6cc3e350 1003 * @return string icon or spacer
6391ebe7 1004 */
d24832f9 1005 public function get_element_icon(&$element, $spacerifnone=false) {
6b608f8f 1006 global $CFG, $OUTPUT;
6cc3e350 1007
1008 switch ($element['type']) {
1009 case 'item':
1010 case 'courseitem':
1011 case 'categoryitem':
cf72e2dd 1012 $is_course = $element['object']->is_course_item();
1013 $is_category = $element['object']->is_category_item();
1014 $is_scale = $element['object']->gradetype == GRADE_TYPE_SCALE;
1015 $is_value = $element['object']->gradetype == GRADE_TYPE_VALUE;
0077f571 1016 $is_outcome = !empty($element['object']->outcomeid);
cf72e2dd 1017
6cc3e350 1018 if ($element['object']->is_calculated()) {
dc482cfa 1019 $strcalc = get_string('calculatedgrade', 'grades');
b5d0cafc 1020 return '<img src="'.$OUTPUT->pix_url('i/calc') . '" class="icon itemicon" title="'.
cf72e2dd 1021 s($strcalc).'" alt="'.s($strcalc).'"/>';
6cc3e350 1022
cf72e2dd 1023 } else if (($is_course or $is_category) and ($is_scale or $is_value)) {
6cc3e350 1024 if ($category = $element['object']->get_item_category()) {
1025 switch ($category->aggregation) {
1026 case GRADE_AGGREGATE_MEAN:
1027 case GRADE_AGGREGATE_MEDIAN:
1028 case GRADE_AGGREGATE_WEIGHTED_MEAN:
1426edac 1029 case GRADE_AGGREGATE_WEIGHTED_MEAN2:
6cc3e350 1030 case GRADE_AGGREGATE_EXTRACREDIT_MEAN:
dc482cfa 1031 $stragg = get_string('aggregation', 'grades');
b5d0cafc 1032 return '<img src="'.$OUTPUT->pix_url('i/agg_mean') . '" ' .
cf72e2dd 1033 'class="icon itemicon" title="'.s($stragg).'" alt="'.s($stragg).'"/>';
0758a08e 1034 case GRADE_AGGREGATE_SUM:
dc482cfa 1035 $stragg = get_string('aggregation', 'grades');
b5d0cafc 1036 return '<img src="'.$OUTPUT->pix_url('i/agg_sum') . '" ' .
cf72e2dd 1037 'class="icon itemicon" title="'.s($stragg).'" alt="'.s($stragg).'"/>';
6cc3e350 1038 }
1039 }
1040
1041 } else if ($element['object']->itemtype == 'mod') {
0077f571
AD
1042 //prevent outcomes being displaying the same icon as the activity they are attached to
1043 if ($is_outcome) {
1044 $stroutcome = s(get_string('outcome', 'grades'));
1045 return '<img src="'.$OUTPUT->pix_url('i/outcomes') . '" ' .
1046 'class="icon itemicon" title="'.$stroutcome.
1047 '" alt="'.$stroutcome.'"/>';
1048 } else {
1049 $strmodname = get_string('modulename', $element['object']->itemmodule);
1050 return '<img src="'.$OUTPUT->pix_url('icon',
e63f88c9 1051 $element['object']->itemmodule) . '" ' .
cf72e2dd 1052 'class="icon itemicon" title="' .s($strmodname).
1053 '" alt="' .s($strmodname).'"/>';
0077f571 1054 }
6cc3e350 1055 } else if ($element['object']->itemtype == 'manual') {
1056 if ($element['object']->is_outcome_item()) {
dc482cfa 1057 $stroutcome = get_string('outcome', 'grades');
b5d0cafc 1058 return '<img src="'.$OUTPUT->pix_url('i/outcomes') . '" ' .
cf72e2dd 1059 'class="icon itemicon" title="'.s($stroutcome).
1060 '" alt="'.s($stroutcome).'"/>';
6cc3e350 1061 } else {
dc482cfa 1062 $strmanual = get_string('manualitem', 'grades');
b5d0cafc 1063 return '<img src="'.$OUTPUT->pix_url('t/manual_item') . '" '.
cf72e2dd 1064 'class="icon itemicon" title="'.s($strmanual).
1065 '" alt="'.s($strmanual).'"/>';
6cc3e350 1066 }
1067 }
1068 break;
1069
1070 case 'category':
dc482cfa 1071 $strcat = get_string('category', 'grades');
b5d0cafc 1072 return '<img src="'.$OUTPUT->pix_url('f/folder') . '" class="icon itemicon" ' .
cf72e2dd 1073 'title="'.s($strcat).'" alt="'.s($strcat).'" />';
6cc3e350 1074 }
1075
1076 if ($spacerifnone) {
b11681e0 1077 return $OUTPUT->spacer().' ';
6cc3e350 1078 } else {
1079 return '';
1080 }
1081 }
6391ebe7 1082
e98871a2 1083 /**
6cc3e350 1084 * Returns name of element optionally with icon and link
cf72e2dd 1085 *
1086 * @param array &$element An array representing an element in the grade_tree
1087 * @param bool $withlink Whether or not this header has a link
1088 * @param bool $icon Whether or not to display an icon with this header
1089 * @param bool $spacerifnone return spacer if no icon found
1090 *
1091 * @return string header
e98871a2 1092 */
d24832f9 1093 public function get_element_header(&$element, $withlink=false, $icon=true, $spacerifnone=false) {
6cc3e350 1094 global $CFG;
1095
1096 $header = '';
1097
1098 if ($icon) {
1099 $header .= $this->get_element_icon($element, $spacerifnone);
1100 }
1101
dc482cfa 1102 $header .= $element['object']->get_name();
6cc3e350 1103
cf72e2dd 1104 if ($element['type'] != 'item' and $element['type'] != 'categoryitem' and
1105 $element['type'] != 'courseitem') {
6cc3e350 1106 return $header;
1107 }
1108
1109 $itemtype = $element['object']->itemtype;
1110 $itemmodule = $element['object']->itemmodule;
1111 $iteminstance = $element['object']->iteminstance;
1112
1113 if ($withlink and $itemtype=='mod' and $iteminstance and $itemmodule) {
46d1af82 1114 if ($cm = get_coursemodule_from_instance($itemmodule, $iteminstance, $this->courseid)) {
6cc3e350 1115
ace9051c 1116 $a = new stdClass();
45a639d5 1117 $a->name = get_string('modulename', $element['object']->itemmodule);
dc482cfa 1118 $title = get_string('linktoactivity', 'grades', $a);
46d1af82 1119 $dir = $CFG->dirroot.'/mod/'.$itemmodule;
6cc3e350 1120
46d1af82 1121 if (file_exists($dir.'/grade.php')) {
1122 $url = $CFG->wwwroot.'/mod/'.$itemmodule.'/grade.php?id='.$cm->id;
1123 } else {
1124 $url = $CFG->wwwroot.'/mod/'.$itemmodule.'/view.php?id='.$cm->id;
1125 }
6cc3e350 1126
1edd08ab 1127 $header = '<a href="'.$url.'" title="'.s($title).'">'.$header.'</a>';
46d1af82 1128 }
6cc3e350 1129 }
1130
1131 return $header;
1132 }
1133
1134 /**
1135 * Returns the grade eid - the grade may not exist yet.
cf72e2dd 1136 *
1137 * @param grade_grade $grade_grade A grade_grade object
1138 *
6cc3e350 1139 * @return string eid
1140 */
d24832f9 1141 public function get_grade_eid($grade_grade) {
6cc3e350 1142 if (empty($grade_grade->id)) {
1143 return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid;
1144 } else {
1145 return 'g'.$grade_grade->id;
1146 }
1147 }
1148
1149 /**
1150 * Returns the grade_item eid
cf72e2dd 1151 * @param grade_item $grade_item A grade_item object
6cc3e350 1152 * @return string eid
1153 */
d24832f9 1154 public function get_item_eid($grade_item) {
6cc3e350 1155 return 'i'.$grade_item->id;
1156 }
1157
cf72e2dd 1158 /**
1159 * Given a grade_tree element, returns an array of parameters
1160 * used to build an icon for that element.
1161 *
1162 * @param array $element An array representing an element in the grade_tree
1163 *
1164 * @return array
1165 */
1166 public function get_params_for_iconstr($element) {
9ecd4386 1167 $strparams = new stdClass();
1168 $strparams->category = '';
1169 $strparams->itemname = '';
1170 $strparams->itemmodule = '';
cf72e2dd 1171
9ecd4386 1172 if (!method_exists($element['object'], 'get_name')) {
1173 return $strparams;
1174 }
345674ca 1175
b285c5aa 1176 $strparams->itemname = html_to_text($element['object']->get_name());
9ecd4386 1177
1178 // If element name is categorytotal, get the name of the parent category
1179 if ($strparams->itemname == get_string('categorytotal', 'grades')) {
1180 $parent = $element['object']->get_parent_category();
1181 $strparams->category = $parent->get_name() . ' ';
1182 } else {
1183 $strparams->category = '';
1184 }
1185
1186 $strparams->itemmodule = null;
1187 if (isset($element['object']->itemmodule)) {
1188 $strparams->itemmodule = $element['object']->itemmodule;
345674ca 1189 }
9ecd4386 1190 return $strparams;
1191 }
1192
6cc3e350 1193 /**
1194 * Return edit icon for give element
cf72e2dd 1195 *
1196 * @param array $element An array representing an element in the grade_tree
1197 * @param object $gpr A grade_plugin_return object
1198 *
6cc3e350 1199 * @return string
1200 */
d24832f9 1201 public function get_edit_icon($element, $gpr) {
6b608f8f 1202 global $CFG, $OUTPUT;
6cc3e350 1203
1204 if (!has_capability('moodle/grade:manage', $this->context)) {
1205 if ($element['type'] == 'grade' and has_capability('moodle/grade:edit', $this->context)) {
1206 // oki - let them override grade
1207 } else {
1208 return '';
1209 }
1210 }
1211
05aba805 1212 static $strfeedback = null;
1213 static $streditgrade = null;
1214 if (is_null($streditgrade)) {
1215 $streditgrade = get_string('editgrade', 'grades');
1216 $strfeedback = get_string('feedback');
6cc3e350 1217 }
1218
9ecd4386 1219 $strparams = $this->get_params_for_iconstr($element);
6ef4878b 1220
6cc3e350 1221 $object = $element['object'];
6cc3e350 1222
1223 switch ($element['type']) {
1224 case 'item':
1225 case 'categoryitem':
1226 case 'courseitem':
9ecd4386 1227 $stredit = get_string('editverbose', 'grades', $strparams);
6cc3e350 1228 if (empty($object->outcomeid) || empty($CFG->enableoutcomes)) {
a6855934 1229 $url = new moodle_url('/grade/edit/tree/item.php',
1c1f64a2 1230 array('courseid' => $this->courseid, 'id' => $object->id));
6cc3e350 1231 } else {
a6855934 1232 $url = new moodle_url('/grade/edit/tree/outcomeitem.php',
1c1f64a2 1233 array('courseid' => $this->courseid, 'id' => $object->id));
6cc3e350 1234 }
6cc3e350 1235 break;
1236
1237 case 'category':
9ecd4386 1238 $stredit = get_string('editverbose', 'grades', $strparams);
a6855934 1239 $url = new moodle_url('/grade/edit/tree/category.php',
1c1f64a2 1240 array('courseid' => $this->courseid, 'id' => $object->id));
6cc3e350 1241 break;
1242
1243 case 'grade':
05aba805 1244 $stredit = $streditgrade;
6cc3e350 1245 if (empty($object->id)) {
a6855934 1246 $url = new moodle_url('/grade/edit/tree/grade.php',
1c1f64a2 1247 array('courseid' => $this->courseid, 'itemid' => $object->itemid, 'userid' => $object->userid));
6cc3e350 1248 } else {
a6855934 1249 $url = new moodle_url('/grade/edit/tree/grade.php',
1c1f64a2 1250 array('courseid' => $this->courseid, 'id' => $object->id));
6cc3e350 1251 }
6cc3e350 1252 if (!empty($object->feedback)) {
1253 $feedback = addslashes_js(trim(format_string($object->feedback, $object->feedbackformat)));
6cc3e350 1254 }
1255 break;
1256
1257 default:
1258 $url = null;
1259 }
1260
1261 if ($url) {
c63923bd 1262 return $OUTPUT->action_icon($gpr->add_url_params($url), new pix_icon('t/edit', $stredit));
6cc3e350 1263
1264 } else {
1265 return '';
1266 }
1267 }
1268
1269 /**
1270 * Return hiding icon for give element
cf72e2dd 1271 *
1272 * @param array $element An array representing an element in the grade_tree
1273 * @param object $gpr A grade_plugin_return object
1274 *
6cc3e350 1275 * @return string
1276 */
d24832f9 1277 public function get_hiding_icon($element, $gpr) {
5d3b9994 1278 global $CFG, $OUTPUT;
6cc3e350 1279
cf72e2dd 1280 if (!has_capability('moodle/grade:manage', $this->context) and
1281 !has_capability('moodle/grade:hide', $this->context)) {
6cc3e350 1282 return '';
1283 }
1284
345674ca 1285 $strparams = $this->get_params_for_iconstr($element);
9ecd4386 1286 $strshow = get_string('showverbose', 'grades', $strparams);
345674ca 1287 $strhide = get_string('hideverbose', 'grades', $strparams);
6cc3e350 1288
a6855934 1289 $url = new moodle_url('/grade/edit/tree/action.php', array('id' => $this->courseid, 'sesskey' => sesskey(), 'eid' => $element['eid']));
8ae8bf8a 1290 $url = $gpr->add_url_params($url);
1c1f64a2 1291
6cc3e350 1292 if ($element['object']->is_hidden()) {
8ae8bf8a 1293 $type = 'show';
6cc3e350 1294 $tooltip = $strshow;
1295
cf72e2dd 1296 // Change the icon and add a tooltip showing the date
1297 if ($element['type'] != 'category' and $element['object']->get_hidden() > 1) {
8ae8bf8a 1298 $type = 'hiddenuntil';
cf72e2dd 1299 $tooltip = get_string('hiddenuntildate', 'grades',
1300 userdate($element['object']->get_hidden()));
6cc3e350 1301 }
1302
8ae8bf8a 1303 $url->param('action', 'show');
b9cc756e 1304
c63923bd 1305 $hideicon = $OUTPUT->action_icon($url, new pix_icon('t/'.$type, $tooltip, 'moodle', array('alt'=>$strshow, 'class'=>'iconsmall')));
6cc3e350 1306
1307 } else {
8ae8bf8a 1308 $url->param('action', 'hide');
c63923bd 1309 $hideicon = $OUTPUT->action_icon($url, new pix_icon('t/hide', $strhide));
6cc3e350 1310 }
1c1f64a2 1311
8ae8bf8a 1312 return $hideicon;
6cc3e350 1313 }
1314
1315 /**
2673c733 1316 * Return locking icon for given element
cf72e2dd 1317 *
1318 * @param array $element An array representing an element in the grade_tree
1319 * @param object $gpr A grade_plugin_return object
1320 *
6cc3e350 1321 * @return string
1322 */
d24832f9 1323 public function get_locking_icon($element, $gpr) {
6b608f8f 1324 global $CFG, $OUTPUT;
6cc3e350 1325
345674ca 1326 $strparams = $this->get_params_for_iconstr($element);
9ecd4386 1327 $strunlock = get_string('unlockverbose', 'grades', $strparams);
1328 $strlock = get_string('lockverbose', 'grades', $strparams);
d24832f9 1329
a6855934 1330 $url = new moodle_url('/grade/edit/tree/action.php', array('id' => $this->courseid, 'sesskey' => sesskey(), 'eid' => $element['eid']));
8ae8bf8a 1331 $url = $gpr->add_url_params($url);
1c1f64a2 1332
2673c733 1333 // Don't allow an unlocking action for a grade whose grade item is locked: just print a state icon
1334 if ($element['type'] == 'grade' && $element['object']->grade_item->is_locked()) {
1335 $strparamobj = new stdClass();
1336 $strparamobj->itemname = $element['object']->grade_item->itemname;
1337 $strnonunlockable = get_string('nonunlockableverbose', 'grades', $strparamobj);
1c1f64a2 1338
000c278c 1339 $action = $OUTPUT->pix_icon('t/unlock_gray', $strnonunlockable);
8ae8bf8a 1340
cf72e2dd 1341 } else if ($element['object']->is_locked()) {
8ae8bf8a 1342 $type = 'unlock';
6cc3e350 1343 $tooltip = $strunlock;
1344
cf72e2dd 1345 // Change the icon and add a tooltip showing the date
1346 if ($element['type'] != 'category' and $element['object']->get_locktime() > 1) {
8ae8bf8a 1347 $type = 'locktime';
cf72e2dd 1348 $tooltip = get_string('locktimedate', 'grades',
1349 userdate($element['object']->get_locktime()));
6cc3e350 1350 }
1351
8ae8bf8a
PS
1352 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:unlock', $this->context)) {
1353 $action = '';
1354 } else {
1355 $url->param('action', 'unlock');
c63923bd 1356 $action = $OUTPUT->action_icon($url, new pix_icon('t/'.$type, $tooltip, 'moodle', array('alt'=>$strunlock, 'class'=>'smallicon')));
6cc3e350 1357 }
6cc3e350 1358
1359 } else {
8ae8bf8a
PS
1360 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:lock', $this->context)) {
1361 $action = '';
1362 } else {
1363 $url->param('action', 'lock');
c63923bd 1364 $action = $OUTPUT->action_icon($url, new pix_icon('t/lock', $strlock));
6cc3e350 1365 }
6cc3e350 1366 }
8ae8bf8a 1367
6cc3e350 1368 return $action;
1369 }
1370
1371 /**
1372 * Return calculation icon for given element
cf72e2dd 1373 *
1374 * @param array $element An array representing an element in the grade_tree
1375 * @param object $gpr A grade_plugin_return object
1376 *
6cc3e350 1377 * @return string
1378 */
d24832f9 1379 public function get_calculation_icon($element, $gpr) {
666e8458 1380 global $CFG, $OUTPUT;
6cc3e350 1381 if (!has_capability('moodle/grade:manage', $this->context)) {
1382 return '';
1383 }
1384
6cc3e350 1385 $type = $element['type'];
1386 $object = $element['object'];
1387
1388 if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') {
345674ca 1389 $strparams = $this->get_params_for_iconstr($element);
9ecd4386 1390 $streditcalculation = get_string('editcalculationverbose', 'grades', $strparams);
6cc3e350 1391
cf72e2dd 1392 $is_scale = $object->gradetype == GRADE_TYPE_SCALE;
1393 $is_value = $object->gradetype == GRADE_TYPE_VALUE;
1394
6cc3e350 1395 // show calculation icon only when calculation possible
cf72e2dd 1396 if (!$object->is_external_item() and ($is_scale or $is_value)) {
6cc3e350 1397 if ($object->is_calculated()) {
666e8458 1398 $icon = 't/calc';
6cc3e350 1399 } else {
666e8458 1400 $icon = 't/calc_off';
6cc3e350 1401 }
cf72e2dd 1402
a6855934 1403 $url = new moodle_url('/grade/edit/tree/calculation.php', array('courseid' => $this->courseid, 'id' => $object->id));
8ae8bf8a 1404 $url = $gpr->add_url_params($url);
c63923bd 1405 return $OUTPUT->action_icon($url, new pix_icon($icon, $streditcalculation)) . "\n";
6cc3e350 1406 }
1407 }
1408
1c1f64a2 1409 return '';
6cc3e350 1410 }
1411}
1412
1413/**
1414 * Flat structure similar to grade tree.
cf72e2dd 1415 *
1416 * @uses grade_structure
1417 * @package moodlecore
1418 * @copyright 2009 Nicolas Connault
1419 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6cc3e350 1420 */
1421class grade_seq extends grade_structure {
1422
6cc3e350 1423 /**
1424 * 1D array of elements
1425 */
d24832f9 1426 public $elements;
e98871a2 1427
1428 /**
1429 * Constructor, retrieves and stores array of all grade_category and grade_item
1430 * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed.
cf72e2dd 1431 *
1432 * @param int $courseid The course id
1433 * @param bool $category_grade_last category grade item is the last child
1434 * @param bool $nooutcomes Whether or not outcomes should be included
e98871a2 1435 */
d24832f9 1436 public function grade_seq($courseid, $category_grade_last=false, $nooutcomes=false) {
e98871a2 1437 global $USER, $CFG;
1438
1439 $this->courseid = $courseid;
e98871a2 1440 $this->context = get_context_instance(CONTEXT_COURSE, $courseid);
1441
1442 // get course grade tree
1443 $top_element = grade_category::fetch_course_tree($courseid, true);
1444
6391ebe7 1445 $this->elements = grade_seq::flatten($top_element, $category_grade_last, $nooutcomes);
1446
1447 foreach ($this->elements as $key=>$unused) {
b89a70ce 1448 $this->items[$this->elements[$key]['object']->id] =& $this->elements[$key]['object'];
6391ebe7 1449 }
e98871a2 1450 }
1451
1452 /**
1453 * Static recursive helper - makes the grade_item for category the last children
cf72e2dd 1454 *
1455 * @param array &$element The seed of the recursion
1456 * @param bool $category_grade_last category grade item is the last child
1457 * @param bool $nooutcomes Whether or not outcomes should be included
1458 *
1459 * @return array
e98871a2 1460 */
d24832f9 1461 public function flatten(&$element, $category_grade_last, $nooutcomes) {
e98871a2 1462 if (empty($element['children'])) {
1463 return array();
1464 }
1465 $children = array();
1466
1467 foreach ($element['children'] as $sortorder=>$unused) {
cf72e2dd 1468 if ($nooutcomes and $element['type'] != 'category' and
1469 $element['children'][$sortorder]['object']->is_outcome_item()) {
e98871a2 1470 continue;
1471 }
1472 $children[] = $element['children'][$sortorder];
1473 }
1474 unset($element['children']);
1475
1476 if ($category_grade_last and count($children) > 1) {
1477 $cat_item = array_shift($children);
1478 array_push($children, $cat_item);
1479 }
1480
1481 $result = array();
1482 foreach ($children as $child) {
e0724506 1483 if ($child['type'] == 'category') {
6391ebe7 1484 $result = $result + grade_seq::flatten($child, $category_grade_last, $nooutcomes);
e98871a2 1485 } else {
1486 $child['eid'] = 'i'.$child['object']->id;
6391ebe7 1487 $result[$child['object']->id] = $child;
e98871a2 1488 }
1489 }
1490
1491 return $result;
1492 }
1493
1494 /**
1495 * Parses the array in search of a given eid and returns a element object with
1496 * information about the element it has found.
cf72e2dd 1497 *
1498 * @param int $eid Gradetree Element ID
1499 *
e98871a2 1500 * @return object element
1501 */
d24832f9 1502 public function locate_element($eid) {
e98871a2 1503 // it is a grade - construct a new object
1504 if (strpos($eid, 'n') === 0) {
1505 if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) {
1506 return null;
1507 }
1508
1509 $itemid = $matches[1];
1510 $userid = $matches[2];
1511
1512 //extra security check - the grade item must be in this tree
1513 if (!$item_el = $this->locate_element('i'.$itemid)) {
1514 return null;
1515 }
1516
1517 // $gradea->id may be null - means does not exist yet
1518 $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid));
1519
1520 $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
1521 return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade');
1522
1523 } else if (strpos($eid, 'g') === 0) {
cf72e2dd 1524 $id = (int) substr($eid, 1);
e98871a2 1525 if (!$grade = grade_grade::fetch(array('id'=>$id))) {
1526 return null;
1527 }
1528 //extra security check - the grade item must be in this tree
1529 if (!$item_el = $this->locate_element('i'.$grade->itemid)) {
1530 return null;
1531 }
1532 $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
1533 return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade');
1534 }
1535
1536 // it is a category or item
6391ebe7 1537 foreach ($this->elements as $element) {
6cc3e350 1538 if ($element['eid'] == $eid) {
1539 return $element;
1540 }
e98871a2 1541 }
6cc3e350 1542
1543 return null;
e98871a2 1544 }
e98871a2 1545}
1546
7a6b7acf 1547/**
1548 * This class represents a complete tree of categories, grade_items and final grades,
1549 * organises as an array primarily, but which can also be converted to other formats.
1550 * It has simple method calls with complex implementations, allowing for easy insertion,
1551 * deletion and moving of items and categories within the tree.
cf72e2dd 1552 *
1553 * @uses grade_structure
1554 * @package moodlecore
1555 * @copyright 2009 Nicolas Connault
1556 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
7a6b7acf 1557 */
6cc3e350 1558class grade_tree extends grade_structure {
7a6b7acf 1559
1560 /**
1561 * The basic representation of the tree as a hierarchical, 3-tiered array.
1562 * @var object $top_element
1563 */
d24832f9 1564 public $top_element;
7a6b7acf 1565
7a6b7acf 1566 /**
1567 * 2D array of grade items and categories
cf72e2dd 1568 * @var array $levels
7a6b7acf 1569 */
d24832f9 1570 public $levels;
7a6b7acf 1571
b89a70ce 1572 /**
1573 * Grade items
cf72e2dd 1574 * @var array $items
b89a70ce 1575 */
d24832f9 1576 public $items;
b89a70ce 1577
7a6b7acf 1578 /**
1579 * Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item
e98871a2 1580 * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed.
cf72e2dd 1581 *
1582 * @param int $courseid The Course ID
1583 * @param bool $fillers include fillers and colspans, make the levels var "rectangular"
1584 * @param bool $category_grade_last category grade item is the last child
4faf5f99 1585 * @param array $collapsed array of collapsed categories
cf72e2dd 1586 * @param bool $nooutcomes Whether or not outcomes should be included
7a6b7acf 1587 */
cf72e2dd 1588 public function grade_tree($courseid, $fillers=true, $category_grade_last=false,
1589 $collapsed=null, $nooutcomes=false) {
7a6b7acf 1590 global $USER, $CFG;
1591
1592 $this->courseid = $courseid;
7a6b7acf 1593 $this->levels = array();
2cc773f5 1594 $this->context = get_context_instance(CONTEXT_COURSE, $courseid);
7a6b7acf 1595
1596 // get course grade tree
1597 $this->top_element = grade_category::fetch_course_tree($courseid, true);
1598
4faf5f99 1599 // collapse the categories if requested
1600 if (!empty($collapsed)) {
1601 grade_tree::category_collapse($this->top_element, $collapsed);
1602 }
1603
aea4df41 1604 // no otucomes if requested
1605 if (!empty($nooutcomes)) {
1606 grade_tree::no_outcomes($this->top_element);
1607 }
1608
4faf5f99 1609 // move category item to last position in category
7a6b7acf 1610 if ($category_grade_last) {
1611 grade_tree::category_grade_last($this->top_element);
1612 }
1613
1614 if ($fillers) {
1615 // inject fake categories == fillers
1616 grade_tree::inject_fillers($this->top_element, 0);
1617 // add colspans to categories and fillers
1618 grade_tree::inject_colspans($this->top_element);
1619 }
1620
1621 grade_tree::fill_levels($this->levels, $this->top_element, 0);
d297269d 1622
7a6b7acf 1623 }
1624
4faf5f99 1625 /**
1626 * Static recursive helper - removes items from collapsed categories
cf72e2dd 1627 *
1628 * @param array &$element The seed of the recursion
4faf5f99 1629 * @param array $collapsed array of collapsed categories
cf72e2dd 1630 *
4faf5f99 1631 * @return void
1632 */
d24832f9 1633 public function category_collapse(&$element, $collapsed) {
4faf5f99 1634 if ($element['type'] != 'category') {
1635 return;
1636 }
1637 if (empty($element['children']) or count($element['children']) < 2) {
1638 return;
1639 }
1640
384960dd 1641 if (in_array($element['object']->id, $collapsed['aggregatesonly'])) {
4faf5f99 1642 $category_item = reset($element['children']); //keep only category item
1643 $element['children'] = array(key($element['children'])=>$category_item);
1644
1645 } else {
384960dd 1646 if (in_array($element['object']->id, $collapsed['gradesonly'])) { // Remove category item
1647 reset($element['children']);
1648 $first_key = key($element['children']);
1649 unset($element['children'][$first_key]);
1650 }
1651 foreach ($element['children'] as $sortorder=>$child) { // Recurse through the element's children
4faf5f99 1652 grade_tree::category_collapse($element['children'][$sortorder], $collapsed);
1653 }
1654 }
1655 }
7a6b7acf 1656
aea4df41 1657 /**
1658 * Static recursive helper - removes all outcomes
cf72e2dd 1659 *
1660 * @param array &$element The seed of the recursion
1661 *
aea4df41 1662 * @return void
1663 */
d24832f9 1664 public function no_outcomes(&$element) {
aea4df41 1665 if ($element['type'] != 'category') {
1666 return;
1667 }
1668 foreach ($element['children'] as $sortorder=>$child) {
1669 if ($element['children'][$sortorder]['type'] == 'item'
1670 and $element['children'][$sortorder]['object']->is_outcome_item()) {
1671 unset($element['children'][$sortorder]);
1672
d4795a07 1673 } else if ($element['children'][$sortorder]['type'] == 'category') {
aea4df41 1674 grade_tree::no_outcomes($element['children'][$sortorder]);
1675 }
1676 }
1677 }
1678
7a6b7acf 1679 /**
1680 * Static recursive helper - makes the grade_item for category the last children
cf72e2dd 1681 *
1682 * @param array &$element The seed of the recursion
1683 *
7a6b7acf 1684 * @return void
1685 */
d24832f9 1686 public function category_grade_last(&$element) {
7a6b7acf 1687 if (empty($element['children'])) {
1688 return;
1689 }
1690 if (count($element['children']) < 2) {
1691 return;
1692 }
3e0e2436 1693 $first_item = reset($element['children']);
4a3dfd9a 1694 if ($first_item['type'] == 'categoryitem' or $first_item['type'] == 'courseitem') {
3e0e2436 1695 // the category item might have been already removed
1696 $order = key($element['children']);
1697 unset($element['children'][$order]);
1698 $element['children'][$order] =& $first_item;
1699 }
206f9953 1700 foreach ($element['children'] as $sortorder => $child) {
7a6b7acf 1701 grade_tree::category_grade_last($element['children'][$sortorder]);
1702 }
1703 }
1704
1705 /**
1706 * Static recursive helper - fills the levels array, useful when accessing tree elements of one level
cf72e2dd 1707 *
1708 * @param array &$levels The levels of the grade tree through which to recurse
1709 * @param array &$element The seed of the recursion
1710 * @param int $depth How deep are we?
7a6b7acf 1711 * @return void
1712 */
d24832f9 1713 public function fill_levels(&$levels, &$element, $depth) {
7a6b7acf 1714 if (!array_key_exists($depth, $levels)) {
1715 $levels[$depth] = array();
1716 }
1717
1718 // prepare unique identifier
1719 if ($element['type'] == 'category') {
1720 $element['eid'] = 'c'.$element['object']->id;
1721 } else if (in_array($element['type'], array('item', 'courseitem', 'categoryitem'))) {
1722 $element['eid'] = 'i'.$element['object']->id;
b89a70ce 1723 $this->items[$element['object']->id] =& $element['object'];
7a6b7acf 1724 }
1725
1726 $levels[$depth][] =& $element;
1727 $depth++;
1728 if (empty($element['children'])) {
1729 return;
1730 }
1731 $prev = 0;
1732 foreach ($element['children'] as $sortorder=>$child) {
1733 grade_tree::fill_levels($levels, $element['children'][$sortorder], $depth);
1734 $element['children'][$sortorder]['prev'] = $prev;
1735 $element['children'][$sortorder]['next'] = 0;
1736 if ($prev) {
1737 $element['children'][$prev]['next'] = $sortorder;
1738 }
1739 $prev = $sortorder;
1740 }
1741 }
1742
1743 /**
1744 * Static recursive helper - makes full tree (all leafes are at the same level)
cf72e2dd 1745 *
1746 * @param array &$element The seed of the recursion
1747 * @param int $depth How deep are we?
1748 *
1749 * @return int
7a6b7acf 1750 */
d24832f9 1751 public function inject_fillers(&$element, $depth) {
7a6b7acf 1752 $depth++;
1753
1754 if (empty($element['children'])) {
1755 return $depth;
1756 }
1757 $chdepths = array();
1758 $chids = array_keys($element['children']);
1759 $last_child = end($chids);
1760 $first_child = reset($chids);
1761
1762 foreach ($chids as $chid) {
1763 $chdepths[$chid] = grade_tree::inject_fillers($element['children'][$chid], $depth);
1764 }
1765 arsort($chdepths);
1766
1767 $maxdepth = reset($chdepths);
1768 foreach ($chdepths as $chid=>$chd) {
1769 if ($chd == $maxdepth) {
1770 continue;
1771 }
1772 for ($i=0; $i < $maxdepth-$chd; $i++) {
1773 if ($chid == $first_child) {
1774 $type = 'fillerfirst';
1775 } else if ($chid == $last_child) {
1776 $type = 'fillerlast';
1777 } else {
1778 $type = 'filler';
1779 }
1780 $oldchild =& $element['children'][$chid];
cf72e2dd 1781 $element['children'][$chid] = array('object'=>'filler', 'type'=>$type,
1782 'eid'=>'', 'depth'=>$element['object']->depth,
1783 'children'=>array($oldchild));
7a6b7acf 1784 }
1785 }
1786
1787 return $maxdepth;
1788 }
1789
1790 /**
1791 * Static recursive helper - add colspan information into categories
cf72e2dd 1792 *
1793 * @param array &$element The seed of the recursion
1794 *
1795 * @return int
7a6b7acf 1796 */
d24832f9 1797 public function inject_colspans(&$element) {
7a6b7acf 1798 if (empty($element['children'])) {
1799 return 1;
1800 }
1801 $count = 0;
1802 foreach ($element['children'] as $key=>$child) {
1803 $count += grade_tree::inject_colspans($element['children'][$key]);
1804 }
1805 $element['colspan'] = $count;
1806 return $count;
1807 }
1808
1809 /**
1810 * Parses the array in search of a given eid and returns a element object with
1811 * information about the element it has found.
cf72e2dd 1812 * @param int $eid Gradetree Element ID
7a6b7acf 1813 * @return object element
1814 */
d24832f9 1815 public function locate_element($eid) {
d3c3da1b 1816 // it is a grade - construct a new object
1817 if (strpos($eid, 'n') === 0) {
1818 if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) {
1819 return null;
1820 }
1821
1822 $itemid = $matches[1];
1823 $userid = $matches[2];
1824
1825 //extra security check - the grade item must be in this tree
1826 if (!$item_el = $this->locate_element('i'.$itemid)) {
1827 return null;
1828 }
1829
1830 // $gradea->id may be null - means does not exist yet
1831 $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid));
1832
1833 $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
1834 return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade');
1835
1836 } else if (strpos($eid, 'g') === 0) {
cf72e2dd 1837 $id = (int) substr($eid, 1);
7a6b7acf 1838 if (!$grade = grade_grade::fetch(array('id'=>$id))) {
1839 return null;
1840 }
1841 //extra security check - the grade item must be in this tree
1842 if (!$item_el = $this->locate_element('i'.$grade->itemid)) {
1843 return null;
1844 }
1845 $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
1846 return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade');
1847 }
1848
1849 // it is a category or item
1850 foreach ($this->levels as $row) {
1851 foreach ($row as $element) {
1852 if ($element['type'] == 'filler') {
1853 continue;
1854 }
1855 if ($element['eid'] == $eid) {
1856 return $element;
1857 }
1858 }
1859 }
1860
1861 return null;
1862 }
d24832f9 1863
b244b9b7 1864 /**
1865 * Returns a well-formed XML representation of the grade-tree using recursion.
cf72e2dd 1866 *
1867 * @param array $root The current element in the recursion. If null, starts at the top of the tree.
1868 * @param string $tabs The control character to use for tabs
1869 *
b244b9b7 1870 * @return string $xml
1871 */
cf72e2dd 1872 public function exporttoxml($root=null, $tabs="\t") {
b244b9b7 1873 $xml = null;
1874 $first = false;
1875 if (is_null($root)) {
1876 $root = $this->top_element;
1877 $xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
1878 $xml .= "<gradetree>\n";
1879 $first = true;
1880 }
d24832f9 1881
b244b9b7 1882 $type = 'undefined';
1883 if (strpos($root['object']->table, 'grade_categories') !== false) {
1884 $type = 'category';
cf72e2dd 1885 } else if (strpos($root['object']->table, 'grade_items') !== false) {
b244b9b7 1886 $type = 'item';
cf72e2dd 1887 } else if (strpos($root['object']->table, 'grade_outcomes') !== false) {
b244b9b7 1888 $type = 'outcome';
1889 }
d24832f9 1890
b244b9b7 1891 $xml .= "$tabs<element type=\"$type\">\n";
1892 foreach ($root['object'] as $var => $value) {
1893 if (!is_object($value) && !is_array($value) && !empty($value)) {
1894 $xml .= "$tabs\t<$var>$value</$var>\n";
1895 }
1896 }
1897
1898 if (!empty($root['children'])) {
1899 $xml .= "$tabs\t<children>\n";
1900 foreach ($root['children'] as $sortorder => $child) {
1901 $xml .= $this->exportToXML($child, $tabs."\t\t");
1902 }
1903 $xml .= "$tabs\t</children>\n";
1904 }
d24832f9 1905
b244b9b7 1906 $xml .= "$tabs</element>\n";
1907
1908 if ($first) {
1909 $xml .= "</gradetree>";
1910 }
d24832f9 1911
b244b9b7 1912 return $xml;
1913 }
d24832f9 1914
b244b9b7 1915 /**
1916 * Returns a JSON representation of the grade-tree using recursion.
cf72e2dd 1917 *
b244b9b7 1918 * @param array $root The current element in the recursion. If null, starts at the top of the tree.
1919 * @param string $tabs Tab characters used to indent the string nicely for humans to enjoy
cf72e2dd 1920 *
1921 * @return string
b244b9b7 1922 */
cf72e2dd 1923 public function exporttojson($root=null, $tabs="\t") {
b244b9b7 1924 $json = null;
1925 $first = false;
1926 if (is_null($root)) {
1927 $root = $this->top_element;
1928 $first = true;
1929 }
d24832f9 1930
b244b9b7 1931 $name = '';
1932
1933
1934 if (strpos($root['object']->table, 'grade_categories') !== false) {
1935 $name = $root['object']->fullname;
1936 if ($name == '?') {
d24832f9 1937 $name = $root['object']->get_name();
b244b9b7 1938 }
cf72e2dd 1939 } else if (strpos($root['object']->table, 'grade_items') !== false) {
b244b9b7 1940 $name = $root['object']->itemname;
cf72e2dd 1941 } else if (strpos($root['object']->table, 'grade_outcomes') !== false) {
b244b9b7 1942 $name = $root['object']->itemname;
1943 }
d24832f9 1944
b244b9b7 1945 $json .= "$tabs {\n";
1946 $json .= "$tabs\t \"type\": \"{$root['type']}\",\n";
1947 $json .= "$tabs\t \"name\": \"$name\",\n";
1948
1949 foreach ($root['object'] as $var => $value) {
1950 if (!is_object($value) && !is_array($value) && !empty($value)) {
1951 $json .= "$tabs\t \"$var\": \"$value\",\n";
1952 }
1953 }
d24832f9 1954
b244b9b7 1955 $json = substr($json, 0, strrpos($json, ','));
d24832f9 1956
b244b9b7 1957 if (!empty($root['children'])) {
1958 $json .= ",\n$tabs\t\"children\": [\n";
1959 foreach ($root['children'] as $sortorder => $child) {
1960 $json .= $this->exportToJSON($child, $tabs."\t\t");
1961 }
1962 $json = substr($json, 0, strrpos($json, ','));
1963 $json .= "\n$tabs\t]\n";
d24832f9 1964 }
b244b9b7 1965
1966 if ($first) {
1967 $json .= "\n}";
1968 } else {
1969 $json .= "\n$tabs},\n";
1970 }
d24832f9 1971
b244b9b7 1972 return $json;
1973 }
d24832f9 1974
cf72e2dd 1975 /**
1976 * Returns the array of levels
1977 *
1978 * @return array
1979 */
d24832f9 1980 public function get_levels() {
1981 return $this->levels;
1982 }
1983
cf72e2dd 1984 /**
1985 * Returns the array of grade items
1986 *
1987 * @return array
1988 */
d24832f9 1989 public function get_items() {
1990 return $this->items;
1991 }
1992
cf72e2dd 1993 /**
1994 * Returns a specific Grade Item
1995 *
1996 * @param int $itemid The ID of the grade_item object
1997 *
1998 * @return grade_item
1999 */
d24832f9 2000 public function get_item($itemid) {
2001 if (array_key_exists($itemid, $this->items)) {
2002 return $this->items[$itemid];
2003 } else {
2004 return false;
2005 }
2006 }
7a6b7acf 2007}
eef00ade 2008
2009/**
2010 * Local shortcut function for creating an edit/delete button for a grade_* object.
4d27bc79 2011 * @param string $type 'edit' or 'delete'
eef00ade 2012 * @param int $courseid The Course ID
2013 * @param grade_* $object The grade_* object
2014 * @return string html
2015 */
2016function grade_button($type, $courseid, $object) {
2017 global $CFG, $OUTPUT;
2018 if (preg_match('/grade_(.*)/', get_class($object), $matches)) {
2019 $objectidstring = $matches[1] . 'id';
2020 } else {
2021 throw new coding_exception('grade_button() only accepts grade_* objects as third parameter!');
2022 }
2023
2024 $strdelete = get_string('delete');
2025 $stredit = get_string('edit');
2026
eef00ade 2027 if ($type == 'delete') {
8ae8bf8a 2028 $url = new moodle_url('index.php', array('id' => $courseid, $objectidstring => $object->id, 'action' => 'delete', 'sesskey' => sesskey()));
eef00ade 2029 } else if ($type == 'edit') {
8ae8bf8a 2030 $url = new moodle_url('edit.php', array('courseid' => $courseid, 'id' => $object->id));
eef00ade 2031 }
2032
c63923bd 2033 return $OUTPUT->action_icon($url, new pix_icon('t/'.$type, ${'str'.$type}));
eef00ade 2034
2035}
4d5059d4
SH
2036
2037/**
2038 * This method adds settings to the settings block for the grade system and its
2039 * plugins
2040 *
2041 * @global moodle_page $PAGE
2042 */
2043function grade_extend_settings($plugininfo, $courseid) {
2044 global $PAGE;
2045
2046 $gradenode = $PAGE->settingsnav->prepend(get_string('gradeadministration', 'grades'), null, navigation_node::TYPE_CONTAINER);
2047
2048 $strings = array_shift($plugininfo);
2049
2050 if ($reports = grade_helper::get_plugins_reports($courseid)) {
2051 foreach ($reports as $report) {
2052 $gradenode->add($report->string, $report->link, navigation_node::TYPE_SETTING, null, $report->id, new pix_icon('i/report', ''));
2053 }
2054 }
2055
2056 if ($imports = grade_helper::get_plugins_import($courseid)) {
2057 $importnode = $gradenode->add($strings['import'], null, navigation_node::TYPE_CONTAINER);
2058 foreach ($imports as $import) {
2059 $importnode->add($import->string, $import->link, navigation_node::TYPE_SETTING, null, $import->id, new pix_icon('i/restore', ''));
2060 }
2061 }
2062
2063 if ($exports = grade_helper::get_plugins_export($courseid)) {
2064 $exportnode = $gradenode->add($strings['export'], null, navigation_node::TYPE_CONTAINER);
2065 foreach ($exports as $export) {
2066 $exportnode->add($export->string, $export->link, navigation_node::TYPE_SETTING, null, $export->id, new pix_icon('i/backup', ''));
2067 }
2068 }
2069
2070 if ($setting = grade_helper::get_info_manage_settings($courseid)) {
2071 $gradenode->add(get_string('coursegradesettings', 'grades'), $setting->link, navigation_node::TYPE_SETTING, null, $setting->id, new pix_icon('i/settings', ''));
2072 }
2073
2074 if ($preferences = grade_helper::get_plugins_report_preferences($courseid)) {
2075 $preferencesnode = $gradenode->add(get_string('myreportpreferences', 'grades'), null, navigation_node::TYPE_CONTAINER);
2076 foreach ($preferences as $preference) {
2077 $preferencesnode->add($preference->string, $preference->link, navigation_node::TYPE_SETTING, null, $preference->id, new pix_icon('i/settings', ''));
2078 }
2079 }
2080
2081 if ($letters = grade_helper::get_info_letters($courseid)) {
2082 $letters = array_shift($letters);
2083 $gradenode->add($strings['letter'], $letters->link, navigation_node::TYPE_SETTING, null, $letters->id, new pix_icon('i/settings', ''));
2084 }
2085
2086 if ($outcomes = grade_helper::get_info_outcomes($courseid)) {
2087 $outcomes = array_shift($outcomes);
2088 $gradenode->add($strings['outcome'], $outcomes->link, navigation_node::TYPE_SETTING, null, $outcomes->id, new pix_icon('i/outcomes', ''));
2089 }
2090
2091 if ($scales = grade_helper::get_info_scales($courseid)) {
2092 $gradenode->add($strings['scale'], $scales->link, navigation_node::TYPE_SETTING, null, $scales->id, new pix_icon('i/scales', ''));
2093 }
2094
2095 if ($categories = grade_helper::get_info_edit_structure($courseid)) {
2096 $categoriesnode = $gradenode->add(get_string('categoriesanditems','grades'), null, navigation_node::TYPE_CONTAINER);
2097 foreach ($categories as $category) {
2098 $categoriesnode->add($category->string, $category->link, navigation_node::TYPE_SETTING, null, $category->id, new pix_icon('i/report', ''));
2099 }
2100 }
2101
2102 if ($gradenode->contains_active_node()) {
2103 // If the gradenode is active include the settings base node (gradeadministration) in
2104 // the navbar, typcially this is ignored.
2105 $PAGE->navbar->includesettingsbase = true;
2106
2107 // If we can get the course admin node make sure it is closed by default
2108 // as in this case the gradenode will be opened
2109 if ($coursenode = $PAGE->settingsnav->get('courseadmin', navigation_node::TYPE_COURSE)){
2110 $coursenode->make_inactive();
2111 $coursenode->forceopen = false;
2112 }
2113 }
2114}
2115
2116/**
2117 * Grade helper class
2118 *
2119 * This class provides several helpful functions that work irrespective of any
2120 * current state.
2121 *
2122 * @copyright 2010 Sam Hemelryk
2123 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2124 */
2125abstract class grade_helper {
2126 /**
2127 * Cached manage settings info {@see get_info_settings}
2128 * @var grade_plugin_info|false
2129 */
2130 protected static $managesetting = null;
2131 /**
2132 * Cached grade report plugins {@see get_plugins_reports}
2133 * @var array|false
2134 */
2135 protected static $gradereports = null;
2136 /**
2137 * Cached grade report plugins preferences {@see get_info_scales}
2138 * @var array|false
2139 */
2140 protected static $gradereportpreferences = null;
2141 /**
2142 * Cached scale info {@see get_info_scales}
2143 * @var grade_plugin_info|false
2144 */
2145 protected static $scaleinfo = null;
2146 /**
2147 * Cached outcome info {@see get_info_outcomes}
2148 * @var grade_plugin_info|false
2149 */
2150 protected static $outcomeinfo = null;
2151 /**
2152 * Cached info on edit structure {@see get_info_edit_structure}
2153 * @var array|false
2154 */
2155 protected static $edittree = null;
2156 /**
2157 * Cached leftter info {@see get_info_letters}
2158 * @var grade_plugin_info|false
2159 */
2160 protected static $letterinfo = null;
2161 /**
2162 * Cached grade import plugins {@see get_plugins_import}
2163 * @var array|false
2164 */
2165 protected static $importplugins = null;
2166 /**
2167 * Cached grade export plugins {@see get_plugins_export}
2168 * @var array|false
2169 */
2170 protected static $exportplugins = null;
2171 /**
2172 * Cached grade plugin strings
2173 * @var array
2174 */
2175 protected static $pluginstrings = null;
2176
2177 /**
2178 * Gets strings commonly used by the describe plugins
2179 *
2180 * report => get_string('view'),
2181 * edittree => get_string('edittree', 'grades'),
2182 * scale => get_string('scales'),
2183 * outcome => get_string('outcomes', 'grades'),
2184 * letter => get_string('letters', 'grades'),
2185 * export => get_string('export', 'grades'),
2186 * import => get_string('import'),
2187 * preferences => get_string('mypreferences', 'grades'),
2188 * settings => get_string('settings')
2189 *
2190 * @return array
2191 */
2192 public static function get_plugin_strings() {
2193 if (self::$pluginstrings === null) {
2194 self::$pluginstrings = array(
2195 'report' => get_string('view'),
2196 'edittree' => get_string('edittree', 'grades'),
2197 'scale' => get_string('scales'),
2198 'outcome' => get_string('outcomes', 'grades'),
2199 'letter' => get_string('letters', 'grades'),
2200 'export' => get_string('export', 'grades'),
2201 'import' => get_string('import'),
2202 'preferences' => get_string('mypreferences', 'grades'),
2203 'settings' => get_string('settings')
2204 );
2205 }
2206 return self::$pluginstrings;
2207 }
2208 /**
2209 * Get grade_plugin_info object for managing settings if the user can
2210 *
2211 * @param int $courseid
2212 * @return grade_plugin_info
2213 */
2214 public static function get_info_manage_settings($courseid) {
2215 if (self::$managesetting !== null) {
2216 return self::$managesetting;
2217 }
2218 $context = get_context_instance(CONTEXT_COURSE, $courseid);
2219 if (has_capability('moodle/course:update', $context)) {
2220 self::$managesetting = new grade_plugin_info('coursesettings', new moodle_url('/grade/edit/settings/index.php', array('id'=>$courseid)), get_string('course'));
2221 } else {
2222 self::$managesetting = false;
2223 }
2224 return self::$managesetting;
2225 }
2226 /**
2227 * Returns an array of plugin reports as grade_plugin_info objects
2228 *
2229 * @param int $courseid
2230 * @return array
2231 */
2232 public static function get_plugins_reports($courseid) {
f7fcf4cd
AD
2233 global $SITE;
2234
4d5059d4
SH
2235 if (self::$gradereports !== null) {
2236 return self::$gradereports;
2237 }
2238 $context = get_context_instance(CONTEXT_COURSE, $courseid);
2239 $gradereports = array();
2240 $gradepreferences = array();
2241 foreach (get_plugin_list('gradereport') as $plugin => $plugindir) {
f7fcf4cd
AD
2242 //some reports make no sense if we're not within a course
2243 if ($courseid==$SITE->id && ($plugin=='grader' || $plugin=='user')) {
2244 continue;
2245 }
2246
4d5059d4
SH
2247 // Remove ones we can't see
2248 if (!has_capability('gradereport/'.$plugin.':view', $context)) {
2249 continue;
2250 }
2251
b5e7b2bf 2252 $pluginstr = get_string('pluginname', 'gradereport_'.$plugin);
4d5059d4
SH
2253 $url = new moodle_url('/grade/report/'.$plugin.'/index.php', array('id'=>$courseid));
2254 $gradereports[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr);
2255
2256 // Add link to preferences tab if such a page exists
2257 if (file_exists($plugindir.'/preferences.php')) {
2258 $url = new moodle_url('/grade/report/'.$plugin.'/preferences.php', array('id'=>$courseid));
2259 $gradepreferences[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr);
2260 }
2261 }
2262 if (count($gradereports) == 0) {
2263 $gradereports = false;
2264 $gradepreferences = false;
2265 } else if (count($gradepreferences) == 0) {
2266 $gradepreferences = false;
2267 asort($gradereports);
2268 } else {
2269 asort($gradereports);
2270 asort($gradepreferences);
2271 }
2272 self::$gradereports = $gradereports;
2273 self::$gradereportpreferences = $gradepreferences;
2274 return self::$gradereports;
2275 }
2276 /**
2277 * Returns an array of grade plugin report preferences for plugin reports that
2278 * support preferences
2279 * @param int $courseid
2280 * @return array
2281 */
2282 public static function get_plugins_report_preferences($courseid) {
2283 if (self::$gradereportpreferences !== null) {
2284 return self::$gradereportpreferences;
2285 }
2286 self::get_plugins_reports($courseid);
2287 return self::$gradereportpreferences;
2288 }
2289 /**
2290 * Get information on scales
2291 * @param int $courseid
2292 * @return grade_plugin_info
2293 */
2294 public static function get_info_scales($courseid) {
2295 if (self::$scaleinfo !== null) {
2296 return self::$scaleinfo;
2297 }
2298 if (has_capability('moodle/course:managescales', get_context_instance(CONTEXT_COURSE, $courseid))) {
2299 $url = new moodle_url('/grade/edit/scale/index.php', array('id'=>$courseid));
2300 self::$scaleinfo = new grade_plugin_info('scale', $url, get_string('view'));
2301 } else {
2302 self::$scaleinfo = false;
2303 }
2304 return self::$scaleinfo;
2305 }
2306 /**
2307 * Get information on outcomes
2308 * @param int $courseid
2309 * @return grade_plugin_info
2310 */
2311 public static function get_info_outcomes($courseid) {
f7fcf4cd 2312 global $CFG, $SITE;
4d5059d4
SH
2313
2314 if (self::$outcomeinfo !== null) {
2315 return self::$outcomeinfo;
2316 }
2317 $context = get_context_instance(CONTEXT_COURSE, $courseid);
2318 $canmanage = has_capability('moodle/grade:manage', $context);
2319 $canupdate = has_capability('moodle/course:update', $context);
2320 if (!empty($CFG->enableoutcomes) && ($canmanage || $canupdate)) {
2321 $outcomes = array();
2322 if ($canupdate) {
f7fcf4cd
AD
2323 if ($courseid!=$SITE->id) {
2324 $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid));
2325 $outcomes['course'] = new grade_plugin_info('course', $url, get_string('outcomescourse', 'grades'));
2326 }
4d5059d4
SH
2327 $url = new moodle_url('/grade/edit/outcome/index.php', array('id'=>$courseid));
2328 $outcomes['edit'] = new grade_plugin_info('edit', $url, get_string('editoutcomes', 'grades'));
c46aeeab
DC
2329 $url = new moodle_url('/grade/edit/outcome/import.php', array('courseid'=>$courseid));
2330 $outcomes['import'] = new grade_plugin_info('import', $url, get_string('importoutcomes', 'grades'));
4d5059d4 2331 } else {
f7fcf4cd
AD
2332 if ($courseid!=$SITE->id) {
2333 $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid));
2334 $outcomes['edit'] = new grade_plugin_info('edit', $url, get_string('outcomescourse', 'grades'));
2335 }
4d5059d4
SH
2336 }
2337 self::$outcomeinfo = $outcomes;
2338 } else {
2339 self::$outcomeinfo = false;
2340 }
2341 return self::$outcomeinfo;
2342 }
2343 /**
2344 * Get information on editing structures
2345 * @param int $courseid
2346 * @return array
2347 */
2348 public static function get_info_edit_structure($courseid) {
2349 if (self::$edittree !== null) {
2350 return self::$edittree;
2351 }
2352 if (has_capability('moodle/grade:manage', get_context_instance(CONTEXT_COURSE, $courseid))) {
2353 $url = new moodle_url('/grade/edit/tree/index.php', array('sesskey'=>sesskey(), 'showadvanced'=>'0', 'id'=>$courseid));
2354 self::$edittree = array(
2355 'simpleview' => new grade_plugin_info('simpleview', $url, get_string('simpleview', 'grades')),
2356 'fullview' => new grade_plugin_info('fullview', new moodle_url($url, array('showadvanced'=>'1')), get_string('fullview', 'grades'))
2357 );
2358 } else {
2359 self::$edittree = false;
2360 }
2361 return self::$edittree;
2362 }
2363 /**
2364 * Get information on letters
2365 * @param int $courseid
4d27bc79 2366 * @return array
4d5059d4
SH
2367 */
2368 public static function get_info_letters($courseid) {
2369 if (self::$letterinfo !== null) {
2370 return self::$letterinfo;
2371 }
2372 $context = get_context_instance(CONTEXT_COURSE, $courseid);
2373 $canmanage = has_capability('moodle/grade:manage', $context);
2374 $canmanageletters = has_capability('moodle/grade:manageletters', $context);
2375 if ($canmanage || $canmanageletters) {
2376 self::$letterinfo = array(
54caa598
AD
2377 'view' => new grade_plugin_info('view', new moodle_url('/grade/edit/letter/index.php', array('id'=>$context->id)), get_string('view')),
2378 'edit' => new grade_plugin_info('edit', new moodle_url('/grade/edit/letter/index.php', array('edit'=>1,'id'=>$context->id)), get_string('edit'))
4d5059d4
SH
2379 );
2380 } else {
2381 self::$letterinfo = false;
2382 }
2383 return self::$letterinfo;
2384 }
2385 /**
2386 * Get information import plugins
2387 * @param int $courseid
2388 * @return array
2389 */
2390 public static function get_plugins_import($courseid) {
2391 global $CFG;
2392
2393 if (self::$importplugins !== null) {
2394 return self::$importplugins;
2395 }
2396 $importplugins = array();
2397 $context = get_context_instance(CONTEXT_COURSE, $courseid);
2398
2399 if (has_capability('moodle/grade:import', $context)) {
2400 foreach (get_plugin_list('gradeimport') as $plugin => $plugindir) {
2401 if (!has_capability('gradeimport/'.$plugin.':view', $context)) {
2402 continue;
2403 }
b5e7b2bf 2404 $pluginstr = get_string('pluginname', 'gradeimport_'.$plugin);
4d5059d4
SH
2405 $url = new moodle_url('/grade/import/'.$plugin.'/index.php', array('id'=>$courseid));
2406 $importplugins[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr);
2407 }
2408
2409
2410 if ($CFG->gradepublishing) {
2411 $url = new moodle_url('/grade/import/keymanager.php', array('id'=>$courseid));
2412 $importplugins['keymanager'] = new grade_plugin_info('keymanager', $url, get_string('keymanager', 'grades'));
2413 }
2414 }
2415
2416 if (count($importplugins) > 0) {
2417 asort($importplugins);
2418 self::$importplugins = $importplugins;
2419 } else {
2420 self::$importplugins = false;
2421 }
2422 return self::$importplugins;
2423 }
2424 /**
2425 * Get information export plugins
2426 * @param int $courseid
2427 * @return array
2428 */
2429 public static function get_plugins_export($courseid) {
2430 global $CFG;
2431
2432 if (self::$exportplugins !== null) {
2433 return self::$exportplugins;
2434 }
2435 $context = get_context_instance(CONTEXT_COURSE, $courseid);
2436 $exportplugins = array();
2437 if (has_capability('moodle/grade:export', $context)) {
2438 foreach (get_plugin_list('gradeexport') as $plugin => $plugindir) {
2439 if (!has_capability('gradeexport/'.$plugin.':view', $context)) {
2440 continue;
2441 }
b5e7b2bf 2442 $pluginstr = get_string('pluginname', 'gradeexport_'.$plugin);
4d5059d4
SH
2443 $url = new moodle_url('/grade/export/'.$plugin.'/index.php', array('id'=>$courseid));
2444 $exportplugins[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr);
2445 }
2446
2447 if ($CFG->gradepublishing) {
2448 $url = new moodle_url('/grade/export/keymanager.php', array('id'=>$courseid));
2449 $exportplugins['keymanager'] = new grade_plugin_info('keymanager', $url, get_string('keymanager', 'grades'));
2450 }
2451 }
2452 if (count($exportplugins) > 0) {
2453 asort($exportplugins);
2454 self::$exportplugins = $exportplugins;
2455 } else {
2456 self::$exportplugins = false;
2457 }
2458 return self::$exportplugins;
2459 }
d4dcfc6b 2460}