MDL-65788 tool_policy: Prevent two modals rendering for guests.
[moodle.git] / blocks / completionstatus / block_completionstatus.php
1 <?php
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/>.
17 /**
18  * Block for displayed logged in user's course completion status
19  *
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
24  */
26 defined('MOODLE_INTERNAL') || die();
28 require_once("{$CFG->libdir}/completionlib.php");
30 /**
31  * Course completion status.
32  * Displays overall, and individual criteria status for logged in user.
33  */
34 class block_completionstatus extends block_base {
36     public function init() {
37         $this->title = get_string('pluginname', 'block_completionstatus');
38     }
40     public function applicable_formats() {
41         return array('course' => true);
42     }
44     public function get_content() {
45         global $USER;
47         $rows = array();
48         $srows = array();
49         $prows = array();
50         // If content is cached.
51         if ($this->content !== null) {
52             return $this->content;
53         }
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 = '';
63         // Can edit settings?
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()) {
71             if ($can_edit) {
72                 $this->content->text .= get_string('completionnotenabledforsite', 'completion');
73             }
74             return $this->content;
76         } else if (!$info->is_enabled()) {
77             if ($can_edit) {
78                 $this->content->text .= get_string('completionnotenabledforcourse', 'completion');
79             }
80             return $this->content;
81         }
83         // Load criteria to display.
84         $completions = $info->get_completions($USER->id);
86         // Check if this course has any criteria.
87         if (empty($completions)) {
88             if ($can_edit) {
89                 $this->content->text .= get_string('nocriteriaset', 'completion');
90             }
91             return $this->content;
92         }
94         // Check this user is enroled.
95         if ($info->is_tracked_user($USER->id)) {
97             // Generate markup for criteria statuses.
98             $data = '';
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;
118                 }
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;
124                     if ($complete) {
125                         $activities_complete++;
126                     }
128                     continue;
129                 }
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;
135                     if ($complete) {
136                         $prerequisites_complete++;
137                     }
139                     continue;
140                 }
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;
146             }
148             // Aggregate activities.
149             if (!empty($activities)) {
150                 $a = new stdClass();
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;';
158                 $srows[] = $row;
159             }
161             // Aggregate prerequisites.
162             if (!empty($prerequisites)) {
163                 $a = new stdClass();
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;';
171                 $prows[] = $row;
173                 $srows = array_merge($prows, $srows);
174             }
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.
188             $params = array(
189                 'userid' => $USER->id,
190                 'course' => $course->id
191             );
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'));
203             } else {
204                 $content .= html_writer::tag('i', get_string('inprogress', 'completion'));
205             }
207             $row->cells[0] = new html_table_cell($content);
208             $row->cells[0]->colspan = '2';
210             $rows[] = $row;
211             $row = new html_table_row();
212             $content = "";
213             // Get overall aggregation method.
214             $overall = $info->get_aggregation_method();
215             if ($overall == COMPLETION_AGGREGATION_ALL) {
216                 $content .= get_string('criteriarequiredall', 'completion');
217             } else {
218                 $content .= get_string('criteriarequiredany', 'completion');
219             }
220             $content .= ':';
221             $row->cells[0] = new html_table_cell($content);
222             $row->cells[0]->colspan = '2';
223             $rows[] = $row;
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;
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'));
240         } else {
241             // If user is not enrolled, show error.
242             $this->content->text = get_string('nottracked', 'completion');
243         }
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 = '';
249             }
250             $this->content->footer .= html_writer::empty_tag('br');
251             $this->content->footer .= html_writer::link($report, get_string('viewcoursereport', 'completion'));
252         }
254         return $this->content;
255     }