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