course completion MDL-2631 Course completion feature - Thanks to Aaron Barnes and...
[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
93 $context = get_context_instance(CONTEXT_COURSE, $this->course);
94 return $DB->get_field('role_assignments', 'timestart', array('contextid' => $context->id, 'userid' => $completion->userid));
95 }
96
97 /**
98 * Review this criteria and decide if the user has completed
99 * @access public
100 * @param object $completion The user's completion record
101 * @param boolean $mark Optionally set false to not save changes to database
102 * @return boolean
103 */
104 public function review($completion, $mark = true) {
105 $timeenrolled = $this->get_timeenrolled($completion);
106
107 // If duration since enrollment has passed
108 if (!$this->enrolperiod || !$timeenrolled) {
109 return false;
110 }
111
112 if (time() > ($timeenrolled + $this->enrolperiod)) {
113 if ($mark) {
114 $completion->mark_complete();
115 }
116
117 return true;
118 }
119
120 return false;
121 }
122
123 /**
124 * Return criteria title for display in reports
125 * @access public
126 * @return string
127 */
128 public function get_title() {
129 return get_string('enrolmentduration', 'completion');
130 }
131
132 /**
133 * Return a more detailed criteria title for display in reports
134 * @access public
135 * @return string
136 */
137 public function get_title_detailed() {
138 return ceil($this->enrolperiod / (60 * 60 * 24)) . ' days';
139 }
140
141 /**
142 * Return criteria type title for display in reports
143 * @access public
144 * @return string
145 */
146 public function get_type_title() {
147 return get_string('days', 'completion');
148 }
149
150 /**
151 * Return criteria status text for display in reports
152 * @access public
153 * @param object $completion The user's completion record
154 * @return string
155 */
156 public function get_status($completion) {
157 $timeenrolled = $this->get_timeenrolled($completion);
158 $timeleft = $timeenrolled + $this->enrolperiod - time();
159 $enrolperiod = ceil($this->enrolperiod / (60 * 60 * 24));
160
161 $daysleft = ceil($timeleft / (60 * 60 * 24));
162
163 return ($daysleft > 0 ? $daysleft : 0).' of '.$enrolperiod;
164 }
165
166 /**
167 * Find user's who have completed this criteria
168 * @access public
169 * @return void
170 */
171 public function cron() {
172 global $DB;
173
174 // Get all users who match meet this criteria
175 $sql = '
176 SELECT DISTINCT
177 c.id AS course,
178 cr.date AS date,
179 cr.id AS criteriaid,
180 ra.userid AS userid,
181 (ra.timestart + cr.enrolperiod) AS timecompleted
182 FROM
183 {course_completion_criteria} cr
184 INNER JOIN
185 {course} c
186 ON cr.course = c.id
187 INNER JOIN
188 {context} con
189 ON con.instanceid = c.id
190 INNER JOIN
191 {role_assignments} ra
192 ON ra.contextid = con.id
193 LEFT JOIN
194 {course_completion_crit_compl} cc
195 ON cc.criteriaid = cr.id
196 AND cc.userid = ra.userid
197 WHERE
198 cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_DURATION.'
199 AND con.contextlevel = '.CONTEXT_COURSE.'
200 AND c.enablecompletion = 1
201 AND cc.id IS NULL
202 AND ra.timestart + cr.enrolperiod < ?
203 ';
204
205 // Loop through completions, and mark as complete
206 if ($rs = $DB->get_recordset_sql($sql, array(time()))) {
207 foreach ($rs as $record) {
208
209 $completion = new completion_criteria_completion((array)$record);
210 $completion->mark_complete($record->timecompleted);
211 }
212
213 $rs->close();
214 }
215 }
216
217 /**
218 * Return criteria progress details for display in reports
219 * @access public
220 * @param object $completion The user's completion record
221 * @return array
222 */
223 public function get_details($completion) {
224 $details = array();
225 $details['type'] = get_string('periodpostenrolment', 'completion');
226 $details['criteria'] = get_string('remainingenroledfortime', 'completion');
227 $details['requirement'] = get_string('xdays', 'completion', ceil($this->enrolperiod / (60*60*24)));
228
229 // Get status
230 $timeenrolled = $this->get_timeenrolled($completion);
231 $timepassed = time() - $timeenrolled;
232 $details['status'] = get_string('xdays', 'completion', floor($timepassed / (60*60*24)));
233
234 return $details;
235 }
236}