MDL-65788 tool_policy: Prevent two modals rendering for guests.
[moodle.git] / blocks / completionstatus / details.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
2be4d090 17/**
f982303d 18 * Block for displaying logged in user's course completion status
2be4d090 19 *
f25a6839 20 * @package block_completionstatus
2ea5b0bb 21 * @copyright 2009-2012 Catalyst IT Ltd
ffd8bb20
PS
22 * @author Aaron Barnes <aaronb@catalyst.net.nz>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2be4d090 24 */
ffd8bb20 25
1fcf0ca8 26require_once(__DIR__.'/../../config.php');
2ea5b0bb 27require_once("{$CFG->libdir}/completionlib.php");
2be4d090 28
f982303d 29// Load data.
2be4d090 30$id = required_param('course', PARAM_INT);
2be4d090
MD
31$userid = optional_param('user', 0, PARAM_INT);
32
f982303d 33// Load course.
74df2951 34$course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
2be4d090 35
f982303d 36// Load user.
2be4d090 37if ($userid) {
a49cef3e 38 $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
2be4d090 39} else {
a49cef3e 40 $user = $USER;
2be4d090
MD
41}
42
f982303d 43// Check permissions.
5bb05a46 44require_login();
2be4d090 45
5bb05a46 46if (!completion_can_view_data($user->id, $course)) {
a49cef3e 47 print_error('cannotviewreport');
2be4d090
MD
48}
49
f982303d 50// Load completion data.
0e5e1daa
AB
51$info = new completion_info($course);
52
2ea5b0bb 53$returnurl = new moodle_url('/course/view.php', array('id' => $id));
0e5e1daa 54
f982303d 55// Don't display if completion isn't enabled.
0e5e1daa
AB
56if (!$info->is_enabled()) {
57 print_error('completionnotenabled', 'completion', $returnurl);
2be4d090
MD
58}
59
f982303d 60// Check this user is enroled.
24a3b341 61if (!$info->is_tracked_user($user->id)) {
0e5e1daa
AB
62 if ($USER->id == $user->id) {
63 print_error('notenroled', 'completion', $returnurl);
64 } else {
65 print_error('usernotenroled', 'completion', $returnurl);
66 }
2be4d090
MD
67}
68
f982303d 69// Display page.
2be4d090 70
2ea5b0bb 71$PAGE->set_context(context_course::instance($course->id));
2be4d090 72
f982303d 73// Print header.
2be4d090
MD
74$page = get_string('completionprogressdetails', 'block_completionstatus');
75$title = format_string($course->fullname) . ': ' . $page;
76
a49cef3e 77$PAGE->navbar->add($page);
566889aa 78$PAGE->set_pagelayout('report');
2ea5b0bb 79$PAGE->set_url('/blocks/completionstatus/details.php', array('course' => $course->id, 'user' => $user->id));
2be4d090
MD
80$PAGE->set_title(get_string('course') . ': ' . $course->fullname);
81$PAGE->set_heading($title);
82echo $OUTPUT->header();
83
84
f982303d
JF
85// Display completion status.
86echo html_writer::start_tag('table', array('class' => 'generalbox boxaligncenter'));
87echo html_writer::start_tag('tbody');
2be4d090 88
f982303d 89// If not display logged in user, show user name.
2be4d090 90if ($USER->id != $user->id) {
f982303d
JF
91 echo html_writer::start_tag('tr');
92 echo html_writer::start_tag('td', array('colspan' => '2'));
cf052a86 93 echo html_writer::tag('b', get_string('showinguser', 'completion') . ' ');
f982303d
JF
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');
2be4d090
MD
98}
99
f982303d
JF
100echo html_writer::start_tag('tr');
101echo html_writer::start_tag('td', array('colspan' => '2'));
cf052a86 102echo html_writer::tag('b', get_string('status') . ' ');
2be4d090
MD
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
f982303d 110// Load course completion.
2ea5b0bb
AB
111$params = array(
112 'userid' => $user->id,
113 'course' => $course->id,
114);
115$ccompletion = new completion_completion($params);
116
2be4d090
MD
117if ($coursecomplete) {
118 echo get_string('complete');
2ea5b0bb 119} else if (!$criteriacomplete && !$ccompletion->timestarted) {
f982303d 120 echo html_writer::tag('i', get_string('notyetstarted', 'completion'));
2be4d090 121} else {
f982303d 122 echo html_writer::tag('i', get_string('inprogress', 'completion'));
2be4d090
MD
123}
124
f982303d
JF
125echo html_writer::end_tag('td');
126echo html_writer::end_tag('tr');
2be4d090 127
f982303d 128// Load criteria to display.
2ea5b0bb 129$completions = $info->get_completions($user->id);
2be4d090 130
f982303d 131// Check if this course has any criteria.
2ea5b0bb 132if (empty($completions)) {
f982303d
JF
133 echo html_writer::start_tag('tr');
134 echo html_writer::start_tag('td', array('colspan' => '2'));
135 echo html_writer::start_tag('br');
03f3f718 136 echo $OUTPUT->box(get_string('nocriteriaset', 'completion'), 'noticebox');
f982303d
JF
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');
2be4d090 141} else {
f982303d
JF
142 echo html_writer::start_tag('tr');
143 echo html_writer::start_tag('td', array('colspan' => '2'));
cf052a86 144 echo html_writer::tag('b', get_string('required') . ' ');
2be4d090 145
f982303d 146 // Get overall aggregation method.
2ea5b0bb 147 $overall = $info->get_aggregation_method();
2be4d090 148
2ea5b0bb
AB
149 if ($overall == COMPLETION_AGGREGATION_ALL) {
150 echo get_string('criteriarequiredall', 'completion');
151 } else {
152 echo get_string('criteriarequiredany', 'completion');
153 }
154
f982303d
JF
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.
2ea5b0bb
AB
174 $rows = array();
175
f982303d 176 // Loop through course criteria.
2ea5b0bb
AB
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 }
2be4d090 189
f982303d 190 // Print table.
2ea5b0bb
AB
191 $last_type = '';
192 $agg_type = false;
193 $oddeven = 0;
2be4d090 194
2ea5b0bb 195 foreach ($rows as $row) {
2be4d090 196
f982303d
JF
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'));
2ea5b0bb
AB
200 if ($last_type !== $row['details']['type']) {
201 $last_type = $row['details']['type'];
202 echo $last_type;
203
f982303d 204 // Reset agg type.
2ea5b0bb
AB
205 $agg_type = true;
206 } else {
f982303d 207 // Display aggregation type.
2ea5b0bb
AB
208 if ($agg_type) {
209 $agg = $info->get_aggregation_method($row['type']);
f982303d 210 echo '('. html_writer::start_tag('i');
2ea5b0bb 211 if ($agg == COMPLETION_AGGREGATION_ALL) {
2f1e464a 212 echo core_text::strtolower(get_string('all', 'completion'));
2ea5b0bb 213 } else {
2f1e464a 214 echo core_text::strtolower(get_string('any', 'completion'));
2ea5b0bb
AB
215 }
216
cf052a86 217 echo ' ' . html_writer::end_tag('i') .core_text::strtolower(get_string('required')).')';
2ea5b0bb
AB
218 $agg_type = false;
219 }
2be4d090 220 }
f982303d 221 echo html_writer::end_tag('td');
2ea5b0bb 222
f982303d
JF
223 // Criteria title.
224 echo html_writer::start_tag('td', array('class' => 'cell c1'));
2ea5b0bb 225 echo $row['details']['criteria'];
f982303d 226 echo html_writer::end_tag('td');
2ea5b0bb 227
f982303d
JF
228 // Requirement.
229 echo html_writer::start_tag('td', array('class' => 'cell c2'));
2ea5b0bb 230 echo $row['details']['requirement'];
f982303d 231 echo html_writer::end_tag('td');
2ea5b0bb 232
f982303d
JF
233 // Status.
234 echo html_writer::start_tag('td', array('class' => 'cell c3'));
2ea5b0bb 235 echo $row['details']['status'];
f982303d 236 echo html_writer::end_tag('td');
2ea5b0bb 237
f982303d
JF
238 // Is complete.
239 echo html_writer::start_tag('td', array('class' => 'cell c4'));
2ea5b0bb 240 echo $row['complete'] ? get_string('yes') : get_string('no');
f982303d 241 echo html_writer::end_tag('td');
2ea5b0bb 242
f982303d
JF
243 // Completion data.
244 echo html_writer::start_tag('td', array('class' => 'cell c5'));
2ea5b0bb
AB
245 if ($row['timecompleted']) {
246 echo userdate($row['timecompleted'], get_string('strftimedate', 'langconfig'));
247 } else {
248 echo '-';
249 }
f982303d
JF
250 echo html_writer::end_tag('td');
251 echo html_writer::end_tag('tr');
252 // For row striping.
2ea5b0bb 253 $oddeven = $oddeven ? 0 : 1;
2be4d090 254 }
2ea5b0bb 255
f982303d
JF
256 echo html_writer::end_tag('tbody');
257 echo html_writer::end_tag('table');
2be4d090 258}
2ea5b0bb 259$courseurl = new moodle_url("/course/view.php", array('id' => $course->id));
f982303d 260echo html_writer::start_tag('div', array('class' => 'buttons'));
2ea5b0bb 261echo $OUTPUT->single_button($courseurl, get_string('returntocourse', 'block_completionstatus'), 'get');
f982303d 262echo html_writer::end_tag('div');
2be4d090 263echo $OUTPUT->footer();