Merge branch 'install_master' of git://github.com/amosbot/moodle
[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) {
ffe50258 96 global $DB;
2be4d090 97
ffe50258 98 $course = $DB->get_record('course', array('id' => $this->courseinstance));
2be4d090
MD
99 $info = new completion_info($course);
100
101 // If the course is complete
102 if ($info->is_course_complete($completion->userid)) {
103
104 if ($mark) {
105 $completion->mark_complete();
106 }
107
108 return true;
109 }
110
111 return false;
112 }
113
114 /**
115 * Return criteria title for display in reports
116 * @access public
117 * @return string
118 */
119 public function get_title() {
120 return get_string('prerequisitescompleted', 'completion');
121 }
122
123 /**
124 * Return a more detailed criteria title for display in reports
125 * @access public
126 * @return string
127 */
128 public function get_title_detailed() {
ffe50258
EL
129 global $DB;
130
131 $prereq = $DB->get_record('course', array('id' => $this->courseinstance));
91d284c1
SH
132 $coursecontext = get_context_instance(CONTEXT_COURSE, $prereq->id, MUST_EXIST);
133 $fullname = format_string($prereq->fullname, true, array('context' => $coursecontext));
134 return shorten_text(urldecode($fullname));
2be4d090
MD
135 }
136
137 /**
138 * Return criteria type title for display in reports
139 * @access public
140 * @return string
141 */
142 public function get_type_title() {
143 return get_string('prerequisites', 'completion');
144 }
145
146 /**
147 * Find user's who have completed this criteria
148 * @access public
149 * @return void
150 */
151 public function cron() {
152
153 global $DB;
154
155 // Get all users who meet this criteria
156 $sql = "
157 SELECT DISTINCT
158 c.id AS course,
159 cr.id AS criteriaid,
160 ra.userid AS userid,
161 cc.timecompleted AS timecompleted
162 FROM
163 {course_completion_criteria} cr
164 INNER JOIN
165 {course} c
166 ON cr.course = c.id
167 INNER JOIN
168 {context} con
169 ON con.instanceid = c.id
170 INNER JOIN
171 {role_assignments} ra
172 ON ra.contextid = con.id
173 INNER JOIN
174 {course_completions} cc
175 ON cc.course = cr.courseinstance
176 AND cc.userid = ra.userid
177 LEFT JOIN
178 {course_completion_crit_compl} ccc
179 ON ccc.criteriaid = cr.id
180 AND ccc.userid = ra.userid
181 WHERE
182 cr.criteriatype = ".COMPLETION_CRITERIA_TYPE_COURSE."
183 AND con.contextlevel = ".CONTEXT_COURSE."
184 AND c.enablecompletion = 1
185 AND ccc.id IS NULL
186 AND cc.timecompleted IS NOT NULL
187 ";
188
189 // Loop through completions, and mark as complete
419178d7
EL
190 $rs = $DB->get_recordset_sql($sql);
191 foreach ($rs as $record) {
192 $completion = new completion_criteria_completion((array)$record);
193 $completion->mark_complete($record->timecompleted);
2be4d090 194 }
419178d7 195 $rs->close();
2be4d090
MD
196 }
197
198 /**
199 * Return criteria progress details for display in reports
200 * @access public
201 * @param object $completion The user's completion record
202 * @return array
203 */
204 public function get_details($completion) {
ffe50258 205 global $CFG, $DB;
2be4d090
MD
206
207 // Get completion info
365a5941 208 $course = new stdClass();
2be4d090
MD
209 $course->id = $completion->course;
210 $info = new completion_info($course);
211
ffe50258 212 $prereq = $DB->get_record('course', array('id' => $this->courseinstance));
91d284c1
SH
213 $coursecontext = get_context_instance(CONTEXT_COURSE, $prereq->id, MUST_EXIST);
214 $fullname = format_string($prereq->fullname, true, array('context' => $coursecontext));
215
2be4d090
MD
216 $prereq_info = new completion_info($prereq);
217
218 $details = array();
219 $details['type'] = $this->get_title();
91d284c1 220 $details['criteria'] = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$this->courseinstance.'">'.s($fullname).'</a>';
2be4d090
MD
221 $details['requirement'] = get_string('coursecompleted', 'completion');
222 $details['status'] = '<a href="'.$CFG->wwwroot.'/blocks/completionstatus/details.php?course='.$this->courseinstance.'">'.get_string('seedetails', 'completion').'</a>';
223
224 return $details;
225 }
226}