Merge branch 'MDL-63297' of https://github.com/timhunt/moodle
[moodle.git] / blocks / selfcompletion / block_selfcompletion.php
blob4e4d2d7e5d894d81e1e92b365e7ced28d5f2f76b
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.
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 * Self completion block.
20 * @package block_selfcompletion
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once($CFG->libdir.'/completionlib.php');
27 /**
28 * Self course completion marking
29 * Let's a user manually complete a course
31 * Will only display if the course has completion enabled,
32 * there is a self completion criteria, and the logged in user is yet
33 * to complete the course.
35 class block_selfcompletion extends block_base {
37 public function init() {
38 $this->title = get_string('pluginname', 'block_selfcompletion');
41 function applicable_formats() {
42 return array('course' => true);
45 public function get_content() {
46 global $CFG, $USER;
48 // If content is cached
49 if ($this->content !== NULL) {
50 return $this->content;
53 // Create empty content
54 $this->content = new stdClass;
56 // Can edit settings?
57 $can_edit = has_capability('moodle/course:update', context_course::instance($this->page->course->id));
59 // Get course completion data
60 $info = new completion_info($this->page->course);
62 // Don't display if completion isn't enabled!
63 if (!completion_info::is_enabled_for_site()) {
64 if ($can_edit) {
65 $this->content->text = get_string('completionnotenabledforsite', 'completion');
67 return $this->content;
69 } else if (!$info->is_enabled()) {
70 if ($can_edit) {
71 $this->content->text = get_string('completionnotenabledforcourse', 'completion');
73 return $this->content;
76 // Get this user's data
77 $completion = $info->get_completion($USER->id, COMPLETION_CRITERIA_TYPE_SELF);
79 // Check if self completion is one of this course's criteria
80 if (empty($completion)) {
81 if ($can_edit) {
82 $this->content->text = get_string('selfcompletionnotenabled', 'block_selfcompletion');
84 return $this->content;
87 // Check this user is enroled
88 if (!$info->is_tracked_user($USER->id)) {
89 $this->content->text = get_string('nottracked', 'completion');
90 return $this->content;
93 // Is course complete?
94 if ($info->is_course_complete($USER->id)) {
95 $this->content->text = get_string('coursealreadycompleted', 'completion');
96 return $this->content;
98 // Check if the user has already marked themselves as complete
99 } else if ($completion->is_complete()) {
100 $this->content->text = get_string('alreadyselfcompleted', 'block_selfcompletion');
101 return $this->content;
103 // If user is not complete, or has not yet self completed
104 } else {
105 $this->content->text = '';
106 $this->content->footer = '<br /><a href="'.$CFG->wwwroot.'/course/togglecompletion.php?course='.$this->page->course->id.'">';
107 $this->content->footer .= get_string('completecourse', 'block_selfcompletion').'</a>...';
110 return $this->content;