MDL-37473 completion: Add missing cap checks to tracked users functions
[moodle.git] / blocks / completionstatus / block_completionstatus.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
MD
17/**
18 * Block for displayed logged in user's course completion status
19 *
ffd8bb20
PS
20 * @package block
21 * @subpackage completion
2ea5b0bb 22 * @copyright 2009-2012 Catalyst IT Ltd
ffd8bb20
PS
23 * @author Aaron Barnes <aaronb@catalyst.net.nz>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2be4d090 25 */
ffd8bb20
PS
26
27defined('MOODLE_INTERNAL') || die();
28
2ea5b0bb 29require_once("{$CFG->libdir}/completionlib.php");
2be4d090
MD
30
31/**
32 * Course completion status
33 * Displays overall, and individual criteria status for logged in user
34 */
35class block_completionstatus extends block_base {
36
37 public function init() {
2ea5b0bb 38 $this->title = get_string('pluginname', 'block_completionstatus');
2be4d090
MD
39 }
40
bfe20414
MN
41 function applicable_formats() {
42 return array('all' => true, 'mod' => false, 'tag' => false, 'my' => false);
43 }
44
2be4d090 45 public function get_content() {
2ea5b0bb 46 global $USER;
2be4d090
MD
47
48 // If content is cached
49 if ($this->content !== NULL) {
50 return $this->content;
51 }
52
2ea5b0bb
AB
53 $course = $this->page->course;
54 $context = context_course::instance($course->id);
55
2be4d090 56 // Create empty content
2ea5b0bb 57 $this->content = new stdClass();
2be4d090 58
0e5e1daa 59 // Can edit settings?
2ea5b0bb 60 $can_edit = has_capability('moodle/course:update', $context);
0e5e1daa
AB
61
62 // Get course completion data
2ea5b0bb 63 $info = new completion_info($course);
0e5e1daa 64
2be4d090 65 // Don't display if completion isn't enabled!
0e5e1daa
AB
66 if (!completion_info::is_enabled_for_site()) {
67 if ($can_edit) {
68 $this->content->text = get_string('completionnotenabledforsite', 'completion');
69 }
70 return $this->content;
71
72 } else if (!$info->is_enabled()) {
73 if ($can_edit) {
74 $this->content->text = get_string('completionnotenabledforcourse', 'completion');
75 }
2be4d090
MD
76 return $this->content;
77 }
78
79 // Load criteria to display
2be4d090
MD
80 $completions = $info->get_completions($USER->id);
81
82 // Check if this course has any criteria
83 if (empty($completions)) {
0e5e1daa
AB
84 if ($can_edit) {
85 $this->content->text = get_string('nocriteriaset', 'completion');
86 }
2be4d090
MD
87 return $this->content;
88 }
89
90 // Check this user is enroled
4dc44fc5 91 if ($info->is_tracked_user($USER->id)) {
2be4d090 92
4dc44fc5
JF
93 // Generate markup for criteria statuses
94 $shtml = '';
2be4d090 95
4dc44fc5
JF
96 // For aggregating activity completion
97 $activities = array();
98 $activities_complete = 0;
2be4d090 99
4dc44fc5
JF
100 // For aggregating course prerequisites
101 $prerequisites = array();
102 $prerequisites_complete = 0;
2be4d090 103
4dc44fc5
JF
104 // Flag to set if current completion data is inconsistent with
105 // what is stored in the database
106 $pending_update = false;
2be4d090 107
4dc44fc5
JF
108 // Loop through course criteria
109 foreach ($completions as $completion) {
2be4d090 110
4dc44fc5
JF
111 $criteria = $completion->get_criteria();
112 $complete = $completion->is_complete();
2be4d090 113
4dc44fc5
JF
114 if (!$pending_update && $criteria->is_pending($completion)) {
115 $pending_update = true;
116 }
2be4d090 117
4dc44fc5
JF
118 // Activities are a special case, so cache them and leave them till last
119 if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
120 $activities[$criteria->moduleinstance] = $complete;
2be4d090 121
4dc44fc5
JF
122 if ($complete) {
123 $activities_complete++;
124 }
2be4d090 125
4dc44fc5 126 continue;
2be4d090
MD
127 }
128
4dc44fc5
JF
129 // Prerequisites are also a special case, so cache them and leave them till last
130 if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) {
131 $prerequisites[$criteria->courseinstance] = $complete;
2be4d090 132
4dc44fc5
JF
133 if ($complete) {
134 $prerequisites_complete++;
135 }
2be4d090 136
4dc44fc5 137 continue;
2be4d090
MD
138 }
139
4dc44fc5
JF
140 $shtml .= '<tr><td>';
141 $shtml .= $criteria->get_title();
142 $shtml .= '</td><td style="text-align: right">';
143 $shtml .= $completion->get_status();
144 $shtml .= '</td></tr>';
2be4d090
MD
145 }
146
4dc44fc5
JF
147 // Aggregate activities
148 if (!empty($activities)) {
149
150 $shtml .= '<tr><td>';
151 $shtml .= get_string('activitiescompleted', 'completion');
152 $shtml .= '</td><td style="text-align: right">';
153 $a = new stdClass();
154 $a->first = $activities_complete;
155 $a->second = count($activities);
156 $shtml .= get_string('firstofsecond', 'block_completionstatus', $a);
157 $shtml .= '</td></tr>';
158 }
2be4d090 159
4dc44fc5
JF
160 // Aggregate prerequisites
161 if (!empty($prerequisites)) {
2be4d090 162
4dc44fc5
JF
163 $phtml = '<tr><td>';
164 $phtml .= get_string('dependenciescompleted', 'completion');
165 $phtml .= '</td><td style="text-align: right">';
166 $a = new stdClass();
167 $a->first = $prerequisites_complete;
168 $a->second = count($prerequisites);
169 $phtml .= get_string('firstofsecond', 'block_completionstatus', $a);
170 $phtml .= '</td></tr>';
2be4d090 171
4dc44fc5
JF
172 $shtml = $phtml . $shtml;
173 }
2be4d090 174
4dc44fc5
JF
175 // Display completion status
176 $this->content->text = '<table width="100%" style="font-size: 90%;"><tbody>';
177 $this->content->text .= '<tr><td colspan="2"><b>'.get_string('status').':</b> ';
178
179 // Is course complete?
180 $coursecomplete = $info->is_course_complete($USER->id);
181
182 // Load course completion
183 $params = array(
184 'userid' => $USER->id,
185 'course' => $course->id
186 );
187 $ccompletion = new completion_completion($params);
188
189 // Has this user completed any criteria?
190 $criteriacomplete = $info->count_course_user_data($USER->id);
191
192 if ($pending_update) {
193 $this->content->text .= '<i>'.get_string('pending', 'completion').'</i>';
194 } else if ($coursecomplete) {
195 $this->content->text .= get_string('complete');
196 } else if (!$criteriacomplete && !$ccompletion->timestarted) {
197 $this->content->text .= '<i>'.get_string('notyetstarted', 'completion').'</i>';
198 } else {
199 $this->content->text .= '<i>'.get_string('inprogress','completion').'</i>';
200 }
2be4d090 201
4dc44fc5
JF
202 $this->content->text .= '</td></tr>';
203 $this->content->text .= '<tr><td colspan="2">';
2be4d090 204
4dc44fc5
JF
205 // Get overall aggregation method
206 $overall = $info->get_aggregation_method();
2be4d090 207
4dc44fc5
JF
208 if ($overall == COMPLETION_AGGREGATION_ALL) {
209 $this->content->text .= get_string('criteriarequiredall', 'completion');
210 } else {
211 $this->content->text .= get_string('criteriarequiredany', 'completion');
212 }
213
214 $this->content->text .= ':</td></tr>';
215 $this->content->text .= '<tr><td><b>'.get_string('requiredcriteria', 'completion').'</b></td><td style="text-align: right"><b>'.get_string('status').'</b></td></tr>';
216 $this->content->text .= $shtml.'</tbody></table>';
2be4d090 217
4dc44fc5
JF
218 // Display link to detailed view
219 $details = new moodle_url('/blocks/completionstatus/details.php', array('course' => $course->id));
220 $this->content->footer = '<br><a href="'.$details->out().'">'.get_string('moredetails', 'completion').'</a>';
2be4d090 221 } else {
4dc44fc5 222 // If user is not enrolled, show error
10a8581f 223 $this->content->text = get_string('nottracked', 'completion');
2be4d090
MD
224 }
225
4dc44fc5
JF
226 if (has_capability('report/completion:view', $context)) {
227 $report = new moodle_url('/report/completion/index.php', array('course' => $course->id));
228 $this->content->footer .= '<br /><a href="'.$report->out().'">'.get_string('viewcoursereport', 'completion').'</a>';
229 }
2be4d090 230
2be4d090
MD
231
232 return $this->content;
233 }
234}