2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Unit tests for prediction actions.
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
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once(__DIR__
. '/fixtures/test_indicator_max.php');
28 require_once(__DIR__
. '/fixtures/test_target_shortname.php');
31 * Unit tests for prediction actions.
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
37 class analytics_prediction_actions_testcase
extends advanced_testcase
{
40 * Common startup tasks
42 public function setUp() {
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);
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');
56 $this->resetAfterTest(true);
58 $course1 = $this->getDataGenerator()->create_course();
59 $course2 = $this->getDataGenerator()->create_course();
60 $this->context
= \context_course
::instance($course1->id
);
62 $this->teacher1
= $this->getDataGenerator()->create_user();
63 $this->teacher2
= $this->getDataGenerator()->create_user();
65 $this->getDataGenerator()->enrol_user($this->teacher1
->id
, $course1->id
, 'editingteacher');
66 $this->getDataGenerator()->enrol_user($this->teacher2
->id
, $course1->id
, 'editingteacher');
68 // The only relevant fields are modelid, contextid and sampleid. I'm cheating and setting
69 // contextid as the course context so teachers can access these predictions.
70 $pred = new \
stdClass();
71 $pred->modelid
= $this->model
->get_id();
72 $pred->contextid
= $this->context
->id
;
73 $pred->sampleid
= $course1->id
;
74 $pred->rangeindex
= 1;
75 $pred->prediction
= 1;
76 $pred->predictionscore
= 1;
77 $pred->calculations
= json_encode(array('test_indicator_max' => 1));
78 $pred->timecreated
= time();
79 $DB->insert_record('analytics_predictions', $pred);
81 $pred->sampleid
= $course2->id
;
82 $DB->insert_record('analytics_predictions', $pred);
86 * test_get_predictions
88 public function test_action_executed() {
91 $this->assertEquals(0, $DB->count_records('analytics_prediction_actions'));
93 // Teacher 2 flags a prediction (it doesn't matter which one) as fixed.
94 $this->setUser($this->teacher2
);
95 list($ignored, $predictions) = $this->model
->get_predictions($this->context
, true);
96 $prediction = reset($predictions);
97 $prediction->action_executed(\core_analytics\prediction
::ACTION_FIXED
, $this->model
->get_target());
99 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions'));
100 $action = $DB->get_record('analytics_prediction_actions', array('userid' => $this->teacher2
->id
));
101 $this->assertEquals(\core_analytics\prediction
::ACTION_FIXED
, $action->actionname
);
103 $prediction->action_executed(\core_analytics\prediction
::ACTION_NOT_USEFUL
, $this->model
->get_target());
104 $this->assertEquals(2, $DB->count_records('analytics_prediction_actions'));
108 * test_get_predictions
110 public function test_get_predictions() {
112 // Already logged in as admin.
113 list($ignored, $predictions) = $this->model
->get_predictions($this->context
, true);
114 $this->assertCount(2, $predictions);
116 $this->setUser($this->teacher1
);
117 list($ignored, $predictions) = $this->model
->get_predictions($this->context
, true);
118 $this->assertCount(2, $predictions);
120 $this->setUser($this->teacher2
);
121 list($ignored, $predictions) = $this->model
->get_predictions($this->context
, false);
122 $this->assertCount(2, $predictions);
124 // Teacher 2 flags a prediction (it doesn't matter which one) as fixed.
125 $prediction = reset($predictions);
126 $prediction->action_executed(\core_analytics\prediction
::ACTION_FIXED
, $this->model
->get_target());
128 list($ignored, $predictions) = $this->model
->get_predictions($this->context
, true);
129 $this->assertCount(1, $predictions);
130 list($ignored, $predictions) = $this->model
->get_predictions($this->context
, false);
131 $this->assertCount(2, $predictions);
133 // Teacher 1 can still see both predictions.
134 $this->setUser($this->teacher1
);
135 list($ignored, $predictions) = $this->model
->get_predictions($this->context
, true);
136 $this->assertCount(2, $predictions);
137 list($ignored, $predictions) = $this->model
->get_predictions($this->context
, false);
138 $this->assertCount(2, $predictions);