MDL-34476 - Course Completeion, Blocks - Allowing teachers to access the Full Course...
[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
21  * @subpackage completion
22  * @copyright  2009-2012 Catalyst IT Ltd
23  * @author     Aaron Barnes <aaronb@catalyst.net.nz>
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
29 require_once("{$CFG->libdir}/completionlib.php");
31 /**
32  * Course completion status
33  * Displays overall, and individual criteria status for logged in user
34  */
35 class block_completionstatus extends block_base {
37     public function init() {
38         $this->title = get_string('pluginname', 'block_completionstatus');
39     }
41     public function get_content() {
42         global $USER;
44         // If content is cached
45         if ($this->content !== NULL) {
46             return $this->content;
47         }
49         $course  = $this->page->course;
51         // Create empty content
52         $this->content = new stdClass();
54         // Can edit settings?
55         $can_edit = has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id));
57         // Get course completion data
58         $info = new completion_info($course);
60         // Don't display if completion isn't enabled!
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;
67         } else if (!$info->is_enabled()) {
68             if ($can_edit) {
69                 $this->content->text = get_string('completionnotenabledforcourse', 'completion');
70             }
71             return $this->content;
72         }
74         // Load criteria to display
75         $completions = $info->get_completions($USER->id);
77         // Check if this course has any criteria
78         if (empty($completions)) {
79             if ($can_edit) {
80                 $this->content->text = get_string('nocriteriaset', 'completion');
81             }
82             return $this->content;
83         }
85         // Check this user is enroled
86         if ($info->is_tracked_user($USER->id)) {
88             // Generate markup for criteria statuses
89             $shtml = '';
91             // For aggregating activity completion
92             $activities = array();
93             $activities_complete = 0;
95             // For aggregating course prerequisites
96             $prerequisites = array();
97             $prerequisites_complete = 0;
99             // Flag to set if current completion data is inconsistent with
100             // what is stored in the database
101             $pending_update = false;
103             // Loop through course criteria
104             foreach ($completions as $completion) {
106                 $criteria = $completion->get_criteria();
107                 $complete = $completion->is_complete();
109                 if (!$pending_update && $criteria->is_pending($completion)) {
110                     $pending_update = true;
111                 }
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;
117                     if ($complete) {
118                         $activities_complete++;
119                     }
121                     continue;
122                 }
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;
128                     if ($complete) {
129                         $prerequisites_complete++;
130                     }
132                     continue;
133                 }
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>';
140             }
142             // Aggregate activities
143             if (!empty($activities)) {
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             }
155             // Aggregate prerequisites
156             if (!empty($prerequisites)) {
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>';
167                 $shtml = $phtml . $shtml;
168             }
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> ';
174             // Is course complete?
175             $coursecomplete = $info->is_course_complete($USER->id);
177             // Load course completion
178             $params = array(
179                 'userid' => $USER->id,
180                 'course' => $course->id
181             );
182             $ccompletion = new completion_completion($params);
184             // Has this user completed any criteria?
185             $criteriacomplete = $info->count_course_user_data($USER->id);
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             }
197             $this->content->text .= '</td></tr>';
198             $this->content->text .= '<tr><td colspan="2">';
200             // Get overall aggregation method
201             $overall = $info->get_aggregation_method();
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             }
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>';
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>';
216         } else {
217             // If user is not enrolled, show error
218             $this->content->text = get_string('notenroled', 'completion');
219         }
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         }
225         return $this->content;
226     }