MDL-10386 More refactoring (grade_report_grader->get_user_pref($pref_name))
authornicolasconnault <nicolasconnault>
Thu, 12 Jul 2007 17:42:23 +0000 (17:42 +0000)
committernicolasconnault <nicolasconnault>
Thu, 12 Jul 2007 17:42:23 +0000 (17:42 +0000)
grade/report/grader/grader_report.php
grade/report/grader/index.php

index 2a5f0aa84e423b8d0fa934f8df34736b69e0f32a..b6c678e3369b79c4d75d56e929b80527080f8dd4 100644 (file)
@@ -58,55 +58,11 @@ class grade_report_grader {
      */
     var $gradeserror = array();
 
-//// USER PREFERENCES
-
-    /**
-     * Number of users on a page.
-     * @var int $perpage
-     */
-    var $studentsperpage;
-
-    /**
-     * Number of digits after the decimal point.
-     * @var int $decimalspoints
-     */
-    var $decimalspoints;
-
-    /**
-     * Whether or not to display the grandtotals row.
-     * @var bool $showgrandtotals
-     */
-    var $showgrandtotals;
-
-    /**
-     * Whether or not to display group selector, total row and other group-related elements.
-     * @var bool $showgroups
-     */
-    var $showgroups;
-
-    /**
-     * The position of the Aggregation column in relation to the raw grade items.
-     * @var int $aggregation_position
-     */
-    var $aggregation_position;
-
-    /**
-     * Whether or not to display a row of scales/ranges for each grade_item.
-     * @var bool $showscales
-     */
-    var $showscales;
-
     /**
-     * Whether or not to use quickgrading.
-     * @var bool $quickgrading
+     * User preferences related to this report.
+     * @var array $user_prefs
      */
-    var $quickgrading;
-
-    /**
-     * Whether or not to use quickfeedback.
-     * @var bool $quickfeedback
-     */
-    var $quickfeedback;
+    var $user_prefs = array();
 
 //// SQL-RELATED
 
@@ -195,39 +151,40 @@ class grade_report_grader {
         // roles to be displayed in the gradebook
         $this->gradebookroles = $CFG->gradebookroles;
 
-        // User preferences
-        $this->studentsperpage      = get_user_preferences('grade_report_studentsperpage',
-                                                            $CFG->grade_report_studentsperpage);
-        $this->decimalpoints        = get_user_preferences('grade_report_decimalpoints',
-                                                            $CFG->grade_report_decimalpoints);
-        $this->showgrandtotals      = get_user_preferences('grade_report_showgrandtotals',
-                                                            $CFG->grade_report_showgrandtotals);
-        $this->showgroups           = get_user_preferences('grade_report_showgroups',
-                                                            $CFG->grade_report_showgroups);
-        $this->aggregation_position = get_user_preferences('grade_report_aggregationposition',
-                                                            $CFG->grade_report_aggregationposition);
-        $this->showscales           = get_user_preferences('grade_report_showscales',
-                                                            $CFG->grade_report_showscales);
-        $this->quickgrading         = get_user_preferences('grade_report_quickgrading',
-                                                            $CFG->grade_report_quickgrading);
-        $this->quickfeedback        = get_user_preferences('grade_report_quickfeedback',
-                                                            $CFG->grade_report_quickfeedback);
-
         // Grab the grade_tree for this course
-        $this->gtree = new grade_tree($this->courseid, true, false, $this->aggregation_position);
+        $this->gtree = new grade_tree($this->courseid, true, false, $this->get_user_pref('aggregationposition'));
 
         // base url for sorting by first/last name
-        $this->baseurl = 'report.php?id='.$this->courseid.'&amp;perpage='.$this->studentsperpage.'&amp;report=grader&amp;page='.$this->page;
+        $this->baseurl = 'report.php?id='.$this->courseid.'&amp;perpage='.$this->get_user_pref('studentsperpage')
+                        .'&amp;report=grader&amp;page='.$this->page;
         //
-        $this->pbarurl = 'report.php?id='.$this->courseid.'&amp;perpage='.$this->studentsperpage.'&amp;report=grader&amp;';
+        $this->pbarurl = 'report.php?id='.$this->courseid.'&amp;perpage='.$this->get_user_pref('studentsperpage')
+                        .'&amp;report=grader&amp;';
 
-        if ($this->showgroups) {
+        if ($this->get_user_pref('showgroups')) {
             $this->setup_groups();
         }
 
         $this->setup_sortitemid();
     }
 
+    /**
+     * Given the name of a user preference (without grade_report_ prefix), locally saves then returns
+     * 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).
+     * @param string $pref The name of the preference (do not include the grade_report_ prefix)
+     * @return mixed The value of the preference
+     */
+    function get_user_pref($pref) {
+        global $CFG;
+
+        if (empty($this->user_prefs[$pref])) {
+            $fullprefname = 'grade_report_' . $pref;
+            $this->user_prefs[$pref] = get_user_preferences($fullprefname, $CFG->$fullprefname);
+        }
+        return $this->user_prefs[$pref];
+    }
     /**
      * Uses set_user_preferences() to update the value of a user preference.
      * Also updates the object's corresponding variable.
@@ -236,9 +193,9 @@ class grade_report_grader {
      * @return bool Success or failure.
      * TODO print visual feedback
      */
-    function set_user_pref($pref_name, $pref_value) {
-        if ($result = set_user_preferences(array($pref_name => $pref_value))) {
-            $this->$pref_name = $pref_value;
+    function set_user_pref($pref, $pref_value) {
+        if ($result = set_user_preferences(array($pref => $pref_value))) {
+            $this->$pref = $pref_value;
         }
         return $result;
     }
@@ -487,13 +444,15 @@ class grade_report_grader {
                          $this->groupwheresql
                     AND ra.contextid ".get_related_contexts_string($this->context)."
                     ORDER BY g.finalgrade $this->sortorder";
-            $this->users = get_records_sql($sql, $this->studentsperpage * $this->page, $this->studentsperpage);
+            $this->users = get_records_sql($sql, $this->get_user_pref('studentsperpage') * $this->page,
+                                $this->get_user_pref('studentsperpage'));
         } else {
             // default sort
             // get users sorted by lastname
             $this->users = get_role_users(@implode(',', $CFG->gradebookroles), $this->context, false,
                                 'u.id, u.firstname, u.lastname', 'u.'.$this->sortitemid .' '. $this->sortorder,
-                                false, $this->page * $this->studentsperpage, $this->studentsperpage, $this->currentgroup);
+                                false, $this->page * $this->get_user_pref('studentsperpage'), $this->get_user_pref('studentsperpage'),
+                                $this->currentgroup);
             // need to cut users down by groups
 
         }
@@ -794,7 +753,7 @@ class grade_report_grader {
                                 $scaleopt[$i] = $scaleoption;
                             }
 
-                            if ($this->quickgrading) {
+                            if ($this->get_user_pref('quickgrading')) {
                                 $studentshtml .= choose_from_menu($scaleopt, 'grade_'.$userid.'_'.$item->id,
                                                               $gradeval, get_string('nograde'), '', -1, true);
                             } elseif ($scale = get_record('scale', 'id', $item->scaleid)) {
@@ -811,7 +770,7 @@ class grade_report_grader {
                             }
                         }
                     } else {
-                        if ($this->quickgrading) {
+                        if ($this->get_user_pref('quickgrading')) {
                             $studentshtml .= '<input size="6" type="text" name="grade_'.$userid.'_'
                                           .$item->id.'" value="'.get_grade_clean($gradeval).'"/>';
                         } else {
@@ -821,8 +780,8 @@ class grade_report_grader {
 
 
                     // If quickfeedback is on, print an input element
-                    if ($this->quickfeedback) {
-                        if ($this->quickgrading) {
+                    if ($this->get_user_pref('quickfeedback')) {
+                        if ($this->get_user_pref('quickgrading')) {
                             $studentshtml .= '<br />';
                         }
                         $studentshtml .= '<input size="6" type="text" name="feedback_'.$userid.'_'.$item->id.'" value="'
@@ -884,7 +843,7 @@ class grade_report_grader {
 
         $groupsumhtml = '';
 
-        if ($this->currentgroup && $this->showgroups) {
+        if ($this->currentgroup && $this->get_user_pref('showgroups')) {
 
         /** SQL for finding group sum */
             $SQL = "SELECT g.itemid, SUM(g.finalgrade) as sum
@@ -919,11 +878,15 @@ class grade_report_grader {
         return $groupsumhtml;
     }
 
+    /**
+     * Builds and return the HTML row of column totals.
+     * @return string HTML
+     */
     function get_gradesumhtml() {
         global $CFG;
 
         $gradesumhtml = '';
-        if ($this->showgrandtotals) {
+        if ($this->get_user_pref('showgrandtotals')) {
 
         /** SQL for finding the SUM grades of all visible users ($CFG->gradebookroles) */
 
@@ -957,9 +920,13 @@ class grade_report_grader {
         return $gradesumhtml;
     }
 
+    /**
+     * Builds and return the HTML row of scales for each column (i.e. range).
+     * @return string HTML
+     */
     function get_scalehtml() {
         $scalehtml = '';
-        if ($this->showscales) {
+        if ($this->get_user_pref('showscales')) {
             $scalehtml = '<tr><td>'.get_string('range','grades').'</td>';
             foreach ($this->items as $item) {
                 $scalehtml .= '<td>'. get_grade_clean($item->grademin).'-'. get_grade_clean($item->grademax).'</td>';
@@ -1014,12 +981,6 @@ class grade_report_grader {
             $object->feedback = '';
         }
 
-        // Load user preferences
-        $aggregationview  = get_user_preferences('grade_report_aggregationview', $CFG->grade_report_aggregationview);
-        $showeyecons      = get_user_preferences('grade_report_showeyecons', $CFG->grade_report_showeyecons);
-        $showlocks        = get_user_preferences('grade_report_showlocks', $CFG->grade_report_showlocks);
-        $showcalculations = get_user_preferences('grade_report_showcalculations', $CFG->grade_report_showcalculations);
-
         // Prepare image strings
         $edit_category_icon = '<a href="report/grader/edit_category.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">'
                             . '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'
@@ -1111,20 +1072,20 @@ class grade_report_grader {
             }
 
             // Calculation icon for items and categories
-            if ($showcalculations && $type != 'grade') {
+            if ($this->get_user_pref('showcalculations') && $type != 'grade') {
                 $html .= $edit_calculation_icon;
             }
 
-            if ($showeyecons) {
+            if ($this->get_user_pref('showeyecons')) {
                 $html .= $show_hide_icon;
             }
 
-            if ($showlocks) {
+            if ($this->get_user_pref('showlocks')) {
                 $html .= $lock_unlock_icon;
             }
 
             // If object is a category, display expand/contract icon
-            if (get_class($object) == 'grade_category' && $aggregationview == GRADER_REPORT_AGGREGATION_VIEW_COMPACT) {
+            if (get_class($object) == 'grade_category' && $this->get_user_pref('aggregationview') == GRADER_REPORT_AGGREGATION_VIEW_COMPACT) {
                 $html .= $contract_expand_icon;
             }
         } else { // Editing mode is off
index cc773afd50098ef78d886cfe0fe6bc7682cdc63a..607e63d9d4b6cdd1443298bc245d769523f93296 100644 (file)
@@ -14,7 +14,7 @@ $type          = optional_param('type', 0, PARAM_ALPHA);
 $target        = optional_param('target', 0, PARAM_ALPHANUM);
 $toggle        = optional_param('toggle', NULL, PARAM_INT);
 $toggle_type   = optional_param('toggle_type', 0, PARAM_ALPHANUM);
-$db->debug=true;
+
 // Handle toggle change request
 if (!is_null($toggle) && !empty($toggle_type)) {
     set_user_preferences(array('grade_report_show' . $toggle_type => $toggle));
@@ -30,7 +30,7 @@ if ($data = data_submitted() and confirm_sesskey()) {
 
 // Override perpage if set in URL
 if ($perpageurl = optional_param('perpage', 0, PARAM_INT)) {
-    $report->studentsperpage = $perpageurl;
+    $report->user_prefs['studentsperpage'] = $perpageurl;
 }
 
 // Perform actions on categories, items and grades
@@ -58,7 +58,7 @@ include('tabs.php');
 
 echo $report->group_selector;
 echo $report->get_toggles_html();
-print_paging_bar($numusers, $report->page, $report->studentsperpage, $report->pbarurl);
+print_paging_bar($numusers, $report->page, $report->get_user_pref('studentsperpage'), $report->pbarurl);
 echo '<br />';
 
 $reporthtml = '<table class="boxaligncenter">';
@@ -80,13 +80,13 @@ if ($USER->gradeediting) {
 echo $reporthtml;
 
 // print submit button
-if ($USER->gradeediting && ($report->quickfeedback || $report->quickgrading)) {
+if ($USER->gradeediting && ($report->get_user_pref('quickfeedback') || $report->get_user_pref('quickgrading'))) {
     echo '<div class="submit"><input type="submit" value="'.get_string('update').'" /></div>';
     echo '</div></form>';
 }
 
 // prints paging bar at bottom for large pages
-if ($report->studentsperpage >= 20) {
-    print_paging_bar($numusers, $report->page, $report->studentsperpage, $report->pbarurl);
+if ($report->get_user_pref('studentsperpage') >= 20) {
+    print_paging_bar($numusers, $report->page, $report->get_user_pref('studentsperpage'), $report->pbarurl);
 }
 ?>