3 require_once $CFG->libdir
.'/gradelib.php';
7 * This class iterates over all users that are graded in a course.
8 * Returns fetailed info about users and their grades.
10 class graded_users_iterator
{
20 * @param $coruse object
21 * @param array grade_items array of grade items, if not specified only user info returned
22 * @param int $groupid iterate only group users if present
24 function graded_users_iterator($course, $grade_items=null, $groupid=0) {
25 $this->course
= $course;
26 $this->grade_items
= $grade_items;
27 $this->groupid
= $groupid;
29 $this->gradestack
= array();
33 * Initialise the iterator
34 * @return boolean success
41 grade_regrade_final_grades($this->course
->id
);
42 $course_item = grade_item
::fetch_course_item($this->course
->id
);
43 if ($course_item->needsupdate
) {
44 // can not calculate all final grades - sorry
48 if (strpos($CFG->gradebookroles
, ',') !== false) {
49 $gradebookroles = " = {$CFG->gradebookroles}";
51 $gradebookroles = " IN ({$CFG->gradebookroles})";
54 $relatedcontexts = get_related_contexts_string(get_context_instance(CONTEXT_COURSE
, $this->course
->id
));
57 $groupsql = "INNER JOIN {$CFG->prefix}groups_members gm ON gm.userid = u.id";
58 $groupwheresql = "AND gm.groupid = {$this->groupid}";
64 $users_sql = "SELECT u.*
65 FROM {$CFG->prefix}user u
66 INNER JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
68 WHERE ra.roleid $gradebookroles
69 AND ra.contextid $relatedcontexts
72 $this->users_rs
= get_recordset_sql($users_sql);
74 if (!empty($this->grade_items
)) {
75 $itemids = array_keys($this->grade_items
);
76 $itemids = implode(',', $itemids);
78 $grades_sql = "SELECT g.*
79 FROM {$CFG->prefix}grade_grades g
80 INNER JOIN {$CFG->prefix}user u ON g.userid = u.id
81 INNER JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
83 WHERE ra.roleid $gradebookroles
84 AND ra.contextid $relatedcontexts
85 AND g.itemid IN ($itemids)
87 ORDER BY g.userid ASC, g.itemid ASC";
88 $this->grades_rs
= get_recordset_sql($grades_sql);
95 * Returns information about the next user
96 * @return mixed array of user info, all grades and feedback or null when no more users found
98 function next_user() {
99 if (!$this->users_rs
or !$this->users_rs
->RecordCount()) {
100 return false; // no users present
103 if (!$user = rs_fetch_next_record($this->users_rs
)) {
104 return false; // no more users
107 //find the first grade of this user
108 $grade_records = array();
110 if (!$current = $this->_pop()) {
111 break; // no more grades
114 if ($current->userid
< $user->id
) {
115 // this should not happen, could be caused by concurrent updates - skip this record
118 } else if ($current->userid
> $user->id
) {
119 // this user does not have any more grades
120 $this->_push($current);
124 $grade_records[$current->itemid
] = $current;
128 $feedbacks = array();
130 foreach ($this->grade_items
as $grade_item) {
131 if (array_key_exists($grade_item->id
, $grade_records)) {
132 $feedbacks[$grade_item->id
]->feedback
= $grade_records[$grade_item->id
]->feedback
;
133 $feedbacks[$grade_item->id
]->feedbackformat
= $grade_records[$grade_item->id
]->feedbackformat
;
134 unset($grade_records[$grade_item->id
]->feedback
);
135 unset($grade_records[$grade_item->id
]->feedbackformat
);
136 $grades[$grade_item->id
] = new grade_grade($grade_records[$grade_item->id
], false);
138 $feedbacks[$grade_item->id
]->feedback
= '';
139 $feedbacks[$grade_item->id
]->feedbackformat
= FORMAT_MOODLE
;
140 $grades[$grade_item->id
] = new grade_grade(array('userid'=>$user->id
, 'itemid'=>$grade_item->id
), false);
144 $result = new object();
145 $result->user
= $user;
146 $result->grades
= $grades;
147 $result->feedbacks
= $feedbacks;
153 * Close the iterator, do not forget to call this function.
157 if ($this->users_rs
) {
158 rs_close($this->users_rs
);
159 $this->users_rs
= null;
161 if ($this->grades_rs
) {
162 rs_close($this->grades_rs
);
163 $this->grades_rs
= null;
165 $this->gradestack
= array();
171 function _push($grade) {
172 array_push($this->gradestack
, $grade);
179 if (empty($this->gradestack
)) {
180 if (!$this->grades_rs
or !$this->grades_rs
->RecordCount()) {
181 return NULL; // no grades present
184 if (!$grade = rs_fetch_next_record($this->grades_rs
)) {
185 return NULL; // no more grades
190 return array_pop($this->gradestack
);
196 * Print grading plugin selection popup form.
198 * @param int $courseid id of course
199 * @param string $active_type type of plugin on current page - import, export, report or edit
200 * @param string $active_plugin active plugin type - grader, user, cvs, ...
201 * @param boolean $return return as string
202 * @return nothing or string if $return true
204 function print_grade_plugin_selector($courseid, $active_type, $active_plugin, $return=false) {
207 $context = get_context_instance(CONTEXT_COURSE
, $courseid);
213 /// report plugins with its special structure
214 if ($reports = get_list_of_plugins('grade/report', 'CVS')) { // Get all installed reports
215 foreach ($reports as $key => $plugin) { // Remove ones we can't see
216 if (!has_capability('gradereport/'.$plugin.':view', $context)) {
217 unset($reports[$key]);
221 $reportnames = array();
222 if (!empty($reports)) {
223 foreach ($reports as $plugin) {
224 $url = 'report/'.$plugin.'/index.php?id='.$courseid;
225 if ($active_type == 'report' and $active_plugin == $plugin ) {
228 $reportnames[$url] = get_string('modulename', 'gradereport_'.$plugin, NULL, $CFG->dirroot
.'/grade/report/'.$plugin.'/lang/');
232 if (!empty($reportnames)) {
233 $menu['reportgroup']='--'.get_string('reportplugins', 'grades');
234 $menu = $menu+
$reportnames;
237 /// standard import plugins
238 if ($imports = get_list_of_plugins('grade/import', 'CVS')) { // Get all installed import plugins
239 foreach ($imports as $key => $plugin) { // Remove ones we can't see
240 if (!has_capability('gradeimport/'.$plugin.':view', $context)) {
241 unset($imports[$key]);
245 $importnames = array();
246 if (!empty($imports)) {
247 foreach ($imports as $plugin) {
248 $url = 'import/'.$plugin.'/index.php?id='.$courseid;
249 if ($active_type == 'import' and $active_plugin == $plugin ) {
252 $importnames[$url] = get_string('modulename', 'gradeimport_'.$plugin, NULL, $CFG->dirroot
.'/grade/import/'.$plugin.'/lang/');
256 if (!empty($importnames)) {
257 $menu['importgroup']='--'.get_string('importplugins', 'grades');
258 $menu = $menu+
$importnames;
261 /// standard export plugins
262 if ($exports = get_list_of_plugins('grade/export', 'CVS')) { // Get all installed export plugins
263 foreach ($exports as $key => $plugin) { // Remove ones we can't see
264 if (!has_capability('gradeexport/'.$plugin.':view', $context)) {
265 unset($exports[$key]);
269 $exportnames = array();
270 if (!empty($exports)) {
271 foreach ($exports as $plugin) {
272 $url = 'export/'.$plugin.'/index.php?id='.$courseid;
273 if ($active_type == 'export' and $active_plugin == $plugin ) {
276 $exportnames[$url] = get_string('modulename', 'gradeexport_'.$plugin, NULL, $CFG->dirroot
.'/grade/export/'.$plugin.'/lang/');
280 if (!empty($exportnames)) {
281 $menu['exportgroup']='--'.get_string('exportplugins', 'grades');
282 $menu = $menu+
$exportnames;
285 /// editing scripts - not real plugins
286 if (has_capability('moodle/grade:manage', $context)
287 or has_capability('moodle/grade:manageletters', $context)
288 or has_capability('moodle/course:managescales', $context)
289 or has_capability('moodle/course:update', $context)) {
290 $menu['edit']='--'.get_string('edit');
292 if (has_capability('moodle/grade:manage', $context)) {
293 $url = 'edit/tree/index.php?id='.$courseid;
294 if ($active_type == 'edit' and $active_plugin == 'tree' ) {
297 $menu[$url] = get_string('edittree', 'grades');
300 if (has_capability('moodle/course:managescales', $context)) {
301 $url = 'edit/scale/index.php?id='.$courseid;
302 if ($active_type == 'edit' and $active_plugin == 'scale' ) {
305 $menu[$url] = get_string('scales');
308 if (!empty($CFG->enableoutcomes
) && (has_capability('moodle/grade:manage', $context) or
309 has_capability('moodle/course:update', $context))) {
310 if (has_capability('moodle/course:update', $context)) { // Default to course assignment
311 $url = 'edit/outcome/course.php?id='.$courseid;
313 $url = 'edit/outcome/index.php?id='.$courseid;
315 if ($active_type == 'edit' and $active_plugin == 'outcome' ) {
318 $menu[$url] = get_string('outcomes', 'grades');
321 if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:manageletters', $context)) {
322 $url = 'edit/letter/index.php?id='.$courseid;
323 if ($active_type == 'edit' and $active_plugin == 'letter' ) {
326 $menu[$url] = get_string('letters', 'grades');
329 if (has_capability('moodle/grade:manage', $context)) {
330 $url = 'edit/settings/index.php?id='.$courseid;
331 if ($active_type == 'edit' and $active_plugin == 'settings' ) {
334 $menu[$url] = get_string('coursesettings', 'grades');
339 /// finally print/return the popup form
340 return popup_form($CFG->wwwroot
.'/grade/', $menu, 'choosepluginreport', $active, 'choose', '', '', $return, 'self', get_string('view'));
344 * Utility class used for return tracking when using edit and other forms in grade plugins
346 class grade_plugin_return
{
355 * @param array $params - associative array with return parameters, if null parameter are taken from _GET or _POST
357 function grade_plugin_return ($params=null) {
358 if (empty($params)) {
359 $this->type
= optional_param('gpr_type', null, PARAM_SAFEDIR
);
360 $this->plugin
= optional_param('gpr_plugin', null, PARAM_SAFEDIR
);
361 $this->courseid
= optional_param('gpr_courseid', null, PARAM_INT
);
362 $this->userid
= optional_param('gpr_userid', null, PARAM_INT
);
363 $this->page
= optional_param('gpr_page', null, PARAM_INT
);
366 foreach ($params as $key=>$value) {
367 if (array_key_exists($key, $this)) {
368 $this->$key = $value;
375 * Returns return parameters as options array suitable for buttons.
376 * @return array options
378 function get_options() {
379 if (empty($this->type
)) {
385 if (!empty($this->plugin
)) {
386 $params['plugin'] = $this->plugin
;
389 if (!empty($this->courseid
)) {
390 $params['id'] = $this->courseid
;
393 if (!empty($this->userid
)) {
394 $params['userid'] = $this->userid
;
397 if (!empty($this->page
)) {
398 $params['page'] = $this->page
;
406 * @param string $default default url when params not set
409 function get_return_url($default, $extras=null) {
412 if (empty($this->type
) or empty($this->plugin
)) {
416 $url = $CFG->wwwroot
.'/grade/'.$this->type
.'/'.$this->plugin
.'/index.php';
419 if (!empty($this->courseid
)) {
420 $url .= $glue.'id='.$this->courseid
;
424 if (!empty($this->userid
)) {
425 $url .= $glue.'userid='.$this->userid
;
429 if (!empty($this->page
)) {
430 $url .= $glue.'page='.$this->page
;
434 if (!empty($extras)) {
435 foreach($extras as $key=>$value) {
436 $url .= $glue.$key.'='.$value;
445 * Returns string with hidden return tracking form elements.
448 function get_form_fields() {
449 if (empty($this->type
)) {
453 $result = '<input type="hidden" name="gpr_type" value="'.$this->type
.'" />';
455 if (!empty($this->plugin
)) {
456 $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin
.'" />';
459 if (!empty($this->courseid
)) {
460 $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid
.'" />';
463 if (!empty($this->userid
)) {
464 $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid
.'" />';
467 if (!empty($this->page
)) {
468 $result .= '<input type="hidden" name="gpr_page" value="'.$this->page
.'" />';
473 * Add hidden elements into mform
474 * @param object $mform moodle form object
477 function add_mform_elements(&$mform) {
478 if (empty($this->type
)) {
482 $mform->addElement('hidden', 'gpr_type', $this->type
);
483 $mform->setType('gpr_type', PARAM_SAFEDIR
);
485 if (!empty($this->plugin
)) {
486 $mform->addElement('hidden', 'gpr_plugin', $this->plugin
);
487 $mform->setType('gpr_plugin', PARAM_SAFEDIR
);
490 if (!empty($this->courseid
)) {
491 $mform->addElement('hidden', 'gpr_courseid', $this->courseid
);
492 $mform->setType('gpr_courseid', PARAM_INT
);
495 if (!empty($this->userid
)) {
496 $mform->addElement('hidden', 'gpr_userid', $this->userid
);
497 $mform->setType('gpr_userid', PARAM_INT
);
500 if (!empty($this->page
)) {
501 $mform->addElement('hidden', 'gpr_page', $this->page
);
502 $mform->setType('gpr_page', PARAM_INT
);
507 * Add return tracking params into url
509 * @return string $url with erturn tracking params
511 function add_url_params($url) {
512 if (empty($this->type
)) {
516 if (strpos($url, '?') === false) {
517 $url .= '?gpr_type='.$this->type
;
519 $url .= '&gpr_type='.$this->type
;
522 if (!empty($this->plugin
)) {
523 $url .= '&gpr_plugin='.$this->plugin
;
526 if (!empty($this->courseid
)) {
527 $url .= '&gpr_courseid='.$this->courseid
;
530 if (!empty($this->userid
)) {
531 $url .= '&gpr_userid='.$this->userid
;
534 if (!empty($this->page
)) {
535 $url .= '&gpr_page='.$this->page
;
543 * Function central to gradebook for building and printing the navigation (breadcrumb trail).
544 * @param string $path The path of the calling script (using __FILE__?)
545 * @param string $pagename The language string to use as the last part of the navigation (non-link)
546 * @param mixed $id Either a plain integer (assuming the key is 'id') or an array of keys and values (e.g courseid => $courseid, itemid...)
549 function grade_build_nav($path, $pagename=null, $id=null) {
550 global $CFG, $COURSE;
552 $strgrades = get_string('grades', 'grades');
554 // Parse the path and build navlinks from its elements
555 $dirroot_length = strlen($CFG->dirroot
) +
1; // Add 1 for the first slash
556 $path = substr($path, $dirroot_length);
557 $path = str_replace('\\', '/', $path);
559 $path_elements = explode('/', $path);
561 $path_elements_count = count($path_elements);
563 // First link is always 'grade'
565 $navlinks[] = array('name' => $strgrades,
566 'link' => $CFG->wwwroot
.'/grade/index.php?id='.$COURSE->id
,
570 $numberofelements = 3;
572 // Prepare URL params string
576 foreach ($id as $idkey => $idvalue) {
577 $id_string .= "$idkey=$idvalue&";
580 $id_string .= "id=$id";
586 // Remove file extensions from filenames
587 foreach ($path_elements as $key => $filename) {
588 $path_elements[$key] = str_replace('.php', '', $filename);
591 // Second level links
592 switch ($path_elements[1]) {
593 case 'edit': // No link
594 if ($path_elements[3] != 'index.php') {
595 $numberofelements = 4;
598 case 'import': // No link
600 case 'export': // No link
603 // $id is required for this link. Do not print it if $id isn't given
605 $link = $CFG->wwwroot
. '/grade/report/index.php' . $id_string;
608 if ($path_elements[2] == 'grader') {
609 $numberofelements = 4;
614 // If this element isn't among the ones already listed above, it isn't supported, throw an error.
615 debugging("grade_build_nav() doesn't support ". $path_elements[1] . " as the second path element after 'grade'.");
619 $navlinks[] = array('name' => get_string($path_elements[1], 'grades'), 'link' => $link, 'type' => 'misc');
622 if (empty($pagename)) {
623 $pagename = get_string($path_elements[2], 'grades');
626 switch ($numberofelements) {
628 $navlinks[] = array('name' => $pagename, 'link' => $link, 'type' => 'misc');
632 if ($path_elements[2] == 'grader' AND $path_elements[3] != 'index.php') {
633 $navlinks[] = array('name' => get_string('modulename', 'gradereport_grader'),
634 'link' => "$CFG->wwwroot/grade/report/grader/index.php$id_string",
637 $navlinks[] = array('name' => $pagename, 'link' => '', 'type' => 'misc');
640 $navigation = build_navigation($navlinks);
646 * Computes then returns the percentage value of the grade value within the given range.
647 * @param float $gradeval
648 * @param float $grademin
649 * @param float $grademx
650 * @return float $percentage
652 function grade_to_percentage($gradeval, $grademin, $grademax) {
653 if ($grademin >= $grademax) {
654 debugging("The minimum grade ($grademin) was higher than or equal to the maximum grade ($grademax)!!! Cannot proceed with computation.");
657 // If given gradevalue is lower than grademin, adjust it to grademin
658 if ($gradeval < $grademin ||
empty($gradeval)) {
659 $gradeval = $grademin;
662 $offset_value = $gradeval - $grademin;
663 $offset_max = $grademax - $grademin;
664 $factor = 100 / $offset_max;
665 $percentage = $offset_value * $factor;
670 * Flat structure similar to grade tree.
675 * A string of GET URL variables, namely courseid and sesskey, used in most URLs built by this class.
676 * @var string $commonvars
681 * 1D array of grade items only
691 * Constructor, retrieves and stores array of all grade_category and grade_item
692 * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed.
693 * @param int $courseid
694 * @param boolean $category_grade_last category grade item is the last child
695 * @param array $collapsed array of collapsed categories
697 function grade_seq($courseid, $category_grade_last=false, $nooutcomes=false) {
700 $this->courseid
= $courseid;
701 $this->commonvars
= "&sesskey=$USER->sesskey&id=$this->courseid";
702 $this->context
= get_context_instance(CONTEXT_COURSE
, $courseid);
704 // get course grade tree
705 $top_element = grade_category
::fetch_course_tree($courseid, true);
707 $this->items
= grade_seq
::flatten($top_element, $category_grade_last, $nooutcomes);
711 * Static recursive helper - makes the grade_item for category the last children
713 * @param array $element The seed of the recursion
716 function flatten(&$element, $category_grade_last, $nooutcomes) {
717 if (empty($element['children'])) {
722 foreach ($element['children'] as $sortorder=>$unused) {
723 if ($nooutcomes and $element['type'] != 'category' and $element['children'][$sortorder]['object']->is_outcome_item()) {
726 $children[] = $element['children'][$sortorder];
728 unset($element['children']);
730 if ($category_grade_last and count($children) > 1) {
731 $cat_item = array_shift($children);
732 array_push($children, $cat_item);
736 foreach ($children as $child) {
737 if ($child['type'] == 'category') {
738 $result = array_merge($result, grade_seq
::flatten($child, $category_grade_last, $nooutcomes));
740 $child['eid'] = 'i'.$child['object']->id
;
749 * Parses the array in search of a given eid and returns a element object with
750 * information about the element it has found.
752 * @return object element
754 function locate_element($eid) {
755 // it is a grade - construct a new object
756 if (strpos($eid, 'n') === 0) {
757 if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) {
761 $itemid = $matches[1];
762 $userid = $matches[2];
764 //extra security check - the grade item must be in this tree
765 if (!$item_el = $this->locate_element('i'.$itemid)) {
769 // $gradea->id may be null - means does not exist yet
770 $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid));
772 $grade->grade_item
=& $item_el['object']; // this may speedup grade_grade methods!
773 return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade');
775 } else if (strpos($eid, 'g') === 0) {
776 $id = (int)substr($eid, 1);
777 if (!$grade = grade_grade
::fetch(array('id'=>$id))) {
780 //extra security check - the grade item must be in this tree
781 if (!$item_el = $this->locate_element('i'.$grade->itemid
)) {
784 $grade->grade_item
=& $item_el['object']; // this may speedup grade_grade methods!
785 return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade');
788 // it is a category or item
789 foreach ($this->items
as $element) {
790 if ($element['eid'] == $eid) {
799 * Returns the grade eid - the grade may not exist yet.
800 * @param $grade_grade object
803 function get_grade_eid($grade_grade) {
804 if (empty($grade_grade->id
)) {
805 return 'n'.$grade_grade->itemid
.'u'.$grade_grade->userid
;
807 return 'g'.$grade_grade->id
;
814 * This class represents a complete tree of categories, grade_items and final grades,
815 * organises as an array primarily, but which can also be converted to other formats.
816 * It has simple method calls with complex implementations, allowing for easy insertion,
817 * deletion and moving of items and categories within the tree.
822 * The basic representation of the tree as a hierarchical, 3-tiered array.
823 * @var object $top_element
828 * A string of GET URL variables, namely courseid and sesskey, used in most URLs built by this class.
829 * @var string $commonvars
834 * 2D array of grade items and categories
844 * Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item
845 * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed.
846 * @param int $courseid
847 * @param boolean $fillers include fillers and colspans, make the levels var "rectangular"
848 * @param boolean $category_grade_last category grade item is the last child
849 * @param array $collapsed array of collapsed categories
851 function grade_tree($courseid, $fillers=true, $category_grade_last=false, $collapsed=null, $nooutcomes=false) {
854 $this->courseid
= $courseid;
855 $this->commonvars
= "&sesskey=$USER->sesskey&id=$this->courseid";
856 $this->levels
= array();
857 $this->context
= get_context_instance(CONTEXT_COURSE
, $courseid);
859 // get course grade tree
860 $this->top_element
= grade_category
::fetch_course_tree($courseid, true);
862 // collapse the categories if requested
863 if (!empty($collapsed)) {
864 grade_tree
::category_collapse($this->top_element
, $collapsed);
867 // no otucomes if requested
868 if (!empty($nooutcomes)) {
869 grade_tree
::no_outcomes($this->top_element
);
872 // move category item to last position in category
873 if ($category_grade_last) {
874 grade_tree
::category_grade_last($this->top_element
);
878 // inject fake categories == fillers
879 grade_tree
::inject_fillers($this->top_element
, 0);
880 // add colspans to categories and fillers
881 grade_tree
::inject_colspans($this->top_element
);
884 grade_tree
::fill_levels($this->levels
, $this->top_element
, 0);
888 * Static recursive helper - removes items from collapsed categories
890 * @param array $element The seed of the recursion
891 * @param array $collapsed array of collapsed categories
894 function category_collapse(&$element, $collapsed) {
895 if ($element['type'] != 'category') {
898 if (empty($element['children']) or count($element['children']) < 2) {
902 if (in_array($element['object']->id
, $collapsed['aggregatesonly'])) {
903 $category_item = reset($element['children']); //keep only category item
904 $element['children'] = array(key($element['children'])=>$category_item);
907 if (in_array($element['object']->id
, $collapsed['gradesonly'])) { // Remove category item
908 reset($element['children']);
909 $first_key = key($element['children']);
910 unset($element['children'][$first_key]);
912 foreach ($element['children'] as $sortorder=>$child) { // Recurse through the element's children
913 grade_tree
::category_collapse($element['children'][$sortorder], $collapsed);
919 * Static recursive helper - removes all outcomes
921 * @param array $element The seed of the recursion
924 function no_outcomes(&$element) {
925 if ($element['type'] != 'category') {
928 foreach ($element['children'] as $sortorder=>$child) {
929 if ($element['children'][$sortorder]['type'] == 'item'
930 and $element['children'][$sortorder]['object']->is_outcome_item()) {
931 unset($element['children'][$sortorder]);
933 } else if ($element['children'][$sortorder]['type'] == 'category') {
934 grade_tree
::no_outcomes($element['children'][$sortorder]);
940 * Static recursive helper - makes the grade_item for category the last children
942 * @param array $element The seed of the recursion
945 function category_grade_last(&$element) {
946 if (empty($element['children'])) {
949 if (count($element['children']) < 2) {
952 $first_item = reset($element['children']);
953 if ($first_item['type'] == 'categoryitem' or $first_item['type'] == 'courseitem') {
954 // the category item might have been already removed
955 $order = key($element['children']);
956 unset($element['children'][$order]);
957 $element['children'][$order] =& $first_item;
959 foreach ($element['children'] as $sortorder => $child) {
960 grade_tree
::category_grade_last($element['children'][$sortorder]);
965 * Static recursive helper - fills the levels array, useful when accessing tree elements of one level
968 * @param array $element The seed of the recursion
972 function fill_levels(&$levels, &$element, $depth) {
973 if (!array_key_exists($depth, $levels)) {
974 $levels[$depth] = array();
977 // prepare unique identifier
978 if ($element['type'] == 'category') {
979 $element['eid'] = 'c'.$element['object']->id
;
980 } else if (in_array($element['type'], array('item', 'courseitem', 'categoryitem'))) {
981 $element['eid'] = 'i'.$element['object']->id
;
984 $levels[$depth][] =& $element;
986 if (empty($element['children'])) {
990 foreach ($element['children'] as $sortorder=>$child) {
991 grade_tree
::fill_levels($levels, $element['children'][$sortorder], $depth);
992 $element['children'][$sortorder]['prev'] = $prev;
993 $element['children'][$sortorder]['next'] = 0;
995 $element['children'][$prev]['next'] = $sortorder;
1002 * Static recursive helper - makes full tree (all leafes are at the same level)
1004 function inject_fillers(&$element, $depth) {
1007 if (empty($element['children'])) {
1010 $chdepths = array();
1011 $chids = array_keys($element['children']);
1012 $last_child = end($chids);
1013 $first_child = reset($chids);
1015 foreach ($chids as $chid) {
1016 $chdepths[$chid] = grade_tree
::inject_fillers($element['children'][$chid], $depth);
1020 $maxdepth = reset($chdepths);
1021 foreach ($chdepths as $chid=>$chd) {
1022 if ($chd == $maxdepth) {
1025 for ($i=0; $i < $maxdepth-$chd; $i++
) {
1026 if ($chid == $first_child) {
1027 $type = 'fillerfirst';
1028 } else if ($chid == $last_child) {
1029 $type = 'fillerlast';
1033 $oldchild =& $element['children'][$chid];
1034 $element['children'][$chid] = array('object'=>'filler', 'type'=>$type, 'eid'=>'', 'depth'=>$element['object']->depth
,'children'=>array($oldchild));
1042 * Static recursive helper - add colspan information into categories
1044 function inject_colspans(&$element) {
1045 if (empty($element['children'])) {
1049 foreach ($element['children'] as $key=>$child) {
1050 $count +
= grade_tree
::inject_colspans($element['children'][$key]);
1052 $element['colspan'] = $count;
1057 * Parses the array in search of a given eid and returns a element object with
1058 * information about the element it has found.
1060 * @return object element
1062 function locate_element($eid) {
1063 // it is a grade - construct a new object
1064 if (strpos($eid, 'n') === 0) {
1065 if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) {
1069 $itemid = $matches[1];
1070 $userid = $matches[2];
1072 //extra security check - the grade item must be in this tree
1073 if (!$item_el = $this->locate_element('i'.$itemid)) {
1077 // $gradea->id may be null - means does not exist yet
1078 $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid));
1080 $grade->grade_item
=& $item_el['object']; // this may speedup grade_grade methods!
1081 return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade');
1083 } else if (strpos($eid, 'g') === 0) {
1084 $id = (int)substr($eid, 1);
1085 if (!$grade = grade_grade
::fetch(array('id'=>$id))) {
1088 //extra security check - the grade item must be in this tree
1089 if (!$item_el = $this->locate_element('i'.$grade->itemid
)) {
1092 $grade->grade_item
=& $item_el['object']; // this may speedup grade_grade methods!
1093 return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade');
1096 // it is a category or item
1097 foreach ($this->levels
as $row) {
1098 foreach ($row as $element) {
1099 if ($element['type'] == 'filler') {
1102 if ($element['eid'] == $eid) {
1112 * Returns the grade eid - the grade may not exist yet.
1113 * @param $grade_grade object
1114 * @return string eid
1116 function get_grade_eid($grade_grade) {
1117 if (empty($grade_grade->id
)) {
1118 return 'n'.$grade_grade->itemid
.'u'.$grade_grade->userid
;
1120 return 'g'.$grade_grade->id
;
1125 * Return edit icon for give element
1126 * @param object $element
1129 function get_edit_icon($element, $gpr) {
1132 if (!has_capability('moodle/grade:manage', $this->context
)) {
1133 if ($element['type'] == 'grade' and has_capability('moodle/grade:edit', $this->context
)) {
1134 // oki - let them override grade
1140 static $stredit = null;
1141 if (is_null($stredit)) {
1142 $stredit = get_string('edit');
1145 $object = $element['object'];
1148 switch ($element['type']) {
1150 case 'categoryitem':
1152 if (empty($object->outcomeid
) ||
empty($CFG->enableoutcomes
)) {
1153 $url = $CFG->wwwroot
.'/grade/edit/tree/item.php?courseid='.$this->courseid
.'&id='.$object->id
;
1155 $url = $CFG->wwwroot
.'/grade/edit/tree/outcomeitem.php?courseid='.$this->courseid
.'&id='.$object->id
;
1157 $url = $gpr->add_url_params($url);
1161 $url = $CFG->wwwroot
.'/grade/edit/tree/category.php?courseid='.$this->courseid
.'&id='.$object->id
;
1162 $url = $gpr->add_url_params($url);
1166 if (empty($object->id
)) {
1167 $url = $CFG->wwwroot
.'/grade/edit/tree/grade.php?courseid='.$this->courseid
.'&itemid='.$object->itemid
.'&userid='.$object->userid
;
1169 $url = $CFG->wwwroot
.'/grade/edit/tree/grade.php?courseid='.$this->courseid
.'&id='.$object->id
;
1171 $url = $gpr->add_url_params($url);
1172 if (!empty($object->feedback
)) {
1173 $feedback = format_text($object->feedback
, $object->feedbackformat
);
1174 $function = "return overlib('".s(ltrim($object->feedback
)."', FULLHTML);");
1175 $overlib = 'onmouseover="'.$function.'" onmouseout="return nd();"';
1184 return '<a href="'.$url.'"><img '.$overlib.' src="'.$CFG->pixpath
.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" title="'.$stredit.'"/></a>';
1192 * Return hiding icon for give element
1193 * @param object $element
1196 function get_hiding_icon($element, $gpr) {
1199 if (!has_capability('moodle/grade:manage', $this->context
) and !has_capability('moodle/grade:hide', $this->context
)) {
1203 static $strshow = null;
1204 static $strhide = null;
1205 if (is_null($strshow)) {
1206 $strshow = get_string('show');
1207 $strhide = get_string('hide');
1210 if ($element['object']->is_hidden()) {
1212 $tooltip = $strshow;
1214 if ($element['type'] != 'category' and $element['object']->get_hidden() > 1) { // Change the icon and add a tooltip showing the date
1215 $icon = 'hiddenuntil';
1216 $tooltip = get_string('hiddenuntildate', 'grades', userdate($element['object']->get_hidden()));
1219 $url = $CFG->wwwroot
.'/grade/edit/tree/action.php?id='.$this->courseid
.'&action=show&sesskey='.sesskey()
1220 . '&eid='.$element['eid'];
1221 $url = $gpr->add_url_params($url);
1222 $action = '<a href="'.$url.'"><img alt="'.$strshow.'" src="'.$CFG->pixpath
.'/t/'.$icon.'.gif" class="iconsmall" title="'.$tooltip.'"/></a>';
1225 $url = $CFG->wwwroot
.'/grade/edit/tree/action.php?id='.$this->courseid
.'&action=hide&sesskey='.sesskey()
1226 . '&eid='.$element['eid'];
1227 $url = $gpr->add_url_params($url);
1228 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath
.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" title="'.$strhide.'"/></a>';
1234 * Return locking icon for give element
1235 * @param object $element
1238 function get_locking_icon($element, $gpr) {
1241 static $strunlock = null;
1242 static $strlock = null;
1243 if (is_null($strunlock)) {
1244 $strunlock = get_string('unlock', 'grades');
1245 $strlock = get_string('lock', 'grades');
1248 if ($element['object']->is_locked()) {
1250 $tooltip = $strunlock;
1252 if ($element['type'] != 'category' and $element['object']->get_locktime() > 1) { // Change the icon and add a tooltip showing the date
1254 $tooltip = get_string('locktimedate', 'grades', userdate($element['object']->get_locktime()));
1257 if (!has_capability('moodle/grade:manage', $this->context
) and !has_capability('moodle/grade:unlock', $this->context
)) {
1260 $url = $CFG->wwwroot
.'/grade/edit/tree/action.php?id='.$this->courseid
.'&action=unlock&sesskey='.sesskey()
1261 . '&eid='.$element['eid'];
1262 $url = $gpr->add_url_params($url);
1263 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath
.'/t/'.$icon.'.gif" alt="'.$strunlock.'" class="iconsmall" title="'.$tooltip.'"/></a>';
1266 if (!has_capability('moodle/grade:manage', $this->context
) and !has_capability('moodle/grade:lock', $this->context
)) {
1269 $url = $CFG->wwwroot
.'/grade/edit/tree/action.php?id='.$this->courseid
.'&action=lock&sesskey='.sesskey()
1270 . '&eid='.$element['eid'];
1271 $url = $gpr->add_url_params($url);
1272 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath
.'/t/lock.gif" class="iconsmall" alt="'.$strlock.'" title="'
1273 . $strlock.'"/></a>';
1279 * Return calculation icon for given element
1280 * @param object $element
1283 function get_calculation_icon($element, $gpr) {
1285 if (!has_capability('moodle/grade:manage', $this->context
)) {
1289 $calculation_icon = '';
1291 $type = $element['type'];
1292 $object = $element['object'];
1294 if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') {
1295 $streditcalculation = get_string('editcalculation', 'grades');
1297 // show calculation icon only when calculation possible
1298 if ((!$object->is_normal_item() or $object->is_outcome_item())
1299 and ($object->gradetype
== GRADE_TYPE_SCALE
or $object->gradetype
== GRADE_TYPE_VALUE
)) {
1300 $url = $CFG->wwwroot
.'/grade/edit/tree/calculation.php?courseid='.$this->courseid
.'&id='.$object->id
;
1301 $url = $gpr->add_url_params($url);
1302 $calculation_icon = '<a href="'. $url.'"><img src="'.$CFG->pixpath
.'/t/calc.gif" class="iconsmall" alt="'
1303 . $streditcalculation.'" title="'.$streditcalculation.'" /></a>'. "\n";
1307 return $calculation_icon;