Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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