-
Notifications
You must be signed in to change notification settings - Fork 1
/
block_glsubs.php
816 lines (760 loc) · 37.3 KB
/
block_glsubs.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
<?php
/**
* Created by PhpStorm.
* User: vasileios
* Date: 28/10/2016
* Time: 15:03
*
* File: blocks/glsubs/block_glsubs.php
*
* Purpose: A glossary subscriptions block plugin for Moodle
*
* Input: N/A
*
* Output: N/A
*
* Notes: This is developed for the QM+ Moodle site of the Queen Mary university of London
*
* 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/>.
*/
// namespace glsubs;
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once __DIR__ . '/../../config.php';
global $CFG;
include_once $CFG->dirroot . '/blocks/glsubs/classes/block_glsubs_form.php';
/** @noinspection PhpIllegalPsrClassPathInspection */
class block_glsubs extends block_base
{
/**
* Glossary Subscriptions Block Frontend Controller Presenter
* Sept 2016
* QM+
* vasileios
*/
public static $old_error_handler;
/**
* Method init
*
* Purpose initalise the class, set its title
*
* Parameters N/A
*
* Returns N/A
*
* Notes Method must be declared, as it is defined abstract
* title should be defined as it used by the Moodle system for the administration pages
*/
public function init()
{
// set the title of this plugin
$this->title = get_string('pluginname', 'block_glsubs');
// block_glsubs::$old_error_handler = set_error_handler("block_glsubs::exception_error_handler");
// error_reporting(E_ALL);
}
/**
* Method exception_error_handler
*
* Purpose Used for debugging, capturing exceptions and throwing errors
* It might not be compatible for the PHP 7 version, as it uses a Throwable parameter
*
* Parameters
* @param $errno , error number
* @param $errstr , error message
* @param $errfile , error file
* @param $errline , error code line
*
* Returns N/A
*
* Notes Method must be declared, as it is defined abstract
* title should be defined as it used by the Moodle system for the administration pages
*
*
* @throws ErrorException
*/
public static function exception_error_handler($errno, $errstr, $errfile, $errline)
{
// debug_print_backtrace();
block_glsubs::$old_error_handler = set_error_handler(block_glsubs::$old_error_handler);
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
/**
* Method applicable_formats
*
* Purpose Set the default block configuration of on which type of pages to be viewable
*
* Parameters N/A
*
* Returns array('all' => true)
* @return array
* Notes make this plugin available to all pages
* avoid the configuration step of making it available to all glossary view pages manually
*/
public function applicable_formats()
{
return array('all' => TRUE);
}
/**
* Method currentPageURL
*
* Purpose Get the glossary view page URL with its parameters, to use it as the form action target
* Used to associate the current page with the form action target,
* so it returns to the same page after submit
*
* Parameters N/A
*
* Returns Return the current page URL
* @return string Get URL of the glossary view page
*
* Notes It is the safest method to set the plugin form action target ,
* so after submission, it will return to the same page
*
*/
private function currentPageURL()
{
$pageURL = 'http';
if (array_key_exists('HTTPS', $_SERVER) && $_SERVER['HTTPS'] === 'on') {
$pageURL .= 's';
}
$pageURL .= '://';
if ($_SERVER['SERVER_PORT'] !== '80') {
$pageURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
} else {
$pageURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
}
$pageurlparts = explode('?', $pageURL);
$pageurlparts['protocol'] = explode('://', $pageurlparts[0])[0];
$pageurlparts['host'] = explode('/', explode('://', $pageurlparts[0])[1])[0];
$pageurlparts['pagepath'] = explode($pageurlparts['host'], explode('://', $pageurlparts[0])[1])[1];
if (isset($pageurlparts[1])) {
foreach (explode('&', $pageurlparts[1]) as $key => $parameterset) {
$keyValueArray = explode('=', $parameterset);
if (array_key_exists(0, $keyValueArray)) {
$param = $keyValueArray[0];
} else {
$param = '';
}
if (array_key_exists(1, $keyValueArray)) {
$value = $keyValueArray[1];
} else {
$value = '';
}
$pageurlparts['parameters'][$param] = $value;
}
}
if (count($_REQUEST) > 0) {
foreach ($_REQUEST as $param => $value) {
$pageurlparts['parameters'][$param] = $value;
}
}
$pageurlparts['fullurl'] = $pageURL;
return $pageurlparts;
}
/**
* Method get_latest_messages
*
* Purpose Get a batch of the latest messages for the user, to show at the top of the block
*
* Parameters
* @param $glossaryid , the glossary id to identify the relevant messages for the user
*
* Returns
* @return array , of message objects
*/
protected function get_latest_messages($glossaryid)
{
global $DB, $USER;
$messages = array();
$glsubs_settings = get_config('block_glsubs');
$messages_count = (int)$glsubs_settings->messagestoshow;
try {
$sql = 'SELECT l.id , l.userid , l.eventlogid , l.timecreated , l.timedelivered
FROM {block_glsubs_messages_log} AS l
JOIN {block_glsubs_event_subs_log} AS e ON l.eventlogid = e.id
WHERE l.userid = :userid AND e.glossaryid = :glossaryid AND l.timedelivered IS NULL
ORDER BY l.id';
$messages = $DB->get_records_sql($sql, array('userid' => (int)$USER->id, 'glossaryid' => $glossaryid), 0, $messages_count);
// if there are no unread messages show the latest read
if (count($messages) === 0) {
$sql = 'SELECT l.* FROM {block_glsubs_messages_log} l ';
$sql .= 'JOIN {block_glsubs_event_subs_log} e ON e.id = l.eventlogid AND e.glossaryid = :glossaryid ';
$sql .= ' WHERE l.userid = :userid ORDER BY l.id DESC ';
$messages = $DB->get_records_sql($sql, array('userid' => (int)$USER->id, 'glossaryid' => $glossaryid), 0, $messages_count);
}
} catch (\Throwable $exception) {
error_log('ERROR: glsubs get_latest_messages ' . $exception->getMessage());
return $messages;
}
if (count($messages) > 0) {
foreach ($messages as $key => & $message) {
try {
$message->event = $DB->get_record('block_glsubs_event_subs_log', array('id' => (int)$message->eventlogid));
$message->date = date('Y-m-d H:i:s', (int)$message->event->timecreated);
$message->user = $DB->get_record('user', array('id' => (int)$message->event->userid));
$message->author = $DB->get_record('user', array('id' => (int)$message->event->authorid));
} catch (\Throwable $exception) {
$this->content->text .= '<strong>' . get_string('block_could_not_access', 'block_glsubs') . $glsubs_settings->messagestoshow . get_string('block_most_recent', 'block_glsubs') . '</strong>';
}
}
}
return $messages;
}
/**
* Method show_messages
*
* Purpose if enabled show the relevant glossary event messages for the user of the current glossary
*
* Parameters
* @param $glossaryid , the glossary id to identify the relevant messages for the user
*
* Returns outputs the message links directly to the block
*/
private function show_messages($glossaryid)
{
global $DB, $USER;
// get the block settings from its configuration
$glsubs_settings = get_config('block_glsubs');
$messages_count = (int)$glsubs_settings->messagestoshow;
// $msg_link = '';
try {
$msg_link = html_writer::link(new moodle_url('/message/index.php', array('user1' => $USER->id, 'viewing' => 'recentnotifications')), get_string('goto_messages', 'block_glsubs'));
} catch (\Throwable $exception) {
error_log('ERROR: glsubs show_messages ' . $exception->getMessage());
$msg_link = '';
}
// $this->content->text .= '<br/>' . $msg_link . '<br/>' ;
if ((int)$glsubs_settings->messagestoshow > 0) {
$messages = $this->get_latest_messages($glossaryid);
try {
// read how many unread messages exist for this glossary
$sql = 'SELECT l.id , l.userid , l.eventlogid , l.timecreated ,l.timedelivered FROM {block_glsubs_messages_log} AS l
JOIN {block_glsubs_event_subs_log} AS e ON l.eventlogid = e.id
WHERE l.userid = :userid AND e.glossaryid = :glossaryid AND l.timedelivered IS NULL
ORDER BY l.id';
$cmessages = $DB->get_records_sql($sql, array('userid' => (int)$USER->id, 'glossaryid' => $glossaryid), 0, $messages_count);
$counter = count($cmessages);
// release memory now
$cmessages = NULL;
$unread = ($counter > 0);
} catch (\Throwable $exception) {
error_log('ERROR: glsubs show_messages ' . $exception->getMessage());
$unread = FALSE;
}
// Create a toggle mechanism for showing or hiding the recent messages
if (count($messages) > 0) {
$this->page->requires->jquery();
$javascriptswitch = <<<JSC
<script>
$( document ).ready(function(){
$("#glossarymessagesshowhide").click(function(){
$("#glossarymessagesblocktable").toggle();
$("#glossarymessagesshowhide").toggle();
$("#glossarymessagesshowhide_2").toggle();
});
$("#glossarymessagesshowhide_2").click(function(){
$("#glossarymessagesblocktable").toggle();
$("#glossarymessagesshowhide").toggle();
$("#glossarymessagesshowhide_2").toggle();
});
}) ;
</script>
JSC;
$this->content->text .= '<div id="glossarymessagesblock">';
$this->content->text .= '<span id="glossarymessagesshowhide">';
$this->content->text .= get_string('view_show_hide', 'block_glsubs');
$this->content->text .= '</span>'; // id="glossarymessagesshowhide"
$this->content->text .= '<span id="glossarymessagesshowhide_2" style="display: none ;">';
$this->content->text .= get_string('view_show_hide_2', 'block_glsubs');
$this->content->text .= '</span>'; // id="glossarymessagesshowhide"
$this->content->text .= get_string('block_found', 'block_glsubs') . count($messages);
$link = html_writer::link(new moodle_url('/message/index.php', array()), $unread ? get_string('block_unread_messages', 'block_glsubs') : get_string('block_read_messages', 'block_glsubs'));
$this->content->text .= $link;
$this->content->text .= '<br/>';
$this->content->text .= '<div id="glossarymessagesblocktable" style="display: none ;">';
$this->content->text .= '<table id="glossarymessagestable"><thead><tr>';
$this->content->text .= '<th>' . get_string('view_when', 'block_glsubs') . '</th>';
$this->content->text .= '<th> </th>';
$this->content->text .= '</th><th>' . get_string('view_by_user', 'block_glsubs') . '</th>';
$this->content->text .= '<th> </th>';
$this->content->text .= '<th>' . get_string('view_on_concept', 'block_glsubs') . '</th></tr></thead><tbody>';
foreach ($messages as $key => $message) {
// if there is a concept asociated with this et its name
if ((int)$message->event->conceptid > 0) {
try {
$record = $DB->get_record('glossary_entries', array('id' => (int)$message->event->conceptid));
$name = $record->concept;
// in case of deleted concept attempt to get it from the message
if (is_null($name)) {
$name = $this->get_entry($message->event->eventtext);
}
} catch (\Throwable $exception) {
error_log('ERROR: glsubs show_messages ' . $exception->getMessage());
$name = '';
}
} else { // else if there is a category associated with it get its name
try {
$record = $DB->get_record('glossary_categories', array('id' => (int)$message->event->categoryid));
$name = $record->name;
if (is_null($name)) {
$name = $this->get_category($message->event->eventtext);
}
} catch (\Throwable $exception) {
error_log('ERROR: glsubs show_messages ' . $exception->getMessage());
$name = '';
}
}
try {
$link = html_writer::link(new moodle_url('/blocks/glsubs/view.php', array('id' => $key)), substr($message->date, 0, 10), array('title' => $message->date . chr(13) . strip_tags($message->event->eventtext), 'font-size' => '90%;'));
} catch (\Throwable $exception) {
error_log('ERROR: glsubs show_messages ' . $exception->getMessage());
$link = '';
}
$this->content->text .= '<tr><td>' . $link . '</td>';
$this->content->text .= '<td> </td>';
$this->content->text .= '<td title="' . fullname($message->user) . '"> ' . $this->ellipsisString(fullname($message->user), 20) . '</td>';
$this->content->text .= '<td> </td>';
$this->content->text .= '<td title="' . $name . '">' . $this->ellipsisString($name, 18) . '</td></tr>';
}
$this->content->text .= '</tbody></table><hr style="visibility: visible !important; display: inline !important;"/>';
$this->content->text .= '</div>'; // id="glossarymessagesblocktable"
$this->content->text .= '</div>'; // id="glossarymessagesblock"
$this->content->text .= $javascriptswitch;
}
}
}
/**
* Method get_category
*
* Purpose Identify and return the message category(ies) in the message text
*
* Parameters
* @param $message_text , the message text
*
* Returns
*
* @return mixed|string, either an empty string , either the category(ies) identified as text
*
*/
private function get_category($message_text)
{
$ret = '';
$msg_parts = explode('<br />', $message_text);
foreach ($msg_parts as $key => & $msg_part) {
$msg_part = strip_tags($msg_part);
if (stripos($msg_part, 'Category(ies) ') !== FALSE) {
$ret = str_ireplace('Category(ies) ', '', $msg_part);
// $ret = str_ireplace(']','',$ret);
$ret = str_ireplace("\n", '', $ret);
}
}
return $ret;
}
/**
* Method get_entry
*
* Purpose Identify and return the message concept in the message text
*
* Parameters
* @param $message_text , the message text
*
* Returns
* @return mixed|string, either an empty string , either the concept as text
*/
private function get_entry($message_text)
{
$ret = '';
$msg_parts = explode('<br />', $message_text);
foreach ($msg_parts as $key => & $msg_part) {
$msg_part = strip_tags($msg_part);
if (stripos($msg_part, 'Concept [') !== FALSE) {
$ret = str_ireplace('Concept [', '', $msg_part);
$ret = str_ireplace(']', '', $ret);
$ret = str_ireplace("\n", '', $ret);
}
}
return $ret;
}
/**
* Method get_content
*
* Purpose create all the block contents and present it
* Subscriptions Block Contents creation function
*
* Parameters N/A
*
* Returns
* @return string, as HTML content for the block
*
*/
public function get_content()
{
/** @var stdClass $this */
// define usage of global variables
global $PAGE, $COURSE;// , $DB , $CFG ; // $USER, $SITE , $OUTPUT, $THEME, $OUTPUT ;
// Check if the page is referring to a glossary module view activity
if ('mod-glossary-view' !== $PAGE->pagetype) {
return $this->content;
}
if (NULL !== $this->title) {
$this->title = get_string('blockheader', 'block_glsubs');
}
// if the contents are already set, just return them
if ($this->content !== NULL) {
return $this->content;
}
// this is only for logged in users
if (!isloggedin() || isguestuser()) {
return '';
}
// get the current moodle configuration
require_once __DIR__ . '/../../config.php';
// this is only for logged in users
require_login();
// get the module information
$courseinfo = get_fast_modinfo($COURSE);
// prapare for contents
$this->content = new stdClass;
$this->content->text = '';
$this->content->text .= '<strong>' . $PAGE->title . '</strong>';
// add a footer for the block
$this->content->footer = '<hr style="display: block!important;"/><div style="text-align:center;">' . get_string('blockfooter', 'block_glsubs') . '</div>';
// get the id parameter if exists
$cmid = optional_param('id', 0, PARAM_INT);
// check if there is a valid glossary view page
if ($cmid > 0) {
// set page context
$PAGE->set_context(context_module::instance($cmid));
try {
if ($courseinfo->get_cm($cmid)) {
$cm = $courseinfo->get_cm($cmid);
} else {
return $this->content;
}
} catch (Throwable $e) {
error_log('ERROR: glsubs get_content glsubs messages ' . $e->getMessage());
return $this->content;
}
// Check if the course module is available and it is visible and it is visible to the user and it is a glossary module
if (!(TRUE === $cm->available && '1' === $cm->visible && TRUE === $cm->uservisible && 'glossary' === $cm->modname)) {
return $this->content;
}
// get glossary ID
$glossaryid = (int)$cm->instance;
// show unread messages
$this->show_messages($glossaryid);
// create a glossary subscriptions block form and assign its action to the original page
$subscriptions_form = new block_glsubs_form($this->currentPageURL()['fullurl']);
// test for the form status , do kee the order of cancelled, submitted, new
if ($subscriptions_form->is_cancelled()) {
// redirect to the original page where the Cancel button was pressed, so use the $_SERVER['HTTP_REFERER'] variable
try {
$url = new moodle_url($_SERVER['HTTP_REFERER'], array());
redirect($url);
} catch (Throwable $e) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
} elseif ($subscriptions_form->is_submitted()) {
// $this->content->text .= '<br/><u>Submitted form</u><br/>';
$subs_data = $subscriptions_form->get_data();
if ($subs_data) {
// store this data set
try {
$errors = $this->store_data($subs_data);
} catch (\Throwable $exception) {
$errors = new \stdClass();
$errors->messages[] = 'ERROR: glsubs get_content while attempting to save data ' . $exception->getMessage();
}
// if there were any errors, display them
if (is_array($errors->messages)) {
foreach ($errors->messages as $key => $errmsg) {
$this->content->text .= '<p>ERROR: glsubs ' . $errmsg . '</p>';
}
}
}
}
// add the contents of the form to the block
$this->content->text .= $subscriptions_form->render();
}
// Finish and return contents
return $this->content;
}
/**
* Method store_data
*
* Purpose Store the subscription preferences for the user on the current glossary
*
* Parameters
* @param $dataset , the data of the Moodle plugin form
*
* Returns
* @return null in case of success or stdClass of errors in case of errors
*/
private function store_data($dataset)
{
global $DB;
$error = new stdClass();
$error->messages = [];
$userid = $dataset->glossary_userid;
$glossaryid = $dataset->glossary_glossaryid;
$fullsubkey = get_string('glossaryformfullelementname', 'block_glsubs');
$newcat = get_string('glossaryformcategorieselementname', 'block_glsubs');
$newconcept = get_string('glossaryformconceptselementname', 'block_glsubs');
$arrData = (array)$dataset;
try {
foreach ($arrData as $key => $value) {
// if the data in the form is a glossary comment subscription instruction then
if ($key === $fullsubkey) {
if ($DB->record_exists('block_glsubs_glossaries_subs', array('userid' => $userid, 'glossaryid' => $glossaryid))) {
$oldrecord = $DB->get_record('block_glsubs_glossaries_subs', array('userid' => $userid, 'glossaryid' => $glossaryid));
$oldrecord->active = $value;
$DB->update_record('block_glsubs_glossaries_subs', $oldrecord, FALSE);
} else {
$newrecord = new stdClass();
$newrecord->userid = $userid;
$newrecord->glossaryid = $glossaryid;
$newrecord->active = $value;
$newrecord->newcategories = 0;
$newrecord->newentriesuncategorised = 0;
$newentryid = $DB->insert_record('block_glsubs_glossaries_subs', $newrecord, TRUE, TRUE); // use bulk updates
if (!$newentryid > 0) {
$error->messages[] = "Cannot create new full subscription record for $userid on glossary $glossaryid ";
}
}
// $msg = 'glossaries table full subscription';
// if the data in the form is a new categories subscription instruction then
} elseif ($key === $newcat) {
if ($DB->record_exists('block_glsubs_glossaries_subs', array('userid' => $userid, 'glossaryid' => $glossaryid))) {
$oldrecord = $DB->get_record('block_glsubs_glossaries_subs', array('userid' => $userid, 'glossaryid' => $glossaryid));
$oldrecord->newcategories = $value;
$DB->update_record('block_glsubs_glossaries_subs', $oldrecord, FALSE);
} else {
$newrecord = new stdClass();
$newrecord->userid = $userid;
$newrecord->glossaryid = $glossaryid;
$newrecord->active = 0;
$newrecord->newcategories = $value;
$newrecord->newentriesuncategorised = 0;
$newentryid = $DB->insert_record('block_glsubs_glossaries_subs', $newrecord, TRUE, TRUE); // use bulk updates
if (!$newentryid > 0) {
$error->messages[] = "Cannot create new new categories subscription record for $userid on glossary $glossaryid ";
}
}
// $msg = 'glossaries table new categories subscription';
// if the data in the form is a new uncategorised consepts sbscription instruction then
} elseif ($key === $newconcept) {
if ($DB->record_exists('block_glsubs_glossaries_subs', array('userid' => $userid, 'glossaryid' => $glossaryid))) {
$oldrecord = $DB->get_record('block_glsubs_glossaries_subs', array('userid' => $userid, 'glossaryid' => $glossaryid));
$oldrecord->newentriesuncategorised = $value;
$DB->update_record('block_glsubs_glossaries_subs', $oldrecord, FALSE);
} else {
$newrecord = new stdClass();
$newrecord->userid = $userid;
$newrecord->glossaryid = $glossaryid;
$newrecord->active = 0;
$newrecord->newcategories = 0;
$newrecord->newentriesuncategorised = $value;
$newentryid = $DB->insert_record('block_glsubs_glossaries_subs', $newrecord, TRUE, TRUE); // use bulk updates
if (!$newentryid > 0) {
$error->messages[] = "Cannot create new uncategorised concepts subscription record for $userid on glossary $glossaryid ";
}
}
// $msg = 'glossaries table new concepts no category subscription';
// if the data inthe form is a category subscription instruction then
} elseif (0 === strpos($key, 'glossary_category')) {
// } elseif ( substr($key,0,17) === 'glossary_category'){
$categoryid = (int)preg_replace('/[^0-9,.]/', '', $key);
if ($DB->record_exists('block_glsubs_categories_subs', array('userid' => $userid, 'glossaryid' => $glossaryid, 'categoryid' => $categoryid))) {
$old_category_record = $DB->get_record('block_glsubs_categories_subs', array('userid' => $userid, 'glossaryid' => $glossaryid, 'categoryid' => $categoryid));
$old_category_record->active = $value;
$DB->update_record('block_glsubs_categories_subs', $old_category_record, FALSE);
} else {
$newrecord = new stdClass();
$newrecord->userid = $userid;
$newrecord->glossaryid = $glossaryid;
$newrecord->categoryid = (int)preg_replace('/[^0-9,.]/', '', $key);
$newrecord->active = $value;
$newentryid = $DB->insert_record('block_glsubs_categories_subs', $newrecord, TRUE, TRUE); //use bulk updates
if (!$newentryid > 0) {
$error->messages[] = "Cannot create new category subscription record for $userid on glossary $glossaryid and category $key";
}
}
// $msg = "categories table $categoryid ";
// if the data in the form is a concept subscription instruction then
} elseif (0 === strpos($key, 'glossary_concept')) {
// } elseif ( substr($key,0,16) === 'glossary_concept') {
$conceptid = (int)preg_replace('/[^0-9,.]/', '', $key);
if ($DB->record_exists('block_glsubs_concept_subs', array('userid' => $userid, 'glossaryid' => $glossaryid, 'conceptid' => $conceptid))) {
$oldrecord = $DB->get_record('block_glsubs_concept_subs', array('userid' => $userid, 'glossaryid' => $glossaryid, 'conceptid' => $conceptid));
$oldrecord->conceptactive = $value;
$DB->update_record('block_glsubs_concept_subs', $oldrecord, FALSE);
} else {
$newrecord = new stdClass();
$newrecord->userid = $userid;
$newrecord->glossaryid = $glossaryid;
$newrecord->conceptid = $conceptid;
$newrecord->conceptactive = $value;
$newrecord->commentsactive = 0;
$newentryid = $DB->insert_record('block_glsubs_concept_subs', $newrecord, TRUE, TRUE); // use bulk updates
if (!$newentryid > 0) {
$error->messages[] = "Cannot create new concept subscription record for $userid on glossary $glossaryid and concept $key ";
}
}
// $msg = 'concepts table';
// if the data in the form is a concept comments subscription instruction then
} elseif (0 === strpos($key, 'glossary_comment')) {
// } elseif ( substr($key ,0 ,16) === 'glossary_comment') {
$conceptid = (int)preg_replace('/[^0-9,.]/', '', $key);
if ($DB->record_exists('block_glsubs_concept_subs', array('userid' => $userid, 'glossaryid' => $glossaryid, 'conceptid' => $conceptid))) {
$oldrecord = $DB->get_record('block_glsubs_concept_subs', array('userid' => $userid, 'glossaryid' => $glossaryid, 'conceptid' => $conceptid));
$oldrecord->commentsactive = $value;
$DB->update_record('block_glsubs_concept_subs', $oldrecord, FALSE);
} else {
$newrecord = new stdClass();
$newrecord->userid = $userid;
$newrecord->glossaryid = $glossaryid;
$newrecord->conceptid = $conceptid;
$newrecord->conceptactive = 0;
$newrecord->commentsactive = $value;
$newentryid = $DB->insert_record('block_glsubs_concept_subs', $newrecord, TRUE, TRUE); // use bulk updates
if (!$newentryid > 0) {
$error->messages[] = "Cannot create new concept comments subscription record for $userid on glossary $glossaryid and concept $key ";
}
}
// $msg = 'concepts table for comments';
// if the data in the form is an author subscription instruction then
} elseif (0 === strpos($key, 'glossary_author')) {
// } elseif ( substr($key ,0 ,15) === 'glossary_author') {
$authorid = (int)preg_replace('/[^0-9,.]/', '', $key);
if ($DB->record_exists('block_glsubs_authors_subs', array('userid' => $userid, 'glossaryid' => $glossaryid, 'authorid' => $authorid))) {
// $oldrecord = new stdClass();
$oldrecord = $DB->get_record('block_glsubs_authors_subs', array('userid' => $userid, 'glossaryid' => $glossaryid, 'authorid' => $authorid));
$oldrecord->active = $value;
$DB->update_record('block_glsubs_authors_subs', $oldrecord, FALSE);
// $msg = 'active author subscription';
} else {
$newrecord = new stdClass();
$newrecord->userid = $userid;
$newrecord->glossaryid = $glossaryid;
$newrecord->authorid = $authorid;
$newrecord->active = $value;
$newentryid = $DB->insert_record('block_glsubs_authors_subs', $newrecord, TRUE, TRUE); // use bulk updates
if (!$newentryid > 0) {
$error->messages[] = "Cannot create new author subscription record for $userid on glossary $glossaryid and author $authorid ";
}
}
// $msg = 'concepts table for authors';
}
// if there is a message to show then add it to the messages
/* if(! is_null($msg)){
$m[$key] = $msg . " $key = $value";
$msg = NULL;
}*/
}
} catch (\Throwable $exception) {
$error->messages[] = 'ERROR: glsubs while attempting to store data ' . $exception->getMessage();
}
// what to report at the end ?
if (count($error->messages) > 0) {
return $error;
} else {
return NULL;
}
}
/**
* Method instance_allow_multiple
*
* Purpose enable only one instance per page to associate with the viewable glossary
*
* Parameters N/A
*
* Returns
* @return bool , false to disable multiple instances of the block on one page
*
*/
public function instance_allow_multiple()
{
return FALSE;
}
/**
* Method has_config
*
* Purpose This function is required by Moodle to overide the default inherited function and to
* return true in order to be able to store and use relevant universal plugin settings.
* If you need more fine grained settings such as user or role or other classification
* you must provide a set of database structures and their associated business logic
*
* Parameters N/A
*
* Returns
* @return bool, true as this block has an administration settings functionality
*/
public function has_config()
{
return parent::has_config() || TRUE;
}
/****************************************************************
*
* Method: ellipsisString
*
* Purpose: Create short form of a string including ellipsis (...)
* if required. Used for making use in narrow elements of
* the web screens
*
* Parameters: $text , the text (any bytes form), $size , the legnth to cut off
*
* Returns: the shortened text version plus optional ellipsis
****************************************************************
* Reduce text to the maximum parameterised length
* @param $text
* @param $size
*
* @return string
*/
protected function ellipsisString($text, $size)
{
$retstr = $text;
if ($size < 1) {
$size = 3;
}
if ($this->is_multibyte($text)) {
if (mb_strlen($text) > $size) {
$retstr = mb_substr($retstr, 0, $size - 3) . '...';
}
} else {
if (strlen($text) > $size) {
$retstr = substr($retstr, 0, $size - 3) . '...';
}
}
return $retstr;
}
/****************************************************************
*
* Method: is_multibyte
*
* Purpose: checks if the text contains mutli byte characters
*
*
*
* Parameters: $s , the text (any bytes form)
*
* Returns: true if at least one character is multi byte
****************************************************************
* check for multibyte strings
*/
protected function is_multibyte($s)
{
return mb_strlen($s, 'utf-8') < strlen($s);
}
}