forked from FMCorz/moodle-block_xp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_xp.php
193 lines (169 loc) · 5.92 KB
/
block_xp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Block XP.
*
* @package block_xp
* @copyright 2014 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Block XP class.
*
* @package block_xp
* @copyright 2014 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_xp extends block_base {
/**
* Applicable formats.
*
* @return array
*/
public function applicable_formats() {
global $CFG;
if (isset($CFG->block_xp_context) && $CFG->block_xp_context == CONTEXT_SYSTEM) {
return array('site' => true, 'course' => true, 'my' => true);
}
return array('course' => true);
}
/**
* Cron.
*
* @return void
*/
public function cron() {
global $DB;
$courseids = $DB->get_fieldset_sql('SELECT DISTINCT(courseid) FROM {block_xp}', array());
foreach ($courseids as $courseid) {
$manager = block_xp_manager::get($courseid);
$manager->purge_log();
}
return true;
}
/**
* The plugin has a settings.php file.
*
* @return boolean True.
*/
public function has_config() {
return true;
}
/**
* Init.
*
* @return void
*/
public function init() {
$this->title = get_string('pluginname', 'block_xp');
}
/**
* Callback when a block is created.
*
* @return bool
*/
public function instance_create() {
// Enable the capture of events for that course.
$manager = block_xp_manager::get($this->page->course->id);
$manager->update_config((object) array('enabled' => true));
return true;
}
/**
* Callback when a block is deleted.
*
* @return bool
*/
public function instance_delete() {
// It's bad, but here we assume there is only one block per course.
$manager = block_xp_manager::get($this->page->course->id);
$manager->update_config((object) array('enabled' => false));
return true;
}
/**
* Get content.
*
* @return stdClass
*/
public function get_content() {
global $DB, $PAGE, $USER;
if (isset($this->content)) {
return $this->content;
}
$this->content = new stdClass();
$this->content->text = '';
$this->content->footer = '';
$manager = block_xp_manager::get($this->page->course->id);
$canview = $manager->can_view();
$canedit = $manager->can_manage();
// Hide the block to non-logged in users, guests and those who cannot view the block.
if (!$USER->id || isguestuser() || !$canview) {
return $this->content;
}
$progress = $manager->get_progress_for_user($USER->id);
$renderer = $this->page->get_renderer('block_xp');
$currentlevelmethod = $manager->get_config('enablecustomlevelbadges') ? 'custom_current_level' : 'current_level';
$this->content->text = $renderer->$currentlevelmethod($progress);
$this->content->text .= $renderer->progress_bar($progress);
if (isset($this->config->description)) {
$this->content->text .= $renderer->description($this->config->description);
} else {
$this->content->text .= $renderer->description(get_string('participatetolevelup', 'block_xp'));
}
$this->content->footer .= $renderer->student_links($manager->get_courseid(),
$manager->can_view_ladder_page(), $manager->can_view_infos_page());
if ($canedit) {
$this->content->footer .= $renderer->admin_links($manager->get_courseid());
if (!$manager->get_config('enabled')) {
$this->content->footer .= html_writer::tag('p',
html_writer::tag('small', get_string('xpgaindisabled', 'block_xp')), array('class' => 'alert alert-warning'));
}
}
// We should be congratulating the user because they leveled up!
// Also resets the flag. We could potentially do that from JS so that if the user does not
// stay on the page long enough they'd be notified the next time they access the course page,
// but that's probably an overkill for now.
if ($manager->has_levelled_up($USER->id)) {
$args = array(
'badge' => $renderer->$currentlevelmethod($progress),
'headline' => get_string('youreachedlevela', 'block_xp', $progress->level),
'level' => $progress->level,
);
$PAGE->requires->yui_module('moodle-block_xp-notification', 'Y.M.block_xp.Notification.init', array($args));
$PAGE->requires->strings_for_js(
array(
'coolthanks',
'congratulationsyouleveledup',
),
'block_xp'
);
}
return $this->content;
}
/**
* Specialization.
*
* Happens right after the initialisation is complete.
*
* @return void
*/
public function specialization() {
parent::specialization();
if (!empty($this->config->title)) {
$this->title = $this->config->title;
}
}
}