3 require_once '../../../config.php';
4 require_once $CFG->dirroot
.'/grade/lib.php';
5 require_once 'grade_form.php';
7 $courseid = required_param('courseid', PARAM_INT
);
8 $id = optional_param('id', 0, PARAM_INT
);
9 $itemid = optional_param('itemid', 0, PARAM_INT
);
10 $userid = optional_param('userid', 0, PARAM_INT
);
12 if (!$course = get_record('course', 'id', $courseid)) {
13 print_error('nocourseid');
16 require_login($course);
17 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
18 if (!has_capability('moodle/grade:manage', $context)) {
19 require_capability('moodle/grade:override', $context);
23 $gpr = new grade_plugin_return();
24 $returnurl = $gpr->get_return_url($CFG->wwwroot
.'/grade/report.php?id='.$course->id
);
28 if (!$grade = get_record('grade_grades', 'id', $id)) {
29 error('Incorrect grade id');
32 if (!empty($itemid) and $itemid != $grade->itemid
) {
33 error('Incorrect itemid');
35 $itemid = $grade->itemid
;
37 if (!empty($userid) and $userid != $grade->userid
) {
38 error('Incorrect userid');
40 $userid = $grade->userid
;
44 } else if (empty($userid) or empty($itemid)) {
45 error('Missing userid and itemid');
48 if (!$grade_item = grade_item
::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) {
49 error('Can not find grade_item');
52 // now verify grading user has access to all groups or is member of the same group when separate groups used in course
53 if (groupmode($COURSE) == SEPARATEGROUPS
and !has_capability('moodle/site:accessallgroups', $context)) {
54 if ($groups = user_group($COURSE->id
, $userid)) {
56 foreach ($groups as $group) {
57 if (groups_is_member($group->id
, $USER->id
)) {
62 error('Can not grade this user');
65 error('Can not grade this user');
69 $mform = new edit_grade_form(null, array('grade_item'=>$grade_item, 'gpr'=>$gpr));
71 if ($grade = get_record('grade_grades', 'itemid', $grade_item->id
, 'userid', $userid)) {
72 if ($grade_text = get_record('grade_grades_text', 'gradeid', $grade->id
)) {
73 // always clean existing feedback - grading should not have XSS risk
74 if (can_use_html_editor()) {
75 $options = new object();
76 $options->smiley
= false;
77 $options->filter
= false;
78 $options->noclean
= false;
79 $grade->feedback
= format_text($grade_text->feedback
, $grade_text->feedbackformat
, $options);
80 $grade->feedbackformat
= FORMAT_HTML
;
82 $grade->feedback
= clean_text($grade_text->feedback
, $grade_text->feedbackformat
);
83 $grade->feedbackformat
= $grade_text->feedbackformat
;
87 $grade->locked
= $grade->locked
> 0 ?
1:0;
88 $grade->overridden
= $grade->overridden
> 0 ?
1:0;
89 $grade->excluded
= $grade->excluded
> 0 ?
1:0;
91 if ($grade->hidden
> 1) {
92 $grade->hiddenuntil
= $grade->hidden
;
95 $grade->hiddenuntil
= 0;
98 if ($grade_item->is_locked()) {
102 $mform->set_data($grade);
105 $mform->set_data(array('itemid'=>$itemid, 'userid'=>$userid, 'locked'=>$grade_item->locked
, 'locktime'=>$grade_item->locktime
));
108 if ($mform->is_cancelled()) {
109 redirect($returnurl);
112 } else if ($data = $mform->get_data(false)) {
113 $old_grade_grade = new grade_grade(array('userid'=>$data->userid
, 'itemid'=>$grade_item->id
), true); //might not exist yet
115 // fix no grade for scales
116 if (!isset($data->finalgrade
)) {
117 $data->finalgrade
= $old_grade_grade->finalgrade
;
119 } else if ($grade_item->gradetype
== GRADE_TYPE_SCALE
and $data->finalgrade
< 1) {
120 $data->finalgrade
= NULL;
123 if (!isset($data->feedback
)) {
124 $data->feedback
= $old_grade_grade->feedback
;
125 $data->feedbackformat
= $old_grade_grade->feedbackformat
;
127 // update final grade or feedback
128 $grade_item->update_final_grade($data->userid
, $data->finalgrade
, NULL, 'editgrade', $data->feedback
, $data->feedbackformat
);
130 $grade_grade = grade_grade
::fetch(array('userid'=>$data->userid
, 'itemid'=>$grade_item->id
));
131 $grade_grade->grade_item
=& $grade_item; // no db fetching
133 if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:hide', $context)) {
134 if (empty($data->hidden
)) {
135 if (empty($data->hiddenuntil
)) {
136 $grade_grade->set_hidden(0);
138 $grade_grade->set_hidden($data->hiddenuntil
);
141 $grade_grade->set_hidden(1);
145 if (isset($data->locked
) and !$grade_item->is_locked()) {
146 if (($old_grade_grade->locked
or $old_grade_grade->locktime
)
147 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:unlock', $context))) {
150 } else if ((!$old_grade_grade->locked
and !$old_grade_grade->locktime
)
151 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:lock', $context))) {
155 $grade_grade->set_locktime($data->locktime
); //set_lock may reset locktime
156 $grade_grade->set_locked($data->locked
, false, true);
160 if (isset($data->excluded
) and has_capability('moodle/grade:manage', $context)) {
161 $grade_grade->set_excluded($data->excluded
);
164 if (isset($data->overridden
) and has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:override', $context)) {
165 // ignore overridden flag when changing final grade
166 if ($old_grade_grade->finalgrade
== $grade_grade->finalgrade
) {
167 $grade_grade->set_overridden($data->overridden
);
171 // detect cases when we need to do full regrading
172 if ($old_grade_grade->excluded
!= $grade_grade->excluded
) {
173 $parent = $grade_item->get_parent_category();
174 $parent->force_regrading();
176 } else if ($old_grade_grade->overridden
!= $grade_grade->overridden
and empty($grade_grade->overridden
)) { // only when unoverriding
177 $grade_item->force_regrading();
179 } else if ($old_grade_grade->locktime
!= $grade_grade->locktime
) {
180 $grade_item->force_regrading();
183 redirect($returnurl);
186 $strgrades = get_string('grades');
187 $strgraderreport = get_string('graderreport', 'grades');
188 $strgradeedit = get_string('editgrade', 'grades');
189 $struser = get_string('user');
191 $navigation = grade_build_nav(__FILE__
, $strgradeedit, array('courseid' => $courseid));
193 /*********** BEGIN OUTPUT *************/
195 print_header_simple($strgrades . ': ' . $strgraderreport . ': ' . $strgradeedit,
196 ': ' . $strgradeedit , $navigation, '', '', true, '', navmenu($course));
198 print_heading($strgradeedit);
200 print_simple_box_start("center");
202 // Form if in edit or add modes
205 print_simple_box_end();
207 print_footer($course);