MDL-30995 completion: Fixed up PHPdocs for activity completion
[moodle.git] / lib / completion / completion_criteria_course.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 course 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 course completion
31 *
32 * This course completion criteria depends on another course with
33 * completion enabled to be marked as complete for this user
34 *
836375ec
SH
35 * @package core_completion
36 * @category completion
2be4d090 37 * @copyright 2009 Catalyst IT Ltd
836375ec
SH
38 * @author Aaron Barnes <aaronb@catalyst.net.nz>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2be4d090
MD
40 */
41class completion_criteria_course extends completion_criteria {
42
43 /**
44 * Criteria type constant
45 * @var int
46 */
47 public $criteriatype = COMPLETION_CRITERIA_TYPE_COURSE;
48
49 /**
50 * Finds and returns a data_object instance based on params.
2be4d090
MD
51 *
52 * @param array $params associative arrays varname=>value
836375ec 53 * @return data_object instance of data_object or false if none found.
2be4d090
MD
54 */
55 public static function fetch($params) {
56 $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_COURSE;
57 return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
58 }
59
60 /**
61 * Add appropriate form elements to the critieria form
836375ec
SH
62 *
63 * @param moodle_form $mform Moodle forms object
64 * @param stdClass $data
2be4d090
MD
65 */
66 public function config_form_display(&$mform, $data = null) {
67 global $CFG;
68
69 $link = "<a href=\"{$CFG->wwwroot}/course/view.php?id={$data->id}\">".s($data->fullname).'</a>';
70 $mform->addElement('checkbox', 'criteria_course['.$data->id.']', $link);
71
72 if ($this->id) {
73 $mform->setDefault('criteria_course['.$data->id.']', 1);
74 }
75 }
76
77 /**
78 * Update the criteria information stored in the database
836375ec
SH
79 *
80 * @param array $data Form data
2be4d090
MD
81 */
82 public function update_config(&$data) {
83
84 if (!empty($data->criteria_course) && is_array($data->criteria_course)) {
85
86 $this->course = $data->id;
87
88 foreach ($data->criteria_course as $course) {
89
90 $this->courseinstance = $course;
91 $this->id = NULL;
92 $this->insert();
93 }
94 }
95 }
96
97 /**
98 * Review this criteria and decide if the user has completed
836375ec
SH
99 *
100 * @param completion_completion $completion The user's completion record
101 * @param boolean $mark Optionally set false to not save changes to database
102 * @return boolean
2be4d090
MD
103 */
104 public function review($completion, $mark = true) {
ffe50258 105 global $DB;
2be4d090 106
ffe50258 107 $course = $DB->get_record('course', array('id' => $this->courseinstance));
2be4d090
MD
108 $info = new completion_info($course);
109
110 // If the course is complete
111 if ($info->is_course_complete($completion->userid)) {
112
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
836375ec
SH
125 *
126 * @return string
2be4d090
MD
127 */
128 public function get_title() {
129 return get_string('prerequisitescompleted', 'completion');
130 }
131
132 /**
133 * Return a more detailed criteria title for display in reports
836375ec
SH
134 *
135 * @return string
2be4d090
MD
136 */
137 public function get_title_detailed() {
ffe50258
EL
138 global $DB;
139
140 $prereq = $DB->get_record('course', array('id' => $this->courseinstance));
91d284c1
SH
141 $coursecontext = get_context_instance(CONTEXT_COURSE, $prereq->id, MUST_EXIST);
142 $fullname = format_string($prereq->fullname, true, array('context' => $coursecontext));
143 return shorten_text(urldecode($fullname));
2be4d090
MD
144 }
145
146 /**
147 * Return criteria type title for display in reports
836375ec
SH
148 *
149 * @return string
2be4d090
MD
150 */
151 public function get_type_title() {
152 return get_string('prerequisites', 'completion');
153 }
154
155 /**
156 * Find user's who have completed this criteria
2be4d090
MD
157 */
158 public function cron() {
159
160 global $DB;
161
162 // Get all users who meet this criteria
163 $sql = "
164 SELECT DISTINCT
165 c.id AS course,
166 cr.id AS criteriaid,
167 ra.userid AS userid,
168 cc.timecompleted AS timecompleted
169 FROM
170 {course_completion_criteria} cr
171 INNER JOIN
172 {course} c
173 ON cr.course = c.id
174 INNER JOIN
175 {context} con
176 ON con.instanceid = c.id
177 INNER JOIN
178 {role_assignments} ra
179 ON ra.contextid = con.id
180 INNER JOIN
181 {course_completions} cc
182 ON cc.course = cr.courseinstance
183 AND cc.userid = ra.userid
184 LEFT JOIN
185 {course_completion_crit_compl} ccc
186 ON ccc.criteriaid = cr.id
187 AND ccc.userid = ra.userid
188 WHERE
189 cr.criteriatype = ".COMPLETION_CRITERIA_TYPE_COURSE."
190 AND con.contextlevel = ".CONTEXT_COURSE."
191 AND c.enablecompletion = 1
192 AND ccc.id IS NULL
193 AND cc.timecompleted IS NOT NULL
194 ";
195
196 // Loop through completions, and mark as complete
419178d7
EL
197 $rs = $DB->get_recordset_sql($sql);
198 foreach ($rs as $record) {
199 $completion = new completion_criteria_completion((array)$record);
200 $completion->mark_complete($record->timecompleted);
2be4d090 201 }
419178d7 202 $rs->close();
2be4d090
MD
203 }
204
205 /**
206 * Return criteria progress details for display in reports
836375ec
SH
207 *
208 * @param completion_completion $completion The user's completion record
209 * @return array An array with the following keys:
210 * type, criteria, requirement, status
2be4d090
MD
211 */
212 public function get_details($completion) {
ffe50258 213 global $CFG, $DB;
2be4d090
MD
214
215 // Get completion info
365a5941 216 $course = new stdClass();
2be4d090
MD
217 $course->id = $completion->course;
218 $info = new completion_info($course);
219
ffe50258 220 $prereq = $DB->get_record('course', array('id' => $this->courseinstance));
91d284c1
SH
221 $coursecontext = get_context_instance(CONTEXT_COURSE, $prereq->id, MUST_EXIST);
222 $fullname = format_string($prereq->fullname, true, array('context' => $coursecontext));
223
2be4d090
MD
224 $prereq_info = new completion_info($prereq);
225
226 $details = array();
227 $details['type'] = $this->get_title();
91d284c1 228 $details['criteria'] = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$this->courseinstance.'">'.s($fullname).'</a>';
2be4d090
MD
229 $details['requirement'] = get_string('coursecompleted', 'completion');
230 $details['status'] = '<a href="'.$CFG->wwwroot.'/blocks/completionstatus/details.php?course='.$this->courseinstance.'">'.get_string('seedetails', 'completion').'</a>';
231
232 return $details;
233 }
234}