/
forumfunction.inc.php
executable file
·5859 lines (5236 loc) · 226 KB
/
forumfunction.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* For licensing terms, see /license.txt */
/**
* These files are a complete rework of the forum. The database structure is
* based on phpBB but all the code is rewritten. A lot of new functionalities
* are added:
* - forum categories and forums can be sorted up or down, locked or made invisible
* - consistent and integrated forum administration
* - forum options: are students allowed to edit their post?
* moderation of posts (approval)
* reply only forums (students cannot create new threads)
* multiple forums per group
* - sticky messages
* - new view option: nested view
* - quoting a message
*
* @package chamilo.forum
*
* @todo several functions have to be moved to the itemmanager library
* @todo displaying icons => display library
* @todo complete the missing phpdoc the correct order should be
*/
use ChamiloSession as Session;
use Doctrine\Common\Collections\Criteria;
define('FORUM_NEW_POST', 0);
get_notifications_of_user();
$htmlHeadXtra[] = api_get_jquery_libraries_js(array('jquery-ui', 'jquery-upload'));
$htmlHeadXtra[] = '<script>
function check_unzip() {
if (document.upload.unzip.checked){
document.upload.if_exists[0].disabled=true;
document.upload.if_exists[1].checked=true;
document.upload.if_exists[2].disabled=true;
} else {
document.upload.if_exists[0].checked=true;
document.upload.if_exists[0].disabled=false;
document.upload.if_exists[2].disabled=false;
}
}
function setFocus() {
$("#title_file").focus();
}
</script>';
// The next javascript script is to manage ajax upload file
$htmlHeadXtra[] = api_get_jquery_libraries_js(array('jquery-ui', 'jquery-upload'));
// Recover Thread ID, will be used to generate delete attachment URL to do ajax
$threadId = isset($_REQUEST['thread']) ? intval($_REQUEST['thread']) : 0;
$forumId = isset($_REQUEST['forum']) ? intval($_REQUEST['forum']) : 0;
// The next javascript script is to delete file by ajax
$htmlHeadXtra[] = '<script>
$(function () {
$(document).on("click", ".deleteLink", function(e) {
e.preventDefault();
e.stopPropagation();
var l = $(this);
var id = l.closest("tr").attr("id");
var filename = l.closest("tr").find(".attachFilename").html();
if (confirm("' . get_lang('AreYouSureToDeleteJS') . '", filename)) {
$.ajax({
type: "POST",
url: "'.api_get_path(WEB_AJAX_PATH) . 'forum.ajax.php?'.api_get_cidreq().'&a=delete_file&attachId=" + id +"&thread='.$threadId .'&forum='.$forumId .'",
dataType: "json",
success: function(data) {
if (data.error == false) {
l.closest("tr").remove();
if ($(".files td").length < 1) {
$(".files").closest(".control-group").hide();
}
}
}
})
}
});
});
</script>';
/**
* This function handles all the forum and forum categories actions. This is a wrapper for the
* forum and forum categories. All this code code could go into the section where this function is
* called but this make the code there cleaner.
* @param int $lp_id Learning path Id
*
* @return void
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @author Juan Carlos Raña Trabado (return to lp_id)
* @version may 2011, Chamilo 1.8.8
*/
function handle_forum_and_forumcategories($lp_id = null)
{
$action_forum_cat = isset($_GET['action']) ? $_GET['action'] : '';
$get_content = isset($_GET['content']) ? $_GET['content'] : '';
$post_submit_cat = isset($_POST['SubmitForumCategory']) ? true : false;
$post_submit_forum = isset($_POST['SubmitForum']) ? true : false;
$get_id = isset($_GET['id']) ? intval($_GET['id']) : '';
$forum_categories_list = get_forum_categories();
//Verify if forum category exists
if (empty($forum_categories_list)) {
$get_content = 'forumcategory';
}
// Adding a forum category
if (($action_forum_cat == 'add' && $get_content == 'forumcategory') || $post_submit_cat) {
show_add_forumcategory_form(array(), $lp_id); //$lp_id when is called from learning path
}
// Adding a forum
if ((($action_forum_cat == 'add' || $action_forum_cat == 'edit') && $get_content == 'forum') || $post_submit_forum) {
if ($action_forum_cat == 'edit' && $get_id || $post_submit_forum) {
$inputvalues = get_forums($get_id);
} else {
$inputvalues = array();
}
show_add_forum_form($inputvalues, $lp_id);
}
// Edit a forum category
if (($action_forum_cat == 'edit' && $get_content == 'forumcategory') || (isset($_POST['SubmitEditForumCategory'])) ? true : false) {
$forum_category = get_forum_categories($get_id);
show_edit_forumcategory_form($forum_category);
}
// Delete a forum category
if ($action_forum_cat == 'delete') {
$id_forum = intval($get_id);
$list_threads = get_threads($id_forum);
for ($i = 0; $i < count($list_threads); $i++) {
deleteForumCategoryThread('thread', $list_threads[$i]['thread_id']);
$link_info = GradebookUtils::is_resource_in_course_gradebook(
api_get_course_id(),
5,
intval($list_threads[$i]['thread_id']),
api_get_session_id()
);
if ($link_info !== false) {
GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
}
}
$return_message = deleteForumCategoryThread($get_content, $get_id);
Display::display_confirmation_message($return_message, false);
}
// Change visibility of a forum or a forum category.
if ($action_forum_cat == 'invisible' || $action_forum_cat == 'visible') {
$return_message = change_visibility($get_content, $get_id, $action_forum_cat);
Display::display_confirmation_message($return_message, false);
}
// Change lock status of a forum or a forum category.
if ($action_forum_cat == 'lock' || $action_forum_cat == 'unlock') {
$return_message = change_lock_status($get_content, $get_id, $action_forum_cat);
Display::display_confirmation_message($return_message, false);
}
// Move a forum or a forum category.
if ($action_forum_cat == 'move' && isset($_GET['direction'])) {
$return_message = move_up_down($get_content, $_GET['direction'], $get_id);
Display::display_confirmation_message($return_message, false);
}
}
/**
* This function displays the form that is used to add a forum category.
*
* @param array $inputvalues (deprecated, set to null when calling)
* @param int $lp_id Learning path ID
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @author Juan Carlos Raña Trabado (return to lp_id)
* @version may 2011, Chamilo 1.8.8
*/
function show_add_forumcategory_form($inputvalues = array(), $lp_id)
{
$gradebook = Security::remove_XSS($_GET['gradebook']);
// Initialize the object.
$form = new FormValidator('forumcategory', 'post', 'index.php?gradebook='.$gradebook.'&'.api_get_cidreq());
// hidden field if from learning path
$form->addElement('hidden', 'lp_id', $lp_id);
// Setting the form elements.
$form->addElement('header', get_lang('AddForumCategory'));
$form->addElement('text', 'forum_category_title', get_lang('Title'), array('autofocus'));
$form->addElement(
'html_editor',
'forum_category_comment',
get_lang('Description'),
null,
array('ToolbarSet' => 'Forum', 'Width' => '98%', 'Height' => '200')
);
$form->addButtonCreate(get_lang('CreateCategory'), 'SubmitForumCategory');
// Setting the rules.
$form->addRule('forum_category_title', get_lang('ThisFieldIsRequired'), 'required');
// The validation or display
if ($form->validate()) {
$check = Security::check_token('post');
if ($check) {
$values = $form->exportValues();
store_forumcategory($values);
}
Security::clear_token();
} else {
$token = Security::get_token();
$form->addElement('hidden', 'sec_token');
$form->setConstants(array('sec_token' => $token));
$form->display();
}
}
/**
* This function displays the form that is used to add a forum category.
*
* @param array $inputvalues
* @param int $lp_id
* @return void HTML
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @author Juan Carlos Raña Trabado (return to lp_id)
*
* @version may 2011, Chamilo 1.8.8
*/
function show_add_forum_form($inputvalues = array(), $lp_id)
{
$_course = api_get_course_info();
$gradebook = Security::remove_XSS($_GET['gradebook']);
// Initialize the object.
$form = new FormValidator('forumcategory', 'post', 'index.php?gradebook='.$gradebook.'&'.api_get_cidreq());
// The header for the form
if (!empty($inputvalues)) {
$form_title = get_lang('EditForum');
} else {
$form_title = get_lang('AddForum');
}
$session_header = isset($_SESSION['session_name']) ? ' ('.$_SESSION['session_name'].') ' : '';
$form->addElement('header', $form_title.$session_header);
// We have a hidden field if we are editing.
if (!empty($inputvalues) && is_array($inputvalues)) {
$my_forum_id = isset($inputvalues['forum_id']) ? $inputvalues['forum_id'] : null;
$form->addElement('hidden', 'forum_id', $my_forum_id);
}
$lp_id = intval($lp_id);
// hidden field if from learning path
$form->addElement('hidden', 'lp_id', $lp_id);
// The title of the forum
$form->addElement('text', 'forum_title', get_lang('Title'), array('autofocus'));
// The comment of the forum.
$form->addElement(
'html_editor',
'forum_comment',
get_lang('Description'),
null,
array('ToolbarSet' => 'Forum', 'Width' => '98%', 'Height' => '200')
);
// Dropdown list: Forum categories
$forum_categories = get_forum_categories();
foreach ($forum_categories as $key => $value) {
$forum_categories_titles[$value['cat_id']] = $value['cat_title'];
}
$form->addElement('select', 'forum_category', get_lang('InForumCategory'), $forum_categories_titles);
$form->applyFilter('forum_category', 'html_filter');
if ($_course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD) {
// This is for horizontal
$group = array();
$group[] = $form->createElement('radio', 'allow_anonymous', null, get_lang('Yes'), 1);
$group[] = $form->createElement('radio', 'allow_anonymous', null, get_lang('No'), 0);
$form->addGroup($group, 'allow_anonymous_group', get_lang('AllowAnonymousPosts'), ' ');
}
$form->addButtonAdvancedSettings('advanced_params');
$form->addElement('html', '<div id="advanced_params_options" style="display:none">');
$group = array();
$group[] = $form->createElement('radio', 'students_can_edit', null, get_lang('Yes'), 1);
$group[] = $form->createElement('radio', 'students_can_edit', null, get_lang('No'), 0);
$form->addGroup($group, 'students_can_edit_group', get_lang('StudentsCanEdit'), ' ');
$group = array();
$group[] = $form->createElement('radio', 'approval_direct', null, get_lang('Approval'), 1);
$group[] = $form->createElement('radio', 'approval_direct', null, get_lang('Direct'), 0);
$group = array();
$group[] = $form->createElement('radio', 'allow_attachments', null, get_lang('Yes'), 1);
$group[] = $form->createElement('radio', 'allow_attachments', null, get_lang('No'), 0);
$group = array();
$group[] = $form->createElement('radio', 'allow_new_threads', null, get_lang('Yes'), 1);
$group[] = $form->createElement('radio', 'allow_new_threads', null, get_lang('No'), 0);
$form->addGroup($group, 'allow_new_threads_group', get_lang('AllowNewThreads'), ' ');
$group = array();
$group[] = $form->createElement('radio', 'default_view_type', null, get_lang('Flat'), 'flat');
$group[] = $form->createElement('radio', 'default_view_type', null, get_lang('Threaded'), 'threaded');
$group[] = $form->createElement('radio', 'default_view_type', null, get_lang('Nested'), 'nested');
$form->addGroup($group, 'default_view_type_group', get_lang('DefaultViewType'), ' ');
// Drop down list: Groups
$groups = GroupManager::get_group_list();
$groups_titles[0] = get_lang('NotAGroupForum');
foreach ($groups as $key => $value) {
$groups_titles[$value['id']] = $value['name'];
}
$form->addElement('select', 'group_forum', get_lang('ForGroup'), $groups_titles);
// Public or private group forum
$group = array();
$group[] = $form->createElement('radio', 'public_private_group_forum', null, get_lang('Public'), 'public');
$group[] = $form->createElement('radio', 'public_private_group_forum', null, get_lang('Private'), 'private');
$form->addGroup($group, 'public_private_group_forum_group', get_lang('PublicPrivateGroupForum'), '');
// Forum image
$form->add_progress_bar();
if (isset($inputvalues['forum_image']) && strlen($inputvalues['forum_image']) > 0) {
$image_path = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/upload/forum/images/'.$inputvalues['forum_image'];
$image_size = api_getimagesize($image_path);
$img_attributes = '';
if (!empty($image_size)) {
if ($image_size['width'] > 100 || $image_size['height'] > 100) {
//limit display width and height to 100px
$img_attributes = 'width="100" height="100"';
}
$show_preview_image = '<img src="'.$image_path.'" '.$img_attributes.'>';
$form->addElement('label', get_lang('PreviewImage'), $show_preview_image);
$form->addElement('checkbox', 'remove_picture', null, get_lang('DelImage'));
}
}
$forum_image = isset($inputvalues['forum_image']) ? $inputvalues['forum_image'] : '';
$form->addElement('file', 'picture', ($forum_image != '' ? get_lang('UpdateImage') : get_lang('AddImage')));
$form->addRule('picture', get_lang('OnlyImagesAllowed'), 'filetype', array('jpg', 'jpeg', 'png', 'gif'));
$form->addElement('html', '</div>');
// The OK button
if (isset($_GET['id']) && $_GET['action'] == 'edit') {
$form->addButtonUpdate(get_lang('ModifyForum'), 'SubmitForum');
} else {
$form->addButtonCreate(get_lang('CreateForum'), 'SubmitForum');
}
// setting the rules
$form->addRule('forum_title', get_lang('ThisFieldIsRequired'), 'required');
$form->addRule('forum_category', get_lang('ThisFieldIsRequired'), 'required');
$defaultSettingAllowNewThreads = api_get_default_tool_setting('forum', 'allow_new_threads', 0);
// Settings the defaults
if (empty($inputvalues) || !is_array($inputvalues)) {
$defaults['allow_anonymous_group']['allow_anonymous'] = 0;
$defaults['students_can_edit_group']['students_can_edit'] = 0;
$defaults['approval_direct_group']['approval_direct'] = 0;
$defaults['allow_attachments_group']['allow_attachments'] = 1;
$defaults['allow_new_threads_group']['allow_new_threads'] = $defaultSettingAllowNewThreads;
$defaults['default_view_type_group']['default_view_type'] = api_get_setting('default_forum_view');
$defaults['public_private_group_forum_group']['public_private_group_forum'] = 'public';
if (isset($_GET['forumcategory'])) {
$defaults['forum_category'] = Security::remove_XSS($_GET['forumcategory']);
}
} else { // the default values when editing = the data in the table
$defaults['forum_id'] = isset($inputvalues['forum_id']) ? $inputvalues['forum_id'] : null;
$defaults['forum_title'] = prepare4display(isset($inputvalues['forum_title']) ? $inputvalues['forum_title'] : null);
$defaults['forum_comment'] = prepare4display(isset($inputvalues['forum_comment']) ? $inputvalues['forum_comment'] : null);
$defaults['forum_category'] = isset($inputvalues['forum_category']) ? $inputvalues['forum_category'] : null;
$defaults['allow_anonymous_group']['allow_anonymous'] = isset($inputvalues['allow_anonymous']) ? $inputvalues['allow_anonymous'] : null;
$defaults['students_can_edit_group']['students_can_edit'] = isset($inputvalues['allow_edit']) ? $inputvalues['allow_edit'] : null;
$defaults['approval_direct_group']['approval_direct'] = isset($inputvalues['approval_direct_post']) ? $inputvalues['approval_direct_post'] : null;
$defaults['allow_attachments_group']['allow_attachments'] = isset($inputvalues['allow_attachments']) ? $inputvalues['allow_attachments'] : null;
$defaults['allow_new_threads_group']['allow_new_threads'] = isset($inputvalues['allow_new_threads']) ? $inputvalues['allow_new_threads'] : $defaultSettingAllowNewThreads;
$defaults['default_view_type_group']['default_view_type'] = isset($inputvalues['default_view']) ? $inputvalues['default_view'] : null;
$defaults['public_private_group_forum_group']['public_private_group_forum'] = isset($inputvalues['forum_group_public_private']) ? $inputvalues['forum_group_public_private'] : null;
$defaults['group_forum'] = isset($inputvalues['forum_of_group']) ? $inputvalues['forum_of_group'] : null;
}
$form->setDefaults($defaults);
// Validation or display
if ($form->validate()) {
$check = Security::check_token('post');
if ($check) {
$values = $form->exportValues();
$return_message = store_forum($values);
Display :: display_confirmation_message($return_message);
}
Security::clear_token();
} else {
$token = Security::get_token();
$form->addElement('hidden', 'sec_token');
$form->setConstants(array('sec_token' => $token));
$form->display();
}
}
/**
* This function deletes the forum image if exists
*
* @param int forum id
* @return boolean true if success
* @author Julio Montoya <gugli100@gmail.com>
* @version february 2006, dokeos 1.8
*/
function delete_forum_image($forum_id)
{
$table_forums = Database::get_course_table(TABLE_FORUM);
$course_id = api_get_course_int_id();
$forum_id = intval($forum_id);
$sql = "SELECT forum_image FROM $table_forums
WHERE forum_id = '".$forum_id."' AND c_id = $course_id";
$result = Database::query($sql);
$row = Database::fetch_array($result);
if ($row['forum_image'] != '') {
$del_file = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/forum/images/'.$row['forum_image'];
return @unlink($del_file);
} else {
return false;
}
}
/**
* This function displays the form that is used to edit a forum category.
* This is more or less a copy from the show_add_forumcategory_form function with the only difference that is uses
* some default values. I tried to have both in one function but this gave problems with the handle_forum_and_forumcategories function
* (storing was done twice)
*
* @param array
* @return void HTML
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version february 2006, dokeos 1.8
*/
function show_edit_forumcategory_form($inputvalues = array())
{
$categoryId = $inputvalues['cat_id'];
$form = new FormValidator('forumcategory', 'post', 'index.php?'.api_get_cidreq().'&id='.$categoryId);
// Setting the form elements.
$form->addElement('header', '', get_lang('EditForumCategory'));
$form->addElement('hidden', 'forum_category_id');
$form->addElement('text', 'forum_category_title', get_lang('Title'));
$form->addElement(
'html_editor',
'forum_category_comment',
get_lang('Comment'),
null,
array('ToolbarSet' => 'Forum', 'Width' => '98%', 'Height' => '200')
);
$form->addButtonUpdate(get_lang('ModifyCategory'), 'SubmitEditForumCategory');
// Setting the default values.
$defaultvalues['forum_category_id'] = $inputvalues['cat_id'];
$defaultvalues['forum_category_title'] = $inputvalues['cat_title'];
$defaultvalues['forum_category_comment'] = $inputvalues['cat_comment'];
$form->setDefaults($defaultvalues);
// Setting the rules.
$form->addRule('forum_category_title', get_lang('ThisFieldIsRequired'), 'required');
// Validation or display
if ($form->validate()) {
$check = Security::check_token('post');
if ($check) {
$values = $form->exportValues();
store_forumcategory($values);
}
Security::clear_token();
} else {
$token = Security::get_token();
$form->addElement('hidden', 'sec_token');
$form->setConstants(array('sec_token' => $token));
$form->display();
}
}
/**
* This function stores the forum category in the database.
* The new category is added to the end.
*
* @param array $values
* @param array $courseInfo
* @param bool $showMessage
* @return void HMTL language variable
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version february 2006, dokeos 1.8
*/
function store_forumcategory($values, $courseInfo = array(), $showMessage = true)
{
$courseInfo = empty($courseInfo) ? api_get_course_info() : $courseInfo;
$course_id = $courseInfo['real_id'];
$table_categories = Database::get_course_table(TABLE_FORUM_CATEGORY);
// Find the max cat_order. The new forum category is added at the end => max cat_order + &
$sql = "SELECT MAX(cat_order) as sort_max
FROM $table_categories
WHERE c_id = $course_id";
$result = Database::query($sql);
$row = Database::fetch_array($result);
$new_max = $row['sort_max'] + 1;
$session_id = api_get_session_id();
$clean_cat_title = $values['forum_category_title'];
$last_id = null;
if (isset($values['forum_category_id'])) {
// Storing after edition.
$params = [
'cat_title' => $clean_cat_title,
'cat_comment' => $values['forum_category_comment'],
];
Database::update(
$table_categories,
$params,
[
'c_id = ? AND cat_id = ?' => [
$course_id,
$values['forum_category_id'],
],
]
);
api_item_property_update(
$courseInfo,
TOOL_FORUM_CATEGORY,
$values['forum_category_id'],
'ForumCategoryUpdated',
api_get_user_id()
);
$return_message = get_lang('ForumCategoryEdited');
} else {
$params = [
'c_id' => $course_id,
'cat_title' => $clean_cat_title,
'cat_comment' => $values['forum_category_comment'],
'cat_order' => $new_max,
'session_id' => $session_id,
];
$last_id = Database::insert($table_categories, $params);
if ($last_id > 0) {
$sql = "UPDATE $table_categories SET cat_id = $last_id WHERE iid = $last_id";
Database::query($sql);
api_item_property_update(
$courseInfo,
TOOL_FORUM_CATEGORY,
$last_id,
'ForumCategoryAdded',
api_get_user_id()
);
api_set_default_visibility(
$last_id,
TOOL_FORUM_CATEGORY,
0,
$courseInfo
);
}
$return_message = get_lang('ForumCategoryAdded');
}
if ($showMessage) {
Display:: display_confirmation_message($return_message);
}
return $last_id;
}
/**
* This function stores the forum in the database. The new forum is added to the end.
*
* @param array $values
* @param array $courseInfo
* @param bool $returnId
* @return string language variable
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version february 2006, dokeos 1.8
*/
function store_forum($values, $courseInfo = array(), $returnId = false)
{
$courseInfo = empty($courseInfo) ? api_get_course_info() : $courseInfo;
$course_id = $courseInfo['real_id'];
$session_id = api_get_session_id();
if (isset($values['group_id']) && !empty($values['group_id'])) {
$group_id = $values['group_id'];
} else {
$group_id = api_get_group_id();
}
$table_forums = Database::get_course_table(TABLE_FORUM);
// Find the max forum_order for the given category. The new forum is added at the end => max cat_order + &
if (is_null($values['forum_category'])) {
$new_max = null;
} else {
$sql = "SELECT MAX(forum_order) as sort_max
FROM ".$table_forums."
WHERE
c_id = $course_id AND
forum_category='".Database::escape_string($values['forum_category'])."'";
$result = Database::query($sql);
$row = Database::fetch_array($result);
$new_max = $row['sort_max'] + 1;
}
// Forum images
$image_moved = false;
if (!empty($_FILES['picture']['name'])) {
$upload_ok = process_uploaded_file($_FILES['picture']);
$has_attachment = true;
} else {
$image_moved = true;
}
// Remove existing picture if it was requested.
if (!empty($_POST['remove_picture'])) {
delete_forum_image($values['forum_id']);
}
$new_file_name = '';
if (isset($upload_ok)) {
if ($has_attachment) {
$course_dir = $courseInfo['path'].'/upload/forum/images';
$sys_course_path = api_get_path(SYS_COURSE_PATH);
$updir = $sys_course_path.$course_dir;
// Try to add an extension to the file if it hasn't one.
$new_file_name = add_ext_on_mime(Database::escape_string($_FILES['picture']['name']), $_FILES['picture']['type']);
if (!filter_extension($new_file_name)) {
//Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
$image_moved = false;
} else {
$file_extension = explode('.', $_FILES['picture']['name']);
$file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
$new_file_name = uniqid('').'.'.$file_extension;
$new_path = $updir.'/'.$new_file_name;
$result = @move_uploaded_file($_FILES['picture']['tmp_name'], $new_path);
// Storing the attachments if any
if ($result) {
$image_moved = true;
}
}
}
}
if (isset($values['forum_id'])) {
$sql_image = isset($sql_image) ? $sql_image : '';
$new_file_name = isset($new_file_name) ? $new_file_name : '';
if ($image_moved) {
if (empty($_FILES['picture']['name'])) {
$sql_image = "";
} else {
$sql_image = $new_file_name;
delete_forum_image($values['forum_id']);
}
}
// Storing after edition.
$params = [
'forum_title'=> $values['forum_title'],
'forum_image'=> $sql_image,
'forum_comment'=> isset($values['forum_comment']) ? $values['forum_comment'] : null,
'forum_category'=> isset($values['forum_category']) ? $values['forum_category'] : null,
'allow_anonymous'=> isset($values['allow_anonymous_group']['allow_anonymous']) ? $values['allow_anonymous_group']['allow_anonymous'] : null,
'allow_edit'=> isset($values['students_can_edit_group']['students_can_edit']) ? $values['students_can_edit_group']['students_can_edit'] : null,
'approval_direct_post'=> isset($values['approval_direct_group']['approval_direct']) ? $values['approval_direct_group']['approval_direct'] : null,
'allow_attachments'=> isset($values['allow_attachments_group']['allow_attachments']) ? $values['allow_attachments_group']['allow_attachments'] : null,
'allow_new_threads'=> isset($values['allow_new_threads_group']['allow_new_threads']) ? $values['allow_new_threads_group']['allow_new_threads'] : null,
'default_view'=> isset($values['default_view_type_group']['default_view_type']) ? $values['default_view_type_group']['default_view_type'] : null,
'forum_of_group'=> isset($values['group_forum']) ? $values['group_forum'] : null,
'forum_group_public_private'=> isset($values['public_private_group_forum_group']['public_private_group_forum']) ? $values['public_private_group_forum_group']['public_private_group_forum'] : null,
'forum_order'=> isset($new_max) ? $new_max : null,
'session_id'=> $session_id,
'lp_id' => isset($values['lp_id']) ? intval($values['lp_id']) : 0
];
Database::update(
$table_forums,
$params,
['c_id = ? AND forum_id = ?' => [$course_id, $values['forum_id']]]
);
api_item_property_update(
$courseInfo,
TOOL_FORUM,
Database::escape_string($values['forum_id']),
'ForumUpdated',
api_get_user_id(),
$group_id
);
$return_message = get_lang('ForumEdited');
} else {
if ($image_moved) {
$new_file_name = isset($new_file_name) ? $new_file_name : '';
}
$params = [
'c_id' => $course_id,
'forum_title'=> $values['forum_title'],
'forum_image'=> $new_file_name,
'forum_comment'=> isset($values['forum_comment']) ? $values['forum_comment'] : null,
'forum_category'=> isset($values['forum_category']) ? $values['forum_category'] : null,
'allow_anonymous'=> isset($values['allow_anonymous_group']['allow_anonymous']) ? $values['allow_anonymous_group']['allow_anonymous'] : null,
'allow_edit'=> isset($values['students_can_edit_group']['students_can_edit']) ? $values['students_can_edit_group']['students_can_edit'] : null,
'approval_direct_post'=> isset($values['approval_direct_group']['approval_direct']) ? $values['approval_direct_group']['approval_direct'] : null,
'allow_attachments'=> isset($values['allow_attachments_group']['allow_attachments']) ? $values['allow_attachments_group']['allow_attachments'] : null,
'allow_new_threads'=> isset($values['allow_new_threads_group']['allow_new_threads']) ? $values['allow_new_threads_group']['allow_new_threads'] : null,
'default_view'=> isset($values['default_view_type_group']['default_view_type']) ? $values['default_view_type_group']['default_view_type'] : null,
'forum_of_group'=> isset($values['group_forum']) ? $values['group_forum'] : null,
'forum_group_public_private'=> isset($values['public_private_group_forum_group']['public_private_group_forum']) ? $values['public_private_group_forum_group']['public_private_group_forum'] : null,
'forum_order'=> isset($new_max) ? $new_max : null,
'session_id'=> $session_id,
'lp_id' => isset($values['lp_id']) ? intval($values['lp_id']) : 0
];
$last_id = Database::insert($table_forums, $params);
if ($last_id > 0) {
$sql = "UPDATE $table_forums SET forum_id = iid WHERE iid = $last_id";
Database::query($sql);
api_item_property_update(
$courseInfo,
TOOL_FORUM,
$last_id,
'ForumAdded',
api_get_user_id(),
$group_id
);
api_set_default_visibility(
$last_id,
TOOL_FORUM,
$group_id,
$courseInfo
);
}
$return_message = get_lang('ForumAdded');
if ($returnId) {
return $last_id;
}
}
return $return_message;
}
/**
* This function deletes a forum or a forum category
* This function currently does not delete the forums inside the category,
* nor the threads and replies inside these forums.
* For the moment this is the easiest method and it has the advantage that it
* allows to recover fora that were acidently deleted
* when the forum category got deleted.
*
* @param $content = what we are deleting (a forum or a forum category)
* @param $id The id of the forum category that has to be deleted.
*
* @todo write the code for the cascading deletion of the forums inside a
* forum category and also the threads and replies inside these forums
* @todo config setting for recovery or not
* (see also the documents tool: real delete or not).
* @return string
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version february 2006, dokeos 1.8
*/
function deleteForumCategoryThread($content, $id)
{
$_course = api_get_course_info();
$table_forums = Database::get_course_table(TABLE_FORUM);
$table_forums_post = Database::get_course_table(TABLE_FORUM_POST);
$table_forum_thread = Database::get_course_table(TABLE_FORUM_THREAD);
$course_id = api_get_course_int_id();
$groupId = api_get_group_id();
$userId = api_get_user_id();
$id = intval($id);
// Delete all attachment file about this tread id.
$sql = "SELECT post_id FROM $table_forums_post
WHERE c_id = $course_id AND thread_id = '".$id."' ";
$res = Database::query($sql);
while ($poster_id = Database::fetch_row($res)) {
delete_attachment($poster_id[0]);
}
$tool_constant = null;
$return_message = '';
if ($content == 'forumcategory') {
$tool_constant = TOOL_FORUM_CATEGORY;
$return_message = get_lang('ForumCategoryDeleted');
if (!empty($forum_list)) {
$sql = "SELECT forum_id FROM ".$table_forums."
WHERE c_id = $course_id AND forum_category='".$id."'";
$result = Database::query($sql);
$row = Database::fetch_array($result);
foreach ($row as $arr_forum) {
$forum_id = $arr_forum['forum_id'];
api_item_property_update(
$_course,
'forum',
$forum_id,
'delete',
api_get_user_id()
);
}
}
}
if ($content == 'forum') {
$tool_constant = TOOL_FORUM;
$return_message = get_lang('ForumDeleted');
if (!empty($number_threads)) {
$sql = "SELECT thread_id FROM".$table_forum_thread."
WHERE c_id = $course_id AND forum_id='".$id."'";
$result = Database::query($sql);
$row = Database::fetch_array($result);
foreach ($row as $arr_forum) {
$forum_id = $arr_forum['thread_id'];
api_item_property_update($_course, 'forum_thread', $forum_id, 'delete', api_get_user_id());
}
}
}
if ($content == 'thread') {
$tool_constant = TOOL_FORUM_THREAD;
$return_message = get_lang('ThreadDeleted');
}
api_item_property_update(
$_course,
$tool_constant,
$id,
'delete',
$userId,
$groupId
);
// Check if this returns a true and if so => return $return_message, if not => return false;
return $return_message;
}
/**
* This function deletes a forum post. This separate function is needed because forum posts do not appear in the item_property table (yet)
* and because deleting a post also has consequence on the posts that have this post as parent_id (they are also deleted).
* an alternative would be to store the posts also in item_property and mark this post as deleted (visibility = 2).
* We also have to decrease the number of replies in the thread table
*
* @param $post_id the id of the post that will be deleted
* @todo write recursive function that deletes all the posts that have this message as parent
* @return string language variable
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @author Hubert Borderiou Function cleanead and fixed
* @version february 2006
*/
function delete_post($post_id)
{
$table_posts = Database :: get_course_table(TABLE_FORUM_POST);
$table_threads = Database :: get_course_table(TABLE_FORUM_THREAD);
$post_id = intval($post_id);
$course_id = api_get_course_int_id();
// Get parent_post_id of deleted post.
$tab_post_info = get_post_information($post_id);
if ($tab_post_info) {
$post_parent_id_of_deleted_post = intval($tab_post_info['post_parent_id']);
$thread_id_of_deleted_post = $tab_post_info['thread_id'];
$forum_if_of_deleted_post = $tab_post_info['forum_id'];
$sql = "UPDATE $table_posts
SET post_parent_id=$post_parent_id_of_deleted_post
WHERE
c_id = $course_id AND
post_parent_id=$post_id AND
thread_id=$thread_id_of_deleted_post AND
forum_id=$forum_if_of_deleted_post";
Database::query($sql);
// Note: This has to be a recursive function that deletes all of the posts in this block.
$sql = "DELETE FROM $table_posts
WHERE c_id = $course_id AND post_id = ".intval($post_id)."";
Database::query($sql);
// Delete attachment file about this post id.
delete_attachment($post_id);
}
$last_post_of_thread = check_if_last_post_of_thread($_GET['thread']);
if (is_array($last_post_of_thread)) {
// Decreasing the number of replies for this thread and also changing the last post information.
$sql = "UPDATE $table_threads SET thread_replies=thread_replies-1,
thread_last_post = ".intval($last_post_of_thread['post_id']).",
thread_date='".Database::escape_string($last_post_of_thread['post_date'])."'
WHERE c_id = $course_id AND thread_id = ".intval($_GET['thread'])."";
Database::query($sql);
return 'PostDeleted';
}
if (!$last_post_of_thread) {
// We deleted the very single post of the thread so we need to delete the entry in the thread table also.
$sql = "DELETE FROM $table_threads
WHERE c_id = $course_id AND thread_id = ".intval($_GET['thread'])."";
Database::query($sql);
return 'PostDeletedSpecial';
}
}
/**
* This function gets the all information of the last (=most recent) post of the thread
* This can be done by sorting the posts that have the field thread_id=$thread_id and sort them by post_date
*
* @param $thread_id the id of the thread we want to know the last post of.
* @return an array or bool if there is a last post found, false if there is
* no post entry linked to that thread => thread will be deleted
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version february 2006, dokeos 1.8
*/
function check_if_last_post_of_thread($thread_id)
{
$table_posts = Database :: get_course_table(TABLE_FORUM_POST);
$course_id = api_get_course_int_id();
$sql = "SELECT * FROM $table_posts
WHERE c_id = $course_id AND thread_id = ".intval($thread_id)."
ORDER BY post_date DESC";
$result = Database::query($sql);
if (Database::num_rows($result) > 0) {
$row = Database::fetch_array($result);
return $row;
} else {
return false;
}
}
/**
* @param $content what is it that we want to make (in)visible: forum category, forum, thread, post
* @param $id the id of the content we want to make invisible
* @param $current_visibility_status what is the current status of the visibility (0 = invisible, 1 = visible)
* @return string HTML
*/
function return_visible_invisible_icon($content, $id, $current_visibility_status, $additional_url_parameters = '')
{
$html = '';
$id = Security::remove_XSS($id);
if ($current_visibility_status == '1') {
$html .= '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&';
if (is_array($additional_url_parameters)) {
foreach ($additional_url_parameters as $key => $value) {
$html .= $key . '=' . $value . '&';
}
}
$html.='action=invisible&content='.$content.'&id='.$id.'">'.
Display::return_icon('visible.png', get_lang('MakeInvisible'), array(), ICON_SIZE_SMALL).'</a>';
}
if ($current_visibility_status == '0') {
$html .= '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&';
if (is_array($additional_url_parameters)) {
foreach ($additional_url_parameters as $key => $value) {
$html .= $key . '=' . $value . '&';
}
}
$html .= 'action=visible&content=' . $content . '&id=' . $id . '">' .
Display::return_icon('invisible.png', get_lang('MakeVisible'), array(), ICON_SIZE_SMALL) . '</a>';
}
return $html;
}
function return_lock_unlock_icon($content, $id, $current_lock_status, $additional_url_parameters = '')
{
$html = '';
$id = intval($id);
//check if the forum is blocked due
if ($content == 'thread') {
if (api_resource_is_locked_by_gradebook($id, LINK_FORUM_THREAD)) {
$html .= Display::return_icon('lock_na.png', get_lang('ResourceLockedByGradebook'), array(), ICON_SIZE_SMALL);
return $html;
}