3 require_once $CFG->libdir
.'/formslib.php';
5 class edit_grade_form
extends moodleform
{
7 function definition() {
10 $mform =& $this->_form
;
12 $grade_item = $this->_customdata
['grade_item'];
13 $gpr = $this->_customdata
['gpr'];
15 /// information fields
16 $mform->addElement('static', 'user', get_string('user'));
17 $mform->addElement('static', 'itemname', get_string('itemname', 'grades'));
19 /// actual grade - numeric or scale
20 if ($grade_item->gradetype
== GRADE_TYPE_VALUE
) {
22 $mform->addElement('text', 'finalgrade', get_string('finalgrade', 'grades'));
23 $mform->setHelpButton('finalgrade', array(false, get_string('finalgrade', 'grades'),
24 false, true, false, get_string('finalgradehelp', 'grades')));
26 } else if ($grade_item->gradetype
== GRADE_TYPE_SCALE
) {
30 if (empty($grade_item->outcomeid
)) {
31 $scaleopt[-1] = get_string('nograde');
33 $scaleopt[-1] = get_string('nooutcome', 'grades');
37 if ($scale = get_record('scale', 'id', $grade_item->scaleid
)) {
38 foreach (split(",", $scale->scale
) as $option) {
39 $scaleopt[$i] = $option;
44 $mform->addElement('select', 'finalgrade', get_string('finalgrade', 'grades'), $scaleopt);
45 $mform->setHelpButton('finalgrade', array(false, get_string('finalgrade', 'grades'),
46 false, true, false, get_string('finalgradehelp', 'grades')));
49 $mform->addElement('advcheckbox', 'overridden', get_string('overridden', 'grades'));
50 $mform->setHelpButton('overridden', array(false, get_string('overridden', 'grades'),
51 false, true, false, get_string('overriddenhelp', 'grades')));
52 $mform->addElement('advcheckbox', 'excluded', get_string('excluded', 'grades'));
53 $mform->setHelpButton('excluded', array(false, get_string('excluded', 'grades'),
54 false, true, false, get_string('excludedhelp', 'grades')));
57 /// advcheckbox is not compatible with disabledIf !!
58 $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades'));
59 $mform->setHelpButton('hidden', array('hidden', get_string('hidden', 'grades'), 'grade'));
60 $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
61 $mform->setHelpButton('hiddenuntil', array('hiddenuntil', get_string('hiddenuntil', 'grades'), 'grade'));
62 $mform->disabledIf('hidden', 'hiddenuntil[off]', 'notchecked');
65 $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
66 $mform->setHelpButton('locked', array('locked', get_string('locked', 'grades'), 'grade'));
67 $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
68 $mform->setHelpButton('locktime', array('locktime', get_string('locktime', 'grades'), 'grade'));
69 $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE
);
71 // Feedback format is automatically converted to html if user has enabled editor
72 $mform->addElement('htmleditor', 'feedback', get_string('feedback', 'grades'),
73 array('rows'=>'15', 'course'=>$COURSE->id
, 'cols'=>'45'));
74 $mform->setHelpButton('feedback', array(false, get_string('feedback', 'grades'),
75 false, true, false, get_string('feedbackhelp', 'grades')));
76 $mform->setType('text', PARAM_RAW
); // to be cleaned before display, no XSS risk
77 $mform->addElement('format', 'feedbackformat', get_string('format'));
78 $mform->setHelpButton('feedbackformat', array('textformat', get_string('helpformatting')));
81 $mform->addElement('hidden', 'oldgrade');
83 $mform->addElement('hidden', 'id', 0);
84 $mform->setType('id', PARAM_INT
);
86 $mform->addElement('hidden', 'itemid', 0);
87 $mform->setType('itemid', PARAM_INT
);
89 $mform->addElement('hidden', 'userid', 0);
90 $mform->setType('userid', PARAM_INT
);
92 $mform->addElement('hidden', 'courseid', $COURSE->id
);
93 $mform->setType('courseid', PARAM_INT
);
95 /// add return tracking info
96 $gpr->add_mform_elements($mform);
98 //-------------------------------------------------------------------------------
100 $this->add_action_buttons();
103 function definition_after_data() {
104 global $CFG, $COURSE;
106 $context = get_context_instance(CONTEXT_COURSE
, $COURSE->id
);
108 $mform =& $this->_form
;
109 $grade_item = $this->_customdata
['grade_item'];
111 // fill in user name if user still exists
112 $userid = $mform->getElementValue('userid');
113 if ($user = get_record('user', 'id', $userid)) {
114 $username = '<a href="'.$CFG->wwwroot
.'/user/view.php?id='.$userid.'">'.fullname($user).'</a>';
115 $user_el =& $mform->getElement('user');
116 $user_el->setValue($username);
119 // add activity name + link
120 if ($grade_item->itemtype
== 'mod') {
121 $cm = get_coursemodule_from_instance($grade_item->itemmodule
, $grade_item->iteminstance
, $grade_item->courseid
);
122 $itemname = '<a href="'.$CFG->wwwroot
.'/mod/'.$grade_item->itemmodule
.'/view.php?id='.$cm->id
.'">'.$grade_item->get_name().'</a>';
124 $itemname = $grade_item->get_name();
126 $itemname_el =& $mform->getElement('itemname');
127 $itemname_el->setValue($itemname);
129 // access control - disable not allowed elements
130 if (!has_capability('moodle/grade:manage', $context)) {
131 $mform->hardFreeze('excluded');
134 if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:hide', $context)) {
135 $mform->hardFreeze('hidden');
136 $mform->hardFreeze('hiddenuntil');
139 $old_grade_grade = new grade_grade(array('itemid'=>$grade_item->id
, 'userid'=>$userid));
141 if ($grade_item->is_hidden()) {
142 $mform->hardFreeze('hidden');
145 if ($old_grade_grade->is_locked()) {
146 if ($grade_item->is_locked()) {
147 $mform->hardFreeze('locked');
148 $mform->hardFreeze('locktime');
151 $mform->hardFreeze('overridden');
152 $mform->hardFreeze('finalgrade');
153 $mform->hardFreeze('feedback');
156 if (empty($old_grade_grade->id
)) {
157 $old_grade_grade->locked
= $grade_item->locked
;
158 $old_grade_grade->locktime
= $grade_item->locktime
;
161 if (($old_grade_grade->locked
or $old_grade_grade->locktime
)
162 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:unlock', $context))) {
163 $mform->hardFreeze('locked');
164 $mform->hardFreeze('locktime');
166 } else if ((!$old_grade_grade->locked
and !$old_grade_grade->locktime
)
167 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:lock', $context))) {
168 $mform->hardFreeze('locked');
169 $mform->hardFreeze('locktime');