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