forked from kordan/moodle-mod_surveypro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
1236 lines (1075 loc) · 41.3 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
*/
defined('MOODLE_INTERNAL') || die();
/**
* Some constants
*/
define('SURVEYPRO_VALUELABELSEPARATOR', '::');
define('SURVEYPRO_OTHERSEPARATOR' , '->');
/**
* TABS
*/
define('SURVEYPRO_TABLAYOUT' , 1);
define('SURVEYPRO_TABSUBMISSIONS', 2);
define('SURVEYPRO_TABUTEMPLATES' , 3);
define('SURVEYPRO_TABMTEMPLATES' , 4);
/**
* PAGES
*/
// PAGES in tab LAYOUT.
define('SURVEYPRO_LAYOUT_PREVIEW' , 1);
define('SURVEYPRO_LAYOUT_ITEMS' , 2);
define('SURVEYPRO_LAYOUT_ITEMSETUP', 3);
define('SURVEYPRO_LAYOUT_VALIDATE' , 4);
// PAGES in tab SUBMISSION.
define('SURVEYPRO_SUBMISSION_CPANEL' , 1);
define('SURVEYPRO_SUBMISSION_MANAGE' , 2);
define('SURVEYPRO_SUBMISSION_INSERT' , 3);
define('SURVEYPRO_SUBMISSION_EDIT' , 4);
define('SURVEYPRO_SUBMISSION_READONLY', 5);
define('SURVEYPRO_SUBMISSION_SEARCH' , 6);
define('SURVEYPRO_SUBMISSION_REPORT' , 7);
define('SURVEYPRO_SUBMISSION_IMPORT' , 8);
define('SURVEYPRO_SUBMISSION_EXPORT' , 9);
// PAGES in tab USER TEMPLATES.
define('SURVEYPRO_UTEMPLATES_MANAGE', 1);
define('SURVEYPRO_UTEMPLATES_BUILD' , 2);
define('SURVEYPRO_UTEMPLATES_IMPORT', 3);
define('SURVEYPRO_UTEMPLATES_APPLY' , 4);
// PAGES in tab MASTER TEMPLATES.
define('SURVEYPRO_MTEMPLATES_BUILD', 1);
define('SURVEYPRO_MTEMPLATES_APPLY', 2);
/**
* ITEM TYPES
*/
define('SURVEYPRO_TYPEFIELD' , 'field');
define('SURVEYPRO_TYPEFORMAT', 'format');
/**
* ACTIONS
*/
define('SURVEYPRO_NOACTION' , '0');
/**
* ACTIONS in LAYOUT MANAGEMENT page
*/
define('SURVEYPRO_CHANGEORDER' , '1');
define('SURVEYPRO_HIDEITEM' , '2');
define('SURVEYPRO_SHOWITEM' , '3');
define('SURVEYPRO_DELETEITEM' , '4');
define('SURVEYPRO_DROPMULTILANG' , '5');
define('SURVEYPRO_REQUIREDOFF' , '6');
define('SURVEYPRO_REQUIREDON' , '7');
define('SURVEYPRO_CHANGEINDENT' , '8');
define('SURVEYPRO_ADDTOSEARCH' , '9');
define('SURVEYPRO_REMOVEFROMSEARCH' , '10');
define('SURVEYPRO_MAKESTANDARD' , '11');
define('SURVEYPRO_MAKERESERVED' , '12');
/**
* 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
*/
// VIEW in USER FORM section.
define('SURVEYPRO_NOVIEW' , '0');
define('SURVEYPRO_NEWRESPONSE' , '1');
define('SURVEYPRO_EDITRESPONSE' , '2');
define('SURVEYPRO_READONLYRESPONSE', '3');
// VIEW in ITEM section.
define('SURVEYPRO_EDITITEM' , '4');
define('SURVEYPRO_CHANGEORDERASK' , '5');
// VIEW in RESPONSES section.
define('SURVEYPRO_RESPONSETOPDF' , '6');
/**
* OVERFLOW
*/
define('SURVEYPRO_LEFT_OVERFLOW' , -10);
define('SURVEYPRO_RIGHT_OVERFLOW', -20);
/**
* SENDERS
*/
define('SURVEYPRO_TAB' , 1);
define('SURVEYPRO_BLOCK', 2);
/**
* FEEDBACKMASK
*/
define('SURVEYPRO_NOFEEDBACK', 0);
/**
* ITEMPREFIX
*/
define('SURVEYPRO_ITEMPREFIX', 'surveypro');
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_STATUSINPROGRESS', 1);
define('SURVEYPRO_STATUSCLOSED' , 0);
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_THANKSHTMLFILEAREA' , '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);
// Moodle core API.
require_once($CFG->dirroot . '/lib/formslib.php'); // Needed by unittest.
/**
* 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);
// Stop working with the surveypro table (unless $surveypro->thankshtml_editor['itemid'] != 0).
// Manage userstyle filemanager.
$draftitemid = $surveypro->userstyle_filemanager;
file_save_draft_area_files($draftitemid, $context->id, 'mod_surveypro', SURVEYPRO_STYLEFILEAREA, 0);
// Manage thankshtml editor.
$editoroptions = surveypro_get_editor_options();
if ($draftitemid = $surveypro->thankshtml_editor['itemid']) {
$surveypro->thankshtml = file_save_draft_area_files($draftitemid, $context->id, 'mod_surveypro',
SURVEYPRO_THANKSHTMLFILEAREA, $surveypro->id, $editoroptions, $surveypro->thankshtml_editor['text']);
$surveypro->thankshtmlformat = $surveypro->thankshtml_editor['format'];
$DB->update_record('surveypro', $surveypro);
}
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);
// I don't think classes are available here!
// So, I can't use $utilityman->reset_items_pages();
$whereparams = array('surveyproid' => $surveypro->id);
$DB->set_field('surveypro_item', 'formpage', 0, $whereparams);
$DB->update_record('surveypro', $surveypro);
// Stop working with the surveypro table (unless $surveypro->thankshtml_editor['itemid'] != 0).
// 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 thankshtml editor.
$editoroptions = surveypro_get_editor_options();
if ($draftitemid = $surveypro->thankshtml_editor['itemid']) {
$surveypro->thankshtml = file_save_draft_area_files($draftitemid, $context->id, 'mod_surveypro',
SURVEYPRO_THANKSHTMLFILEAREA, $surveypro->id, $editoroptions, $surveypro->thankshtml_editor['text']);
$surveypro->thankshtmlformat = $surveypro->thankshtml_editor['format'];
$DB->update_record('surveypro', $surveypro);
}
return true;
}
/**
* 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 = array('newpageforchild', 'history', 'saveresume', 'anonymous', 'notifyteachers');
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', array('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 = array('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 = array(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;
$whereparams['plugin'] = $plugin;
if ($deletelist = $DB->get_records('surveypro_item', $whereparams, 'id', 'id')) {
$deletelist = array_keys($deletelist);
list($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', array('surveyproid' => $surveypro->id))) {
$status = false;
}
// Finally, delete the surveypro record.
if (!$DB->delete_records('surveypro', array('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 true;
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;
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) {
}
/**
* Function to be run periodically according to the moodle cron
* This function searches for things that need to be done, such
* as sending out mail, toggling flags etc ...
*
* @return boolean
* @todo Finish documenting this function
*/
function surveypro_cron() {
global $DB;
// Delete too old submissions from surveypro_answer and surveypro_submission.
$saveresumestatus = array(0, 1);
// If $saveresumestatus == 0 then saveresume is not allowed.
// Users leaved responses in progress more than four hours ago...
// I can not believe they are still working on them so I delete thier responses now.
// If $saveresumestatus == 1 then saveresume is allowed
// Users leaved responses in progress more than maximum allowed time delay so I delete thier responses now.
$maxinputdelay = get_config('mod_surveypro', 'maxinputdelay');
foreach ($saveresumestatus as $saveresume) {
if (($saveresume == 1) && ($maxinputdelay == 0)) { // Maxinputdelay == 0 means, please don't delete.
continue;
}
// First step: filter surveypro to the subset having 'saveresume' = $saveresume.
if ($surveypros = $DB->get_records('surveypro', array('saveresume' => $saveresume), null, 'id')) {
$sofar = ($saveresume == 0) ? (4 * 3600) : ($maxinputdelay * 3600);
$sofar = time() - $sofar;
foreach ($surveypros as $surveypro) {
// Second step: for each surveypro,
// filter only submissions having 'status' = SURVEYPRO_STATUSINPROGRESS and timecreated < :sofar.
$where = 'surveyproid = :surveyproid AND status = :status AND timecreated < :sofar';
$whereparams = array('surveyproid' => $surveypro->id, 'status' => SURVEYPRO_STATUSINPROGRESS, 'sofar' => $sofar);
if ($submissions = $DB->get_recordset_select('surveypro_submission', $where, $whereparams, 'surveyproid', 'id')) {
$cm = get_coursemodule_from_instance('surveypro', $surveypro->id, 0, false, MUST_EXIST);
$utilityman = new mod_surveypro_utility($cm, $surveypro);
foreach ($submissions as $submission) {
// Third step: delete each selected submission.
$utilityman->delete_submissions(array('id' => $submission->id));
}
}
}
}
}
return true;
}
/**
* 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, array('surveyproid' => $surveyproid));
}
/**
* Returns all other caps used in the module
*
* @return array
*/
function surveypro_get_extra_capabilities() {
return array('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.
/**
* Returns the lists of all browsable file areas within the given module context
*
* The file area 'intro' for the activity introduction field is added automatically
* by {@link file_browser::get_file_info_context_module()}
*
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @return array of [(string)filearea] => (string)description
*/
function surveypro_get_file_areas($course, $cm, $context) {
return array();
}
/**
* Serves the files from the surveypro file areas
*
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @return void this should never return to the caller
*/
function surveypro_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
global $CFG;
$debug = false;
if ($debug) {
$debugfile = $CFG->dataroot.'/debug_'.date("m.d.y_H:i:s").'.txt';
$debughandle = fopen($debugfile, 'w');
fwrite($debughandle, 'Scrivo dalla riga '.__LINE__.' di '.__FILE__."\n");
fwrite($debughandle, '$course'."\n");
foreach ($course as $k => $v) {
fwrite($debughandle, '$args['.$k.'] = '.$v."\n");
}
fwrite($debughandle, "\n".'$cm'."\n");
foreach ($cm as $k => $v) {
fwrite($debughandle, '$args['.$k.'] = '.$v."\n");
}
fwrite($debughandle, "\n".'$context'."\n");
foreach ($context as $k => $v) {
fwrite($debughandle, '$args['.$k.'] = '.$v."\n");
}
fwrite($debughandle, "\n".'$filearea = '.$filearea."\n");
fwrite($debughandle, "\n".'$args'."\n");
foreach ($args as $k => $v) {
fwrite($debughandle, '$args['.$k.'] = '.$v."\n");
}
fwrite($debughandle, "\n".'$forcedownload = '.$forcedownload."\n");
}
$itemid = (int)array_shift($args);
if ($debug) {
fwrite($debughandle, "\n".'$itemid = '.$itemid."\n");
}
$relativepath = implode('/', $args);
if ($debug) {
fwrite($debughandle, "\n".'$relativepath = '.$relativepath."\n");
}
$fs = get_file_storage();
$fullpath = "/$context->id/mod_surveypro/$filearea/$itemid/$relativepath";
if ($debug) {
fwrite($debughandle, "\n".'$fullpath = '.$fullpath."\n");
}
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
if ($debug) {
fwrite($debughandle, "\n".'$file da problemi: riporterei false'."\n");
} else {
return false;
}
}
if ($debug) {
fclose($debughandle);
}
// Finally send the file.
send_stored_file($file, 0, 0, true); // Download MUST be forced - security!
return false;
}
// Navigation API.
/**
* extend a surveypro navigation settings
*
* @param settings_navigation $settings
* @param navigation_node $navref
* @return void
*/
function surveypro_extend_settings_navigation(settings_navigation $settings, navigation_node $navref) {
global $CFG, $PAGE, $DB;
if (!$cm = $PAGE->cm) {
return;
}
$surveypro = $DB->get_record('surveypro', array('id' => $cm->instance), '*', MUST_EXIST);
$utilityman = new mod_surveypro_utility($cm, $surveypro);
$isallowed = $utilityman->get_admin_elements_visibility(SURVEYPRO_BLOCK);
$paramurlbase = array('s' => $cm->instance);
// SURVEYPRO_TABLAYOUT.
if ($isallowed['tab_layout']['root']) {
// Parent.
$nodelabel = get_string('tablayoutname', 'mod_surveypro');
$navnode = $navref->add($nodelabel, null, navigation_node::TYPE_CONTAINER);
// Children.
if ($isallowed['tab_layout']['preview']) {
$nodelabel = get_string('tabitemspage1', 'mod_surveypro');
$localparamurl = array('s' => $cm->instance);
$nodeurl = new moodle_url('/mod/surveypro/layout_preview.php', $localparamurl);
$navnode->add($nodelabel, $nodeurl, navigation_node::TYPE_SETTING);
}
if ($isallowed['tab_layout']['manage']) {
$nodelabel = get_string('tabitemspage2', 'mod_surveypro');
$nodeurl = new moodle_url('/mod/surveypro/layout_items.php', $paramurlbase);
$navnode->add($nodelabel, $nodeurl, navigation_node::TYPE_SETTING);
}
if ($isallowed['tab_layout']['validate']) {
$nodelabel = get_string('tabitemspage4', 'mod_surveypro');
$nodeurl = new moodle_url('/mod/surveypro/layout_validation.php', $paramurlbase);
$navnode->add($nodelabel, $nodeurl, navigation_node::TYPE_SETTING);
}
}
// SURVEYPRO_TABSUBMISSIONS.
if ($isallowed['tab_submissions']['root']) {
// Parent.
$nodelabel = get_string('tabsubmissionsname', 'mod_surveypro');
$navnode = $navref->add($nodelabel, null, navigation_node::TYPE_CONTAINER);
// Children.
if ($isallowed['tab_submissions']['import']) { // Import.
$nodelabel = get_string('tabsubmissionspage8', 'mod_surveypro');
$nodeurl = new moodle_url('/mod/surveypro/view_import.php', $paramurlbase);
$navnode->add($nodelabel, $nodeurl, navigation_node::TYPE_SETTING);
}
if ($isallowed['tab_submissions']['export']) { // Export.
$nodelabel = get_string('tabsubmissionspage9', 'mod_surveypro');
$nodeurl = new moodle_url('/mod/surveypro/view_export.php', $paramurlbase);
$navnode->add($nodelabel, $nodeurl, navigation_node::TYPE_SETTING);
}
}
// SURVEYPRO_TABUTEMPLATES.
if ($isallowed['tab_utemplate']['root']) {
// Parent.
$nodelabel = get_string('tabutemplatename', 'mod_surveypro');
$navnode = $navref->add($nodelabel, null, navigation_node::TYPE_CONTAINER);
// Children.
if ($isallowed['tab_utemplate']['manage']) {
$nodelabel = get_string('tabutemplatepage1', 'mod_surveypro');
$nodeurl = new moodle_url('/mod/surveypro/utemplate_manage.php', $paramurlbase);
$navnode->add($nodelabel, $nodeurl, navigation_node::TYPE_SETTING);
}
if ($isallowed['tab_utemplate']['save']) {
$nodelabel = get_string('tabutemplatepage2', 'mod_surveypro');
$nodeurl = new moodle_url('/mod/surveypro/utemplate_save.php', $paramurlbase);
$navnode->add($nodelabel, $nodeurl, navigation_node::TYPE_SETTING);
}
if ($isallowed['tab_utemplate']['import']) {
$nodelabel = get_string('tabutemplatepage3', 'mod_surveypro');
$nodeurl = new moodle_url('/mod/surveypro/utemplate_import.php', $paramurlbase);
$navnode->add($nodelabel, $nodeurl, navigation_node::TYPE_SETTING);
}
if ($isallowed['tab_utemplate']['apply']) {
$nodelabel = get_string('tabutemplatepage4', 'mod_surveypro');
$nodeurl = new moodle_url('/mod/surveypro/utemplate_apply.php', $paramurlbase);
$navnode->add($nodelabel, $nodeurl, navigation_node::TYPE_SETTING);
}
}
// SURVEYPRO_TABMTEMPLATES.
if ($isallowed['tab_mtemplate']['root']) {
// Parent.
$nodelabel = get_string('tabmtemplatename', 'mod_surveypro');
$navnode = $navref->add($nodelabel, null, navigation_node::TYPE_CONTAINER);
// Children.
if ($isallowed['tab_mtemplate']['save']) {
$nodelabel = get_string('tabmtemplatepage1', 'mod_surveypro');
$nodeurl = new moodle_url('/mod/surveypro/mtemplate_save.php', $paramurlbase);
$navnode->add($nodelabel, $nodeurl, navigation_node::TYPE_SETTING);
}
if ($isallowed['tab_mtemplate']['apply']) {
$nodelabel = get_string('tabmtemplatepage2', 'mod_surveypro');
$nodeurl = new moodle_url('/mod/surveypro/mtemplate_apply.php', $paramurlbase);
$navnode->add($nodelabel, $nodeurl, navigation_node::TYPE_SETTING);
}
}
// SURVEYPRO REPORTS.
$context = context_module::instance($cm->id);
if ($surveyproreportlist = get_plugin_list('surveyproreport')) {
$canaccessownreports = has_capability('mod/surveypro:accessownreports', $context);
$canaccessreports = has_capability('mod/surveypro:accessreports', $context);
$icon = new pix_icon('i/report', '', 'moodle');
foreach ($surveyproreportlist as $reportname => $reportpath) {
$classname = 'surveyproreport_'.$reportname.'_report';
$reportman = new $classname($cm, $context, $surveypro);
$allowedtemplates = $reportman->allowed_templates();
if ((!$allowedtemplates) || in_array($surveypro->template, $allowedtemplates)) {
if ($canaccessreports || ($reportman->has_student_report() && $canaccessownreports)) {
if ($reportman->report_apply()) {
if (!isset($reportnode)) {
$nodelabel = get_string('report');
$reportnode = $navref->add($nodelabel, null, navigation_node::TYPE_CONTAINER);
}
if ($childreports = $reportman->has_childreports($canaccessreports)) {
$nodelabel = get_string('pluginname', 'surveyproreport_'.$reportname);
$childnode = $reportnode->add($nodelabel, null, navigation_node::TYPE_CONTAINER);
surveypro_add_report_link($surveypro->template, $childreports, $childnode, $reportname, $icon);
} else {
$url = new moodle_url('/mod/surveypro/report/'.$reportname.'/view.php', $paramurlbase);
$nodelabel = get_string('pluginname', 'surveyproreport_'.$reportname);
$reportnode->add($nodelabel, $url, navigation_node::TYPE_SETTING, null, null, $icon);
}
}
}
}
}
}
}
/**
* Recursive function to add links for reports nested as much times as wanted
*
* Uncomment lines of has_childreports method in the surveyproreport_colles_report class of report/colles/classes/report.php file
* to see this function in action.
*
* @param string $templatename
* @param array $childreports
* @param navigation_node $childnode
* @param string $reportname
* @param pix_icon $icon
* @return void
*/
function surveypro_add_report_link($templatename, $childreports, $childnode, $reportname, $icon) {
global $PAGE;
foreach ($childreports as $reportkey => $reportparams) {
$label = get_string($reportkey, 'surveyprotemplate_'.$templatename);
if (is_array(reset($reportparams))) { // If the first element of $reportparams is an array.
$childnode = $childnode->add($label, null, navigation_node::TYPE_CONTAINER);
surveypro_add_report_link($templatename, $reportparams, $childnode, $reportname, $icon);
} else {
$reportparams['s'] = $PAGE->cm->instance;
$url = new moodle_url('/mod/surveypro/report/'.$reportname.'/view.php', $reportparams);
$childnode->add($label, $url, navigation_node::TYPE_SETTING, null, null, $icon);
}
}
}
/**
* Extends the global navigation tree by adding surveypro nodes if there is a relevant content
*
* 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 $navref An object representing the navigation tree node of the surveypro module instance
* @param stdClass $course
* @param stdClass $surveypro
* @param cm_info $cm
* @return void