-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlocallib.php
1438 lines (1250 loc) · 54.4 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/>.
/**
* Plugin "Evaluations (evasys)" - Local library
*
* @package block_onlinesurvey
* @copyright 2018 Soon Systems GmbH on behalf of evasys GmbH
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
define('BLOCK_ONLINESURVEY_COMM_SOAP', "SOAP");
define('BLOCK_ONLINESURVEY_COMM_LTI', "LTI");
define('BLOCK_ONLINESURVEY_DEFAULT_TIMEOUT', 15);
define('BLOCK_ONLINESURVEY_LTI_REGEX_LEARNER_DEFAULT', '/<(p){1}(.){0,}[\s]{0,}(data-participated="false"){1}[\s]{0,}/');
define('BLOCK_ONLINESURVEY_LTI_REGEX_INSTRUCTOR_DEFAULT',
'/<(div){1}[\s]{1,}(class=){1}["|\']{1}[a-z]{0,}[\s]{0,}(response-box){1}[\s]{0,}[a-z]{0,}[\s]{0,}["|\']{1}>/');
define('BLOCK_ONLINESURVEY_PRESENTATION_BRIEF', "brief");
define('BLOCK_ONLINESURVEY_PRESENTATION_DETAILED', "detailed");
require_once($CFG->dirroot . '/mod/lti/locallib.php');
/**
* Request surveys for the current user according to email or username and displays the result.
* @param string $config block settings of "block_onlinesurvey"
* @param string $moodleusername username for SOAP request
* @param string $moodleemail email for SOAP request
* @param int $modalzoom indicates if the modal list popup is open or not
* @return string
*/
function block_onlinesurvey_get_soap_content($config = null, $moodleusername = '', $moodleemail = '', $modalzoom = 0) {
global $SESSION;
$surveyurl = 'indexstud.php?type=html&user_tan=';
if (empty($config)) {
$config = get_config("block_onlinesurvey");
}
$connectiontype = $config->connectiontype;
$surveyurl = $config->survey_login . $surveyurl;
$wsdl = $config->survey_server;
$soapuser = $config->survey_user;
$soappassword = $config->survey_pwd;
$debugmode = $config->survey_debug;
$hideempty = $config->survey_hide_empty;
$offerzoom = $config->offer_zoom;
$timeout = isset($config->survey_timeout) ? $config->survey_timeout : BLOCK_ONLINESURVEY_DEFAULT_TIMEOUT;
// Parse wsdlnamespace from the wsdl url.
preg_match('/\/([^\/]+\.wsdl)$/', $wsdl, $matches);
$soapcontentstr = '';
if (count($matches) == 2) {
$wsdlnamespace = $matches[1];
$soapconfigobj = new stdClass();
$soapconfigobj->connectiontype = $connectiontype;
$soapconfigobj->wsdl = $wsdl;
$soapconfigobj->timeout = $timeout;
$soapconfigobj->debugmode = $debugmode;
$soapconfigobj->soapuser = $soapuser;
$soapconfigobj->soappassword = $soappassword;
$soapconfigobj->wsdlnamespace = $wsdlnamespace;
$soapconfigobj->useridentifier = $config->useridentifier;
$soapconfigobj->moodleemail = $moodleemail;
$soapconfigobj->moodleusername = $moodleusername;
$soapconfigobj->customfieldnumber = $config->customfieldnumber;
$soapconfigobj->coursecode = '';
$result = new stdClass();
$soaprequesteachtime = $config->soap_request_eachtime;
// Get surveys if no surveys in SESSION or debug mode for the block is enabled.
if (!isset($SESSION->block_onlinesurvey_surveykeys) || $debugmode || $soaprequesteachtime) {
$result = block_onlinesurvey_get_surveys($soapconfigobj);
$SESSION->block_onlinesurvey_surveykeys = $result->surveys;
$SESSION->block_onlinesurvey_error = $result->error;
}
if (isset($SESSION->block_onlinesurvey_error)) {
$result->error = $SESSION->block_onlinesurvey_error;
}
if (is_object($SESSION->block_onlinesurvey_surveykeys)) {
if (!is_array($SESSION->block_onlinesurvey_surveykeys->OnlineSurveyKeys)) {
$SESSION->block_onlinesurvey_surveykeys->OnlineSurveyKeys = array(
$SESSION->block_onlinesurvey_surveykeys->OnlineSurveyKeys
);
}
$count = count($SESSION->block_onlinesurvey_surveykeys->OnlineSurveyKeys);
$count2 = 0;
$surveysfound = false;
foreach ($SESSION->block_onlinesurvey_surveykeys->OnlineSurveyKeys as $surveykey) {
if (!empty($surveykey->TransactionNumber) && ($surveykey->TransactionNumber != null &&
$surveykey->TransactionNumber !== 'null')) {
$surveysfound = true;
$count2++;
}
}
if ($hideempty && $count2 > 0) {
$soapcontentstr .= block_onlinesurvey_viewscript();
}
if (!$offerzoom && $count2 > 0 && !$modalzoom) {
$soapcontentstr .= block_onlinesurvey_surveybuttonscript();
}
if ($count2 > 0 && !$modalzoom) {
$soapcontentstr .= block_onlinesurvey_highlightscript($count2);
} else if ($count2 == 0 && !$modalzoom) {
$soapcontentstr .= block_onlinesurvey_donthighlightscript();
}
if ($config->presentation == BLOCK_ONLINESURVEY_PRESENTATION_BRIEF && !$modalzoom) {
$soapcontentstr .= block_onlinesurvey_createsummary($count2);
// Surveys found.
if ($count2 && $surveysfound) {
if (!empty($config->survey_show_popupinfo)) {
$soapcontentstr .= '<script language="JavaScript">' .
'if (typeof window.parent.evasysGeneratePopupinfo == "function") { ' .
'window.parent.evasysGeneratePopupinfo(); }</script>';
}
}
} else {
// Surveys found.
if ($count && $surveysfound) {
$soapcontentstr .= '<ul class="block_onlinesurvey_survey_list">';
$cnt = 0;
foreach ($SESSION->block_onlinesurvey_surveykeys->OnlineSurveyKeys as $surveykey) {
if ($surveykey->TransactionNumber !== 'null') {
$cnt++;
$soapcontentstr .= '<li class="survey">';
$soapcontentstr .= "<a id=\"surveylink_" . $cnt . "\" " .
"href=\"$surveyurl" . "{$surveykey->TransactionNumber}\" " .
"target=\"_blank\">$surveykey->CourseName</a>";
$soapcontentstr .= '</li>';
}
}
$soapcontentstr .= '</ul>';
if (!empty($config->survey_show_popupinfo)) {
$soapcontentstr .= '<script language="JavaScript">' .
'if (typeof window.parent.evasysGeneratePopupinfo == "function") { ' .
'window.parent.evasysGeneratePopupinfo(); }</script>';
}
} else {
$soapcontentstr = '<div class="block_onlinesurvey_info">' .
get_string('surveys_exist_not', 'block_onlinesurvey') . '</div>';
}
}
} else if (empty($SESSION->block_onlinesurvey_surveykeys)) {
$soapcontentstr = '<div class="block_onlinesurvey_info">' . get_string('surveys_exist_not', 'block_onlinesurvey') .
'</div>';
}
if (isset($result->error) && !empty($result->error)) {
$soapcontentstr = get_string('error_occured', 'block_onlinesurvey', $result->error);
}
// TODO: Check, was hier angezeigt werden soll.
if ($debugmode && isset($result->warning) && !empty($result->warning)) {
$soapcontentstr = get_string('error_warning_message', 'block_onlinesurvey', $result->warning) . "<br>" . $soapcontentstr;
}
} else {
if ($debugmode) {
$soapcontentstr = get_string('error_wsdl_namespace', 'block_onlinesurvey');
}
}
return $soapcontentstr;
}
/**
* Returns a string with HTML code for the compact view.
*
* @param int $surveycount number of surveys
* @return string
*/
function block_onlinesurvey_createsummary($surveycount) {
$offerzoom = get_config('block_onlinesurvey', 'offer_zoom');
if ($surveycount == 0 && $offerzoom == false) {
$contentstr = "<div id=\"block_onlinesurvey_area\" class=\"block_onlinesurvey_area\">";
$contentstr .= "<div class=\"block_onlinesurvey_circle\" >";
$contentstr .= "<span class=\"block_onlinesurvey_number\">";
$contentstr .= "<i class=\"fa fa-check\"></i>";
$contentstr .= "</span>";
$contentstr .= "</div>";
$contentstr .= '<div class="block_onlinesurvey_text">' . get_string('surveys_exist_not', 'block_onlinesurvey') . '</div>';
$contentstr .= "</div>";
} else if ($surveycount == 0 && $offerzoom == true) {
$contentstr = "<div id=\"block_onlinesurvey_area\" class=\"block_onlinesurvey_area block_onlinesurvey_offerzoom\" " .
"onClick=\"parent.document.getElementById('block_onlinesurvey_surveys_content').click(parent.document);\">";
$contentstr .= "<div class=\"block_onlinesurvey_circle\" >";
$contentstr .= "<span class=\"block_onlinesurvey_number\">";
$contentstr .= "<i class=\"fa fa-check\"></i>";
$contentstr .= "</span>";
$contentstr .= "<div class=\"block_onlinesurvey_compact_magnifier\">";
$contentstr .= "<i class=\"fa fa-search-plus\"></i>";
$contentstr .= "</div>";
$contentstr .= "</div>";
$contentstr .= '<div class="block_onlinesurvey_text">' . get_string('surveys_exist_not', 'block_onlinesurvey') . '</div>';
$contentstr .= "</div>";
} else {
if ($surveycount > 0 && $surveycount <= 3) {
$surveycountclass = 'block_onlinesurvey_surveycount_' . $surveycount;
}
if ($surveycount > 3) {
$surveycountclass = 'block_onlinesurvey_surveycount_gt3';
}
$contentstr = "<div id=\"block_onlinesurvey_area\" " .
"class=\"block_onlinesurvey_area block_onlinesurvey_surveysexist " . $surveycountclass . "\" " .
"onClick=\"parent.document.getElementById('block_onlinesurvey_surveys_content').click(parent.document);\">";
$contentstr .= "<div class=\"block_onlinesurvey_circle\" >";
$contentstr .= "<span class=\"block_onlinesurvey_number\">";
$contentstr .= $surveycount;
$contentstr .= "</span>";
$contentstr .= "<div class=\"block_onlinesurvey_compact_magnifier\">";
$contentstr .= "<i class=\"fa fa-search-plus\"></i>";
$contentstr .= "</div>";
$contentstr .= "</div>";
$contentstr .= '<div class="block_onlinesurvey_text">' . get_string('surveys_exist', 'block_onlinesurvey') . '</div>';
$contentstr .= "</div>";
}
return $contentstr;
}
/**
* Returns a string with a <script> tag which shows the previously hidden block.
*
* @return string
*/
function block_onlinesurvey_viewscript() {
return '<script language="JavaScript">' . "\n" .
' var hiddenelements = parent.document.getElementsByClassName("block_onlinesurvey");' . "\n" .
' for (var i = 0; i < hiddenelements.length; i++) {' . "\n" .
' hiddenelements[i].style.display = "block";' . "\n" .
' }' . "\n" .
'</script>';
}
/**
* Returns a string with a <script> tag which shows the previously hidden 'zoom survey list' button.
*
* @return string
*/
function block_onlinesurvey_surveybuttonscript() {
return '<script language="JavaScript">' . "\n" .
' var hiddenelements = parent.document.getElementsByClassName("block_onlinesurvey_allsurveys");' . "\n" .
' for (var i = 0; i < hiddenelements.length; i++) {' . "\n" .
' hiddenelements[i].style.display = "block";' . "\n" .
' }' . "\n" .
'</script>';
}
/**
* Returns a string with a <script> tag which adds a class to indicate that surveys exist.
*
* @param int $surveycount The number of open surveys.
* @return string
*/
function block_onlinesurvey_highlightscript($surveycount) {
if ($surveycount > 0 && $surveycount <= 3) {
$surveycountclass = 'block_onlinesurvey_surveycount_' . $surveycount;
}
if ($surveycount > 3) {
$surveycountclass = 'block_onlinesurvey_surveycount_gt3';
}
return '<script language="JavaScript">' . "\n" .
' var parentelements = parent.document.getElementsByClassName("block_onlinesurvey");' . "\n" .
' for (var i = 0; i < parentelements.length; i++) {' . "\n" .
' parentelements[i].classList.add("block_onlinesurvey_surveysexist");' . "\n" .
' parentelements[i].classList.add("' . $surveycountclass . '");' . "\n" .
' }' . "\n" .
'</script>';
}
/**
* Returns a string with a <script> tag which removes a class to indicate that no surveys exist.
*
* @return string
*/
function block_onlinesurvey_donthighlightscript() {
return '<script language="JavaScript">' . "\n" .
' var parentelements = parent.document.getElementsByClassName("block_onlinesurvey");' . "\n" .
' for (var i = 0; i < parentelements.length; i++) {' . "\n" .
' parentelements[i].classList.remove("block_onlinesurvey_surveysexist");' . "\n" .
' }' . "\n" .
'</script>';
}
/**
* Perform SOAP request for surveys of a user according to user email or username.
*
* @param object $soapconfigobj Object containing data for SOAP request.
* @return object Object containing surveys if present and errors or warnings of the onlinesurvey_soap_client
*/
function block_onlinesurvey_get_surveys($soapconfigobj) {
$retval = new stdClass();
$retval->error = null;
$retval->warning = null;
$retval->surveys = false;
try {
// Check connectiontype for SOAP.
if ($soapconfigobj->connectiontype == 'SOAP') {
require_once('onlinesurvey_soap_client.php');
$client = new onlinesurvey_soap_client($soapconfigobj->wsdl,
array(
'trace' => 1,
'feature' => SOAP_SINGLE_ELEMENT_ARRAYS,
'connection_timeout' => $soapconfigobj->timeout),
$soapconfigobj->timeout,
$soapconfigobj->debugmode
);
$header = array(
'Login' => $soapconfigobj->soapuser,
'Password' => $soapconfigobj->soappassword
);
if (is_object($client)) {
if ($client->haswarning) {
$retval->warning = $client->warnmessage;
}
$soapheader = new SoapHeader($soapconfigobj->wsdlnamespace, 'Header', $header);
$client->__setSoapHeaders($soapheader);
} else {
$retval->error = block_onlinesurvey_handle_error("SOAP client configuration error");
return $retval;
}
if (!empty($soapconfigobj->useridentifier)) {
if ($soapconfigobj->useridentifier == 'email') {
if ($soapconfigobj->moodleemail) {
$retval->surveys = $client->GetPswdsByParticipant($soapconfigobj->moodleemail);
}
} else if ($soapconfigobj->useridentifier == 'username') {
$retval->surveys = $client->GetPswdsByParticipant($soapconfigobj->moodleusername,
$soapconfigobj->coursecode, $soapconfigobj->customfieldnumber);
}
}
}
} catch (Exception $e) {
$retval->error = block_onlinesurvey_handle_error($e);
return $retval;
}
return $retval;
}
/**
* Helper function that returns an error string
* @param Array|object|string $err
* @return string human readable representation of an error
*/
function block_onlinesurvey_handle_error($err) {
$error = '';
if (is_array($err)) {
// Configuration validation error.
if (!$err[0]) {
$error = $err[1];
}
} else if (is_string($err)) {
// Simple error message.
$error = $err;
} else {
// Error should be an exception.
$error = block_onlinesurvey_print_exceptions($err);
}
return $error;
}
/**
* Helper function for exceptions
* @param object $e should be an exception
* @return string formatted error message of the excetion
*/
function block_onlinesurvey_print_exceptions($e) {
if (get_class($e) == "SoapFault") {
$msg = "{$e->faultstring}";
$context = context_system::instance();
if (has_capability('block/onlinesurvey:view_debugdetails', $context)) {
$detail = '';
if (isset($e->detail) && !empty($e->detail)) {
$detail = $e->detail;
if (is_object($detail) && isset($detail->tSoapfault)) {
$detail = $detail->tSoapfault;
if (isset($detail->sDetails)) {
$detail = $detail->sDetails;
}
} elseif (is_object($detail) && isset($detail->return) && isset($detail->return->sDetails)) {
$detail = $detail->return->sDetails;
}
$msg .= "<br>" . $detail;
}
}
} else {
$msg = $e->getMessage();
}
return $msg;
}
/**
* Request surveys via LTI for the current user according to email or username and displays the result.
* This functions uses functions of '/mod/lti/locallib.php'.
* Performs a second request via curl to check the result for learner content in order to include code to display popupinfo dialog -
* if option is selected in the settings.
* @param string $config block settings of "block_onlinesurvey"
* @param string $context context for LTI request - not yet supported by LTI provider
* @param string $course course for LTI request - not yet supported by LTI provider
* @param int $modalzoom indicates if the modal list popup is open or not
* @return string
*/
function block_onlinesurvey_get_lti_content($config = null, $context = null, $course = null, $modalzoom = 0, $foruserid = 0) {
global $CFG, $SESSION;
require_once($CFG->dirroot . '/mod/lti/locallib.php');
$lticontentstr = '';
if (empty($config)) {
$config = block_onlinesurvey_get_launch_config();
}
$courseid = (!empty($course->id)) ? $course->id : 1;
if ($config->connectiontype == LTI_VERSION_1P3) {
list($endpoint, $parameter) = block_onlinesurvey_lti_get_launch_data($config, '', '', $foruserid);
} else {
list($endpoint, $parameter) = block_onlinesurvey_get_launch_data($config, $context, $course);
}
$debuglaunch = $config->survey_debug;
$surveycount = 0;
// Check for learner content in LTI result.
try {
$content2 = block_onlinesurvey_lti_post_launch_html_curl($parameter, $endpoint, $config);
} catch (Exception $e) {
return $e->getMessage();
}
// Search in $content2 for e.g.: <div class="cell participate centered">.
// If match found and survey_show_popupinfo is set, add code to generate popup.
if (!empty($content2)) {
if (isset($config->lti_regex_learner) && !empty($config->lti_regex_learner)) {
$re = $config->lti_regex_learner;
// No regex in config -> use default regex.
} else {
$re = BLOCK_ONLINESURVEY_LTI_REGEX_LEARNER_DEFAULT;
}
if (!empty($re)) {
$surveycount = preg_match_all($re, $content2, $matches, PREG_SET_ORDER, 0);
$SESSION->block_onlinesurvey_curl_checked = true;
if (!empty($matches) && !empty($config->survey_show_popupinfo)) {
// Check to display dialog is (also) done in JS function "evasysGeneratePopupinfo".
$lticontentstr .= '<script language="JavaScript">if (typeof window.parent.evasysGeneratePopupinfo == "function") { ' .
'window.parent.evasysGeneratePopupinfo(); }</script>';
}
}
if (isset($config->lti_regex_instructor) && !empty($config->lti_regex_instructor)) {
$reinstructor = $config->lti_regex_instructor;
// No regex in config -> use default regex.
} else {
$reinstructor = BLOCK_ONLINESURVEY_LTI_REGEX_INSTRUCTOR_DEFAULT;
}
if (!empty($reinstructor)) {
$surveycount += preg_match_all($reinstructor, $content2, $matches, PREG_SET_ORDER, 0);
}
}
if ($config->survey_hide_empty && $surveycount > 0 && !$modalzoom) {
$lticontentstr .= block_onlinesurvey_viewscript();
}
if (!$config->offer_zoom && $surveycount > 0 && !$modalzoom) {
$lticontentstr .= block_onlinesurvey_surveybuttonscript();
}
if ($surveycount > 0 && !$modalzoom) {
$lticontentstr .= block_onlinesurvey_highlightscript($surveycount);
} else if ($surveycount == 0 && !$modalzoom) {
$lticontentstr .= block_onlinesurvey_donthighlightscript();
}
if ($config->presentation == BLOCK_ONLINESURVEY_PRESENTATION_BRIEF && !$modalzoom) {
$lticontentstr .= block_onlinesurvey_createsummary($surveycount);
} else {
if (empty($context)) {
$context = context_system::instance();
}
if (empty($debuglaunch) || has_capability('block/onlinesurvey:view_debugdetails', $context)) {
foreach($parameter as &$value) {
if (is_object($value) || is_array($value)) {
$value = json_encode($value);
}
}
$lticontentstr .= lti_post_launch_html($parameter, $endpoint, $debuglaunch);
if ($debuglaunch && has_capability('block/onlinesurvey:view_debugdetails', $context)) {
$debuglaunch = false;
// $lti_content_str2 = lti_post_launch_html($parameter, $endpoint, $debuglaunch);
// echo "$lti_content_str2 <br><br>";
}
} else {
$lticontentstr = get_string('error_debugmode_missing_capability', 'block_onlinesurvey');
}
}
return $lticontentstr;
}
/**
* Return the endpoint and parameter for lti request based on the block settings.
* This function uses '/mod/lti/locallib.php'.
* @param string $config block settings of "block_onlinesurvey"
* @param string $context optional context for LTI request - not yet supported by LTI provider
* @param string $course optional course for LTI request - not yet supported by LTI provider
* @return multitype:string
*/
function block_onlinesurvey_get_launch_data($config = null, $context = null, $course = null) {
global $CFG, $PAGE;
require_once($CFG->dirroot.'/mod/lti/locallib.php');
if (empty($config)) {
$config = get_config("block_onlinesurvey");
}
// Default the organizationid if not specified.
if (empty($config->lti_tool_consumer_instance_guid)) {
$urlparts = parse_url($CFG->wwwroot);
$config->lti_tool_consumer_instance_guid = $urlparts['host'];
}
$key = $config->lti_clientid;
if (!empty($config->lti_password)) {
$secret = $config->lti_password;
} else if (is_array($config) && !empty($config['lti_password'])) {
$secret = $config['lti_password'];
} else {
$secret = '';
}
$endpoint = !empty($config->lti_url) ? $config->lti_url : $config['lti_url'];
$endpoint = trim($endpoint);
// If the current request is using SSL and a secure tool URL is specified, use it.
if (lti_request_is_using_ssl() && !empty($config->securetoolurl)) {
$endpoint = trim($config->securetoolurl);
}
// If SSL is forced, use the secure tool url if specified. Otherwise, make sure https is on the normal launch URL.
if (isset($config->forcessl) && ($config->forcessl == '1')) {
if (!empty($config->securetoolurl)) {
$endpoint = trim($config->securetoolurl);
}
$endpoint = lti_ensure_url_is_https($endpoint);
} else {
if (!strstr($endpoint, '://')) {
$endpoint = 'http://' . $endpoint;
}
}
$orgid = $config->lti_tool_consumer_instance_guid;
if (empty($course)) {
$course = $PAGE->course;
}
$allparams = block_onlinesurvey_build_request_lti($config, $course);
if (!isset($config->id)) {
$config->id = null;
}
$requestparams = $allparams;
$requestparams = array_merge($requestparams, lti_build_standard_message($config, $orgid, false));
$customstr = '';
if (isset($config->lti_customparameters)) {
$customstr = $config->lti_customparameters;
}
// The function 'lti_build_custom_parameters' expects some parameters that are not part of the block setting -
// so we build "dummys".
$toolproxy = new stdClass();
$tool = new stdClass();
$tool->ltiversion = LTI_VERSION_1;
$tool->parameter = '';
$tool->enabledcapability = array();
$instance = null;
$instructorcustomstr = null;
$requestparams = array_merge($requestparams, lti_build_custom_parameters($toolproxy, $tool, $instance, $allparams, $customstr,
$instructorcustomstr, false));
$target = 'iframe';
if (!empty($target)) {
$requestparams['launch_presentation_document_target'] = $target;
}
$requestparams['launch_presentation_return_url'] = $CFG->wwwroot . '/blocks/onlinesurvey/show_surveys.php';
// Consumer key currently not used -> $key can be '' -> check "(true or !empty(key))".
if ((true or !empty($key)) && !empty($secret)) {
$parms = lti_sign_parameters($requestparams, $endpoint, "POST", $key, $secret);
$endpointurl = new \moodle_url($endpoint);
$endpointparams = $endpointurl->params();
// Strip querystring params in endpoint url from $parms to avoid duplication.
if (!empty($endpointparams) && !empty($parms)) {
foreach (array_keys($endpointparams) as $paramname) {
if (isset($parms[$paramname])) {
unset($parms[$paramname]);
}
}
}
} else {
// If no key and secret, do the launch unsigned.
$returnurlparams['unsigned'] = '1';
$parms = $requestparams;
}
return array($endpoint, $parms);
}
/**
* Return the endpoint and parameter for lti request based on the block settings.
* This function uses '/mod/lti/locallib.php'.
* @param string $config block settings of "block_onlinesurvey"
* @param string $course optional course for LTI request - not yet supported by LTI provider
* @param string $nonce the nonce value to use (applies to LTI 1.3 only)
* @param string $messagetype LTI Message Type for this launch
* @return array the endpoint URL and parameters (including the signature)
*/
function block_onlinesurvey_lti_get_launch_data($config = null, $nonce = '', $messagetype = 'basic-lti-launch-request', $foruserid = 0)
{
global $CFG, $PAGE, $USER, $DB, $SESSION;
require_once($CFG->dirroot . '/mod/lti/locallib.php');
if (empty($config)) {
$config = get_config("block_onlinesurvey");
}
$ltiversion = $config->connectiontype;
$typeid = $config->typeid;
if (empty($config->lti_clientid)) {
$config->lti_clientid = block_onlinesurvey_get_clientid($typeid);
}
// Default the organizationid if not specified.
if (empty($config->lti_tool_consumer_instance_guid)) {
$urlparts = parse_url($CFG->wwwroot);
$config->lti_tool_consumer_instance_guid = $urlparts['host'];
}
if ($ltiversion === LTI_VERSION_1P3) {
$key = $config->lti_clientid;
} else {
$key = '';
}
if (!empty($config->lti_password)) {
$secret = $config->lti_password;
} else if (is_array($config) && !empty($config['lti_password'])) {
$secret = $config['lti_password'];
} else {
$secret = '';
}
$endpoint = !empty($config->lti_url) ? $config->lti_url : $config['lti_url'];
$endpoint = trim($endpoint);
// If the current request is using SSL and a secure tool URL is specified, use it.
if (lti_request_is_using_ssl() && !empty($config->securetoolurl)) {
$endpoint = trim($config->securetoolurl);
}
// If SSL is forced, use the secure tool url if specified. Otherwise, make sure https is on the normal launch URL.
if (isset($config->forcessl) && ($config->forcessl == '1')) {
if (!empty($config->securetoolurl)) {
$endpoint = trim($config->securetoolurl);
}
$endpoint = lti_ensure_url_is_https($endpoint);
} else {
if (!strstr($endpoint, '://')) {
$endpoint = 'http://' . $endpoint;
}
}
$orgid = $config->lti_tool_consumer_instance_guid;
if (empty($course)) {
$course = $PAGE->course;
}
$allparams = block_onlinesurvey_build_request_lti($config, $course, $messagetype, $foruserid); // analog to lti/locallib.php line 560
if (!isset($config->id)) {
$config->id = null;
}
$requestparams = $allparams;
$requestparams = array_merge($requestparams, lti_build_standard_message($config, $orgid, false, $messagetype));
$customstr = '';
if (isset($config->lti_customparameters)) {
$customstr = $config->lti_customparameters;
}
// The function 'lti_build_custom_parameters' expects some parameters that are not part of the block setting -
// so we build "dummys".
$toolproxy = new stdClass();
$tool = new stdClass();
$tool->ltiversion = $ltiversion;
$tool->parameter = '';
$tool->enabledcapability = array();
$instance = null;
$instructorcustomstr = null;
$requestparams = array_merge($requestparams, lti_build_custom_parameters($toolproxy, $tool, $instance, $allparams, $customstr,
$instructorcustomstr, false));
$target = 'iframe';
if (!empty($target)) {
$requestparams['launch_presentation_document_target'] = $target;
}
$requestparams['launch_presentation_return_url'] = $CFG->wwwroot . '/blocks/onlinesurvey/show_surveys.php';
$basicoutcome = new \stdClass();
$servicesalt = $DB->get_field('lti_types_config', 'value', ['typeid' => $typeid, 'name' => 'servicesalt']);
$basicoutcome->lis_result_sourcedid = json_encode(lti_build_sourcedid($typeid,
$USER->id,
$servicesalt,
$typeid));
$requestparams['lis_result_sourcedid'] = $basicoutcome->lis_result_sourcedid;
$serviceurl = new \moodle_url('/mod/lti/service.php');
$serviceurl = $serviceurl->out();
$forcessl = false;
if (!empty($CFG->mod_lti_forcessl)) {
$forcessl = true;
}
if ((isset($toolconfig['forcessl']) && ($toolconfig['forcessl'] == '1')) or $forcessl) {
$serviceurl = lti_ensure_url_is_https($serviceurl);
}
$basicoutcome->lis_outcome_service_url = $serviceurl;
$requestparams['lis_outcome_service_url'] = $serviceurl;
if ($foruserid) {
$requestparams['for_user_id'] = $foruserid;
}
$requestparams['https://purl.imsglobal.org/spec/lti-bo/claim/basicoutcome'] = $basicoutcome;
$requestparams['resource_link_id'] = block_onlinesurvey_get_lti_typeid();
// Consumer key currently not used -> $key can be '' -> check "(true or !empty(key))".
if ((!empty($key) && !empty($secret)) || ($ltiversion === LTI_VERSION_1P3)) { // ICNOTICE: matches mod/lti/locallib.php, lines 632ff
if ($ltiversion !== LTI_VERSION_1P3) {
$requestparams['lti_version'] = LTI_VERSION_1;
$parms = lti_sign_parameters($requestparams, $endpoint, 'POST', $key, $secret);
} else {
$requestparams['https://purl.imsglobal.org/spec/lti/claim/version'] = '1.3.0';
$requestparams['lti_version'] = '1.3.0';
// $requestparams = block_onlinesurvey_get_dummy_request(); // only use for testing purposes
if (isset($SESSION->lti_state) && !empty($SESSION->lti_state)) {
$state = $SESSION->lti_state;
} else {
$state = 'state-' . hash('sha256', random_bytes(64));
}
$SESSION->lti_state = $state;
$requestparams['custom_state'] = $state;
$requestparams['lti1p3_' . $state] = $state;
$requestparams['ext_state'] = $state;
$parms = lti_sign_jwt($requestparams, $endpoint, $key, $typeid, $nonce);
}
$endpointurl = new \moodle_url($endpoint);
$endpointparams = $endpointurl->params();
// Strip querystring params in endpoint url from $parms to avoid duplication.
if (!empty($endpointparams) && !empty($parms)) {
foreach (array_keys($endpointparams) as $paramname) {
if (isset($parms[$paramname])) {
unset($parms[$paramname]);
}
}
}
} else {
// If no key and secret, do the launch unsigned.
$returnurlparams['unsigned'] = '1';
$parms = $requestparams;
}
return array($endpoint, $parms);
}
/**
* Builds array of parameters for the LTI request
* @param object $config block settings of "block_onlinesurvey"
* @param object $course course that is used for some context attributes
* @param string $messagetype LTI Message Type for this launch
* @param int $foruserid
* @return multitype:string NULL
*/
function block_onlinesurvey_build_request_lti($config, $course, $messagetype = null, $foruserid = 0) {
global $USER;
$roles = block_onlinesurvey_get_ims_roles($USER, $config);
$requestparams = array(
'user_id' => $USER->id,
'lis_person_sourcedid' => $USER->idnumber,
'roles' => $roles,
'context_id' => $course->id,
'context_label' => $course->shortname,
'context_title' => $course->fullname,
);
if ($messagetype) {
$requestparams['lti_message_type'] = $messagetype;
}
if ($course->format == 'site') {
$requestparams['context_type'] = 'Group';
} else {
$requestparams['context_type'] = 'CourseSection';
$requestparams['lis_course_section_sourcedid'] = $course->idnumber;
}
// E-mail address is evaluated in EVERY case, even if it is decided to use the Username instead.
$requestparams['lis_person_contact_email_primary'] = $USER->email;
$requestparams['resource_link_id'] = block_onlinesurvey_get_lti_typeid();
$requestparams['ext_lms'] = 'moodle-2';
if ($foruserid) {
$requestparams['for_user_id'] = $foruserid;
}
if ($config->connectiontype == LTI_VERSION_1P3) {
$requestparams["https://purl.imsglobal.org/spec/lti/claim/ext"] = [
"user_username" => $USER->username,
"lms" => "moodle-2",
];
$requestparams["email"] = $USER->email;
$requestparams['lis_person_name_given'] = $USER->firstname;
$requestparams['lis_person_name_family'] = $USER->lastname;
$requestparams['lis_person_name_full'] = fullname($USER);
$requestparams['ext_user_username'] = $USER->username;
$requestparams['resource_link_title'] = $config->blocktitle;
$requestparams['resource_link_description'] = $config->blocktitle;
$requestparams["https://purl.imsglobal.org/spec/lti/claim/version"] = '1.3.0';
}
if (strpos($roles, 'Learner') !== false) {
if ($config->useridentifier == 'email') {
$requestparams['custom_learner_lms_identifier'] = 'lis_person_contact_email_primary';
$requestparams['lis_person_contact_email_primary'] = $USER->email;
} else if ($config->useridentifier == 'username') {
$requestparams['custom_learner_lms_identifier'] = 'ext_user_username';
$requestparams['ext_user_username'] = $USER->username;
$requestparams['custom_learner_provider_identifier'] = "custom" . $config->customfieldnumber;
}
}
if (strpos($roles, 'Instructor') !== false) {
// $requestparams['custom_instructor_lms_identifier'] = 'ext_user_username';
// $requestparams['ext_user_username'] = $USER->username;
if ($config->useridentifier == 'email') {
$requestparams['custom_instructor_lms_identifier'] = 'lis_person_contact_email_primary';
$requestparams['lis_person_contact_email_primary'] = $USER->email;
} else if ($config->useridentifier == 'username') {
$requestparams['custom_instructor_lms_identifier'] = 'ext_user_username';
$requestparams['ext_user_username'] = $USER->username;
// $requestparams['custom_instructor_provider_identifier'] = "custom".$config->customfieldnumber;
}
}
return $requestparams;
}
/**
* Gets the LTI role string for the specified user according to lti rolemappings
*
* @param object $user user object
* @param object $config block settings of "block_onlinesurvey"
* @return string A role string suitable for passing with an LTI launch
*/
function block_onlinesurvey_get_ims_roles($user, $config) {
global $DB;
$roles = array();
// Check if user has "mapped" roles.
$isinstructor = false;
$ltimapping = $config->lti_instructormapping;
if (!empty($ltimapping)) {
try {
$ltimapping = explode(',', $ltimapping);
list($sql, $params) = $DB->get_in_or_equal($ltimapping, SQL_PARAMS_NAMED, 'lti_mapping');
$params['userid'] = $user->id;
$isinstructor = $DB->record_exists_select('role_assignments', "userid = :userid and roleid $sql", $params);
} catch (Exception $e) {
error_log("error check user roles for 'instructor': " . $e->getMessage());
}
}
$islearner = false;
$ltimapping = $config->lti_learnermapping;
if (!empty($ltimapping)) {
try {
$ltimapping = explode(',', $ltimapping);
list($sql, $params) = $DB->get_in_or_equal($ltimapping, SQL_PARAMS_NAMED, 'lti_mapping');
$params['userid'] = $user->id;
$islearner = $DB->record_exists_select('role_assignments', "userid = :userid and roleid $sql", $params);
} catch (Exception $e) {
error_log("error check user roles for 'learner': " . $e->getMessage());
}
}
if (!empty($isinstructor)) {
array_push($roles, 'Instructor');
}
if (!empty($islearner)) {
array_push($roles, 'Learner');
}
// User has NO role in moodle -> use role mapping for learner.
if (empty($roles)) {
array_push($roles, 'Learner');
}
if (is_siteadmin($user)) {
array_push($roles, 'urn:lti:sysrole:ims/lis/Administrator', 'urn:lti:instrole:ims/lis/Administrator');
}
return join(',', $roles);
}
/**
* Fetches the LTI content on the server for analyzing the survey list server-side.
*
* @param array $parameter parameter for LTI request
* @param string $endpoint endpoint for LTI request
* @param object $config the plugin configuration
* @param string $state
* @return string result of the curl LTI request
*/
function block_onlinesurvey_lti_post_launch_html_curl($parameter, $endpoint, $config, $state = '') {
global $SESSION, $USER;
// Set POST variables.
$fields = array();
// Construct html for the launch parameters.
foreach ($parameter as $key => $value) {
$key = htmlspecialchars($key);
if (is_string($value)) {
$value = htmlspecialchars($value);