MDL-29030 update themes to use new report selectors
[moodle.git] / blocks / completionstatus / block_completionstatus.php
CommitLineData
2be4d090
MD
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * Block for displayed logged in user's course completion status
21 *
22 * @package moodlecore
23 * @copyright 2009 Catalyst IT Ltd
24 * @author Aaron Barnes <aaronb@catalyst.net.nz>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27require_once($CFG->libdir.'/completionlib.php');
28
29/**
30 * Course completion status
31 * Displays overall, and individual criteria status for logged in user
32 */
33class block_completionstatus extends block_base {
34
35 public function init() {
36 $this->title = get_string('completionstatus', 'block_completionstatus');
2be4d090
MD
37 }
38
39 public function get_content() {
40 global $USER, $CFG, $DB, $COURSE;
41
42 // If content is cached
43 if ($this->content !== NULL) {
44 return $this->content;
45 }
46
47 // Create empty content
48 $this->content = new stdClass;
49
0e5e1daa
AB
50 // Can edit settings?
51 $can_edit = has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $this->page->course->id));
52
53 // Get course completion data
54 $info = new completion_info($this->page->course);
55
2be4d090 56 // Don't display if completion isn't enabled!
0e5e1daa
AB
57 if (!completion_info::is_enabled_for_site()) {
58 if ($can_edit) {
59 $this->content->text = get_string('completionnotenabledforsite', 'completion');
60 }
61 return $this->content;
62
63 } else if (!$info->is_enabled()) {
64 if ($can_edit) {
65 $this->content->text = get_string('completionnotenabledforcourse', 'completion');
66 }
2be4d090
MD
67 return $this->content;
68 }
69
70 // Load criteria to display
2be4d090
MD
71 $completions = $info->get_completions($USER->id);
72
73 // Check if this course has any criteria
74 if (empty($completions)) {
0e5e1daa
AB
75 if ($can_edit) {
76 $this->content->text = get_string('nocriteriaset', 'completion');
77 }
2be4d090
MD
78 return $this->content;
79 }
80
81 // Check this user is enroled
24a3b341 82 if (!$info->is_tracked_user($USER->id)) {
2be4d090
MD
83 // If not enrolled, but are can view the report:
84 if (has_capability('coursereport/completion:view', get_context_instance(CONTEXT_COURSE, $COURSE->id))) {
85 $this->content->text = '<a href="'.$CFG->wwwroot.'/course/report/completion/index.php?course='.$COURSE->id.
86 '">'.get_string('viewcoursereport', 'completion').'</a>';
87 return $this->content;
88 }
89
90 // Otherwise, show error
91 $this->content->text = get_string('notenroled', 'completion');
92 return $this->content;
93 }
94
95 // Generate markup for criteria statuses
96 $shtml = '';
97
98 // For aggregating activity completion
99 $activities = array();
100 $activities_complete = 0;
101
102 // For aggregating course prerequisites
103 $prerequisites = array();
104 $prerequisites_complete = 0;
105
106 // Flag to set if current completion data is inconsistent with
107 // what is stored in the database
108 $pending_update = false;
109
110 // Loop through course criteria
111 foreach ($completions as $completion) {
112
113 $criteria = $completion->get_criteria();
114 $complete = $completion->is_complete();
115
116 if (!$pending_update && $criteria->is_pending($completion)) {
117 $pending_update = true;
118 }
119
120 // Activities are a special case, so cache them and leave them till last
121 if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
122 $activities[$criteria->moduleinstance] = $complete;
123
124 if ($complete) {
125 $activities_complete++;
126 }
127
128 continue;
129 }
130
131 // Prerequisites are also a special case, so cache them and leave them till last
132 if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) {
133 $prerequisites[$criteria->courseinstance] = $complete;
134
135 if ($complete) {
136 $prerequisites_complete++;
137 }
138
139 continue;
140 }
141
142 $shtml .= '<tr><td>';
143 $shtml .= $criteria->get_title();
144 $shtml .= '</td><td style="text-align: right">';
145 $shtml .= $completion->get_status();
146 $shtml .= '</td></tr>';
147 }
148
149 // Aggregate activities
150 if (!empty($activities)) {
151
152 $shtml .= '<tr><td>';
153 $shtml .= get_string('activitiescompleted', 'completion');
154 $shtml .= '</td><td style="text-align: right">';
a58f4d55
DP
155 $a = new stdClass();
156 $a->first = $activities_complete;
157 $a->second = count($activities);
158 $shtml .= get_string('firstofsecond', 'block_completionstatus', $a);
2be4d090
MD
159 $shtml .= '</td></tr>';
160 }
161
162 // Aggregate prerequisites
163 if (!empty($prerequisites)) {
164
165 $phtml = '<tr><td>';
166 $phtml .= get_string('prerequisitescompleted', 'completion');
167 $phtml .= '</td><td style="text-align: right">';
a58f4d55
DP
168 $a = new stdClass();
169 $a->first = $prerequisites_complete;
170 $a->second = count($prerequisites);
171 $shtml .= get_string('firstofsecond', 'block_completionstatus', $a);
2be4d090
MD
172 $phtml .= '</td></tr>';
173
174 $shtml = $phtml . $shtml;
175 }
176
177 // Display completion status
178 $this->content->text = '<table width="100%" style="font-size: 90%;"><tbody>';
179 $this->content->text .= '<tr><td colspan="2"><b>'.get_string('status').':</b> ';
180
181 // Is course complete?
182 $coursecomplete = $info->is_course_complete($USER->id);
0b15b43b 183
b2f82f80
AB
184 // Load course completion
185 $params = array(
186 'userid' => $USER->id,
187 'course' => $COURSE->id
188 );
189 $ccompletion = new completion_completion($params);
2be4d090
MD
190
191 // Has this user completed any criteria?
192 $criteriacomplete = $info->count_course_user_data($USER->id);
193
194 if ($pending_update) {
195 $this->content->text .= '<i>'.get_string('pending', 'completion').'</i>';
196 } else if ($coursecomplete) {
197 $this->content->text .= get_string('complete');
cba8b026 198 } else if (!$criteriacomplete && !$ccompletion->timestarted) {
2be4d090
MD
199 $this->content->text .= '<i>'.get_string('notyetstarted', 'completion').'</i>';
200 } else {
201 $this->content->text .= '<i>'.get_string('inprogress','completion').'</i>';
202 }
203
204 $this->content->text .= '</td></tr>';
205 $this->content->text .= '<tr><td colspan="2">';
206
207 // Get overall aggregation method
208 $overall = $info->get_aggregation_method();
209
210 if ($overall == COMPLETION_AGGREGATION_ALL) {
211 $this->content->text .= get_string('criteriarequiredall', 'completion');
212 } else {
213 $this->content->text .= get_string('criteriarequiredany', 'completion');
214 }
215
216 $this->content->text .= ':</td></tr>';
217 $this->content->text .= '<tr><td><b>'.get_string('requiredcriteria', 'completion').'</b></td><td style="text-align: right"><b>'.get_string('status').'</b></td></tr>';
218 $this->content->text .= $shtml.'</tbody></table>';
219
220 // Display link to detailed view
6d35a09b 221 $this->content->footer = '<br><a href="'.$CFG->wwwroot.'/blocks/completionstatus/details.php?course='.$COURSE->id.'">'.get_string('moredetails', 'completion').'</a>';
2be4d090
MD
222
223 return $this->content;
224 }
225}