]> git.mjollnir.org Git - moodle.git/commitdiff
Optimising the grader report
authornicolasconnault <nicolasconnault>
Fri, 13 Jul 2007 19:02:40 +0000 (19:02 +0000)
committernicolasconnault <nicolasconnault>
Fri, 13 Jul 2007 19:02:40 +0000 (19:02 +0000)
grade/report/grader/lib.php
grade/report/lib.php
lib/grade/grade_grades.php

index b8158baa8ced990ce920f35bad2126b00a5f06c7..3f4deeb3e9037fef3049e13b249f4b004cae7bd8 100644 (file)
@@ -401,8 +401,8 @@ class grade_report_grader extends grade_report {
         $pref_name = 'grade_report_show' . $type;
         $show_pref = get_user_preferences($pref_name, $CFG->$pref_name);
 
-        $strshow = get_string('show' . $type, 'grades');
-        $strhide = get_string('hide' . $type, 'grades');
+        $strshow = $this->get_lang_string('show' . $type, 'grades');
+        $strhide = $this->get_lang_string('hide' . $type, 'grades');
 
         $show_hide = 'show';
         $toggle_action = 1;
@@ -440,8 +440,8 @@ class grade_report_grader extends grade_report {
     function get_headerhtml() {
         global $CFG, $USER;
 
-        $strsortasc  = get_string('sortasc', 'grades');
-        $strsortdesc = get_string('sortdesc', 'grades');
+        $strsortasc  = $this->get_lang_string('sortasc', 'grades');
+        $strsortdesc = $this->get_lang_string('sortdesc', 'grades');
         if ($this->sortitemid === 'lastname') {
             if ($this->sortorder == 'ASC') {
                 $lastarrow = print_arrow('up', $strsortasc, true);
@@ -528,10 +528,10 @@ class grade_report_grader extends grade_report {
 
                     if ($object->itemtype == 'mod') {
                         $icon = '<img src="'.$CFG->modpixpath.'/'.$object->itemmodule.'/icon.gif" class="icon" alt="'
-                              .get_string('modulename', $object->itemmodule).'"/>';
+                              .$this->get_lang_string('modulename', $object->itemmodule).'"/>';
                     } else if ($object->itemtype == 'manual') {
                         //TODO: add manual grading icon
-                        $icon = '<img src="'.$CFG->pixpath.'/t/edit.gif" class="icon" alt="'.get_string('manualgrade', 'grades')
+                        $icon = '<img src="'.$CFG->pixpath.'/t/edit.gif" class="icon" alt="'.$this->get_lang_string('manualgrade', 'grades')
                               .'"/>';
                     }
 
@@ -559,10 +559,24 @@ class grade_report_grader extends grade_report {
     function get_studentshtml() {
         global $CFG, $USER;
         $studentshtml = '';
-        $strfeedback = get_string("feedback");
+        $strfeedback = $this->get_lang_string("feedback");
         $gradetabindex = 1;
         $feedbacktabindex = 16380; // The maximum number of tabindices on 1 page is 32767
 
+        // Preload scale objects for items with a scaleid
+        $scales_list = '';
+        foreach ($this->items as $item) {
+            if (!empty($item->scaleid)) {
+                $scales_list .= "$item->scaleid,";
+            }
+        }
+        $scales_array = array();
+
+        if (!empty($scales_list)) {
+            $scales_list = substr($scales_list, 0, -1);
+            $scales_array = get_records_list('scale', 'id', $scales_list);
+        }
+
         foreach ($this->users as $userid => $user) {
             // Student name and link
             $studentshtml .= '<tr><th class="user"><a href="' . $CFG->wwwroot . '/user/view.php?id='
@@ -594,8 +608,8 @@ 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 (!empty($grade->id)) {
-                    $studentshtml .= $this->get_icons($element);
+                if (!empty($grade->id) && $USER->gradeediting) {
+                    $studentshtml .= $this->get_icons($element, null, true, $item);
                 }
 
                 // if in editting mode, we need to print either a text box
@@ -606,31 +620,31 @@ class grade_report_grader extends grade_report {
                     // We need to retrieve each grade_grade object from DB in order to
                     // know if they are hidden/locked
 
-                    if ($item->scaleid) {
-                        if ($scale = get_record('scale', 'id', $item->scaleid)) {
+                    if ($item->scaleid && !empty($scales_array[$item->scaleid])) {
+                        $scale = $scales_array[$item->scaleid];
+
+                        $scales = explode(",", $scale->scale);
+                        // reindex because scale is off 1
+                        $i = 0;
+                        foreach ($scales as $scaleoption) {
+                            $i++;
+                            $scaleopt[$i] = $scaleoption;
+                        }
+
+                        if ($this->get_pref('quickgrading') and $grade->is_editable()) {
+                            $studentshtml .= choose_from_menu($scaleopt, 'grade_'.$userid.'_'.$item->id,
+                                                          $gradeval, $this->get_lang_string('nograde'), '', -1, true, false, $gradetabindex++);
+                        } elseif(!empty($scale)) {
                             $scales = explode(",", $scale->scale);
-                            // reindex because scale is off 1
-                            $i = 0;
-                            foreach ($scales as $scaleoption) {
-                                $i++;
-                                $scaleopt[$i] = $scaleoption;
-                            }
 
-                            if ($this->get_pref('quickgrading') and $grade->is_editable()) {
-                                $studentshtml .= choose_from_menu($scaleopt, 'grade_'.$userid.'_'.$item->id,
-                                                              $gradeval, get_string('nograde'), '', -1, true, false, $gradetabindex++);
-                            } elseif ($scale = get_record('scale', 'id', $item->scaleid)) {
-                                $scales = explode(",", $scale->scale);
-
-                                // invalid grade if gradeval < 1
-                                if ((int) $gradeval < 1) {
-                                    $studentshtml .= '-';
-                                } else {
-                                    $studentshtml .= $scales[$gradeval-1];
-                                }
+                            // invalid grade if gradeval < 1
+                            if ((int) $gradeval < 1) {
+                                $studentshtml .= '-';
                             } else {
-                                // no such scale, throw error?
+                                $studentshtml .= $scales[$gradeval-1];
                             }
+                        } else {
+                            // no such scale, throw error?
                         }
 
                     } else if ($item->gradetype != GRADE_TYPE_TEXT) {
@@ -666,18 +680,15 @@ class grade_report_grader extends grade_report {
 
                     // finalgrades[$userid][$itemid] could be null because of the outer join
                     // in this case it's different than a 0
-                    if ($item->scaleid) {
-                        if ($scale = get_record('scale', 'id', $item->scaleid)) {
-                            $scales = explode(",", $scale->scale);
+                    if ($item->scaleid && !empty($scales_array[$item->scaleid])) {
+                        $scale = $scales_array[$item->scaleid];
+                        $scales = explode(",", $scale->scale);
 
-                            // invalid grade if gradeval < 1
-                            if ((int) $gradeval < 1) {
-                                $studentshtml .= '-';
-                            } else {
-                                $studentshtml .= $scales[$gradeval-1];
-                            }
+                        // invalid grade if gradeval < 1
+                        if ((int) $gradeval < 1) {
+                            $studentshtml .= '-';
                         } else {
-                            // no such scale, throw error?
+                            $studentshtml .= $scales[$gradeval-1];
                         }
                     } else {
                         if (is_null($gradeval)) {
@@ -795,7 +806,7 @@ class grade_report_grader extends grade_report {
     function get_scalehtml() {
         $scalehtml = '';
         if ($this->get_pref('showscales')) {
-            $scalehtml = '<tr><td>'.get_string('range','grades').'</td>';
+            $scalehtml = '<tr><td>'.$this->get_lang_string('range','grades').'</td>';
             foreach ($this->items as $item) {
                 $scalehtml .= '<td>'. $this->get_grade_clean($item->grademin).'-'. $this->get_grade_clean($item->grademax).'</td>';
             }
@@ -812,29 +823,34 @@ class grade_report_grader extends grade_report {
      * @param object $object
      * @param array $icons An array of icon names that this function is explicitly requested to print, regardless of settings
      * @param bool $limit If true, use the $icons array as the only icons that will be printed. If false, use it to exclude these icons.
+     * @param object $parent_object An optional parent object (like grade_item if $element is grade_grades)
+     *                              that can be checked for hidden or locked status
      * @return string HTML
      */
-    function get_icons($element, $icons=null, $limit=true) {
+    function get_icons($element, $icons=null, $limit=true, $parent_object=null) {
         global $CFG;
         global $USER;
 
+        // If no parent object is given, we need to let the element load its parent object to get hidden, locked and editable status
+        $check_parent = empty($parent_object);
+
         // Load language strings
-        $stredit           = get_string("edit");
-        $streditcalculation= get_string("editcalculation", 'grades');
-        $strfeedback       = get_string("feedback");
-        $strmove           = get_string("move");
-        $strmoveup         = get_string("moveup");
-        $strmovedown       = get_string("movedown");
-        $strmovehere       = get_string("movehere");
-        $strcancel         = get_string("cancel");
-        $stredit           = get_string("edit");
-        $strdelete         = get_string("delete");
-        $strhide           = get_string("hide");
-        $strshow           = get_string("show");
-        $strlock           = get_string("lock", 'grades');
-        $strswitch_minus   = get_string("contract", 'grades');
-        $strswitch_plus    = get_string("expand", 'grades');
-        $strunlock         = get_string("unlock", 'grades');
+        $stredit           = $this->get_lang_string("edit");
+        $streditcalculation= $this->get_lang_string("editcalculation", 'grades');
+        $strfeedback       = $this->get_lang_string("feedback");
+        $strmove           = $this->get_lang_string("move");
+        $strmoveup         = $this->get_lang_string("moveup");
+        $strmovedown       = $this->get_lang_string("movedown");
+        $strmovehere       = $this->get_lang_string("movehere");
+        $strcancel         = $this->get_lang_string("cancel");
+        $stredit           = $this->get_lang_string("edit");
+        $strdelete         = $this->get_lang_string("delete");
+        $strhide           = $this->get_lang_string("hide");
+        $strshow           = $this->get_lang_string("show");
+        $strlock           = $this->get_lang_string("lock", 'grades');
+        $strswitch_minus   = $this->get_lang_string("contract", 'grades');
+        $strswitch_plus    = $this->get_lang_string("expand", 'grades');
+        $strunlock         = $this->get_lang_string("unlock", 'grades');
 
         // Prepare container div
         $html = '<div class="grade_icons">';
@@ -861,7 +877,7 @@ class grade_report_grader extends grade_report {
 
         // Prepare image strings
         $edit_icon = '';
-        if ($object->is_editable()) {
+        if ((!$check_parent && $parent_object->is_editable()) || $object->is_editable($parent_object)) {
             if ($type == 'category') {
                 $edit_icon = '<a href="'. GRADE_EDIT_URL . '/category.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">'
                            . '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'
@@ -890,7 +906,9 @@ class grade_report_grader extends grade_report {
 
         // Prepare Hide/Show icon state
         $hide_show = 'hide';
-        if ($object->is_hidden()) {
+        if (!$check_parent && $parent_object->is_hidden()) {
+            $hide_show = 'show';
+        } elseif ($object->is_hidden($parent_object)) {
             $hide_show = 'show';
         }
 
@@ -901,7 +919,7 @@ class grade_report_grader extends grade_report {
 
         // Prepare lock/unlock string
         $lock_unlock = 'lock';
-        if ($object->is_locked()) {
+        if ((!$check_parent && $parent_object->is_locked()) || $object->is_locked($parent_object)) {
             $lock_unlock = 'unlock';
         }
 
index 9ae353116b3b74ba8bfb5d71641c82cc1616d24c..05bb3fc62429681b9cc3702c7bcca1635a9fafa2 100755 (executable)
@@ -60,6 +60,12 @@ class grade_report {
      */
     var $page;
 
+    /**
+     * Array of cached language strings (using get_string() all the time takes a long time!).
+     * @var array $lang_strings
+     */
+    var $lang_strings = array();
+
     /**
      * Constructor. Sets local copies of user preferences and initialises grade_tree.
      * @param int $courseid
@@ -143,7 +149,7 @@ class grade_report {
 
                 } else { // Print confirmation dialog
                     $eid = $element['eid'];
-                    $strdeletecheckfull = get_string('deletecheck', '', $element['object']->get_name());
+                    $strdeletecheckfull = $this->get_lang_string('deletecheck', '', $element['object']->get_name());
                     $linkyes = GRADE_EDIT_URL . "/tree.php?target=$eid&amp;action=delete&amp;confirm=1$this->gtree->commonvars";
                     $linkno = GRADE_EDIT_URL . "/tree.php?$this->gtree->commonvars";
                     notice_yesno($strdeletecheckfull, $linkyes, $linkno);
@@ -192,7 +198,7 @@ class grade_report {
         } else {
             // decimal points as specified by user
             $decimals = get_user_preferences('grade_report_decimalpoints', $CFG->grade_report_decimalpoints);
-            $gradeval = number_format($gradeval, $decimals, get_string('decpoint', 'langconfig'), get_string('thousandsep', 'langconfig'));
+            $gradeval = number_format($gradeval, $decimals, $this->get_lang_string('decpoint', 'langconfig'), $this->get_lang_string('thousandsep', 'langconfig'));
         }
 
         return $gradeval;
@@ -217,8 +223,8 @@ class grade_report {
      */
     function format_grade($gradeval) {
 
-        $decimalpt = get_string('decpoint', 'langconfig');
-        $thousandsep = get_string('thousandsep', 'langconfig');
+        $decimalpt = $this->get_lang_string('decpoint', 'langconfig');
+        $thousandsep = $this->get_lang_string('thousandsep', 'langconfig');
         // replace decimal point with '.';
         $gradeval = str_replace($decimalpt, '.', $gradeval);
         // thousand separator is not useful
@@ -227,5 +233,20 @@ class grade_report {
         return clean_param($gradeval, PARAM_NUMBER);
     }
 
+    /**
+     * First checks the cached language strings, then returns match if found, or uses get_string()
+     * to get it from the DB, caches it then returns it.
+     * @param string $strcode
+     * @param string $section Optional language section
+     * @return string
+     */
+    function get_lang_string($strcode, $section=null) {
+        if (empty($this->lang_strings[$strcode])) {
+            $this->lang_strings[$strcode] = get_string($strcode, $section);
+        }
+        return $this->lang_strings[$strcode];
+    }
+
+
 }
 ?>
index 0acfb97af2a2748d7d08dd33cad7199f1951fa8c..0f2ad33514a4b72c461c5e15b5d09e5213ac6a58 100644 (file)
@@ -146,10 +146,13 @@ class grade_grades extends grade_object {
 
     /**
      * Loads the grade_item object referenced by $this->itemid and saves it as $this->grade_item for easy access.
+     * @param object $grade_item An optional grade_item given to avoid having to reload one from the DB
      * @return object grade_item.
      */
-    function load_grade_item() {
-        if (empty($this->grade_item) && !empty($this->itemid)) {
+    function load_grade_item($grade_item=null) {
+        if (!empty($grade_item) && get_class($grade_item) == 'grade_item') {
+            $this->grade_item = $grade_item;
+        } elseif (empty($this->grade_item) && !empty($this->itemid)) {
             $this->grade_item = grade_item::fetch(array('id'=>$this->itemid));
         }
         return $this->grade_item;
@@ -157,15 +160,15 @@ class grade_grades extends grade_object {
 
     /**
      * Is grading object editable?
+     * @param object $grade_item An optional grade_item given to avoid having to reload one from the DB
      * @return boolean
      */
-    function is_editable() {
-        if ($this->is_locked()) {
+    function is_editable($grade_item=null) {
+        if ($this->is_locked($grade_item)) {
             return false;
         }
 
-
-        $grade_item = $this->load_grade_item();
+        $grade_item = $this->load_grade_item($grade_item);
 
         if ($grade_item->gradetype == GRADE_TYPE_NONE) {
             return false;
@@ -179,10 +182,11 @@ class grade_grades extends grade_object {
      * Internally any date in locked field (including future ones) means locked,
      * the date is stored for logging purposes only.
      *
+     * @param object $grade_item An optional grade_item given to avoid having to reload one from the DB
      * @return boolean true if locked, false if not
      */
-    function is_locked() {
-        $this->load_grade_item();
+    function is_locked($grade_item=null) {
+        $this->load_grade_item($grade_item);
 
         return !empty($this->locked) or $this->grade_item->is_locked();
     }
@@ -192,7 +196,7 @@ class grade_grades extends grade_object {
     }
 
     /**
-     * Lock/unlopck this grade.
+     * Lock/unlock this grade.
      *
      * @param boolean $lockstate true means lock, false unlock grade
      * @return boolean true if sucessful, false if can not set new lock state for grade
@@ -277,10 +281,11 @@ class grade_grades extends grade_object {
      * Internally any date in hidden field (including future ones) means hidden,
      * the date is stored for logging purposes only.
      *
+     * @param object $grade_item An optional grade_item given to avoid having to reload one from the DB
      * @return boolean true if hidden, false if not
      */
-    function is_hidden() {
-        $this->load_grade_item();
+    function is_hidden($grade_item=null) {
+        $this->load_grade_item($grade_item);
 
         return $this->hidden == 1 or $this->hidden > time() or $this->grade_item->is_hidden();
     }