Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   1  <?php
   2  // This file is part of Moodle - https://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  /**
  18   * Provides the {@link analytics_stats_testcase} class.
  19   *
  20   * @package     core_analytics
  21   * @category    test
  22   * @copyright   2019 David Mudrák <david@moodle.com>
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once (__DIR__ . '/fixtures/test_indicator_fullname.php');
  29  require_once (__DIR__ . '/fixtures/test_target_shortname.php');
  30  
  31  /**
  32   * Unit tests for the analytics stats.
  33   *
  34   * @copyright 2019 David Mudrák <david@moodle.com>
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class analytics_stats_testcase extends advanced_testcase {
  38  
  39      /**
  40       * Set up the test environment.
  41       */
  42      public function setUp(): void {
  43  
  44          $this->setAdminUser();
  45      }
  46  
  47      /**
  48       * Test the {@link \core_analytics\stats::enabled_models()} implementation.
  49       */
  50      public function test_enabled_models() {
  51  
  52          $this->resetAfterTest(true);
  53  
  54          // By default, sites have {@link \core_course\analytics\target\no_teaching} and
  55          // {@link \core_user\analytics\target\upcoming_activities_due} enabled.
  56          $this->assertEquals(4, \core_analytics\stats::enabled_models());
  57  
  58          $model = \core_analytics\model::create(
  59              \core_analytics\manager::get_target('\core_course\analytics\target\course_dropout'),
  60              [
  61                  \core_analytics\manager::get_indicator('\core\analytics\indicator\any_write_action'),
  62              ]
  63          );
  64  
  65          // Purely adding a new model does not make it included in the stats.
  66          $this->assertEquals(4, \core_analytics\stats::enabled_models());
  67  
  68          // New models must be enabled to have them counted.
  69          $model->enable('\core\analytics\time_splitting\quarters');
  70          $this->assertEquals(5, \core_analytics\stats::enabled_models());
  71      }
  72  
  73      /**
  74       * Test the {@link \core_analytics\stats::predictions()} implementation.
  75       */
  76      public function test_predictions() {
  77  
  78          $this->resetAfterTest(true);
  79  
  80          $model = \core_analytics\model::create(
  81              \core_analytics\manager::get_target('test_target_shortname'),
  82              [
  83                  \core_analytics\manager::get_indicator('test_indicator_fullname'),
  84              ]
  85          );
  86  
  87          $model->enable('\core\analytics\time_splitting\no_splitting');
  88  
  89          // Train the model.
  90          $this->getDataGenerator()->create_course(['shortname' => 'a', 'fullname' => 'a', 'visible' => 1]);
  91          $this->getDataGenerator()->create_course(['shortname' => 'b', 'fullname' => 'b', 'visible' => 1]);
  92          $model->train();
  93  
  94          // No predictions yet.
  95          $this->assertEquals(0, \core_analytics\stats::predictions());
  96  
  97          // Get one new prediction.
  98          $this->getDataGenerator()->create_course(['shortname' => 'aa', 'fullname' => 'aa', 'visible' => 0]);
  99          $result = $model->predict();
 100  
 101          $this->assertEquals(1, count($result->predictions));
 102          $this->assertEquals(1, \core_analytics\stats::predictions());
 103  
 104          // Nothing changes if there is no new prediction.
 105          $result = $model->predict();
 106          $this->assertFalse(isset($result->predictions));
 107          $this->assertEquals(1, \core_analytics\stats::predictions());
 108  
 109          // Get two more predictions, we have three in total now.
 110          $this->getDataGenerator()->create_course(['shortname' => 'bb', 'fullname' => 'bb', 'visible' => 0]);
 111          $this->getDataGenerator()->create_course(['shortname' => 'cc', 'fullname' => 'cc', 'visible' => 0]);
 112  
 113          $result = $model->predict();
 114          $this->assertEquals(2, count($result->predictions));
 115          $this->assertEquals(3, \core_analytics\stats::predictions());
 116      }
 117  
 118      /**
 119       * Test the {@link \core_analytics\stats::actions()} and {@link \core_analytics\stats::actions_not_useful()} implementation.
 120       */
 121      public function test_actions() {
 122          global $DB;
 123          $this->resetAfterTest(true);
 124  
 125          $model = \core_analytics\model::create(
 126              \core_analytics\manager::get_target('test_target_shortname'),
 127              [
 128                  \core_analytics\manager::get_indicator('test_indicator_fullname'),
 129              ]
 130          );
 131  
 132          $model->enable('\core\analytics\time_splitting\no_splitting');
 133  
 134          // Train the model.
 135          $this->getDataGenerator()->create_course(['shortname' => 'a', 'fullname' => 'a', 'visible' => 1]);
 136          $this->getDataGenerator()->create_course(['shortname' => 'b', 'fullname' => 'b', 'visible' => 1]);
 137          $model->train();
 138  
 139          // Generate two predictions.
 140          $this->getDataGenerator()->create_course(['shortname' => 'aa', 'fullname' => 'aa', 'visible' => 0]);
 141          $this->getDataGenerator()->create_course(['shortname' => 'bb', 'fullname' => 'bb', 'visible' => 0]);
 142          $model->predict();
 143  
 144          list($p1, $p2) = array_values($DB->get_records('analytics_predictions'));
 145  
 146          $p1 = new \core_analytics\prediction($p1, []);
 147          $p2 = new \core_analytics\prediction($p2, []);
 148  
 149          // No actions executed at the start.
 150          $this->assertEquals(0, \core_analytics\stats::actions());
 151          $this->assertEquals(0, \core_analytics\stats::actions_not_useful());
 152  
 153          // The user has acknowledged the first prediction.
 154          $p1->action_executed(\core_analytics\prediction::ACTION_FIXED, $model->get_target());
 155          $this->assertEquals(1, \core_analytics\stats::actions());
 156          $this->assertEquals(0, \core_analytics\stats::actions_not_useful());
 157  
 158          // The user has marked the other prediction as not useful.
 159          $p2->action_executed(\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED, $model->get_target());
 160          $this->assertEquals(2, \core_analytics\stats::actions());
 161          $this->assertEquals(1, \core_analytics\stats::actions_not_useful());
 162      }
 163  }