From d3c3da1b771a72a2bcf2e06c0ec9e72af254fb90 Mon Sep 17 00:00:00 2001 From: skodak Date: Tue, 7 Aug 2007 08:30:25 +0000 Subject: [PATCH] MDL-9628 non-existent grades can be locked/hidden/edited :-) --- grade/edit/tree/action.php | 13 +++++++++ grade/edit/tree/grade_form.php | 2 +- grade/edit/tree/outcomeitem_form.php | 2 +- grade/lib.php | 43 +++++++++++++++++++++++++--- grade/report/grader/lib.php | 7 ++--- 5 files changed, 57 insertions(+), 10 deletions(-) diff --git a/grade/edit/tree/action.php b/grade/edit/tree/action.php index 90a870004ec..b7b83e0aa7e 100644 --- a/grade/edit/tree/action.php +++ b/grade/edit/tree/action.php @@ -26,6 +26,7 @@ if (!$element = $gtree->locate_element($eid)) { error('Incorrect element id!', $returnurl); } $object = $element['object']; +$type = $element['type']; switch ($action) { @@ -34,6 +35,9 @@ switch ($action) { if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:hide', $context)) { error('No permission to hide!', $returnurl); } + if ($type == 'grade' and empty($object->id)) { + $object->insert(); + } $object->set_hidden(1); } break; @@ -43,6 +47,9 @@ switch ($action) { if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:hide', $context)) { error('No permission to show!', $returnurl); } + if ($type == 'grade' and empty($object->id)) { + $object->insert(); + } $object->set_hidden(0); } break; @@ -52,6 +59,9 @@ switch ($action) { if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:lock', $context)) { error('No permission to lock!', $returnurl); } + if ($type == 'grade' and empty($object->id)) { + $object->insert(); + } $object->set_locked(1); } break; @@ -61,6 +71,9 @@ switch ($action) { if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:unlock', $context)) { error('No permission to unlock!', $returnurl); } + if ($type == 'grade' and empty($object->id)) { + $object->insert(); + } $object->set_locked(0); } break; diff --git a/grade/edit/tree/grade_form.php b/grade/edit/tree/grade_form.php index b1cd0b197de..faec98924ea 100755 --- a/grade/edit/tree/grade_form.php +++ b/grade/edit/tree/grade_form.php @@ -43,7 +43,7 @@ class edit_grade_form extends moodleform { /// hiding /// advcheckbox is not compatible with disabledIf !! $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades')); - $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddeuntil', 'grades'), array('optional'=>true)); + $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true)); $mform->disabledIf('hiddenuntil', 'hidden', 'checked'); /// locking diff --git a/grade/edit/tree/outcomeitem_form.php b/grade/edit/tree/outcomeitem_form.php index 05f07c5fe24..a5d575fbac5 100644 --- a/grade/edit/tree/outcomeitem_form.php +++ b/grade/edit/tree/outcomeitem_form.php @@ -46,7 +46,7 @@ class edit_outcomeitem_form extends moodleform { /// hiding /// advcheckbox is not compatible with disabledIf !! $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades')); - $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddeuntil', 'grades'), array('optional'=>true)); + $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true)); $mform->disabledIf('hiddenuntil', 'hidden', 'checked'); //locking diff --git a/grade/lib.php b/grade/lib.php index bfc4bcca745..54c2281221a 100644 --- a/grade/lib.php +++ b/grade/lib.php @@ -648,8 +648,27 @@ class grade_tree { * @return object element */ function locate_element($eid) { - if (strpos($eid, 'g') === 0) { - // it is a grade construct a new object + // it is a grade - construct a new object + if (strpos($eid, 'n') === 0) { + if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) { + return null; + } + + $itemid = $matches[1]; + $userid = $matches[2]; + + //extra security check - the grade item must be in this tree + if (!$item_el = $this->locate_element('i'.$itemid)) { + return null; + } + + // $gradea->id may be null - means does not exist yet + $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid)); + + $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! + return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade'); + + } else if (strpos($eid, 'g') === 0) { $id = (int)substr($eid, 1); if (!$grade = grade_grade::fetch(array('id'=>$id))) { return null; @@ -677,6 +696,19 @@ class grade_tree { return null; } + /** + * Returns the grade eid - the grade may not exist yet. + * @param $grade_grade object + * @return string eid + */ + function get_grade_eid($grade_grade) { + if (empty($grade_grade->id)) { + return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid; + } else { + return 'g'.$grade_grade->id; + } + } + /** * Return edit icon for give element * @param object $element @@ -715,8 +747,11 @@ class grade_tree { break; case 'grade': - //TODO: improve dealing with new grades - $url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&id='.$object->id; + if (empty($object->id)) { + $url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&itemid='.$object->itemid.'&userid='.$object->userid; + } else { + $url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&id='.$object->id; + } $url = $gpr->add_url_params($url); if (!empty($object->feedback)) { $feedback = format_text($object->feedback, $object->feedbackformat); diff --git a/grade/report/grader/lib.php b/grade/report/grader/lib.php index cad13c5dff5..564c7832208 100644 --- a/grade/report/grader/lib.php +++ b/grade/report/grader/lib.php @@ -562,7 +562,6 @@ class grade_report_grader extends grade_report { if (isset($this->finalgrades[$userid][$item->id])) { $gradeval = $this->finalgrades[$userid][$item->id]->finalgrade; - $grade = new grade_grade($this->finalgrades[$userid][$item->id], false); $grade->feedback = stripslashes_safe($this->finalgrades[$userid][$item->id]->feedback); $grade->feedbackformat = $this->finalgrades[$userid][$item->id]->feedbackformat; @@ -577,7 +576,8 @@ class grade_report_grader extends grade_report { $grade->grade_item =& $this->items[$itemid]; // this speedsup is_hidden() and other grade_grade methods // emulate grade element - $element = array('eid'=>'g'.$grade->id, 'object'=>$grade, 'type'=>'grade'); + $eid = $this->gtree->get_grade_eid($grade); + $element = array('eid'=>$eid, 'object'=>$grade, 'type'=>'grade'); if ($grade->is_overridden()) { $studentshtml .= ''; @@ -590,8 +590,7 @@ class grade_report_grader extends grade_report { } // Do not show any icons if no grade (no record in DB to match) - // TODO: change edit/hide/etc. links to use itemid and userid to allow creating of new grade objects - if (!$item->needsupdate and !empty($grade->id) and $USER->gradeediting[$this->courseid]) { + if (!$item->needsupdate and $USER->gradeediting[$this->courseid]) { $studentshtml .= $this->get_icons($element); }