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

main/work/edit_work.php (1 issue)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
require_once __DIR__.'/../inc/global.inc.php';
6
7
api_protect_course_script(true);
8
9
$lib_path = api_get_path(LIBRARY_PATH);
10
11
/* Libraries */
12
require_once 'work.lib.php';
13
14
// Section (for the tabs)
15
$this_section = SECTION_COURSES;
16
17
if (!api_is_allowed_to_edit()) {
18
    api_not_allowed(true);
19
}
20
21
$blockEdition = api_get_configuration_value('block_student_publication_edition');
22
23
if ($blockEdition && !api_is_platform_admin()) {
24
    api_not_allowed(true);
25
}
26
27
$courseInfo = api_get_course_info();
28
$sessionId = api_get_session_id();
29
$groupId = api_get_group_id();
30
$workId = isset($_GET['id']) ? (int) ($_GET['id']) : null;
31
$workData = get_work_data_by_id($workId);
32
$homework = get_work_assignment_by_id($workId);
33
$locked = api_resource_is_locked_by_gradebook($workId, LINK_STUDENTPUBLICATION);
34
35
if (false == api_is_platform_admin() && true == $locked) {
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...
36
    api_not_allowed(true);
37
}
38
39
$htmlHeadXtra[] = to_javascript_work();
40
$interbreadcrumb[] = [
41
    'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
42
    'name' => get_lang('StudentPublications'),
43
];
44
$interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Edit')];
45
46
$form = new FormValidator(
47
    'edit_dir',
48
    'post',
49
    api_get_path(WEB_CODE_PATH).'work/edit_work.php?id='.$workId.'&'.api_get_cidreq()
50
);
51
$form->addElement('header', get_lang('Edit'));
52
53
$title = !empty($workData['title']) ? $workData['title'] : basename($workData['url']);
54
55
$defaults = $workData;
56
$defaults['new_dir'] = Security::remove_XSS($title);
57
58
$there_is_a_end_date = false;
59
60
if (Gradebook::is_active()) {
61
    $link_info = GradebookUtils::isResourceInCourseGradebook(
62
        api_get_course_id(),
63
        LINK_STUDENTPUBLICATION,
64
        $workId
65
    );
66
    if (!empty($link_info)) {
67
        $defaults['weight'] = $link_info['weight'];
68
        $defaults['category_id'] = $link_info['category_id'];
69
        $defaults['make_calification'] = 1;
70
    }
71
} else {
72
    $defaults['category_id'] = '';
73
}
74
if (!empty($homework['expires_on'])) {
75
    $homework['expires_on'] = api_get_local_time($homework['expires_on']);
76
    $defaults['enableExpiryDate'] = true;
77
    $defaults['expires_on'] = $homework['expires_on'];
78
} else {
79
    $homework['expires_on'] = null;
80
}
81
82
if (!empty($homework['ends_on'])) {
83
    $homework['ends_on'] = api_get_local_time($homework['ends_on']);
84
    $defaults['ends_on'] = $homework['ends_on'];
85
    $defaults['enableEndDate'] = true;
86
} else {
87
    $homework['ends_on'] = null;
88
    $defaults['enableEndDate'] = false;
89
    $defaults['ends_on'] = null;
90
}
91
92
$defaults['add_to_calendar'] = isset($homework['add_to_calendar']) ? $homework['add_to_calendar'] : null;
93
$form = getFormWork($form, $defaults, $workId);
94
$form->addElement('hidden', 'work_id', $workId);
95
$form->addButtonUpdate(get_lang('ModifyDirectory'));
96
97
$currentUrl = api_get_path(WEB_CODE_PATH).'work/edit_work.php?id='.$workId.'&'.api_get_cidreq();
98
if ($form->validate()) {
99
    $params = $form->getSubmitValues();
100
    $params['enableEndDate'] = isset($params['enableEndDate']) ? true : false;
101
    $params['enableExpiryDate'] = isset($params['enableExpiryDate']) ? true : false;
102
103
    if ($params['enableExpiryDate'] &&
104
        $params['enableEndDate']
105
    ) {
106
        if ($params['expires_on'] > $params['ends_on']) {
107
            Display::addFlash(
108
                Display::return_message(
109
                    get_lang('DateExpiredNotBeLessDeadLine'),
110
                    'warning'
111
                )
112
            );
113
            header('Location: '.$currentUrl);
114
            exit;
115
        }
116
    }
117
118
    $workId = $params['work_id'];
119
    $editCheck = false;
120
    $workData = get_work_data_by_id($workId);
121
122
    if (!empty($workData)) {
123
        $editCheck = true;
124
    } else {
125
        $editCheck = true;
126
    }
127
128
    if ($editCheck) {
129
        updateWork($workData['iid'], $params, $courseInfo, $sessionId);
130
        updatePublicationAssignment($workId, $params, $courseInfo, $groupId);
131
        updateDirName($workData, $params['new_dir']);
132
        Skill::saveSkills($form, ITEM_TYPE_STUDENT_PUBLICATION, $workData['iid']);
133
        Display::addFlash(Display::return_message(get_lang('Updated'), 'success'));
134
        header('Location: '.$currentUrl);
135
        exit;
136
    } else {
137
        Display::addFlash(Display::return_message(get_lang('FileExists'), 'warning'));
138
    }
139
}
140
141
Display::display_header();
142
143
$form->display();
144
145
Display :: display_footer();
146