forked from itamart/moodle-mod_dataform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.php
More file actions
1619 lines (1350 loc) · 62.6 KB
/
Copy pathlib.php
File metadata and controls
1619 lines (1350 loc) · 62.6 KB
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
* @subpackage 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;
$data->timemodified = time();
if (empty($data->grade)) {
$data->grade = 0;
$data->grademethod = 0;
}
if ($CFG->dataform_maxentries) {
$data->maxentries = $CFG->dataform_maxentries;
}
if (!$data->id = $DB->insert_record('dataform', $data)) {
return false;
}
dataform_grade_item_update($data);
return $data->id;
}
/**
* updates an instance of a data
*
* @global object
* @param object $data
* @return bool
*/
function dataform_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->notification)) {
$data->notification = 0;
}
if (!$DB->update_record('dataform', $data)) {
return false;
}
dataform_update_grades($data);
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->get_record('dataform', array('id'=>$id))) {
return false;
}
$cm = get_coursemodule_from_instance('dataform', $data->id);
$context = context_module::instance($cm->id);
// files
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_dataform');
// get all the content in this dataform
$sql = "SELECT e.id FROM {dataform_entries} e WHERE e.dataid = ?";
$DB->delete_records_select('dataform_contents', "entryid IN ($sql)", array($id));
// delete fields views filters entries
$DB->delete_records('dataform_fields', array('dataid'=>$id));
$DB->delete_records('dataform_views', array('dataid'=>$id));
$DB->delete_records('dataform_filters', array('dataid'=>$id));
$DB->delete_records('dataform_entries', array('dataid'=>$id));
// Delete the instance itself
$result = $DB->delete_records('dataform', array('id'=>$id));
// cleanup gradebook
dataform_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
*/
function dataform_page_type_list($pagetype, $parentcontext, $currentcontext) {
$module_pagetype = array('mod-dataform-*'=>get_string('page-mod-dataform-x', 'dataform'));
return $module_pagetype;
}
//------------------------------------------------------------
// 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');
$mform->addElement('checkbox', 'reset_dataform_ratings', get_string('deleteallratings'));
$mform->disabledIf('reset_dataform_ratings', 'reset_dataform_data', 'checked');
$mform->addElement('checkbox', 'reset_dataform_comments', get_string('deleteallcomments'));
$mform->disabledIf('reset_dataform_comments', '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_ratings'=>1, 'reset_dataform_comments'=>1, 'reset_dataform_notenrolled'=>0);
}
/**
* 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;
$sql = "SELECT d.*, cm.idnumber as cmidnumber, d.course as courseid
FROM {dataform} d, {course_modules} cm, {modules} m
WHERE m.name='dataform' AND m.id=cm.module AND cm.instance=d.id AND d.course=?";
if ($dataforms = $DB->get_records_sql($sql, array($courseid))) {
foreach ($dataforms as $dataform) {
dataform_grade_item_update($dataform, '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 dataform_reset_userdata($data) {
global $CFG, $DB;
require_once($CFG->libdir.'/filelib.php');
require_once($CFG->dirroot.'/rating/lib.php');
$componentstr = get_string('modulenameplural', 'dataform');
$status = array();
$allrecordssql = "SELECT e.id
FROM {dataform_entries} e
INNER JOIN {dataform} d ON e.dataid = d.id
WHERE d.course = ?";
$alldatassql = "SELECT d.id
FROM {dataform} d
WHERE d.course=?";
$rm = new rating_manager();
$ratingdeloptions = new stdClass;
$ratingdeloptions->component = 'mod_dataform';
$ratingdeloptions->ratingarea = 'entry';
// delete entries if requested
if (!empty($data->reset_dataform_data)) {
$DB->delete_records_select('comments', "itemid IN ($allrecordssql) AND commentarea='entry'", array($data->courseid));
$DB->delete_records_select('dataform_contents', "entryid IN ($allrecordssql)", array($data->courseid));
$DB->delete_records_select('dataform_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/dataform/$dataid");
if (!$cm = get_coursemodule_from_instance('dataform', $dataid)) {
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
dataform_reset_gradebook($data->courseid);
}
$status[] = array('component'=>$componentstr, 'item'=>get_string('entriesdeleteall', 'dataform'), 'error'=>false);
}
// remove entries by users not enrolled into course
if (!empty($data->reset_dataform_notenrolled)) {
$recordssql = "SELECT e.id, e.userid, e.dataid, u.id AS userexists, u.deleted AS userdeleted
FROM {dataform_entries} e
INNER JOIN {dataform} d ON e.dataid = d.id
LEFT OUTER JOIN {user} u ON e.userid = u.id
WHERE d.course = ? AND e.userid > 0";
$course_context = 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) or !$record->userexists or $record->userdeleted
or !is_enrolled($course_context, $record->userid)) {
//delete ratings
if (!$cm = get_coursemodule_from_instance('dataform', $record->dataid)) {
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('dataform_contents', array('entryid'=>$record->id));
$DB->delete_records('dataform_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('dataform_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/dataform/$record->dataid/$fieldid/$record->id");
}
$notenrolled[$record->userid] = true;
}
rs_close($rs);
$status[] = array('component'=>$componentstr, 'item'=>get_string('deletenotenrolled', 'dataform'), 'error'=>false);
}
}
// remove all ratings
if (!empty($data->reset_dataform_ratings)) {
if ($datas = $DB->get_records_sql($alldatassql, array($data->courseid))) {
foreach ($datas as $dataid=>$unused) {
if (!$cm = get_coursemodule_from_instance('dataform', $dataid)) {
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
dataform_reset_gradebook($data->courseid);
}
$status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallratings'), 'error'=>false);
}
// remove all comments
if (!empty($data->reset_dataform_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('dataform', 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 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');
}
/**
* @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_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;
}
}
/**
* 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;
// FIELD CONTENT files
if ($filearea === 'content' and $context->contextlevel == CONTEXT_MODULE) {
$contentid = (int)array_shift($args);
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;
}
// nanogong ugly hack
if ($field->type != 'nanogong') {
if (empty($USER->id)) {
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;
}
//check if approved
if ($dataform->approval and !has_capability('mod/dataform:approve', $context) and !$entry->approved and $USER->id != $entry->userid) {
return false;
}
// group access
if ($entry->groupid) {
$groupmode = groups_get_activity_groupmode($cm, $course);
if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
if (!groups_is_member($record->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 and !has_capability('mod/dataform:manageentries', $context)) {
return false;
}
}
// TODO
//require_once("field/$field->type/field_class.php");
//$fieldclass = "dataform_field_$field->type";
//if (!$fieldclass::file_ok($relativepath)) {
// 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!
}
// VIEW TEMPLATE files
if (strpos($filearea, 'view') !== false and $context->contextlevel == CONTEXT_MODULE) {
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!
}
// PDF VIEW files
$viewpdfareas = array('view_pdfframe', 'view_pdfwmark', 'view_pdfcert');
if (in_array($filearea, $viewpdfareas) and $context->contextlevel == CONTEXT_MODULE) {
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!
}
// PRESET files
if (($filearea === 'course_presets' or $filearea === 'site_presets')) {
// and $context->contextlevel == CONTEXT_MODULE) {
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')) {
// and $context->contextlevel == CONTEXT_MODULE) {
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 (strpos($filearea, 'actor-') === 0 and $context->contextlevel == CONTEXT_MODULE) {
require_course_login($course, true, $cm);
$itemid = (int)array_shift($args);
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_dataform/$filearea/$itemid/$relativepath";
//require_once("field/$field->type/field_class.php");
//$fieldclass = "dataform_field_$field->type";
//if (!$fieldclass::file_ok($relativepath)) {
// return false;
//}
$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) {
}
/**
* 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, $USER;
$templatesmanager = has_capability('mod/dataform:managetemplates', $PAGE->cm->context);
$entriesmanager = has_capability('mod/dataform:manageentries', $PAGE->cm->context);
// delete
if ($templatesmanager) {
$dfnode->add(get_string('renew', 'dataform'), new moodle_url('/mod/dataform/view.php', array('id' => $PAGE->cm->id, 'renew' => 1, 'sesskey' => sesskey())));
$dfnode->add(get_string('delete'), new moodle_url('/course/mod.php', array('delete' => $PAGE->cm->id, 'sesskey' => sesskey())));
}
// index
$dfnode->add(get_string('index', 'dataform'), new moodle_url('/mod/dataform/index.php', array('id' => $PAGE->course->id)));
// notifications
if (isloggedin() and !isguestuser()) {
$dfnode->add(get_string('messaging', 'message'), new moodle_url('/message/edit.php', array('id' => $USER->id, 'course' => $PAGE->course->id, 'context' => $PAGE->context->id)));
}
// manage
if ($templatesmanager or $entriesmanager) {
$manage = $dfnode->add(get_string('manage', 'dataform'));
if ($templatesmanager) {
$manage->add(get_string('presets', 'dataform'), new moodle_url('/mod/dataform/preset/index.php', array('id' => $PAGE->cm->id)));
$manage->add(get_string('fields', 'dataform'), new moodle_url('/mod/dataform/field/index.php', array('id' => $PAGE->cm->id)));
$manage->add(get_string('views', 'dataform'), new moodle_url('/mod/dataform/view/index.php', array('id' => $PAGE->cm->id)));
$manage->add(get_string('filters', 'dataform'), new moodle_url('/mod/dataform/filter/index.php', array('id' => $PAGE->cm->id)));
$manage->add(get_string('rules', 'dataform'), new moodle_url('/mod/dataform/rule/index.php', array('id' => $PAGE->cm->id)));
$manage->add(get_string('tools', 'dataform'), new moodle_url('/mod/dataform/tool/index.php', array('id' => $PAGE->cm->id)));
$manage->add(get_string('jsinclude', 'dataform'), new moodle_url('/mod/dataform/js.php', array('id' => $PAGE->cm->id, 'jsedit' => 1)));
$manage->add(get_string('cssinclude', 'dataform'), new moodle_url('/mod/dataform/css.php', array('id' => $PAGE->cm->id, 'cssedit' => 1)));
}
$manage->add(get_string('import', 'dataform'), new moodle_url('/mod/dataform/import.php', array('id' => $PAGE->cm->id)));
}
}
//------------------------------------------------------------
// 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' 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 dataform activity of this user
*/
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 object();
$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 object();
$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 dataform_user_complete($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 = 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('dataform_entries', $sqlparams)) {
// TODO get the default view add a filter for user only and display
}
}
//------------------------------------------------------------
// Participantion Reports
//------------------------------------------------------------
/**
*/
function dataform_get_view_actions() {
return array('view');
}
/**
*/
function dataform_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 $comment_param {
* context => context the context object
* courseid => int course id
* cm => stdClass course module object
* commentarea => string comment area
* itemid => int itemid
* }
* @return array
*/
function dataform_comment_permissions($comment_param) {
global $CFG;
//require_once("$CFG->field/_comment/field_class.php");
//$comment = new dataform_field__comment($comment_param->cm->instance);
//return $comment->permissions($comment_param);
return array('post'=>true, 'view'=>true);
}
/**
* Validate comment parameter before perform other comments actions
*
* @param stdClass $comment_param {
* context => context the context object
* courseid => int course id
* cm => stdClass course module object
* commentarea => string comment area
* itemid => int itemid
* }
* @return boolean
*/
function dataform_comment_validate($comment_param) {
global $CFG;
require_once("field/_comment/field_class.php");
$comment = new dataform_field__comment($comment_param->cm->instance);
return $comment->validation($comment_param);
}
/**
*
*/
function dataform_comment_add($newcomment, $comment_param) {
$df = new dataform(null, $comment_param->cm->instance);
$eventdata = (object) array('items' => $newcomment);
$df->events_trigger("commentadded", $eventdata);
}
//------------------------------------------------------------
// 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 an associative array of the user's rating permissions
*/
function dataform_rating_permissions($contextid, $component, $ratingarea) {
$context = context::instance_by_id($contextid, MUST_EXIST);
if ($component == 'mod_dataform' and ($ratingarea == 'entry' or $ratingarea == 'activity')) {
return array(
'view' => has_capability('mod/dataform:ratingsview',$context),
'viewany' => has_capability('mod/dataform:ratingsviewany',$context),
'viewall' => has_capability('mod/dataform:ratingsviewall',$context),
'rate' => has_capability('mod/dataform:rate',$context)
);
}
return null;
}
/**
* Validates a submitted rating
* @param array $params submitted data
* context => object the context in which the rated items exists [required]
* ratingarea => string 'entry' or 'activity' [required]
* itemid => int the ID of the object being rated
* scaleid => int the scale from which the user can select a rating. Used for bounds checking. [required]
* rating => int the submitted rating
* rateduserid => int the id of the user whose items have been rated. NOT the user who submitted the ratings. 0 to update all. [required]
* aggregation => int the aggregation method to apply when calculating grades ie RATING_AGGREGATE_AVERAGE [required]
* @return boolean true if the rating is valid. Will throw rating_exception if not
*/
function dataform_rating_validate($params) {
require_once("mod_class.php");
require_once("field/_rating/field_class.php");
$df = new dataform(null, $params['context']->instanceid);
$rating = $df->get_field_from_id(dataform_field__rating::_RATING);
return $rating->validation($params);
}
/**
* Return grade for given user or all users.
* @return array array of grades, false if none
*/
function dataform_get_user_grades($data, $userid = 0) {
global $CFG;
require_once("$CFG->dirroot/rating/lib.php");
$options = new object();
$options->component = 'mod_dataform';
if ($data->grade and !$data->grademethod) {
$options->ratingarea = 'activity';
$options->aggregationmethod = RATING_AGGREGATE_MAXIMUM;
$options->itemtable = 'user';
$options->itemtableusercolumn = 'id';
} else {
$options->ratingarea = 'entry';
$options->aggregationmethod = $data->grademethod;
$options->itemtable = 'dataform_entries';
$options->itemtableusercolumn = 'userid';
}
$options->modulename = 'dataform';
$options->moduleid = $data->id;
$options->userid = $userid;
$options->scaleid = $data->grade;
$rm = new rating_manager();
return $rm->get_user_grades($options);
}
/**
* Update grades by firing grade_updated event
* @param object $data null means all databases
* @param int $userid specific user only, 0 mean all
* @param bool $nullifnone
* @param array $grades
*/
function dataform_update_grades($data=null, $userid=0, $nullifnone=true, $grades=null) {
global $CFG, $DB;
require_once("$CFG->libdir/gradelib.php");
if ($data != null) {
if ($data->grade) {
if ($grades or $grades = dataform_get_user_grades($data, $userid)) {
dataform_grade_item_update($data, $grades);
} else if ($userid and $nullifnone) {
$grade = new object();
$grade->userid = $userid;
$grade->rawgrade = NULL;
dataform_grade_item_update($data, $grade);
} else {
dataform_grade_item_update($data);
}
} else {
dataform_grade_item_delete($data);
}
}
}
/**
* Update all grades in gradebook.
*
* @global object
*/
function dataform_upgrade_grades() {
global $DB;
$sql = "SELECT COUNT('x')
FROM {dataform} d, {course_modules} cm, {modules} m
WHERE m.name='dataform' AND m.id=cm.module AND cm.instance=d.id";
$count = $DB->count_records_sql($sql);
$sql = "SELECT d.*, cm.idnumber AS cmidnumber, d.course AS courseid
FROM {dataform} d, {course_modules} cm, {modules} m
WHERE m.name='dataform' AND m.id=cm.module AND cm.instance=d.id";
$rs = $DB->get_recordset_sql($sql);
if ($rs->valid()) {
// too much debug output
$pbar = new progress_bar('dataupgradegrades', 500, true);
$i=0;
foreach ($rs as $data) {
$i++;
upgrade_set_timeout(60*5); // set up timeout, may also abort execution
dataform_update_grades($data, 0, false);
$pbar->update($i, $count, "Updating Dataform grades ($i/$count).");
}
}
$rs->close();
}
/**
* Update/create grade item for given dataform
* @param object $data object with extra cmidnumber
* @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return object grade_item
*/
function dataform_grade_item_update($data, $grades=NULL) {
global $CFG;
require_once("$CFG->libdir/gradelib.php");
$params = array(
'itemname'=>$data->name,
'idnumber'=>$data->cmidnumber
);
if (!$data->grade) {
$params['gradetype'] = GRADE_TYPE_NONE;
} else if ($data->grade > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $data->grade;
$params['grademin'] = 0;
} else if ($data->grade < 0) {
$params['gradetype'] = GRADE_TYPE_SCALE;
$params['scaleid'] = -$data->grade;
}
if ($grades === 'reset') {
$params['reset'] = true;