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