-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathcontent.class.php
More file actions
2565 lines (2323 loc) · 113 KB
/
Copy pathcontent.class.php
File metadata and controls
2565 lines (2323 loc) · 113 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
/**
* File for content classes
*
* @package eFront
*/
//This file cannot be called directly, only included.
if (str_replace(DIRECTORY_SEPARATOR, "/", __FILE__) == $_SERVER['SCRIPT_FILENAME']) {
exit;
}
/**
* Efront content exceptions
*
* This class extends Exception to provide the exceptions related to content
* @package eFront
*
* @since 3.5.0
*/
class EfrontContentException extends Exception
{
/**
* The id provided is not valid, for example it is not a number or it is 0
* @since 3.5.0
*/
const INVALID_ID = 501;
/**
* The unit requested does not exist
* @since 3.5.0
*/
const UNIT_NOT_EXISTS = 502;
/**
* The unit can not be inserted for some reason
* @since 3.5.0
*/
const CANNOT_INSERT_UNIT = 503;
/**
* The project requested does not exist
* @since 3.5.0
*/
const PROJECT_NOT_EXISTS = 504;
/**
* The user login provided is not valid or does not exist
* @since 3.5.0
*/
const INVALID_LOGIN = 505;
/**
* The score is not valid, for example it is not numeric
* @since 3.5.0
*/
const INVALID_SCORE = 506;
/**
* The data provided is not valid, for example quotes or other illegal characters
* @since 3.5.0
*/
const INVALID_DATA = 507;
/**
* An error originating in database actions
* @since 3.5.0
*/
const DATABASE_ERROR = 508;
/**
* Unsupported content type, for example SCORM 2004 in community edition
* @since 3.6.0
*/
const UNSUPPORTED_CONTENT = 509;
/**
* An unspecific error
* @since 3.5.0
*/
const GENERAL_ERROR = 599;
}
/**
* This class represents a content unit in eFront
*
* @package eFront
* @since 3.5.0
*/
class EfrontUnit extends ArrayObject
{
/**
* The maximum length for unit names. After that, the names appear truncated
*/
const MAXIMUM_NAME_LENGTH = 40;
const COMPLETION_OPTIONS_DEFAULT = 0;
const COMPLETION_OPTIONS_AUTOCOMPLETE = 1;
const COMPLETION_OPTIONS_COMPLETEWITHQUESTION = 2;
const COMPLETION_OPTIONS_COMPLETEAFTERSECONDS = 3;
const COMPLETION_OPTIONS_HIDECOMPLETEUNITICON = 4;
/**
* Class constructor
*
* This function is used to instantiate the unit object
* Since the class inherits from ArrayObject, normally
* an array should be provided for instantiation. However,
* the choice of using a unit id has been added for greater
* flexibility, but still you are advised to avoid doing so,
* since it might lead to big and unnecessary database overhead
* <br/>Example:
* <code>
* $content = eF_getTableData("content", "*");
* $unit = new EfrontUnit($content[4]); //The best way: instantiate unit using existing information
* $unit = new EfrontUnit(7); //The bad way: Let class to retrieve information. Should be avoided unless we are dealing with a single unit
* </code>
*
* @param mixed $array Either a unit information array, or a unit id
* @since 3.5.0
* @access public
*/
function __construct($array) {
if (!is_array($array)) {
if (eF_checkParameter($array, 'id')) {
$result = eF_getTableData("content", "*", "id=$array");
if (sizeof($result) == 0) {
throw new EfrontContentException(_UNITDOESNOTEXIST.": $array", EfrontContentException :: UNIT_NOT_EXISTS);
} else {
$array = $result[0];
}
} else {
throw new EfrontContentException(_INVALIDID.": $array", EfrontContentException :: INVALID_ID);
}
}
if (G_VERSIONTYPE != 'community') { #cpp#ifndef COMMUNITY
if (G_VERSIONTYPE != 'standard') { #cpp#ifndef STANDARD
//SCORM 2004 If SCORM 2004 content add the appropriate information to the unit object
$scorm2004 = in_array($array['scorm_version'], EfrontContentTreeSCORM :: $scorm2004Versions);
if ($scorm2004 && !in_array('package_ID', array_keys($array))) {
//pr(array_keys($array));
$array = $this -> convertToScorm($array);
}
} #cpp#endif
} #cpp#endif
//pr($array['options']);exit;
if (unserialize($array['options'])) {
$array['options'] = unserialize($array['options']);
} else {
$array['options'] = false;
}
$persist = false;
if (is_array($array['options']) && (isset($array['options']['hide_complete_unit']) || isset($array['options']['auto_complete']) || isset($array['options']['questions']))) { //ugprade from 3.6.9
$array['options'] = $this->upgradeUnitOptions($array['options']);
$persist = true;
}
parent :: __construct($array);
if ($persist) {
$this->persist();
}
}
private function upgradeUnitOptions($options) {
$newOptions = $options;
if ($newOptions['complete_question'] && $newOptions['questions']) {
$newOptions['complete_unit_setting'] = self::COMPLETION_OPTIONS_COMPLETEWITHQUESTION;
$newOptions['complete_question'] = $newOptions['questions'];
} else if ($newOptions['complete_question'] && $newOptions['complete_question']) {
$newOptions['complete_unit_setting'] = self::COMPLETION_OPTIONS_COMPLETEWITHQUESTION;
$newOptions['complete_question'] = $newOptions['complete_question'];
} else if ($newOptions['auto_complete']) {
$newOptions['complete_unit_setting'] = self::COMPLETION_OPTIONS_AUTOCOMPLETE;
} else if ($newOptions['hide_complete_unit']) {
$newOptions['complete_unit_setting'] = self::COMPLETION_OPTIONS_HIDECOMPLETEUNITICON;
} else {
$newOptions['complete_unit_setting'] = self::COMPLETION_OPTIONS_DEFAULT;
}
if (isset($newOptions['hide_complete_unit'])) {
unset($newOptions['hide_complete_unit']);
}
if (isset($newOptions['auto_complete'])) {
unset($newOptions['auto_complete']);
}
if (isset($newOptions['questions'])) {
unset($newOptions['questions']);
}
return $newOptions;
}
/**
* Covert unit to SCORM unit
*
* This function augments a unit so that it includes
* all the scorm-related fields
*
* @param array $array The original unit
* @return array The unit augmented with scorm fields
* @since 3.6.0
* @access public
*/
public function convertToScorm($array) {
$result = eF_getTableData("scorm_sequencing_content_to_organization as c, scorm_sequencing_organizations as o", "*", "c.content_ID=".$array['id']." AND c.organization_content_ID = o.content_ID");
$array['package_ID'] = $result[0]['content_ID'];
$array['objectives_global_to_system'] = $result[0]['objectives_global_to_system'];
$array['shared_data_global_to_system'] = $result[0]['shared_data_global_to_system'];
$result = eF_getTableData("scorm_sequencing_control_mode", "*", "content_ID=".$array['id']);
if (!empty($result)) {
$array = array_merge($array, $result[0]);
}
$result = eF_getTableData("scorm_sequencing_constrained_choice", "*", "content_ID=".$array['id']);
if (!empty($result)) {
$array = array_merge($array, $result[0]);
}
$result = eF_getTableData("scorm_sequencing_completion_threshold", "*", "content_ID=".$array['id']);
if (!empty($result)) {
$array = array_merge($array, $result[0]);
}
$result = eF_getTableData("scorm_sequencing_delivery_controls", "*", "content_ID=".$array['id']);
if (!empty($result)) {
$array = array_merge($array, $result[0]);
}
$result = eF_getTableData("scorm_sequencing_hide_lms_ui", "*", "content_ID=".$array['id']);
if (!empty($result)) {
$array['hide_lms_ui'] = unserialize($result[0]['options']);
} else {
$array['hide_lms_ui'] = false;
}
$limit_condition = eF_getTableData("scorm_sequencing_limit_conditions", "*", "content_ID = '".$array['id']."'");
if (empty($limit_condition)) {
$array['limit_condition_attempt_control'] = 'false';
} else {
$array['limit_condition_attempt_control'] = 'true';
$array = array_merge($array, $limit_condition[0]);
}
$result = eF_getTableData("scorm_sequencing_rollup_considerations", "*", "content_ID = '".$array['id']."'");
if (!empty($result)) {
$array = array_merge($array, $result[0]);
}
$result = eF_getTableData("scorm_sequencing_rollup_controls", "*", "content_ID = '".$array['id']."'");
if (!empty($result)) {
$array = array_merge($array, $result[0]);
}
//SCORM 2004 Rollup rules
$result = eF_getTableData("scorm_sequencing_rollup_rules", "*", "content_ID = ".$array['id']);
$array['rollup_rules'] = $result;
foreach ($result as $key => $value) {
$result = eF_getTableData("scorm_sequencing_rollup_rule", "*", "scorm_sequencing_rollup_rules_ID = ".$value['id']);
$array['rollup_rules'][$key]['rollup_rule'] = $result;
}
//SCORM 2004 Rules
$result = eF_getTableData("scorm_sequencing_rules", "*", "content_ID = ".$array['id']);
$array['rules'] = $result;
foreach ($result as $key => $value) {
$result = eF_getTableData("scorm_sequencing_rule", "*", "scorm_sequencing_rules_ID = ".$value['id']);
$array['rules'][$key]['rule'] = $result;
}
return $array;
}
/**
* Store changed values to the database
*
* This unit is used to stored any changed values to the database
* <br/>Example:
* <code>
* $unit['name'] = 'new name';
* $unit -> persist();
* </code>
*
* @return boolean true if everything is ok
* @since 3.5.0
* @access public
*/
public function persist() {
$fields = array('name' => $this['name'],
'data' => $this['data'],
'parent_content_ID' => $this['parent_content_ID'],
'lessons_ID' => $this['lessons_ID'],
'timestamp' => $this['timestamp'],
'ctg_type' => $this['ctg_type'],
'active' => $this['active'],
'linked_to' => $this['linked_to'],
'previous_content_ID' => $this['previous_content_ID'],
'options' => !is_array($this['options']) && $this['options'] ? $this['options'] : serialize($this['options']),
'metadata' => $this['metadata']);
//The special string efront#special#text is used in order to remove the (heavy) content from the nodes when traversing it. However,
//there is a chance that the tree traversal persists values as well. So, using this special string, we know that we must not
//alter the content
if ($this['data'] == 'efront#special#text') {
unset($fields['data']);
}
EfrontEvent::triggerEvent(array("type" => EfrontEvent::CONTENT_MODIFICATION, "lessons_ID" => $this['lessons_ID'], "entity_ID" => $this['id'], "entity_name" => $this['name']));
eF_updateTableData("content", $fields, "id=".$this['id']);
$result = eF_getTableData("content", "id", "linked_to={$this['id']}");
foreach ($result as $value) {
eF_updateTableData("content", array('name' => $this['name'], 'data' => $this['data'], 'ctg_type' => $this['ctg_type'], 'metadata' => $this['metadata']), "id={$value['id']}");
}
return true;
}
/**
* Set search keywords
*
* This function updates the search keywords related to this unit's name and content.
* It should be executed when and only when there is a change in any of the above fields,
* since it performs excessive database queries.
* <br>Example:
* <code>
* $unit = new EfrontUnit(34); //Instantiate unit with id 34
* $unit['name'] = 'New unit name'; //Change unit name
* $unit -> persist(); //Store new data
* $unit -> setSearchKeywords(); //Update keywords
* </code>
*
* @return boolean true if everything is ok
* @since 3.5.2
* @access public
*/
public function setSearchKeywords() {
EfrontSearch :: removeText('content', $this['id'], 'data'); //Refresh the search keywords
EfrontSearch :: insertText($this['data'], $this['id'], "content", "data");
EfrontSearch :: removeText('content', $this['id'], 'title'); //Refresh the search keywords
EfrontSearch :: insertText($this['name'], $this['id'], "content", "title");
}
/**
* Delete unit
*
* This function is used to delete the current unit.
* <br/>Example:
* <code>
* $unit -> delete();
* </code>
*
* @return boolean true if everything is ok
* @since 3.5.0
* @access public
*/
public function delete() {
if ($this['ctg_type'] == 'tests' || $this['ctg_type'] == 'feedback') {
$result = eF_getTableData("tests", "id, content_ID", "content_ID=".$this['id']);
if (sizeof($result) > 0) {
$test = new EfrontTest($result[0]);
$test -> delete();
}
}
$result = eF_getTableData("lesson_conditions", "*", "lessons_ID=".$this['lessons_ID']);
foreach ($result as $value) {
$conditionOptions = unserialize($value['options']);
if (($value['type'] == 'specific_unit' || $condition['type'] == 'specific_test') && $conditionOptions[0] == $this['id']) {
eF_deleteTableData("lesson_conditions", "id=".$value['id']);
}
}
eF_deleteTableData("content", "id=".$this['id']); //Delete Unit from database
eF_deleteTableData("scorm_data", "content_ID=".$this['id']); //Delete Unit from scorm_data
eF_deleteTableData("comments", "content_ID=".$this['id']); //Delete comments of this unit
eF_deleteTableData("users_to_content", "content_ID=".$this['id']); //Delete time data for the unit
eF_deleteTableData("rules", "content_ID=".$this['id']." OR rule_content_ID=".$this['id']); //Delete rules associated with this unit
eF_updateTableData("questions", array("content_ID" => 0), "content_ID=".$this['id']); //Remove association of questions with this unit but not delete them
EfrontSearch :: removeText('content', $this['id'], ''); //Delete keywords
//Delete scorm data related to the unit
if (G_VERSIONTYPE != 'community') { #cpp#ifndef COMMUNITY
if (G_VERSIONTYPE != 'standard') { #cpp#ifndef STANDARD
EfrontContentTreeSCORM :: deleteSCORMContentOrganization($this['id']);
} #cpp#endif
} #cpp#endif
}
/**
* Activate unit
*
* This function is used to activate the current unit.
* If the unit is a test unit, the correspoding test is also activated
* <br/>Example:
* <code>
* $unit = new EfrontUnit(43); //Instantiate object for unit with id 43
* $unit -> activate(); //Activate unit
* </code>
*
* @since 3.5.0
* @access public
*/
public function activate() {
$this['active'] = 1;
$this -> persist();
if ($this['ctg_type'] == 'tests') {
$result = eF_getTableData("tests", "id", "content_ID=".$this['id']);
if (sizeof($result) > 0) {
$test = new EfrontTest($result[0]['id']);
if (!$test -> test['active']) {
$test -> activate();
}
}
}
}
/**
* Deactivate unit
*
* This function is used to deactivate the current unit.
* If the unit is a test unit, the correspoding test is also deactivated
* <br/>Example:
* <code>
* $unit = new EfrontUnit(43); //Instantiate object for unit with id 43
* $unit -> deactivate(); //Deactivate unit
* </code>
*
* @since 3.5.0
* @access public
*/
public function deactivate() {
if ($this['ctg_type'] == 'tests') {
$result = eF_getTableData("tests", "id", "content_ID=".$this['id']);
if (sizeof($result) > 0) {
$test = new EfrontTest($result[0]['id']);
if ($test -> test['active']) {
$test -> deactivate();
}
}
}
$this['active'] = 0;
$this -> persist();
}
/**
* Get unit questions
*
* This function returns a list with all the questions
* that belong to this unit. If $returnObjects is true, then
* Question objects are returned.
* <br/>Example:
* <code>
* $questions = $this -> getQuestions(); //Get a simple list of questions
* $questions = $this -> getQuestions(true); //Get a list of Question objects
* </code>
*
* @param boolean $returnObjects Whether to return Question objects
* @return array An array of questions
* @since 3.5.0
* @access public
*/
public function getQuestions($returnObjects = false) {
$questions = array();
$result = eF_getTableData("questions", "*", "content_ID=".$this['id']);
if (sizeof($result) > 0) {
foreach ($result as $value) {
$returnObjects ? $questions[$value['id']] = QuestionFactory :: factory($value) : $questions[$value['id']] = $value;
}
}
return $questions;
}
/**
* Query if the unit is a test
*
* This function returns true if the unit corresponds to a test,
* otherwise it returns false
* <br/>Example:
* <code>
* $unit = new EfrontUnit(7);
* $flg = $unit->isTest();
* </code>
*
*
* @return boolean A flag to indicate if the unit is test
* @since 3.5.0
* @access public
*/
public function isTest(){
if (parent::offsetGet('ctg_type') == "tests"){
return true;
}
else{
return false;
}
}
public function toXML(){
$xml = '<?xml version="1.0" encoding="UTF-8"?>' ."\n";
$xml .= "\t" . '<unit>';
$xml .= "\t\t<name>".parent::offsetGet('name')."</name>\n";
$xml .= "\t\t<ctg_type>".parent::offsetGet('ctg_type')."</ctg_type>\n";
$xml .= "\t</unit>";
return $xml;
}
public function fromXML($xmlstr){
$xml = new SimpleXMLElement($xmlstr);
parent::offsetSet('name', (string)$xml->unit->name);
parent::offsetSet('ctg_type', (string)$xml->unit->ctg_type);
}
/**
* Get the id of prerequisite unit for this unit
*
* This function returns false if there is no prerequisite unit,
* otherwise returns the id of the prerequisite unit
* <br/>Example:
* <code>
* $unit = new EfrontUnit(7);
* $pid = $unit->getPrerequisite();
* </code>
*
*
* @return mixed An integer id if there is a prerequisite, or false otherwise
* @since 3.5.0
* @access public
*/
public function getPrerequisite(){
if (parent::offsetGet('previous_content_ID') != "0"){
return parent::offsetGet('previous_content_ID');
}
else
return false;
}
/**
* Get the lesson files
*
* This function returns an array of the file ids or paths which are used by this unit
* <br/>Example:
* <code>
* $unit = new EfrontUnit(7);
* $files = $unit -> getFiles();
* </code>
*
* @return array An array with the file ids
* @since 3.5.0
* @access public
*/
public function getFiles($returnObjects = false) {
$files = array();
$data = parent :: offsetGet('data');
preg_match_all("/view_file\.php\?file=(\d+)/", $data, $matchesId);
$filesId = $matchesId[1];
preg_match_all("#(".G_SERVERNAME.")*content/lessons/(.*)\"#U", $data, $matchesPath);
$filesPath = $matchesPath[2];
try {
foreach ($filesId as $file) {
$file = trim($file, "';");
$returnObjects ? $files[] = new EfrontFile($file) : $files[] = $file;
}
foreach ($filesPath as $file) {
$returnObjects ? $files[] = new EfrontFile(G_LESSONSPATH.html_entity_decode(urldecode($file))) : $files[] = G_LESSONSPATH.html_entity_decode(urldecode($file));
}
} catch (Exception $e) {
//don't halt for non-existing files
}
return $files;
}
/**
* Create a new unit
*
* This function is used to create a new unit.
* <br/>Example:
* <code>
* $fields = array('name' => 'new unit', 'ctg_type' => 'theory');
* $unit = EfrontUnit :: createUnit($fields);
* </code>
*
* @param array $fields The new unit fields
* @return EfrontUnit The newly created unit
* @since 3.5.0
* @access public
*/
public static function createUnit($fields = array()) {
if (!isset($fields['lessons_ID'])) {
return false;
}
!isset($fields['name']) ? $fields['name'] = 'Default unit' : null;
!isset($fields['timestamp']) ? $fields['timestamp'] = time() : null;
!isset($fields['ctg_type']) ? $fields['ctg_type'] = 'theory' : null;
if (!isset($fields['metadata'])) {
$defaultMetadata = array('title' => $fields['name'],
'creator' => $GLOBALS['currentUser'] -> user['name'].' '.$GLOBALS['currentUser'] -> user['surname'],
'publisher' => $GLOBALS['currentUser'] -> user['name'].' '.$GLOBALS['currentUser'] -> user['surname'],
'contributor' => $GLOBALS['currentUser'] -> user['name'].' '.$GLOBALS['currentUser'] -> user['surname'],
'date' => date("Y/m/d", $fields['timestamp']),
'type' => 'content');
$fields['metadata'] = serialize($defaultMetadata);
}
$newId = eF_insertTableData("content", $fields);
$result = eF_getTableData("content", "*", "id=".$newId); //We perform an extra step/query for retrieving data, sinve this way we make sure that the array fields will be in correct order (forst id, then name, etc)
$unit = new EfrontUnit($result[0]);
EfrontSearch :: insertText(htmlspecialchars($fields['name'], ENT_QUOTES), $unit['id'], "content", "title");
EfrontEvent::triggerEvent(array("type" => EfrontEvent::CONTENT_CREATION, "lessons_ID" => $fields['lessons_ID'], "entity_ID" => $unit['id'], "entity_name" => $fields['name']));
return $unit;
}
}
/**
* This class represents the content tree and extends EfrontTree class
* @package eFront
* @since 3.5.0
*/
class EfrontContentTree extends EfrontTree
{
/**
* The lesson id
*
* @var int
* @since 3.5.0
* @access public
*/
public $lessonId = 0;
/**
* Content rules. The array is initialized only after the call to getRules()
*
* @since 3.5.0
* @var array
* @access public
* @see getRules()
*/
protected $rules = false;
/**
* These values signify the SCORM 2004 version
*
* @since 3.6.0
* @var array
* @access public
* @static
*/
public static $scorm2004Versions = array('CAM 1.3' , '2004 3rd Edition', '2004 4th Edition');
/**
* Instantiate tree object
*
* The constructor instantiates the tree based on the lesson id
* <br/>Example:
* <code>
* $tree = new EfrontContentTree(23); //23 is the lesson id
* $lesson = new EfrontLesson(23); //23 is the lesson id
* $tree = new EfrontContentTree($lesson); //Content may be alternatively instantiated using the lesson object
* </code>
*
* @param mixed $lesson Either The lesson id or an EfrontLesson object
* @param array $data If true, then the tree nodes hold data as well
* @since 3.5.0
* @access public
*/
function __construct($lesson, $data = false) {
if ($lesson instanceof EfrontLesson) {
$lessonId = $lesson -> lesson['id'];
} elseif (!eF_checkParameter($lesson, 'id')) {
throw new EfrontContentException(_INVALIDLESSONID.': '.$lesson, EfrontContentException :: INVALID_ID);
} else {
$lessonId = $lesson;
}
$this -> lessonId = $lessonId; //Set the lesson id
$this -> data = $data; //Is used in reset()
$this -> reset(); //Initialize content tree
$firstUnit = $this -> getFirstNode();
$this -> currentUnitId = $firstUnit['id'];
}
/**
* Construct content tree structure
*
* Creates a tree-like representation of the content, using arrays as EfrontUnit,
* a class that extends ArrayObject.
* Each unit is represented as an array with the appropriate fields
* (id, name, timestamp etc). If the unit has children units, then
* these are subarrays of the current unit array. All keys correspond
* to unit ids.
* If, for some reason, there are units with invalid succession data,
* (parent or previous content ids), these are appended at the end of
* the content tree.
* <br/>Example:
* <code>
* $content = new EfrontContentTree(4); //Initialize content tree for lesson with id 4
* //Do some nasty stuff with content tree
* $content -> reset(); //Reset content tree to its original state
* </code>
*
* @since 3.5.0
* @access public
*/
public function reset() {
if ($this -> data) {
$result = eF_getTableData("content", "*, data != '' as has_data", "lessons_ID = '".$this -> lessonId."'");
} else {
$fields = eF_getTableFields("content");
unset($fields[array_search('data', $fields)]);
$result = eF_getTableData("content", implode(",", $fields).", data != '' as has_data", "lessons_ID = '".$this -> lessonId."'");
}
if (sizeof($result) == 0) {
$this -> tree = new RecursiveArrayIterator(array());
return;
}
$scorm2004Units = array();
$units = array();
foreach ($result as $unit) {
$units[$unit['id']] = $unit;
if (G_VERSIONTYPE != 'community') { #cpp#ifndef COMMUNITY
if (G_VERSIONTYPE != 'standard') { #cpp#ifndef STANDARD
if (in_array($unit['scorm_version'], EfrontContentTreeSCORM :: $scorm2004Versions)) {
$scorm2004Units[] = $unit['id'];
}
} #cpp#endif
} #cpp#endif
}
if (!empty($scorm2004Units)) {
$units = $this -> convertUnitsTo2004($units, $scorm2004Units);
}
//$units = eF_getTableData("content", "id,name,parent_content_ID,lessons_ID,timestamp,ctg_type,active,previous_content_ID", "lessons_ID = '".$this -> lessonId."'");
$rejected = array();
foreach ($units as $node) { //Assign previous content ids as keys to the previousNodes array, which will be used for sorting afterwards
if (!$this -> data) {
$node['has_data'] ? $node['data'] = 'efront#special#text' : $node['data'] = ''; //Eliminate with 'efront#special#text' data for units that don't have any content, and set an empty space ' ' for units that have content. This is done so that the toHTML can handle differently the ones from the others. The efront#special#text is checked by persist() in order to leave data unchanged in case we are updating
}
$node = new EfrontUnit($node); //We convert arrays to array objects, which is best for manipulating data through iterators
if (!isset($previousNodes[$node['previous_content_ID']])) {
$previousNodes[$node['previous_content_ID']] = $node;
} else {
$rejected[$node['id']] = $node; //$rejected holds cut off units, which do not have a valid previous_content_ID
}
}
$node = 0;
$count = 0;
$nodes = array(); //$count is used to prevent infinite loops
while (sizeof($previousNodes) > 0 && isset($previousNodes[$node]) && $count++ < 10000) { //Order the nodes array according to previous_content_ID information. if $previousNodes[$node] is not set, it means that there are illegal previous content id entries in the array (for example, a unit reports as previous a non-existent unit). In this case, all the remaining units in the $previousNodes array are rejected
$nodes[$previousNodes[$node]['id']] = $previousNodes[$node]; //Assign the previous node to be the array key
$newNode = $previousNodes[$node]['id'];
unset($previousNodes[$node]);
$node = $newNode;
}
if (sizeof($previousNodes) > 0) { //If $previousNodes is not empty, it means there are invalid (orphan) units in the array, so append them to the $rejected list
foreach ($previousNodes as $value) {
$rejected[$value['id']] = $value;
}
}
$tree = $nodes;
$count = 0; //$count is used to prevent infinite loops
while (sizeof($tree) > 1 && $count++ < 50000) { //We will merge all branches under the main tree branch, the 0 node, so its size will become 1
foreach ($nodes as $key => $value) {
if ($value['parent_content_ID'] == 0 || in_array($value['parent_content_ID'], array_keys($nodes))) { //If the unit parent is in the $nodes array keys - which are the unit ids- or it is 0, then it is valid
$parentNodes[$value['parent_content_ID']][] = $value; //Find which nodes have children and assign them to $parentNodes
$tree[$value['parent_content_ID']][$value['id']] = array(); //We create the "slots" where the node's children will be inserted. This way, the ordering will not be lost
} else {
$rejected = $rejected + array($value['id'] => $value); //Append units with invalid parents to $rejected list
unset($nodes[$key]); //Remove the invalid unit from the units array, as well as from the parentUnits, in case a n entry for it was created earlier
unset($parentNodes[$value['parent_content_ID']]);
}
}
if (isset($parentNodes)) { //If the unit was rejected, there won't be a $parentNodes array
$leafNodes = array_diff(array_keys($nodes), array_keys($parentNodes)); //Now, it's easy to see which nodes are leaf nodes, just by subtracting $parentNodes from the whole set
foreach ($leafNodes as $leaf) {
$parent_id = $nodes[$leaf]['parent_content_ID']; //Get the leaf's parent
$tree[$parent_id][$leaf] = $tree[$leaf]; //Append the leaf to its parent's tree branch
unset($tree[$leaf]); //Remove the leaf from the main tree branch
unset($nodes[$leaf]); //Remove the leaf from the nodes set
}
unset($parentNodes); //Reset $parentNodes; new ones will be calculated at the next loop
}
}
if (sizeof($tree) > 0 && !isset($tree[0])) { //This is a special case, where only one node exists in the tree
$tree = array($tree);
}
isset($tree[0]) ? $tree = $tree[0] : $tree = array();
if (sizeof($rejected) > 0) { //Append rejected nodes to the end of the tree array, updating their parent/previous information
foreach (new EfrontNodeFilterIterator(new RecursiveIteratorIterator(new RecursiveArrayIterator($tree), RecursiveIteratorIterator :: SELF_FIRST)) as $lastUnit); //Advance to the last tree node
isset($lastUnit) ? $previousId = $lastUnit['id'] : $previousId = 0; //There is a chance that no normal units exist in the tree. In this case, there will be no $lastUnit
foreach ($rejected as $id => $node) { //Update broken nodes
$node['parent_content_ID'] = 0;
$node['previous_content_ID'] = $previousId;
$node -> persist(); //Persist changes to the database
$tree[$id] = $node;
$previousId = $id;
}
}
$this -> tree = new RecursiveArrayIterator($tree);
//Create arrays for assigning the immediate children and the parents of each unit. These will come especially handy for SCORM 2004 calculations
$this -> immediateDescendants = array();
$this -> nodeParents = array();
foreach (new EfrontNodeFilterIterator(new RecursiveIteratorIterator(new RecursiveArrayIterator($tree), RecursiveIteratorIterator :: SELF_FIRST)) as $key => $value) {
foreach(array_keys((array)$value) as $member) {
!is_numeric($member) OR $this -> immediateDescendants[$key][] = $member;
}
if (isset($this -> nodeParents[$value['parent_content_ID']]) && $this -> nodeParents[$value['parent_content_ID']]) {
$this -> nodeParents[$key] = array_merge(array($value['parent_content_ID']), $this -> nodeParents[$value['parent_content_ID']]);
} else {
$this -> nodeParents[$key] = array($value['parent_content_ID']);
}
}
//pr($this -> nodeParents);
//pr($this -> immediateDescendants);
}
public function convertUnitsTo2004($units, $scorm2004Units) {
$scormContentIds = implode(",", $scorm2004Units);
$result = eF_getTableData("scorm_sequencing_content_to_organization as c, scorm_sequencing_organizations as o", "c.content_ID, c.organization_content_ID, o.objectives_global_to_system, o.shared_data_global_to_system", "c.content_ID in ($scormContentIds) AND c.organization_content_ID = o.content_ID");
foreach ($result as $value) {
$units[$value['content_ID']]['package_ID'] = $value['organization_content_ID'];
$array['objectives_global_to_system'] = $value['objectives_global_to_system'];
$array['shared_data_global_to_system'] = $value['shared_data_global_to_system'];
}
$result = eF_getTableData("scorm_sequencing_control_mode", "*", "content_ID in ($scormContentIds)");
foreach ($result as $value) {
$units[$value['content_ID']] = array_merge($units[$value['content_ID']], $value);
}
$result = eF_getTableData("scorm_sequencing_constrained_choice", "*", "content_ID in ($scormContentIds)");
foreach ($result as $value) {
$units[$value['content_ID']] = array_merge($units[$value['content_ID']], $value);
}
$result = eF_getTableData("scorm_sequencing_completion_threshold", "*", "content_ID in ($scormContentIds)");
foreach ($result as $value) {
$units[$value['content_ID']] = array_merge($units[$value['content_ID']], $value);
}
$result = eF_getTableData("scorm_sequencing_delivery_controls", "*", "content_ID in ($scormContentIds)");
foreach ($result as $value) {
$units[$value['content_ID']] = array_merge($units[$value['content_ID']], $value);
}
$result = eF_getTableData("scorm_sequencing_hide_lms_ui", "*", "content_ID in ($scormContentIds)");
foreach ($result as $value) {
$units[$value['content_ID']]['hide_lms_ui'] = unserialize($value['options']);
}
$result = eF_getTableData("scorm_sequencing_limit_conditions", "*", "content_ID in ($scormContentIds)");
foreach ($scorm2004Units as $value) { //First, assign to every SCORM unit the 'false' for the limit_condition_attempt_control...
$units[$value]['limit_condition_attempt_control'] = 'false';
}
foreach ($result as $value) { //...and then set it to true where applicable
$units[$value['content_ID']]['limit_condition_attempt_control'] = 'true';
$units[$value['content_ID']] = array_merge($units[$value['content_ID']], $value);
}
$result = eF_getTableData("scorm_sequencing_rollup_considerations", "*", "content_ID in ($scormContentIds)");
foreach ($result as $value) {
$units[$value['content_ID']] = array_merge($units[$value['content_ID']], $value);
}
$result = eF_getTableData("scorm_sequencing_rollup_controls", "*", "content_ID in ($scormContentIds)");
foreach ($result as $value) {
$units[$value['content_ID']] = array_merge($units[$value['content_ID']], $value);
}
//SCORM 2004 Rollup rules
//First get all rollup rules to an array and assign them to each rollup rull group, based on scorm_sequencing_rollup_rules_ID
$allRollupRules = array();
$result = eF_getTableData("scorm_sequencing_rollup_rule", "*");
foreach ($result as $value) {
$allRollupRules[$value['scorm_sequencing_rollup_rules_ID']][] = $value;
}
//Now, asssign the rollup rule and the group to the unit
$result = eF_getTableData("scorm_sequencing_rollup_rules", "*", "content_ID in ($scormContentIds)");
foreach ($result as $value) {
$value['rollup_rule'] = $allRollupRules[$value['id']];
$units[$value['content_ID']]['rollup_rules'][] = $value;
// $units[$value['content_ID']]['rollup_rules']['rollup_rule'] = $allRollupRules[$value['id']];
// $units[$value['content_ID']] = array_merge($units[$value['content_ID']], $allRollupRules[$value['id']]);
}
//First get all sequencing rules to an array and assign them to each sequencing rull group, based on scorm_sequencing_rules_ID
$allRules = array();
$result = eF_getTableData("scorm_sequencing_rule", "*");
foreach ($result as $value) {
$allRules[$value['scorm_sequencing_rules_ID']][] = $value;
//$allRollupRules[$value['scorm_sequencing_rollup_rules_ID']][] = array('rollup_rule' => $value);
}
//Now, asssign the sequencing rule and the group to the unit
$result = eF_getTableData("scorm_sequencing_rules", "*", "content_ID in ($scormContentIds)");
foreach ($result as $key => $value) {
$value['rule'] = $allRules[$value['id']];
$units[$value['content_ID']]['rules'][] = $value;
// $units[$value['content_ID']]['rules']['rule'] = $allRules[$value['id']];
// $value['rule'] = $allRules[$value['id']]['rule'];
// $units[$value['content_ID']]['rules'][$key] = $value;
//pr($value);
//pr($allRules[$value['id']]);
//$units[$value['content_ID']] = array_merge($units[$value['content_ID']], $allRules[$value['id']]);
//pr($units[$value['content_ID']]);
}
// pr($value);
/*
$result = eF_getTableData("scorm_sequencing_rollup_rules", "*", "content_ID in ($scormContentIds)");
$array['rollup_rules'] = $result;
foreach ($result as $key => $value) {
$result = eF_getTableData("scorm_sequencing_rollup_rule", "*", "scorm_sequencing_rollup_rules_ID = ".$value['id']);
$array['rollup_rules'][$key]['rollup_rule'] = $result;
}
//SCORM 2004 Rules
$result = eF_getTableData("scorm_sequencing_rules", "*", "content_ID in ($scormContentIds)");
$array['rules'] = $result;
foreach ($result as $key => $value) {
$result = eF_getTableData("scorm_sequencing_rule", "*", "scorm_sequencing_rules_ID = ".$value['id']);
$array['rules'][$key]['rule'] = $result;
}
*/
return $units;
}
/**
* Remove unit
*
* This function is used to remove a unit from the content tree
* <br/>Example:
* <code>
* $content = new EfrontContentTree(4); //Initialize content tree for lesson with id 4
* $content -> removeNode(57); //Remove the unit 57 and all of its subunits
* </code>
*
* @param int $removeId The unit id that will be removed
* @since 3.5.0
* @access public
*/
public function removeNode($removeId) {
$iterator = new EfrontNodeFilterIterator(new RecursiveIteratorIterator($this -> tree, RecursiveIteratorIterator :: SELF_FIRST)); //Get an iterator for the current tree. This iterator returns only whole unit arrays and not unit members separately (such as id, timestamp etc)
$iterator -> rewind(); //Initialize iterator
while ($iterator -> valid() && $iterator -> key() != $removeId) { //Forward iterator index until you reach the designated element, which has an index equal to the unit id that will be removed
$iterator -> next();
}
if ($iterator -> valid()) {
$iterator -> current() -> delete(); //Delete the current unit from the database
$previousUnit = $this -> getPreviousNode($iterator -> key()); //Get the deleted unit's previous unit
$iterator -> offsetUnset($removeId); //Delete the unit from the content tree
if ($previousUnit) { //If we are deleting the first unit, there is no previous unit
$nextUnit = $this -> getNextNode($previousUnit['id']); //Get the previous unit's next unit, which still points to the old unit
if ($nextUnit) { //If we are deleting the last unit, there is not next unit
$nextUnit['previous_content_ID'] = $previousUnit['id']; //Update the next unit to point at the deleted unit' previous unit
$nextUnit -> persist(); //Persist these changes to the database