2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Block for displayed logged in user's course completion status
20 * @package block_completionstatus
21 * @copyright 2009-2012 Catalyst IT Ltd
22 * @author Aaron Barnes <aaronb@catalyst.net.nz>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
28 require_once("{$CFG->libdir}/completionlib.php");
31 * Course completion status.
32 * Displays overall, and individual criteria status for logged in user.
34 class block_completionstatus
extends block_base
{
36 public function init() {
37 $this->title
= get_string('pluginname', 'block_completionstatus');
40 public function applicable_formats() {
41 return array('course' => true);
44 public function get_content() {
50 // If content is cached.
51 if ($this->content
!== null) {
52 return $this->content
;
55 $course = $this->page
->course
;
56 $context = context_course
::instance($course->id
);
58 // Create empty content.
59 $this->content
= new stdClass();
60 $this->content
->text
= '';
61 $this->content
->footer
= '';
64 $can_edit = has_capability('moodle/course:update', $context);
66 // Get course completion data.
67 $info = new completion_info($course);
69 // Don't display if completion isn't enabled!
70 if (!completion_info
::is_enabled_for_site()) {
72 $this->content
->text
.= get_string('completionnotenabledforsite', 'completion');
74 return $this->content
;
76 } else if (!$info->is_enabled()) {
78 $this->content
->text
.= get_string('completionnotenabledforcourse', 'completion');
80 return $this->content
;
83 // Load criteria to display.
84 $completions = $info->get_completions($USER->id
);
86 // Check if this course has any criteria.
87 if (empty($completions)) {
89 $this->content
->text
.= get_string('nocriteriaset', 'completion');
91 return $this->content
;
94 // Check this user is enroled.
95 if ($info->is_tracked_user($USER->id
)) {
97 // Generate markup for criteria statuses.
100 // For aggregating activity completion.
101 $activities = array();
102 $activities_complete = 0;
104 // For aggregating course prerequisites.
105 $prerequisites = array();
106 $prerequisites_complete = 0;
108 // Flag to set if current completion data is inconsistent with what is stored in the database.
109 $pending_update = false;
111 // Loop through course criteria.
112 foreach ($completions as $completion) {
113 $criteria = $completion->get_criteria();
114 $complete = $completion->is_complete();
116 if (!$pending_update && $criteria->is_pending($completion)) {
117 $pending_update = true;
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;
125 $activities_complete++
;
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;
136 $prerequisites_complete++
;
141 $row = new html_table_row();
142 $row->cells
[0] = new html_table_cell($criteria->get_title());
143 $row->cells
[1] = new html_table_cell($completion->get_status());
144 $row->cells
[1]->style
= 'text-align: right;';
148 // Aggregate activities.
149 if (!empty($activities)) {
151 $a->first
= $activities_complete;
152 $a->second
= count($activities);
154 $row = new html_table_row();
155 $row->cells
[0] = new html_table_cell(get_string('activitiescompleted', 'completion'));
156 $row->cells
[1] = new html_table_cell(get_string('firstofsecond', 'block_completionstatus', $a));
157 $row->cells
[1]->style
= 'text-align: right;';
161 // Aggregate prerequisites.
162 if (!empty($prerequisites)) {
164 $a->first
= $prerequisites_complete;
165 $a->second
= count($prerequisites);
167 $row = new html_table_row();
168 $row->cells
[0] = new html_table_cell(get_string('dependenciescompleted', 'completion'));
169 $row->cells
[1] = new html_table_cell(get_string('firstofsecond', 'block_completionstatus', $a));
170 $row->cells
[1]->style
= 'text-align: right;';
173 $srows = array_merge($prows, $srows);
176 // Display completion status.
177 $table = new html_table();
178 $table->width
= '100%';
179 $table->attributes
= array('style'=>'font-size: 90%;', 'class'=>'');
181 $row = new html_table_row();
182 $content = html_writer
::tag('b', get_string('status').': ');
184 // Is course complete?
185 $coursecomplete = $info->is_course_complete($USER->id
);
187 // Load course completion.
189 'userid' => $USER->id
,
190 'course' => $course->id
192 $ccompletion = new completion_completion($params);
194 // Has this user completed any criteria?
195 $criteriacomplete = $info->count_course_user_data($USER->id
);
197 if ($pending_update) {
198 $content .= html_writer
::tag('i', get_string('pending', 'completion'));
199 } else if ($coursecomplete) {
200 $content .= get_string('complete');
201 } else if (!$criteriacomplete && !$ccompletion->timestarted
) {
202 $content .= html_writer
::tag('i', get_string('notyetstarted', 'completion'));
204 $content .= html_writer
::tag('i', get_string('inprogress', 'completion'));
207 $row->cells
[0] = new html_table_cell($content);
208 $row->cells
[0]->colspan
= '2';
211 $row = new html_table_row();
213 // Get overall aggregation method.
214 $overall = $info->get_aggregation_method();
215 if ($overall == COMPLETION_AGGREGATION_ALL
) {
216 $content .= get_string('criteriarequiredall', 'completion');
218 $content .= get_string('criteriarequiredany', 'completion');
221 $row->cells
[0] = new html_table_cell($content);
222 $row->cells
[0]->colspan
= '2';
225 $row = new html_table_row();
226 $row->cells
[0] = new html_table_cell(html_writer
::tag('b', get_string('requiredcriteria', 'completion')));
227 $row->cells
[1] = new html_table_cell(html_writer
::tag('b', get_string('status')));
228 $row->cells
[1]->style
= 'text-align: right;';
231 // Array merge $rows and $data here.
232 $rows = array_merge($rows, $srows);
234 $table->data
= $rows;
235 $this->content
->text
.= html_writer
::table($table);
237 // Display link to detailed view.
238 $details = new moodle_url('/blocks/completionstatus/details.php', array('course' => $course->id
));
239 $this->content
->footer
.= html_writer
::link($details, get_string('moredetails', 'completion'));
241 // If user is not enrolled, show error.
242 $this->content
->text
= get_string('nottracked', 'completion');
245 if (has_capability('report/completion:view', $context)) {
246 $report = new moodle_url('/report/completion/index.php', array('course' => $course->id
));
247 if (empty($this->content
->footer
)) {
248 $this->content
->footer
= '';
250 $this->content
->footer
.= html_writer
::empty_tag('br');
251 $this->content
->footer
.= html_writer
::link($report, get_string('viewcoursereport', 'completion'));
254 return $this->content
;