3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Course completion critieria - completion after specific duration from course enrolment
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
26 class completion_criteria_duration extends completion_criteria {
29 * Criteria type constant
32 public $criteriatype = COMPLETION_CRITERIA_TYPE_DURATION;
35 * Finds and returns a data_object instance based on params.
38 * @param array $params associative arrays varname=>value
39 * @return object data_object instance or false if none found.
41 public static function fetch($params) {
42 $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_DURATION;
43 return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
47 * Add appropriate form elements to the critieria form
49 * @param object $mform Moodle forms object
50 * @param mixed $data optional
53 public function config_form_display(&$mform, $data = null) {
55 $mform->addElement('checkbox', 'criteria_duration', get_string('enable'));
57 $thresholdmenu=array();
58 for ($i=1; $i<=30; $i++) {
59 $seconds = $i * 86400;
60 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
62 $mform->addElement('select', 'criteria_duration_days', get_string('daysafterenrolment', 'completion'), $thresholdmenu);
65 $mform->setDefault('criteria_duration', 1);
66 $mform->setDefault('criteria_duration_days', $this->enrolperiod);
71 * Update the criteria information stored in the database
73 * @param array $data Form data
76 public function update_config(&$data) {
78 if (!empty($data->criteria_duration)) {
79 $this->course = $data->id;
80 $this->enrolperiod = $data->criteria_duration_days;
86 * Get the time this user was enroled
87 * @param object $completion
90 private function get_timeenrolled($completion) {
93 return $DB->get_field('user_enrolments', 'timestart', array('courseid' => $this->course, 'userid' => $completion->userid));
97 * Review this criteria and decide if the user has completed
99 * @param object $completion The user's completion record
100 * @param boolean $mark Optionally set false to not save changes to database
103 public function review($completion, $mark = true) {
104 $timeenrolled = $this->get_timeenrolled($completion);
106 // If duration since enrollment has passed
107 if (!$this->enrolperiod || !$timeenrolled) {
111 if (time() > ($timeenrolled + $this->enrolperiod)) {
113 $completion->mark_complete();
123 * Return criteria title for display in reports
127 public function get_title() {
128 return get_string('enrolmentduration', 'completion');
132 * Return a more detailed criteria title for display in reports
136 public function get_title_detailed() {
137 return ceil($this->enrolperiod / (60 * 60 * 24)) . ' days';
141 * Return criteria type title for display in reports
145 public function get_type_title() {
146 return get_string('days', 'completion');
150 * Return criteria status text for display in reports
152 * @param object $completion The user's completion record
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));
160 $daysleft = ceil($timeleft / (60 * 60 * 24));
162 return ($daysleft > 0 ? $daysleft : 0).' of '.$enrolperiod;
166 * Find user's who have completed this criteria
170 public function cron() {
174 * Get all users who match meet this criteria
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
186 ue.timestart AS otimestart,
187 (ue.timestart + cr.enrolperiod) AS ctimestart,
188 ue.timecreated AS otimeenrolled,
189 (ue.timecreated + cr.enrolperiod) AS ctimeenrolled
202 {course_completion_criteria} cr
205 {course_completion_crit_compl} cc
206 ON cc.criteriaid = cr.id
209 cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_DURATION.'
210 AND c.enablecompletion = 1
214 ue.timestart > 0 AND ue.timestart + cr.enrolperiod < ?
215 OR ue.timecreated > 0 AND ue.timecreated + cr.enrolperiod < ?
219 // Loop through completions, and mark as complete
221 $rs = $DB->get_recordset_sql($sql, array($now, $now));
222 foreach ($rs as $record) {
224 $completion = new completion_criteria_completion((array)$record);
226 // Use time start if not 0, otherwise use timeenrolled
227 if ($record->otimestart) {
228 $completion->mark_complete($record->ctimestart);
230 $completion->mark_complete($record->ctimeenrolled);
237 * Return criteria progress details for display in reports
239 * @param object $completion The user's completion record
242 public function get_details($completion) {
244 $details['type'] = get_string('periodpostenrolment', 'completion');
245 $details['criteria'] = get_string('remainingenroledfortime', 'completion');
246 $details['requirement'] = get_string('xdays', 'completion', ceil($this->enrolperiod / (60*60*24)));
249 $timeenrolled = $this->get_timeenrolled($completion);
250 $timepassed = time() - $timeenrolled;
251 $details['status'] = get_string('xdays', 'completion', floor($timepassed / (60*60*24)));