]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10481 Elements added to form. item preferences not yet recognised by report.
authornicolasconnault <nicolasconnault>
Tue, 17 Jul 2007 14:53:10 +0000 (14:53 +0000)
committernicolasconnault <nicolasconnault>
Tue, 17 Jul 2007 14:53:10 +0000 (14:53 +0000)
grade/edit/item.php
grade/edit/item_form.php
grade/report/lib.php

index 01230d112ec2aae6a7422616b7f97f094eefde47..e5948fe94c92a7cfa595fc48eab402252f6dfe1b 100644 (file)
@@ -1,6 +1,7 @@
 <?php  //$Id$
 require_once '../../config.php';
 require_once $CFG->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');
index f15faebf8f7445e7a5ffb5b4a63b92e4c8886b2e..edc093d10314ad9df4b757d834f3baaf2c4a843c 100644 (file)
@@ -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);
index 6084efc5d21c1ce82d02330d3fd7713ef3e8f1ff..0370ebe66915ecec63a6157be9aa56108eb3f40c 100755 (executable)
@@ -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;
     }
 
     /**