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