MDL-38559 Clean-up the course completion settings code
[moodle.git] / completion / criteria / completion_criteria_duration.php
CommitLineData
2be4d090 1<?php
2be4d090
MD
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 * Course completion critieria - completion after specific duration from course enrolment
19 *
836375ec
SH
20 * @package core_completion
21 * @category completion
2be4d090 22 * @copyright 2009 Catalyst IT Ltd
836375ec
SH
23 * @author Aaron Barnes <aaronb@catalyst.net.nz>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Course completion critieria - completion after specific duration from course enrolment
31 *
32 * @package core_completion
33 * @category completion
34 * @copyright 2009 Catalyst IT Ltd
35 * @author Aaron Barnes <aaronb@catalyst.net.nz>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2be4d090
MD
37 */
38class completion_criteria_duration extends completion_criteria {
39
95dd54ee 40 /* @var int Criteria type constant [COMPLETION_CRITERIA_TYPE_DURATION] */
2be4d090
MD
41 public $criteriatype = COMPLETION_CRITERIA_TYPE_DURATION;
42
43 /**
44 * Finds and returns a data_object instance based on params.
2be4d090
MD
45 *
46 * @param array $params associative arrays varname=>value
836375ec 47 * @return data_object data_object instance or false if none found.
2be4d090
MD
48 */
49 public static function fetch($params) {
50 $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_DURATION;
51 return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
52 }
53
54 /**
55 * Add appropriate form elements to the critieria form
836375ec
SH
56 *
57 * @param moodleform $mform Moodle forms object
95dd54ee 58 * @param stdClass $data not used
2be4d090
MD
59 */
60 public function config_form_display(&$mform, $data = null) {
61
62 $mform->addElement('checkbox', 'criteria_duration', get_string('enable'));
63
64 $thresholdmenu=array();
65 for ($i=1; $i<=30; $i++) {
66 $seconds = $i * 86400;
67 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
68 }
69 $mform->addElement('select', 'criteria_duration_days', get_string('daysafterenrolment', 'completion'), $thresholdmenu);
70
71 if ($this->id) {
72 $mform->setDefault('criteria_duration', 1);
73 $mform->setDefault('criteria_duration_days', $this->enrolperiod);
74 }
75 }
76
77 /**
78 * Update the criteria information stored in the database
836375ec
SH
79 *
80 * @param stdClass $data Form data
2be4d090
MD
81 */
82 public function update_config(&$data) {
2be4d090
MD
83 if (!empty($data->criteria_duration)) {
84 $this->course = $data->id;
85 $this->enrolperiod = $data->criteria_duration_days;
86 $this->insert();
87 }
88 }
89
90 /**
91 * Get the time this user was enroled
836375ec
SH
92 *
93 * @param completion_completion $completion
94 * @return int
2be4d090
MD
95 */
96 private function get_timeenrolled($completion) {
97 global $DB;
98
b58f4df6
AB
99 return $DB->get_field_sql('
100 SELECT eu.timestart
101 FROM {user_enrolments} eu
102 JOIN {enrol} e ON eu.enrolid = e.id
103 WHERE e.courseid = ?
104 AND eu.userid = ?', array($this->course, $completion->userid));
2be4d090
MD
105 }
106
107 /**
108 * Review this criteria and decide if the user has completed
836375ec
SH
109 *
110 * @param completion_completion $completion The user's completion record
95dd54ee
AA
111 * @param bool $mark Optionally set false to not save changes to database
112 * @return bool
2be4d090
MD
113 */
114 public function review($completion, $mark = true) {
115 $timeenrolled = $this->get_timeenrolled($completion);
116
117 // If duration since enrollment has passed
118 if (!$this->enrolperiod || !$timeenrolled) {
119 return false;
120 }
121
122 if (time() > ($timeenrolled + $this->enrolperiod)) {
123 if ($mark) {
124 $completion->mark_complete();
125 }
126
127 return true;
128 }
129
130 return false;
131 }
132
133 /**
134 * Return criteria title for display in reports
836375ec
SH
135 *
136 * @return string
2be4d090
MD
137 */
138 public function get_title() {
139 return get_string('enrolmentduration', 'completion');
140 }
141
142 /**
143 * Return a more detailed criteria title for display in reports
836375ec
SH
144 *
145 * @return string
2be4d090
MD
146 */
147 public function get_title_detailed() {
148 return ceil($this->enrolperiod / (60 * 60 * 24)) . ' days';
149 }
150
151 /**
152 * Return criteria type title for display in reports
836375ec
SH
153 *
154 * @return string
2be4d090
MD
155 */
156 public function get_type_title() {
157 return get_string('days', 'completion');
158 }
159
160 /**
161 * Return criteria status text for display in reports
836375ec
SH
162 *
163 * @param completion_completion $completion The user's completion record
164 * @return string
2be4d090
MD
165 */
166 public function get_status($completion) {
167 $timeenrolled = $this->get_timeenrolled($completion);
168 $timeleft = $timeenrolled + $this->enrolperiod - time();
169 $enrolperiod = ceil($this->enrolperiod / (60 * 60 * 24));
170
171 $daysleft = ceil($timeleft / (60 * 60 * 24));
172
173 return ($daysleft > 0 ? $daysleft : 0).' of '.$enrolperiod;
174 }
175
176 /**
177 * Find user's who have completed this criteria
2be4d090
MD
178 */
179 public function cron() {
180 global $DB;
181
89482538
AB
182 /*
183 * Get all users who match meet this criteria
184 *
185 * We can safely ignore duplicate enrolments for
186 * a user in a course here as we only care if
187 * one of the enrolments has passed the set
188 * duration.
189 */
2be4d090 190 $sql = '
89482538 191 SELECT
2be4d090 192 c.id AS course,
2be4d090 193 cr.id AS criteriaid,
89482538
AB
194 u.id AS userid,
195 ue.timestart AS otimestart,
196 (ue.timestart + cr.enrolperiod) AS ctimestart,
fbc133e5
AB
197 ue.timecreated AS otimeenrolled,
198 (ue.timecreated + cr.enrolperiod) AS ctimeenrolled
2be4d090 199 FROM
89482538 200 {user} u
2be4d090 201 INNER JOIN
89482538
AB
202 {user_enrolments} ue
203 ON ue.userid = u.id
2be4d090 204 INNER JOIN
89482538
AB
205 {enrol} e
206 ON e.id = ue.enrolid
2be4d090 207 INNER JOIN
89482538
AB
208 {course} c
209 ON c.id = e.courseid
210 INNER JOIN
211 {course_completion_criteria} cr
212 ON c.id = cr.course
2be4d090
MD
213 LEFT JOIN
214 {course_completion_crit_compl} cc
215 ON cc.criteriaid = cr.id
89482538 216 AND cc.userid = u.id
2be4d090
MD
217 WHERE
218 cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_DURATION.'
2be4d090
MD
219 AND c.enablecompletion = 1
220 AND cc.id IS NULL
89482538
AB
221 AND
222 (
223 ue.timestart > 0 AND ue.timestart + cr.enrolperiod < ?
fbc133e5 224 OR ue.timecreated > 0 AND ue.timecreated + cr.enrolperiod < ?
89482538 225 )
2be4d090
MD
226 ';
227
228 // Loop through completions, and mark as complete
89482538 229 $now = time();
419178d7
EL
230 $rs = $DB->get_recordset_sql($sql, array($now, $now));
231 foreach ($rs as $record) {
28e0c239 232 $completion = new completion_criteria_completion((array) $record, DATA_OBJECT_FETCH_BY_KEY);
89482538 233
419178d7
EL
234 // Use time start if not 0, otherwise use timeenrolled
235 if ($record->otimestart) {
236 $completion->mark_complete($record->ctimestart);
237 } else {
238 $completion->mark_complete($record->ctimeenrolled);
2be4d090 239 }
2be4d090 240 }
419178d7 241 $rs->close();
2be4d090
MD
242 }
243
244 /**
245 * Return criteria progress details for display in reports
836375ec
SH
246 *
247 * @param completion_completion $completion The user's completion record
248 * @return array An array with the following keys:
249 * type, criteria, requirement, status
2be4d090
MD
250 */
251 public function get_details($completion) {
252 $details = array();
253 $details['type'] = get_string('periodpostenrolment', 'completion');
254 $details['criteria'] = get_string('remainingenroledfortime', 'completion');
255 $details['requirement'] = get_string('xdays', 'completion', ceil($this->enrolperiod / (60*60*24)));
256
257 // Get status
258 $timeenrolled = $this->get_timeenrolled($completion);
259 $timepassed = time() - $timeenrolled;
260 $details['status'] = get_string('xdays', 'completion', floor($timepassed / (60*60*24)));
261
262 return $details;
263 }
95dd54ee 264}