forked from itamart/moodle-mod_dataform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_class.php
More file actions
1669 lines (1434 loc) · 57.3 KB
/
Copy pathmod_class.php
File metadata and controls
1669 lines (1434 loc) · 57.3 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.
*/
/**
* Dataform class
*/
class dataform {
const NOTIFICATION_ENTRY_ADDED = 1;
const NOTIFICATION_ENTRY_UPDATED = 2;
const NOTIFICATION_ENTRY_DELETED = 4;
const NOTIFICATION_COMMENT_ADDED = 8;
const COUNT_ALL = 0;
const COUNT_APPROVED = 1;
const COUNT_UNAPPROVED = 2;
const COUNT_LEFT = 3;
public $cm = NULL; // The course module
public $course = NULL; // The course record
public $data = NULL; // The dataform record
public $context = NULL; //
public $groupmode = 0;
public $currentgroup = 0; // current group id
public $notifications = array('bad' => array(), 'good' => array());
protected $pagefile = 'view';
protected $fields = array();
protected $views = array();
protected $_filtermanager = null;
protected $_rulemanager = null;
protected $_presetmanager = null;
protected $_currentview = null;
// internal fields
protected $internalfields = array();
// internal group modes
protected $internalgroupmodes = array(
'separateparticipants' => -1
);
/**
* constructor
*/
public function __construct($d = 0, $id = 0, $autologinguest = false) {
global $DB;
// initialize from dataform id or object
if ($d) {
if (is_object($d)) { // try object first
$this->data = $d;
} else if (!$this->data = $DB->get_record('dataform', array('id' => $d))) {
throw new moodle_exception('invaliddataform', 'dataform', null, null, "Dataform id: $d");
}
if (!$this->course = $DB->get_record('course', array('id' => $this->data->course))) {
throw new moodle_exception('invaliddataform', 'dataform', null, null, "Course id: {$this->data->course}");
}
if (!$this->cm = get_coursemodule_from_instance('dataform', $this->id(), $this->course->id)) {
throw new moodle_exception('invaliddataform', 'dataform', null, null, "Cm id: {$this->id()}");
}
// initialize from course module id
} else if ($id) {
if (!$this->cm = get_coursemodule_from_id('dataform', $id)) {
throw new moodle_exception('invaliddataform', 'dataform', null, null, "Cm id: $id");
}
if (!$this->course = $DB->get_record('course', array('id' => $this->cm->course))) {
throw new moodle_exception('invaliddataform', 'dataform', null, null, "Course id: {$this->cm->course}");
}
if (!$this->data = $DB->get_record('dataform', array('id' => $this->cm->instance))) {
throw new moodle_exception('invaliddataform', 'dataform', null, null, "Dataform id: {$this->cm->instance}");
}
}
// get context
$this->context = context_module::instance($this->cm->id);
// set groups
if ($this->cm->groupmode and in_array($this->cm->groupmode, $this->internalgroupmodes)) {
$this->groupmode = $this->cm->groupmode;
} else {
$this->groupmode = groups_get_activity_groupmode($this->cm);
$this->currentgroup = groups_get_activity_group($this->cm, true);
}
// set fields manager
//$this->_fieldmanager = new dataform_field_manager($this);
// set views manager
//$this->_viewmanager = new dataform_view_manager($this);
}
/**
*
*/
public function id() {
return $this->data->id;
}
/**
*
*/
public function name() {
return $this->data->name;
}
/**
*
*/
public function pagefile() {
return $this->pagefile;
}
/**
*
*/
public function internal_group_modes() {
return $this->internalgroupmodes;
}
/**
*
*/
public function get_current_view() {
return $this->_currentview;
}
/**
*
*/
public function get_filter_manager() {
// set filters manager
if (!$this->_filtermanager) {
require_once('filter/filter_class.php');
$this->_filtermanager = new dataform_filter_manager($this);
}
return $this->_filtermanager;
}
/**
*
*/
public function get_rule_manager() {
// set rules manager
if (!$this->_rulemanager) {
require_once('rule/rule_manager.php');
$this->_rulemanager = new dataform_rule_manager($this);
}
return $this->_rulemanager;
}
/**
*
*/
public function get_preset_manager() {
// set preset manager
if (!$this->_presetmanager) {
require_once('preset/preset_manager.php');
$this->_presetmanager = new dataform_preset_manager($this);
}
return $this->_presetmanager;
}
/**
*
*/
public function get_entriescount($type, $user = 0) {
global $DB;
switch ($type) {
case self::COUNT_ALL:
$count = $DB->count_records_sql('SELECT COUNT(e.id) FROM {dataform_entries} e WHERE e.dataid = ?', array($this->id()));
break;
case self::COUNT_APPROVED:
$count = '---';
break;
case self::COUNT_UNAPPROVED:
$count = '---';
break;
case self::COUNT_LEFT:
$count = '---';
break;
default:
$count = '---';
}
return $count;
}
/**
*
*/
public function update($params, $notify = '') {
global $DB;
if ($params) {
$updatedf = false;
foreach ($params as $key => $value) {
$oldvalue = !empty($this->data->{$key}) ? $this->data->{$key} : null;
$newvalue = !empty($value) ? $value : null;
if ($newvalue != $oldvalue) {
$this->data->{$key} = $value;
$updatedf = true;
}
}
if ($updatedf) {
if (!$DB->update_record('dataform', $this->data)) {
if ($notify === true) {
$this->notifications['bad'][] = get_string('dfupdatefailed', 'dataform');
} else if ($notify) {
$this->notifications['bad'][] = $notify;
}
return false;
} else {
if ($notify === true) {
//$this->notifications['good'][] = get_string('dfupdatefailed', 'dataform');
} else if ($notify) {
$this->notifications['good'][] = $notify;
}
}
}
}
return true;
}
/**
* TODO complete cleanup
*/
protected function renew() {
global $DB;
// files
$fs = get_file_storage();
$fs->delete_area_files($this->context->id, 'mod_dataform');
// delete fields and their content
if ($fields = $this->get_fields()) {
foreach ($fields as $field) {
$field->delete_field();
}
// reset this fields
$this->get_fields(null, false, true);
}
// delete views
if ($views = $this->get_views()) {
foreach ($views as $view) {
$view->delete();
}
$this->get_views(null, false, true);
}
// delete filters
$DB->delete_records('dataform_filters', array('dataid'=>$this->data->id));
// delete entries
$DB->delete_records('dataform_entries', array('dataid'=>$this->data->id));
// delete ratings
// delete comments
// cleanup gradebook
dataform_grade_item_delete($this->data);
return true;
}
/**
* sets the dataform page
*
* @param string $page current page
* @param array $params
*/
public function set_page($page = 'view', $params = null) {
global $CFG, $PAGE, $USER, $OUTPUT;
$this->pagefile = $page;
$thisid = $this->id();
$params = (object) $params;
$urlparams = array();
if (!empty($params->urlparams)) {
foreach ($params->urlparams as $param => $value) {
if ($value != 0 and $value != '') {
$urlparams[$param] = $value;
}
}
}
if (empty($params->nologin)) {
// guest auto login
$autologinguest = false;
if ($page == 'view' or $page == 'embed' or $page == 'external') {
$autologinguest = true;
}
// require login
require_login($this->course->id, $autologinguest, $this->cm);
}
// make sure there is at least dataform id param
$urlparams['d'] = $thisid;
$manager = has_capability('mod/dataform:managetemplates', $this->context);
// renew if requested
if ($manager and !empty($urlparams['renew']) and confirm_sesskey()) {
$this->renew();
}
// if dataform activity closed don't let students in
if (!$manager) {
$timenow = time();
if (!empty($this->data->timeavailable) and $this->data->timeavailable > $timenow) {
throw new moodle_exception('notopenyet', 'dataform', '', userdate($this->data->timeavailable));
}
}
// RSS
if (!empty($params->rss) and
!empty($CFG->enablerssfeeds) and
!empty($CFG->dataform_enablerssfeeds) and
$this->data->rssarticles > 0) {
require_once("$CFG->libdir/rsslib.php");
$rsstitle = format_string($this->course->shortname) . ': %fullname%';
rss_add_http_header($this->context, 'mod_dataform', $this->data, $rsstitle);
}
// COMMENTS
if (!empty($params->comments)) {
require_once("$CFG->dirroot/comment/lib.php");
comment::init();
}
$fs = get_file_storage();
/////////////////////////////////////
// PAGE setup for activity pages only
if ($page != 'external') {
// Is user editing
$urlparams['edit'] = optional_param('edit', 0, PARAM_BOOL);
$PAGE->set_url("/mod/dataform/$page.php", $urlparams);
// editing button (omit in embedded dataforms)
if ($page != 'embed' and $PAGE->user_allowed_editing()) {
// teacher editing mode
if ($urlparams['edit'] != -1) {
$USER->editing = $urlparams['edit'];
}
$buttons = '<table><tr><td><form method="get" action="'. $PAGE->url. '"><div>'.
'<input type="hidden" name="d" value="'.$thisid.'" />'.
'<input type="hidden" name="edit" value="'.($PAGE->user_is_editing()?0:1).'" />'.
'<input type="submit" value="'.get_string($PAGE->user_is_editing()?'blockseditoff':'blocksediton').'" /></div></form></td></tr></table>';
$PAGE->set_button($buttons);
}
// auto refresh
if (!empty($urlparams['refresh'])) {
$PAGE->set_periodic_refresh_delay($urlparams['refresh']);
}
// page layout
if (!empty($params->pagelayout)) {
$PAGE->set_pagelayout($params->pagelayout);
}
// Mark as viewed
if (!empty($params->completion)) {
require_once($CFG->libdir . '/completionlib.php');
$completion = new completion_info($this->course);
$completion->set_module_viewed($this->cm);
}
$PAGE->set_title($this->name());
$PAGE->set_heading($this->course->fullname);
// Include blocks dragdrop when editing
if ($PAGE->user_is_editing()) {
$params = array(
'courseid' => $this->course->id,
'cmid' => $this->cm->id,
'pagetype' => $PAGE->pagetype,
'pagelayout' => $PAGE->pagelayout,
'regions' => $PAGE->blocks->get_regions(),
);
$PAGE->requires->yui_module('moodle-core-blocks', 'M.core_blocks.init_dragdrop', array($params), null, true);
}
}
////////////////////////////////////
// PAGE setup for dataform content anywhere
// Use this to return css if this df page is set after header
$output = '';
// CSS (cannot be required after head)
$cssurls = array();
if (!empty($params->css)) {
// js includes from the js template
if ($this->data->cssincludes) {
foreach (explode("\n", $this->data->cssincludes) as $cssinclude) {
$cssinclude = trim($cssinclude);
if ($cssinclude) {
$cssurls[] = new moodle_url($cssinclude);
}
}
}
// Uploaded css files
if ($files = $fs->get_area_files($this->context->id, 'mod_dataform', 'css', 0, 'sortorder', false)) {
$path = "/{$this->context->id}/mod_dataform/css/0";
foreach ($files as $file) {
$filename = $file->get_filename();
$cssurls[] = moodle_url::make_file_url('/pluginfile.php', "$path/$filename");
}
}
// css code from the css template
if ($this->data->css) {
$cssurls[] = new moodle_url('/mod/dataform/css.php', array('d' => $thisid));
}
}
if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) {
foreach ($cssurls as $cssurl) {
$PAGE->requires->css($cssurl);
}
} else {
$attrs = array('rel' => 'stylesheet', 'type' => 'text/css');
foreach ($cssurls as $cssurl) {
$attrs['href'] = $cssurl;
$output .= html_writer::empty_tag('link', $attrs). "\n";
unset($attrs['id']);
}
}
// JS
$jsurls = array();
if (!empty($params->js)) {
// js includes from the js template
if ($this->data->jsincludes) {
foreach (explode("\n", $this->data->jsincludes) as $jsinclude) {
$jsinclude = trim($jsinclude);
if ($jsinclude) {
$jsurls[] = new moodle_url($jsinclude);
}
}
}
// Uploaded js files
if ($files = $fs->get_area_files($this->context->id, 'mod_dataform', 'js', 0, 'sortorder', false)) {
$path = "/{$this->context->id}/mod_dataform/js/0";
foreach ($files as $file) {
$filename = $file->get_filename();
$jsurls[] = moodle_url::make_file_url('/pluginfile.php', "$path/$filename");
}
}
// js code from the js template
if ($this->data->js) {
$jsurls[] = new moodle_url('/mod/dataform/js.php', array('d' => $thisid));
}
}
foreach ($jsurls as $jsurl) {
$PAGE->requires->js($jsurl);
}
// MOD JS
if (!empty($params->modjs)) {
$PAGE->requires->js('/mod/dataform/dataform.js');
}
// TODO
//if ($mode == 'asearch') {
// $PAGE->navbar->add(get_string('search'));
//}
// set current view and view's page requirements
$currentview = !empty($urlparams['view']) ? $urlparams['view'] : 0;
if ($this->_currentview = $this->get_view_from_id($currentview)) {
$this->_currentview->set_page($page);
}
// if a new dataform or incomplete design, direct manager to manage area
if ($manager) {
$views = $this->get_views();
if (!$views) {
if ($page == 'view' or $page == 'embed') {
$getstarted = new object;
$getstarted->presets = html_writer::link(new moodle_url('preset/index.php', array('d' => $thisid)), get_string('presets', 'dataform'));
$getstarted->fields = html_writer::link(new moodle_url('field/index.php', array('d' => $thisid)), get_string('fields', 'dataform'));
$getstarted->views = html_writer::link(new moodle_url('view/index.php', array('d' => $thisid)), get_string('views', 'dataform'));
$this->notifications['bad']['getstarted'] = html_writer::tag('div', get_string('getstarted', 'dataform', $getstarted), array('class' => 'mdl-left'));
}
} else if (!$this->data->defaultview) {
$linktoviews = html_writer::link(new moodle_url('view/index.php', array('d' => $thisid)), get_string('views', 'dataform'));
$this->notifications['bad']['defaultview'] = get_string('viewnodefault','dataform', $linktoviews);
}
}
return $output;
}
/**
* prints the header of the current dataform page
*
* @param array $params
*/
public function print_header($params = null) {
global $OUTPUT;
$params = (object) $params;
echo $OUTPUT->header();
// print intro
if (!empty($params->heading)) {
echo $OUTPUT->heading(format_string($this->name()));
}
// print intro
if (!empty($params->intro) and $params->intro) {
$this->print_intro();
}
// print the tabs
if (!empty($params->tab)) {
$currenttab = $params->tab;
include('tabs.php');
}
// print groups menu if needed
if (!empty($params->groups)) {
$this->print_groups_menu($params->urlparams->view, $params->urlparams->filter);
}
// TODO: explore letting view decide whether to print rsslink and intro
//$df->print_rsslink();
// print any notices
if (empty($params->nonotifications)) {
foreach ($this->notifications['good'] as $notification) {
if (!empty($notification)) {
echo $OUTPUT->notification($notification, 'notifysuccess'); // good (usually green)
}
}
foreach ($this->notifications['bad'] as $notification) {
if (!empty($notification)) {
echo $OUTPUT->notification($notification); // bad (usually red)
}
}
}
}
/**
* prints the footer of the current dataform page
*
* @param array $params
*/
public function print_footer($params = null) {
global $OUTPUT;
echo $OUTPUT->footer();
}
/**
* TODO: consider moving into the view
*/
public function print_groups_menu($view, $filter) {
if ($this->groupmode and !in_array($this->groupmode, $this->internalgroupmodes)) {
$returnurl = new moodle_url("/mod/dataform/{$this->pagefile}.php",
array('d' => $this->id(),
'view' => $view,
'filter' => $filter));
groups_print_activity_menu($this->cm, $returnurl.'&');
}
}
/**
* TODO: consider moving into the view
*/
public function print_rsslink() {
// Link to the RSS feed
if (!empty($CFG->enablerssfeeds) && !empty($CFG->dataform_enablerssfeeds) && $this->data->rssarticles > 0) {
echo '<div style="float:right;">';
rss_print_link($this->course->id, $USER->id, 'dataform', $this->id(), get_string('rsstype'));
echo '</div>';
echo '<div style="clear:both;"></div>';
}
}
/**
* TODO: consider moving into the view
*/
public function print_intro() {
global $OUTPUT;
// TODO: make intro stickily closable
// display the intro only when there are on pages: if ($this->data->intro and empty($page)) {
if ($this->data->intro) {
$options = new stdClass();
$options->noclean = true;
echo $OUTPUT->box(format_module_intro('dataform', $this->data, $this->cm->id), 'generalbox', 'intro');
}
}
/**
*
*/
public function set_content() {
if (!empty($this->_currentview)) {
$this->_currentview->process_data();
$this->_currentview->set_content();
}
}
/**
*
*/
public function display() {
if (!empty($this->_currentview)) {
add_to_log($this->course->id, 'dataform', 'view', $this->pagefile. '.php?id='. $this->cm->id, $this->id(), $this->cm->id);
$this->_currentview->display();
}
}
/**********************************************************************************
* FIELDS
*********************************************************************************/
/**
* Initialize if needed and return the internal fields
*/
protected function get_internal_fields() {
global $CFG;
if (!$this->internalfields) {
$fieldplugins = get_list_of_plugins('mod/dataform/field/');
foreach ($fieldplugins as $fieldname) {
// Internal should start with _
if (strpos($fieldname, '_') !== 0) {
continue;
}
require_once("$CFG->dirroot/mod/dataform/field/$fieldname/field_class.php");
$fieldclass = "dataform_field_$fieldname";
$internalfields = $fieldclass::get_field_objects($this->data->id);
foreach ($internalfields as $fid => $field) {
$this->internalfields[$fid] = $this->get_field($field);
}
}
}
return $this->internalfields;
}
/**
*
*/
public function get_user_defined_fields($forceget = false, $sort = '') {
$this->get_fields(null, false, $forceget, $sort);
return $this->fields;
}
/**
* given a field id return the field object from get_fields
* Initializes get_fields if necessary
*/
public function get_field_from_id($fieldid, $forceget = false) {
$fields = $this->get_fields(null, false, $forceget);
if (empty($fields[$fieldid])) {;
return false;
} else {
return $fields[$fieldid];
}
}
/**
* given a field type returns the field object from get_fields
* Initializes get_fields if necessary
*/
public function get_fields_by_type($type, $menu = false) {
$typefields = array();
foreach ($this->get_fields() as $fieldid => $field) {
if ($field->type() === $type) {
if ($menu) {
$typefields[$fieldid] = $field->name();
} else {
$typefields[$fieldid] = $field;
}
}
}
return $typefields;
}
/**
* given a field name returns the field object from get_fields
*/
public function get_field_by_name($name) {
foreach ($this->get_fields() as $field) {
if ($field->name() === $name) {
return $field;
}
}
return false;
}
/**
* returns a subclass field object given a record of the field
* used to invoke plugin methods
* input: $param $field record from db, or field type
*/
public function get_field($key) {
global $CFG;
if ($key) {
if (is_object($key)) {
$type = $key->type;
} else {
$type = $key;
$key = 0;
}
require_once('field/'. $type. '/field_class.php');
$fieldclass = 'dataform_field_'. $type;
$field = new $fieldclass($this, $key);
return $field;
} else {
return false;
}
}
/**
*
*/
public function get_fields($exclude = null, $menu = false, $forceget = false, $sort = '') {
global $DB;
if (!$this->fields or $forceget) {
$this->fields = array();
// collate user fields
if ($fields = $DB->get_records('dataform_fields', array('dataid' => $this->id()), $sort)) {
foreach ($fields as $fieldid => $field) {
$this->fields[$fieldid] = $this->get_field($field);
}
}
}
// collate all fields
$fields = $this->fields + $this->get_internal_fields();
if (empty($exclude) and !$menu) {
return $fields;
} else {
$retfields = array();
foreach ($fields as $fieldid => $field) {
if (!empty($exclude) and in_array($fieldid, $exclude)) {
continue;
}
if ($menu) {
$retfields[$fieldid]= $field->name();
} else {
$retfields[$fieldid]= $field;
}
}
return $retfields;
}
}
/**
*
*/
public function process_fields($action, $fids, $confirmed = false) {
global $OUTPUT, $DB;
if (!has_capability('mod/dataform:managetemplates', $this->context)) {
// TODO throw exception
return false;
}
$dffields = $this->get_fields();
$fields = array();
// collate the fields for processing
if ($fieldids = explode(',', $fids)) {
foreach ($fieldids as $fieldid) {
if ($fieldid > 0 and isset($dffields[$fieldid])) {
$fields[$fieldid] = $dffields[$fieldid];
}
}
}
$processedfids = array();
$strnotify = '';
if (empty($fields) and $action != 'add') {
$this->notifications['bad'][] = get_string("fieldnoneforaction",'dataform');
return false;
} else {
if (!$confirmed) {
// print header
$this->print_header('fields');
// Print a confirmation page
echo $OUTPUT->confirm(get_string("fieldsconfirm$action", 'dataform', count($fields)),
new moodle_url('/mod/dataform/field/index.php', array('d' => $this->id(),
$action => implode(',', array_keys($fields)),
'sesskey' => sesskey(),
'confirmed' => 1)),
new moodle_url('/mod/dataform/field/index.php', array('d' => $this->id())));
echo $OUTPUT->footer();
exit;
} else {
// go ahead and perform the requested action
switch ($action) {
case 'add': // TODO add new
if ($forminput = data_submitted()) {
// Check for arrays and convert to a comma-delimited string
$this->convert_arrays_to_strings($forminput);
// Create a field object to collect and store the data safely
$field = $this->get_field($forminput->type);
$field->insert_field($forminput);
}
$strnotify = 'fieldsadded';
break;
case 'update': // update existing
if ($forminput = data_submitted()) {
// Check for arrays and convert to a comma-delimited string
$this->convert_arrays_to_strings($forminput);
// Create a field object to collect and store the data safely
$field = reset($fields);
$oldfieldname = $field->field->name;
$field->update_field($forminput);
// Update the views
if ($oldfieldname != $field->field->name) {
$this->replace_field_in_views($oldfieldname, $field->field->name);
}
}
$strnotify = 'fieldsupdated';
break;
case 'visible':
foreach ($fields as $fid => $field) {
// hide = 0; (show to owner) = 1; show to everyone = 2
$visible = (($field->field->visible + 1) % 3);
$DB->set_field('dataform_fields', 'visible', $visible, array('id' => $fid));
$processedfids[] = $fid;
}
$strnotify = '';
break;
case 'editable':
foreach ($fields as $fid => $field) {
// lock = 0; unlock = -1;
$editable = $field->field->edits ? 0 : -1;
$DB->set_field('dataform_fields', 'edits', $editable, array('id' => $fid));
$processedfids[] = $fid;
}
$strnotify = '';
break;
case 'duplicate':
foreach ($fields as $field) {
// set new name
while ($this->name_exists('fields', $field->name())) {
$field->field->name .= '_1';
}
$fieldid = $DB->insert_record('dataform_fields', $field->field);
$processedfids[] = $fieldid;
}
$strnotify = 'fieldsadded';
break;
case 'delete':
foreach ($fields as $field) {
$field->delete_field();
$processedfids[] = $field->field->id;
// Update views
$this->replace_field_in_views($field->field->name, '');
}
$strnotify = 'fieldsdeleted';
break;
default:
break;
}
add_to_log($this->course->id, 'dataform', 'field '. $action, 'field/index.php?id='. $this->cm->id, $this->id(), $this->cm->id);
if ($strnotify) {
$fieldsprocessed = $processedfids ? count($processedfids) : 'No';
$this->notifications['good'][] = get_string($strnotify, 'dataform', $fieldsprocessed);
}
return $processedfids;
}
}
}
/**********************************************************************************
* VIEWS
*********************************************************************************/
/**
* TODO there is no need to instantiate all viewds!!!
* this function creates an instance of the particular subtemplate class *
*/
public function get_view_from_id($viewid = 0) {
if ($views = $this->get_views()) {
if ($viewid and isset($views[$viewid])) {
return $views[$viewid];
// if can't find the requested, try the default
} else if ($viewid = $this->data->defaultview and isset($views[$viewid])) {
return $views[$viewid];
}
}
return false;
}
/**
* returns a view subclass object given a view record or view type
* invoke plugin methods
* input: $param $vt - mixed, view record or view type
*/
public function get_view($vt) {
global $CFG;
if ($vt) {
if (is_object($vt)) {
$type = $vt->type;
} else {
$type = $vt;
$vt = 0;
}
require_once($CFG->dirroot. '/mod/dataform/view/'. $type. '/view_class.php');
$viewclass = 'dataform_view_'. $type;
$view = new $viewclass($this, $vt);
return $view;
}
}
/**
* given a view type returns the view object from $this->views
* Initializes $this->views if necessary
*/
public function get_views_by_type($type, $menu = false, $forceget = false) {
if (!$views = $this->get_views(null, false, $forceget)) {;
return false;
} else {
$typeviews = array();
foreach ($views as $viewid => $view) {
if ($view->type() === $type) {
if ($menu) {
$typeviews[$viewid] = $view->name();
} else {
$typeviews[$viewid] = $view;