-
Notifications
You must be signed in to change notification settings - Fork 15
/
lib.php
1459 lines (1268 loc) · 48 KB
/
lib.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/>.
/**
* Library of interface functions and constants for module surveypro
*
* All the core Moodle functions, needed to allow the module to work
* integrated in Moodle should be placed here.
* All the surveypro specific functions, needed to implement all the module
* logic, should go to locallib.php. This will help to save some memory when
* Moodle is performing actions across all modules.
*
* @package mod_surveypro
* @copyright 2013 onwards kordan <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_surveypro\utility_layout;
use mod_surveypro\surveypro_file_info;
defined('MOODLE_INTERNAL') || die();
/**
* Some constants
*/
define('SURVEYPRO_VALUELABELSEPARATOR', '::');
define('SURVEYPRO_OTHERSEPARATOR' , '->');
/**
* ITEM TYPES
*/
define('SURVEYPRO_TYPEFIELD' , 'field');
define('SURVEYPRO_TYPEFORMAT', 'format');
/**
* KIND OF SUBMISSION
*/
define('SURVEYPRO_ONESHOTNOEMAIL', 0);
define('SURVEYPRO_ONESHOTEMAIL', 1);
define('SURVEYPRO_PAUSERESUMENOEMAIL', 2);
define('SURVEYPRO_PAUSERESUMEEMAIL', 3);
/**
* ACTIONS
*/
define('SURVEYPRO_NOACTION' , '0');
/**
* ACTIONS in LAYOUT MANAGEMENT page
*/
define('SURVEYPRO_CHANGEORDER' , '1');
define('SURVEYPRO_DELETEITEM' , '2');
define('SURVEYPRO_DROPMULTILANG' , '3');
define('SURVEYPRO_CHANGEINDENT' , '4');
define('SURVEYPRO_HIDEITEM' , '5');
define('SURVEYPRO_SHOWITEM' , '6');
define('SURVEYPRO_MAKERESERVED' , '7');
define('SURVEYPRO_MAKEAVAILABLE' , '8');
define('SURVEYPRO_RESPONSETOPDF' , '9');
/**
* BULK ACTIONS in LAYOUT MANAGEMENT and in APPLY UTEMPLATE page
*/
define('SURVEYPRO_IGNOREITEMS' , '0');
define('SURVEYPRO_HIDEALLITEMS' , '13');
define('SURVEYPRO_SHOWALLITEMS' , '14');
define('SURVEYPRO_DELETEALLITEMS' , '15');
define('SURVEYPRO_DELETEVISIBLEITEMS', '16');
define('SURVEYPRO_DELETEHIDDENITEMS' , '17');
// ACTIONS in RESPONSES section.
define('SURVEYPRO_DELETERESPONSE' , '18');
define('SURVEYPRO_DELETEALLRESPONSES', '19');
define('SURVEYPRO_DUPLICATERESPONSE' , '20');
// ACTIONS in UTEMPLATE section.
define('SURVEYPRO_DELETEUTEMPLATE' , '21');
define('SURVEYPRO_EXPORTUTEMPLATE' , '22');
/**
* VIEW
*/
// MODE in which the USER FORM is going to be used.
define('SURVEYPRO_NOMODE' , '0');
define('SURVEYPRO_NEWRESPONSEMODE', '1');
define('SURVEYPRO_EDITMODE' , '2');
define('SURVEYPRO_READONLYMODE' , '3');
define('SURVEYPRO_PREVIEWMODE' , '4');
// VIEW in ITEM section.
define('SURVEYPRO_NEWITEM' , '5');
define('SURVEYPRO_EDITITEM' , '6');
define('SURVEYPRO_CHANGEORDERASK' , '7');
/**
* SENDERS
*/
define('SURVEYPRO_TAB' , 1);
define('SURVEYPRO_BLOCK', 2);
/**
* FEEDBACKMASK
*/
define('SURVEYPRO_NOFEEDBACK', 0);
/**
* ITEMPREFIX
*/
define('SURVEYPRO_ITEMPREFIX' , 'surveypro');
define('SURVEYPRO_PLACEHOLDERPREFIX', 'placeholder');
define('SURVEYPRO_DONTSAVEMEPREFIX' , 'placeholder');
/**
* INVITE, NO-ANSWER AND IGNOREME VALUE
*/
// Since the very first beginning of the development.
// define('SURVEYPRO_INVITATIONVALUE', '__invItat10n__'); // User should never guess it.
// define('SURVEYPRO_NOANSWERVALUE', '__n0__Answer__'); // User should never guess it.
// define('SURVEYPRO_IGNOREMEVALUE', '__1gn0rE__me__'); // User should never guess it.
// Starting from version 2015090901.
define('SURVEYPRO_INVITEVALUE' , '@@_INVITE_@@'); // User should never guess it.
define('SURVEYPRO_NOANSWERVALUE' , '@@_NOANSW_@@'); // User should never guess it.
define('SURVEYPRO_IGNOREMEVALUE' , '@@_IGNORE_@@'); // User should never guess it.
define('SURVEYPRO_EXPNULLVALUE' , '@@_NULVAL_@@'); // User should never guess it.
define('SURVEYPRO_IMPFORMATSUFFIX', '@@_FORMAT_@@'); // User should never guess it.
/**
* ITEM ADJUSTMENTS
*/
define('SURVEYPRO_VERTICAL' , 0);
define('SURVEYPRO_HORIZONTAL', 1);
/**
* SURVEYPRO STATUS
*/
define('SURVEYPRO_STATUSCLOSED' , 0);
define('SURVEYPRO_STATUSINPROGRESS', 1);
define('SURVEYPRO_STATUSALL' , 2);
/**
* DOWNLOAD
*/
define('SURVEYPRO_DOWNLOADCSV', 1);
define('SURVEYPRO_DOWNLOADTSV', 2);
define('SURVEYPRO_DOWNLOADXLS', 3);
define('SURVEYPRO_FILESBYUSER', 4);
define('SURVEYPRO_FILESBYITEM', 5);
define('SURVEYPRO_NOFIELDSSELECTED' , 1);
define('SURVEYPRO_NORECORDSFOUND' , 2);
define('SURVEYPRO_NOATTACHMENTFOUND', 3);
define('SURVEYPRO_OWNERIDLABEL', 'ownerid');
define('SURVEYPRO_TIMECREATEDLABEL', 'timecreated');
define('SURVEYPRO_TIMEMODIFIEDLABEL', 'timemodified');
/**
* SEPARATORS
*/
define('SURVEYPRO_DBMULTICONTENTSEPARATOR', ';');
define('SURVEYPRO_OUTPUTMULTICONTENTSEPARATOR', '; ');
/**
* CONFIRMATION
*/
define('SURVEYPRO_UNCONFIRMED' , 0);
define('SURVEYPRO_CONFIRMED_YES' , 1);
define('SURVEYPRO_CONFIRMED_NO' , 2);
define('SURVEYPRO_ACTION_EXECUTED', 3);
/**
* DEFAULTVALUE OPTION
*/
define('SURVEYPRO_CUSTOMDEFAULT' , 1);
define('SURVEYPRO_INVITEDEFAULT' , 2);
define('SURVEYPRO_NOANSWERDEFAULT', 3);
define('SURVEYPRO_LIKELASTDEFAULT', 4);
define('SURVEYPRO_TIMENOWDEFAULT' , 5);
/**
* FILEAREAS
*/
define('SURVEYPRO_STYLEFILEAREA' , 'userstyle');
define('SURVEYPRO_TEMPLATEFILEAREA' , 'templatefilearea');
define('SURVEYPRO_THANKSPAGEFILEAREA' , 'thankshtml');
define('SURVEYPRO_ITEMCONTENTFILEAREA', 'itemcontent');
/**
* FIRENDLY FORMAT
*/
define('SURVEYPRO_FRIENDLYFORMAT', -1);
/**
* POSITION OF THE QUESTION CONTENT IN THE ITEM
*/
define('SURVEYPRO_POSITIONLEFT' , 0);
define('SURVEYPRO_POSITIONTOP' , 1);
define('SURVEYPRO_POSITIONFULLWIDTH', 2);
/**
* STATUS OF CONDITIONS OF RELATIONS
*/
define('SURVEYPRO_CONDITIONOK' , 0);
define('SURVEYPRO_CONDITIONNEVERMATCH', 1);
define('SURVEYPRO_CONDITIONMALFORMED' , 2);
/**
* SEMANTIC OF CONTENT RETURNED BY ITEMS
*/
define('SURVEYPRO_ITEMSRETURNSVALUES' , 0);
define('SURVEYPRO_ITEMRETURNSLABELS' , 1);
define('SURVEYPRO_ITEMRETURNSPOSITION', 2);
/**
* OUTPUT CONTENT
*/
define('SURVEYPRO_LABELS' , 'labels');
define('SURVEYPRO_VALUES' , 'values');
define('SURVEYPRO_POSITIONS' , 'positions');
define('SURVEYPRO_ITEMDRIVEN', 'itemdriven');
/**
* DUMMY CONTENT USED AT ANSWER SAVE TIME
*/
define('SURVEYPRO_DUMMYCONTENT', '__my_dummy_content@@');
/**
* OUTPUT OF FINAL SUBMISSION EVALUATION
*/
define('SURVEYPRO_VALIDRESPONSE' , 0);
define('SURVEYPRO_MISSINGMANDATORY' , 1);
define('SURVEYPRO_MISSINGVALIDATION', 2);
/**
* PDF TEMPLATES
*/
define('SURVEYPRO_2COLUMNSTEMPLATE', 2);
define('SURVEYPRO_3COLUMNSTEMPLATE', 3);
/**
* EXPORT CSV FILE STYLE
*/
define('SURVEYPRO_RAW', 0);
define('SURVEYPRO_VERBOSE', 1);
/**
* SURVEYPRO EVENT TYPE
*/
define('SURVEYPRO_EVENT_TYPE_OPEN', 'open');
// Moodle core API.
require_once($CFG->dirroot.'/lib/formslib.php'); // Needed by unittest.
require_once($CFG->dirroot.'/mod/surveypro/deprecatedlib.php');
/* Do not include any libraries here! */
/**
* Saves a new instance of the surveypro into the database
*
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will create a new instance and return the id number
* of the new instance.
*
* @param object $surveypro An object from the form in mod_form.php
* @param mod_surveypro_mod_form $mform
* @return int The id of the newly inserted surveypro record
*/
function surveypro_add_instance($surveypro, $mform) {
global $DB;
$cmid = $surveypro->coursemodule;
$context = \context_module::instance($cmid);
surveypro_pre_process_checkboxes($surveypro);
$surveypro->timecreated = time();
$surveypro->timemodified = time();
$surveypro->id = $DB->insert_record('surveypro', $surveypro);
// Manage userstyle filemanager.
$draftitemid = $surveypro->userstyle_filemanager;
file_save_draft_area_files($draftitemid, $context->id, 'mod_surveypro', SURVEYPRO_STYLEFILEAREA, 0);
// Manage thankspage editor.
$editoroptions = surveypro_get_editor_options();
if ($draftitemid = $surveypro->thankspageeditor['itemid']) {
$surveypro->thankspage = file_save_draft_area_files($draftitemid, $context->id, 'mod_surveypro',
SURVEYPRO_THANKSPAGEFILEAREA, 0, $editoroptions, $surveypro->thankspageeditor['text']);
$surveypro->thankspageformat = $surveypro->thankspageeditor['format'];
}
// Manage mailcontent editor. No embedded pictures to handle.
$surveypro->mailcontent = $surveypro->mailcontenteditor['text'];
$surveypro->mailcontentformat = $surveypro->mailcontenteditor['format'];
$DB->update_record('surveypro', $surveypro);
surveypro_create_event_on_calendar($surveypro, 'open');
surveypro_create_event_on_calendar($surveypro, 'close');
return $surveypro->id;
}
/**
* Updates an instance of the surveypro in the database
*
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will update an existing instance with new data.
*
* @param object $surveypro An object from the form in mod_form.php
* @param mod_surveypro_mod_form $mform
* @return boolean Success/Fail
*/
function surveypro_update_instance($surveypro, $mform) {
global $DB;
$cmid = $surveypro->coursemodule;
$draftitemid = $surveypro->userstyle_filemanager;
$context = \context_module::instance($cmid);
$surveypro->timemodified = time();
$surveypro->id = $surveypro->instance;
surveypro_pre_process_checkboxes($surveypro);
// Classes are not available here!
// So, I can't use $utilitylayoutman->reset_pages();
$whereparams = ['surveyproid' => $surveypro->id];
$DB->set_field('surveypro_item', 'formpage', 0, $whereparams);
// Manage userstyle filemanager.
if ($draftitemid = file_get_submitted_draft_itemid('userstyle_filemanager')) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_surveypro', SURVEYPRO_STYLEFILEAREA, 0);
}
// Manage thankspage editor.
$editoroptions = surveypro_get_editor_options();
if ($draftitemid = $surveypro->thankspageeditor['itemid']) {
$surveypro->thankspage = file_save_draft_area_files($draftitemid, $context->id, 'mod_surveypro',
SURVEYPRO_THANKSPAGEFILEAREA, 0, $editoroptions, $surveypro->thankspageeditor['text']);
$surveypro->thankspageformat = $surveypro->thankspageeditor['format'];
}
// Manage mailcontent editor. No embedded pictures to handle.
$surveypro->mailcontentformat = $surveypro->mailcontenteditor['format'];
$surveypro->mailcontent = $surveypro->mailcontenteditor['text'];
$DB->update_record('surveypro', $surveypro);
surveypro_create_event_on_calendar($surveypro, 'open');
surveypro_create_event_on_calendar($surveypro, 'close');
return true;
}
/**
* Create a calendar event from the newly
* created surveypro module
*
* @param object $surveypro instance
* @param string $type
* @return void
*/
function surveypro_create_event_on_calendar($surveypro, $type) {
global $CFG, $DB, $USER;
require_once($CFG->dirroot.'/calendar/lib.php');
$types = ['open', 'close'];
if (!in_array($type, $types)) {
$message = 'Wrong type passed to surveypro_create_event_on_calendar';
debugging('Error at line '.__LINE__.' of '.__FILE__.'. '.$message, DEBUG_DEVELOPER);
}
$condition = (!is_null($surveypro->timeopen) && ($type == 'open'));
$condition = $condition || (!is_null($surveypro->timeclose) && ($type == 'close'));
if ($condition) {
// Create an event to show on calendar.
$event = new stdClass(); // Event class created.
// If the same old event has already been added to calendar, reuse it.
$conditions = ['instance' => $surveypro->id, 'eventtype' => $type];
if ($oldevent = $DB->get_record('event', $conditions, 'id', 'id', IGNORE_MISSING)) {
$event->id = $oldevent->id;
$editing = true;
} else {
$editing = false;
}
$event->courseid = $surveypro->course; // Course ID.
$event->userid = $USER->id; // User ID creating the event.
$event->modulename = 'surveypro'; // Module name.
$event->instance = $surveypro->id; // Module surveypro's ID.
$event->visible = instance_is_visible('surveypro', $surveypro); // Set visibility for users.
$event->eventtype = $type; // Calendar's event type.
if ($editing) {
$event->timemodified = time(); // Event modification date.
}
if ($type == 'open') {
$event->timestart = $surveypro->timeopen; // Event's start date.
$event->name = get_string('calendar_open_time', 'mod_surveypro', $surveypro->name);
$event->description = get_string('calendar_open_description', 'mod_surveypro', $surveypro->name);
} else {
$event->timestart = $surveypro->timeclose; // Event's close date.
$event->name = get_string('calendar_close_time', 'mod_surveypro', $surveypro->name);
$event->description = get_string('calendar_close_description', 'mod_surveypro', $surveypro->name);
}
// Add the event to calendar.
calendar_event::create($event);
}
}
/**
* Runs any processes that must run before
* a lesson insert/update
*
* surveypro_pre_process_checkboxes
*
* @param object $surveypro Surveypro record
* @return void
*/
function surveypro_pre_process_checkboxes($surveypro) {
$checkboxes = ['newpageforchild', 'neverstartedemail', 'keepinprogress', 'history', 'anonymous', 'captcha'];
foreach ($checkboxes as $checkbox) {
if (!isset($surveypro->{$checkbox})) {
$surveypro->{$checkbox} = 0;
}
}
}
/**
* Removes an instance of the surveypro from the database
*
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @param int $id Id of the module instance
* @return boolean Success/Failure
*/
function surveypro_delete_instance($id) {
global $DB;
if (!$surveypro = $DB->get_record('surveypro', ['id' => $id])) {
return false;
}
$status = true;
// Now get rid of all files.
$fs = get_file_storage();
if ($cm = get_coursemodule_from_instance('surveypro', $surveypro->id)) {
$context = \context_module::instance($cm->id);
$fs->delete_area_files($context->id);
}
$whereparams = ['surveyproid' => $surveypro->id];
// Delete any dependent records here.
if ($submissions = $DB->get_records('surveypro_submission', $whereparams, '', 'id')) {
$submissions = array_keys($submissions);
// Delete all associated surveypro_answer.
if (!$DB->delete_records_list('surveypro_answer', 'submissionid', $submissions)) {
$status = false;
}
// Delete all associated surveypro_submission.
if (!$DB->delete_records('surveypro_submission', $whereparams)) {
$status = false;
}
}
// Get all item_<<plugin>> and format_<<plugin>>.
$types = [SURVEYPRO_TYPEFIELD, SURVEYPRO_TYPEFORMAT];
foreach ($types as $type) {
$pluginlist = surveypro_get_plugin_list($type);
// Delete all associated item<<$type>>_<<plugin>>.
foreach ($pluginlist as $plugin) {
$tablename = 'surveypro'.$type.'_'.$plugin;
if ($DB->get_manager()->table_exists($tablename)) {
$whereparams['plugin'] = $plugin;
if ($deletelist = $DB->get_records('surveypro_item', $whereparams, 'id', 'id')) {
$deletelist = array_keys($deletelist);
[$insql, $inparams] = $DB->get_in_or_equal($deletelist, SQL_PARAMS_NAMED, 'delete');
$select = 'itemid '.$insql;
if (!$DB->delete_records_select($tablename, $select, $inparams)) {
$status = false;
}
}
}
}
}
// Delete all associated surveypro_items.
if (!$DB->delete_records('surveypro_item', ['surveyproid' => $surveypro->id])) {
$status = false;
}
// Finally, delete the surveypro record.
if (!$DB->delete_records('surveypro', ['id' => $surveypro->id])) {
$status = false;
}
return $status;
}
/**
* Returns the information on whether the module supports a feature
*
* @see plugin_supports() in lib/moodlelib.php
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed true if the feature is supported, null if unknown
*/
function surveypro_supports($feature) {
switch ($feature) {
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return false;
case FEATURE_GROUPMEMBERSONLY:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return false;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_GRADE_HAS_GRADE:
return false;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
case FEATURE_MOD_PURPOSE:
return MOD_PURPOSE_COLLABORATION;
default:
return null;
}
}
/**
* Print the grade information for the surveypro for this user.
*
* @param \stdClass $course
* @param \stdClass $user
* @param \stdClass $coursemodule
* @param \stdClass $surveypro
*/
function surveypro_user_outline($course, $user, $coursemodule, $surveypro) {
$return = new \stdClass();
$return->time = 0;
$return->info = '';
return $return;
}
/**
* Prints a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @param \stdClass $course the current course record
* @param \stdClass $user the record of the user we are generating report for
* @param cm_info $mod course module info
* @param \stdClass $surveypro the module instance record
* @return void, is supposed to echp directly
*/
function surveypro_user_complete($course, $user, $mod, $surveypro) {
return true;
}
/**
* Print recent activity from all surveypro in a given course
*
* This is used by the recent activity block
* @param mixed $course the course to print activity for
* @param bool $viewfullnames boolean to determine whether to show full names or not
* @param int $timestart the time the rendering started
* @return bool true if activity was printed, false otherwise.
*/
function surveypro_print_recent_activity($course, $viewfullnames, $timestart) {
return false; // True if anything was printed, otherwise false.
}
/**
* Returns all surveypro since a given time.
*
* @param array $activities The activity information is returned in this array
* @param int $index The current index in the activities array
* @param int $timestart The earliest activity to show
* @param int $courseid Limit the search to this course
* @param int $cmid The course module id
* @param int $userid Optional user id
* @param int $groupid Optional group id
* @return void
*/
function surveypro_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid=0, $groupid=0) {
}
/**
* Print recent activity from all assignments in a given course
*
* This is used by course/recent.php
* @param \stdClass $activity
* @param int $courseid
* @param bool $detail
* @param array $modnames
*/
function surveypro_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
}
/**
* Returns an array of users who are participanting in this surveypro
*
* Must return an array of users who are participants for a given instance
* of surveypro. Must include every user involved in the instance,
* independient of his role (student, teacher, admin...). The returned
* objects must contain at least id property.
* See other modules as example.
*
* @param int $surveyproid ID of an instance of this module
* @return boolean|array false if no participants, array of objects otherwise
*/
function surveypro_get_participants($surveyproid) {
global $DB;
$sql = 'SELECT DISTINCT s.userid as id
FROM {surveypro_submission} s
WHERE s.surveyproid = :surveyproid';
return $DB->get_records_sql($sql, ['surveyproid' => $surveyproid]);
}
/**
* Returns all other caps used in the module
*
* @return array
*/
function surveypro_get_extra_capabilities() {
return ['moodle/site:config', 'moodle/site:accessallgroups'];
}
// Gradebook API.
/**
* Checks if a scale is being used by an surveypro.
*
* This is used by the backup code to decide whether to back up a scale
* @param int $surveyproid
* @param int $scaleid
* @return boolean True if the scale is used by the surveypro
*/
function surveypro_scale_used($surveyproid, $scaleid) {
}
/**
* Checks if scale is being used by any instance of surveypro
*
* This is used to find out if scale used anywhere
* @param int $scaleid
* @return boolean True if the scale is used by any surveypro
*/
function surveypro_scale_used_anywhere($scaleid) {
}
/**
* Creates or updates grade item for the give surveypro instance
*
* Needed by grade_update_mod_grades() in lib/gradelib.php
*
* @param \stdClass $surveypro instance object with extra cmidnumber and modname property
* @return void
*/
function surveypro_grade_item_update(\stdClass $surveypro) {
}
/**
* Update surveypro grades in the gradebook
*
* Needed by grade_update_mod_grades() in lib/gradelib.php
*
* @param \stdClass $surveypro instance object with extra cmidnumber and modname property
* @param int $userid Update grade of specific user only, 0 means all participants
* @return void
*/
function surveypro_update_grades(\stdClass $surveypro, $userid = 0) {
}
// File API.
/**
* Lists all browsable file areas
*
* @param \stdClass $course course object
* @param \stdClass $cm course module object
* @param \stdClass $context context object
* @return array
*/
function surveypro_get_file_areas($course, $cm, $context) {
$fileareas = [
SURVEYPRO_STYLEFILEAREA => get_string('cssstylefilearea', 'mod_surveypro'),
SURVEYPRO_TEMPLATEFILEAREA => get_string('templatesfilearea', 'mod_surveypro'),
SURVEYPRO_THANKSPAGEFILEAREA => get_string('thankspagefilearea', 'mod_surveypro'),
SURVEYPRO_ITEMCONTENTFILEAREA => get_string('itemcontentfilearea', 'mod_surveypro'),
];
return $fileareas;
}
/**
* File browsing support for surveypro module.
*
* @param file_browser $browser
* @param array $areas
* @param stdClass $course
* @param cm_info $cm
* @param context $context
* @param string $filearea
* @param int $itemid
* @param string $filepath
* @param string $filename
* @return file_info_stored file_info_stored instance or null if not found
*/
function surveypro_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
global $CFG, $DB, $USER;
// Checks to see if the user can manage files.
if (!has_capability('moodle/course:managefiles', $context)) {
// No peaking here for students!
return null;
}
if ($context->contextlevel != CONTEXT_MODULE) {
return null;
}
// Condition to drop requests for files belonging to submitted responses.
// It should never be verified.
if ($areas == 'surveyprofield_fileupload') {
return null;
}
if (!isset($areas[$filearea])) {
return null;
}
if (is_null($itemid)) {
return new surveypro_file_info($browser, $course, $cm, $context, $areas, $filearea);
}
$filepath = is_null($filepath) ? '/' : $filepath;
$filename = is_null($filename) ? '.' : $filename;
$fs = get_file_storage();
if (!($storedfile = $fs->get_file($context->id, 'mod_surveypro', $filearea, $itemid, $filepath, $filename))) {
return null;
}
$urlbase = $CFG->wwwroot.'/pluginfile.php';
return new file_info_stored($browser, $context, $storedfile, $urlbase, $itemid, true, true, false, false);
}
/**
* Serves the files from the surveypro file areas
*
* @param \stdClass $course the course object
* @param \stdClass $cm the course module object
* @param \stdClass $context context object
* @param string $filearea the name of the file area
* @param array $args extra arguments
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
* @return bool false if the file is not found, just send the file otherwise returning nothing
*/
function surveypro_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=[]) {
global $DB;
require_login($course, true, $cm);
if (!$surveypro = $DB->get_record('surveypro', ['id' => $cm->instance])) {
send_file_not_found();
}
$fs = get_file_storage();
// For toplevelfileareas $args come without itemid, just the path.
// Other fileareas come with both itemid and path.
$toplevelfilearea = ($filearea == SURVEYPRO_THANKSPAGEFILEAREA);
$toplevelfilearea = $toplevelfilearea || ($filearea == SURVEYPRO_STYLEFILEAREA);
$toplevelfilearea = $toplevelfilearea || ($filearea == SURVEYPRO_TEMPLATEFILEAREA);
$itemid = ($toplevelfilearea) ? 0 : (int)array_shift($args);
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_surveypro/$filearea/$itemid/$relativepath";
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
send_file_not_found();
}
// Finally send the file.
send_stored_file($file, 0, 0, true); // Download MUST be forced - security!
}
// Navigation API.
/**
* Adds module specific settings to the settings block
*
* @param settings_navigation $settings
* @param navigation_node $surveypronode
* @return void
*/
function surveypro_extend_settings_navigation(settings_navigation $settings, navigation_node $surveypronode) {
global $PAGE, $DB;
// Surveypro.
[$condition, $label, $url] = surveypro_get_link_and_condition('surveypro');
if ($condition) {
$navnode = $surveypronode->add($label, $url, navigation_node::TYPE_SETTING);
// Do not add it. It is added by moodle core with the modulename label.
$navnode->set_show_in_secondary_navigation(false);
}
// Layout.
[$condition, $label, $url] = surveypro_get_link_and_condition('layout');
if ($condition) {
$navnode = $surveypronode->add($label, $url, navigation_node::TYPE_SETTING);
}
// Reports.
[$condition, $label, $url] = surveypro_get_link_and_condition('reports');
if ($condition) {
$navnode = $surveypronode->add($label, $url, navigation_node::TYPE_SETTING);
}
// Tools.
[$condition, $label, $url] = surveypro_get_link_and_condition('tools');
if ($condition) {
$navnode = $surveypronode->add($label, $url, navigation_node::TYPE_SETTING);
}
// User templates. (Maybe "User presets" is better?).
[$condition, $label, $url] = surveypro_get_link_and_condition('utemplates');
if ($condition) {
$navnode = $surveypronode->add($label, $url, navigation_node::TYPE_SETTING);
}
// Master templates. (Maybe "Master presets" is better?).
[$condition, $label, $url] = surveypro_get_link_and_condition('mtemplates');
if ($condition) {
$navnode = $surveypronode->add($label, $url, navigation_node::TYPE_SETTING);
}
}
/**
* Extends the global navigation tree in the Navigation block
*
* This can be called by an AJAX request so do not rely on $PAGE as it might not be set up properly.
*
* @param navigation_node $navigation An object representing the navigation tree node of the surveypro module instance
* @param \stdClass $course
* @param \stdClass $surveypro
* @param cm_info $cm
* @return void
*/
function surveypro_extend_navigation(navigation_node $navigation, \stdClass $course, \stdClass $surveypro, cm_info $cm) {
global $PAGE;
// Surveypro.
[$condition, $label, $url] = surveypro_get_link_and_condition('surveypro');
if ($condition) {
$navigation->add($label, $url, navigation_node::TYPE_SETTING);
}
// Layout.
[$condition, $label, $url] = surveypro_get_link_and_condition('layout');
if ($condition) {
$navigation->add($label, $url, navigation_node::TYPE_SETTING);
}
// Report.
[$condition, $label, $url] = surveypro_get_link_and_condition('reports');
if ($condition) {
$navigation->add($label, $url, navigation_node::TYPE_SETTING);
}
// Tools.
[$condition, $label, $url] = surveypro_get_link_and_condition('tools');
if ($condition) {
$navigation->add($label, $url, navigation_node::TYPE_SETTING);
}
// User templates. (Maybe "User presets" is better?).
[$condition, $label, $url] = surveypro_get_link_and_condition('utemplates');
if ($condition) {
$navigation->add($label, $url, navigation_node::TYPE_SETTING);
}
// Master templates. (Maybe "Master presets" is better?).
[$condition, $label, $url] = surveypro_get_link_and_condition('mtemplates');
if ($condition) {
$navigation->add($label, $url, navigation_node::TYPE_SETTING);
}
}
// CUSTOM SURVEYPRO API.
/**
* Define the link for Navigation block and Administration block
*
* @param string $area
* @return array|string defaultsection
*/
function surveypro_get_defaults_section_per_area($area) {
$defaultsection = [];
$defaultsection['surveypro'] = 'cover';
$defaultsection['layout'] = 'itemslist';
$defaultsection['reports'] = 'view'; // Default section for each report IS ALWAYS 'view'.
$defaultsection['tools'] = 'export';
$defaultsection['utemplates'] = 'manage';
$defaultsection['mtemplates'] = 'save';
// Verify a correct $area was requested.
if (!isset($defaultsection[$area])) {
$message = 'The requested area \''.$area.'\' is invalid.';
debugging('Error at line '.__LINE__.' of file '.__FILE__.'. '.$message , DEBUG_DEVELOPER);
}
// End of: Verify a correct $area was requested.
return $defaultsection[$area];
}
/**
* Define the link for "Navigation block" and "Administration block"
*
* @param string $area
* @return array [$condition, $label, $url]
*/
function surveypro_get_link_and_condition($area) {
global $PAGE, $DB;
if (!$cm = $PAGE->cm) {
return [false, '', ''];
}
$context = \context_module::instance($cm->id);
$pageparams = $PAGE->url->params();
$section = surveypro_get_defaults_section_per_area($area); // Here $section is a string, not an array.
if (isset($pageparams['section'])) {
// Overwrite the default section with the passed one in the passed area ONLY.
if (isset($pageparams['area']) && ($pageparams['area'] == $area)) {
$section = $pageparams['section'];
}
}
$paramurl = ['s' => $cm->instance, 'area' => $area, 'section' => $section];
$label = '';
$url = '';
switch ($area) {
case 'surveypro':
$condition = has_capability('mod/surveypro:submit', $context);
if ($condition) {
if (isset($pageparams['submissionid'])) {
$paramurl['submissionid'] = $pageparams['submissionid'];
}
if (isset($pageparams['mode'])) {
$paramurl['mode'] = $pageparams['mode'];
}
if (isset($pageparams['begin'])) {
$paramurl['begin'] = $pageparams['begin'];
}
$label = get_string('modulename', 'mod_surveypro');
$url = new \moodle_url('/mod/surveypro/view.php', $paramurl);
}
break;
case 'layout':
$condition = has_capability('mod/surveypro:manageitems', $context);