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.
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
17/**
18 * Block for displaying logged in user's course completion status
19 *
20 * @package    block_completionstatus
21 * @copyright  2009-2012 Catalyst IT Ltd
22 * @author     Aaron Barnes <aaronb@catalyst.net.nz>
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26require_once(__DIR__.'/../../config.php');
27require_once("{$CFG->libdir}/completionlib.php");
28
29// Load data.
30$id = required_param('course', PARAM_INT);
31$userid = optional_param('user', 0, PARAM_INT);
32
33// Load course.
34$course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
35
36// Load user.
37if ($userid) {
38    $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
39} else {
40    $user = $USER;
41}
42
43// Check permissions.
44require_login();
45
46if (!completion_can_view_data($user->id, $course)) {
47    print_error('cannotviewreport');
48}
49
50// Load completion data.
51$info = new completion_info($course);
52
53$returnurl = new moodle_url('/course/view.php', array('id' => $id));
54
55// Don't display if completion isn't enabled.
56if (!$info->is_enabled()) {
57    print_error('completionnotenabled', 'completion', $returnurl);
58}
59
60// Check this user is enroled.
61if (!$info->is_tracked_user($user->id)) {
62    if ($USER->id == $user->id) {
63        print_error('notenroled', 'completion', $returnurl);
64    } else {
65        print_error('usernotenroled', 'completion', $returnurl);
66    }
67}
68
69// Display page.
70
71$PAGE->set_context(context_course::instance($course->id));
72
73// Print header.
74$page = get_string('completionprogressdetails', 'block_completionstatus');
75$title = format_string($course->fullname) . ': ' . $page;
76
77$PAGE->navbar->add($page);
78$PAGE->set_pagelayout('report');
79$PAGE->set_url('/blocks/completionstatus/details.php', array('course' => $course->id, 'user' => $user->id));
80$PAGE->set_title(get_string('course') . ': ' . $course->fullname);
81$PAGE->set_heading($title);
82echo $OUTPUT->header();
83
84
85// Display completion status.
86echo html_writer::start_tag('table', array('class' => 'generalbox boxaligncenter'));
87echo html_writer::start_tag('tbody');
88
89// If not display logged in user, show user name.
90if ($USER->id != $user->id) {
91    echo html_writer::start_tag('tr');
92    echo html_writer::start_tag('td', array('colspan' => '2'));
93    echo html_writer::tag('b', get_string('showinguser', 'completion') . ' ');
94    $url = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id));
95    echo html_writer::link($url, fullname($user));
96    echo html_writer::end_tag('td');
97    echo html_writer::end_tag('tr');
98}
99
100echo html_writer::start_tag('tr');
101echo html_writer::start_tag('td', array('colspan' => '2'));
102echo html_writer::tag('b', get_string('status') . ' ');
103
104// Is course complete?
105$coursecomplete = $info->is_course_complete($user->id);
106
107// Has this user completed any criteria?
108$criteriacomplete = $info->count_course_user_data($user->id);
109
110// Load course completion.
111$params = array(
112    'userid' => $user->id,
113    'course' => $course->id,
114);
115$ccompletion = new completion_completion($params);
116
117if ($coursecomplete) {
118    echo get_string('complete');
119} else if (!$criteriacomplete && !$ccompletion->timestarted) {
120    echo html_writer::tag('i', get_string('notyetstarted', 'completion'));
121} else {
122    echo html_writer::tag('i', get_string('inprogress', 'completion'));
123}
124
125echo html_writer::end_tag('td');
126echo html_writer::end_tag('tr');
127
128// Load criteria to display.
129$completions = $info->get_completions($user->id);
130
131// Check if this course has any criteria.
132if (empty($completions)) {
133    echo html_writer::start_tag('tr');
134    echo html_writer::start_tag('td', array('colspan' => '2'));
135    echo html_writer::start_tag('br');
136    echo $OUTPUT->box(get_string('nocriteriaset', 'completion'), 'noticebox');
137    echo html_writer::end_tag('td');
138    echo html_writer::end_tag('tr');
139    echo html_writer::end_tag('tbody');
140    echo html_writer::end_tag('table');
141} else {
142    echo html_writer::start_tag('tr');
143    echo html_writer::start_tag('td', array('colspan' => '2'));
144    echo html_writer::tag('b', get_string('required') . ' ');
145
146    // Get overall aggregation method.
147    $overall = $info->get_aggregation_method();
148
149    if ($overall == COMPLETION_AGGREGATION_ALL) {
150        echo get_string('criteriarequiredall', 'completion');
151    } else {
152        echo get_string('criteriarequiredany', 'completion');
153    }
154
155    echo html_writer::end_tag('td');
156    echo html_writer::end_tag('tr');
157    echo html_writer::end_tag('tbody');
158    echo html_writer::end_tag('table');
159
160    // Generate markup for criteria statuses.
161    echo html_writer::start_tag('table',
162            array('class' => 'generalbox logtable boxaligncenter', 'id' => 'criteriastatus', 'width' => '100%'));
163    echo html_writer::start_tag('tbody');
164    echo html_writer::start_tag('tr', array('class' => 'ccheader'));
165    echo html_writer::tag('th', get_string('criteriagroup', 'block_completionstatus'), array('class' => 'c0 header', 'scope' => 'col'));
166    echo html_writer::tag('th', get_string('criteria', 'completion'), array('class' => 'c1 header', 'scope' => 'col'));
167    echo html_writer::tag('th', get_string('requirement', 'block_completionstatus'), array('class' => 'c2 header', 'scope' => 'col'));
168    echo html_writer::tag('th', get_string('status'), array('class' => 'c3 header', 'scope' => 'col'));
169    echo html_writer::tag('th', get_string('complete'), array('class' => 'c4 header', 'scope' => 'col'));
170    echo html_writer::tag('th', get_string('completiondate', 'report_completion'), array('class' => 'c5 header', 'scope' => 'col'));
171    echo html_writer::end_tag('tr');
172
173    // Save row data.
174    $rows = array();
175
176    // Loop through course criteria.
177    foreach ($completions as $completion) {
178        $criteria = $completion->get_criteria();
179
180        $row = array();
181        $row['type'] = $criteria->criteriatype;
182        $row['title'] = $criteria->get_title();
183        $row['status'] = $completion->get_status();
184        $row['complete'] = $completion->is_complete();
185        $row['timecompleted'] = $completion->timecompleted;
186        $row['details'] = $criteria->get_details($completion);
187        $rows[] = $row;
188    }
189
190    // Print table.
191    $last_type = '';
192    $agg_type = false;
193    $oddeven = 0;
194
195    foreach ($rows as $row) {
196
197        echo html_writer::start_tag('tr', array('class' => 'r' . $oddeven));
198        // Criteria group.
199        echo html_writer::start_tag('td', array('class' => 'cell c0'));
200        if ($last_type !== $row['details']['type']) {
201            $last_type = $row['details']['type'];
202            echo $last_type;
203
204            // Reset agg type.
205            $agg_type = true;
206        } else {
207            // Display aggregation type.
208            if ($agg_type) {
209                $agg = $info->get_aggregation_method($row['type']);
210                echo '('. html_writer::start_tag('i');
211                if ($agg == COMPLETION_AGGREGATION_ALL) {
212                    echo core_text::strtolower(get_string('all', 'completion'));
213                } else {
214                    echo core_text::strtolower(get_string('any', 'completion'));
215                }
216
217                echo ' ' . html_writer::end_tag('i') .core_text::strtolower(get_string('required')).')';
218                $agg_type = false;
219            }
220        }
221        echo html_writer::end_tag('td');
222
223        // Criteria title.
224        echo html_writer::start_tag('td', array('class' => 'cell c1'));
225        echo $row['details']['criteria'];
226        echo html_writer::end_tag('td');
227
228        // Requirement.
229        echo html_writer::start_tag('td', array('class' => 'cell c2'));
230        echo $row['details']['requirement'];
231        echo html_writer::end_tag('td');
232
233        // Status.
234        echo html_writer::start_tag('td', array('class' => 'cell c3'));
235        echo $row['details']['status'];
236        echo html_writer::end_tag('td');
237
238        // Is complete.
239        echo html_writer::start_tag('td', array('class' => 'cell c4'));
240        echo $row['complete'] ? get_string('yes') : get_string('no');
241        echo html_writer::end_tag('td');
242
243        // Completion data.
244        echo html_writer::start_tag('td', array('class' => 'cell c5'));
245        if ($row['timecompleted']) {
246            echo userdate($row['timecompleted'], get_string('strftimedate', 'langconfig'));
247        } else {
248            echo '-';
249        }
250        echo html_writer::end_tag('td');
251        echo html_writer::end_tag('tr');
252        // For row striping.
253        $oddeven = $oddeven ? 0 : 1;
254    }
255
256    echo html_writer::end_tag('tbody');
257    echo html_writer::end_tag('table');
258}
259$courseurl = new moodle_url("/course/view.php", array('id' => $course->id));
260echo html_writer::start_tag('div', array('class' => 'buttons'));
261echo $OUTPUT->single_button($courseurl, get_string('returntocourse', 'block_completionstatus'), 'get');
262echo html_writer::end_tag('div');
263echo $OUTPUT->footer();
264