MDL-30995 completion: Fixed up PHPdocs for activity completion
[moodle.git] / lib / completion / completion_criteria_date.php
CommitLineData
2be4d090 1<?php
2be4d090
MD
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
836375ec
SH
17/**
18 * This file contains the date criteria type
19 *
20 * @package core_completion
21 * @category completion
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 */
26
27defined('MOODLE_INTERNAL') || die();
28
2be4d090
MD
29/**
30 * Course completion critieria - completion on specified date
31 *
836375ec
SH
32 * @package core_completion
33 * @category completion
2be4d090 34 * @copyright 2009 Catalyst IT Ltd
836375ec
SH
35 * @author Aaron Barnes <aaronb@catalyst.net.nz>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2be4d090
MD
37 */
38class completion_criteria_date extends completion_criteria {
39
40 /**
836375ec 41 * Criteria type constant [COMPLETION_CRITERIA_TYPE_DATE]
2be4d090
MD
42 * @var int
43 */
44 public $criteriatype = COMPLETION_CRITERIA_TYPE_DATE;
45
46 /**
47 * Finds and returns a data_object instance based on params.
2be4d090
MD
48 *
49 * @param array $params associative arrays varname=>value
836375ec 50 * @return data_object data_object instance or false if none found.
2be4d090
MD
51 */
52 public static function fetch($params) {
53 $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_DATE;
54 return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
55 }
56
57 /**
58 * Add appropriate form elements to the critieria form
836375ec
SH
59 *
60 * @param moodleform $mform Moodle forms object
61 * @param stdClass $data
2be4d090 62 */
836375ec 63 public function config_form_display(&$mform, $data = null) {
2be4d090 64 $mform->addElement('checkbox', 'criteria_date', get_string('enable'));
a1b98722 65 $mform->addElement('date_selector', 'criteria_date_value', get_string('afterspecifieddate', 'completion'));
2be4d090
MD
66
67 // If instance of criteria exists
68 if ($this->id) {
69 $mform->setDefault('criteria_date', 1);
70 $mform->setDefault('criteria_date_value', $this->timeend);
71 } else {
72 $mform->setDefault('criteria_date_value', time() + 3600 * 24);
73 }
74 }
75
76 /**
77 * Update the criteria information stored in the database
836375ec
SH
78 *
79 * @param stdClass $data Form data
2be4d090
MD
80 */
81 public function update_config(&$data) {
2be4d090
MD
82 if (!empty($data->criteria_date)) {
83 $this->course = $data->id;
a1b98722 84 $this->timeend = $data->criteria_date_value;
2be4d090
MD
85 $this->insert();
86 }
87 }
88
89 /**
90 * Review this criteria and decide if the user has completed
836375ec
SH
91 *
92 * @param completion_completion $completion The user's completion record
93 * @param boolean $mark Optionally set false to not save changes to database
94 * @return boolean
2be4d090 95 */
836375ec 96 public function review($completion, $mark = true) {
2be4d090
MD
97 // If current time is past timeend
98 if ($this->timeend && $this->timeend < time()) {
99 if ($mark) {
100 $completion->mark_complete();
101 }
102
103 return true;
104 }
2be4d090
MD
105 return false;
106 }
107
108 /**
109 * Return criteria title for display in reports
836375ec
SH
110 *
111 * @return string
2be4d090
MD
112 */
113 public function get_title() {
114 return get_string('date');
115 }
116
117 /**
118 * Return a more detailed criteria title for display in reports
836375ec
SH
119 *
120 * @return string
2be4d090
MD
121 */
122 public function get_title_detailed() {
123 return userdate($this->timeend, '%d-%h-%y');
124 }
125
126 /**
127 * Return criteria type title for display in reports
836375ec
SH
128 *
129 * @return string
2be4d090
MD
130 */
131 public function get_type_title() {
132 return get_string('date');
133 }
134
135
136 /**
137 * Return criteria status text for display in reports
836375ec
SH
138 *
139 * @param completion_completion $completion The user's completion record
140 * @return string
2be4d090
MD
141 */
142 public function get_status($completion) {
143 return $completion->is_complete() ? get_string('yes') : userdate($this->timeend, '%d-%h-%y');
144 }
145
146 /**
147 * Find user's who have completed this criteria
2be4d090
MD
148 */
149 public function cron() {
150 global $DB;
151
152 // Get all users who match meet this criteria
153 $sql = '
154 SELECT DISTINCT
155 c.id AS course,
156 cr.timeend AS timeend,
157 cr.id AS criteriaid,
158 ra.userid AS userid
159 FROM
160 {course_completion_criteria} cr
161 INNER JOIN
162 {course} c
163 ON cr.course = c.id
164 INNER JOIN
165 {context} con
166 ON con.instanceid = c.id
167 INNER JOIN
168 {role_assignments} ra
169 ON ra.contextid = con.id
170 LEFT JOIN
171 {course_completion_crit_compl} cc
172 ON cc.criteriaid = cr.id
173 AND cc.userid = ra.userid
174 WHERE
175 cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_DATE.'
176 AND con.contextlevel = '.CONTEXT_COURSE.'
177 AND c.enablecompletion = 1
178 AND cc.id IS NULL
179 AND cr.timeend < ?
180 ';
181
182 // Loop through completions, and mark as complete
419178d7
EL
183 $rs = $DB->get_recordset_sql($sql, array(time()));
184 foreach ($rs as $record) {
185 $completion = new completion_criteria_completion((array)$record);
186 $completion->mark_complete($record->timeend);
2be4d090 187 }
419178d7 188 $rs->close();
2be4d090
MD
189 }
190
191 /**
192 * Return criteria progress details for display in reports
836375ec
SH
193 *
194 * @param completion_completion $completion The user's completion record
195 * @return array An array with the following keys:
196 * type, criteria, requirement, status
2be4d090
MD
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}