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 if (!isteacher($course->id
)) {
16 error(get_string('notteachererror', 'grades'));
19 $group = get_current_group($course->id
);
21 print_header("$course->shortname: ".get_string('grades'), "$course->fullname", grade_nav($course, $action));
23 grade_preferences_menu($action, $course, $group);
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);
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
;
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
]);
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);
136 function grade_get_grade_item_exceptions($id) {
140 $sql = "SELECT ge.id, ge.userid FROM {$CFG->prefix}grade_exceptions ge, {$CFG->prefix}user_students us WHERE us.course=$course->id AND grade_itemid=$id AND ge.userid = us.userid AND us.course=ge.courseid";
141 $grade_exceptions = get_records_sql($sql);
142 return $grade_exceptions;