Issues (2160)

main/work/edit.php (2 issues)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
require_once __DIR__.'/../inc/global.inc.php';
6
$current_course_tool = TOOL_STUDENTPUBLICATION;
7
8
api_protect_course_script(true);
9
10
$blockEdition = api_get_configuration_value('block_student_publication_edition');
11
12
if ($blockEdition && !api_is_platform_admin()) {
13
    api_not_allowed(true);
14
}
15
16
require_once 'work.lib.php';
17
18
$this_section = SECTION_COURSES;
19
20
$work_id = isset($_REQUEST['id']) ? (int) ($_REQUEST['id']) : null;
21
$item_id = isset($_REQUEST['item_id']) ? (int) ($_REQUEST['item_id']) : null;
22
$work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
23
24
$course_id = api_get_course_int_id();
25
$user_id = api_get_user_id();
26
$session_id = api_get_session_id();
27
$courseInfo = api_get_course_info();
28
29
if (empty($work_id) || empty($item_id)) {
30
    api_not_allowed(true);
31
}
32
33
$parent_data = $my_folder_data = get_work_data_by_id($work_id);
34
35
if (empty($parent_data)) {
36
    api_not_allowed(true);
37
}
38
39
$is_course_member = CourseManager::is_user_subscribed_in_real_or_linked_course(
0 ignored issues
show
Deprecated Code introduced by
The function CourseManager::is_user_s...real_or_linked_course() has been deprecated: linked_courses definition doesn't exists ( Ignorable by Annotation )

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

39
$is_course_member = /** @scrutinizer ignore-deprecated */ CourseManager::is_user_subscribed_in_real_or_linked_course(

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...
40
    $user_id,
41
    $course_id,
42
    $session_id
43
);
44
45
$is_course_member = $is_course_member || api_is_platform_admin();
46
47
$allowBaseCourseTeacher = api_get_configuration_value('assignment_base_course_teacher_access_to_all_session');
48
$isCourseTeacher = false;
49
$redirectToSelf = false;
50
if (false === $is_course_member && $allowBaseCourseTeacher) {
51
    // Check if user is base course teacher.
52
    if (CourseManager::is_course_teacher(api_get_user_id(), $courseInfo['code'])) {
53
        $is_course_member = true;
54
        $isCourseTeacher = true;
55
        $redirectToSelf = true;
56
    }
57
}
58
if (false == $is_course_member) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
59
    api_not_allowed(true);
60
}
61
62
$is_allowed_to_edit = api_is_allowed_to_edit() || $isCourseTeacher;
63
$student_can_edit_in_session = api_is_allowed_to_session_edit(false, true) || $isCourseTeacher;
64
65
$check = Security::check_token('post');
66
$token = Security::get_token();
67
68
$has_ended = false;
69
$work_item = get_work_data_by_id($item_id);
70
71
// Get the author ID for that document from the item_property table
72
$is_author = user_is_author($item_id) || $isCourseTeacher;
73
74
if (!$is_author) {
75
    api_not_allowed(true);
76
}
77
78
// Student's can't edit work only if he can delete his docs.
79
if (!api_is_allowed_to_edit() && false === $isCourseTeacher) {
80
    if (api_get_course_setting('student_delete_own_publication') != 1) {
81
        api_not_allowed(true);
82
    }
83
}
84
85
if (!empty($my_folder_data)) {
86
    $homework = get_work_assignment_by_id($my_folder_data['id']);
87
88
    if (!empty($homework['expires_on']) || !empty($homework['ends_on'])) {
89
        $time_now = time();
90
91
        if (!empty($homework['expires_on'])) {
92
            $time_expires = api_strtotime($homework['expires_on'], 'UTC');
93
            $difference = $time_expires - $time_now;
94
            if ($difference < 0) {
95
                $has_expired = true;
96
            }
97
        }
98
99
        if (empty($homework['expires_on'])) {
100
            $has_expired = false;
101
        }
102
103
        if (!empty($homework['ends_on'])) {
104
            $time_ends = api_strtotime($homework['ends_on'], 'UTC');
105
            $difference2 = $time_ends - $time_now;
106
            if ($difference2 < 0) {
107
                $has_ended = true;
108
            }
109
        }
110
111
        $ends_on = api_convert_and_format_date($homework['ends_on']);
112
        $expires_on = api_convert_and_format_date($homework['expires_on']);
113
    }
114
}
115
116
$interbreadcrumb[] = [
117
    'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
118
    'name' => get_lang('StudentPublications'),
119
];
120
121
if (api_is_allowed_to_edit()) {
122
    $interbreadcrumb[] = [
123
        'url' => api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$work_id,
124
        'name' => $parent_data['title'],
125
    ];
126
} else {
127
    $interbreadcrumb[] = [
128
        'url' => api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$work_id,
129
        'name' => $parent_data['title'],
130
    ];
131
}
132
133
$form_title = get_lang('Edit');
134
$interbreadcrumb[] = ['url' => '#', 'name' => $form_title];
135
136
$form = new FormValidator(
137
    'form',
138
    'POST',
139
    api_get_self().'?'.api_get_cidreq().'&id='.$work_id,
140
    '',
141
    ['enctype' => 'multipart/form-data']
142
);
143
$form->addElement('header', $form_title);
144
$show_progress_bar = false;
145
$form->addElement('hidden', 'id', $work_id);
146
$form->addElement('hidden', 'item_id', $item_id);
147
$form->addText('title', get_lang('Title'), true, ['id' => 'file_upload']);
148
if ($is_allowed_to_edit && !empty($item_id)) {
149
    $sql = "SELECT contains_file, url
150
            FROM $work_table
151
            WHERE c_id = $course_id AND id ='$item_id' ";
152
    $result = Database::query($sql);
153
    if ($result !== false && Database::num_rows($result) > 0) {
154
        $row = Database::fetch_array($result);
155
        if ($row['contains_file'] || !empty($row['url'])) {
156
            $form->addLabel(
157
                get_lang('Download'),
158
                '<a href="'.api_get_path(WEB_CODE_PATH).'work/download.php?id='.$item_id.'&'.api_get_cidreq().'">'.
159
                    Display::return_icon('save.png', get_lang('Save'), [], ICON_SIZE_MEDIUM).'
160
                </a>'
161
            );
162
        }
163
    }
164
}
165
$form->addHtmlEditor(
166
    'description',
167
    get_lang('Description'),
168
    false,
169
    false,
170
    getWorkDescriptionToolbar()
171
);
172
173
$defaults['title'] = $work_item['title'];
174
$defaults["description"] = $work_item['description'];
175
$defaults['qualification'] = $work_item['qualification'];
176
177
if ($is_allowed_to_edit && !empty($item_id)) {
178
    // Get qualification from parent_id that will allow the validation qualification over
179
    /*$sql = "SELECT qualification FROM $work_table
180
            WHERE c_id = $course_id AND id ='$work_id' ";
181
    $result = Database::query($sql);
182
    $row = Database::fetch_array($result);
183
    $qualification_over = $row['qualification'];
184
    if (!empty($qualification_over) && intval($qualification_over) > 0) {
185
        $form->addText('qualification', array(get_lang('Qualification'), " / ".$qualification_over), false, 'size="10"');
186
        $form->addElement('hidden', 'qualification_over', $qualification_over);
187
    }*/
188
189
    $form->addCheckBox(
190
        'send_email',
191
        null,
192
        get_lang('SendMailToStudent')
193
    );
194
195
    // Check if user to qualify has some DRHs
196
    $drhList = UserManager::getDrhListFromUser($work_item['user_id']);
197
    if (!empty($drhList)) {
198
        $form->addCheckBox(
199
            'send_to_drh_users',
200
            null,
201
            get_lang('SendMailToHR')
202
        );
203
    }
204
}
205
206
$form->addElement('hidden', 'active', 1);
207
$form->addElement('hidden', 'accepted', 1);
208
$form->addElement('hidden', 'item_to_edit', $item_id);
209
$form->addElement('hidden', 'sec_token', $token);
210
211
$text = get_lang('UpdateWork');
212
$class = 'save';
213
214
// fix the Ok button when we see the tool in the learn path
215
$form->addButtonUpdate($text);
216
217
$form->setDefaults($defaults);
218
$_course = api_get_course_info();
219
$currentCourseRepositorySys = api_get_path(SYS_COURSE_PATH).$_course['path'].'/';
220
221
$succeed = false;
222
if ($form->validate()) {
223
    if ($student_can_edit_in_session && $check) {
224
        /*
225
         * SPECIAL CASE ! For a work edited
226
        */
227
        //Get the author ID for that document from the item_property table
228
        $item_to_edit_id = (int) ($_POST['item_to_edit']);
229
        $is_author = user_is_author($item_to_edit_id) || $isCourseTeacher;
230
231
        if ($is_author) {
232
            $work_data = get_work_data_by_id($item_to_edit_id);
233
            if (!empty($_POST['title'])) {
234
                $title = isset($_POST['title']) ? $_POST['title'] : $work_data['title'];
235
            }
236
            $description = isset($_POST['description']) ? $_POST['description'] : $work_data['description'];
237
238
            $add_to_update = null;
239
            if ($is_allowed_to_edit && ($_POST['qualification'] != '')) {
240
                if (isset($_POST['send_email'])) {
241
                    $url = api_get_path(WEB_CODE_PATH).'work/view.php?'.api_get_cidreq().'&id='.$item_to_edit_id;
242
                    $subject = sprintf(get_lang('ThereIsANewWorkFeedback'), $work_item['title']);
243
                    $message = sprintf(get_lang('ThereIsANewWorkFeedbackInWorkXHere'), $work_item['title'], $url);
244
245
                    MessageManager::send_message_simple(
246
                        $work_item['user_id'],
247
                        $subject,
248
                        $message,
249
                        api_get_user_id(),
250
                        isset($_POST['send_to_drh_users'])
251
                    );
252
                }
253
            }
254
255
            if ($_POST['qualification'] > $_POST['qualification_over']) {
256
                Display::addFlash(Display::return_message(
257
                    get_lang('QualificationMustNotBeMoreThanQualificationOver'),
258
                    'error'
259
                ));
260
            } else {
261
                $sql = "UPDATE  ".$work_table."
262
                        SET	title = '".Database::escape_string($title)."',
263
                            description = '".Database::escape_string($description)."'
264
                            ".$add_to_update."
265
                        WHERE c_id = $course_id AND id = $item_to_edit_id";
266
                Database::query($sql);
267
            }
268
269
            api_item_property_update(
270
                $_course,
271
                'work',
272
                $item_to_edit_id,
273
                'DocumentUpdated',
274
                $user_id
275
            );
276
277
            $succeed = true;
278
            Display::addFlash(Display::return_message(get_lang('ItemUpdated')));
279
        }
280
        Security::clear_token();
281
    } else {
282
        // Bad token or can't add works
283
        Display::addFlash(Display::return_message(get_lang('ImpossibleToSaveTheDocument'), 'error'));
284
    }
285
286
    $script = 'work_list.php';
287
    if ($is_allowed_to_edit) {
288
        $script = 'work_list_all.php';
289
    }
290
    if ($redirectToSelf) {
291
        api_location(
292
            api_get_path(WEB_CODE_PATH).'work/edit.php?'.api_get_cidreq().'&id='.$work_id.'&item_id='.$item_id
293
        );
294
    }
295
    api_location(api_get_path(WEB_CODE_PATH).'work/'.$script.'?'.api_get_cidreq().'&id='.$work_id);
296
}
297
298
$htmlHeadXtra[] = to_javascript_work();
299
300
$tpl = new Template();
301
$content = null;
302
if (!empty($work_id)) {
303
    if ($is_allowed_to_edit) {
304
        if (api_resource_is_locked_by_gradebook($work_id, LINK_STUDENTPUBLICATION)) {
305
            echo Display::return_message(get_lang('ResourceLockedByGradebook'), 'warning');
306
        } else {
307
            $content .= $form->returnForm();
308
        }
309
    } elseif ($is_author) {
310
        if (empty($work_item['qualificator_id']) || $work_item['qualificator_id'] == 0) {
311
            $content .= $form->returnForm();
312
        } else {
313
            $content .= Display::return_message(get_lang('ActionNotAllowed'), 'error');
314
        }
315
    } elseif ($student_can_edit_in_session && $has_ended == false) {
316
        $content .= $form->returnForm();
317
    } else {
318
        $content .= Display::return_message(get_lang('ActionNotAllowed'), 'error');
319
    }
320
} else {
321
    $content .= Display::return_message(get_lang('ActionNotAllowed'), 'error');
322
}
323
324
$tpl->assign('content', $content);
325
$tpl->display_one_col_template();
326