Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/forum/editthread.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Edit a Forum Thread.
6
 *
7
 * @Author José Loguercio <[email protected]>
8
 *
9
 * @package chamilo.forum
10
 */
11
require_once __DIR__.'/../inc/global.inc.php';
12
13
// The section (tabs).
14
$this_section = SECTION_COURSES;
15
// Notification for unauthorized people.
16
api_protect_course_script(true);
17
18
$cidreq = api_get_cidreq();
19
$nameTools = get_lang('ToolForum');
20
$_user = api_get_user_info();
21
22
require_once 'forumfunction.inc.php';
23
24
// Are we in a lp ?
25
$origin = api_get_origin();
26
27
/* MAIN DISPLAY SECTION */
28
$forumId = (int) $_GET['forum'];
29
$currentForum = get_forum_information($forumId);
0 ignored issues
show
Deprecated Code introduced by
The function get_forum_information() has been deprecated: this functionality is now moved to get_forums($forum_id) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

29
$currentForum = /** @scrutinizer ignore-deprecated */ get_forum_information($forumId);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
30
$currentForumCategory = get_forumcategory_information($currentForum['forum_category']);
31
32
if (api_is_in_gradebook()) {
33
    $interbreadcrumb[] = [
34
        'url' => Category::getUrl(),
35
        'name' => get_lang('ToolGradebook'),
36
    ];
37
}
38
39
$threadId = isset($_GET['thread']) ? intval($_GET['thread']) : 0;
40
$courseInfo = isset($_GET['cidReq']) ? api_get_course_info($_GET['cidReq']) : 0;
41
$cId = isset($courseInfo['real_id']) ? intval($courseInfo['real_id']) : 0;
42
$gradebookId = intval(api_is_in_gradebook());
43
44
/* Is the user allowed here? */
45
46
// The user is not allowed here if:
47
48
// 1. the forumcategory or forum is invisible (visibility==0) and the user is not a course manager
49
if (!api_is_allowed_to_edit(false, true) &&
50
    (($currentForumCategory['visibility'] && $currentForumCategory['visibility'] == 0) || $currentForum['visibility'] == 0)
51
) {
52
    api_not_allowed();
53
}
54
55
// 2. the forumcategory or forum is locked (locked <>0) and the user is not a course manager
56
if (!api_is_allowed_to_edit(false, true) &&
57
    (($currentForumCategory['visibility'] && $currentForumCategory['locked'] != 0) || $currentForum['locked'] != 0)
58
) {
59
    api_not_allowed();
60
}
61
62
// 3. new threads are not allowed and the user is not a course manager
63
if (!api_is_allowed_to_edit(false, true) &&
64
    $currentForum['allow_new_threads'] != 1
65
) {
66
    api_not_allowed();
67
}
68
// 4. anonymous posts are not allowed and the user is not logged in
69
if (!$_user['user_id'] && $currentForum['allow_anonymous'] != 1) {
70
    api_not_allowed();
71
}
72
73
// 5. Check user access
74
if ($currentForum['forum_of_group'] != 0) {
75
    $show_forum = GroupManager::user_has_access(
76
        api_get_user_id(),
77
        $currentForum['forum_of_group'],
78
        GroupManager::GROUP_TOOL_FORUM
79
    );
80
    if (!$show_forum) {
81
        api_not_allowed();
82
    }
83
}
84
85
// 6. Invited users can't create new threads
86
if (api_is_invitee()) {
87
    api_not_allowed(true);
88
}
89
90
$groupId = api_get_group_id();
91
if (!empty($groupId)) {
92
    $groupProperties = GroupManager:: get_group_properties($groupId);
93
    $interbreadcrumb[] = [
94
        'url' => api_get_path(WEB_CODE_PATH).'group/group.php?'.$cidreq,
95
        'name' => get_lang('Groups'),
96
    ];
97
    $interbreadcrumb[] = [
98
        'url' => api_get_path(WEB_CODE_PATH).'group/group_space.php?'.$cidreq,
99
        'name' => get_lang('GroupSpace').' '.$groupProperties['name'],
100
    ];
101
    $interbreadcrumb[] = [
102
        'url' => api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.$cidreq.'&forum='.$forumId,
103
        'name' => $currentForum['forum_title'],
104
    ];
105
    $interbreadcrumb[] = [
106
        'url' => api_get_path(WEB_CODE_PATH).'forum/newthread.php?'.$cidreq.'&forum='.$forumId,
107
        'name' => get_lang('EditThread'),
108
    ];
109
} else {
110
    $interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH).'forum/index.php?'.$cidreq, 'name' => $nameTools];
111
    $interbreadcrumb[] = [
112
        'url' => api_get_path(WEB_CODE_PATH).'forum/viewforumcategory.php?'.$cidreq.'&forumcategory='.$currentForumCategory['cat_id'],
113
        'name' => $currentForumCategory['cat_title'],
114
    ];
115
    $interbreadcrumb[] = [
116
        'url' => api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.$cidreq.'&forum='.$forumId,
117
        'name' => $currentForum['forum_title'],
118
    ];
119
    $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('EditThread')];
120
}
121
122
$tableLink = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
123
124
/* Header */
125
$htmlHeadXtra[] = <<<JS
126
    <script>
127
    $(function() {
128
        $('[name="thread_qualify_gradebook"]:checkbox').change(function () {
129
            if (this.checked) {
130
                $('#options_field').show();
131
            } else {
132
                $('#options_field').hide();
133
                $("[name='numeric_calification']").val(0);
134
                $("[name='calification_notebook_title']").val('');
135
                $("[name='weight_calification']").val(0);
136
                $("[name='thread_peer_qualify'][value='0']").prop('checked', true);
137
            }
138
        });
139
    });
140
    </script>
141
JS;
142
143
// Action links
144
$actions = [
145
    Display::url(
146
        Display::return_icon('back.png', get_lang('BackToForum'), '', ICON_SIZE_MEDIUM),
147
        'viewforum.php?forum='.$forumId.'&'.$cidreq
148
    ),
149
    search_link(),
150
];
151
152
$threadData = getThreadInfo($threadId, $cId);
153
154
$gradeThisThread = empty($_POST) && ($threadData['threadQualifyMax'] > 0 || $threadData['threadWeight'] > 0);
155
156
$form = new FormValidator(
157
    'thread',
158
    'post',
159
    api_get_self().'?'.http_build_query([
160
        'forum' => $forumId,
161
        'thread' => $threadId,
162
    ]).'&'.api_get_cidreq()
163
);
164
165
$form->addElement('header', get_lang('EditThread'));
166
$form->setConstants(['forum' => '5']);
167
$form->addElement('hidden', 'forum_id', $forumId);
168
$form->addElement('hidden', 'thread_id', $threadId);
169
$form->addElement('hidden', 'gradebook', $gradebookId);
170
$form->addElement('text', 'thread_title', get_lang('Title'));
171
$form->addElement('advanced_settings', 'advanced_params', get_lang('AdvancedParameters'));
172
$form->addElement('html', '<div id="advanced_params_options" style="display:none">');
173
174
if ((api_is_course_admin() || api_is_session_general_coach() || api_is_course_tutor()) && $threadId) {
175
    // Thread qualify
176
    if (Gradebook::is_active()) {
177
        //Loading gradebook select
178
        GradebookUtils::load_gradebook_select_in_tool($form);
179
        $form->addElement(
180
            'checkbox',
181
            'thread_qualify_gradebook',
182
            '',
183
            get_lang('QualifyThreadGradebook')
184
        );
185
    } else {
186
        $form->addElement('hidden', 'thread_qualify_gradebook', false);
187
    }
188
189
    $form->addElement('html', '<div id="options_field" style="'.($gradeThisThread ? '' : 'display:none;').'">');
190
    $form->addElement('text', 'numeric_calification', get_lang('QualificationNumeric'));
191
    $form->applyFilter('numeric_calification', 'html_filter');
192
    $form->addElement('text', 'calification_notebook_title', get_lang('TitleColumnGradebook'));
193
    $form->applyFilter('calification_notebook_title', 'html_filter');
194
    $form->addElement(
195
        'number',
196
        'weight_calification',
197
        get_lang('QualifyWeight'),
198
        ['value' => '0.00', 'step' => '0.01']
199
    );
200
    $form->applyFilter('weight_calification', 'html_filter');
201
    $group = [];
202
    $group[] = $form->createElement('radio', 'thread_peer_qualify', null, get_lang('Yes'), 1);
203
    $group[] = $form->createElement('radio', 'thread_peer_qualify', null, get_lang('No'), 0);
204
    $form->addGroup(
205
        $group,
206
        '',
207
        [get_lang('ForumThreadPeerScoring'), get_lang('ForumThreadPeerScoringComment')]
208
    );
209
    $form->addElement('html', '</div>');
210
}
211
212
if (api_is_allowed_to_edit(null, true)) {
213
    $form->addElement('checkbox', 'thread_sticky', '', get_lang('StickyPost'));
214
}
215
216
$form->addElement('html', '</div>');
217
218
$skillList = Skill::addSkillsToForm($form, ITEM_TYPE_FORUM_THREAD, $threadId);
219
220
if (!empty($threadData)) {
221
    $defaults['thread_qualify_gradebook'] = $gradeThisThread;
222
    $defaults['thread_title'] = prepare4display($threadData['threadTitle']);
223
    $defaults['thread_sticky'] = strval(intval($threadData['threadSticky']));
224
    $defaults['thread_peer_qualify'] = intval($threadData['threadPeerQualify']);
225
    $defaults['numeric_calification'] = $threadData['threadQualifyMax'];
226
    $defaults['calification_notebook_title'] = $threadData['threadTitleQualify'];
227
    $defaults['weight_calification'] = $threadData['threadWeight'];
228
} else {
229
    $defaults['thread_qualify_gradebook'] = 0;
230
    $defaults['numeric_calification'] = 0;
231
    $defaults['calification_notebook_title'] = '';
232
    $defaults['weight_calification'] = 0;
233
    $defaults['thread_peer_qualify'] = 0;
234
}
235
236
$defaults['skills'] = array_keys($skillList);
237
238
$form->addButtonUpdate(get_lang('ModifyThread'), 'SubmitPost');
239
240
if ($form->validate()) {
241
    $redirectUrl = api_get_path(WEB_CODE_PATH).'forum/viewforum.php?forum='.$forumId.'&'.api_get_cidreq();
242
    $check = Security::check_token('post');
243
    if ($check) {
244
        $values = $form->exportValues();
245
        Security::clear_token();
246
        updateThread($values);
247
        Skill::saveSkills($form, ITEM_TYPE_FORUM_THREAD, $threadId);
248
        header('Location: '.$redirectUrl);
249
        exit;
250
    }
251
}
252
253
$form->setDefaults(isset($defaults) ? $defaults : null);
254
$token = Security::get_token();
255
$form->addElement('hidden', 'sec_token');
256
$form->setConstants(['sec_token' => $token]);
257
$originIsLearnPath = $origin == 'learnpath';
258
259
$view = new Template(
260
    '',
261
    !$originIsLearnPath,
262
    !$originIsLearnPath,
263
    $originIsLearnPath,
264
    $originIsLearnPath
265
);
266
$view->assign(
267
    'actions',
268
    Display::toolbarAction('toolbar', $actions)
269
);
270
$view->assign('content', $form->returnForm());
271
$view->display_one_col_template();
272