Merge branch 'install_master' of git://github.com/amosbot/moodle
[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'));
a1b98722 56 $mform->addElement('date_selector', 'criteria_date_value', get_string('afterspecifieddate', 'completion'));
2be4d090
MD
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;
a1b98722 77 $this->timeend = $data->criteria_date_value;
2be4d090
MD
78 $this->insert();
79 }
80 }
81
82 /**
83 * Review this criteria and decide if the user has completed
84 * @access public
85 * @param object $completion The user's completion record
86 * @param boolean $mark Optionally set false to not save changes to database
87 * @return boolean
88 */
89 public function review($completion, $mark = true)
90 {
91 // If current time is past timeend
92 if ($this->timeend && $this->timeend < time()) {
93 if ($mark) {
94 $completion->mark_complete();
95 }
96
97 return true;
98 }
99
100 return false;
101 }
102
103 /**
104 * Return criteria title for display in reports
105 * @access public
106 * @return string
107 */
108 public function get_title() {
109 return get_string('date');
110 }
111
112 /**
113 * Return a more detailed criteria title for display in reports
114 * @access public
115 * @return string
116 */
117 public function get_title_detailed() {
118 return userdate($this->timeend, '%d-%h-%y');
119 }
120
121 /**
122 * Return criteria type title for display in reports
123 * @access public
124 * @return string
125 */
126 public function get_type_title() {
127 return get_string('date');
128 }
129
130
131 /**
132 * Return criteria status text for display in reports
133 * @access public
134 * @param object $completion The user's completion record
135 * @return string
136 */
137 public function get_status($completion) {
138 return $completion->is_complete() ? get_string('yes') : userdate($this->timeend, '%d-%h-%y');
139 }
140
141 /**
142 * Find user's who have completed this criteria
143 * @access public
144 * @return void
145 */
146 public function cron() {
147 global $DB;
148
149 // Get all users who match meet this criteria
150 $sql = '
151 SELECT DISTINCT
152 c.id AS course,
153 cr.timeend AS timeend,
154 cr.id AS criteriaid,
155 ra.userid AS userid
156 FROM
157 {course_completion_criteria} cr
158 INNER JOIN
159 {course} c
160 ON cr.course = c.id
161 INNER JOIN
162 {context} con
163 ON con.instanceid = c.id
164 INNER JOIN
165 {role_assignments} ra
166 ON ra.contextid = con.id
167 LEFT JOIN
168 {course_completion_crit_compl} cc
169 ON cc.criteriaid = cr.id
170 AND cc.userid = ra.userid
171 WHERE
172 cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_DATE.'
173 AND con.contextlevel = '.CONTEXT_COURSE.'
174 AND c.enablecompletion = 1
175 AND cc.id IS NULL
176 AND cr.timeend < ?
177 ';
178
179 // Loop through completions, and mark as complete
419178d7
EL
180 $rs = $DB->get_recordset_sql($sql, array(time()));
181 foreach ($rs as $record) {
182 $completion = new completion_criteria_completion((array)$record);
183 $completion->mark_complete($record->timeend);
2be4d090 184 }
419178d7 185 $rs->close();
2be4d090
MD
186 }
187
188 /**
189 * Return criteria progress details for display in reports
190 * @access public
191 * @param object $completion The user's completion record
192 * @return array
193 */
194 public function get_details($completion) {
195 $details = array();
196 $details['type'] = get_string('datepassed', 'completion');
197 $details['criteria'] = get_string('remainingenroleduntildate', 'completion');
198 $details['requirement'] = userdate($this->timeend, '%d %B %Y');
199 $details['status'] = '';
200
201 return $details;
202 }
203}