Choice MDL-23088 fix old get_record call - thanks to Brian Gray for report/patch
[moodle.git] / lib / completion / completion_criteria_course.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 course completion
20 *
21 * This course completion criteria depends on another course with
22 * completion enabled to be marked as complete for this user
23 *
24 * @package moodlecore
25 * @copyright 2009 Catalyst IT Ltd
26 * @author Aaron Barnes <aaronb@catalyst.net.nz>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 */
29class completion_criteria_course extends completion_criteria {
30
31 /**
32 * Criteria type constant
33 * @var int
34 */
35 public $criteriatype = COMPLETION_CRITERIA_TYPE_COURSE;
36
37 /**
38 * Finds and returns a data_object instance based on params.
39 * @static abstract
40 *
41 * @param array $params associative arrays varname=>value
42 * @return object data_object instance or false if none found.
43 */
44 public static function fetch($params) {
45 $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_COURSE;
46 return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
47 }
48
49 /**
50 * Add appropriate form elements to the critieria form
51 * @access public
52 * @param object $mform Moodle forms object
53 * @param mixed $data optional
54 * @return void
55 */
56 public function config_form_display(&$mform, $data = null) {
57 global $CFG;
58
59 $link = "<a href=\"{$CFG->wwwroot}/course/view.php?id={$data->id}\">".s($data->fullname).'</a>';
60 $mform->addElement('checkbox', 'criteria_course['.$data->id.']', $link);
61
62 if ($this->id) {
63 $mform->setDefault('criteria_course['.$data->id.']', 1);
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_course) && is_array($data->criteria_course)) {
76
77 $this->course = $data->id;
78
79 foreach ($data->criteria_course as $course) {
80
81 $this->courseinstance = $course;
82 $this->id = NULL;
83 $this->insert();
84 }
85 }
86 }
87
88 /**
89 * Review this criteria and decide if the user has completed
90 * @access public
91 * @param object $completion The user's completion record
92 * @param boolean $mark Optionally set false to not save changes to database
93 * @return boolean
94 */
95 public function review($completion, $mark = true) {
96
97 $course = get_record('course', 'id', $this->courseinstance);
98 $info = new completion_info($course);
99
100 // If the course is complete
101 if ($info->is_course_complete($completion->userid)) {
102
103 if ($mark) {
104 $completion->mark_complete();
105 }
106
107 return true;
108 }
109
110 return false;
111 }
112
113 /**
114 * Return criteria title for display in reports
115 * @access public
116 * @return string
117 */
118 public function get_title() {
119 return get_string('prerequisitescompleted', 'completion');
120 }
121
122 /**
123 * Return a more detailed criteria title for display in reports
124 * @access public
125 * @return string
126 */
127 public function get_title_detailed() {
128 $prereq = get_record('course', 'id', $this->courseinstance);
129 return shorten_text(urldecode($prereq->fullname));
130 }
131
132 /**
133 * Return criteria type title for display in reports
134 * @access public
135 * @return string
136 */
137 public function get_type_title() {
138 return get_string('prerequisites', 'completion');
139 }
140
141 /**
142 * Find user's who have completed this criteria
143 * @access public
144 * @return void
145 */
146 public function cron() {
147
148 global $DB;
149
150 // Get all users who meet this criteria
151 $sql = "
152 SELECT DISTINCT
153 c.id AS course,
154 cr.id AS criteriaid,
155 ra.userid AS userid,
156 cc.timecompleted AS timecompleted
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 INNER JOIN
169 {course_completions} cc
170 ON cc.course = cr.courseinstance
171 AND cc.userid = ra.userid
172 LEFT JOIN
173 {course_completion_crit_compl} ccc
174 ON ccc.criteriaid = cr.id
175 AND ccc.userid = ra.userid
176 WHERE
177 cr.criteriatype = ".COMPLETION_CRITERIA_TYPE_COURSE."
178 AND con.contextlevel = ".CONTEXT_COURSE."
179 AND c.enablecompletion = 1
180 AND ccc.id IS NULL
181 AND cc.timecompleted IS NOT NULL
182 ";
183
184 // Loop through completions, and mark as complete
185 if ($rs = $DB->get_recordset_sql($sql)) {
186 foreach ($rs as $record) {
187 $completion = new completion_criteria_completion((array)$record);
188 $completion->mark_complete($record->timecompleted);
189 }
190
191 $rs->close();
192 }
193 }
194
195 /**
196 * Return criteria progress details for display in reports
197 * @access public
198 * @param object $completion The user's completion record
199 * @return array
200 */
201 public function get_details($completion) {
202 global $CFG;
203
204 // Get completion info
205 $course = new object();
206 $course->id = $completion->course;
207 $info = new completion_info($course);
208
209 $prereq = get_record('course', 'id', $this->courseinstance);
210 $prereq_info = new completion_info($prereq);
211
212 $details = array();
213 $details['type'] = $this->get_title();
214 $details['criteria'] = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$this->courseinstance.'">'.s($prereq->fullname).'</a>';
215 $details['requirement'] = get_string('coursecompleted', 'completion');
216 $details['status'] = '<a href="'.$CFG->wwwroot.'/blocks/completionstatus/details.php?course='.$this->courseinstance.'">'.get_string('seedetails', 'completion').'</a>';
217
218 return $details;
219 }
220}