MDL-10724 Started help strings for category edit form
[moodle.git] / grade / edit / tree / grade.php
CommitLineData
20544755 1<?php //$Id$
739196ba 2
78ad5f3f 3require_once '../../../config.php';
3af29899 4require_once $CFG->dirroot.'/grade/lib.php';
6fb39a98 5require_once 'grade_form.php';
20544755 6
7$courseid = required_param('courseid', PARAM_INT);
23207a1a 8$id = optional_param('id', 0, PARAM_INT);
9$itemid = optional_param('itemid', 0, PARAM_INT);
10$userid = optional_param('userid', 0, PARAM_INT);
20544755 11
12if (!$course = get_record('course', 'id', $courseid)) {
13 print_error('nocourseid');
14}
15
23207a1a 16// TODO: fix capabilities check
17// TODO: add proper check that grade is editable
20544755 18require_login($course);
19$context = get_context_instance(CONTEXT_COURSE, $course->id);
f10fac96 20require_capability('moodle/grade:override', $context);
20544755 21
22// default return url
3af29899 23$gpr = new grade_plugin_return();
24$returnurl = $gpr->get_return_url($CFG->wwwroot.'/grade/report.php?id='.$course->id);
20544755 25
23207a1a 26// security checks!
27if (!empty($id)) {
28 if (!$grade = get_record('grade_grades', 'id', $id)) {
29 error('Incorrect grade id');
30 }
31
32 if (!empty($itemid) and $itemid != $grade->itemid) {
33 error('Incorrect itemid');
34 }
35 $itemid = $grade->itemid;
36
37 if (!empty($userid) and $userid != $grade->userid) {
38 error('Incorrect userid');
39 }
40 $userid = $grade->userid;
79eabc2a 41
23207a1a 42 unset($grade);
9c25957a 43
23207a1a 44} else if (empty($userid) or empty($itemid)) {
45 error('Missing userid and itemid');
46}
47
48if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) {
49 error('Can not find grade_item');
50}
51
52
53$mform = new edit_grade_form(null, array('grade_item'=>$grade_item, 'gpr'=>$gpr));
54
15a51159 55if ($grade = get_record('grade_grades', 'itemid', $grade_item->id, 'userid', $userid)) {
23207a1a 56 if ($grade_text = get_record('grade_grades_text', 'gradeid', $grade->id)) {
57 // always clean existing feedback - grading should not have XSS risk
79eabc2a 58 if (can_use_html_editor()) {
59 $options = new object();
23207a1a 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;
65 } else {
66 $grade->feedback = clean_text($grade_text->feedback, $grade_text->feedbackformat);
67 $grade->feedbackformat = $grade_text->feedbackformat;
79eabc2a 68 }
920a0fb2 69 }
70
15a51159 71 $grade->locked = $grade->locked > 0 ? 1:0;
72 $grade->overridden = $grade->overridden > 0 ? 1:0;
73 $grade->excluded = $grade->excluded > 0 ? 1:0;
74
75 if ($grade->hidden > 1) {
76 $grade->hiddenuntil = $grade->hidden;
77 $grade->hidden = 0;
78 } else {
79 $grade->hiddenuntil = 0;
80 }
23207a1a 81
82 $mform->set_data($grade);
9c25957a 83
20544755 84} else {
23207a1a 85 $mform->set_data(array('itemid'=>$itemid, 'userid'=>$userid));
20544755 86}
87
88if ($mform->is_cancelled()) {
89 redirect($returnurl);
23207a1a 90
20544755 91// form processing
41486d1c 92} else if ($data = $mform->get_data(false)) {
23207a1a 93 $old_grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true); //might not exist yet
920a0fb2 94
23207a1a 95 // update final grade or feedback
96 $grade_item->update_final_grade($data->userid, $data->finalgrade, NULL, 'editgrade', $data->feedback, $data->feedbackformat);
8c4d80f1 97
23207a1a 98 $grade_grade = grade_grade::fetch(array('userid'=>$data->userid, 'itemid'=>$grade_item->id));
8c4d80f1 99
15a51159 100 if (empty($data->hidden)) {
101 if (empty($data->hiddenuntil)) {
102 $grade_grade->set_hidden(0);
103 } else {
104 $grade_grade->set_hidden($data->hiddenuntil);
105 }
106 } else {
107 $grade_grade->set_hidden(1);
108 }
23207a1a 109
110 // ignore overridden flag when changing final grade
111 if ($old_grade_grade->finalgrade == $grade_grade->finalgrade) {
112 if ($grade_grade->set_overridden($data->overridden) and empty($data->overridden)) {
113 $grade_item->force_regrading(); // force regrading only when clearing the flag
114 }
115 }
116
117 if ($grade_grade->set_excluded($data->excluded)) {
118 $grade_item->force_regrading();
119 }
920a0fb2 120
23207a1a 121 $grade_grade->set_locked($data->locked);
3ee5c201 122 $grade_grade->set_locktime($data->locktime);
20544755 123
3af29899 124 redirect($returnurl);
20544755 125}
126
20544755 127$strgrades = get_string('grades');
20544755 128$strgraderreport = get_string('graderreport', 'grades');
23207a1a 129$strgradeedit = get_string('editgrade', 'grades');
130$struser = get_string('user');
20544755 131
826c5f86 132$navigation = grade_build_nav(__FILE__, $strgradeedit, array('courseid' => $courseid));
20544755 133
134/*********** BEGIN OUTPUT *************/
135
23207a1a 136print_header_simple($strgrades . ': ' . $strgraderreport . ': ' . $strgradeedit,
137 ': ' . $strgradeedit , $navigation, '', '', true, '', navmenu($course));
20544755 138
23207a1a 139print_heading($strgradeedit);
20544755 140
141print_simple_box_start("center");
142
20544755 143// Form if in edit or add modes
144$mform->display();
20544755 145
146print_simple_box_end();
147
148print_footer($course);
149die;