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] [Versions 401 and 402] [Versions 401 and 403]

   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_analytics;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  require_once (__DIR__ . '/fixtures/test_indicator_max.php');
  22  require_once (__DIR__ . '/fixtures/test_target_shortname.php');
  23  
  24  /**
  25   * Unit tests for prediction actions.
  26   *
  27   * @package   core_analytics
  28   * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
  29   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class prediction_actions_test extends \advanced_testcase {
  32  
  33      /**
  34       * Common startup tasks
  35       */
  36      public function setUp(): void {
  37          global $DB;
  38  
  39          $this->setAdminUser();
  40          $target = \core_analytics\manager::get_target('test_target_shortname');
  41          $indicators = array('test_indicator_max');
  42          foreach ($indicators as $key => $indicator) {
  43              $indicators[$key] = \core_analytics\manager::get_indicator($indicator);
  44          }
  45  
  46          $this->model = \core_analytics\model::create($target, $indicators);
  47          $this->modelobj = $this->model->get_model_obj();
  48          $this->model->enable('\core\analytics\time_splitting\single_range');
  49  
  50          $this->resetAfterTest(true);
  51  
  52          $this->course1 = $this->getDataGenerator()->create_course();
  53          $this->course2 = $this->getDataGenerator()->create_course();
  54          $this->context = \context_course::instance($this->course1->id);
  55  
  56          $this->teacher1 = $this->getDataGenerator()->create_user();
  57          $this->teacher2 = $this->getDataGenerator()->create_user();
  58          $this->teacher3 = $this->getDataGenerator()->create_user();
  59  
  60          $this->getDataGenerator()->enrol_user($this->teacher1->id, $this->course1->id, 'editingteacher');
  61          $this->getDataGenerator()->enrol_user($this->teacher2->id, $this->course1->id, 'editingteacher');
  62          $this->getDataGenerator()->enrol_user($this->teacher3->id, $this->course1->id, 'editingteacher');
  63  
  64          // The only relevant fields are modelid, contextid and sampleid. I'm cheating and setting
  65          // contextid as the course context so teachers can access these predictions.
  66          $pred = new \stdClass();
  67          $pred->modelid = $this->model->get_id();
  68          $pred->contextid = $this->context->id;
  69          $pred->sampleid = $this->course1->id;
  70          $pred->rangeindex = 1;
  71          $pred->prediction = 1;
  72          $pred->predictionscore = 1;
  73          $pred->calculations = json_encode(array('test_indicator_max' => 1));
  74          $pred->timecreated = time();
  75          $DB->insert_record('analytics_predictions', $pred);
  76  
  77          $pred->sampleid = $this->course2->id;
  78          $DB->insert_record('analytics_predictions', $pred);
  79      }
  80  
  81      /**
  82       * test_get_predictions
  83       */
  84      public function test_action_executed() {
  85          global $DB;
  86  
  87          $this->assertEquals(0, $DB->count_records('analytics_prediction_actions'));
  88  
  89          // Teacher 2 flags a prediction (it doesn't matter which one) as fixed.
  90          $this->setUser($this->teacher2);
  91          list($ignored, $predictions) = $this->model->get_predictions($this->context, true);
  92          $prediction = reset($predictions);
  93          $prediction->action_executed(\core_analytics\prediction::ACTION_FIXED, $this->model->get_target());
  94  
  95          $recordset = $this->model->get_prediction_actions($this->context);
  96          $this->assertCount(1, $recordset);
  97          $recordset->close();
  98          $this->assertEquals(1, $DB->count_records('analytics_prediction_actions'));
  99          $action = $DB->get_record('analytics_prediction_actions', array('userid' => $this->teacher2->id));
 100          $this->assertEquals(\core_analytics\prediction::ACTION_FIXED, $action->actionname);
 101  
 102          $prediction->action_executed(\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED, $this->model->get_target());
 103          $recordset = $this->model->get_prediction_actions($this->context);
 104          $this->assertCount(2, $recordset);
 105          $recordset->close();
 106          $this->assertEquals(2, $DB->count_records('analytics_prediction_actions'));
 107      }
 108  
 109      /**
 110       * Data provider for test_get_executed_actions.
 111       *
 112       * @return  array
 113       */
 114      public function execute_actions_provider(): array {
 115          return [
 116              'Empty actions with no filter' => [
 117                  [],
 118                  [],
 119                  0
 120              ],
 121              'Empty actions with filter' => [
 122                  [],
 123                  [\core_analytics\prediction::ACTION_FIXED],
 124                  0
 125              ],
 126              'Multiple actions with no filter' => [
 127                  [
 128                      \core_analytics\prediction::ACTION_FIXED,
 129                      \core_analytics\prediction::ACTION_FIXED,
 130                      \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED
 131                  ],
 132                  [],
 133                  3
 134              ],
 135              'Multiple actions applying filter' => [
 136                  [
 137                      \core_analytics\prediction::ACTION_FIXED,
 138                      \core_analytics\prediction::ACTION_FIXED,
 139                      \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED
 140                  ],
 141                  [\core_analytics\prediction::ACTION_FIXED],
 142                  2
 143              ],
 144              'Multiple actions not applying filter' => [
 145                  [
 146                      \core_analytics\prediction::ACTION_FIXED,
 147                      \core_analytics\prediction::ACTION_FIXED,
 148                      \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED
 149                  ],
 150                  [\core_analytics\prediction::ACTION_NOT_APPLICABLE],
 151                  0
 152              ],
 153              'Multiple actions with multiple filter' => [
 154                  [
 155                      \core_analytics\prediction::ACTION_FIXED,
 156                      \core_analytics\prediction::ACTION_FIXED,
 157                      \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED
 158                  ],
 159                  [\core_analytics\prediction::ACTION_FIXED, \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED],
 160                  3
 161              ],
 162          ];
 163      }
 164  
 165      /**
 166       * Tests for get_executed_actions() function.
 167       *
 168       * @dataProvider    execute_actions_provider
 169       * @param   array   $actionstoexecute    An array of actions to execute
 170       * @param   array   $actionnamefilter   Actions to filter
 171       * @param   int     $returned             Number of actions returned
 172       *
 173       * @covers \core_analytics\prediction::get_executed_actions
 174       */
 175      public function test_get_executed_actions(array $actionstoexecute, array $actionnamefilter, int $returned) {
 176  
 177          $this->setUser($this->teacher2);
 178          list($ignored, $predictions) = $this->model->get_predictions($this->context, true);
 179          $prediction = reset($predictions);
 180          $target = $this->model->get_target();
 181          foreach($actionstoexecute as $action) {
 182              $prediction->action_executed($action, $target);
 183          }
 184  
 185          $filteredactions = $prediction->get_executed_actions($actionnamefilter);
 186          $this->assertCount($returned, $filteredactions);
 187      }
 188  
 189      /**
 190       * test_get_predictions
 191       */
 192      public function test_get_predictions() {
 193          global $DB;
 194  
 195          // Already logged in as admin.
 196          list($ignored, $predictions) = $this->model->get_predictions($this->context, true);
 197          $this->assertCount(2, $predictions);
 198  
 199          $this->setUser($this->teacher1);
 200          list($ignored, $predictions) = $this->model->get_predictions($this->context, true);
 201          $this->assertCount(2, $predictions);
 202  
 203          $this->setUser($this->teacher2);
 204          list($ignored, $predictions) = $this->model->get_predictions($this->context, false);
 205          $this->assertCount(2, $predictions);
 206  
 207          // Teacher 2 flags a prediction (it doesn't matter which one).
 208          $prediction = reset($predictions);
 209          $prediction->action_executed(\core_analytics\prediction::ACTION_FIXED, $this->model->get_target());
 210          $prediction->action_executed(\core_analytics\prediction::ACTION_NOT_APPLICABLE, $this->model->get_target());
 211          $prediction->action_executed(\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED, $this->model->get_target());
 212  
 213          $recordset = $this->model->get_prediction_actions($this->context);
 214          $this->assertCount(3, $recordset);
 215          $recordset->close();
 216  
 217          list($ignored, $predictions) = $this->model->get_predictions($this->context, true);
 218          $this->assertCount(1, $predictions);
 219          list($ignored, $predictions) = $this->model->get_predictions($this->context, false);
 220          $this->assertCount(2, $predictions);
 221  
 222          // Teacher 1 can still see both predictions.
 223          $this->setUser($this->teacher1);
 224          list($ignored, $predictions) = $this->model->get_predictions($this->context, true);
 225          $this->assertCount(2, $predictions);
 226          list($ignored, $predictions) = $this->model->get_predictions($this->context, false);
 227          $this->assertCount(2, $predictions);
 228  
 229          $recordset = $this->model->get_prediction_actions($this->context);
 230          $this->assertCount(3, $recordset);
 231          $recordset->close();
 232  
 233          // Trying with a deleted course.
 234          $DB->delete_records('course', ['id' => $this->course2->id]);
 235          $this->setUser($this->teacher3);
 236          list($ignored, $predictions) = $this->model->get_predictions($this->context);
 237          $this->assertCount(1, $predictions);
 238          reset($predictions)->action_executed(\core_analytics\prediction::ACTION_FIXED, $this->model->get_target());
 239          $this->assertEmpty($this->model->get_predictions($this->context));
 240      }
 241  }