MDL-26190 completion blocks: Show messages rather than hiding the block
[moodle.git] / blocks / selfcompletion / block_selfcompletion.php
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 //                                                                       //
5 // NOTICE OF COPYRIGHT                                                   //
6 //                                                                       //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
8 //          http://moodle.com                                            //
9 //                                                                       //
10 // Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
11 //                                                                       //
12 // This program is free software; you can redistribute it and/or modify  //
13 // it under the terms of the GNU General Public License as published by  //
14 // the Free Software Foundation; either version 2 of the License, or     //
15 // (at your option) any later version.                                   //
16 //                                                                       //
17 // This program is distributed in the hope that it will be useful,       //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
20 // GNU General Public License for more details:                          //
21 //                                                                       //
22 //          http://www.gnu.org/copyleft/gpl.html                         //
23 //                                                                       //
24 ///////////////////////////////////////////////////////////////////////////
26 require_once($CFG->libdir.'/completionlib.php');
28 /**
29  * Self course completion marking
30  * Let's a user manually complete a course
31  *
32  * Will only display if the course has completion enabled,
33  * there is a self completion criteria, and the logged in user is yet
34  * to complete the course.
35  */
36 class block_selfcompletion extends block_base {
38     public function init() {
39         $this->title   = get_string('selfcompletion', 'block_selfcompletion');
40     }
42     public function get_content() {
43         global $USER;
45         // If content is cached
46         if ($this->content !== NULL) {
47           return $this->content;
48         }
50         global $CFG;
52         // Create empty content
53         $this->content = new stdClass;
55         // Don't display if completion isn't enabled!
56         if (!$this->page->course->enablecompletion) {
57             $this->content->text = get_string('completionnotenabled', 'block_selfcompletion');
58             return $this->content;
59         }
61         // Get course completion data
62         $info = new completion_info($this->page->course);
63         $completion = $info->get_completion($USER->id, COMPLETION_CRITERIA_TYPE_SELF);
65         // Is course complete?
66         if ($info->is_course_complete($USER->id)) {
67             return $this->content;
68         }
70         // Check if self completion is one of this course's criteria
71         if (empty($completion)) {
72             $this->content->text = get_string('selfcompletionnotenabled', 'block_selfcompletion');
73             return $this->content;
74         }
76         // Check this user is enroled
77         if (!$info->is_tracked_user($USER->id)) {
78             $this->content->text = get_string('notenroled', 'completion');
79             return $this->content;
80         }
82         // Check if the user has already marked themselves as complete
83         if ($completion->is_complete()) {
84             return $this->content;
85         } else {
86             $this->content->text = '';
87             $this->content->footer = '<br /><a href="'.$CFG->wwwroot.'/course/togglecompletion.php?course='.$this->page->course->id.'">'.
88                                        get_string('completecourse', 'block_selfcompletion').'</a>...';
89         }
91         return $this->content;
92     }
93 }