From 501e0e346ecb1d8334a84c6d8d60694387b7a011 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Tue, 17 Jul 2007 14:53:10 +0000 Subject: [PATCH] MDL-10481 Elements added to form. item preferences not yet recognised by report. --- grade/edit/item.php | 42 ++++++++++++++++++++++++++++++++-------- grade/edit/item_form.php | 14 ++++++++++++++ grade/report/lib.php | 35 ++++++++++++++++++++++----------- 3 files changed, 72 insertions(+), 19 deletions(-) diff --git a/grade/edit/item.php b/grade/edit/item.php index 01230d112e..e5948fe94c 100644 --- a/grade/edit/item.php +++ b/grade/edit/item.php @@ -1,6 +1,7 @@ dirroot.'/grade/lib.php'; +require_once $CFG->dirroot.'/grade/report/lib.php'; require_once $CFG->libdir.'/gradelib.php'; require_once 'item_form.php'; @@ -21,18 +22,24 @@ $gpr = new grade_plugin_return(); $returnurl = $gpr->get_return_url('tree.php?id='.$course->id); $mform = new edit_item_form(null, array('gpr'=>$gpr)); -if ($item = get_record('grade_items', 'id', $id, 'courseid', $course->id)) { - $item->calculation = grade_item::denormalize_formula($item->calculation, $course->id); - $mform->set_data($item); - -} else { - $mform->set_data(array('courseid'=>$course->id, 'itemtype'=>'manual')); -} if ($mform->is_cancelled()) { redirect($returnurl); +} else if (!$mform->is_submitted()) { + if ($item = get_record('grade_items', 'id', $id, 'courseid', $course->id)) { + // Get Item preferences + $item->gradedisplaytype = get_user_preferences('grade_report_gradedisplaytype' . $id, 'default'); + $item->decimalpoints = get_user_preferences('grade_report_decimalpoints' . $id, 'default'); + + $item->calculation = grade_item::denormalize_formula($item->calculation, $course->id); + $mform->set_data($item); + } else { + $mform->set_data(array('courseid'=>$course->id, 'itemtype'=>'manual')); + } } else if ($data = $mform->get_data()) { + $errors = array(); + if (array_key_exists('calculation', $data)) { $data->calculation = grade_item::normalize_formula($data->calculation, $course->id); } @@ -48,7 +55,26 @@ if ($mform->is_cancelled()) { $grade_item->update(); } - redirect($returnurl); + // Handle user preferences + if (!empty($data->gradedisplaytype)) { + if (!grade_report::set_pref('gradedisplaytype', $data->gradedisplaytype, $id)) { + $errors[] = "Could not set preference gradedisplaytype to $value for this grade item"; + } + } + + if (!empty($data->decimalpoints)) { + if (!grade_report::set_pref('decimalpoints', $data->decimalpoints, $id)) { + $errors[] = "Could not set preference decimalpoints to $value for this grade item"; + } + } + + if (empty($errors)) { + // redirect($returnurl); + } else { + foreach ($errors as $error) { + error($error); + } + } } $strgrades = get_string('grades'); diff --git a/grade/edit/item_form.php b/grade/edit/item_form.php index f15faebf8f..edc093d103 100644 --- a/grade/edit/item_form.php +++ b/grade/edit/item_form.php @@ -72,6 +72,20 @@ class edit_item_form extends moodleform { $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true)); $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE); + $mform->addElement('select', 'gradedisplaytype', get_string('gradedisplaytype', 'grades'), + array('default' => get_string('default', 'grades'), + GRADE_REPORT_GRADE_DISPLAY_TYPE_RAW => get_string('raw', 'grades'), + GRADE_REPORT_GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'))); + $mform->setHelpButton('gradedisplaytype', array(false, get_string('gradedisplaytype', 'grades'), + false, true, false, get_string("config_gradedisplaytype", 'grades'))); + $mform->setDefault('gradedisplaytype', 'default'); + + $mform->addElement('select', 'decimalpoints', get_string('decimalpoints', 'grades'), + array('default' => get_string('default', 'grades'), 0, 1, 2, 3, 4, 5)); + $mform->setHelpButton('decimalpoints', array(false, get_string('decimalpoints', 'grades'), + false, true, false, get_string("config_decimalpoints", 'grades'))); + $mform->setDefault('decimalpoints', 'default'); + /// hidden params $mform->addElement('hidden', 'id', 0); $mform->setType('id', PARAM_INT); diff --git a/grade/report/lib.php b/grade/report/lib.php index 6084efc5d2..0370ebe669 100755 --- a/grade/report/lib.php +++ b/grade/report/lib.php @@ -96,40 +96,53 @@ class grade_report { * the value of that preference. If the preference has already been fetched before, * the saved value is returned. If the preference is not set at the User level, the $CFG equivalent * is given (site default). + * @static (Can be called statically, but then doesn't benefit from caching) * @param string $pref The name of the preference (do not include the grade_report_ prefix) * @param int $itemid An optional itemid to check for a more fine-grained preference * @return mixed The value of the preference */ function get_pref($pref, $itemid=null) { global $CFG; + $fullprefname = 'grade_report_' . $pref; - if (empty($this->user_prefs[$pref.$itemid])) { - $fullprefname = 'grade_report_' . $pref; + if (!isset($this)) { if (!empty($itemid)) { - $value = get_user_preferences($fullprefname . $itemid, $this->get_pref($pref)); + $value = get_user_preferences($fullprefname . $itemid, grade_report::get_pref($pref)); } else { $value = get_user_preferences($fullprefname, $CFG->$fullprefname); } - $this->user_prefs[$pref.$itemid] = $value; + return $value; + + } else { + if (empty($this->user_prefs[$pref.$itemid])) { + if (!empty($itemid)) { + $value = get_user_preferences($fullprefname . $itemid, $this->get_pref($pref)); + } else { + $value = get_user_preferences($fullprefname, $CFG->$fullprefname); + } + $this->user_prefs[$pref.$itemid] = $value; + } + return $this->user_prefs[$pref.$itemid]; } - return $this->user_prefs[$pref.$itemid]; } /** - * Uses set_user_preferences() to update the value of a user preference. - * Also updates the object's corresponding variable. + * Uses set_user_preferences() to update the value of a user preference. If 'default' is given as the value, + * the preference will be removed in favour of a higher-level preference. + * @static * @param string $pref_name The name of the preference. * @param mixed $pref_value The value of the preference. * @param int $itemid An optional itemid to which the preference will be assigned * @return bool Success or failure. * TODO print visual feedback */ - function set_pref($pref, $pref_value, $itemid=null) { + function set_pref($pref, $pref_value='default', $itemid=null) { $fullprefname = 'grade_report_' . $pref; - if ($result = set_user_preferences(array($fullprefname.$itemid => $pref_value))) { - $this->user_prefs[$pref.$itemid] = $pref_value; + if ($pref_value == 'default') { + return unset_user_preference($fullprefname.$itemid); + } else { + return set_user_preference($fullprefname.$itemid, $pref_value); } - return $result; } /** -- 2.39.5