forked from itamart/moodle-mod_dataform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
1182 lines (1026 loc) · 38 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/>.
/**
* @package mod_dataform
* @copyright 2012 Itamar Tzadok
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* The Dataform has been developed as an enhanced counterpart
* of Moodle's Database activity module (1.9.11+ (20110323)).
* To the extent that Dataform code corresponds to Database code,
* certain copyrights on the Database module may obtain.
*/
/**
* MOD FUNCTIONS WHICH ARE CALLED FROM OUTSIDE THE MODULE
*/
defined('MOODLE_INTERNAL') or die;
/**
* Adds an instance of a dataform
*
* @global object
* @param object $data
* @return $int
*/
function dataform_add_instance($data) {
global $CFG, $DB, $COURSE;
$data->timemodified = time();
if (empty($data->grade)) {
$data->grade = 0;
$data->gradeitems = null;
}
// Max entries.
if (!isset($data->maxentries) or $data->maxentries < -1) {
$data->maxentries = -1;
}
if ($CFG->dataform_maxentries == 0) {
$data->maxentries = 0;
} else if ($CFG->dataform_maxentries > 0 and ($data->maxentries > $CFG->dataform_maxentries or $data->maxentries < 0)) {
$data->maxentries = $CFG->dataform_maxentries;
}
if (!$data->id = $DB->insert_record('dataform', $data)) {
return false;
}
// Activity icon.
if (!empty($data->activityicon)) {
// We need to use context now, so we need to make sure all needed info is already in db.
$DB->set_field('course_modules', 'instance', $data->id, array('id' => $data->coursemodule));
$context = context_module::instance($data->coursemodule);
$options = array('subdirs' => 0,
'maxbytes' => $COURSE->maxbytes,
'maxfiles' => 1,
'accepted_types' => array('image'));
file_save_draft_area_files($data->activityicon, $context->id, 'mod_dataform', 'activityicon', 0, $options);
}
// Calendar.
\mod_dataform\helper\calendar_event::update_event_timeavailable($data);
\mod_dataform\helper\calendar_event::update_event_timedue($data);
// Grading.
if ($data->grade) {
$grademan = \mod_dataform_grade_manager::instance($data->id);
$itemparams = $grademan->get_grade_item_params_from_data($data);
$grademan->update_grade_item(0, $itemparams);
}
return $data->id;
}
/**
* updates an instance of a data
*
* @global object
* @param object $data
* @return bool
*/
function dataform_update_instance($data) {
$id = $data->instance;
$df = mod_dataform_dataform::instance($id);
if (!$df->update($data)) {
return false;
}
return true;
}
/**
* deletes an instance of a data
*
* @global object
* @param int $id
* @return bool
*/
function dataform_delete_instance($id) {
global $DB;
if (!$data = $DB->record_exists('dataform', array('id' => $id))) {
return false;
}
$df = new mod_dataform_dataform($id);
$result = $df->delete();
return $result;
}
/**
* Make sure up-to-date events are created for all Dataform instances
*
* This standard function will check all instances of this module
* and make sure there are up-to-date events created for each of them.
* If courseid = 0, then every instance event in the site is checked, else
* only instance events belonging to the course specified are checked.
* This function is used, in its new format, by restore_refresh_events()
*
* @param $courseid int optional If zero then all module instances for all courses are covered
* @return boolean Always returns true
*/
function dataform_refresh_events($courseid = 0) {
global $DB;
if ($courseid == 0) {
if (!$dataforms = $DB->get_records('dataform')) {
return true;
}
} else {
if (!$dataforms = $DB->get_records('dataform', array('course' => $courseid))) {
return true;
}
}
$moduleid = $DB->get_field('modules', 'id', array('name' => 'dataform'));
foreach ($dataforms as $data) {
$data->module = $moduleid;
$cm = get_coursemodule_from_instance('dataform', $data->id, $courseid, false, MUST_EXIST);
$data->coursemodule = $cm->id;
\mod_dataform\helper\calendar_event::update_event_timeavailable($data);
\mod_dataform\helper\calendar_event::update_event_timedue($data);
}
return true;
}
/**
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, null if doesn't know
*/
function dataform_supports($feature) {
switch($feature) {
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_GROUPMEMBERSONLY:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_ADVANCED_GRADING:
return true;
case FEATURE_GRADE_OUTCOMES:
return true;
case FEATURE_BACKUP_MOODLE2:
return true;
default:
return null;
}
}
/**
* Return a list of page types
*
* @param string $pagetype current page type
* @param stdClass $parentcontext Block's parent context
* @param stdClass $currentcontext Current context of block
*/
function dataform_page_type_list($pagetype, $parentcontext, $currentcontext) {
$modulepagetype = array(
'mod-dataform-*' => get_string('page-mod-dataform-x', 'dataform'),
'mod-dataform-view-index' => get_string('page-mod-dataform-view-index', 'dataform'),
'mod-dataform-field-index' => get_string('page-mod-dataform-field-index', 'dataform'),
'mod-dataform-access-index' => get_string('page-mod-dataform-access-index', 'dataform'),
'mod-dataform-notification-index' => get_string('page-mod-dataform-notification-index', 'dataform'),
);
return $modulepagetype;
}
/**
* Given a coursemodule object, this function returns the extra
* information needed to print this activity in various places.
*
* If displaying custom icon we store additional information
* in customdata, so functions {@link folder_cm_info_dynamic()} and
* {@link folder_cm_info_view()} do not need to do DB queries
*
* @param cm_info $cm
* @return cached_cm_info info
*/
function dataform_get_coursemodule_info($cm) {
global $DB;
if (!$dataform = $DB->get_record('dataform', array('id' => $cm->instance), 'id, name, intro, introformat, inlineview, embedded')) {
return null;
}
$cminfo = new cached_cm_info();
// Activity custom icon.
if ($customiconurl = dataform_get_custom_icon_url($cm->id)) {
$cminfo->iconurl = $customiconurl;
}
// Inline view.
if (!empty($dataform->inlineview)) {
$cminfo->customdata = $dataform;
return $cminfo;
}
$cminfo->name = $dataform->name;
if ($cm->showdescription) {
$cminfo->content = format_module_intro('dataform', $dataform, $cm->id, false);
}
return $cminfo;
}
/**
* Sets dynamic information about a course module
*
* This function is called from cm_info when displaying the module
* mod_dataform can be displayed inline on course page in which case it should not have course link
*
* @param cm_info $cm
*/
function dataform_cm_info_dynamic(cm_info $cm) {
if ($customdata = $cm->customdata and !empty($customdata->inlineview)) {
// See CONTRIB-6109.
$cm->set_extra_classes('dataform-inlineview');
}
}
/**
* Overwrites the content in the course-module object with the Dataform view content
* if dataform.inlineview is not empty
*
* @param cm_info $cm
*/
function dataform_cm_info_view(cm_info $cm) {
global $PAGE, $CFG, $OUTPUT;
if (!$cm->uservisible) {
return;
}
// Default content if not displaying inline view.
if (!$dataform = $cm->customdata or empty($dataform->inlineview)) {
return;
}
if (!empty($dataform->embedded)) {
$content = mod_dataform_dataform::get_content_embedded($dataform->id, $dataform->inlineview);
} else {
$content = mod_dataform_dataform::get_content_inline($dataform->id, $dataform->inlineview);
}
if (!empty($content)) {
$cm->set_content($content);
}
}
/**
* Gets the instance custom icon if exists.
* Filearea: activityicon
* Itemid: 0
*
* @param int cm id
* @return moodle_url
*/
function dataform_get_custom_icon_url($cmid) {
$fs = get_file_storage();
$context = context_module::instance($cmid);
if ($files = $fs->get_area_files($context->id, 'mod_dataform', 'activityicon', 0, 'sortorder', false)) {
$file = reset($files);
$filename = $file->get_filename();
$path = "/{$context->id}/mod_dataform/activityicon/0";
return moodle_url::make_file_url('/pluginfile.php', "$path/$filename");
}
return null;
}
/**
* Obtains the automatic completion state for this dataform based on any conditions
* in dataform settings.
*
* @global object
* @global object
* @param object $course Course
* @param object $cm Course-module
* @param int $userid User ID
* @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
* @return bool True if completed, false if not. (If no conditions, then return
* value depends on comparison type)
*/
function dataform_get_completion_state($course, $cm, $userid, $type) {
global $DB;
// Get dataform details.
if (!($dataform = $DB->get_record('dataform', array('id' => $cm->instance)))) {
throw new Exception("Can't find dataform {$cm->instance}");
}
$result = $type;
// Required entries.
if ($dataform->completionentries) {
$entriescount = $DB->count_records('dataform_entries', array('dataid' => $dataform->id, 'userid' => $userid));
$value = ($entriescount >= $dataform->completionentries);
if ($type == COMPLETION_AND) {
$result = $result && $value;
} else {
$result = $result || $value;
}
}
// Required specific grade.
if ($dataform->completionspecificgrade) {
// Get the user's grade.
$params = array(
'itemtype' => 'mod',
'itemmodule' => 'dataform',
'iteminstance' => $dataform->id,
'courseid' => $course->id,
'itemnumber' => 0
);
$gitem = grade_item::fetch($params);
$grade = $gitem->get_grade($userid, false);
$value = ($grade->finalgrade >= $dataform->completionspecificgrade);
if ($type == COMPLETION_AND) {
$result = $result && $value;
} else {
$result = $result || $value;
}
}
return $result;
}
// BACKUP/RESTORE.
/**
* Checks if a scale is being used by a dataform,
*
* This is used by the backup code to decide whether to back up a scale
* @param $dataformid int
* @param $scaleid int
* @return boolean True if the scale is used by the dataform
*/
function dataform_scale_used($dataformid, $scaleid) {
global $DB;
if ($scaleid) {
// Check the dataform instance.
if ($DB->record_exists('dataform', array('id' => $dataformid, 'grade' => -$scaleid))) {
return true;
}
// Check all fields which are instances of interface usingscale.
foreach (array_keys(core_component::get_plugin_list('dataformfield')) as $type) {
$fieldclass = "dataformfield_{$type}_$type";
if (is_subclass_of($fieldclass, '\mod_dataform\interfaces\usingscale')) {
if ($fieldclass::is_using_scale($scaleid, $dataformid)) {
return true;
}
}
}
}
return false;
}
/**
* Checks if scale is being used in any instance of dataform.
*
* This is used to find out if scale used anywhere
* @param $scaleid int
* @return boolean True if the scale is used in any dataform
*/
function dataform_scale_used_anywhere($scaleid) {
global $DB;
if ($scaleid) {
if ($DB->record_exists('dataform', array('grade' => -$scaleid))) {
return true;
}
// Check all fields which are instances of interface usingscale.
foreach (array_keys(core_component::get_plugin_list('dataformfield')) as $type) {
$fieldclass = "dataformfield_{$type}_$type";
if (is_subclass_of($fieldclass, '\mod_dataform\interfaces\usingscale')) {
if ($fieldclass::is_using_scale($scaleid)) {
return true;
}
}
}
}
return false;
}
// RESET.
/**
* prints the form elements that control
* whether the course reset functionality affects the data.
*
* @param $mform form passed by reference
*/
function dataform_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'dataformheader', get_string('modulenameplural', 'dataform'));
$mform->addElement('checkbox', 'reset_dataform_data', get_string('entriesdeleteall', 'dataform'));
$mform->addElement('checkbox', 'reset_dataform_notenrolled', get_string('deletenotenrolled', 'dataform'));
$mform->disabledIf('reset_dataform_notenrolled', 'reset_dataform_data', 'checked');
}
/**
* Course reset form defaults.
* @return array
*/
function dataform_reset_course_form_defaults($course) {
return array('reset_dataform_data' => 0, 'reset_dataform_notenrolled' => 0);
}
/**
* Actual implementation of the rest coures functionality, delete all the
* data responses for course $data->courseid.
*
* @global object
* @global object
* @param object $data the data submitted from the reset course.
* @return array status array
*/
function dataform_reset_userdata($data) {
global $DB;
$componentstr = get_string('modulenameplural', 'dataform');
$entriesdeleteallstr = get_string('entriesdeleteall', 'dataform');
$deletenotenrolledstr = get_string('deletenotenrolled', 'dataform');
$datachangedstr = get_string('datechanged');
$status = array();
if (!$dataforms = $DB->get_records('dataform', array('course' => $data->courseid), '', 'id')) {
return $status;
}
foreach (array_keys($dataforms) as $dataformid) {
$df = mod_dataform_dataform::instance($dataformid);
// Delete all user data.
if (!empty($data->reset_dataform_data)) {
$df->reset_user_data();
$status['entriesdeleteall'] = array('component' => $componentstr, 'item' => $entriesdeleteallstr, 'error' => false);
}
// Delete user data for not enrolled users.
if (!empty($data->reset_dataform_notenrolled)) {
$sql = "
SELECT
e.id,
e.userid,
e.dataid,
u.id AS userexists,
u.deleted AS userdeleted
FROM
{dataform_entries} e
LEFT JOIN {user} u ON e.userid = u.id
WHERE
e.dataid = ?
";
$coursecontext = context_course::instance($this->course->id);
$skipped = array();
$notenrolled = array();
$entries = $DB->get_records_sql($sql, array($dataformid));
foreach ($entries as $entry) {
if (array_key_exists($entry->userid, $notenrolled)) {
continue;
}
if (array_key_exists($entry->userid, $skipped)) {
continue;
}
if (!$entry->userexists or $entry->userdeleted or !is_enrolled($coursecontext, $entry->userid)) {
$df->reset_user_data($entry->userid);
$notenrolled[$entry->userid] = true;
continue;
}
$skipped[$entry->userid] = true;
$status['deletenotenrolled'] = array('component' => $componentstr, 'item' => $deletenotenrolledstr, 'error' => false);
}
}
// Updating dates - shift may be negative too.
if ($data->timeshift) {
shift_course_mod_dates('dataform', array('timeavailable', 'timedue'), $data->timeshift, $data->courseid);
$status['datechanged'] = array('component' => $componentstr, 'item' => $datechangedstr, 'error' => false);
}
}
return $status;
}
/**
* Removes all grades from gradebook
*
* @global object
* @global object
* @param int $courseid
* @param string $type optional type
*/
function dataform_reset_gradebook($courseid, $type = '') {
global $DB;
if ($dataforms = $DB->get_records('dataform', array('course' => $courseid))) {
foreach ($dataforms as $dataform) {
dataform_grade_item_update($dataform, 'reset');
}
}
}
// PERMISSIONS AND NAVIGATION.
/**
* Returns all other caps used in module
*
* @return array
*/
function dataform_get_extra_capabilities() {
return array('moodle/site:accessallgroups',
'moodle/site:viewfullnames',
'moodle/rating:view',
'moodle/rating:viewany',
'moodle/rating:viewall',
'moodle/rating:rate',
'moodle/comment:view',
'moodle/comment:post',
'moodle/comment:delete');
}
/**
* Lists all browsable file areas
*
* @param object $course
* @param object $cm
* @param object $context
* @return array
*/
function dataform_get_file_areas($course, $cm, $context) {
$areas = array();
return $areas;
}
/**
* Serves the dataform attachments. Implements needed access control ;-)
*
* @param object $course
* @param object $cm
* @param object $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @return bool false if file not found, does not return if found - justsend the file
*/
function mod_dataform_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
global $CFG, $DB, $USER;
// DATAFORM activity icon.
if ($filearea === 'activityicon' and $context->contextlevel == CONTEXT_MODULE) {
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_dataform/activityicon/$relativepath";
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
// Finally send the file.
send_stored_file($file, 0, 0, true); // Download MUST be forced - security!
}
// FIELD CONTENT files.
if ($filearea === 'content' and $context->contextlevel == CONTEXT_MODULE) {
$contentidhash = array_shift($args);
$df = \mod_dataform_dataform::instance($cm->instance);
if (!$contentid = $df->get_content_id_from_hash($contentidhash)) {
return false;
}
if (!$content = $DB->get_record('dataform_contents', array('id' => $contentid))) {
return false;
}
if (!$field = $DB->get_record('dataform_fields', array('id' => $content->fieldid))) {
return false;
}
require_course_login($course, true, $cm);
if (!$entry = $DB->get_record('dataform_entries', array('id' => $content->entryid))) {
return false;
}
if (!$dataform = $DB->get_record('dataform', array('id' => $field->dataid))) {
return false;
}
if ($dataform->id != $cm->instance) {
// Hacker attempt - context does not match the contentid.
return false;
}
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_dataform/content/$contentid/$relativepath";
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
// Finally send the file.
send_stored_file($file, 0, 0, true); // Download MUST be forced - security!
}
// PRESET files.
if (($filearea === 'course_presets' or $filearea === 'site_presets')) {
require_course_login($course, true, $cm);
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_dataform/$filearea/$relativepath";
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
// Finally send the file.
send_stored_file($file, 0, 0, true); // Download MUST be forced - security!
}
if (($filearea === 'js' or $filearea === 'css')) {
require_course_login($course, true, $cm);
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_dataform/$filearea/$relativepath";
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
// Finally send the file.
send_stored_file($file, 0, 0, true); // Download MUST be forced - security!
}
return false;
}
/**
*
*/
function dataform_extend_navigation($navigation, $course, $module, $cm) {
global $PAGE, $USER, $CFG;
$df = mod_dataform_dataform::instance($cm->instance);
if ($views = $df->view_manager->views_navigation_menu) {
foreach ($views as $viewid => $name) {
$navigation->add($name, new moodle_url('/mod/dataform/view.php', array('d' => $df->id, 'view' => $viewid)));
}
}
// RSS links.
if (!empty($CFG->enablerssfeeds) and !empty($CFG->dataform_enablerssfeeds)) {
if ($rssviews = $df->get_rss_views()) {
require_once("$CFG->libdir/rsslib.php");
$rssstr = get_string('rss');
foreach ($rssviews as $viewid => $view) {
$feedname = $view->name;
$componentinstance = "$df->id/$viewid";
$url = new moodle_url(rss_get_url($PAGE->cm->context->id, $USER->id, 'mod_dataform', $componentinstance));
$navigation->add($feedname, $url, settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', $rssstr));
}
}
}
}
/**
* Adds module specific settings to the settings block.
*
* @param settings_navigation $settings The settings navigation object
* @param navigation_node $datanode The node to add module settings to
*/
function dataform_extend_settings_navigation(settings_navigation $settings, navigation_node $dfnode) {
global $PAGE, $CFG;
// MANAGEMENT.
// Must be activity manager.
$df = \mod_dataform_dataform::instance(null, $PAGE->cm->id);
if ($manager = $df->user_manage_permissions) {
if ($manager['templates']) {
// Grade items.
if ($CFG->dataform_multigradeitems) {
$params = array('id' => $PAGE->cm->id);
$url = new moodle_url('/mod/dataform/grade/items.php', $params);
$dfnode->add(get_string('gradeitems', 'dataform'), $url);
}
// Renew activity.
$params = array('id' => $PAGE->cm->id, 'renew' => 1, 'sesskey' => sesskey());
$dfnode->add(get_string('renewactivity', 'dataform'), new moodle_url('/mod/dataform/view.php', $params));
// Delete activity.
$params = array('delete' => $PAGE->cm->id, 'sesskey' => sesskey());
$dfnode->add(get_string('deleteactivity', 'dataform'), new moodle_url('/course/mod.php', $params));
}
// Manage.
$manage = $dfnode;
if ($manager['views']) {
$params = array('id' => $PAGE->cm->id);
$manage->add(get_string('dataform:manageviews', 'dataform'), new moodle_url('/mod/dataform/view/index.php', $params));
}
if ($manager['fields']) {
$params = array('id' => $PAGE->cm->id);
$manage->add(get_string('dataform:managefields', 'dataform'), new moodle_url('/mod/dataform/field/index.php', $params));
}
if ($manager['filters']) {
$params = array('id' => $PAGE->cm->id);
$manage->add(get_string('dataform:managefilters', 'dataform'), new moodle_url('/mod/dataform/filter/index.php', $params));
}
if ($manager['access']) {
$params = array('id' => $PAGE->cm->id);
$manage->add(get_string('dataform:manageaccess', 'dataform'), new moodle_url('/mod/dataform/access/index.php', $params));
}
if ($manager['notifications']) {
$params = array('id' => $PAGE->cm->id);
$manage->add(get_string('dataform:managenotifications', 'dataform'), new moodle_url('/mod/dataform/notification/index.php', $params));
}
if ($manager['css']) {
$params = array('id' => $PAGE->cm->id, 'cssedit' => 1);
$manage->add(get_string('cssinclude', 'dataform'), new moodle_url('/mod/dataform/css.php', $params));
}
if ($manager['js']) {
$params = array('id' => $PAGE->cm->id, 'jsedit' => 1);
$manage->add(get_string('jsinclude', 'dataform'), new moodle_url('/mod/dataform/js.php', $params));
}
if ($manager['tools']) {
$params = array('id' => $PAGE->cm->id);
$manage->add(get_string('tools', 'dataform'), new moodle_url('/mod/dataform/tool/index.php', $params));
}
if ($manager['presets']) {
$params = array('id' => $PAGE->cm->id);
$manage->add(get_string('presets', 'dataform'), new moodle_url('/mod/dataform/preset/index.php', $params));
}
}
// View gradebook.
if ($df->grade) {
$params = array('id' => $PAGE->course->id);
$dfnode->add(get_string('gradebook', 'grades'), new moodle_url('/grade/report/index.php', $params));
}
// Module administration (requires site:config).
if (has_capability('moodle/site:config', context_system::instance())) {
$url = new \moodle_url('/admin/settings.php', array('section' => 'modsettingdataform'));
$dfnode->add(get_string('modulesettings', 'dataform'), $url);
}
// Index.
$coursecontext = context_course::instance($PAGE->course->id);
if (has_capability('mod/dataform:indexview', $coursecontext)) {
$params = array('id' => $PAGE->course->id);
$dfnode->add(get_string('index', 'dataform'), new moodle_url('/mod/dataform/index.php', $params));
}
}
// INFO.
/**
* returns a list of participants of this dataform
*/
function dataform_get_participants($dataid) {
global $DB;
$params = array('dataid' => $dataid);
$sql = "SELECT DISTINCT u.id
FROM {user} u,
{dataform_entries} e
WHERE e.dataid = :dataid AND
u.id = e.userid";
$entries = $DB->get_records_sql($sql, $params);
$sql = "SELECT DISTINCT u.id
FROM {user} u,
{dataform_entries} e,
{comments} c
WHERE e.dataid = ? AND
u.id = e.userid AND
e.id = c.itemid AND
c.commentarea = 'entry'";
$comments = $DB->get_records_sql($sql, $params);
$sql = "SELECT DISTINCT u.id
FROM {user} u,
{dataform_entries} e,
{ratings} r
WHERE e.dataid = ? AND
u.id = e.userid AND
e.id = r.itemid AND
r.component = 'mod_dataform'";
$ratings = $DB->get_records_sql($sql, $params);
$participants = array();
if ($entries) {
foreach ($entries as $entry) {
$participants[$entry->id] = $entry;
}
}
if ($comments) {
foreach ($comments as $comment) {
$participants[$comment->id] = $comment;
}
}
if ($ratings) {
foreach ($ratings as $rating) {
$participants[$rating->id] = $rating;
}
}
return $participants;
}
/**
* returns a summary of data activity of this user
*
* @global object
* @param object $course
* @param object $user
* @param object $mod
* @param object $data
* @return object|null
*/
function dataform_user_outline($course, $user, $mod, $data) {
global $DB, $CFG;
require_once("$CFG->libdir/gradelib.php");
$grades = grade_get_grades($course->id, 'mod', 'dataform', $data->id, $user->id);
if (empty($grades->items[0]->grades)) {
$grade = false;
} else {
$grade = reset($grades->items[0]->grades);
}
$sqlparams = array('dataid' => $data->id, 'userid' => $user->id);
if ($countrecords = $DB->count_records('dataform_entries', $sqlparams)) {
$result = new stdClass;
$result->info = get_string('entriescount', 'dataform', $countrecords);
$lastrecordset = $DB->get_records(
'dataform_entries',
$sqlparams,
'timemodified DESC',
'id,timemodified',
0,
1
);
$lastrecord = reset($lastrecordset);
$result->time = $lastrecord->timemodified;
if ($grade) {
$result->info .= ', ' . get_string('grade') . ': ' . $grade->str_long_grade;
}
return $result;
} else if ($grade) {
$result = new stdClass;
$result->info = get_string('grade') . ': ' . $grade->str_long_grade;
$result->time = $grade->dategraded;
return $result;
}
return null;
}
/**
* Print a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @param object $course
* @param object $user
* @param object $mod
* @param object $dataform
* @return bool
*/
function dataform_user_complete($course, $user, $mod, $dataform) {
global $DB, $CFG, $OUTPUT;
require_once("$CFG->libdir/gradelib.php");
$grades = grade_get_grades($course->id, 'mod', 'dataform', $dataform->id, $user->id);
if (!empty($grades->items[0]->grades)) {
$grade = reset($grades->items[0]->grades);
echo $OUTPUT->container(get_string('grade').': '.$grade->str_long_grade);
if ($grade->str_feedback) {
echo $OUTPUT->container(get_string('feedback').': '.$grade->str_feedback);
}
}
$df = new mod_dataform_dataform($dataform->id);
if (!$df->defaultview) {
return true;
}
if ($view = $df->view_manager->get_view_by_id($df->defaultview)) {
$view->set_viewfilter(array('users' => array($user->id)));
$view->section = '##entries##';
$viewcontent = $view->display();
echo $viewcontent;
}
return true;
}
/**
* Generates overview info for instances in the specified courses. An instance
* has an overview for a user if the instance contains a view of type
* 'overview' (dataformview_overview) that is accessible by the user. The content
* of the overview is the content of that view.
*
* @param mixed $courses The list of courses to genenrate the overview for.
* @param array $htmlarray The array of html to return.
*
* @return true
*/
function dataform_print_overview($courses, &$htmlarray) {
global $CFG, $DB;
if (empty($courses) || !is_array($courses) || count($courses) == 0) {
return true;
}
if (!$dataforms = get_all_instances_in_courses('dataform', $courses)) {
return true;
}
// Collate overview info into $htmlarray.
foreach ($dataforms as $dataform) {
$overview = '';
// Get the instance overview views.
$df = new \mod_dataform_dataform($dataform->id);
if ($views = $df->view_manager->get_views_by_instanceof('mod_dataform\interfaces\overview')) {
// Remove unpermitted.
foreach ($views as $viewid => $view) {
$params = array('dataformid' => $dataform->id, 'viewid' => $viewid);
if (!mod_dataform\access\view_access::validate($params)) {
continue;
}
$params = array(