Merge branch 'wip-MDL-41921-master' of git://github.com/marinaglancy/moodle
[moodle.git] / report / completion / user.php
blob7100df8d30af3b08f6e18696e636d5b915a9b426
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 * Display user completion report
20 * @package report
21 * @subpackage completion
22 * @copyright 2009 Catalyst IT Ltd
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require('../../config.php');
27 require_once($CFG->dirroot.'/report/completion/lib.php');
28 require_once($CFG->libdir.'/completionlib.php');
30 $userid = required_param('id', PARAM_INT);
31 $courseid = required_param('course', PARAM_INT);
33 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
34 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
36 $coursecontext = context_course::instance($course->id);
37 $personalcontext = context_user::instance($user->id);
39 if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
40 and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
41 //TODO: do not require parents to be enrolled in courses - this is a hack!
42 require_login();
43 $PAGE->set_course($course);
44 } else {
45 require_login($course);
48 if (!report_completion_can_access_user_report($user, $course, true)) {
49 // this should never happen
50 error('Can not access user completion report');
53 add_to_log($course->id, 'course', 'report completion', "report/completion/user.php?id=$user->id&course=$course->id", $course->id);
55 $stractivityreport = get_string('activityreport');
57 $PAGE->set_pagelayout('admin');
58 $PAGE->set_url('/report/completion/user.php', array('id'=>$user->id, 'course'=>$course->id));
59 $PAGE->navigation->extend_for_user($user);
60 $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
61 $PAGE->set_title("$course->shortname: $stractivityreport");
62 $PAGE->set_heading($course->fullname);
63 echo $OUTPUT->header();
66 // Display course completion user report
68 // Grab all courses the user is enrolled in and their completion status
69 $sql = "
70 SELECT DISTINCT
71 c.id AS id
72 FROM
73 {course} c
74 INNER JOIN
75 {context} con
76 ON con.instanceid = c.id
77 INNER JOIN
78 {role_assignments} ra
79 ON ra.contextid = con.id
80 INNER JOIN
81 {enrol} e
82 ON c.id = e.courseid
83 INNER JOIN
84 {user_enrolments} ue
85 ON e.id = ue.enrolid AND ra.userid = ue.userid
86 AND ra.userid = {$user->id}
89 // Get roles that are tracked by course completion
90 if ($roles = $CFG->gradebookroles) {
91 $sql .= '
92 AND ra.roleid IN ('.$roles.')
96 $sql .= '
97 WHERE
98 con.contextlevel = '.CONTEXT_COURSE.'
99 AND c.enablecompletion = 1
103 // If we are looking at a specific course
104 if ($course->id != 1) {
105 $sql .= '
106 AND c.id = '.(int)$course->id.'
110 // Check if result is empty
111 $rs = $DB->get_recordset_sql($sql);
112 if (!$rs->valid()) {
114 if ($course->id != 1) {
115 $error = get_string('nocompletions', 'report_completion'); // TODO: missing string
116 } else {
117 $error = get_string('nocompletioncoursesenroled', 'report_completion'); // TODO: missing string
120 echo $OUTPUT->notification($error);
121 $rs->close(); // not going to loop (but break), close rs
122 echo $OUTPUT->footer();
123 die();
126 // Categorize courses by their status
127 $courses = array(
128 'inprogress' => array(),
129 'complete' => array(),
130 'unstarted' => array()
133 // Sort courses by the user's status in each
134 foreach ($rs as $course_completion) {
135 $c_info = new completion_info((object)$course_completion);
137 // Is course complete?
138 $coursecomplete = $c_info->is_course_complete($user->id);
140 // Has this user completed any criteria?
141 $criteriacomplete = $c_info->count_course_user_data($user->id);
143 if ($coursecomplete) {
144 $courses['complete'][] = $c_info;
145 } else if ($criteriacomplete) {
146 $courses['inprogress'][] = $c_info;
147 } else {
148 $courses['unstarted'][] = $c_info;
151 $rs->close(); // after loop, close rs
153 // Loop through course status groups
154 foreach ($courses as $type => $infos) {
156 // If there are courses with this status
157 if (!empty($infos)) {
159 echo '<h1 align="center">'.get_string($type, 'report_completion').'</h1>';
160 echo '<table class="generaltable boxaligncenter">';
161 echo '<tr class="ccheader">';
162 echo '<th class="c0 header" scope="col">'.get_string('course').'</th>';
163 echo '<th class="c1 header" scope="col">'.get_string('requiredcriteria', 'completion').'</th>';
164 echo '<th class="c2 header" scope="col">'.get_string('status').'</th>';
165 echo '<th class="c3 header" scope="col" width="15%">'.get_string('info').'</th>';
167 if ($type === 'complete') {
168 echo '<th class="c4 header" scope="col">'.get_string('completiondate', 'report_completion').'</th>';
171 echo '</tr>';
173 // For each course
174 foreach ($infos as $c_info) {
176 // Get course info
177 $c_course = $DB->get_record('course', array('id' => $c_info->course_id));
178 $course_context = context_course::instance($c_course->id, MUST_EXIST);
179 $course_name = format_string($c_course->fullname, true, array('context' => $course_context));
181 // Get completions
182 $completions = $c_info->get_completions($user->id);
184 // Save row data
185 $rows = array();
187 // For aggregating activity completion
188 $activities = array();
189 $activities_complete = 0;
191 // For aggregating prerequisites
192 $prerequisites = array();
193 $prerequisites_complete = 0;
195 // Loop through course criteria
196 foreach ($completions as $completion) {
197 $criteria = $completion->get_criteria();
198 $complete = $completion->is_complete();
200 // Activities are a special case, so cache them and leave them till last
201 if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
202 $activities[$criteria->moduleinstance] = $complete;
204 if ($complete) {
205 $activities_complete++;
208 continue;
211 // Prerequisites are also a special case, so cache them and leave them till last
212 if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) {
213 $prerequisites[$criteria->courseinstance] = $complete;
215 if ($complete) {
216 $prerequisites_complete++;
219 continue;
222 $row = array();
223 $row['title'] = $criteria->get_title();
224 $row['status'] = $completion->get_status();
225 $rows[] = $row;
228 // Aggregate activities
229 if (!empty($activities)) {
231 $row = array();
232 $row['title'] = get_string('activitiescomplete', 'report_completion');
233 $row['status'] = $activities_complete.' of '.count($activities);
234 $rows[] = $row;
237 // Aggregate prerequisites
238 if (!empty($prerequisites)) {
240 $row = array();
241 $row['title'] = get_string('prerequisitescompleted', 'completion');
242 $row['status'] = $prerequisites_complete.' of '.count($prerequisites);
243 array_splice($rows, 0, 0, array($row));
246 $first_row = true;
248 // Print table
249 foreach ($rows as $row) {
251 // Display course name on first row
252 if ($first_row) {
253 echo '<tr><td class="c0"><a href="'.$CFG->wwwroot.'/course/view.php?id='.$c_course->id.'">'.$course_name.'</a></td>';
254 } else {
255 echo '<tr><td class="c0"></td>';
258 echo '<td class="c1">';
259 echo $row['title'];
260 echo '</td><td class="c2">';
262 switch ($row['status']) {
263 case 'Yes':
264 echo get_string('complete');
265 break;
267 case 'No':
268 echo get_string('incomplete', 'report_completion');
269 break;
271 default:
272 echo $row['status'];
275 // Display link on first row
276 echo '</td><td class="c3">';
277 if ($first_row) {
278 echo '<a href="'.$CFG->wwwroot.'/blocks/completionstatus/details.php?course='.$c_course->id.'&user='.$user->id.'">'.get_string('detailedview', 'report_completion').'</a>';
280 echo '</td>';
282 // Display completion date for completed courses on first row
283 if ($type === 'complete' && $first_row) {
284 $params = array(
285 'userid' => $user->id,
286 'course' => $c_course->id
289 $ccompletion = new completion_completion($params);
290 echo '<td class="c4">'.userdate($ccompletion->timecompleted, '%e %B %G').'</td>';
293 $first_row = false;
294 echo '</tr>';
298 echo '</table>';
303 echo $OUTPUT->footer();