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 // TODO: fix capabilities check
17 // TODO: add proper check that grade is editable
18 require_login($course);
19 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
20 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');
53 $mform = new edit_grade_form(null, array('grade_item'=>$grade_item, 'gpr'=>$gpr));
55 if ($grade = get_record('grade_grades', 'itemid', $id, 'userid', $userid)) {
56 if ($grade_text = get_record('grade_grades_text', 'gradeid', $grade->id
)) {
57 // always clean existing feedback - grading should not have XSS risk
58 if (can_use_html_editor()) {
59 $options = new object();
60 $options->smiley
= false;
61 $options->filter
= false;
62 $options->noclean
= false;
63 $grade->feedback
= format_text($grade_text->feedback
, $grade_text->feedbackformat
, $options);
64 $grade->feedbackformat
= FORMAT_HTML
;
66 $grade->feedback
= clean_text($grade_text->feedback
, $grade_text->feedbackformat
);
67 $grade->feedbackformat
= $grade_text->feedbackformat
;
71 $grade->locked
= $grade->locked
> 0 ?
1:0;
72 $grade->overridden
= $grade->overridden
> 0 ?
1:0;
73 $grade->excluded
= $grade->excluded
> 0 ?
1:0;
75 $mform->set_data($grade);
78 $mform->set_data(array('itemid'=>$itemid, 'userid'=>$userid));
81 if ($mform->is_cancelled()) {
85 } else if ($data = $mform->get_data()) {
86 $old_grade_grade = new grade_grade(array('userid'=>$data->userid
, 'itemid'=>$grade_item->id
), true); //might not exist yet
88 // update final grade or feedback
89 $grade_item->update_final_grade($data->userid
, $data->finalgrade
, NULL, 'editgrade', $data->feedback
, $data->feedbackformat
);
91 $grade_grade = grade_grade
::fetch(array('userid'=>$data->userid
, 'itemid'=>$grade_item->id
));
93 $grade_grade->set_hidden($data->hidden
); // TODO: this is wrong!
95 // ignore overridden flag when changing final grade
96 if ($old_grade_grade->finalgrade
== $grade_grade->finalgrade
) {
97 if ($grade_grade->set_overridden($data->overridden
) and empty($data->overridden
)) {
98 $grade_item->force_regrading(); // force regrading only when clearing the flag
102 if ($grade_grade->set_excluded($data->excluded
)) {
103 $grade_item->force_regrading();
106 $grade_grade->set_locked($data->locked
);
107 $grade_grade->set_locktime($data->locktime
);
109 redirect($returnurl);
112 $strgrades = get_string('grades');
113 $strgraderreport = get_string('graderreport', 'grades');
114 $strgradeedit = get_string('editgrade', 'grades');
115 $struser = get_string('user');
117 $nav = array(array('name'=>$strgrades,'link'=>$CFG->wwwroot
.'/grade/index.php?id='.$courseid, 'type'=>'misc'),
118 array('name'=>$strgradeedit, 'link'=>'', 'type'=>'misc'));
120 $navigation = build_navigation($nav);
122 /*********** BEGIN OUTPUT *************/
124 print_header_simple($strgrades . ': ' . $strgraderreport . ': ' . $strgradeedit,
125 ': ' . $strgradeedit , $navigation, '', '', true, '', navmenu($course));
127 print_heading($strgradeedit);
129 print_simple_box_start("center");
131 // Form if in edit or add modes
134 print_simple_box_end();
136 print_footer($course);