Merge branch 'MDL-55267-master-deprecation' of https://github.com/marcusgreen/moodle
[moodle.git] / completion / criteria / 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
95dd54ee 43 /* @var int Criteria type constant */
2be4d090
MD
44 public $criteriatype = COMPLETION_CRITERIA_TYPE_COURSE;
45
46 /**
47 * Finds and returns a data_object instance based on params.
2be4d090
MD
48 *
49 * @param array $params associative arrays varname=>value
836375ec 50 * @return data_object instance of data_object or false if none found.
2be4d090
MD
51 */
52 public static function fetch($params) {
53 $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_COURSE;
54 return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
55 }
56
57 /**
58 * Add appropriate form elements to the critieria form
836375ec
SH
59 *
60 * @param moodle_form $mform Moodle forms object
95dd54ee 61 * @param stdClass $data data used to define default value of the form
2be4d090
MD
62 */
63 public function config_form_display(&$mform, $data = null) {
64 global $CFG;
65
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);
68
69 if ($this->id) {
70 $mform->setDefault('criteria_course['.$data->id.']', 1);
71 }
72 }
73
74 /**
75 * Update the criteria information stored in the database
836375ec
SH
76 *
77 * @param array $data Form data
2be4d090
MD
78 */
79 public function update_config(&$data) {
80
81 if (!empty($data->criteria_course) && is_array($data->criteria_course)) {
82
83 $this->course = $data->id;
84
85 foreach ($data->criteria_course as $course) {
86
87 $this->courseinstance = $course;
88 $this->id = NULL;
89 $this->insert();
90 }
91 }
92 }
93
94 /**
95 * Review this criteria and decide if the user has completed
836375ec
SH
96 *
97 * @param completion_completion $completion The user's completion record
95dd54ee
AA
98 * @param bool $mark Optionally set false to not save changes to database
99 * @return bool
2be4d090
MD
100 */
101 public function review($completion, $mark = true) {
ffe50258 102 global $DB;
2be4d090 103
ffe50258 104 $course = $DB->get_record('course', array('id' => $this->courseinstance));
2be4d090
MD
105 $info = new completion_info($course);
106
107 // If the course is complete
108 if ($info->is_course_complete($completion->userid)) {
109
110 if ($mark) {
111 $completion->mark_complete();
112 }
113
114 return true;
115 }
116
117 return false;
118 }
119
120 /**
121 * Return criteria title for display in reports
836375ec
SH
122 *
123 * @return string
2be4d090
MD
124 */
125 public function get_title() {
eb8f89a9 126 return get_string('dependenciescompleted', 'completion');
2be4d090
MD
127 }
128
129 /**
130 * Return a more detailed criteria title for display in reports
95dd54ee 131 *
836375ec 132 * @return string
2be4d090
MD
133 */
134 public function get_title_detailed() {
ffe50258
EL
135 global $DB;
136
137 $prereq = $DB->get_record('course', array('id' => $this->courseinstance));
bf0f06b1 138 $coursecontext = context_course::instance($prereq->id, MUST_EXIST);
91d284c1
SH
139 $fullname = format_string($prereq->fullname, true, array('context' => $coursecontext));
140 return shorten_text(urldecode($fullname));
2be4d090
MD
141 }
142
143 /**
144 * Return criteria type title for display in reports
836375ec
SH
145 *
146 * @return string
2be4d090
MD
147 */
148 public function get_type_title() {
eb8f89a9 149 return get_string('dependencies', 'completion');
2be4d090
MD
150 }
151
152 /**
153 * Find user's who have completed this criteria
2be4d090
MD
154 */
155 public function cron() {
156
157 global $DB;
158
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
191 ";
192
193 // Loop through completions, and mark as complete
419178d7
EL
194 $rs = $DB->get_recordset_sql($sql);
195 foreach ($rs as $record) {
28e0c239 196 $completion = new completion_criteria_completion((array) $record, DATA_OBJECT_FETCH_BY_KEY);
ccc604a2 197 $completion->mark_complete($record->timecompleted);
2be4d090 198 }
419178d7 199 $rs->close();
2be4d090
MD
200 }
201
202 /**
203 * Return criteria progress details for display in reports
836375ec
SH
204 *
205 * @param completion_completion $completion The user's completion record
206 * @return array An array with the following keys:
207 * type, criteria, requirement, status
2be4d090
MD
208 */
209 public function get_details($completion) {
ffe50258 210 global $CFG, $DB;
2be4d090
MD
211
212 // Get completion info
365a5941 213 $course = new stdClass();
2be4d090
MD
214 $course->id = $completion->course;
215 $info = new completion_info($course);
216
ffe50258 217 $prereq = $DB->get_record('course', array('id' => $this->courseinstance));
bf0f06b1 218 $coursecontext = context_course::instance($prereq->id, MUST_EXIST);
91d284c1
SH
219 $fullname = format_string($prereq->fullname, true, array('context' => $coursecontext));
220
2be4d090
MD
221 $prereq_info = new completion_info($prereq);
222
223 $details = array();
224 $details['type'] = $this->get_title();
91d284c1 225 $details['criteria'] = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$this->courseinstance.'">'.s($fullname).'</a>';
2be4d090
MD
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>';
228
229 return $details;
230 }
231}