MDL-65788 tool_policy: Prevent two modals rendering for guests.
[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 *
f25a6839 20 * @package block_completionstatus
2ea5b0bb 21 * @copyright 2009-2012 Catalyst IT Ltd
ffd8bb20
PS
22 * @author Aaron Barnes <aaronb@catalyst.net.nz>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2be4d090 24 */
ffd8bb20
PS
25
26defined('MOODLE_INTERNAL') || die();
27
2ea5b0bb 28require_once("{$CFG->libdir}/completionlib.php");
2be4d090
MD
29
30/**
f982303d
JF
31 * Course completion status.
32 * Displays overall, and individual criteria status for logged in user.
2be4d090
MD
33 */
34class block_completionstatus extends block_base {
35
36 public function init() {
2ea5b0bb 37 $this->title = get_string('pluginname', 'block_completionstatus');
2be4d090
MD
38 }
39
f982303d 40 public function applicable_formats() {
5b40141f 41 return array('course' => true);
bfe20414
MN
42 }
43
2be4d090 44 public function get_content() {
2ea5b0bb 45 global $USER;
2be4d090 46
f982303d
JF
47 $rows = array();
48 $srows = array();
49 $prows = array();
50 // If content is cached.
51 if ($this->content !== null) {
2be4d090
MD
52 return $this->content;
53 }
54
f982303d 55 $course = $this->page->course;
2ea5b0bb
AB
56 $context = context_course::instance($course->id);
57
f982303d 58 // Create empty content.
2ea5b0bb 59 $this->content = new stdClass();
525d6ce3
AB
60 $this->content->text = '';
61 $this->content->footer = '';
2be4d090 62
0e5e1daa 63 // Can edit settings?
2ea5b0bb 64 $can_edit = has_capability('moodle/course:update', $context);
0e5e1daa 65
f982303d 66 // Get course completion data.
2ea5b0bb 67 $info = new completion_info($course);
0e5e1daa 68
2be4d090 69 // Don't display if completion isn't enabled!
0e5e1daa
AB
70 if (!completion_info::is_enabled_for_site()) {
71 if ($can_edit) {
525d6ce3 72 $this->content->text .= get_string('completionnotenabledforsite', 'completion');
0e5e1daa
AB
73 }
74 return $this->content;
75
76 } else if (!$info->is_enabled()) {
77 if ($can_edit) {
525d6ce3 78 $this->content->text .= get_string('completionnotenabledforcourse', 'completion');
0e5e1daa 79 }
2be4d090
MD
80 return $this->content;
81 }
82
f982303d 83 // Load criteria to display.
2be4d090
MD
84 $completions = $info->get_completions($USER->id);
85
f982303d 86 // Check if this course has any criteria.
2be4d090 87 if (empty($completions)) {
0e5e1daa 88 if ($can_edit) {
525d6ce3 89 $this->content->text .= get_string('nocriteriaset', 'completion');
0e5e1daa 90 }
2be4d090
MD
91 return $this->content;
92 }
93
f982303d 94 // Check this user is enroled.
4dc44fc5 95 if ($info->is_tracked_user($USER->id)) {
2be4d090 96
f982303d
JF
97 // Generate markup for criteria statuses.
98 $data = '';
2be4d090 99
f982303d 100 // For aggregating activity completion.
4dc44fc5
JF
101 $activities = array();
102 $activities_complete = 0;
2be4d090 103
f982303d 104 // For aggregating course prerequisites.
4dc44fc5
JF
105 $prerequisites = array();
106 $prerequisites_complete = 0;
2be4d090 107
f982303d 108 // Flag to set if current completion data is inconsistent with what is stored in the database.
4dc44fc5 109 $pending_update = false;
2be4d090 110
f982303d 111 // Loop through course criteria.
4dc44fc5 112 foreach ($completions as $completion) {
4dc44fc5
JF
113 $criteria = $completion->get_criteria();
114 $complete = $completion->is_complete();
2be4d090 115
4dc44fc5
JF
116 if (!$pending_update && $criteria->is_pending($completion)) {
117 $pending_update = true;
118 }
2be4d090 119
f982303d 120 // Activities are a special case, so cache them and leave them till last.
4dc44fc5
JF
121 if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
122 $activities[$criteria->moduleinstance] = $complete;
2be4d090 123
4dc44fc5
JF
124 if ($complete) {
125 $activities_complete++;
126 }
2be4d090 127
4dc44fc5 128 continue;
2be4d090
MD
129 }
130
f982303d 131 // Prerequisites are also a special case, so cache them and leave them till last.
4dc44fc5
JF
132 if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) {
133 $prerequisites[$criteria->courseinstance] = $complete;
2be4d090 134
4dc44fc5
JF
135 if ($complete) {
136 $prerequisites_complete++;
137 }
2be4d090 138
4dc44fc5 139 continue;
2be4d090 140 }
f982303d
JF
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;';
145 $srows[] = $row;
2be4d090
MD
146 }
147
f982303d 148 // Aggregate activities.
4dc44fc5 149 if (!empty($activities)) {
4dc44fc5
JF
150 $a = new stdClass();
151 $a->first = $activities_complete;
152 $a->second = count($activities);
f982303d
JF
153
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;';
158 $srows[] = $row;
4dc44fc5 159 }
2be4d090 160
f982303d 161 // Aggregate prerequisites.
4dc44fc5 162 if (!empty($prerequisites)) {
4dc44fc5
JF
163 $a = new stdClass();
164 $a->first = $prerequisites_complete;
165 $a->second = count($prerequisites);
2be4d090 166
f982303d
JF
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;';
171 $prows[] = $row;
172
173 $srows = array_merge($prows, $srows);
4dc44fc5 174 }
2be4d090 175
f982303d
JF
176 // Display completion status.
177 $table = new html_table();
178 $table->width = '100%';
179 $table->attributes = array('style'=>'font-size: 90%;', 'class'=>'');
180
181 $row = new html_table_row();
182 $content = html_writer::tag('b', get_string('status').': ');
4dc44fc5
JF
183
184 // Is course complete?
185 $coursecomplete = $info->is_course_complete($USER->id);
186
f982303d 187 // Load course completion.
4dc44fc5
JF
188 $params = array(
189 'userid' => $USER->id,
190 'course' => $course->id
191 );
192 $ccompletion = new completion_completion($params);
193
194 // Has this user completed any criteria?
195 $criteriacomplete = $info->count_course_user_data($USER->id);
196
197 if ($pending_update) {
f982303d 198 $content .= html_writer::tag('i', get_string('pending', 'completion'));
4dc44fc5 199 } else if ($coursecomplete) {
f982303d 200 $content .= get_string('complete');
4dc44fc5 201 } else if (!$criteriacomplete && !$ccompletion->timestarted) {
f982303d 202 $content .= html_writer::tag('i', get_string('notyetstarted', 'completion'));
4dc44fc5 203 } else {
f982303d 204 $content .= html_writer::tag('i', get_string('inprogress', 'completion'));
4dc44fc5 205 }
2be4d090 206
f982303d
JF
207 $row->cells[0] = new html_table_cell($content);
208 $row->cells[0]->colspan = '2';
2be4d090 209
f982303d
JF
210 $rows[] = $row;
211 $row = new html_table_row();
212 $content = "";
213 // Get overall aggregation method.
4dc44fc5 214 $overall = $info->get_aggregation_method();
4dc44fc5 215 if ($overall == COMPLETION_AGGREGATION_ALL) {
f982303d 216 $content .= get_string('criteriarequiredall', 'completion');
4dc44fc5 217 } else {
f982303d 218 $content .= get_string('criteriarequiredany', 'completion');
4dc44fc5 219 }
f982303d
JF
220 $content .= ':';
221 $row->cells[0] = new html_table_cell($content);
222 $row->cells[0]->colspan = '2';
223 $rows[] = $row;
224
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;';
229 $rows[] = $row;
4dc44fc5 230
f982303d
JF
231 // Array merge $rows and $data here.
232 $rows = array_merge($rows, $srows);
2be4d090 233
f982303d 234 $table->data = $rows;
525d6ce3 235 $this->content->text .= html_writer::table($table);
f982303d
JF
236
237 // Display link to detailed view.
4dc44fc5 238 $details = new moodle_url('/blocks/completionstatus/details.php', array('course' => $course->id));
525d6ce3 239 $this->content->footer .= html_writer::link($details, get_string('moredetails', 'completion'));
2be4d090 240 } else {
f982303d 241 // If user is not enrolled, show error.
10a8581f 242 $this->content->text = get_string('nottracked', 'completion');
2be4d090
MD
243 }
244
4dc44fc5
JF
245 if (has_capability('report/completion:view', $context)) {
246 $report = new moodle_url('/report/completion/index.php', array('course' => $course->id));
40d23016
AD
247 if (empty($this->content->footer)) {
248 $this->content->footer = '';
249 }
f982303d 250 $this->content->footer .= html_writer::empty_tag('br');
10c56dc1 251 $this->content->footer .= html_writer::link($report, get_string('viewcoursereport', 'completion'));
4dc44fc5 252 }
2be4d090 253
2be4d090
MD
254 return $this->content;
255 }
256}