MDL-26190 completion blocks: Show messages rather than hiding the block
[moodle.git] / blocks / selfcompletion / block_selfcompletion.php
CommitLineData
2be4d090
MD
1<?php
2
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///////////////////////////////////////////////////////////////////////////
25
26require_once($CFG->libdir.'/completionlib.php');
27
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 */
36class block_selfcompletion extends block_base {
37
38 public function init() {
39 $this->title = get_string('selfcompletion', 'block_selfcompletion');
2be4d090
MD
40 }
41
42 public function get_content() {
43 global $USER;
44
45 // If content is cached
46 if ($this->content !== NULL) {
47 return $this->content;
48 }
49
50 global $CFG;
51
52 // Create empty content
53 $this->content = new stdClass;
54
55 // Don't display if completion isn't enabled!
56 if (!$this->page->course->enablecompletion) {
0b15b43b 57 $this->content->text = get_string('completionnotenabled', 'block_selfcompletion');
2be4d090
MD
58 return $this->content;
59 }
60
61 // Get course completion data
62 $info = new completion_info($this->page->course);
63 $completion = $info->get_completion($USER->id, COMPLETION_CRITERIA_TYPE_SELF);
64
65 // Is course complete?
66 if ($info->is_course_complete($USER->id)) {
67 return $this->content;
68 }
69
70 // Check if self completion is one of this course's criteria
71 if (empty($completion)) {
0b15b43b 72 $this->content->text = get_string('selfcompletionnotenabled', 'block_selfcompletion');
2be4d090
MD
73 return $this->content;
74 }
75
76 // Check this user is enroled
24a3b341 77 if (!$info->is_tracked_user($USER->id)) {
2be4d090
MD
78 $this->content->text = get_string('notenroled', 'completion');
79 return $this->content;
80 }
81
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 }
90
91 return $this->content;
92 }
93}