MDL-48804 core: do not set current course until login validated
[moodle.git] / completion / criteria / completion_criteria_course.php
blobc5eb9cb151c666fa6290212bff4c01959ff94fa4
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 course 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 course completion
32 * This course completion criteria depends on another course with
33 * completion enabled to be marked as complete for this user
35 * @package core_completion
36 * @category completion
37 * @copyright 2009 Catalyst IT Ltd
38 * @author Aaron Barnes <aaronb@catalyst.net.nz>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class completion_criteria_course extends completion_criteria {
43 /* @var int Criteria type constant */
44 public $criteriatype = COMPLETION_CRITERIA_TYPE_COURSE;
46 /**
47 * Finds and returns a data_object instance based on params.
49 * @param array $params associative arrays varname=>value
50 * @return data_object instance of data_object or false if none found.
52 public static function fetch($params) {
53 $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_COURSE;
54 return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
57 /**
58 * Add appropriate form elements to the critieria form
60 * @param moodle_form $mform Moodle forms object
61 * @param stdClass $data data used to define default value of the form
63 public function config_form_display(&$mform, $data = null) {
64 global $CFG;
66 $link = "<a href=\"{$CFG->wwwroot}/course/view.php?id={$data->id}\">".s($data->fullname).'</a>';
67 $mform->addElement('checkbox', 'criteria_course['.$data->id.']', $link);
69 if ($this->id) {
70 $mform->setDefault('criteria_course['.$data->id.']', 1);
74 /**
75 * Update the criteria information stored in the database
77 * @param array $data Form data
79 public function update_config(&$data) {
81 if (!empty($data->criteria_course) && is_array($data->criteria_course)) {
83 $this->course = $data->id;
85 foreach ($data->criteria_course as $course) {
87 $this->courseinstance = $course;
88 $this->id = NULL;
89 $this->insert();
94 /**
95 * Review this criteria and decide if the user has completed
97 * @param completion_completion $completion The user's completion record
98 * @param bool $mark Optionally set false to not save changes to database
99 * @return bool
101 public function review($completion, $mark = true) {
102 global $DB;
104 $course = $DB->get_record('course', array('id' => $this->courseinstance));
105 $info = new completion_info($course);
107 // If the course is complete
108 if ($info->is_course_complete($completion->userid)) {
110 if ($mark) {
111 $completion->mark_complete();
114 return true;
117 return false;
121 * Return criteria title for display in reports
123 * @return string
125 public function get_title() {
126 return get_string('dependenciescompleted', 'completion');
130 * Return a more detailed criteria title for display in reports
132 * @return string
134 public function get_title_detailed() {
135 global $DB;
137 $prereq = $DB->get_record('course', array('id' => $this->courseinstance));
138 $coursecontext = context_course::instance($prereq->id, MUST_EXIST);
139 $fullname = format_string($prereq->fullname, true, array('context' => $coursecontext));
140 return shorten_text(urldecode($fullname));
144 * Return criteria type title for display in reports
146 * @return string
148 public function get_type_title() {
149 return get_string('dependencies', 'completion');
153 * Find user's who have completed this criteria
155 public function cron() {
157 global $DB;
159 // Get all users who meet this criteria
160 $sql = "
161 SELECT DISTINCT
162 c.id AS course,
163 cr.id AS criteriaid,
164 ra.userid AS userid,
165 cc.timecompleted AS timecompleted
166 FROM
167 {course_completion_criteria} cr
168 INNER JOIN
169 {course} c
170 ON cr.course = c.id
171 INNER JOIN
172 {context} con
173 ON con.instanceid = c.id
174 INNER JOIN
175 {role_assignments} ra
176 ON ra.contextid = con.id
177 INNER JOIN
178 {course_completions} cc
179 ON cc.course = cr.courseinstance
180 AND cc.userid = ra.userid
181 LEFT JOIN
182 {course_completion_crit_compl} ccc
183 ON ccc.criteriaid = cr.id
184 AND ccc.userid = ra.userid
185 WHERE
186 cr.criteriatype = ".COMPLETION_CRITERIA_TYPE_COURSE."
187 AND con.contextlevel = ".CONTEXT_COURSE."
188 AND c.enablecompletion = 1
189 AND ccc.id IS NULL
190 AND cc.timecompleted IS NOT NULL
193 // Loop through completions, and mark as complete
194 $rs = $DB->get_recordset_sql($sql);
195 foreach ($rs as $record) {
196 $completion = new completion_criteria_completion((array) $record, DATA_OBJECT_FETCH_BY_KEY);
197 $completion->mark_complete($record->timecompleted);
199 $rs->close();
203 * Return criteria progress details for display in reports
205 * @param completion_completion $completion The user's completion record
206 * @return array An array with the following keys:
207 * type, criteria, requirement, status
209 public function get_details($completion) {
210 global $CFG, $DB;
212 // Get completion info
213 $course = new stdClass();
214 $course->id = $completion->course;
215 $info = new completion_info($course);
217 $prereq = $DB->get_record('course', array('id' => $this->courseinstance));
218 $coursecontext = context_course::instance($prereq->id, MUST_EXIST);
219 $fullname = format_string($prereq->fullname, true, array('context' => $coursecontext));
221 $prereq_info = new completion_info($prereq);
223 $details = array();
224 $details['type'] = $this->get_title();
225 $details['criteria'] = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$this->courseinstance.'">'.s($fullname).'</a>';
226 $details['requirement'] = get_string('coursecompleted', 'completion');
227 $details['status'] = '<a href="'.$CFG->wwwroot.'/blocks/completionstatus/details.php?course='.$this->courseinstance.'">'.get_string('seedetails', 'completion').'</a>';
229 return $details;