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