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