course completion MDL-2631 Course completion feature - Thanks to Aaron Barnes and...
[moodle.git] / lib / completion / completion_criteria_date.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 on specified date
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_date extends completion_criteria {
27
28 /**
29 * Criteria type constant
30 * @var int
31 */
32 public $criteriatype = COMPLETION_CRITERIA_TYPE_DATE;
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_DATE;
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_date', get_string('enable'));
56 $mform->addElement('date', 'criteria_date_value', get_string('afterspecifieddate', 'completion'));
57
58 // If instance of criteria exists
59 if ($this->id) {
60 $mform->setDefault('criteria_date', 1);
61 $mform->setDefault('criteria_date_value', $this->timeend);
62 } else {
63 $mform->setDefault('criteria_date_value', time() + 3600 * 24);
64 }
65 }
66
67 /**
68 * Update the criteria information stored in the database
69 * @access public
70 * @param array $data Form data
71 * @return void
72 */
73 public function update_config(&$data) {
74
75 if (!empty($data->criteria_date)) {
76 $this->course = $data->id;
77 $date = $data->criteria_date_value;
78 $this->timeend = strtotime($date['Y'].'-'.$date['M'].'-'.$date['d']);
79 $this->insert();
80 }
81 }
82
83 /**
84 * Review this criteria and decide if the user has completed
85 * @access public
86 * @param object $completion The user's completion record
87 * @param boolean $mark Optionally set false to not save changes to database
88 * @return boolean
89 */
90 public function review($completion, $mark = true)
91 {
92 // If current time is past timeend
93 if ($this->timeend && $this->timeend < time()) {
94 if ($mark) {
95 $completion->mark_complete();
96 }
97
98 return true;
99 }
100
101 return false;
102 }
103
104 /**
105 * Return criteria title for display in reports
106 * @access public
107 * @return string
108 */
109 public function get_title() {
110 return get_string('date');
111 }
112
113 /**
114 * Return a more detailed criteria title for display in reports
115 * @access public
116 * @return string
117 */
118 public function get_title_detailed() {
119 return userdate($this->timeend, '%d-%h-%y');
120 }
121
122 /**
123 * Return criteria type title for display in reports
124 * @access public
125 * @return string
126 */
127 public function get_type_title() {
128 return get_string('date');
129 }
130
131
132 /**
133 * Return criteria status text for display in reports
134 * @access public
135 * @param object $completion The user's completion record
136 * @return string
137 */
138 public function get_status($completion) {
139 return $completion->is_complete() ? get_string('yes') : userdate($this->timeend, '%d-%h-%y');
140 }
141
142 /**
143 * Find user's who have completed this criteria
144 * @access public
145 * @return void
146 */
147 public function cron() {
148 global $DB;
149
150 // Get all users who match meet this criteria
151 $sql = '
152 SELECT DISTINCT
153 c.id AS course,
154 cr.timeend AS timeend,
155 cr.id AS criteriaid,
156 ra.userid AS userid
157 FROM
158 {course_completion_criteria} cr
159 INNER JOIN
160 {course} c
161 ON cr.course = c.id
162 INNER JOIN
163 {context} con
164 ON con.instanceid = c.id
165 INNER JOIN
166 {role_assignments} ra
167 ON ra.contextid = con.id
168 LEFT JOIN
169 {course_completion_crit_compl} cc
170 ON cc.criteriaid = cr.id
171 AND cc.userid = ra.userid
172 WHERE
173 cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_DATE.'
174 AND con.contextlevel = '.CONTEXT_COURSE.'
175 AND c.enablecompletion = 1
176 AND cc.id IS NULL
177 AND cr.timeend < ?
178 ';
179
180 // Loop through completions, and mark as complete
181 if ($rs = $DB->get_recordset_sql($sql, array(time()))) {
182 foreach ($rs as $record) {
183
184 $completion = new completion_criteria_completion((array)$record);
185 $completion->mark_complete($record->timeend);
186 }
187
188 $rs->close();
189 }
190 }
191
192 /**
193 * Return criteria progress details for display in reports
194 * @access public
195 * @param object $completion The user's completion record
196 * @return array
197 */
198 public function get_details($completion) {
199 $details = array();
200 $details['type'] = get_string('datepassed', 'completion');
201 $details['criteria'] = get_string('remainingenroleduntildate', 'completion');
202 $details['requirement'] = userdate($this->timeend, '%d %B %Y');
203 $details['status'] = '';
204
205 return $details;
206 }
207}