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