-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfilter.php
796 lines (711 loc) · 33.2 KB
/
filter.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
<?php
/*
* __________________________________________________________________________
*
* PoodLL filter for Moodle 2.9 and above
*
* This filter will replace any PoodLL filter string with the appropriate PoodLL widget
*
* @package filter
* @subpackage poodll
* @copyright 2012 Justin Hunt {@link http://www.poodll.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* __________________________________________________________________________
*/
class filter_poodll extends moodle_text_filter {
protected $adminconfig = null;
protected $courseconfig = null;
/**
* Apply the filter to the text
*
* @see filter_manager::apply_filter_chain()
* @param string $text to be processed by the text
* @param array $options filter options
* @return string text after processing
* @return string text after processing
*/
public function filter($text, array $options = array()) {
if (!is_string($text)) {
// non string data can not be filtered anyway
return $text;
}
$newtext = $text;
//No links or poodll curlys then .. bail
$havelinks = !(stripos($text, '</a>') === false);
$have_poodll_curlys = (strpos($text, '{POODLL:') !== false);
if (!$havelinks) {
if (!$have_poodll_curlys) {
return $text;
}
}
//get config
$this->adminconfig = get_config('filter_poodll');
//if text has links, lets parse them
if ($havelinks) {
//get handle extensions
$exts = \filter_poodll\filtertools::fetch_extensions();
$handleexts = array();
foreach ($exts as $ext) {
if ($ext != 'youtube' && $this->fetchconf('handle' . $ext)) {
$handleexts[] = $ext;
}
}
//do all the non youtube extensions in one foul swoop
if (!empty($handleexts)) {
$handleextstring = implode('|', $handleexts);
//$oldsearch = '/<a\s[^>]*href="([^"#\?]+\.(' . $handleextstring. '))(\?d=([\d]{1,4})x([\d]{1,4}))?"[^>]*>([^>]*)<\/a>/is';
$search = '/<a\s[^>]*href="([^"#\?]+\.(' . $handleextstring . '))(.*?)"[^>]*>([^>]*)<\/a>/is';
$newtext = preg_replace_callback($search, 'self::filter_poodll_allexts_callback', $newtext);
}
//check for legacy pdl links
$search = '/<a\s[^>]*href="([^"#\?]+\.(pdl))(.*?)"[^>]*>([^>]*)<\/a>/is';
$newtext = preg_replace_callback($search, 'self::filter_poodll_pdl_callback', $newtext);
//check for youtube
if ($this->fetchconf('handleyoutube')) {
$search =
'/<a\s[^>]*href="(?:https?:\/\/)?(?:www\.)?youtu(?:\.be|be\.com)\/(?:watch\?v=|v\/)?([\w-]{10,})(?:.*?)<\/a>/is';
$newtext = preg_replace_callback($search, 'self::filter_poodll_youtube_callback', $newtext);
}
}// end of if $havelinks
//if text has poodll curly brackets, lets parse
if ($have_poodll_curlys) {
//check for poodll curly brackets notation
$search = '/{POODLL:.*?}/is';
if (!is_string($text)) {
// non string data can not be filtered anyway
return $text;
}
$newtext = preg_replace_callback($search, 'self::filter_poodll_process', $newtext);
}//end of if has poodl curlys
//return the correct thing to wherever called us
if (is_null($newtext) or $newtext === $text) {
// error or not filtered
return $text;
}
return $newtext;
}
private function fetchconf($prop) {
global $COURSE;
//I don't know why we need this whole courseconfig business.
//we are supposed to be able to just call $this->localconfig / $this->localconfig[$propertyname]
//as per here:https://docs.moodle.org/dev/Filters#Local_configuration , but its always empty
//at least at course context, in mod context it works ...
//I just gave up and do it myself and stuff it in $this->courseconfig . bug?? Justin 20150106
if ($this->localconfig && !empty($this->localconfig)) {
$this->courseconfig = $this->localconfig;
}
if ($this->courseconfig === null) {
$cache = \cache::make_from_params(\cache_store::MODE_REQUEST, 'filter_poodll', 'local_config');
$contextid = context_course::instance($COURSE->id)->id;
$data = $cache->get($contextid);
if ($data === false) {
$data = filter_get_local_config('poodll', $contextid);
$cache->set($contextid, $data);
}
$this->courseconfig = $data;
}
if ($this->courseconfig && isset($this->courseconfig[$prop]) && $this->courseconfig[$prop] != 'sitedefault') {
return $this->courseconfig[$prop];
} else {
return isset($this->adminconfig->{$prop}) ? $this->adminconfig->{$prop} : false;
}
}
/**
* Replace youtube links with player
*
* @param $link
* @return string
*/
private function filter_poodll_youtube_callback($link) {
return $this->filter_poodll_process($link, 'youtube');
}
/**
* Replace links with player/widget
*
* @param $link
* @return string
*/
private function filter_poodll_allexts_callback($link) {
return $this->filter_poodll_process($link, $link[2]);
}
/**
* Replace legacy pdl links with widget
*
* @param $link
* @return string
*/
function filter_poodll_pdl_callback($link) {
global $CFG;
//strip the .pdl extension
$len = strlen($link[1]);
$trimpoint = strpos($link[1], ".pdl");
$key = substr($link[1], 0, $trimpoint);
if (strpos($key, "https://") === 0 && $len > 12) {
$key = substr($key, 8);
} else if (strpos($key, "http://") === 0 && $len > 11) {
$key = substr($key, 7);
}
$fstring = '';
//see if there is a parameter to this widget
$pos = strpos($key, "_");
$param = "";
//if yes, trim it off the key and get its value
if ($pos) {
$param = substr($key, $pos + 1);
$key = substr($key, 0, $pos);
}
//depending on the widget, make up a filter string
switch ($key) {
case "audiorecorder":
$fstring = "{POODLL:type=audiorecorder}";
break; //not implemented
case "videorecorder":
$fstring = "{POODLL:type=videorecorder}";
break; //not implemented
case "whiteboardsimple":
$fstring = "{POODLL:type=whiteboard,mode=simple,standalone=true}";
break;//not implemented
case "whiteboardfull":
$fstring = "{POODLL:type=whiteboard,mode=normal,standalone=true}";
break;//not implemented
case "snapshot":
$fstring = "{POODLL:type=snapshot}";
break;
case "stopwatch":
$fstring = "{POODLL:type=stopwatch}";
break;
case "dice":
$fstring = "{POODLL:type=dice,dicecount=$param}";
break;
case "calculator":
$fstring = "{POODLL:type=calculator}";
break;
case "countdown":
$fstring = "{POODLL:type=countdown,initseconds=$param}";
break;
case "counter":
$fstring = "{POODLL:type=counter}";
break;
case "flashcards":
$fstring = "{POODLL:type=flashcards,cardset=$param}";
break;
}
//resolve the string and return it
if (!empty($fstring)) {
return self::filter_poodll_process(array($fstring));
} else {
return '';
}
}
/*
* Main callback function , exists outside of class definition(because its a callback ...)
*
*/
function filter_poodll_process(array $link, $ext = false) {
global $CFG, $COURSE, $USER, $PAGE, $DB;
$lm = new \filter_poodll\licensemanager();
$registration_status = $lm->validate_license();
if ($registration_status != \filter_poodll\licensemanager::FILTER_POODLL_IS_REGISTERED) {
return $lm->fetch_unregistered_content($registration_status);
}
//get the Poodll configs
$conf = get_object_vars(get_config('filter_poodll'));
//setup context, but only fetch it when we need to
$context = false;
//we use this to see if its a web service calling this,
//in which case we return the alternate content
$is_webservice = false;
$climode = defined('CLI_SCRIPT') && CLI_SCRIPT;
if (!$climode) {
$is_webservice = strpos($PAGE->url, $CFG->wwwroot . '/webservice/') === 0;
}
//get our filter props
if ($ext) {
$filterprops = \filter_poodll\filtertools::fetch_filter_properties_fromurl($link, $ext, $is_webservice);
} else {
$filterprops = \filter_poodll\filtertools::fetch_filter_properties($link[0]);
}
//if we have no props, quit
if (empty($filterprops)) {
return "";
}
//if this was a link that was filtered, and the content was not text , we don't like it
//it most likely should be let alone or hidden as it is an icon to accompany the player
//in teh case of attachements/resources/podcasts etc
if ($ext) {
if (empty($filterprops['TITLE'])) {
return $link[0];
}
}
//if this was a link but it had a "nopoodll" class on it then we ought to ignore it
if ($ext) {
if (preg_match('/class="[^"]*nopoodll/i', $link[0])) {
return $link[0];
}
}
//if we want to ignore the filter (for "how to use poodll" or "cut and paste" this style use) we let it go
//to use this, make the last parameter of the filter passthrough=1
if (!empty($filterprops['passthrough'])) {
return str_replace(",passthrough=1", "", $link[0]);
}
//set a default end tag of none
$endtag = false;
//determine which template we are using
//If we have an extension then it is from link
//get our template info
if ($ext) {
//in a special case it is possible for the player to be specified in the url params
//however usually it will be from preferences
if (array_key_exists('player', $filterprops) && !empty($filterprops['player'])) {
$playerkey = $filterprops['player'];
} else {
$playerkey = $this->fetchconf('useplayer' . $ext);
}
$tempindex = 0;
$templatenumbers = \filter_poodll\filtertools::fetch_template_indexes($conf);
foreach ($templatenumbers as $templatenumber) {
if ($conf['templatekey_' . $templatenumber] == $playerkey) {
$tempindex = $templatenumber;
break;
}
}
if (!$tempindex) {
return "";
}
} else {
//else its from a poodll filter string
for ($tempindex = 1; $tempindex <= $conf['templatecount']; $tempindex++) {
if ($filterprops['type'] == $conf['templatekey_' . $tempindex]) {
break;
} else if ($filterprops['type'] == $conf['templatekey_' . $tempindex] . '_end') {
$endtag = true;
break;
}
}
//no key could be found if got all the way to templatecount
if ($tempindex == $conf['templatecount'] + 1) {
return '';
}
}
//fetch our template
if ($endtag) {
$poodlltemplate = $conf['templateend_' . $tempindex];
$alternate_content = $conf['templatealternate_' . $tempindex];
} else {
$poodlltemplate = $conf['template_' . $tempindex];
$alternate_content = $conf['templatealternate_end_' . $tempindex];
}
//fetch alternate content
$alternate_content = $conf['templatealternate_' . $tempindex];
//fetch dataset info
$dataset_body = $conf['dataset_' . $tempindex];
$dataset_vars = $conf['datasetvars_' . $tempindex];
//js custom script
//we really just want to be sure anything that appears in custom script
//is stored in $filterprops and passed to js. we dont replace it server side because
//of caching
$js_custom_script = $conf['templatescript_' . $tempindex];
//if we have a download variable in the template
$filterprops['CANDOWNLOAD'] = 'hide';
if ($CFG->filter_poodll_download_media_ok) {
$context = context_course::instance($COURSE->id);
if (has_capability('filter/poodll:candownloadmedia', $context)) {
$filterprops['CANDOWNLOAD'] = 'filter_poodll_download_button';
};
}
//replace the specified names with spec values
foreach ($filterprops as $name => $value) {
$poodlltemplate = str_replace('@@' . $name . '@@', $value, $poodlltemplate);
$dataset_vars = str_replace('@@' . $name . '@@', $value, $dataset_vars);
$alternate_content = str_replace('@@' . $name . '@@', $value, $alternate_content);
}
//fetch defaults for this template
$defaults = $conf['templatedefaults_' . $tempindex];
if (!empty($defaults)) {
$defaults = "{POODLL:" . $defaults . "}";
$defaultprops = \filter_poodll\filtertools::fetch_filter_properties($defaults);
//replace our defaults, if not spec in the the filter string
if (!empty($defaultprops)) {
foreach ($defaultprops as $name => $value) {
if (!array_key_exists($name, $filterprops)) {
//if we have options as defaults, lets just take the first one
if (strpos($value, '|') !== false) {
$valuearray = explode('|', $value);
$value = $valuearray[0];
}
$poodlltemplate = str_replace('@@' . $name . '@@', strip_tags($value), $poodlltemplate);
$dataset_vars = str_replace('@@' . $name . '@@', strip_tags($value), $dataset_vars);
$alternate_content = str_replace('@@' . $name . '@@', strip_tags($value), $alternate_content);
//stash for using in JS later
$filterprops[$name] = $value;
}
}
}
}
//If we have autoid lets deal with that
$autoid = 'filterpoodll_' . time() . (string) rand(100, 32767);
$poodlltemplate = str_replace('@@AUTOID@@', $autoid, $poodlltemplate);
$alternate_content = str_replace('@@AUTOID@@', $autoid, $alternate_content);
//stash this for passing to js
$filterprops['AUTOID'] = $autoid;
//If we need a Cloud Poodll token, lets fetch it
if (strpos($poodlltemplate, '@@CLOUDPOODLLTOKEN@@') &&
!empty($conf['cpapiuser']) &&
!empty($conf['cpapisecret'])) {
$lm = new \filter_poodll\licensemanager();
$tokenobject = $lm->fetch_token($conf['cpapiuser'], $conf['cpapisecret']);
if(isset($tokenobject->token)){
$token=$tokenobject->token;
}else{
$token = false;
}
if(!$token){$token = 'NO_TOKEN RETRIEVED';}
$poodlltemplate = str_replace('@@CLOUDPOODLLTOKEN@@', $token, $poodlltemplate);
//stash this for passing to js
$filterprops['CLOUDPOODLLTOKEN'] = $token;
}
//If template requires a MOODLEPAGEID lets give them one
//this is a bit redundant now it can be done now with @@URLPARAM:id@@
$moodlepageid = optional_param('id', 0, PARAM_INT);
$poodlltemplate = str_replace('@@MOODLEPAGEID@@', $moodlepageid, $poodlltemplate);
$dataset_vars = str_replace('@@MOODLEPAGEID@@', $moodlepageid, $dataset_vars);
$alternate_content = str_replace('@@MOODLEPAGEID@@', $moodlepageid, $alternate_content);
//stash this for passing to js
$filterprops['MOODLEPAGEID'] = $moodlepageid;
//we should stash our wwwroot too
$poodlltemplate = str_replace('@@WWWROOT@@', $CFG->wwwroot, $poodlltemplate);
$dataset_vars = str_replace('@@WWWROOT@@', $CFG->wwwroot, $dataset_vars);
$alternate_content = str_replace('@@WWWROOT@@', $CFG->wwwroot, $alternate_content);
//actually this is available from JS anyway M.cfg.wwwroot . But lets make it easy for people
$filterprops['WWWROOT'] = $CFG->wwwroot;
//if we have urlparam variables e.g @@URLPARAM:id@@
if (strpos($poodlltemplate . ' ' . $dataset_vars . ' ' . $alternate_content . ' ' . $js_custom_script, '@@URLPARAM:') !==
false) {
$urlparamstubs = explode('@@URLPARAM:', $poodlltemplate);
$dv_stubs = explode('@@URLPARAM:', $dataset_vars);
if ($dv_stubs) {
$urlparamstubs = array_merge($urlparamstubs, $dv_stubs);
}
$js_stubs = explode('@@URLPARAM:', $js_custom_script);
if ($js_stubs) {
$urlparamstubs = array_merge($urlparamstubs, $js_stubs);
}
$alt_stubs = explode('@@URLPARAM:', $alternate_content);
if ($alt_stubs) {
$urlparamstubs = array_merge($urlparamstubs, $alt_stubs);
}
//URL Param Props
$count = 0;
foreach ($urlparamstubs as $propstub) {
//we don't want the first one, its junk
$count++;
if ($count == 1) {
continue;
}
//init our prop value
$propvalue = false;
//fetch the property name
//user can use any case, but we work with lower case version
$end = strpos($propstub, '@@');
$urlprop = substr($propstub, 0, $end);
if (empty($urlprop)) {
continue;
}
//check if it exists in the params to the url and if so, set it.
$propvalue = optional_param($urlprop, '', PARAM_TEXT);
$poodlltemplate = str_replace('@@URLPARAM:' . $urlprop . '@@', $propvalue, $poodlltemplate);
$dataset_vars = str_replace('@@URLPARAM:' . $urlprop . '@@', $propvalue, $dataset_vars);
$alternate_content = str_replace('@@URLPARAM:' . $urlprop . '@@', $propvalue, $alternate_content);
//stash this for passing to js
$filterprops['URLPARAM:' . $urlprop] = $propvalue;
}//end of for each
}//end of if we have@@URLPARAM
//if we have course variables e.g @@COURSE:ID@@
if (strpos($poodlltemplate . ' ' . $dataset_vars . ' ' . $alternate_content . ' ' . $js_custom_script, '@@COURSE:') !==
false) {
$coursevars = get_object_vars($COURSE);
//custom fields
if(class_exists('\core_customfield\handler')) {
$handler = \core_customfield\handler::get_handler('core_course', 'course');
$customfields = $handler->get_instance_data($filterprops['courseid']);
foreach ($customfields as $customfield) {
if (empty($customfield->get_value())) {
continue;
}
$shortname = $customfield->get_field()->get('shortname');
$coursevars[$shortname] = $customfield->get_value();
}
}
$coursepropstubs = explode('@@COURSE:', $poodlltemplate);
$d_stubs = explode('@@COURSE:', $dataset_vars);
if ($d_stubs) {
$coursepropstubs = array_merge($coursepropstubs, $d_stubs);
}
$j_stubs = explode('@@COURSE:', $js_custom_script);
if ($j_stubs) {
$coursepropstubs = array_merge($coursepropstubs, $j_stubs);
}
$alt_stubs = explode('@@COURSE:', $alternate_content);
if ($alt_stubs) {
$coursepropstubs = array_merge($coursepropstubs, $alt_stubs);
}
//Course Props
$profileprops = false;
$count = 0;
foreach ($coursepropstubs as $propstub) {
//we don't want the first one, its junk
$count++;
if ($count == 1) {
continue;
}
//init our prop value
$propvalue = false;
//fetch the property name
//user can use any case, but we work with lower case version
$end = strpos($propstub, '@@');
$courseprop_allcase = substr($propstub, 0, $end);
$courseprop = strtolower($courseprop_allcase);
//check if it exists in course
if (array_key_exists($courseprop, $coursevars)) {
$propvalue = $coursevars[$courseprop];
} else if ($courseprop == 'contextid') {
if (!$context) {
$context = context_course::instance($COURSE->id);
}
if ($context) {
$propvalue = $context->id;
}
}
//if we have a propname and a propvalue, do the replace
if (!empty($courseprop) && !empty($propvalue)) {
$poodlltemplate = str_replace('@@COURSE:' . $courseprop_allcase . '@@', $propvalue, $poodlltemplate);
$dataset_vars = str_replace('@@COURSE:' . $courseprop_allcase . '@@', $propvalue, $dataset_vars);
$alternate_content = str_replace('@@COURSE:' . $courseprop_allcase . '@@', $propvalue, $alternate_content);
//stash this for passing to js
$filterprops['COURSE:' . $courseprop_allcase] = $propvalue;
}
}
}//end of if @@COURSE
//if we have user variables e.g @@USER:FIRSTNAME@@
//It is a bit wordy, because trying to avoid loading a lib
//or making a DB call if unneccessary
if (strpos($poodlltemplate . ' ' . $dataset_vars . ' ' . $js_custom_script . ' ' . $alternate_content, '@@USER:') !==
false) {
$uservars = get_object_vars($USER);
$userpropstubs = explode('@@USER:', $poodlltemplate);
$d_stubs = explode('@@USER:', $dataset_vars);
if ($d_stubs) {
$userpropstubs = array_merge($userpropstubs, $d_stubs);
}
$j_stubs = explode('@@USER:', $js_custom_script);
if ($j_stubs) {
$userpropstubs = array_merge($userpropstubs, $j_stubs);
}
$alt_stubs = explode('@@USER:', $alternate_content);
if ($alt_stubs) {
$userpropstubs = array_merge($userpropstubs, $alt_stubs);
}
//User Props
$profileprops = false;
$count = 0;
foreach ($userpropstubs as $propstub) {
//we don't want the first one, its junk
$count++;
if ($count == 1) {
continue;
}
//init our prop value
$propvalue = false;
//fetch the property name
//user can use any case, but we work with lower case version
$end = strpos($propstub, '@@');
$userprop_allcase = substr($propstub, 0, $end);
$userprop = strtolower($userprop_allcase);
//check if it exists in user, else look for it in profile fields
if (array_key_exists($userprop, $uservars)) {
$propvalue = $uservars[$userprop];
} else {
if (!$profileprops) {
require_once("$CFG->dirroot/user/profile/lib.php");
$profileprops = get_object_vars(profile_user_record($USER->id));
}
if ($profileprops && array_key_exists($userprop, $profileprops)) {
$propvalue = $profileprops[$userprop];
} else {
switch ($userprop) {
case 'picurl':
require_once("$CFG->libdir/outputcomponents.php");
global $PAGE;
$user_picture = new user_picture($USER);
$propvalue = $user_picture->get_url($PAGE);
break;
case 'pic':
global $OUTPUT;
$propvalue = $OUTPUT->user_picture($USER, array('popup' => true));
break;
}
}
}
//if we have a propname and a propvalue, do the replace
if (!empty($userprop) && !empty($propvalue)) {
//echo "userprop:" . $userprop . '<br/>propvalue:' . $propvalue;
$poodlltemplate = str_replace('@@USER:' . $userprop_allcase . '@@', $propvalue, $poodlltemplate);
$dataset_vars = str_replace('@@USER:' . $userprop_allcase . '@@', $propvalue, $dataset_vars);
$alternate_content = str_replace('@@USER:' . $userprop_allcase . '@@', $propvalue, $alternate_content);
//stash this for passing to js
$filterprops['USER:' . $userprop_allcase] = $propvalue;
}
}
}//end of of we @@USER
//if we have a dataset body
//we split the $data_vars string passed in by user (which should have had all the replacing done)
//into the vars array. This is passed to get_records_sql and the returned result is stored
//in filter props. If its a single record, its available to the body area.
//otherwise it needs to be accessewd from javascript in the DATASET variable
$filterprops['DATASET'] = false;
if ($dataset_body) {
$vars = array();
if ($dataset_vars) {
$vars = explode(',', $dataset_vars);
}
//turn numeric vars into numbers (not strings)
$query_vars = array();
for ($i = 0; $i < sizeof($vars); $i++) {
if (is_numeric($vars[$i])) {
$query_vars[] = intval($vars[$i]);
} else {
$query_vars[] = $vars[$i];
}
}
try {
$alldata = $DB->get_records_sql($dataset_body, $query_vars);
if ($alldata) {
$filterprops['DATASET'] = $alldata;
//replace the specified names with spec values, if its a one element array
if (sizeof($filterprops['DATASET']) == 1) {
$thedata = get_object_vars(array_pop($alldata));
foreach ($thedata as $name => $value) {
$poodlltemplate = str_replace('@@DATASET:' . $name . '@@', $value, $poodlltemplate);
$alternate_content = str_replace('@@DATASET:' . $name . '@@', $value, $alternate_content);
}
}
}
} catch (Exception $e) {
//do nothing;
}
}//end of if dataset
//if this is a web service/mobile app we just return the alternate content
if ($is_webservice && !empty($alternate_content)) {
return $alternate_content;
}
//If this is the end tag we don't need to subsequent CSS and JS stuff. We already did it.
if ($endtag) {
return $poodlltemplate;
}
//get the conf info we need for this template
$thescript = $conf['templatescript_' . $tempindex];
$defaults = $conf['templatedefaults_' . $tempindex];
$require_js = $conf['templaterequire_js_' . $tempindex];
$require_css = $conf['templaterequire_css_' . $tempindex];
$require_css = str_replace('@@WWWROOT@@', $CFG->wwwroot, $require_css);
$require_js = str_replace('@@WWWROOT@@', $CFG->wwwroot, $require_js);
//are we AMD and Moodle 2.9 or more?
$require_amd = $conf['template_amd_' . $tempindex];
//figure out if this is https or http. We don't want to scare the browser
if (!$climode && strpos($PAGE->url->out(), 'https:') === 0) {
$scheme = 'https:';
} else {
$scheme = 'http:';
}
//massage the js URL depending on schemes and rel. links etc. Then insert it
//with AMD we set these as dependencies, so we don't need this song and dance
if (!$require_amd) {
$filterprops['JSLINK'] = false;
if ($require_js) {
if (strpos($require_js, '//') === 0) {
$require_js = $scheme . $require_js;
} else if (strpos($require_js, '/') === 0) {
$require_js = $CFG->wwwroot . $require_js;
}
//for load method: NO AMD
$PAGE->requires->js(new moodle_url($require_js));
//for load method: AMD
//$require_js = substr($require_js, 0, -3);
$filterprops['JSLINK'] = $require_js;
}
}
//massage the CSS URL depending on schemes and rel. links etc.
if (!empty($require_css)) {
if (strpos($require_css, '//') === 0) {
$require_css = $scheme . $require_css;
} else if (strpos($require_css, '/') === 0) {
$require_css = $CFG->wwwroot . $require_css;
}
}
//set up our revision flag for forcing cache refreshes etc
if (!empty($conf['revision'])) {
$revision = $conf['revision'];
} else {
$revision = '0';
}
//if not too late: load css in header
// if too late: inject it there via JS
$filterprops['CSSLINK'] = false;
$filterprops['CSSCUSTOM'] = false;
//require any scripts from the template
$customcssurl = false;
if ($conf['templatestyle_' . $tempindex]) {
$customcssurl = new moodle_url('/filter/poodll/templatecss.php', array('t' => $tempindex, 'rev' => $revision));
}
if (!$PAGE->headerprinted && !$PAGE->requires->is_head_done()) {
if ($require_css) {
$PAGE->requires->css(new moodle_url($require_css));
}
if ($customcssurl) {
$PAGE->requires->css($customcssurl);
}
} else {
if ($require_css) {
$filterprops['CSSLINK'] = $require_css;
}
if ($customcssurl) {
$filterprops['CSSCUSTOM'] = $customcssurl->out();
}
}
//Tell javascript which template this is
$filterprops['TEMPLATEID'] = $tempindex;
$jsmodule = array(
'name' => 'filter_poodll',
'fullpath' => '/filter/poodll/module.js',
'requires' => array('json')
);
//AMD or not, and then load our js for this template on the page
if ($require_amd) {
$generator = new \filter_poodll\templatescriptgenerator($tempindex);
$template_amd_script = $generator->get_template_script();
//props can't be passed at much length , Moodle complains about too many
//so we do this ... lets hope it don't break things
$jsonstring = json_encode($filterprops);
$props_html = \html_writer::tag('input', '',
array('id' => 'filter_poodll_amdopts_' . $filterprops['AUTOID'], 'type' => 'hidden', 'value' => $jsonstring));
$poodlltemplate = $props_html . $poodlltemplate;
//load define for this template. Later it will be called from loadtemplate
if (!empty($template_amd_script)) {
$PAGE->requires->js_amd_inline($template_amd_script);
}
//for AMD template script
$PAGE->requires->js_call_amd('filter_poodll/template_amd', 'loadtemplate',
array(array('AUTOID' => $filterprops['AUTOID'])));
//echo $filterprops['AUTOID'] . PHP_EOL;
} else {
//require any scripts from the template
$customjsurl = new moodle_url('/filter/poodll/templatejs.php', array('t' => $tempindex, 'rev' => $revision));
$PAGE->requires->js($customjsurl);
//for no AMD
$PAGE->requires->js_init_call('M.filter_poodll_templates.loadtemplate', array($filterprops), false, $jsmodule);
}
//finally return our template text
return $poodlltemplate;
}//end of function
}//end of class