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.
   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;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once("$CFG->libdir/graphlib.php");
  23  
  24  /**
  25   * Tests for Graphlib.
  26   *
  27   * @coversDefaultClass \graph
  28   * @package    core
  29   * @copyright  2023 Meirza (meirza.arson@moodle.com)
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class graphlib_test extends \basic_testcase {
  33  
  34      /**
  35       * Data provider for test_graphlib.
  36       *
  37       * @return array
  38       */
  39      public function create_data(): array {
  40          return [
  41              'data' =>
  42              [
  43                  'mock' => [
  44                      'survey_name' => 'Survey 101',
  45                      'names' => [
  46                          'Relevance', 'Reflective thinking', 'Interactivity', 'Tutor support', 'Peer support', 'Interpretation'
  47                      ],
  48                      'buckets1' => [
  49                          0.75, 2.5, 1.5, 1.5, 2.5, 2.5
  50                      ],
  51                      'stractual' => 'Actual',
  52                      'buckets2' => [
  53                          -1, -1, -1, -1, -1, -1
  54                      ],
  55                      'strpreferred' => 'Preferred',
  56                      'stdev1' => [
  57                          0.82915619758885, 1.1180339887499, 1.1180339887499, 1.1180339887499, 1.1180339887499, 1.1180339887499
  58                      ],
  59                      'stdev2' => [
  60                          0, 0, 0, 0, 0, 0
  61                      ],
  62                      'options' => [
  63                          'Almost never', 'Seldom', 'Sometimes', 'Often', 'Almost always'
  64                      ],
  65                      'maxbuckets1' => 2.5,
  66                      'maxbuckets2' => -1
  67                  ]
  68              ]
  69          ];
  70      }
  71  
  72      /**
  73       * Test graphlib.
  74       *
  75       * @dataProvider create_data
  76       * @covers ::output
  77       * @param array $mock
  78       * @return void
  79       */
  80      public function test_graphlib($mock) {
  81          $graph = new \graph(300, 200);
  82          ob_start();
  83          $graph->parameter['title'] = strip_tags(format_string($mock['survey_name'], true));
  84          $graph->x_data = $mock['names'];
  85          $graph->y_data['answers1'] = $mock['buckets1'];
  86          $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
  87                  'shadow_offset' => 4, 'legend' => $mock['stractual']);
  88          $graph->y_data['answers2'] = $mock['buckets2'];
  89          $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
  90                  'shadow_offset' => 4, 'legend' => $mock['strpreferred']);
  91          $graph->y_data['stdev1'] = $mock['stdev1'];
  92          $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
  93                  'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3);
  94          $graph->y_data['stdev2'] = $mock['stdev2'];
  95          $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
  96                  'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2);
  97          $graph->offset_relation['stdev1'] = 'answers1';
  98          $graph->offset_relation['stdev2'] = 'answers2';
  99          $graph->parameter['legend'] = 'outside-top';
 100          $graph->parameter['legend_border'] = 'black';
 101          $graph->parameter['legend_offset'] = 4;
 102          $graph->y_tick_labels = $mock['options'];
 103          if (($mock['maxbuckets1'] > 0.0) && ($mock['maxbuckets2'] > 0.0)) {
 104              $graph->y_order = array('stdev1', 'answers1', 'stdev2', 'answers2');
 105          } else if ($mock['maxbuckets1'] > 0.0) {
 106              $graph->y_order = array('stdev1', 'answers1');
 107          } else {
 108              $graph->y_order = array('stdev2', 'answers2');
 109          }
 110          $graph->parameter['y_max_left'] = 4;
 111          $graph->parameter['y_axis_gridlines'] = 5;
 112          $graph->parameter['y_resolution_left'] = 1;
 113          $graph->parameter['y_decimal_left'] = 1;
 114          $graph->parameter['x_axis_angle'] = 0;
 115          $graph->parameter['x_inner_padding'] = 6;
 116          @$graph->draw();
 117          $res = ob_end_clean();
 118  
 119          $this->assertTrue($res);
 120      }
 121  }