MDL-37473 completion: Add missing cap checks to tracked users functions
[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() {
bfe20414
MN
39 $this->title = get_string('pluginname', 'block_selfcompletion');
40 }
41
42 function applicable_formats() {
43 return array('all' => true, 'mod' => false, 'tag' => false, 'my' => false);
2be4d090
MD
44 }
45
46 public function get_content() {
0e5e1daa 47 global $CFG, $USER;
2be4d090
MD
48
49 // If content is cached
50 if ($this->content !== NULL) {
51 return $this->content;
52 }
53
2be4d090
MD
54 // Create empty content
55 $this->content = new stdClass;
56
0e5e1daa 57 // Can edit settings?
fe2fdd11 58 $can_edit = has_capability('moodle/course:update', context_course::instance($this->page->course->id));
2be4d090
MD
59
60 // Get course completion data
61 $info = new completion_info($this->page->course);
2be4d090 62
0e5e1daa
AB
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;
69
70 } else if (!$info->is_enabled()) {
71 if ($can_edit) {
72 $this->content->text = get_string('completionnotenabledforcourse', 'completion');
73 }
2be4d090
MD
74 return $this->content;
75 }
76
0e5e1daa
AB
77 // Get this user's data
78 $completion = $info->get_completion($USER->id, COMPLETION_CRITERIA_TYPE_SELF);
79
2be4d090
MD
80 // Check if self completion is one of this course's criteria
81 if (empty($completion)) {
0e5e1daa
AB
82 if ($can_edit) {
83 $this->content->text = get_string('selfcompletionnotenabled', 'block_selfcompletion');
84 }
2be4d090
MD
85 return $this->content;
86 }
87
88 // Check this user is enroled
24a3b341 89 if (!$info->is_tracked_user($USER->id)) {
10a8581f 90 $this->content->text = get_string('nottracked', 'completion');
2be4d090
MD
91 return $this->content;
92 }
93
0e5e1daa
AB
94 // Is course complete?
95 if ($info->is_course_complete($USER->id)) {
96 $this->content->text = get_string('coursealreadycompleted', 'completion');
97 return $this->content;
98
2be4d090 99 // Check if the user has already marked themselves as complete
0e5e1daa
AB
100 } else if ($completion->is_complete()) {
101 $this->content->text = get_string('alreadyselfcompleted', 'block_selfcompletion');
2be4d090 102 return $this->content;
0e5e1daa
AB
103
104 // If user is not complete, or has not yet self completed
2be4d090
MD
105 } else {
106 $this->content->text = '';
0e5e1daa
AB
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>...';
2be4d090
MD
109 }
110
111 return $this->content;
112 }
113}