Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace core_grades;
  18  
  19  use grade_edit_tree;
  20  use grade_edit_tree_column;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  require_once($CFG->dirroot.'/grade/edit/tree/lib.php');
  26  
  27  /**
  28   * Tests grade_edit_tree (deals with the data on the 'Gradebook setup' page in the gradebook)
  29   *
  30   * @package  core_grades
  31   * @category test
  32   * @author   Andrew Davis
  33   * @license  http://www.gnu.org/copyleft/gpl.html GNU Public License
  34   */
  35  class edittreelib_test extends \advanced_testcase {
  36      public function test_format_number() {
  37          $numinput = array(0,   1,   1.01, '1.010', 1.2345);
  38          $numoutput = array(0.0, 1.0, 1.01,  1.01,   1.2345);
  39  
  40          for ($i = 0; $i < count($numinput); $i++) {
  41              $msg = 'format_number() testing '.$numinput[$i].' %s';
  42              $this->assertEquals(grade_edit_tree::format_number($numinput[$i]), $numoutput[$i], $msg);
  43          }
  44      }
  45  
  46      public function test_grade_edit_tree_column_range_get_item_cell() {
  47          global $DB, $CFG;
  48  
  49          $this->resetAfterTest(true);
  50  
  51          // Make some things we need.
  52          $scale = $this->getDataGenerator()->create_scale();
  53          $course = $this->getDataGenerator()->create_course();
  54          $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
  55          $modulecontext = \context_module::instance($assign->cmid);
  56          // The generator returns a dummy object, lets get the real assign object.
  57          $assign = new \assign($modulecontext, false, false);
  58          $cm = $assign->get_course_module();
  59  
  60          // Get range column.
  61          $column = grade_edit_tree_column::factory('range');
  62  
  63          $gradeitemparams = array(
  64              'itemtype'     => 'mod',
  65              'itemmodule'   => $cm->modname,
  66              'iteminstance' => $cm->instance,
  67              'courseid'     => $cm->course,
  68              'itemnumber'   => 0
  69          );
  70  
  71          // Lets set the grade to something we know.
  72          $instance = $assign->get_instance();
  73          $instance->grade = 70;
  74          $instance->instance = $instance->id;
  75          $assign->update_instance($instance);
  76  
  77          $gradeitem = \grade_item::fetch($gradeitemparams);
  78          $cell = $column->get_item_cell($gradeitem, array());
  79  
  80          $this->assertEquals(GRADE_TYPE_VALUE, $gradeitem->gradetype);
  81          $this->assertEquals(null, $gradeitem->scaleid);
  82          $this->assertEqualsWithDelta(70.0, (float) $cell->text, 0.01, "Grade text is 70");
  83  
  84          // Now change it to a scale.
  85          $instance = $assign->get_instance();
  86          $instance->grade = -($scale->id);
  87          $instance->instance = $instance->id;
  88          $assign->update_instance($instance);
  89  
  90          $gradeitem = \grade_item::fetch($gradeitemparams);
  91          $cell = $column->get_item_cell($gradeitem, array());
  92  
  93          // Make the expected scale text.
  94          $scaleitems = null;
  95          $scaleitems = explode(',', $scale->scale);
  96          // Make sure that we expect grademax (displayed in parenthesis) be the same
  97          // as number of items in the scale.
  98          $scalestring = end($scaleitems) . ' (' .
  99                  format_float(count($scaleitems), 2) . ')';
 100  
 101          $this->assertEquals(GRADE_TYPE_SCALE, $gradeitem->gradetype);
 102          $this->assertEquals($scale->id, $gradeitem->scaleid);
 103          $this->assertEquals($scalestring, $cell->text, "Grade text matches scale");
 104  
 105          // Now change it to no grade with gradebook feedback enabled.
 106          $adminconfig = $assign->get_admin_config();
 107          $gradebookplugin = $adminconfig->feedback_plugin_for_gradebook;
 108          $gradebookplugin .= '_enabled';
 109  
 110          $instance = $assign->get_instance();
 111          $instance->grade = 0;
 112          $instance->$gradebookplugin = 1;
 113          $instance->instance = $instance->id;
 114          $assign->update_instance($instance);
 115  
 116          $gradeitem = \grade_item::fetch($gradeitemparams);
 117          $cell = $column->get_item_cell($gradeitem, array());
 118  
 119          $this->assertEquals(GRADE_TYPE_TEXT, $gradeitem->gradetype);
 120          $this->assertEquals(null, $gradeitem->scaleid);
 121          $this->assertEquals(' - ', $cell->text, 'Grade text matches empty value of " - "');
 122  
 123          // Now change it to no grade with gradebook feedback disabled.
 124          $instance = $assign->get_instance();
 125          $instance->grade = 0;
 126          $instance->$gradebookplugin = 0;
 127          $instance->instance = $instance->id;
 128          $assign->update_instance($instance);
 129  
 130          $gradeitem = \grade_item::fetch($gradeitemparams);
 131          $cell = $column->get_item_cell($gradeitem, array());
 132  
 133          $this->assertEquals(GRADE_TYPE_NONE, $gradeitem->gradetype);
 134          $this->assertEquals(null, $gradeitem->scaleid);
 135      }
 136  }
 137  
 138