-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocallib.php
1116 lines (947 loc) · 40.1 KB
/
locallib.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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Private leganto module utility functions.
*
* @package mod_leganto
* @copyright 2017 Lancaster University {@link http://www.lancaster.ac.uk/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Tony Butler <a.butler4@lancaster.ac.uk>
*/
defined('MOODLE_INTERNAL') || die();
define('ALMA_GET_COURSES', 0);
define('ALMA_GET_COURSE', 1);
define('ALMA_GET_LIST', 2);
define('ALMA_GET_CITATION', 3);
require_once($CFG->dirroot . '/mod/leganto/lib.php');
/**
* Standard base class for mod_leganto.
*
* @package mod_leganto
* @copyright 2017 Lancaster University {@link http://www.lancaster.ac.uk/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Tony Butler <a.butler4@lancaster.ac.uk>
*/
class leganto {
/** @var stdClass The leganto record that contains the global settings for this leganto instance. */
private $instance;
/** @var context The context of the course module for this leganto instance
* (or just the course if we are creating a new one).
*/
private $context;
/** @var stdClass The course this leganto instance belongs to. */
private $course;
/** @var stdClass The admin config for all leganto instances. */
private $adminconfig;
/** @var leganto_renderer The custom renderer for this module. */
private $output;
/** @var stdClass The course module for this leganto instance. */
private $coursemodule;
/** @var array Cache for things like the coursemodule name or the scale menu - only lives for a single request. */
private $cache;
/** @var string Action to be used to return to this page (without repeating any form submissions etc). */
private $returnaction = 'view';
/** @var array Params to be used to return to this page. */
private $returnparams = [];
/** @var string modulename Prevents excessive calls to get_string. */
private static $modulename = null;
/** @var string modulenameplural Prevents excessive calls to get_string. */
private static $modulenameplural = null;
/** @var array List of suspended user ids in form of ([id1] => id1). */
public $susers = null;
/** @var bool Whether or not the Alma API has been fully configured. */
private $apiconfigured = false;
/** @var string Regular expression matching a leganto section id. */
private $sectionidregex = '/^section-[0-9]{16,}$/';
/** @var string Regular expression matching a leganto citation id. */
private $citationidregex = '/^citation-[0-9]{16,}$/';
/**
* Constructor for the base leganto class.
*
* @param mixed $coursemodulecontext context|null The course module context
* (or the course context if the coursemodule has not been
* created yet).
* @param mixed $coursemodule The current course module if it was already loaded,
* otherwise this class will load one from the context as required.
* @param mixed $course The current course if it was already loaded,
* otherwise this class will load one from the context as required.
*/
public function __construct($coursemodulecontext, $coursemodule, $course) {
$this->context = $coursemodulecontext;
$this->coursemodule = $coursemodule;
$this->course = $course;
// Temporary cache only lives for a single request - used to reduce db lookups.
$this->cache = [];
}
/**
* Set the action and parameters that can be used to return to the current page.
*
* @param string $action The action for the current page.
* @param array $params An array of name value pairs which form the parameters to return to the current page.
* @return void
*/
public function register_return_link($action, $params) {
global $PAGE;
$params['action'] = $action;
$currenturl = $PAGE->url;
$currenturl->params($params);
$PAGE->set_url($currenturl);
}
/**
* Return an action that can be used to get back to the current page.
*
* @return string Action.
*/
public function get_return_action() {
global $PAGE;
$params = $PAGE->url->params();
if (!empty($params['action'])) {
return $params['action'];
}
return '';
}
/**
* Return a list of parameters that can be used to get back to the current page.
*
* @return array Params.
*/
public function get_return_params() {
global $PAGE;
$params = $PAGE->url->params();
unset($params['id']);
unset($params['action']);
return $params;
}
/**
* Set the submitted form data.
*
* @param stdClass $data The form data (instance).
*/
public function set_instance(stdClass $data) {
$this->instance = $data;
}
/**
* Set the context.
*
* @param context $context The new context.
*/
public function set_context(context $context) {
$this->context = $context;
}
/**
* Set the course data.
*
* @param stdClass $course The course data.
*/
public function set_course(stdClass $course) {
$this->course = $course;
}
/**
* Has this leganto been constructed from an instance?
*
* @return bool
*/
public function has_instance() {
return $this->instance || $this->get_course_module();
}
/**
* Get the settings for the current instance of this leganto.
*
* @return stdClass The settings.
*/
public function get_instance() {
global $DB;
if ($this->instance) {
return $this->instance;
}
if ($this->get_course_module()) {
$params = ['id' => $this->get_course_module()->instance];
$this->instance = $DB->get_record('leganto', $params, '*', MUST_EXIST);
}
if (!$this->instance) {
throw new coding_exception('Improper use of the leganto class. ' .
'Cannot load the leganto record.');
}
return $this->instance;
}
/**
* Get the context of the current course.
*
* @return context|null The course context.
*/
public function get_course_context() {
if (!$this->context && !$this->course) {
throw new coding_exception('Improper use of the leganto class. Cannot load the course context.');
}
if ($this->context) {
return $this->context->get_course_context();
}
return context_course::instance($this->course->id);
}
/**
* Get the current course module.
*
* @return stdClass|null The course module.
*/
public function get_course_module() {
if ($this->coursemodule) {
return $this->coursemodule;
}
if (!$this->context) {
return null;
}
if ($this->context->contextlevel == CONTEXT_MODULE) {
$this->coursemodule = get_coursemodule_from_id('leganto', $this->context->instanceid, 0, false, MUST_EXIST);
return $this->coursemodule;
}
return null;
}
/**
* Get context module.
*
* @return context
*/
public function get_context() {
return $this->context;
}
/**
* Get the current course.
*
* @return mixed stdClass|null The course.
*/
public function get_course() {
global $DB;
if ($this->course) {
return $this->course;
}
if (!$this->context) {
return null;
}
$params = ['id' => $this->get_course_context()->instanceid];
$this->course = $DB->get_record('course', $params, '*', MUST_EXIST);
return $this->course;
}
/**
* Get the name of the current module.
*
* @return string The module name (Leganto reading list).
*/
protected function get_module_name() {
if (isset(self::$modulename)) {
return self::$modulename;
}
self::$modulename = get_string('modulename', 'leganto');
return self::$modulename;
}
/**
* Get the plural name of the current module.
*
* @return string The module name plural (Leganto reading lists).
*/
protected function get_module_name_plural() {
if (isset(self::$modulenameplural)) {
return self::$modulenameplural;
}
self::$modulenameplural = get_string('modulenameplural', 'leganto');
return self::$modulenameplural;
}
/**
* View a link to go back to the previous page. Uses url parameters returnaction and returnparams.
*
* @return string
*/
protected function view_return_links() {
$returnaction = optional_param('returnaction', '', PARAM_ALPHA);
$returnparams = optional_param('returnparams', '', PARAM_TEXT);
$params = [];
$returnparams = str_replace('&', '&', $returnparams);
parse_str($returnparams, $params);
$newparams = ['id' => $this->get_course_module()->id, 'action' => $returnaction];
$params = array_merge($newparams, $params);
$url = new moodle_url('/mod/leganto/view.php', $params);
return $this->get_renderer()->single_button($url, get_string('back'), 'get');
}
/**
* Load and cache the admin config for the leganto module.
*
* @return stdClass The plugin config.
*/
public function get_admin_config() {
global $CFG;
if ($this->adminconfig) {
return $this->adminconfig;
}
$this->adminconfig = get_config('leganto');
// Clean up Alma API URL if necessary.
$search = ['http://', 'https://', get_string('apiurl_default', 'leganto')];
$baseurl = trim(str_ireplace($search, '', $this->adminconfig->apiurl), '/');
if (!empty($baseurl)) {
$slashpos = strpos($baseurl, '/');
if ($slashpos !== false) {
$baseurl = substr_replace($baseurl, '', $slashpos);
}
$this->adminconfig->apiurl = 'https://' . $baseurl;
} else {
$this->adminconfig->apiurl = '';
}
// Remove database prefix from Alma course code table name if present.
if (isset($this->adminconfig->codetable)) {
$this->adminconfig->codetable = str_replace($CFG->prefix, '', $this->adminconfig->codetable);
}
return $this->adminconfig;
}
/**
* Check whether the Alma API has been fully configured.
*
* @return bool True if fully configured, else false.
*/
private function is_api_configured() {
if ($this->apiconfigured) {
return true;
}
$adminconfig = $this->get_admin_config();
$settings = [
'apiurl',
'apikey',
];
$message = [];
foreach ($settings as $setting) {
if (empty($adminconfig->$setting)) {
$message[] = get_string('settingnotconfigured', 'leganto', $setting);
}
}
if (!empty($message)) {
$message[] = get_string('apinotconfigured', 'leganto');
mtrace(implode("\n", $message));
return false;
}
return $this->apiconfigured = true;
}
/**
* Call a specified Alma API method, passing the parameters provided.
*
* @param int $method The API method to call.
* @param string $q An Alma course search query string.
* @param string $courseid The identifier of an Alma course.
* @param string $listid The identifier of a reading list.
* @param string $citationid The identifier of a citation.
* @param array $params An array of additional params to pass.
* @param bool $cached Whether to return cached data instead (if available).
* @return stdClass|bool The decoded JSON response, or false.
*/
private function call_api($method, $q = '', $courseid = '', $listid = '', $citationid = '', $params = [], $cached = false) {
// Start by checking that the API is configured.
if (!$this->is_api_configured()) {
return false;
}
// Make sure we have all the required data.
$debugdata = [];
if ($method == ALMA_GET_COURSES) {
if (empty($q)) {
$debugdata['method'] = 'Retrieve courses';
$debugdata['params'][] = 'search query';
}
} else if ($method == ALMA_GET_COURSE || $method == ALMA_GET_LIST || $method == ALMA_GET_CITATION) {
if (empty($courseid)) {
$debugdata['method'] = 'Retrieve course';
$debugdata['params'][] = 'course identifier';
}
if ($method == ALMA_GET_LIST || $method == ALMA_GET_CITATION) {
if (empty($listid)) {
$debugdata['method'] = 'Retrieve reading list';
$debugdata['params'][] = 'reading list identifier';
}
if ($method == ALMA_GET_CITATION) {
if (empty($citationid)) {
$debugdata['method'] = 'Retrieve citation';
$debugdata['params'][] = 'citation identifier';
}
}
}
} else {
debugging(get_string('invalidapimethod', 'leganto', $method), DEBUG_DEVELOPER);
}
if (!empty($debugdata)) {
$debugdata['params'] = implode(', ', $debugdata['params']);
debugging(get_string('insufficientapidata', 'leganto', $debugdata), DEBUG_DEVELOPER);
return false;
}
global $CFG;
require_once($CFG->libdir . '/filelib.php');
$adminconfig = $this->get_admin_config();
$path = '/almaws/v1/courses';
// Create a cache object to store Alma data.
$cache = cache::make('mod_leganto', 'listdata');
if ($method == ALMA_GET_COURSES) {
$params['q'] = $q;
$cachedid = $q;
} else {
$path .= '/' . $courseid;
$cachedid = $courseid;
if ($method == ALMA_GET_LIST || $method == ALMA_GET_CITATION) {
$path .= '/reading-lists/' . $listid;
$cachedid = $listid;
if ($method == ALMA_GET_CITATION) {
$path .= '/citations/' . $citationid;
$cachedid = $citationid;
}
}
}
if ($cached && ($json = $cache->get($cachedid))) {
return $json;
}
// Prepare cURL request data.
$curl = new curl;
$header = [
'Accept: application/json',
'Authorization: apikey ' . $adminconfig->apikey,
];
$options = [
'CURLOPT_TIMEOUT' => 30,
];
$curl->setHeader($header);
$curl->setopt($options);
$url = new moodle_url($adminconfig->apiurl . $path);
// Submit request to Alma API.
$response = $curl->get($url->out(), $params);
// Check response and log any errors.
$curlinfo = $curl->get_info();
$json = json_decode($response);
// If all is well, return data.
if ($curlinfo['http_code'] == 200 && !empty($json)) {
if ($method != ALMA_GET_COURSES) {
if (!$cache->set($cachedid, $json)) {
debugging('Unable to cache data retrieved from Alma API.', DEBUG_DEVELOPER);
}
}
return $json;
}
// Check for invalid JSON and/or API errors, and log.
if (empty($json)) {
debugging('Invalid JSON response.', DEBUG_DEVELOPER);
} else {
debugging('Unknown error.', DEBUG_DEVELOPER);
}
debugging('HTTP code: ' . $curlinfo['http_code'], DEBUG_DEVELOPER);
debugging('API response: ' . $response, DEBUG_DEVELOPER);
// Fall back on cached data if available.
debugging('Attempting to use cached data (if available).', DEBUG_DEVELOPER);
if ($json = $cache->get($cachedid)) {
return $json;
}
debugging('Alma API and cached data unavailable.', DEBUG_DEVELOPER);
return false;
}
/**
* Fetch all Alma course codes associated with a given Moodle course.
*
* @param stdClass $course The data object for the course.
* @param bool $child Whether or not this is a meta child course.
* @return array An array of Alma course codes.
*/
private function get_codes($course, $child = false) {
global $DB;
$adminconfig = $this->get_admin_config();
if ($adminconfig->codesource == 'codetable') {
$codetable = $adminconfig->codetable;
$codecolumn = $adminconfig->codecolumn;
$coursecolumn = $adminconfig->coursecolumn;
$courseattribute = $course->{$adminconfig->courseattribute};
$codes = [];
if ($records = $DB->get_records($codetable, [$coursecolumn => $courseattribute])) {
foreach ($records as $record) {
$codes[] = $record->$codecolumn;
}
}
} else if ($adminconfig->codesource == 'shortname') {
$codes = $this->extract_codes($course->shortname);
}
// Try ID number as fallback if no code found so far, regardless of code source specified in admin config.
if ($adminconfig->codesource == 'idnumber' || empty($codes)) {
$codes = $this->extract_codes($course->idnumber);
}
// Check for additional codes in meta child courses (if enabled in site config).
if ($adminconfig->includechildcodes && !$child) {
if ($childcourses = $this->get_child_courses($course->id)) {
foreach ($childcourses as $childcourse) {
$codes = array_merge($codes, $this->get_codes($childcourse, true));
}
}
}
return array_filter($codes);
}
/**
* Extract one or more Alma course codes from a given source string.
*
* @param string $source A string containing one or more codes.
* @return array An array of Alma course codes.
*/
private function extract_codes($source) {
$adminconfig = $this->get_admin_config();
if ($coderegex = $adminconfig->coderegex) {
preg_match_all($coderegex, $source, $codes, PREG_PATTERN_ORDER);
$codes = (!empty($codes[1])) ? $codes[1] : $codes[0];
} else {
$codes = [$source];
}
$codes = array_unique($codes);
return $codes;
}
/**
* Determine whether the current course has any course meta link enrolment instances,
* and if it does, fetch the child courses.
*
* @param int $courseid The id of the current course.
* @return array An array of meta child course objects.
*/
private function get_child_courses($courseid) {
global $DB;
$childcourses = [];
$select = "enrol = 'meta' AND status = 0 AND courseid = $courseid";
if ($childcourseids = $DB->get_fieldset_select('enrol', 'customint1', $select)) {
foreach ($childcourseids as $childcourseid) {
$childcourses[] = get_course($childcourseid);
}
}
return $childcourses;
}
/**
* Retrieve all Leganto reading lists associated with the current course.
*
* @param stdClass $course The data for the current course.
* @param bool $resetcache Whether to reset cached lists.
* @return array An array of Leganto reading list objects.
*/
public function get_lists($course, $resetcache = false) {
$adminconfig = $this->get_admin_config();
$codes = $this->get_codes($course);
// Check if the course idnumber or shortname contains a year reference.
$year = '';
if ($yearregex = $adminconfig->yearregex) {
if (preg_match($yearregex, $course->idnumber, $year) || preg_match($yearregex, $course->shortname, $year)) {
$year = (!empty($year[1])) ? $year[1] : $year[0];
}
}
// Initialise the cache if necessary.
if ($resetcache) {
$cache = cache::make('mod_leganto', 'listdata');
}
$lists = [];
foreach ($codes as $code) {
// Build the course search query string for the Alma API request.
$query = 'code~' . $code;
if (!empty($year)) {
$query .= '%20AND%20year~' . $year;
}
if (!($courses = $this->call_api(ALMA_GET_COURSES, $query)) || $courses->total_record_count == 0) {
continue;
}
foreach ($courses->course as $almacourse) {
$courseid = $almacourse->id;
if (!$coursedata = $this->call_api(ALMA_GET_COURSE, '', $courseid, '', '', ['view' => 'full'])) {
continue;
}
if (empty($coursedata->reading_lists->reading_list)) {
continue;
}
foreach ($coursedata->reading_lists->reading_list as $list) {
$list->courseid = $courseid;
$listname = trim($list->name);
$lists[$listname] = $list;
if ($resetcache) {
if (!$cache->set($list->id, $list)) {
debugging('Unable to reset cache, list data may be stale.', DEBUG_DEVELOPER);
}
}
}
}
}
// Sort the lists by name.
ksort($lists, SORT_NATURAL | SORT_FLAG_CASE);
return $lists;
}
/**
* Retrieve the specified Leganto reading list data from the Alma API.
*
* @param string $courseid The identifier of the parent Alma course.
* @param string $listid The identifier of the required Leganto list.
* @param bool $cached Whether to return cached data if available.
* @return stdClass|bool A JSON object containing the data, or false.
*/
private function get_list_data($courseid, $listid, $cached = false) {
if (!$list = $this->call_api(ALMA_GET_LIST, '', $courseid, $listid, '', ['view' => 'full'], $cached)) {
return false;
}
return $list;
}
/**
* Fetch the data for a Leganto reading list section, given a list object and a section identifier.
*
* @param stdClass $list A JSON object containing the reading list data.
* @param string $sectionid The identifier of the required section.
* @return stdClass|bool An object containing the section data, or false.
*/
public function get_section_data($list, $sectionid) {
$citationcount = 0;
if (empty($list->citations->citation)) {
return false;
}
foreach ($list->citations->citation as $citation) {
if ($citation->section_info->id != $sectionid) {
continue;
}
// Get the section details (for the first match only).
if ($citationcount < 1) {
$section = $citation->section_info;
if (!empty($section->description)) {
$section->description = html_writer::div($section->description, 'sectiondesc');
}
}
$citationcount++;
}
if (!empty($section)) {
$section->citationcount = $citationcount;
return $section;
}
return false;
}
/**
* Fetch the data for a Leganto citation, given a reading list object and a citation identifier.
*
* @param stdClass $list A JSON object containing the reading list data.
* @param string $citationid The identifier of the required citation.
* @param string $parentpath A path comprising the course, list and section identifiers.
* @param int $display The list display mode (inline or separate page).
* @return stdClass|bool An object containing the citation data, or false.
*/
public function get_citation_data($list, $citationid, $parentpath = '', $display = null) {
global $OUTPUT;
if (empty($list->citations->citation)) {
return false;
}
foreach ($list->citations->citation as $citation) {
if ($citation->id != $citationid) {
continue;
}
if (!empty($citation->metadata->chapter_title)) {
$title = $citation->metadata->chapter_title;
if (!empty($citation->metadata->chapter_author)) {
$citation->author = html_writer::span($citation->metadata->chapter_author, 'citationauthor');
if (!empty($citation->metadata->author)) {
$citation->bookauthor = html_writer::span($citation->metadata->author, 'citationbookauthor');
}
}
if (!empty($citation->metadata->title)) {
$citation->booktitle = html_writer::span($citation->metadata->title, 'citationbooktitle');
}
} else {
$title = !empty($citation->metadata->title) ? $citation->metadata->title : $citation->metadata->article_title;
}
$headinglevel = !is_null($display) && $display == LEGANTO_DISPLAY_PAGE ? 3 : 5;
$citation->title = $OUTPUT->heading($title, $headinglevel, 'citationtitle h' . ($headinglevel + 1));
if (!empty($citation->leganto_permalink)) {
$permalink = str_replace('auth=local', 'auth=SAML', $citation->leganto_permalink);
if (!empty($this->adminconfig->usenewui)) {
$permalink = str_replace('readinglist', 'nui', $permalink);
}
$linkaction = new popup_action('click', $permalink, 'popup', ['width' => 1024, 'height' => 768]);
$linktitle = get_string('viewcitation', 'leganto');
$linkclass = !is_null($display) && $display == LEGANTO_DISPLAY_PAGE ? ' fa-lg' : '';
$citation->permalink = $OUTPUT->action_link($permalink, ' ', $linkaction,
['class' => 'fa fa-external-link citationlink' . $linkclass, 'title' => $linktitle]);
}
if (empty($citation->author) && !empty($citation->metadata->author)) {
$citation->author = html_writer::span($citation->metadata->author, 'citationauthor');
}
if (!empty($citation->metadata->edition)) {
$citation->edition = html_writer::span($citation->metadata->edition, 'citationedition');
}
if (!empty($citation->metadata->publisher)) {
$citation->publisher = html_writer::span($citation->metadata->publisher, 'citationpublisher');
}
if (!empty($citation->metadata->publication_date)) {
$citation->published = html_writer::span($citation->metadata->publication_date, 'citationpublished');
}
if (empty($citation->booktitle) && !empty($citation->metadata->chapter)) {
$citation->chapter = html_writer::span($citation->metadata->chapter, 'citationchapter');
}
if (!empty($citation->secondary_type->desc)) {
$citation->resourcetype = html_writer::span($citation->secondary_type->desc, 'citationresourcetype');
}
if (!empty($citation->citation_tags->citation_tag)) {
$citation->tags = [];
foreach ($citation->citation_tags->citation_tag as $tag) {
if (!empty($tag->type->value) && $tag->type->value == 'PUBLIC' && !empty($tag->value->desc)) {
$citation->tags[] = html_writer::span($tag->value->desc, 'citationtag');
}
}
}
if (!empty($citation->public_note)) {
$citation->note = html_writer::span(ucfirst(trim($citation->public_note)), 'citationnote');
}
if (!empty($citation->metadata->source)) {
$buttonhref = $citation->metadata->source;
$buttonlabel = get_string('viewonline', 'leganto');
$buttonaction = new popup_action('click', $buttonhref, 'popup', ['width' => 1024, 'height' => 768]);
// The URL in the popup_action object is encoded, but needs to be un-encoded!
$buttonaction->jsfunctionargs['url'] = $buttonhref;
$buttonattributes = ['class' => 'citationsource', 'title' => $title];
$citation->source = $OUTPUT->action_link($buttonhref, $buttonlabel, $buttonaction, $buttonattributes);
}
if (!empty($parentpath)) {
$citation->path = $parentpath . '_citation-' . $citation->id;
}
return $citation;
}
return false;
}
/**
* Extract all selected citations from submitted leganto module config form data and return as a comma separated list.
*
* @param stdClass $formdata The config form data submitted.
* @return string A JSON encoded list of selected citations.
*/
public function get_citations($formdata) {
$citationpathregex = '/^course-[0-9]{16,}_list-[0-9]{16,}_section-[0-9]{16,}_citation-[0-9]{16,}$/';
$selected = [];
foreach ($formdata as $name => $value) {
if (preg_match($citationpathregex, $name) && $value == 1) {
$path = $this->explode_citation_path($name);
$selected = array_merge_recursive($selected, $path);
}
}
if (!$citations = json_encode($selected)) {
return '';
}
return $citations;
}
/**
* Fetch the list of previously selected citations for the current leganto instance.
*
* @return array A list of citation paths.
*/
public function get_selected_citations() {
if (!$this->has_instance()) {
return [];
}
if (!$config = $this->get_instance()->citations) {
return [];
}
if (!$tree = json_decode($config)) {
return [];
}
$paths = [];
foreach ($tree as $coursekey => $coursetree) {
foreach ($coursetree as $listkey => $listtree) {
foreach ($listtree as $sectionkey => $citations) {
foreach ($citations as $citation) {
$paths[] = $coursekey . '_' . $listkey . '_' . $sectionkey . '_' . $citation;
}
}
}
}
return $paths;
}
/**
* Given a JSON encoded list of selected citations, construct an array representing a tree
* structure of the selection, and use this to generate the HTML output to display the custom list.
*
* @param string $citations A JSON encoded list of selected citations.
* @param int $display The list display mode (inline or separate page).
* @return string The final HTML output to display the custom reading list.
*/
public function get_list_html($citations, $display) {
if (empty($citations) || !($tree = json_decode($citations))) {
return '';
}
$html = '';
foreach ($tree as $coursekey => $coursetree) {
$courseid = str_replace('course-', '', $coursekey);
foreach ($coursetree as $listkey => $listtree) {
$listid = str_replace('list-', '', $listkey);
// Fetch list data, from cache if available.
$listdata = $this->get_list_data($courseid, $listid, true);
$html .= $this->build_list_elements($listdata, $listtree, $display);
}
}
return $this->condense_whitespace($html);
}
/**
* Return an array representing the full path (i.e. course => list => section => citation) for a given citation.
*
* @param string $citationpath The path string of a selected citation.
* @return array A partial tree structure representation of the path components.
*/
private function explode_citation_path($citationpath) {
$parts = explode('_', $citationpath);
$partscount = count($parts);
$path = [$parts[$partscount - 1]];
for ($i = $partscount - 2; $i >= 0; $i--) {
$path = [$parts[$i] => $path];
}
return $path;
}
/**
* Given a list's JSON object and an array representing a tree structure of the selected citations
* and their parent sections, recursively assemble the HTML to display the custom list.
*
* @param stdClass $list A JSON object containing the reading list data.
* @param mixed $elements A tree structure representing the selected citations by section.
* @param int $display The configured display mode for the list (inline or separate page).
* @param string $html The HTML output that has been generated from previous iterations.
* @param bool $wascitation Whether or not the previous element was a citation.
* @return string The HTML output for the custom list.
*/
private function build_list_elements($list, $elements, $display, &$html = '', $wascitation = false) {
foreach ($elements as $elementkey => $element) {
if (preg_match($this->sectionidregex, $elementkey)) {
// This is a section.
$sectionid = str_replace('section-', '', $elementkey);
if ($wascitation) {
// If previous element was a citation, close the unordered list.
$html .= html_writer::end_tag('ul');
}
// Open a section container and output the heading details.
$html .= html_writer::start_div('listsection', ['id' => $sectionid]);
$html .= $this->get_section_html($list, $sectionid, $display, count($element));
// Remember that this was a section heading.
$wascitation = false;
// Then process any sub-elements it contains.
if (is_array($element)) {
$this->build_list_elements($list, $element, $display, $html, $wascitation);
}
// Close the section container.
$html .= html_writer::end_div();
} else if (preg_match($this->citationidregex, $element)) {
// This is a citation.
$citationid = str_replace('citation-', '', $element);
if (!$wascitation) {
// If previous element was a section heading, open an unordered list.
$html .= html_writer::start_tag('ul', ['class' => 'citations']);
}
// Output the citation details.
$html .= $this->get_citation_html($list, $citationid, $display);
// Remember that this was a citation.
$wascitation = true;
}
}
if ($wascitation) {
// If the last element was a citation, close the unordered list.
$html .= html_writer::end_tag('ul');
}