forked from brickfield/moodle-block_accessibility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_accessibility.php
executable file
·384 lines (309 loc) · 12.5 KB
/
block_accessibility.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?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/>.
/**
* Define the accessibility block's class
*
* @package block_accessibility
* @author Mark Johnson <[email protected]>
* @copyright 2010 Tauntons College, UK
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/blocks/accessibility/lib.php');
/**
* accessibility Block's class
*/
class block_accessibility extends block_base {
CONST JS_URL = '/blocks/accessibility/module.js';
CONST CSS_URL = '/blocks/accessibility/userstyles.php';
CONST FONTSIZE_URL = '/blocks/accessibility/changesize.php';
CONST COLOUR_URL = '/blocks/accessibility/changecolour.php';
CONST DB_URL = '/blocks/accessibility/database.php';
//CONST LOADER_ICON = '/blocks/accessibility/pix/loader.gif'; // not in use anymore
/**
* Set the title and include the stylesheet
*
* We need to include the stylesheet here rather than in {@see get_content()} since get_content
* is sometimes called after $OUTPUT->heading(), e.g. such as /user/index.php where the middle
* region is hard-coded.
* However, /admin/plugins.php calls init() for each plugin after $OUTPUT->heading(), so the
* sheet is not included at all on that page.
*/
public function init() {
$this->title = get_string('pluginname', 'block_accessibility');
}
/**
* Called after init(). Here we have instance id so we can use config for specific instance
* The function will include CSS declarations into Moodle Page
* CSS declarations will be generated according to user settings and instance configuration
*
*/
public function specialization(){
$instance_id = $this->instance->id;
if (!$this->page->requires->is_head_done()){
// link default/saved settings to a page
// each block instance has it's own configuration form, so we need instance id
$cssurl = self::CSS_URL.'?instance_id='.$instance_id;
$this->page->requires->css($cssurl);
}
}
/**
* instance_allow_multiple explicitly tells there cannot be multiple
* block instance on the same page
*
*/
public function instance_allow_multiple(){
return FALSE;
}
/**
* Set where the block should be allowed to be added
*
* @return array
*/
public function applicable_formats() {
return array('all' => true);
}
/**
* Generate the contents for the block
*
* @return object Block contents and footer
*/
public function get_content () {
global $CFG;
global $USER;
global $FULLME;
global $DB;
// Until Issue #63 is fixed, we don't want to display block for unauthenticated users
if(!isloggedin()) return null;
if ($this->content !== null) {
return $this->content;
}
/* this is not implemented yet
// autosave block's config option
$autosave = DEFAULT_AUTOSAVE; // by default
if(isset($this->config->autosave)) $autosave = $this->config->autosave;
*/
// get the current page url (redirection after action when no Javascript)
$params = array('redirect' => $FULLME);
// set block services paths: changesize.php, changecolour.php and database.php
$size_url = new moodle_url(self::FONTSIZE_URL, $params);
$colour_url = new moodle_url(self::COLOUR_URL, $params);
$params['op'] = 'save';
$params['size'] = true;
$params['scheme'] = true;
$db_url = new moodle_url(self::DB_URL, $params);
// INITIALIZE BUTTON ATTRIBUTES
// ===============================================
// initialization of increase_font, decrease_font and save button
$inc_attrs = array(
'title' => get_string('inctext', 'block_accessibility'),
'id' => "block_accessibility_inc",
'href' => $size_url->out(false, array('op' => 'inc'))
);
$dec_attrs = array(
'title' => get_string('dectext', 'block_accessibility'),
'id' => "block_accessibility_dec",
'href' => $size_url->out(false, array('op' => 'dec'))
);
$save_attrs = array(
'title' => get_string('save', 'block_accessibility'),
'id' => "block_accessibility_save",
'href' => $db_url->out(false)
);
// initialization of reset button
$reset_attrs = array(
'id' => 'block_accessibility_reset',
'title' => get_string('resettext', 'block_accessibility'),
'href' => $size_url->out(false, array('op' => 'reset'))
);
// if any of increase/decrease buttons reached maximum size, disable it
if (isset($USER->fontsize)) {
if ($USER->fontsize == MIN_FONTSIZE) {
$dec_attrs['class'] = 'disabled';
unset($dec_attrs['href']);
}
if ($USER->fontsize == MAX_FONTSIZE) {
$inc_attrs['class'] = 'disabled';
unset($inc_attrs['href']);
}
}
// or disable reset button
else{
$reset_attrs['class'] = 'disabled';
}
// if user is not logged in, disable save button
// UPDATE 18.9.2014. Anyway the block will not be displayed for nonauthenticated users
/*
if (isset($USER->username) && (isset($USER->fontsize) || isset($USER->colourscheme))) {
$save_attrs['href'] = $db_url->out(false);
} else {
$save_attrs['class'] = 'disabled';
}
*/
// initialization of scheme profiles buttons
$c1_attrs = array(
'id' => 'block_accessibility_colour1',
'href' => $colour_url->out(false, array('scheme' => 1))
);
$c2_attrs = array(
'id' => 'block_accessibility_colour2',
'href' => $colour_url->out(false, array('scheme' => 2))
);
$c3_attrs = array(
'id' => 'block_accessibility_colour3',
'href' => $colour_url->out(false, array('scheme' => 3))
);
$c4_attrs = array(
'id' => 'block_accessibility_colour4',
'href' => $colour_url->out(false, array('scheme' => 4))
);
if (!isset($USER->colourscheme)) {
$c1_attrs['class'] = 'disabled';
}
// display:inline-block CSS declaration is not applied to block's buttons because IE7 doesn't support it. float is used insted for IE7 only
$clearfix = '';
if(preg_match('/(?i)msie [1-7]/',$_SERVER['HTTP_USER_AGENT'])){
$clearfix = html_writer::tag('div', '', array('style'=>'clear:both')); // required for IE7
}
// RENDER BLOCK HTML
// ===============================================
$content = '';
$strchar = get_string('char', 'block_accessibility');
$resetchar = "R";
$divattrs = array('id' => 'accessibility_controls', 'class' => 'content');
$listattrs = array('id' => 'block_accessibility_textresize', 'class' => 'button_row');
$content .= html_writer::start_tag('div', $divattrs);
$content .= html_writer::start_tag('ul', $listattrs);
$content .= html_writer::start_tag('li', array('class' => 'access-button'));
$content .= html_writer::tag('a', $strchar.'-', $dec_attrs);
$content .= html_writer::end_tag('li');
$content .= html_writer::start_tag('li', array('class' => 'access-button'));
$content .= html_writer::tag('a', $strchar, $reset_attrs);
$content .= html_writer::end_tag('li');
$content .= html_writer::start_tag('li', array('class' => 'access-button'));
$content .= html_writer::tag('a', $strchar.'+', $inc_attrs);
$content .= html_writer::end_tag('li');
$content .= html_writer::start_tag('li', array('class' => 'access-button'));
$content .= html_writer::tag('a', ' ', $save_attrs);
$content .= html_writer::end_tag('li');
$content .= html_writer::end_tag('ul');
$content .= $clearfix;
// Colour change buttons
$content .= html_writer::start_tag('ul', array('id' => 'block_accessibility_changecolour'));
$content .= html_writer::start_tag('li', array('class' => 'access-button'));
$content .= html_writer::tag('a', $resetchar, $c1_attrs);
$content .= html_writer::end_tag('li');
$content .= html_writer::start_tag('li', array('class' => 'access-button'));
$content .= html_writer::tag('a', $strchar, $c2_attrs);
$content .= html_writer::end_tag('li');
$content .= html_writer::start_tag('li', array('class' => 'access-button'));
$content .= html_writer::tag('a', $strchar, $c3_attrs);
$content .= html_writer::end_tag('li');
$content .= html_writer::start_tag('li', array('class' => 'access-button'));
$content .= html_writer::tag('a', $strchar, $c4_attrs);
$content .= html_writer::end_tag('li');
$content .= html_writer::end_tag('ul');
$content .= $clearfix;
// e.g. "settings saved" or etc.
if (isset($USER->accessabilitymsg)) {
$message = $USER->accessabilitymsg;
unset($USER->accessabilitymsg);
} else {
$message = '';
}
$messageattrs = array('id' => 'block_accessibility_message', 'class' => 'clearfix');
$content .= html_writer::tag('div', $message, $messageattrs);
// data to pass to module.js
$jsdata['autoload_atbar'] = false;
$jsdata['instance_id'] = $this->instance->id;
// RENDER ATBAR
// ===============================================
$showATbar = DEFAULT_SHOWATBAR; // by default
if(isset($this->config->showATbar)) $showATbar = $this->config->showATbar;
if($showATbar){
// load database record for a current user
$options = $DB->get_record('block_accessibility', array('userid' => $USER->id));
// initialize ATBar
$checkbox_attrs = array(
'type' => 'checkbox',
'value' => 1,
'id' => 'atbar_auto',
'name' => 'atbar_auto',
'class' => 'atbar_control',
);
$label_attrs = array(
'for' => 'atbar_auto',
'class' => 'atbar_control'
);
if ($options && $options->autoload_atbar) {
$checkbox_attrs['checked'] = 'checked';
$jsdata['autoload_atbar'] = true;
}
// ATbar launch button (if javascript is enabled)
$launch_attrs = array(
'type' => 'button',
'value' => get_string('launchtoolbar', 'block_accessibility'),
'id' => 'block_accessibility_launchtoolbar',
'class' => 'atbar_control access-button'
);
// render ATBar
$content .= html_writer::empty_tag('input', $launch_attrs);
$spanattrs = array('class' => 'atbar-always');
$content .= html_writer::start_tag('span', $spanattrs);
$content .= html_writer::empty_tag('input', $checkbox_attrs);
$strlaunch = get_string('autolaunch', 'block_accessibility');
$content .= html_writer::tag('label', $strlaunch, $label_attrs);
$content .= html_writer::end_tag('span');
}
$content .= html_writer::end_tag('div');
// loader icon
// html_writer::empty_tag('img', array('src' => new moodle_url(self::LOADER_ICON), 'alt' => 'loader icon')); // it doesn't work so it's implemented by CSS
$spanattrs = array('id' => 'loader-icon');
$content .= html_writer::start_tag('span', $spanattrs);
$content .= html_writer::end_tag('span');
$content .= $clearfix;
// SET THE BLOCK CONTENT
// ===============================================
$this->content = new stdClass;
$this->content->footer = '';
$this->content->text = $content;
// keep in mind that dynamic AJAX mode cannot work properly with IE <= 8 (for now), so javascript will not even be loaded
if(!preg_match('/(?i)msie [1-8]/',$_SERVER['HTTP_USER_AGENT']))
{
// INCLUDE JS AND PASS PARAMETERS
// ===============================================
// language strings to pass to module.js
$this->page->requires->string_for_js('saved', 'block_accessibility');
$this->page->requires->string_for_js('jsnosave', 'block_accessibility');
$this->page->requires->string_for_js('reset', 'block_accessibility');
$this->page->requires->string_for_js('jsnosizereset', 'block_accessibility');
$this->page->requires->string_for_js('jsnocolourreset', 'block_accessibility');
$this->page->requires->string_for_js('jsnosize', 'block_accessibility');
$this->page->requires->string_for_js('jsnocolour', 'block_accessibility');
$this->page->requires->string_for_js('jsnosizereset', 'block_accessibility');
$this->page->requires->string_for_js('jsnotloggedin', 'block_accessibility');
$this->page->requires->string_for_js('launchtoolbar', 'block_accessibility');
$jsmodule = array(
'name' => 'block_accessibility',
'fullpath' => self::JS_URL,
'requires' => array('base', 'node', 'stylesheet')
);
// include js script and pass the arguments
$this->page->requires->js_init_call('M.block_accessibility.init', $jsdata, false, $jsmodule);
}
return $this->content;
}
}