forked from praxisdigital/moodle-local_pages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.php
584 lines (534 loc) · 23.9 KB
/
renderer.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
<?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/>.
/**
* Local Pages Renderer
*
* @package local_pages
* @author Kevin Dibble
* @copyright 2017 LearningWorks Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/local/pages/forms/edit.php');
/**
*
* Class local_pages_renderer
*
* @copyright 2017 LearningWorks Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class local_pages_renderer extends plugin_renderer_base {
/**
* @var array
*/
public $errorfields = array();
/**
*
* Get the submenu item
*
* @param mixed $parent
* @param string $name
* @return string
*/
public function get_submenuitem($parent, $name) {
global $DB, $CFG, $USER;
$html = '';
$records = $DB->get_records_sql("SELECT * FROM {local_pages} WHERE deleted=0 AND " .
"pageparent=? ORDER BY pageorder", array($parent));
if ($records) {
$html .= "<li class='custompages-list-element'>";
$html .= '<div class="pages-action">' .
'<a href="' . new moodle_url($CFG->wwwroot . '/local/pages/',
array('id' => $parent)) . '" class="custompages-edit">' .
get_string('view', 'local_pages') .'</a> | ' .
'<a href="' . new moodle_url($CFG->wwwroot . '/local/pages/edit.php',
array('id' => $parent)) . '" class="custompages-edit">' .
get_string('edit', 'local_pages') . '</a> | ' .
'<a href="' . new moodle_url($CFG->wwwroot . '/local/pages/pages.php',
array('pagedel' => $parent, 'sesskey' => $USER->sesskey)) . '" class="custompages-delete">' .
get_string('delete', 'local_pages') .' </a></div>';
$html .= "<h4 class='custompages-title'>" . $name . "</h4>";
$html .= "<ul class='custompages_submenu'>";
foreach ($records as $page) {
$html .= $this->get_submenuitem($page->id, $page->pagename);
}
$html .= "</ul>";
$html .= "</li>";
} else {
$html .= "<li class='custompages-list-element'>";
$html .= '<div class="pages-action">' .
'<a href="' . new moodle_url($CFG->wwwroot . '/local/pages/',
array('id' => $parent)) . '" class="custompages-edit">' .
get_string('view', 'local_pages') .'</a> | ' .
'<a href="' . new moodle_url($CFG->wwwroot . '/local/pages/edit.php',
array('id' => $parent)) . '" class="custompages-edit">' .
get_string('edit', 'local_pages') .'</a> | ' .
'<a href="' . new moodle_url($CFG->wwwroot . '/local/pages/pages.php',
array('pagedel' => $parent, 'sesskey' => $USER->sesskey)) . '" class="custompages-delete">' .
get_string('delete', 'local_pages') .' </a></div>';
$html .= "<h4 class='custompages-title'>" . $name . "</h4>";
$html .= "</li>";
}
return $html;
}
/**
*
* List the pages for the user to view
*
* @return string
*/
public function list_pages() {
global $DB, $CFG;
$html = '<ul class="custompages-list">';
$records = $DB->get_records_sql("SELECT * FROM {local_pages} WHERE deleted=0 AND pageparent=0 ORDER BY pageorder");
foreach ($records as $page) {
$html .= $this->get_submenuitem($page->id, $page->pagename);
}
$html .= "<li class='custompages-list-element'>
<a href='" . new moodle_url($CFG->wwwroot . '/local/pages/edit.php') .
"' class='custompages-add'>Add Page</a>
</li>";
$html .= "<li class='custompages-list-element'>
<a target='_blank' href='" . new moodle_url($CFG->wwwroot .
'/local/pages/pages.pdf') . "' class='custompages-add'>PDF Manual</a>
</li>";
$html .= "</ul>";
return $html;
}
/**
*
* Show the page based on users rights
*
* @param mixed $page
* @return mixed
*/
public function showpage($page) {
global $DB;
if ($this->can_access_page($page) && ($page->pagedate <= date('U') || is_siteadmin())) {
$today = date('U');
$records = $DB->get_records_sql("SELECT * FROM {local_pages} WHERE deleted=0 AND pagetype <> 'page' " .
"AND pageparent=? AND pagedate <=? ORDER BY pageorder", array($page->id, $today));
$form = '';
foreach ($records as $key => $value) {
switch (strtolower($value->pagetype)) {
case 'form':
$form = $this->createform($value);
break;
}
}
// Format the input for XSS.
$page->pagecontent = format_text($this->adduserdata($page->pagecontent), FORMAT_HTML);
return str_replace(array("#form#", "{form}"), array($form, $form), $page->pagecontent);
} else {
return get_string('noaccess', 'local_pages');
}
}
/**
*
* Add user data to the form to display on the page
*
* @param mixed $data
* @return mixed
*/
public function adduserdata($data) {
global $USER, $DB;
if (isloggedin()) {
$usr = $USER;
} else {
$usr = $DB->get_record_sql('SELECT * FROM {user} WHERE id=1');
}
foreach ((array)$usr as $key => $details) {
if (!is_array($details) && !is_object($details)) {
$data = str_ireplace('{' . $key . '}', $details, $data);
}
}
return $data;
}
/**
*
* Create the form for the page
*
* @param mixed $data
* @return string
*/
public function createform($data) {
global $USER;
// Setup required parameters.
$honeypot = optional_param('hp', '', PARAM_RAW);
$formsubmit = optional_param('formsubmit', 0, PARAM_RAW);
if (isloggedin()) {
$USER->fullname = $USER->firstname . " " . $USER->lastname;
}
$records = json_decode($data->pagedata);
$valuesout = array();
$valuesin = array();
if ($formsubmit == 1 && $honeypot == '') {
if ($this->valid($records)) {
$cache = cache::make('local_pages', 'sent');
if (!$cache->get($data->pagename)) {
$this->processform($data);
if (get_config('local_pages', 'enable_limit') != 0) {
$cache->set($data->pagename, 'sent');
}
foreach ((array)$records as $key => $value) {
$valuesout[] = "{" . $value->name . "}";
// Get all data sent from the form.
$tmpparam = str_replace(" ", "_", $value->name);
$tmpparam = optional_param($tmpparam, '', PARAM_RAW);
$valuesin[] = $tmpparam;
}
return str_replace($valuesout, $valuesin, $data->pagecontent);
} else {
return get_string('cannnot_send', 'local_pages');
}
}
}
$str = '<form method="post" action="" class="mform">';
foreach ((array)$records as $key => $value) {
$errorclass = isset($this->error_fields[$value->name]) ? 'has-error' : '';
$record = $value->readsfrom;
$tmpparam = str_replace(' ', '', $value->name);
$tmpparam = optional_param($tmpparam, '', PARAM_RAW);
if ($value->type == "Text Area") {
$str .= '<div class="form-group fitem ' . $errorclass . '">';
$str .= '<div class="fitemtitle"><label for="' .
str_replace(" ", "", $value->name) . '">' . $value->name . '</label></div>';
$str .= '<div class="felement"><textarea class="form-control" name="' .
str_replace(" ", "_", $value->name) . '" id="' .
str_replace(" ", "", $value->name) . '" ' . ($value->required == "Yes" ? "Required" : '') .
' placeholder="' . $value->defaultvalue . '">' .
($tmpparam != '' ? $tmpparam : (isset($USER->$record) ? $USER->$record : ''))
. '</textarea></div></div>';
} else if (strtolower($value->type) == "checkbox") {
$str .= '<div class="checkbox ' . $errorclass . '">';
$str .= '<label for="' . str_replace(" ", "", $value->name) . '">';
$str .= '<input name="' . str_replace(" ", "_", $value->name) . '" type="hidden" value="0" id="' .
str_replace(" ", "", $value->name) . '" />';
$str .= '<input name="' . str_replace(" ", "_", $value->name) . '" type="' .
strtolower($value->type) . '" value="' . $tmpparam . '" id="' .
str_replace(" ", "", $value->name) . '" ' . ($value->required == "Yes" ? "Required" : '') .
' placeholder="' . $value->defaultvalue . '" />';
$str .= $value->name . '</label></div>';
} else {
if ($value->type == "HTML") {
$str .= '<div class="form-break">' . $value->name ."</div>";
} else if ($value->type == "Select") {
$str .= '<div class="form-group fitem fitem_fselect' . $errorclass . '">';
$str .= '<div class="fitemtitle"><label for="' . str_replace(" ", "", $value->name) . '">' .
$value->name . '</label></div>';
$str .= '<div class="felement fselect">'.
'<select class="form-control" ' . ($value->required == "Yes" ? "Required" : '') .
' name="' . str_replace(" ", "_", $value->name) . '" id="' .
str_replace(" ", "", $value->name) . '">';
$selectlist = explode("\r\n", $value->defaultvalue);
foreach ($selectlist as $option) {
$options = explode("|", $option);
if (trim($options[0]) == '' && !isset($options[1])) {
$options[1] == 'Please Select an option';
}
$str .= '<option value="' . $options[0] . '" ' .
($tmpparam == $options[0] ? 'selected="selected"' : '') . ' >' .
(isset($options[1]) ? $options[1] : $options[0]) . '</option>';
}
$str .= '</select></div></div>';
} else {
$str .= '<div class="form-group fitem fitem_ftext ' . $errorclass . '">';
$str .= '<div class="fitemtitle"><label for="' . str_replace(" ", "", $value->name) . '">' .
$value->name . '</label></div>';
$str .= '<div class="felement ftext"><input name="' . str_replace(" ", "_", $value->name) . '" type="' .
strtolower($value->type) . '" value="' .
($tmpparam != '' ? $tmpparam : (isset($USER->$record) ? $USER->$record : ''))
. '" placeholder="' . $value->defaultvalue .
'" class="form-control" id="' . str_replace(" ", "", $value->name) . '" ' .
($value->required == "Yes" ? "Required" : '') . ' /></div></div>';
}
}
}
if (isset($this->error_fields[$value->name])) {
$str .= '<span class="help-block">' . $this->error_fields[$value->name] . '</span>';
}
$str .= '<div class="fitem fitem_actionbuttons fitem_fgroup"><div class="felement fgroup">' .
'<input type="text" name="hp" value="" style="position:absolute;left:-99999px" /> ' .
'<button type="submit" name="formsubmit" value="1" class="btn btn-primary">Submit</button>' .
'</div></div></form>';
return $str;
}
/**
*
* Check if the form is valid
*
* @param mixed $records
* @return bool
*/
public function valid($records) {
$valid = true;
foreach ((array)$records as $key => $value) {
$tmpparam = str_replace(" ", "_", $value->name);
$tmpparam = optional_param($tmpparam, '', PARAM_RAW);
if ($value->required == "Yes" && $value->type != "HTML") {
if ($value->type == "Email" && (stripos($tmpparam, "@") === false ||
stripos($tmpparam, ".") === false)
) {
$this->error_fields[$value->name] = "Please Supply a valid email address for " . $value->name;
$valid = false;
}
if ($value->type != 'Email' && $tmpparam == '') {
$this->error_fields[$value->name] = "Please fill in " . $value->name;
$valid = false;
}
if ($value->type == 'Numeric' && !is_numeric($tmpparam)) {
$this->error_fields[$value->name] = "Please provide a number for " . $value->name;
$valid = false;
}
}
}
return $valid;
}
/**
*
* Process the submitted form up update page data
*
* @param mixed $page
*/
public function processform($page) {
global $DB;
$touser = get_admin();
$fromuser = clone $touser;
$touser->email = isset($page->emailto) && trim($page->emailto) ? $page->emailto : $touser->email;
$touser->emailstop = 0;
$messagetext = '';
$fields = array();
$records = json_decode($page->pagedata);
$outarray = array();
foreach ((array)$records as $key => $value) {
if ($value->type != "HTML") {
$outarray[] = "{" . $value->name . "}";
$tmpparam = str_replace(" ", "_", $value->name);
$tmpparam = optional_param($tmpparam, '', PARAM_RAW);
$fields[$value->name] = $tmpparam;
$messagetext .= ucfirst($value->name) . ": " . $tmpparam . "\r\n";
$field = strtolower(str_replace(" ", "", $value->name));
$fromuser->$field = $tmpparam;
}
}
$messagehtml = nl2br($messagetext);
$subject = $page->pagename;
$data = new stdClass();
$data->formdate = date('U');
$data->formcontent = json_encode($fields);
$data->formname = $page->id;
$DB->insert_record('local_pageslogging', $data);
email_to_user($touser, $fromuser, $subject, $messagetext, $messagehtml, '', '', true);
$outarray[] = "{table}";
$fields[] = $messagetext;
$messageforuser = str_replace($outarray, $fields, get_config('local_pages', 'message_copy'));
$messagetext = strip_tags(str_replace(array("</p>", "<br>", " "), array("</p>\r\n", "<br>\r\n", ""),
$messageforuser));
// Emails a copy to the user.
if (get_config('local_pages', 'user_copy') == 1) {
email_to_user($fromuser, $touser, $subject, $messagetext, $messageforuser, '', '', true);
}
}
/**
*
* Save the page to the database and redirect the user
*
* @param bool $page
*/
public function save_page($page = false) {
global $CFG;
$mform = new pages_edit_product_form($page);
if ($mform->is_cancelled()) {
redirect(new moodle_url($CFG->wwwroot . '/local/pages/pages.php'));
} else if ($data = $mform->get_data()) {
require_once($CFG->libdir . '/formslib.php');
$context = context_system::instance();
$data->pagecontent['text'] = file_save_draft_area_files($data->pagecontent['itemid'], $context->id,
'local_pages', 'pagecontent',
0, array('subdirs' => true), $data->pagecontent['text']);
$data->pagedata = '';
if (strtolower($data->pagetype) == "form") {
$pagedata = array();
$fieldnames = required_param_array('fieldname', PARAM_RAW);
$fieldtype = required_param_array('fieldtype', PARAM_RAW);
$fieldrequired = required_param_array('fieldrequired', PARAM_RAW);
$fielddefault = required_param_array('defaultvalue', PARAM_RAW);
$fieldreadsfrom = required_param_array('readsfrom', PARAM_RAW);
foreach ($fieldnames as $key => $value) {
// Get all data sent from the form.
// Stop empty fields being created.
if (trim($value) != '') {
$pagedata[] = array("name" => $value,
"type" => $fieldtype[$key],
"required" => $fieldrequired[$key],
"defaultvalue" => $fielddefault[$key],
"readsfrom" => $fieldreadsfrom[$key]);
}
}
$data->pagedata = json_encode($pagedata);
}
$recordpage = new stdClass();
$recordpage->id = $data->id;
$recordpage->pagedate = $data->pagedate;
$recordpage->pagename = $data->pagename;
$recordpage->pageorder = intval($data->pageorder);
$recordpage->menuname = strtolower(str_replace(array(" ", "/", "\\", "'", '"', ";", "~",
"?", "&", "@", "#", "$", "%", "^", "*", "(", ")", "+", "="), "", trim($data->menuname)));
$recordpage->onmenu = $data->onmenu;
$recordpage->loginrequired = $data->loginrequired;
$recordpage->accesslevel = $data->accesslevel;
$recordpage->pagedata = $data->pagedata;
$recordpage->pagetype = $data->pagetype;
$recordpage->emailto = $data->emailto;
$recordpage->pagelayout = $data->pagelayout;
$recordpage->pageparent = intval($data->pageparent);
$recordpage->pagecontent = $data->pagecontent['text'];
$result = $page->update($recordpage);
if ($result && $result > 0) {
redirect(new moodle_url($CFG->wwwroot . '/local/pages/edit.php', array('id' => $result)));
}
}
}
/**
*
* Show the page information to edit
*
* @param object $page
*/
public function edit_page($page) {
$mform = new pages_edit_product_form($page);
$forform = new stdClass();
$forform->pagecontent['text'] = $page->pagecontent;
$forform->pagename = $page->pagename;
$forform->onmenu = $page->onmenu;
$forform->loginrequired = $page->loginrequired;
$forform->accesslevel = $page->accesslevel;
$forform->pageparent = $page->pageparent;
$forform->menuname = $page->menuname;
$forform->id = $page->id;
$forform->emailto = $page->emailto;
$forform->pagedate = $page->pagedate;
$forform->pagelayout = $page->pagelayout;
$forform->pageorder = $page->pageorder;
$forform->pagetype = $page->pagetype;
$mform->set_data($forform);
$mform->display();
}
/**
*
* Gets all the menu items
*
* @param mixed $parent
* @param string $name
* @param string $url
* @return string
*/
public function get_menuitem($parent, $name, $url) {
global $DB, $CFG;
$context = context_system::instance();
$html = '';
$urllocation = new moodle_url($CFG->wwwroot . '/local/pages/', array('id' => $parent));
if (get_config('local_pages', 'cleanurl_enabled')) {
$urllocation = new moodle_url($CFG->wwwroot . '/local/pages/' . $url);
}
$today = date('U');
$records = $DB->get_records_sql("SELECT * FROM {local_pages} WHERE deleted=0 AND onmenu=1 " .
"AND pagetype='page' AND pageparent=? AND pagedate <=? " .
"ORDER BY pageorder", array($parent, $today));
if ($records) {
$html .= "<li class='custompages_item'><a href='" . $urllocation . "'>" . $name . "</a>";
$html .= "<ul class='custompages_submenu'>";
$canaccess = true;
foreach ($records as $page) {
if (isset($page->accesslevel) && stripos($page->accesslevel, ":") !== false) {
$canaccess = false; // Page Has level Requirements - check rights.
$levels = explode(",", $page->accesslevel);
foreach ($levels as $level) {
if ($canaccess != true) {
if (stripos($level, "!") !== false) {
$level = str_replace("!", "", $level);
$canaccess = has_capability(trim($level), $context) ? false : true;
} else {
$canaccess = has_capability(trim($level), $context) ? true : false;
}
}
}
}
if ($canaccess) {
$html .= $this->get_menuitem($page->id, $page->pagename, $page->menuname);
}
}
$html .= "</ul>";
$html .= "</li>";
} else {
$html .= "<li class='custompages_item'><a href='" . $urllocation . "'>" . $name . "</a></li>";
}
return $html;
}
/**
*
* Builds the menu for the page
*
* @return string
*/
public function build_menu() {
global $DB;
$dbman = $DB->get_manager();
$html = '';
if ($dbman->table_exists('local_pages')) {
$html = '<ul class="custompages_nav">';
$today = date('U');
$records = $DB->get_records_sql("SELECT * FROM {local_pages} WHERE deleted=0 AND onmenu=1 " .
"AND pagetype='page' AND pageparent=0 AND pagedate <= ? ORDER BY pageorder", array($today));
foreach ($records as $page) {
if ($this->can_access_page($page)) {
$html .= $this->get_menuitem($page->id, $page->pagename, $page->menuname);
}
}
$html .= "</ul>";
}
return $html;
}
/**
* Checks if user has access to provided page
*
* @param $page
* @return bool
* @throws coding_exception
* @throws dml_exception
*/
private function can_access_page($page): bool
{
$context = context_system::instance();
$canaccess = true;
if (trim($page->accesslevel) != '' && stripos($page->accesslevel, ':') !== false) {
// Page Has level Requirements - check rights.
$canaccess = false;
$levels = explode(",", $page->accesslevel);
foreach ($levels as $key => $level) {
if ($canaccess != true) {
if (stripos($level, "!") !== false) {
$level = str_replace("!", "", $level);
$canaccess = has_capability(trim($level), $context);
} else {
$canaccess = has_capability(trim($level), $context);
}
}
}
}
return $canaccess;
}
}