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