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