2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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();
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
;
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);
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) {
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);
70 $mform->setDefault('criteria_course['.$data->id
.']', 1);
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;
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
101 public function review($completion, $mark = true) {
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
)) {
111 $completion->mark_complete();
121 * Return criteria title for display in reports
125 public function get_title() {
126 return get_string('dependenciescompleted', 'completion');
130 * Return a more detailed criteria title for display in reports
134 public function get_title_detailed() {
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
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() {
159 // Get all users who meet this criteria
165 cc.timecompleted AS timecompleted
167 {course_completion_criteria} cr
173 ON con.instanceid = c.id
175 {role_assignments} ra
176 ON ra.contextid = con.id
178 {course_completions} cc
179 ON cc.course = cr.courseinstance
180 AND cc.userid = ra.userid
182 {course_completion_crit_compl} ccc
183 ON ccc.criteriaid = cr.id
184 AND ccc.userid = ra.userid
186 cr.criteriatype = ".COMPLETION_CRITERIA_TYPE_COURSE
."
187 AND con.contextlevel = ".CONTEXT_COURSE
."
188 AND c.enablecompletion = 1
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
);
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) {
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);
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>';