MDL-47146 core_grades: change columns on setup page
authorMarina Glancy <marina@moodle.com>
Tue, 23 Sep 2014 03:37:12 +0000 (11:37 +0800)
committerAdrian Greeve <adrian@moodle.com>
Fri, 3 Oct 2014 05:54:39 +0000 (13:54 +0800)
Part of: MDL-46576

grade/edit/tree/index.php
grade/edit/tree/lib.php
lang/en/grades.php

index c3ae0d3..b4b4ed1 100644 (file)
@@ -203,7 +203,7 @@ if ($data = data_submitted() and confirm_sesskey()) {
             $recreatetree = true;
 
         // Grade item text inputs
-        } elseif (preg_match('/^(grademax|weight|multfactor|plusfactor)_([0-9]+)$/', $key, $matches)) {
+        } elseif (preg_match('/^(weight|multfactor|plusfactor)_([0-9]+)$/', $key, $matches)) {
             $param = $matches[1];
             $aid   = $matches[2];
 
index 8a3cb27..fa3041d 100644 (file)
@@ -42,10 +42,11 @@ class grade_edit_tree {
 
     public $deepest_level;
 
-    public $uses_extra_credit = false;
-
     public $uses_weight = false;
 
+    /** @var bool indicates if tree has categories with aggregation method other than Natural. */
+    protected $uses_non_natural = false;
+
     public $table;
 
     public $categories = array();
@@ -70,26 +71,29 @@ class grade_edit_tree {
         $this->gpr = $gpr;
         $this->deepest_level = $this->get_deepest_level($this->gtree->top_element);
 
-        $this->columns = array(grade_edit_tree_column::factory('name', array('deepest_level' => $this->deepest_level)),
-                               grade_edit_tree_column::factory('aggregation', array('flag' => true)));
+        $this->columns = array(grade_edit_tree_column::factory('name', array('deepest_level' => $this->deepest_level)));
+
+        if ($this->uses_non_natural) {
+            $this->columns[] = grade_edit_tree_column::factory('aggregation', array('flag' => true));
+        }
 
         if ($this->uses_weight) {
             $this->columns[] = grade_edit_tree_column::factory('weight', array('adv' => 'weight'));
         }
-        if ($this->uses_extra_credit) {
-            $this->columns[] = grade_edit_tree_column::factory('extracredit', array('adv' => 'aggregationcoef'));
-        }
 
         $this->columns[] = grade_edit_tree_column::factory('range'); // This is not a setting... How do we deal with it?
-        $this->columns[] = grade_edit_tree_column::factory('aggregateonlygraded', array('flag' => true));
+        /*$this->columns[] = grade_edit_tree_column::factory('aggregateonlygraded', array('flag' => true));
         $this->columns[] = grade_edit_tree_column::factory('aggregatesubcats', array('flag' => true));
         $this->columns[] = grade_edit_tree_column::factory('aggregateoutcomes', array('flag' => true));
         $this->columns[] = grade_edit_tree_column::factory('droplow', array('flag' => true));
         $this->columns[] = grade_edit_tree_column::factory('keephigh', array('flag' => true));
         $this->columns[] = grade_edit_tree_column::factory('multfactor', array('adv' => true));
-        $this->columns[] = grade_edit_tree_column::factory('plusfactor', array('adv' => true));
+        $this->columns[] = grade_edit_tree_column::factory('plusfactor', array('adv' => true));*/
         $this->columns[] = grade_edit_tree_column::factory('actions');
-        $this->columns[] = grade_edit_tree_column::factory('select');
+
+        if ($this->deepest_level > 1) {
+            $this->columns[] = grade_edit_tree_column::factory('select');
+        }
 
         $this->table = new html_table();
         $this->table->id = "grade_edit_tree_table";
@@ -352,10 +356,9 @@ class grade_edit_tree {
     /**
      * Given a grade_item object, returns a labelled input if an aggregation coefficient (weight or extra credit) applies to it.
      * @param grade_item $item
-     * @param string type "extra" or "weight": the type of the column hosting the weight input
      * @return string HTML
      */
-    static function get_weight_input($item, $type) {
+    static function get_weight_input($item) {
         global $OUTPUT;
 
         if (!is_object($item) || get_class($item) !== 'grade_item') {
@@ -371,22 +374,17 @@ class grade_edit_tree {
         $parent_category->apply_forced_settings();
         $aggcoef = $item->get_coefstring();
 
-        if ((($aggcoef == 'aggregationcoefweight' || $aggcoef == 'aggregationcoef') && $type == 'weight') ||
-            ($aggcoef == 'aggregationcoefextraweight' && $type == 'extra')) {
+        $str = '';
+
+        if ($aggcoef == 'aggregationcoefweight' || $aggcoef == 'aggregationcoef' || $aggcoef == 'aggregationcoefextraweight') {
             return '<label class="accesshide" for="weight_'.$item->id.'">'.
                 get_string('extracreditvalue', 'grades', $item->itemname).'</label>'.
                 '<input type="text" size="6" id="weight_'.$item->id.'" name="weight_'.$item->id.'"
                 value="'.grade_edit_tree::format_number($item->aggregationcoef).'" />';
-        } elseif (($aggcoef == 'aggregationcoefextrasum' || $aggcoef == 'aggregationcoefextraweightsum') && $type == 'extra') {
-            $checked = ($item->aggregationcoef > 0) ? 'checked="checked"' : '';
-            return '<input type="hidden" name="extracredit_'.$item->id.'" value="0" />
-                <label class="accesshide" for="extracredit_'.$item->id.'">'.
-                get_string('extracreditvalue', 'grades', $item->itemname).'</label>
-                <input type="checkbox" id="extracredit_'.$item->id.'" name="extracredit_'.$item->id.'" value="1" '."$checked />\n";
-        } else if ($aggcoef == 'aggregationcoefextraweightsum' && $type == 'weight') {
+        } else if ($aggcoef == 'aggregationcoefextraweightsum') {
             $label = '';
             if ($item->weightoverride && $parent_category->aggregation == GRADE_AGGREGATE_SUM) {
-                $label = get_string('adjusted', 'grades');
+                $label = ' '.get_string('adjusted', 'grades');
             }
 
             $name = 'weight_' . $item->id;
@@ -410,10 +408,15 @@ class grade_edit_tree {
                 )
             );
 
-            return $hiddenlabel . $input . $label;
-        } else {
-            return '';
+            $str .= $hiddenlabel . $input . $label;
+
+            if ($item->aggregationcoef > 0) {
+                $str .= ' ' . html_writer::tag('abbr', get_string('aggregationcoefextrasumabbr', 'grades'),
+                        array('title' => get_string('aggregationcoefextrasum', 'grades')));
+            }
         }
+
+        return $str;
     }
 
     //Trims trailing zeros
@@ -508,11 +511,13 @@ class grade_edit_tree {
         $level++;
         $coefstring = $element['object']->get_coefstring();
         if ($element['type'] == 'category') {
-            if ($coefstring == 'aggregationcoefweight' || $coefstring == 'aggregationcoefextraweightsum') {
-                $this->uses_weight = true;
+            if ($element['object']->aggregation != GRADE_AGGREGATE_SUM) {
+                $this->uses_non_natural = true;
             }
-            if ($coefstring ==  'aggregationcoefextraweight' || $coefstring == 'aggregationcoefextraweightsum' || $coefstring == 'aggregationcoefextrasum') {
-                $this->uses_extra_credit = true;
+
+            if ($coefstring == 'aggregationcoefweight' || $coefstring == 'aggregationcoefextraweightsum' ||
+                    $coefstring == 'aggregationcoefextraweight') {
+                $this->uses_weight = true;
             }
 
             foreach($element['children'] as $child_el) {
@@ -687,25 +692,7 @@ class grade_edit_tree_column_aggregation extends grade_edit_tree_column_category
         }
 
         $options = grade_helper::get_aggregation_strings();
-
-        $visible = explode(',', $CFG->grade_aggregations_visible);
-        foreach ($options as $constant => $string) {
-            if (!in_array($constant, $visible) && $constant != $category->aggregation) {
-                unset($options[$constant]);
-            }
-        }
-
-        if ($this->forced) {
-            $aggregation = $options[$category->aggregation];
-        } else {
-            $attributes = array();
-            $attributes['id'] = 'aggregation_'.$category->id;
-            $attributes['class'] = 'ignoredirty';
-            $aggregation = html_writer::label(get_string('aggregation', 'grades'), 'aggregation_'.$category->id, false, array('class' => 'accesshide'));
-            $aggregation .= html_writer::select($options, 'aggregation_'.$category->id, $category->aggregation, null, $attributes);
-            $action = new component_action('change', 'update_category_aggregation', array('courseid' => $params['id'], 'category' => $category->id, 'sesskey' => sesskey()));
-            $OUTPUT->add_action_handler($action, 'aggregation_'.$category->id);
-        }
+        $aggregation = $options[$category->aggregation];
 
         $categorycell = clone($this->categorycell);
         $categorycell->attributes['class'] .= ' ' . $levelclass;
@@ -721,48 +708,6 @@ class grade_edit_tree_column_aggregation extends grade_edit_tree_column_category
     }
 }
 
-class grade_edit_tree_column_extracredit extends grade_edit_tree_column {
-
-    public function get_header_cell() {
-        global $OUTPUT;
-        $headercell = clone($this->headercell);
-        $headercell->text = get_string('aggregationcoefextra', 'grades').$OUTPUT->help_icon('aggregationcoefextra', 'grades');
-        return $headercell;
-    }
-
-    public function get_category_cell($category, $levelclass, $params) {
-        $item = $category->get_grade_item();
-        $categorycell = clone($this->categorycell);
-        $categorycell->attributes['class'] .= ' ' . $levelclass;
-        $categorycell->text = grade_edit_tree::get_weight_input($item, 'extra');
-        return $categorycell;
-    }
-
-    public function get_item_cell($item, $params) {
-        if (empty($params['element'])) {
-            throw new Exception('Array key (element) missing from 2nd param of grade_edit_tree_column_weightorextracredit::get_item_cell($item, $params)');
-        }
-
-        $itemcell = clone($this->itemcell);
-        $itemcell->text = '&nbsp;';
-
-        if (!in_array($params['element']['object']->itemtype, array('courseitem', 'categoryitem', 'category'))) {
-            $itemcell->text = grade_edit_tree::get_weight_input($item, 'extra');
-        }
-
-        return $itemcell;
-    }
-
-    public function is_hidden($mode='simple') {
-        global $CFG;
-        if ($mode == 'simple') {
-            return strstr($CFG->grade_item_advanced, 'aggregationcoef');
-        } elseif ($mode == 'advanced') {
-            return false;
-        }
-    }
-}
-
 class grade_edit_tree_column_weight extends grade_edit_tree_column {
 
     public function get_header_cell() {
@@ -777,7 +722,7 @@ class grade_edit_tree_column_weight extends grade_edit_tree_column {
         $item = $category->get_grade_item();
         $categorycell = clone($this->categorycell);
         $categorycell->attributes['class']  .= ' ' . $levelclass;
-        $categorycell->text = grade_edit_tree::get_weight_input($item, 'weight');
+        $categorycell->text = grade_edit_tree::get_weight_input($item);
         return $categorycell;
     }
 
@@ -789,7 +734,7 @@ class grade_edit_tree_column_weight extends grade_edit_tree_column {
         $itemcell->text = '&nbsp;';
 
         if (!in_array($params['element']['object']->itemtype, array('courseitem', 'categoryitem', 'category'))) {
-            $itemcell->text = grade_edit_tree::get_weight_input($item, 'weight');
+            $itemcell->text = grade_edit_tree::get_weight_input($item);
         }
 
         return $itemcell;
@@ -830,7 +775,7 @@ class grade_edit_tree_column_range extends grade_edit_tree_column {
             $grademax = ' - ';
         } else if ($parent_cat->aggregation == GRADE_AGGREGATE_SUM) {
             $grademax = format_float($item->grademax, $item->get_decimals());
-        } elseif ($item->gradetype == GRADE_TYPE_SCALE) {
+        } else if ($item->gradetype == GRADE_TYPE_SCALE) {
             $scale = $DB->get_record('scale', array('id' => $item->scaleid));
             $scale_items = null;
             if (empty($scale)) { //if the item is using a scale that's been removed
@@ -839,12 +784,8 @@ class grade_edit_tree_column_range extends grade_edit_tree_column {
                 $scale_items = explode(',', $scale->scale);
             }
             $grademax = end($scale_items) . ' (' . count($scale_items) . ')';
-        } elseif ($item->is_external_item()) {
-            $grademax = format_float($item->grademax, $item->get_decimals());
         } else {
-            $grademax = '<label class="accesshide" for="grademax'.$item->id.'">'.get_string('grademax', 'grades').'</label>
-                <input type="text" size="6" id="grademax'.$item->id.'" name="grademax_'.$item->id.'" value="'.
-                format_float($item->grademax, $item->get_decimals()).'" />';
+            $grademax = format_float($item->grademax, $item->get_decimals());
         }
 
         $itemcell = clone($this->itemcell);
index 1baeea4..85874d7 100644 (file)
@@ -73,6 +73,7 @@ $string['aggregationcoefextra_help'] = 'If the aggregation is Natural or Simple
 If the aggregation is Mean of grades (with extra credits) and the extra credit is set to a value greater than zero, the extra credit is the factor by which the grade is multiplied before adding it to the total after the computation of the mean.';
 $string['aggregationcoefextra_link'] = 'grade/aggregation';
 $string['aggregationcoefextrasum'] = 'Extra credit'; // for the form with checkboxes: Natural or Simple weighted mean
+$string['aggregationcoefextrasumabbr'] = '+';
 $string['aggregationcoefextrasum_help'] = 'If the extra credit checkbox is ticked, the grade item\'s maximum grade is not added to the category\'s maximum grade, resulting in the possibility of achieving the maximum grade (or grades over the maximum if enabled by the site administrator) in the category without having the maximum grade in all the grade items.';
 $string['aggregationcoefextrasum_link'] = 'grade/aggregation';
 $string['aggregationcoefextraweight'] = 'Extra credit weight'; // for the form with input: Mean of grades (with extra credits) only