weekly release 2.2dev
[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));
2be4d090
MD
132 return shorten_text(urldecode($prereq->fullname));
133 }
134
135 /**
136 * Return criteria type title for display in reports
137 * @access public
138 * @return string
139 */
140 public function get_type_title() {
141 return get_string('prerequisites', 'completion');
142 }
143
144 /**
145 * Find user's who have completed this criteria
146 * @access public
147 * @return void
148 */
149 public function cron() {
150
151 global $DB;
152
153 // Get all users who meet this criteria
154 $sql = "
155 SELECT DISTINCT
156 c.id AS course,
157 cr.id AS criteriaid,
158 ra.userid AS userid,
159 cc.timecompleted AS timecompleted
160 FROM
161 {course_completion_criteria} cr
162 INNER JOIN
163 {course} c
164 ON cr.course = c.id
165 INNER JOIN
166 {context} con
167 ON con.instanceid = c.id
168 INNER JOIN
169 {role_assignments} ra
170 ON ra.contextid = con.id
171 INNER JOIN
172 {course_completions} cc
173 ON cc.course = cr.courseinstance
174 AND cc.userid = ra.userid
175 LEFT JOIN
176 {course_completion_crit_compl} ccc
177 ON ccc.criteriaid = cr.id
178 AND ccc.userid = ra.userid
179 WHERE
180 cr.criteriatype = ".COMPLETION_CRITERIA_TYPE_COURSE."
181 AND con.contextlevel = ".CONTEXT_COURSE."
182 AND c.enablecompletion = 1
183 AND ccc.id IS NULL
184 AND cc.timecompleted IS NOT NULL
185 ";
186
187 // Loop through completions, and mark as complete
419178d7
EL
188 $rs = $DB->get_recordset_sql($sql);
189 foreach ($rs as $record) {
190 $completion = new completion_criteria_completion((array)$record);
191 $completion->mark_complete($record->timecompleted);
2be4d090 192 }
419178d7 193 $rs->close();
2be4d090
MD
194 }
195
196 /**
197 * Return criteria progress details for display in reports
198 * @access public
199 * @param object $completion The user's completion record
200 * @return array
201 */
202 public function get_details($completion) {
ffe50258 203 global $CFG, $DB;
2be4d090
MD
204
205 // Get completion info
365a5941 206 $course = new stdClass();
2be4d090
MD
207 $course->id = $completion->course;
208 $info = new completion_info($course);
209
ffe50258 210 $prereq = $DB->get_record('course', array('id' => $this->courseinstance));
2be4d090
MD
211 $prereq_info = new completion_info($prereq);
212
213 $details = array();
214 $details['type'] = $this->get_title();
215 $details['criteria'] = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$this->courseinstance.'">'.s($prereq->fullname).'</a>';
216 $details['requirement'] = get_string('coursecompleted', 'completion');
217 $details['status'] = '<a href="'.$CFG->wwwroot.'/blocks/completionstatus/details.php?course='.$this->courseinstance.'">'.get_string('seedetails', 'completion').'</a>';
218
219 return $details;
220 }
221}