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