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