MDL-30995 Completion Fixedup some more PHP DOC issues
[moodle.git] / lib / completion / completion_criteria_date.php
blobe6e81c09148a7e6ce83a8ffe27f9c33066261527
1 <?php
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.
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/>.
17 /**
18 * This file contains the date criteria type
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
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Course completion critieria - completion on specified date
32 * @package core_completion
33 * @category completion
34 * @copyright 2009 Catalyst IT Ltd
35 * @author Aaron Barnes <aaronb@catalyst.net.nz>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class completion_criteria_date extends completion_criteria {
40 /* @var int Criteria type constant [COMPLETION_CRITERIA_TYPE_DATE] */
41 public $criteriatype = COMPLETION_CRITERIA_TYPE_DATE;
43 /**
44 * Finds and returns a data_object instance based on params.
46 * @param array $params associative arrays varname=>value
47 * @return data_object data_object instance or false if none found.
49 public static function fetch($params) {
50 $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_DATE;
51 return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
54 /**
55 * Add appropriate form elements to the critieria form
57 * @param moodleform $mform Moodle forms object
58 * @param stdClass $data not used
60 public function config_form_display(&$mform, $data = null) {
61 $mform->addElement('checkbox', 'criteria_date', get_string('enable'));
62 $mform->addElement('date_selector', 'criteria_date_value', get_string('afterspecifieddate', 'completion'));
64 // If instance of criteria exists
65 if ($this->id) {
66 $mform->setDefault('criteria_date', 1);
67 $mform->setDefault('criteria_date_value', $this->timeend);
68 } else {
69 $mform->setDefault('criteria_date_value', time() + 3600 * 24);
73 /**
74 * Update the criteria information stored in the database
76 * @param stdClass $data Form data
78 public function update_config(&$data) {
79 if (!empty($data->criteria_date)) {
80 $this->course = $data->id;
81 $this->timeend = $data->criteria_date_value;
82 $this->insert();
86 /**
87 * Review this criteria and decide if the user has completed
89 * @param completion_completion $completion The user's completion record
90 * @param bool $mark Optionally set false to not save changes to database
91 * @return bool
93 public function review($completion, $mark = true) {
94 // If current time is past timeend
95 if ($this->timeend && $this->timeend < time()) {
96 if ($mark) {
97 $completion->mark_complete();
100 return true;
102 return false;
106 * Return criteria title for display in reports
108 * @return string
110 public function get_title() {
111 return get_string('date');
115 * Return a more detailed criteria title for display in reports
117 * @return string
119 public function get_title_detailed() {
120 return userdate($this->timeend, '%d-%h-%y');
124 * Return criteria type title for display in reports
126 * @return string
128 public function get_type_title() {
129 return get_string('date');
134 * Return criteria status text for display in reports
136 * @param completion_completion $completion The user's completion record
137 * @return string
139 public function get_status($completion) {
140 return $completion->is_complete() ? get_string('yes') : userdate($this->timeend, '%d-%h-%y');
144 * Find user's who have completed this criteria
146 public function cron() {
147 global $DB;
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 < ?
179 // Loop through completions, and mark as complete
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);
185 $rs->close();
189 * Return criteria progress details for display in reports
191 * @param completion_completion $completion The user's completion record
192 * @return array An array with the following keys:
193 * type, criteria, requirement, status
195 public function get_details($completion) {
196 $details = array();
197 $details['type'] = get_string('datepassed', 'completion');
198 $details['criteria'] = get_string('remainingenroleduntildate', 'completion');
199 $details['requirement'] = userdate($this->timeend, '%d %B %Y');
200 $details['status'] = '';
202 return $details;