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_MODULE
, $id));
17 $group = get_current_group($course->id
);
19 print_header("$course->shortname: ".get_string('grades'), $course->fullname
, grade_nav($course, $action));
21 grade_preferences_menu($action, $course, $group);
23 grade_set_uncategorized();
26 /// We are in editing mode. First, process any inputs there may be.
28 if ($data = data_submitted()) {
29 // make sure it is safe to process data
30 if (!empty($USER->id
)) {
31 if (!confirm_sesskey()) {
32 error(get_string('confirmsesskeybad', 'error'));
36 if (!empty($data->nonmembersadd
)) { /// Add people to a grade_item
37 if (!empty($data->nonmembers
) and !empty($data->grade_itemid
)) {
38 $grade_itemmodified = false;
39 foreach ($data->nonmembers
as $userid) {
40 $record->courseid
= $course->id
;
41 $record->grade_itemid
= $data->grade_itemid
;
42 $record->userid
= $userid;
43 if (!record_exists('grade_exceptions','courseid',$course->id
,'grade_itemid',$data->grade_itemid
,'userid',$userid)) {
44 if (!insert_record('grade_exceptions', $record)) {
45 notify(get_string('addexceptionerror','grades').$userid.':'.$data->grade_itemid
);
48 $grade_itemmodified = true;
51 $selectedgrade_item = $data->grade_itemid
;
53 } else if (!empty($data->membersremove
)) { /// Remove selected people from a particular grade_item
54 if (!empty($data->members
) and !empty($data->grade_itemid
)) {
55 foreach ($data->members
as $userid) {
56 delete_records('grade_exceptions', 'userid', $userid, "grade_itemid", $data->grade_itemid
, 'courseid', $course->id
);
59 $selectedgrade_item = $data->grade_itemid
;
63 /// Calculate data ready to create the editing interface
65 $strgradeitemnonmembers = get_string('gradeitemnonmembers','grades');
66 $strgradeitemmembersselected = get_string('gradeitemmembersselected','grades');
67 $strgradeitemremovemembers = get_string('gradeitemremovemembers','grades');
68 $strgradeiteminfomembers = get_string('gradeiteminfomembers','grades');
69 $strgradeitemadd = get_string('gradeitemadd','grades');
70 $strgradeitemremove = get_string('gradeitemremove','grades');
71 $strgradeiteminfo = get_string('gradeiteminfo','grades');
72 $strgradeiteminfopeople = get_string('gradeiteminfopeople','grades');
73 $strgradeitemrandomassign = get_string('gradeitemrandomassign','grades');
74 $strgradeitemaddusers = get_string('gradeitemaddusers','grades');
75 $strgradeitems = get_string('gradeitems','grades');
76 $courseid = $course->id
;
77 $listgrade_items = array();
78 $listmembers = array();
79 $nonmembers = array();
80 $grade_items = array();
81 $grade_items = get_records('grade_item', 'courseid', $course->id
);
82 $grade_itemcount = count($grade_items);
84 // we need to create a multidimensional array keyed by grade_itemid with all_students at each level
85 if (isset($grade_items)) {
86 foreach($grade_items as $grade_item) {
87 $nonmembers[$grade_item->id
] = array();
88 if ($students = get_course_students($course->id
)) {
89 foreach ($students as $student) {
90 $nonmembers[$grade_item->id
][$student->id
] = fullname($student, true);
99 foreach ($grade_items as $grade_item) {
100 $modname = get_record('modules', 'id', $grade_item->modid
);
101 $itemname = get_record($modname->name
, 'id', $grade_item->cminstance
, 'course', $course->id
);
102 $grade_item->name
= $itemname->name
;
104 $listmembers[$grade_item->id
] = array();
105 if ($grade_itemexceptions = grade_get_grade_item_exceptions($grade_item->id
)) {
106 foreach ($grade_itemexceptions as $grade_itemexception) {
107 $listmembers[$grade_item->id
][$grade_itemexception->userid
] = $nonmembers[$grade_item->id
][$grade_itemexception->userid
];
108 unset($nonmembers[$grade_item->id
][$grade_itemexception->userid
]);
112 $listgrade_items[$grade_item->id
] = strip_tags(format_string($grade_item->name
,true))." ($countusers)";
113 if (!isset($grade_item->name
)) {
114 // these were items that have since been deleted
115 unset($listgrade_items[$grade_item->id
]);
116 delete_records('grade_item', 'id', $grade_item->id
);
117 delete_records('grade_exceptions', 'grade_itemid', $grade_item->id
, 'courseid', $course->id
);
119 natcasesort($listmembers[$grade_item->id
]);
120 natcasesort($nonmembers[$grade_item->id
]);
122 natcasesort($listgrade_items);
125 if (empty($selectedgrade_item)) { // Choose the first group by default
126 $selectedgrade_item = array_shift($temparr = array_keys($listgrade_items));
129 include('exceptions.html');
131 print_footer($course);
134 function grade_get_grade_item_exceptions($id) {
136 global $CFG, $course;
138 $contextlists = get_related_contexts_string(get_context_instance(CONTEXT_COURSE
, $course->id
));
140 $sql = "SELECT ge.id, ge.userid
141 FROM {$CFG->prefix}grade_exceptions ge,
142 {$CFG->prefix}role_assignments ra
143 WHERE grade_itemid = $id
144 AND ge.userid = ra.userid
145 AND ra.contextid $contextlists";
147 return get_records_sql($sql);