MDL-9137 Added the overview report
[moodle-pu.git] / grade / exceptions.php
blob6a6b39eb80703e370f217a8c19956d58f143d6df
1 <?php // $Id$
3 require_once("../config.php");
4 require_once("lib.php");
6 $id = required_param('id'); // course id
7 $action = optional_param('action', '');
9 if (!$course = get_record('course', 'id', $id)) {
10 error('No course ID');
13 require_login($course->id);
15 require_capability('moodle/course:managegrades', get_context_instance(CONTEXT_COURSE, $id));
17 print_header("$course->shortname: ".get_string('grades'), $course->fullname, grade_nav($course, $action));
19 $groupmode = groupmode($course);
20 $group = setup_and_print_groups($course, $groupmode, 'exceptions.php?id=' . $course->id);
21 echo '<div class="clearer"></div>';
23 grade_preferences_menu($action, $course);
25 grade_set_uncategorized();
28 /// We are in editing mode. First, process any inputs there may be.
30 if ($data = data_submitted()) {
31 // make sure it is safe to process data
32 if (!empty($USER->id)) {
33 if (!confirm_sesskey()) {
34 error(get_string('confirmsesskeybad', 'error'));
38 if (!empty($data->nonmembersadd)) { /// Add people to a grade_item
39 if (!empty($data->nonmembers) and !empty($data->grade_itemid)) {
40 $grade_itemmodified = false;
41 foreach ($data->nonmembers as $userid) {
42 $record->courseid = $course->id;
43 $record->grade_itemid = $data->grade_itemid;
44 $record->userid = $userid;
45 if (!record_exists('grade_exceptions','courseid',$course->id,'grade_itemid',$data->grade_itemid,'userid',$userid)) {
46 if (!insert_record('grade_exceptions', $record)) {
47 notify(get_string('addexceptionerror','grades').$userid.':'.$data->grade_itemid);
50 $grade_itemmodified = true;
53 $selectedgrade_item = $data->grade_itemid;
55 } else if (!empty($data->membersremove)) { /// Remove selected people from a particular grade_item
56 if (!empty($data->members) and !empty($data->grade_itemid)) {
57 foreach ($data->members as $userid) {
58 delete_records('grade_exceptions', 'userid', $userid, "grade_itemid", $data->grade_itemid, 'courseid', $course->id);
61 $selectedgrade_item = $data->grade_itemid;
65 /// Calculate data ready to create the editing interface
67 $strgradeitemnonmembers = get_string('gradeitemnonmembers','grades');
68 $strgradeitemmembersselected = get_string('gradeitemmembersselected','grades');
69 $strgradeitemremovemembers = get_string('gradeitemremovemembers','grades');
70 $strgradeiteminfomembers = get_string('gradeiteminfomembers','grades');
71 $strgradeitemadd = get_string('gradeitemadd','grades');
72 $strgradeitemremove = get_string('gradeitemremove','grades');
73 $strgradeiteminfo = get_string('gradeiteminfo','grades');
74 $strgradeiteminfopeople = get_string('gradeiteminfopeople','grades');
75 $strgradeitemrandomassign = get_string('gradeitemrandomassign','grades');
76 $strgradeitemaddusers = get_string('gradeitemaddusers','grades');
77 $strgradeitems = get_string('gradeitems','grades');
78 $courseid = $course->id;
79 $listgrade_items = array();
80 $listmembers = array();
81 $nonmembers = array();
82 $grade_items = array();
83 $grade_items = get_records('grade_item', 'courseid', $course->id);
84 $grade_itemcount = count($grade_items);
86 // we need to create a multidimensional array keyed by grade_itemid with all_students at each level
87 if (isset($grade_items)) {
88 foreach($grade_items as $grade_item) {
89 $nonmembers[$grade_item->id] = array();
90 if ($students = get_course_students($course->id)) {
91 foreach ($students as $student) {
92 $nonmembers[$grade_item->id][$student->id] = fullname($student, true);
94 unset($students);
100 if ($grade_items) {
101 foreach ($grade_items as $grade_item) {
102 $modname = get_record('modules', 'id', $grade_item->modid);
103 $itemname = get_record($modname->name, 'id', $grade_item->cminstance, 'course', $course->id);
104 $grade_item->name = $itemname->name;
105 $countusers = 0;
106 $listmembers[$grade_item->id] = array();
107 if ($grade_itemexceptions = grade_get_grade_item_exceptions($grade_item->id)) {
108 foreach ($grade_itemexceptions as $grade_itemexception) {
109 $listmembers[$grade_item->id][$grade_itemexception->userid] = $nonmembers[$grade_item->id][$grade_itemexception->userid];
110 unset($nonmembers[$grade_item->id][$grade_itemexception->userid]);
111 $countusers++;
114 $listgrade_items[$grade_item->id] = strip_tags(format_string($grade_item->name,true))." ($countusers)";
115 if (!isset($grade_item->name)) {
116 // these were items that have since been deleted
117 unset($listgrade_items[$grade_item->id]);
118 delete_records('grade_item', 'id', $grade_item->id);
119 delete_records('grade_exceptions', 'grade_itemid', $grade_item->id, 'courseid', $course->id);
121 natcasesort($listmembers[$grade_item->id]);
122 natcasesort($nonmembers[$grade_item->id]);
124 natcasesort($listgrade_items);
127 if (empty($selectedgrade_item)) { // Choose the first group by default
128 $selectedgrade_item = array_shift($temparr = array_keys($listgrade_items));
131 include('exceptions.html');
133 print_footer($course);
134 exit;
136 function grade_get_grade_item_exceptions($id) {
138 global $CFG, $course;
140 $contextlists = get_related_contexts_string(get_context_instance(CONTEXT_COURSE, $course->id));
142 $sql = "SELECT ge.id, ge.userid
143 FROM {$CFG->prefix}grade_exceptions ge,
144 {$CFG->prefix}role_assignments ra
145 WHERE grade_itemid = $id
146 AND ge.userid = ra.userid
147 AND ra.contextid $contextlists";
149 return get_records_sql($sql);