MDL-60275 analytics: Specify missing prediction_action constructor arg
[moodle.git] / lib / classes / analytics / target / course_dropout.php
CommitLineData
efaf8534
DM
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 * Drop out course target.
19 *
206d7aa9 20 * @package core
efaf8534
DM
21 * @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
206d7aa9 25namespace core\analytics\target;
efaf8534
DM
26
27defined('MOODLE_INTERNAL') || die();
28
29require_once($CFG->dirroot . '/course/lib.php');
30require_once($CFG->dirroot . '/lib/gradelib.php');
31require_once($CFG->dirroot . '/lib/completionlib.php');
32require_once($CFG->dirroot . '/completion/completion_completion.php');
33
34/**
35 * Drop out course target.
36 *
206d7aa9 37 * @package core
efaf8534
DM
38 * @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class course_dropout extends \core_analytics\local\target\binary {
42
413f19bc 43 /**
7e00aa72 44 * Returns the name.
413f19bc 45 *
7e00aa72
MN
46 * If there is a corresponding '_help' string this will be shown as well.
47 *
48 * @return \lang_string
413f19bc 49 */
7e00aa72
MN
50 public static function get_name() : \lang_string {
51 return new \lang_string('target:coursedropout');
efaf8534
DM
52 }
53
413f19bc
DM
54 /**
55 * prediction_actions
56 *
57 * @param \core_analytics\prediction $prediction
58 * @param bool $includedetailsaction
59 * @return \core_analytics\prediction_action[]
60 */
1611308b 61 public function prediction_actions(\core_analytics\prediction $prediction, $includedetailsaction = false) {
efaf8534
DM
62 global $USER;
63
025363d1 64 $actions = array();
efaf8534
DM
65
66 $sampledata = $prediction->get_sample_data();
67 $studentid = $sampledata['user']->id;
68
025363d1
DM
69 $attrs = array('target' => '_blank');
70
efaf8534
DM
71 // Send a message.
72 $url = new \moodle_url('/message/index.php', array('user' => $USER->id, 'id' => $studentid));
73 $pix = new \pix_icon('t/message', get_string('sendmessage', 'message'));
025363d1 74 $actions[] = new \core_analytics\prediction_action('studentmessage', $prediction, $url, $pix,
698f3617 75 get_string('sendmessage', 'message'), false, $attrs);
efaf8534
DM
76
77 // View outline report.
78 $url = new \moodle_url('/report/outline/user.php', array('id' => $studentid, 'course' => $sampledata['course']->id,
79 'mode' => 'outline'));
80 $pix = new \pix_icon('i/report', get_string('outlinereport'));
025363d1 81 $actions[] = new \core_analytics\prediction_action('viewoutlinereport', $prediction, $url, $pix,
698f3617 82 get_string('outlinereport'), false, $attrs);
efaf8534 83
025363d1 84 return array_merge($actions, parent::prediction_actions($prediction, $includedetailsaction));
efaf8534
DM
85 }
86
413f19bc
DM
87 /**
88 * classes_description
89 *
90 * @return string[]
91 */
efaf8534
DM
92 protected static function classes_description() {
93 return array(
206d7aa9
DM
94 get_string('targetlabelstudentdropoutno'),
95 get_string('targetlabelstudentdropoutyes')
efaf8534
DM
96 );
97 }
98
99 /**
100 * Returns the predicted classes that will be ignored.
101 *
102 * Overwriten because we are also interested in knowing when the student is far from the risk of dropping out.
103 *
104 * @return array
105 */
106 protected function ignored_predicted_classes() {
107 return array();
108 }
109
413f19bc
DM
110 /**
111 * get_analyser_class
112 *
113 * @return string
114 */
efaf8534 115 public function get_analyser_class() {
206d7aa9 116 return '\core\analytics\analyser\student_enrolments';
efaf8534
DM
117 }
118
413f19bc 119 /**
1a5204a4 120 * Discards courses that are not yet ready to be used for training or prediction.
413f19bc
DM
121 *
122 * @param \core_analytics\analysable $course
123 * @param bool $fortraining
124 * @return true|string
125 */
efaf8534
DM
126 public function is_valid_analysable(\core_analytics\analysable $course, $fortraining = true) {
127 global $DB;
128
129 if (!$course->was_started()) {
206d7aa9 130 return get_string('coursenotyetstarted');
efaf8534
DM
131 }
132
133 if (!$students = $course->get_students()) {
206d7aa9 134 return get_string('nocoursestudents');
efaf8534
DM
135 }
136
137 if (!course_format_uses_sections($course->get_course_data()->format)) {
138 // We can not split activities in time ranges.
206d7aa9 139 return get_string('nocoursesections');
efaf8534
DM
140 }
141
1cc2b4ba
DM
142 if ($course->get_start() == 0) {
143 // We require time start to be set.
206d7aa9 144 return get_string('nocoursestarttime');
1cc2b4ba
DM
145 }
146
6ec2ae0f
DM
147 if ($course->get_end() == 0) {
148 // We require time end to be set.
206d7aa9 149 return get_string('nocourseendtime');
6ec2ae0f
DM
150 }
151
1a5204a4
DM
152 if ($course->get_end() < $course->get_start()) {
153 return get_string('errorendbeforestart', 'analytics');
154 }
155
156 // A course that lasts longer than 1 year probably have wrong start or end dates.
157 if ($course->get_end() - $course->get_start() > (YEARSECS + (WEEKSECS * 4))) {
158 return get_string('coursetoolong', 'analytics');
159 }
160
efaf8534
DM
161 // Ongoing courses data can not be used to train.
162 if ($fortraining && !$course->is_finished()) {
206d7aa9 163 return get_string('coursenotyetfinished');
efaf8534
DM
164 }
165
166 if ($fortraining) {
1a5204a4 167 // Not a valid target for training if there are not enough course accesses between the course start and end dates.
f67f35f3 168
efaf8534
DM
169 $params = array('courseid' => $course->get_id(), 'anonymous' => 0, 'start' => $course->get_start(),
170 'end' => $course->get_end());
171 list($studentssql, $studentparams) = $DB->get_in_or_equal($students, SQL_PARAMS_NAMED);
f67f35f3 172 // Using anonymous to use the db index, not filtering by timecreated to speed it up.
efaf8534
DM
173 $select = 'courseid = :courseid AND anonymous = :anonymous AND timecreated > :start AND timecreated < :end ' .
174 'AND userid ' . $studentssql;
f67f35f3 175
2db6e981
DM
176 if (!$logstore = \core_analytics\manager::get_analytics_logstore()) {
177 throw new \coding_exception('No available log stores');
178 }
f67f35f3 179 $nlogs = $logstore->get_events_select_count($select, array_merge($params, $studentparams));
efaf8534
DM
180
181 // At least a minimum of students activity.
182 $nstudents = count($students);
183 if ($nlogs / $nstudents < 10) {
206d7aa9 184 return get_string('nocourseactivity');
efaf8534
DM
185 }
186 }
187
188 return true;
189 }
190
357507b4 191 /**
1a5204a4 192 * Discard student enrolments that are invalid.
357507b4
DM
193 *
194 * @param int $sampleid
195 * @param \core_analytics\analysable $course
1611308b 196 * @param bool $fortraining
1a5204a4 197 * @return bool
357507b4 198 */
1611308b 199 public function is_valid_sample($sampleid, \core_analytics\analysable $course, $fortraining = true) {
1a5204a4
DM
200
201 $userenrol = $this->retrieve('user_enrolments', $sampleid);
202 if ($userenrol->timeend && $course->get_start() > $userenrol->timeend) {
203 // Discard enrolments which time end is prior to the course start. This should get rid of
204 // old user enrolments that remain on the course.
205 return false;
206 }
207
208 $limit = $course->get_start() - (YEARSECS + (WEEKSECS * 4));
209 if (($userenrol->timestart && $userenrol->timestart < $limit) ||
210 (!$userenrol->timestart && $userenrol->timecreated < $limit)) {
211 // Following what we do in is_valid_sample, we will discard enrolments that last more than 1 academic year
212 // because they have incorrect start and end dates or because they are reused along multiple years
213 // without removing previous academic years students. This may not be very accurate because some courses
214 // can last just some months, but it is better than nothing and they will be flagged as drop out anyway
215 // in most of the cases.
216 return false;
217 }
218
219 if (($userenrol->timestart && $userenrol->timestart > $course->get_end()) ||
220 (!$userenrol->timestart && $userenrol->timecreated > $course->get_end())) {
221 // Discard user enrolments that starts after the analysable official end.
222 return false;
223 }
224
357507b4
DM
225 return true;
226 }
227
efaf8534
DM
228 /**
229 * calculate_sample
230 *
231 * The meaning of a drop out changes depending on the settings enabled in the course. Following these priorities order:
232 * 1.- Course completion
233 * 2.- No logs during the last quarter of the course
234 *
235 * @param int $sampleid
236 * @param \core_analytics\analysable $course
413f19bc
DM
237 * @param int $starttime
238 * @param int $endtime
690ad875 239 * @return float 0 -> not at risk, 1 -> at risk
efaf8534 240 */
357507b4 241 protected function calculate_sample($sampleid, \core_analytics\analysable $course, $starttime = false, $endtime = false) {
efaf8534 242
584ffa4f 243 $userenrol = $this->retrieve('user_enrolments', $sampleid);
efaf8534
DM
244
245 // We use completion as a success metric only when it is enabled.
246 $completion = new \completion_info($course->get_course_data());
247 if ($completion->is_enabled() && $completion->has_criteria()) {
248 $ccompletion = new \completion_completion(array('userid' => $userenrol->userid, 'course' => $course->get_id()));
249 if ($ccompletion->is_complete()) {
250 return 0;
251 } else {
252 return 1;
253 }
254 }
255
2db6e981
DM
256 if (!$logstore = \core_analytics\manager::get_analytics_logstore()) {
257 throw new \coding_exception('No available log stores');
258 }
259
efaf8534
DM
260 // No logs during the last quarter of the course.
261 $courseduration = $course->get_end() - $course->get_start();
262 $limit = intval($course->get_end() - ($courseduration / 4));
f67f35f3 263 $select = "courseid = :courseid AND userid = :userid AND timecreated > :limit";
efaf8534 264 $params = array('userid' => $userenrol->userid, 'courseid' => $course->get_id(), 'limit' => $limit);
f67f35f3
DM
265 $nlogs = $logstore->get_events_select_count($select, $params);
266 if ($nlogs == 0) {
690ad875 267 return 1;
efaf8534 268 }
690ad875 269 return 0;
efaf8534
DM
270 }
271}