forked from Wunderbyte-GmbH/moodle-mod_datalynx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
1465 lines (1277 loc) · 50.5 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 mod_datalynx for Moodle - http://moodle.org/
//
// It 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.
//
// It 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
* @subpackage mod_datalynx
* @copyright 2013 onwards Ivan Sakic, David Bogner, Michael Pollak and others
* @copyright based on the work by 2012 Itamar Tzadok
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
defined('MOODLE_INTERNAL') || die();
/**
* MOD FUNCTIONS WHICH ARE CALLED FROM OUTSIDE THE MODULE
*/
/**
* Indicates API features that the datalynx supports.
*
* @param string $feature
* @return mixed true if yes (some features may use other values)
*/
function datalynx_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_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_GRADE_OUTCOMES:
return true;
case FEATURE_RATE:
return true;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
default:
return null;
}
}
/**
* Adds an instance of a datalynx
*
* @global object
* @param object $data
* @return $int
*/
function datalynx_add_instance($data) {
global $CFG, $DB;
$data->timemodified = time();
if (empty($data->grade)) {
$data->grade = 0;
$data->grademethod = 0;
}
if (!empty($data->assessed)) {
$data->grademethod = $data->assessed;
} else {
$data->rating = 0;
}
if (empty($data->ratingtime) || empty($data->assessed)) {
$data->assesstimestart = 0;
$data->assesstimefinish = 0;
}
if (!empty($data->scale)) {
$data->rating = $data->scale;
} else {
$data->rating = 0;
}
if ($CFG->datalynx_maxentries) {
$data->maxentries = $CFG->datalynx_maxentries;
}
$data->id = $DB->insert_record('datalynx', $data);
if (!$data->id) {
return false;
}
datalynx_grade_item_update($data);
return $data->id;
}
/**
* updates an instance of a data
*
* @global object
* @param object $data
* @return bool
*/
function datalynx_update_instance($data) {
global $DB;
$data->id = $data->instance;
$data->timemodified = time();
if (empty($data->grade)) {
$data->grade = 0;
$data->grademethod = 0;
}
if (!empty($data->assessed)) {
$data->grademethod = $data->assessed;
} else {
$data->rating = 0;
}
if (empty($data->ratingtime) || empty($data->assessed)) {
$data->assesstimestart = 0;
$data->assesstimefinish = 0;
}
if (!empty($data->scale)) {
$data->rating = $data->scale;
} else {
$data->rating = 0;
}
if (!$DB->update_record('datalynx', $data)) {
return false;
}
datalynx_update_grades($data);
return true;
}
/**
* deletes an instance of a data
*
* @global object
* @param int $id
* @return bool
*/
function datalynx_delete_instance($id) {
global $DB;
$data = $DB->get_record('datalynx', array('id' => $id));
if (!$data) {
return false;
}
$cm = get_coursemodule_from_instance('datalynx', $data->id);
$context = context_module::instance($cm->id);
// Files.
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_datalynx');
// Get all the content in this datalynx.
$sql = "SELECT e.id FROM {datalynx_entries} e WHERE e.dataid = ?";
$DB->delete_records_select('datalynx_contents', "entryid IN ($sql)", array($id));
// Delete fields views filters entries.
$DB->delete_records('datalynx_fields', array('dataid' => $id));
$DB->delete_records('datalynx_views', array('dataid' => $id));
$DB->delete_records('datalynx_filters', array('dataid' => $id));
$DB->delete_records('datalynx_entries', array('dataid' => $id));
$DB->delete_records('datalynx_rules', array('dataid' => $id));
// Delete the instance itself.
$result = $DB->delete_records('datalynx', array('id' => $id));
// Cleanup gradebook.
datalynx_grade_item_delete($data);
return $result;
}
/**
* 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
* @return array
*/
function datalynx_page_type_list($pagetype, $parentcontext, $currentcontext) {
$modulepagetype = array('mod-datalynx-*' => get_string('page-mod-datalynx-x', 'datalynx'));
return $modulepagetype;
}
// RESET.
/**
* prints the form elements that control
* whether the course reset functionality affects the data.
*
* @param $mform form passed by reference
*/
function datalynx_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'datalynxheader', get_string('modulenameplural', 'datalynx'));
$mform->addElement('checkbox', 'reset_datalynx_data', get_string('entriesdeleteall',
'datalynx'));
$mform->addElement('checkbox', 'reset_datalynx_notenrolled',
get_string('deletenotenrolled', 'datalynx'));
$mform->disabledIf('reset_datalynx_notenrolled', 'reset_datalynx_data', 'checked');
$mform->addElement('checkbox', 'reset_datalynx_ratings', get_string('deleteallratings'));
$mform->disabledIf('reset_datalynx_ratings', 'reset_datalynx_data', 'checked');
$mform->addElement('checkbox', 'reset_datalynx_comments', get_string('deleteallcomments'));
$mform->disabledIf('reset_datalynx_comments', 'reset_datalynx_data', 'checked');
}
/**
* Course reset form defaults.
*
* @return array
*/
function datalynx_reset_course_form_defaults($course) {
return array('reset_datalynx_data' => 0, 'reset_datalynx_ratings' => 1,
'reset_datalynx_comments' => 1, 'reset_datalynx_notenrolled' => 0
);
}
/**
* Removes all grades from gradebook
*
* @global object
* @global object
* @param int $courseid
* @param string $type optional type
*/
function datalynx_reset_gradebook($courseid, $type = '') {
global $DB;
$sql = "SELECT d.*, cm.idnumber as cmidnumber, d.course as courseid
FROM {datalynx} d, {course_modules} cm, {modules} m
WHERE m.name='datalynx' AND m.id=cm.module AND cm.instance=d.id AND d.course=?";
if ($datalynxs = $DB->get_records_sql($sql, array($courseid))) {
foreach ($datalynxs as $datalynx) {
datalynx_grade_item_update($datalynx, 'reset');
}
}
}
/**
* 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 datalynx_reset_userdata($data) {
global $CFG, $DB;
require_once($CFG->libdir . '/filelib.php');
require_once($CFG->dirroot . '/rating/lib.php');
$componentstr = get_string('modulenameplural', 'datalynx');
$status = array();
$allrecordssql = "SELECT e.id
FROM {datalynx_entries} e
INNER JOIN {datalynx} d ON e.dataid = d.id
WHERE d.course = ?";
$alldatassql = "SELECT d.id
FROM {datalynx} d
WHERE d.course=?";
$rm = new rating_manager();
$ratingdeloptions = new stdClass();
$ratingdeloptions->component = 'mod_datalynx';
$ratingdeloptions->ratingarea = 'entry';
// Delete entries if requested.
if (!empty($data->reset_datalynx_data)) {
$DB->delete_records_select('comments', "itemid IN ($allrecordssql) AND commentarea='entry'",
array($data->courseid));
$DB->delete_records_select('datalynx_contents', "entryid IN ($allrecordssql)",
array($data->courseid));
$DB->delete_records_select('datalynx_entries', "dataid IN ($alldatassql)",
array($data->courseid));
if ($datas = $DB->get_records_sql($alldatassql, array($data->courseid))) {
foreach ($datas as $dataid => $unused) {
fulldelete("$CFG->dataroot/$data->courseid/moddata/datalynx/$dataid");
$cm = get_coursemodule_from_instance('datalynx', $dataid);
if (!$cm) {
continue;
}
$datacontext = context_module::instance($cm->id);
$ratingdeloptions->contextid = $datacontext->id;
$rm->delete_ratings($ratingdeloptions);
}
}
if (empty($data->reset_gradebook_grades)) {
// Remove all grades from gradebook.
datalynx_reset_gradebook($data->courseid);
}
$status[] = array('component' => $componentstr,
'item' => get_string('entriesdeleteall', 'datalynx'), 'error' => false);
}
// Remove entries by users not enrolled into course.
if (!empty($data->reset_datalynx_notenrolled)) {
$recordssql = "SELECT e.id, e.userid, e.dataid, u.id AS userexists, u.deleted AS userdeleted
FROM {datalynx_entries} e
INNER JOIN {datalynx} d ON e.dataid = d.id
LEFT OUTER JOIN {user} u ON e.userid = u.id
WHERE d.course = ? AND e.userid > 0";
$coursecontext = context_course::instance($data->courseid);
$notenrolled = array();
$fields = array();
$rs = $DB->get_recordset_sql($recordssql, array($data->courseid));
foreach ($rs as $record) {
if (array_key_exists($record->userid, $notenrolled) || !$record->userexists ||
$record->userdeleted || !is_enrolled($coursecontext, $record->userid)
) {
// Delete ratings.
$cm = get_coursemodule_from_instance('datalynx', $record->dataid);
if (!$cm) {
continue;
}
$datacontext = context_module::instance($cm->id);
$ratingdeloptions->contextid = $datacontext->id;
$ratingdeloptions->itemid = $record->id;
$rm->delete_ratings($ratingdeloptions);
$DB->delete_records('comments', array('itemid' => $record->id, 'commentarea' => 'entry'));
$DB->delete_records('datalynx_contents', array('entryid' => $record->id));
$DB->delete_records('datalynx_entries', array('id' => $record->id));
// HACK: this is ugly - the entryid should be before the fieldid!
if (!array_key_exists($record->dataid, $fields)) {
if ($fs = $DB->get_records('datalynx_fields', array('dataid' => $record->dataid))) {
$fields[$record->dataid] = array_keys($fs);
} else {
$fields[$record->dataid] = array();
}
}
foreach ($fields[$record->dataid] as $fieldid) {
fulldelete(
"$CFG->dataroot/$data->courseid/moddata/datalynx/$record->dataid/$fieldid/$record->id");
}
$notenrolled[$record->userid] = true;
}
rs_close($rs);
$status[] = array('component' => $componentstr,
'item' => get_string('deletenotenrolled', 'datalynx'), 'error' => false);
}
}
// Remove all ratings.
if (!empty($data->reset_datalynx_ratings)) {
if ($datas = $DB->get_records_sql($alldatassql, array($data->courseid))) {
foreach ($datas as $dataid => $unused) {
$cm = get_coursemodule_from_instance('datalynx', $dataid);
if (!$cm) {
continue;
}
$datacontext = context_module::instance($cm->id);
$ratingdeloptions->contextid = $datacontext->id;
$rm->delete_ratings($ratingdeloptions);
}
}
if (empty($data->reset_gradebook_grades)) {
// Remove all grades from gradebook.
datalynx_reset_gradebook($data->courseid);
}
$status[] = array('component' => $componentstr, 'item' => get_string('deleteallratings'),
'error' => false);
}
// Remove all comments.
if (!empty($data->reset_datalynx_comments)) {
$DB->delete_records_select('comments', "itemid IN ($allrecordssql) AND commentarea='entry'",
array($data->courseid));
$status[] = array('component' => $componentstr, 'item' => get_string('deleteallcomments'),
'error' => false);
}
// Updating dates - shift may be negative too.
if ($data->timeshift) {
shift_course_mod_dates('datalynx', array('timeavailable', 'timedue'), $data->timeshift, $data->courseid);
$status[] = array('component' => $componentstr, 'item' => get_string('datechanged'),
'error' => false);
}
return $status;
}
/**
* Returns all other caps used in module
*
* @return array
*/
function datalynx_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 datalynx_get_file_areas($course, $cm, $context) {
$areas = array('viewsection' => 'View template files', 'viewparam2' => 'Entry template files',
'content' => 'Entry content files'
);
return $areas;
}
/**
* File browsing support for datalynx 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 datalynx_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid,
$filepath, $filename) {
global $CFG, $DB, $USER;
if ($context->contextlevel != CONTEXT_MODULE) {
return null;
}
if (!isset($areas[$filearea])) {
return null;
}
if (is_null($itemid)) {
require_once($CFG->dirroot . '/mod/datalynx/locallib.php');
return new datalynx_file_info_container($browser, $course, $cm, $context, $areas, $filearea);
}
$view = $DB->get_record('datalynx_views', array('id' => $itemid));
if (!$view) {
return null;
}
$fs = get_file_storage();
$filepath = is_null($filepath) ? '/' : $filepath;
$filename = is_null($filename) ? '.' : $filename;
$storedfile = $fs->get_file($context->id, 'mod_datalynx', $filearea, $itemid, $filepath, $filename);
if (!$storedfile) {
return null;
}
$urlbase = $CFG->wwwroot . '/pluginfile.php';
return new file_info_stored($browser, $context, $storedfile, $urlbase, s($view->name), true,
true, false, false);
}
/**
* Serves the datalynx 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_datalynx_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
global $CFG, $DB, $USER;
// Check permissions, if anonymous access is allowed login user.
require_course_login($course, true, $cm);
// FIELD CONTENT files.
if (($filearea === 'content' || $filearea === 'thumb') &&
$context->contextlevel == CONTEXT_MODULE
) {
$contentid = (int) array_shift($args);
$content = $DB->get_record('datalynx_contents', array('id' => $contentid));
if (!$content) {
return false;
}
$field = $DB->get_record('datalynx_fields', array('id' => $content->fieldid));
if (!$field) {
return false;
}
$entry = $DB->get_record('datalynx_entries', array('id' => $content->entryid));
if (!$entry) {
return false;
}
$datalynx = $DB->get_record('datalynx', array('id' => $field->dataid));
if (!$datalynx) {
return false;
}
if ($datalynx->id != $cm->instance) {
// Hacker attempt - context does not match the contentid.
return false;
}
// Check if approved.
if ($datalynx->approval && !has_capability('mod/datalynx:approve', $context) &&
!$entry->approved && $USER->id != $entry->userid
) {
return false;
}
// Group access.
if ($entry->groupid) {
$groupmode = groups_get_activity_groupmode($cm, $course);
if ($groupmode == SEPARATEGROUPS &&
!has_capability('moodle/site:accessallgroups', $context)
) {
if (!groups_is_member($entry->groupid)) {
return false;
}
}
}
// Separate participants.
$groupmode = isset($groupmode) ? $groupmode : groups_get_activity_groupmode($cm, $course);
if ($groupmode == -1) {
if (empty($USER->id)) {
return false;
}
if ($USER->id != $entry->userid &&
!has_capability('mod/datalynx:manageentries', $context)
) {
return false;
}
}
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_datalynx/$filearea/$contentid/$relativepath";
$oldpath = "/$context->id/mod_dataform/$filearea/$contentid/$relativepath";
$fs = get_file_storage();
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($oldpath));
if (!$file || $file->is_directory()) {
return false;
}
}
// Finally send the file.
send_stored_file($file, 0, 0, false); // Download MUST be forced - security!
}
// VIEW TEMPLATE files.
if (strpos($filearea, 'view') !== false && $context->contextlevel == CONTEXT_MODULE) {
require_course_login($course, true, $cm);
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_datalynx/$filearea/$relativepath";
$oldpath = "/$context->id/mod_dataform/$filearea/$relativepath";
$fs = get_file_storage();
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($oldpath));
if (!$file || $file->is_directory()) {
return false;
}
}
// Finally send the file.
send_stored_file($file, 0, 0, true); // Download MUST be forced - security!
}
// PDF VIEW files.
$viewpdfareas = array('view_pdfframe', 'view_pdfwmark', 'view_pdfcert');
if (in_array($filearea, $viewpdfareas) && $context->contextlevel == CONTEXT_MODULE) {
require_course_login($course, true, $cm);
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_datalynx/$filearea/$relativepath";
$oldpath = "/$context->id/mod_dataform/$filearea/$relativepath";
$fs = get_file_storage();
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($oldpath));
if (!$file || $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' || $filearea === 'site_presets')) {
require_course_login($course, true, $cm);
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_datalynx/$filearea/$relativepath";
$oldpath = "/$context->id/mod_dataform/$filearea/$relativepath";
$fs = get_file_storage();
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($oldpath));
if (!$file || $file->is_directory()) {
return false;
}
}
// Finally send the file.
send_stored_file($file, 0, 0, true); // Download MUST be forced - security!
}
if (($filearea === 'js' || $filearea === 'css')) {
require_course_login($course, true, $cm);
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_datalynx/$filearea/$relativepath";
$oldpath = "/$context->id/mod_dataform/$filearea/$relativepath";
$fs = get_file_storage();
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($oldpath));
if (!$file || $file->is_directory()) {
return false;
}
}
// Finally send the file.
send_stored_file($file, 0, 0, true); // Download MUST be forced - security!
}
if (strpos($filearea, 'actor-') === 0 && $context->contextlevel == CONTEXT_MODULE) {
require_course_login($course, true, $cm);
$itemid = (int) array_shift($args);
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_datalynx/$filearea/$itemid/$relativepath";
$oldpath = "/$context->id/mod_dataform/$filearea/$itemid/$relativepath";
$fs = get_file_storage();
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($oldpath));
if (!$file || $file->is_directory()) {
return false;
}
}
// Finally send the file.
send_stored_file($file, 0, 0, true); // Download MUST be forced - security!
}
return false;
}
/**
*/
function datalynx_extend_navigation($navigation, $course, $module, $cm) {
}
/**
* 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 datalynx_extend_settings_navigation(settings_navigation $settings, navigation_node $dfnode) {
global $USER;
$page = $settings->get_page();
$templatesmanager = has_capability('mod/datalynx:managetemplates', $page->cm->context);
$entriesmanager = has_capability('mod/datalynx:manageentries', $page->cm->context);
// Delete.
if ($templatesmanager) {
$dfnode->add(get_string('delete'),
new moodle_url('/course/mod.php',
array('delete' => $page->cm->id, 'sesskey' => sesskey()
)));
}
// Index.
if (has_capability('mod/datalynx:viewindex', $page->cm->context)) {
$dfnode->add(get_string('index', 'datalynx'),
new moodle_url('/mod/datalynx/index.php', array('id' => $page->course->id)));
}
// Notifications.
if (isloggedin() && !isguestuser()) {
$dfnode->add(get_string('messages', 'message'),
new moodle_url('/message/edit.php',
array('id' => $USER->id, 'course' => $page->course->id,
'context' => $page->context->id)));
}
// Manage.
if ($templatesmanager || $entriesmanager) {
$manage = $dfnode->add(get_string('manage', 'datalynx'));
if ($templatesmanager) {
$manage->add(get_string('views', 'datalynx'),
new moodle_url('/mod/datalynx/view/index.php', array('id' => $page->cm->id)));
$fields = $manage->add(get_string('fields', 'datalynx'),
new moodle_url('/mod/datalynx/field/index.php', array('id' => $page->cm->id)));
$manage->add(get_string('filters', 'datalynx'),
new moodle_url('/mod/datalynx/filter/index.php', array('id' => $page->cm->id)));
$manage->add(get_string('customfilters', 'datalynx'),
new moodle_url('/mod/datalynx/customfilter/index.php', array('id' => $page->cm->id)));
$manage->add(get_string('rules', 'datalynx'),
new moodle_url('/mod/datalynx/rule/index.php', array('id' => $page->cm->id)));
$manage->add(get_string('tools', 'datalynx'),
new moodle_url('/mod/datalynx/tool/index.php', array('id' => $page->cm->id)));
$manage->add(get_string('jsinclude', 'datalynx'),
new moodle_url('/mod/datalynx/js.php',
array('id' => $page->cm->id, 'jsedit' => 1)));
$manage->add(get_string('cssinclude', 'datalynx'),
new moodle_url('/mod/datalynx/css.php',
array('id' => $page->cm->id, 'cssedit' => 1)));
$manage->add(get_string('presets', 'datalynx'),
new moodle_url('/mod/datalynx/preset/index.php', array('id' => $page->cm->id)));
$manage->add(get_string('statistics', 'datalynx'),
new moodle_url('/mod/datalynx/statistics/index.php',
array('id' => $page->cm->id)));
$fields->add(get_string('behaviors', 'datalynx'),
new moodle_url('/mod/datalynx/behavior/index.php', array('id' => $page->cm->id)));
$fields->add(get_string('renderers', 'datalynx'),
new moodle_url('/mod/datalynx/renderer/index.php', array('id' => $page->cm->id)));
}
$manage->add(get_string('import', 'datalynx'),
new moodle_url('/mod/datalynx/import.php', array('id' => $page->cm->id)));
}
}
// Info.
/**
* returns a list of participants of this datalynx
*/
function datalynx_get_participants($dataid) {
global $DB;
$params = array('dataid' => $dataid);
$sql = "SELECT DISTINCT u.id
FROM {user} u,
{datalynx_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,
{datalynx_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,
{datalynx_entries} e,
{ratings} r
WHERE e.dataid = ? AND
u.id = e.userid AND
e.id = r.itemid AND
r.component = 'mod_datalynx' AND
(r.ratingarea = 'entry' OR
r.ratingarea = 'activity')";
$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 datalynx activity of this user
*/
function datalynx_user_outline($course, $user, $mod, $data) {
global $DB, $CFG;
require_once("$CFG->libdir/gradelib.php");
$grades = grade_get_grades($course->id, 'mod', 'datalynx', $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('datalynx_entries', $sqlparams)) {
$result = new stdClass();
$result->info = get_string('entriescount', 'datalynx', $countrecords);
$lastrecordset = $DB->get_records('datalynx_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;
}
/**
* TODO Prints all the records uploaded by this user
*/
function datalynx_user_complete($course, $user, $mod, $data) {
global $DB, $CFG;
require_once("$CFG->libdir/gradelib.php");
$grades = grade_get_grades($course->id, 'mod', 'datalynx', $data->id, $user->id);
if (!empty($grades->items[0]->grades)) {
$grade = reset($grades->items[0]->grades);
echo '<p>' . get_string('grade') . ': ' . $grade->str_long_grade . '</p>';
if ($grade->str_feedback) {
echo '<p>' . get_string('feedback') . ': ' . $grade->str_feedback . '</p>';
}
}
$sqlparams = array('dataid' => $data->id, 'userid' => $user->id);
if ($countrecords = $DB->count_records('datalynx_entries', $sqlparams)) {
// TODO get the default view add a filter for user only and display.
$x = 1;
}
}
// Participantion Reports.
/**
*/
function datalynx_get_view_actions() {
return array('view');
}
/**
*/
function datalynx_get_post_actions() {
return array('add', 'update', 'record delete');
}
// COMMENTS.
/**
* Running addtional permission check on plugin, for example, plugins
* may have switch to turn on/off comments option, this callback will
* affect UI display, not like pluginname_comment_validate only throw
* exceptions.
* Capability check has been done in comment->check_permissions(), we
* don't need to do it again here.
*
* @param stdClass $commentparam {
* context => context the context object
* courseid => int course id
* cm => stdClass course module object
* commentarea => string comment area
* itemid => int itemid
* }
* @return array
*/
function datalynx_comment_permissions($commentparam) {
global $CFG;
return array('post' => true, 'view' => true);
}
/**
* Validate comment parameter before perform other comments actions
*
* @param stdClass $commentparam {
* context => context the context object
* courseid => int course id
* cm => stdClass course module object
* commentarea => string comment area
* itemid => int itemid
* }
* @return boolean
*/
function datalynx_comment_validate($commentparam) {
global $CFG;
require_once("field/_comment/field_class.php");
$comment = new datalynxfield__comment($commentparam->cm->instance);
return $comment->validation($commentparam);
}
/**
*/
function datalynx_comment_add($newcomment, $commentparam) {
$df = new mod_datalynx\datalynx($commentparam->cm->instance);
$eventdata = (object) array('items' => $newcomment);
}
// Grading.
/**
* Return rating related permissions
*
* @param string $contextid the context id
* @param string $component the component to get rating permissions for
* @param string $ratingarea the rating area to get permissions for
* @return array * @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 tyay an associative array of the user's rating permissions
*/
function datalynx_rating_permissions($contextid, $component, $ratingarea) {
$context = context::instance_by_id($contextid, MUST_EXIST);
if ($component == 'mod_datalynx' && ($ratingarea == 'entry' || $ratingarea == 'activity')) {
return array('view' => has_capability('mod/datalynx:ratingsview', $context),
'viewany' => has_capability('mod/datalynx:ratingsviewany', $context),
'viewall' => has_capability('mod/datalynx:ratingsviewall', $context),
'rate' => has_capability('mod/datalynx:rate', $context));
}
return null;
}
/**
*
* @param $params
* @return bool
* @throws coding_exception
* @throws rating_exception
*/
function datalynx_rating_validate($params) {
global $DB, $USER;
require_once(dirname(__FILE__) . "/classes/datalynx.php");