MDL-34476 - Blocks - Fixing context issue
[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
f0c1bcf5 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
f0c1bcf5 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() {
f0c1bcf5 38 $this->title = get_string('pluginname', 'block_completionstatus');
2be4d090
MD
39 }
40
41 public function get_content() {
f0c1bcf5 42 global $USER;
2be4d090
MD
43
44 // If content is cached
45 if ($this->content !== NULL) {
46 return $this->content;
47 }
48
f0c1bcf5
AB
49 $course = $this->page->course;
50
2be4d090 51 // Create empty content
f0c1bcf5 52 $this->content = new stdClass();
2be4d090 53
f0f33380
JF
54 $context = get_context_instance(CONTEXT_COURSE, $course->id);
55
0e5e1daa 56 // Can edit settings?
f0f33380 57 $can_edit = has_capability('moodle/course:update', $context);
0e5e1daa
AB
58
59 // Get course completion data
f0c1bcf5 60 $info = new completion_info($course);
0e5e1daa 61
2be4d090 62 // Don't display if completion isn't enabled!
0e5e1daa
AB
63 if (!completion_info::is_enabled_for_site()) {
64 if ($can_edit) {
65 $this->content->text = get_string('completionnotenabledforsite', 'completion');
66 }
67 return $this->content;
68
69 } else if (!$info->is_enabled()) {
70 if ($can_edit) {
71 $this->content->text = get_string('completionnotenabledforcourse', 'completion');
72 }
2be4d090
MD
73 return $this->content;
74 }
75
76 // Load criteria to display
2be4d090
MD
77 $completions = $info->get_completions($USER->id);
78
79 // Check if this course has any criteria
80 if (empty($completions)) {
0e5e1daa
AB
81 if ($can_edit) {
82 $this->content->text = get_string('nocriteriaset', 'completion');
83 }
2be4d090
MD
84 return $this->content;
85 }
86
87 // Check this user is enroled
94167a2d 88 if ($info->is_tracked_user($USER->id)) {
2be4d090 89
94167a2d
JF
90 // Generate markup for criteria statuses
91 $shtml = '';
2be4d090 92
94167a2d
JF
93 // For aggregating activity completion
94 $activities = array();
95 $activities_complete = 0;
2be4d090 96
94167a2d
JF
97 // For aggregating course prerequisites
98 $prerequisites = array();
99 $prerequisites_complete = 0;
2be4d090 100
94167a2d
JF
101 // Flag to set if current completion data is inconsistent with
102 // what is stored in the database
103 $pending_update = false;
2be4d090 104
94167a2d
JF
105 // Loop through course criteria
106 foreach ($completions as $completion) {
2be4d090 107
94167a2d
JF
108 $criteria = $completion->get_criteria();
109 $complete = $completion->is_complete();
2be4d090 110
94167a2d
JF
111 if (!$pending_update && $criteria->is_pending($completion)) {
112 $pending_update = true;
113 }
2be4d090 114
94167a2d
JF
115 // Activities are a special case, so cache them and leave them till last
116 if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
117 $activities[$criteria->moduleinstance] = $complete;
2be4d090 118
94167a2d
JF
119 if ($complete) {
120 $activities_complete++;
121 }
2be4d090 122
94167a2d 123 continue;
2be4d090
MD
124 }
125
94167a2d
JF
126 // Prerequisites are also a special case, so cache them and leave them till last
127 if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) {
128 $prerequisites[$criteria->courseinstance] = $complete;
2be4d090 129
94167a2d
JF
130 if ($complete) {
131 $prerequisites_complete++;
132 }
2be4d090 133
94167a2d 134 continue;
2be4d090
MD
135 }
136
94167a2d
JF
137 $shtml .= '<tr><td>';
138 $shtml .= $criteria->get_title();
139 $shtml .= '</td><td style="text-align: right">';
140 $shtml .= $completion->get_status();
141 $shtml .= '</td></tr>';
2be4d090
MD
142 }
143
94167a2d
JF
144 // Aggregate activities
145 if (!empty($activities)) {
146
147 $shtml .= '<tr><td>';
148 $shtml .= get_string('activitiescompleted', 'completion');
149 $shtml .= '</td><td style="text-align: right">';
150 $a = new stdClass();
151 $a->first = $activities_complete;
152 $a->second = count($activities);
153 $shtml .= get_string('firstofsecond', 'block_completionstatus', $a);
154 $shtml .= '</td></tr>';
155 }
2be4d090 156
94167a2d
JF
157 // Aggregate prerequisites
158 if (!empty($prerequisites)) {
2be4d090 159
94167a2d
JF
160 $phtml = '<tr><td>';
161 $phtml .= get_string('prerequisitescompleted', 'completion');
162 $phtml .= '</td><td style="text-align: right">';
163 $a = new stdClass();
164 $a->first = $prerequisites_complete;
165 $a->second = count($prerequisites);
166 $phtml .= get_string('firstofsecond', 'block_completionstatus', $a);
167 $phtml .= '</td></tr>';
2be4d090 168
94167a2d
JF
169 $shtml = $phtml . $shtml;
170 }
2be4d090 171
94167a2d
JF
172 // Display completion status
173 $this->content->text = '<table width="100%" style="font-size: 90%;"><tbody>';
174 $this->content->text .= '<tr><td colspan="2"><b>'.get_string('status').':</b> ';
175
176 // Is course complete?
177 $coursecomplete = $info->is_course_complete($USER->id);
178
179 // Load course completion
180 $params = array(
181 'userid' => $USER->id,
182 'course' => $course->id
183 );
184 $ccompletion = new completion_completion($params);
185
186 // Has this user completed any criteria?
187 $criteriacomplete = $info->count_course_user_data($USER->id);
188
189 if ($pending_update) {
190 $this->content->text .= '<i>'.get_string('pending', 'completion').'</i>';
191 } else if ($coursecomplete) {
192 $this->content->text .= get_string('complete');
193 } else if (!$criteriacomplete && !$ccompletion->timestarted) {
194 $this->content->text .= '<i>'.get_string('notyetstarted', 'completion').'</i>';
195 } else {
196 $this->content->text .= '<i>'.get_string('inprogress','completion').'</i>';
197 }
2be4d090 198
94167a2d
JF
199 $this->content->text .= '</td></tr>';
200 $this->content->text .= '<tr><td colspan="2">';
201
202 // Get overall aggregation method
203 $overall = $info->get_aggregation_method();
2be4d090 204
94167a2d
JF
205 if ($overall == COMPLETION_AGGREGATION_ALL) {
206 $this->content->text .= get_string('criteriarequiredall', 'completion');
207 } else {
208 $this->content->text .= get_string('criteriarequiredany', 'completion');
209 }
2be4d090 210
94167a2d
JF
211 $this->content->text .= ':</td></tr>';
212 $this->content->text .= '<tr><td><b>'.get_string('requiredcriteria', 'completion').'</b></td><td style="text-align: right"><b>'.get_string('status').'</b></td></tr>';
213 $this->content->text .= $shtml.'</tbody></table>';
2be4d090 214
94167a2d
JF
215 // Display link to detailed view
216 $details = new moodle_url('/blocks/completionstatus/details.php', array('course' => $course->id));
217 $this->content->footer = '<br><a href="'.$details->out().'">'.get_string('moredetails', 'completion').'</a>';
2be4d090 218 } else {
94167a2d
JF
219 // If user is not enrolled, show error
220 $this->content->text = get_string('notenroled', 'completion');
2be4d090
MD
221 }
222
94167a2d
JF
223 if (has_capability('report/completion:view', $context)) {
224 $report = new moodle_url('/report/completion/index.php', array('course' => $course->id));
225 $this->content->footer .= '<br /><a href="'.$report->out().'">'.get_string('viewcoursereport', 'completion').'</a>';
226 }
2be4d090
MD
227 return $this->content;
228 }
229}