Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 39 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  /**
  18   * Unit tests for report_insights externallib.
  19   *
  20   * @package   report_insights
  21   * @copyright 2019 David MonllaĆ³ {@link http://www.davidmonllao.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace report_insights;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once (__DIR__ . '/../../../analytics/tests/fixtures/test_indicator_max.php');
  30  require_once (__DIR__ . '/../../../analytics/tests/fixtures/test_target_shortname.php');
  31  
  32  /**
  33   * Unit tests for report_insights externallib.
  34   *
  35   * @package   report_insights
  36   * @copyright 2019 David MonllaĆ³ {@link http://www.davidmonllao.com}
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class externallib_test extends \advanced_testcase {
  40  
  41      /**
  42       * test_action_executed
  43       */
  44      public function test_action_executed() {
  45          global $DB;
  46  
  47          $this->setAdminUser();
  48          $target = \core_analytics\manager::get_target('test_target_shortname');
  49          $indicators = array('test_indicator_max');
  50          foreach ($indicators as $key => $indicator) {
  51              $indicators[$key] = \core_analytics\manager::get_indicator($indicator);
  52          }
  53  
  54          $model = \core_analytics\model::create($target, $indicators);
  55          $modelobj = $model->get_model_obj();
  56          $model->enable('\core\analytics\time_splitting\single_range');
  57  
  58          $this->resetAfterTest(true);
  59  
  60          $course1 = $this->getDataGenerator()->create_course();
  61          $course2 = $this->getDataGenerator()->create_course();
  62          $context = \context_course::instance($course1->id);
  63  
  64          $teacher1 = $this->getDataGenerator()->create_user();
  65          $teacher2 = $this->getDataGenerator()->create_user();
  66  
  67          $this->getDataGenerator()->enrol_user($teacher1->id, $course1->id, 'editingteacher');
  68          $this->getDataGenerator()->enrol_user($teacher2->id, $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 = $model->get_id();
  74          $pred->contextid = $context->id;
  75          $pred->sampleid = $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 = $course2->id;
  84          $DB->insert_record('analytics_predictions', $pred);
  85  
  86          $this->assertEquals(0, $DB->count_records('analytics_prediction_actions'));
  87  
  88          // Teacher 2 flags a prediction (it doesn't matter which one) as fixed.
  89          $this->setUser($teacher2);
  90          list($ignored, $predictions) = $model->get_predictions($context, true);
  91          $prediction = reset($predictions);
  92  
  93          \report_insights\external::action_executed(\core_analytics\prediction::ACTION_FIXED,
  94              [$prediction->get_prediction_data()->id]);
  95          $recordset = $model->get_prediction_actions($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' => $teacher2->id));
 100          $this->assertEquals(\core_analytics\prediction::ACTION_FIXED, $action->actionname);
 101  
 102          \report_insights\external::action_executed(\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED,
 103              [$prediction->get_prediction_data()->id]);
 104          $recordset = $model->get_prediction_actions($context);
 105          $this->assertCount(2, $recordset);
 106          $recordset->close();
 107          $this->assertEquals(2, $DB->count_records('analytics_prediction_actions'));
 108      }
 109  }