9ea6db40cf3ae42c271dc285ecac9a17ee5154e0
[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('pluginname', 'block_selfcompletion');
40     }
42     function applicable_formats() {
43         return array('all' => true, 'mod' => false, 'tag' => false, 'my' => false);
44     }
46     public function get_content() {
47         global $CFG, $USER;
49         // If content is cached
50         if ($this->content !== NULL) {
51           return $this->content;
52         }
54         // Create empty content
55         $this->content = new stdClass;
57         // Can edit settings?
58         $can_edit = has_capability('moodle/course:update', context_course::instance($this->page->course->id));
60         // Get course completion data
61         $info = new completion_info($this->page->course);
63         // Don't display if completion isn't enabled!
64         if (!completion_info::is_enabled_for_site()) {
65             if ($can_edit) {
66                 $this->content->text = get_string('completionnotenabledforsite', 'completion');
67             }
68             return $this->content;
70         } else if (!$info->is_enabled()) {
71             if ($can_edit) {
72                 $this->content->text = get_string('completionnotenabledforcourse', 'completion');
73             }
74             return $this->content;
75         }
77         // Get this user's data
78         $completion = $info->get_completion($USER->id, COMPLETION_CRITERIA_TYPE_SELF);
80         // Check if self completion is one of this course's criteria
81         if (empty($completion)) {
82             if ($can_edit) {
83                 $this->content->text = get_string('selfcompletionnotenabled', 'block_selfcompletion');
84             }
85             return $this->content;
86         }
88         // Check this user is enroled
89         if (!$info->is_tracked_user($USER->id)) {
90             $this->content->text = get_string('notenroled', 'completion');
91             return $this->content;
92         }
94         // Is course complete?
95         if ($info->is_course_complete($USER->id)) {
96             $this->content->text = get_string('coursealreadycompleted', 'completion');
97             return $this->content;
99         // Check if the user has already marked themselves as complete
100         } else if ($completion->is_complete()) {
101             $this->content->text = get_string('alreadyselfcompleted', 'block_selfcompletion');
102             return $this->content;
104         // If user is not complete, or has not yet self completed
105         } else {
106             $this->content->text = '';
107             $this->content->footer = '<br /><a href="'.$CFG->wwwroot.'/course/togglecompletion.php?course='.$this->page->course->id.'">';
108             $this->content->footer .= get_string('completecourse', 'block_selfcompletion').'</a>...';
109         }
111         return $this->content;
112     }