Issues (2037)

main/admin/extra_field_workflow.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Entity\ExtraFieldOptionRelFieldOption;
5
6
$cidReset = true;
7
require_once __DIR__.'/../inc/global.inc.php';
8
9
$this_section = SECTION_PLATFORM_ADMIN;
10
$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
11
12
api_protect_admin_script();
13
14
// setting breadcrumbs
15
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
16
17
$tool_name = null;
18
19
$action = isset($_GET['action']) ? $_GET['action'] : null;
20
$field_id = isset($_GET['field_id']) ? (int) $_GET['field_id'] : null;
21
22
if (empty($field_id)) {
23
    api_not_allowed();
24
}
25
if (!in_array($type, ExtraField::getValidExtraFieldTypes())) {
26
    api_not_allowed();
27
}
28
29
$extraField = new ExtraField($type);
30
$extraFieldInfo = $extraField->get($field_id);
31
32
$check = Security::check_token('request');
33
$token = Security::get_token();
34
35
if ($action == 'add') {
36
    $interbreadcrumb[] = ['url' => 'extra_fields.php?type='.$extraField->type, 'name' => $extraField->pageName];
37
    $interbreadcrumb[] = [
38
        'url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],
39
        'name' => $extraFieldInfo['display_text'],
40
    ];
41
    $interbreadcrumb[] = [
42
        'url' => 'extra_field_options.php?type='.$extraField->type.'&field_id='.$extraFieldInfo['id'],
43
        'name' => get_lang('EditExtraFieldOptions'),
44
    ];
45
    $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Add')];
46
} elseif ($action == 'edit') {
47
    $interbreadcrumb[] = [
48
        'url' => 'extra_fields.php?type='.$extraField->type,
49
        'name' => $extraField->pageName,
50
    ];
51
    $interbreadcrumb[] = [
52
        'url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],
53
        'name' => $extraFieldInfo['display_text'],
54
    ];
55
    $interbreadcrumb[] = [
56
        'url' => 'extra_field_options.php?type='.$extraField->type.'&field_id='.$extraFieldInfo['id'],
57
        'name' => get_lang('EditExtraFieldOptions'),
58
    ];
59
60
    $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Edit')];
61
} else {
62
    $interbreadcrumb[] = [
63
        'url' => 'extra_fields.php?type='.$extraField->type,
64
        'name' => $extraField->pageName,
65
    ];
66
    $interbreadcrumb[] = [
67
        'url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],
68
        'name' => $extraFieldInfo['display_text'],
69
    ];
70
    $interbreadcrumb[] = [
71
        'url' => '#',
72
        'name' => get_lang('EditExtraFieldOptions'),
73
    ];
74
}
75
76
$roleId = isset($_REQUEST['roleId']) ? (int) $_REQUEST['roleId'] : null;
77
78
//jqgrid will use this URL to do the selects
79
$params = 'field_id='.$field_id.'&type='.$extraField->type.'&roleId='.$roleId;
80
$paramsNoRole = 'field_id='.$field_id.'&type='.$extraField->type;
81
82
// The order is important you need to check the the $column variable in the model.ajax.php file
83
$columns = [get_lang('Name'), get_lang('Value'), get_lang('Order'), get_lang('Actions')];
84
85
$htmlHeadXtra[] = '<script>
86
    function setHidden(obj) {
87
        var name = $(obj).attr("name");
88
        var hiddenName = "hidden_" + name;
89
        if ($("#" + hiddenName).attr("value") == 1) {
90
            $("#" + hiddenName).attr("value", 0);
91
        } else {
92
            $("#" + hiddenName).attr("value", 1);
93
        }
94
    }
95
96
    $(function() {
97
        $("#workflow_status").on("change", function() {
98
            var roleId = $(this).find(":selected").val();
99
            if (roleId != 0) {
100
                window.location.replace("'.api_get_self().'?'.$paramsNoRole.'&roleId="+roleId);
101
            }
102
        });
103
104
        $("[name=select_all]").on("click", function() {
105
            $("#workflow :checkbox").prop("checked", 1);
106
            $("#workflow :hidden").prop("value", 1);
107
            return false;
108
        });
109
110
        $("[name=unselect_all]").on("click", function() {
111
            $("#workflow :checkbox").prop("checked", 0);
112
            $("#workflow :hidden").prop("value", 0);
113
            return false;
114
        });
115
    });
116
</script>';
117
118
$obj = new ExtraFieldOption($type);
119
$columns = ['display_text', 'option_value', 'option_order'];
120
$result = Database::select(
121
    '*',
122
    $obj->table,
123
    [
124
        'where' => ['field_id = ? ' => $field_id],
125
        'order' => 'option_order ASC',
126
    ]
127
);
128
129
$table = new HTML_Table(['class' => 'table table-hover table-striped data_table']);
130
$column = 0;
131
$row = 0;
132
$table->setHeaderContents($row, $column, get_lang('CurrentStatus'));
133
$column++;
134
foreach ($result as $item) {
135
    $table->setHeaderContents($row, $column, $item['display_text']);
136
    $column++;
137
}
138
$row++;
139
140
$form = new FormValidator('workflow', 'post', api_get_self().'?'.$params);
141
//$options = api_get_user_roles();
142
$options[0] = get_lang('SelectAnOption');
143
$options[STUDENT] = get_lang('Student');
144
$options[COURSEMANAGER] = get_lang('Teacher');
145
146
ksort($options);
147
$form->addElement('select', 'status', get_lang('SelectRole'), $options);
148
149
$em = Database::getManager();
150
$repo = $em->getRepository('ChamiloCoreBundle:ExtraFieldOptionRelFieldOption');
151
152
$checks = $repo->findBy(
153
    ['fieldId' => $field_id, 'roleId' => $roleId]
154
);
155
$includedFields = [];
156
if (!empty($checks)) {
157
    foreach ($checks as $availableField) {
158
        $includedFields[$availableField->getFieldOptionId()][] = $availableField->getRelatedFieldOptionId();
159
    }
160
}
161
162
foreach ($result as $item) {
0 ignored issues
show
Comprehensibility Bug introduced by
$item is overwriting a variable from outer foreach loop.
Loading history...
163
    $column = 0;
164
    $table->setCellContents($row, $column, $item['display_text']);
165
    $column++;
166
    $value = null;
167
168
    foreach ($result as $itemCol) {
169
        $id = 'extra_field_status_'.$item['id'].'_'.$itemCol['id'];
170
        $idForm = 'extra_field_status['.$item['id'].']['.$itemCol['id'].']';
171
        $attributes = ['onclick' => 'setHidden(this)'];
172
        $value = 0;
173
174
        if (isset($includedFields[$itemCol['id']]) && in_array($item['id'], $includedFields[$itemCol['id']])) {
175
            $value = 1;
176
            $attributes['checked'] = 'checked';
177
        }
178
179
        $element = Display::input('checkbox', $id, null, $attributes);
180
        $table->setCellContents($row, $column, $element);
181
        $form->addElement('hidden', 'hidden_'.$idForm, $value, ['id' => 'hidden_'.$id]);
182
        $column++;
183
    }
184
    $row++;
185
}
186
187
if (!empty($roleId)) {
188
    $form->addElement('html', $table->toHtml());
189
    $group = [];
190
    $group[] = $form->addButtonSave(get_lang('Save'), 'submit', true);
191
    $group[] = $form->addButton(
192
        'select_all',
193
        get_lang('SelectAll'),
194
        'check',
195
        'default',
196
        'default',
197
        null,
198
        [],
199
        true
200
    );
201
    $group[] = $form->addButton(
202
        'unselect_all',
203
        get_lang('UnSelectAll'),
204
        'check',
205
        'default',
206
        'default',
207
        null,
208
        [],
209
        true
210
    );
211
212
    $form->addGroup($group, '', null, ' ');
213
    $form->setDefaults(['status' => $roleId]);
214
} else {
215
    $form->addButtonUpdate(get_lang('Edit'));
216
}
217
218
if ($form->validate()) {
219
    $values = $form->getSubmitValues();
220
    $result = $values['hidden_extra_field_status'];
221
222
    if (!empty($result)) {
223
        foreach ($result as $id => $items) {
224
            foreach ($items as $subItemId => $value) {
225
                $extraFieldOptionRelFieldOption = $repo->findOneBy(
226
                    [
227
                        'fieldId' => $field_id,
228
                        'fieldOptionId' => $subItemId,
229
                        'roleId' => $roleId,
230
                        'relatedFieldOptionId' => $id,
231
                    ]
232
                );
233
234
                if ($value == 1) {
235
                    if (empty($extraFieldOptionRelFieldOption)) {
236
                        $extraFieldOptionRelFieldOption = new ExtraFieldOptionRelFieldOption();
237
                        $extraFieldOptionRelFieldOption
238
                            ->setFieldId($field_id)
239
                            ->setFieldOptionId($subItemId)
240
                            ->setRelatedFieldOptionId($id)
241
                            ->setRoleId($roleId)
242
                        ;
243
244
                        $em->persist($extraFieldOptionRelFieldOption);
245
                    }
246
                } else {
247
                    if ($extraFieldOptionRelFieldOption) {
248
                        $em->remove($extraFieldOptionRelFieldOption);
249
                    }
250
                }
251
            }
252
        }
253
        $em->flush();
254
255
        Display::addFlash(Display::return_message(get_lang('Updated')));
256
        header('Location:'.api_get_self().'?'.$params);
257
        exit;
258
    }
259
}
260
261
Display::display_header($tool_name);
262
echo Display::page_header($extraFieldInfo['display_text']);
263
$form->display();
264
265
Display::display_footer();
266