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