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

main/admin/add_courses_to_usergroup.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
// Resetting the course id.
5
$cidReset = true;
6
7
// Including some necessary files.
8
require_once __DIR__.'/../inc/global.inc.php';
9
10
$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
11
$usergroup = new UserGroup();
12
$data = $usergroup->get($id);
13
$usergroup->protectScript($data);
14
15
$xajax = new xajax();
16
$xajax->registerFunction('search');
17
18
// Setting the section (for the tabs).
19
$this_section = SECTION_PLATFORM_ADMIN;
20
21
// Setting breadcrumbs.
22
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
23
$interbreadcrumb[] = ['url' => 'usergroups.php', 'name' => get_lang('Classes')];
24
25
// Setting the name of the tool.
26
$tool_name = get_lang('SubscribeClassToCourses');
27
28
$add_type = 'multiple';
29
if (isset($_REQUEST['add_type']) && $_REQUEST['add_type'] != '') {
30
    $add_type = Security::remove_XSS($_REQUEST['add_type']);
31
}
32
33
$add = isset($_GET['add']) ? Security::remove_XSS($_GET['add']) : null;
34
$htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
35
$htmlHeadXtra[] = '<script>
36
function remove_item(origin) {
37
    for(var i = 0 ; i<origin.options.length ; i++) {
38
        if(origin.options[i].selected) {
39
            origin.options[i]=null;
40
            i = i-1;
41
        }
42
    }
43
}
44
45
</script>';
46
47
$errorMsg = '';
48
if (isset($_POST['form_sent']) && $_POST['form_sent']) {
49
    $form_sent = $_POST['form_sent'];
50
    $elements_posted = $_POST['elements_in_name'];
51
    if (!is_array($elements_posted)) {
52
        $elements_posted = [];
53
    }
54
    if ($form_sent == 1) {
55
        $usergroup->subscribe_courses_to_usergroup($id, $elements_posted);
56
        Display::addFlash(Display::return_message(get_lang('Updated')));
57
        header('Location: usergroups.php');
58
        exit;
59
    }
60
}
61
62
// Filters
63
$filters = [
64
    ['type' => 'text', 'name' => 'code', 'label' => get_lang('CourseCode')],
65
    ['type' => 'text', 'name' => 'title', 'label' => get_lang('Title')],
66
];
67
68
$searchForm = new FormValidator('search', 'get', api_get_self().'?id='.$id);
69
$searchForm->addHeader(get_lang('AdvancedSearch'));
70
$renderer = &$searchForm->defaultRenderer();
71
$searchForm->addElement('hidden', 'id', $id);
72
foreach ($filters as $param) {
73
    $searchForm->addElement($param['type'], $param['name'], $param['label']);
74
}
75
$searchForm->addButtonSearch();
76
77
$filterData = [];
78
if ($searchForm->validate()) {
79
    $filterData = $searchForm->getSubmitValues();
80
}
81
82
$conditions = [];
83
if (!empty($filters) && !empty($filterData)) {
84
    foreach ($filters as $filter) {
85
        if (isset($filter['name']) && isset($filterData[$filter['name']])) {
86
            $value = $filterData[$filter['name']];
87
            if (!empty($value)) {
88
                $conditions[$filter['name']] = $value;
89
            }
90
        }
91
    }
92
}
93
94
$course_list_in = $usergroup->get_courses_by_usergroup($id, true);
95
96
$onlyThisCourseList = [];
97
if ($usergroup->allowTeachers()) {
98
    $userId = api_get_user_id();
99
    $courseList = CourseManager::getCoursesFollowedByUser($userId, COURSEMANAGER);
100
    if (!empty($courseList)) {
101
        $onlyThisCourseList = array_column($courseList, 'id');
102
    }
103
}
104
105
$course_list = CourseManager::get_courses_list(
106
    0,
107
    0,
108
    'title',
109
    'asc',
110
    -1,
111
    null,
112
    api_get_current_access_url_id(),
113
    false,
114
    $conditions,
115
    $onlyThisCourseList
116
);
117
118
$elements_not_in = $elements_in = [];
119
foreach ($course_list_in as $course) {
120
    $elements_in[$course['id']] = $course['title']." (".$course['visual_code'].")";
121
}
122
123
if (!empty($course_list)) {
124
    foreach ($course_list as $item) {
125
        if (isset($elements_in[$item['id']])) {
126
            continue;
127
        }
128
        $elements_not_in[$item['id']] = $item['title']." (".$item['visual_code'].")";
129
    }
130
}
131
132
$ajax_search = $add_type == 'unique' ? true : false;
133
134
// checking for extra field with filter on
135
function search($needle, $type)
136
{
137
    global $elements_in;
138
    $xajax_response = new xajaxResponse();
139
    $return = '';
140
    if (!empty($needle) && !empty($type)) {
141
        if ($type != 'single') {
142
            $list = CourseManager::get_courses_list(
143
                0,
144
                0,
145
                'title',
146
                'ASC',
147
                -1,
148
                $needle
149
            );
150
        }
151
        if ($type != 'single') {
152
            $return .= '<select id="elements_not_in" name="elements_not_in_name[]" multiple="multiple" size="15" style="width:360px;">';
153
154
            foreach ($list as $row) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $list does not seem to be defined for all execution paths leading up to this point.
Loading history...
155
                if (!in_array($row['id'], array_keys($elements_in))) {
156
                    $return .= '<option value="'.$row['id'].'">'.$row['title'].' ('.$row['visual_code'].')</option>';
157
                }
158
            }
159
            $return .= '</select>';
160
            $xajax_response->addAssign('ajax_list_multiple', 'innerHTML', api_utf8_encode($return));
161
        }
162
    }
163
164
    return $xajax_response;
165
}
166
167
$xajax->processRequests();
168
Display::display_header($tool_name);
169
170
if ($add_type == 'multiple') {
171
    $link_add_type_unique = '<a href="'.api_get_self().'?add='.$add.'&add_type=unique">'.
172
        Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'</a>';
173
    $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple');
174
} else {
175
    $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique');
176
    $link_add_type_multiple = '<a href="'.api_get_self().'?add='.$add.'&add_type=multiple">'.
177
        Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').'</a>';
178
}
179
180
echo '<div class="actions">';
181
echo '<a href="usergroups.php">';
182
echo Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM).'</a>';
183
echo Display::url(get_lang('AdvancedSearch'), '#', ['class' => 'advanced_options', 'id' => 'advanced_search']);
184
echo '</div>';
185
186
echo '<div id="advanced_search_options" style="display:none">';
187
$searchForm->display();
188
echo '</div>';
189
?>
190
<form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?id=<?php echo $id; if (!empty($_GET['add'])) {
191
    echo '&add=true';
192
} ?>" style="margin:0px;" <?php if ($ajax_search) {
193
    echo ' onsubmit="valide();"';
194
}?>>
195
<?php echo '<legend>'.$data['name'].': '.$tool_name.'</legend>';
196
echo Display::input('hidden', 'id', $id);
197
echo Display::input('hidden', 'form_sent', '1');
198
echo Display::input('hidden', 'add_type', null);
199
if (!empty($errorMsg)) {
200
    echo Display::return_message($errorMsg, 'normal'); //main API
201
}
202
?>
203
204
<table border="0" cellpadding="5" cellspacing="0" width="100%">
205
<tr>
206
  <td align="center"><b><?php echo get_lang('CoursesInPlatform'); ?> :</b>
207
  </td>
208
  <td></td>
209
  <td align="center"><b><?php echo get_lang('CoursesInGroup'); ?> :</b></td>
210
</tr>
211
212
<?php if ($add_type == 'multiple') {
213
    ?>
214
<tr>
215
<td align="center">
216
<?php echo get_lang('FirstLetterCourseTitle'); ?> :
217
    <select name="firstLetterUser" onchange = "xajax_search(this.value,'multiple')" >
218
    <option value = "%">--</option>
219
    <?php
220
    echo Display :: get_alphabet_options(); ?>
221
    </select>
222
</td>
223
<td align="center">&nbsp;</td>
224
</tr>
225
<?php
226
} ?>
227
<tr>
228
  <td align="center">
229
  <div id="content_source">
230
      <?php
231
      if (!($add_type == 'multiple')) {
232
          ?>
233
        <input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')" />
234
        <div id="ajax_list_users_single"></div>
235
        <?php
236
      } else {
237
          ?>
238
      <div id="ajax_list_multiple">
239
        <?php
240
        echo Display::select(
241
            'elements_not_in_name',
242
            $elements_not_in,
243
            '',
244
            ['style' => 'width:360px', 'multiple' => 'multiple', 'id' => 'elements_not_in', 'size' => '15px'],
245
            false
246
        ); ?>
247
      </div>
248
    <?php
249
      }
250
     ?>
251
  </div>
252
  </td>
253
  <td width="10%" valign="middle" align="center">
254
  <?php
255
  if ($ajax_search) {
256
      ?>
257
    <button class="btn bt-default" type="button" onclick="remove_item(document.getElementById('elements_in'))" >
258
        <em class="fa fa-arrow-left"></em>
259
    </button>
260
  <?php
261
  } else {
262
      ?>
263
    <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('elements_not_in'), document.getElementById('elements_in'))" onclick="moveItem(document.getElementById('elements_not_in'), document.getElementById('elements_in'))">
264
        <em class="fa fa-arrow-right"></em>
265
    </button>
266
    <br /><br />
267
    <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('elements_in'), document.getElementById('elements_not_in'))" onclick="moveItem(document.getElementById('elements_in'), document.getElementById('elements_not_in'))">
268
        <em class="fa fa-arrow-left"></em>
269
    </button>
270
    <?php
271
  }
272
  ?>
273
    <br /><br /><br /><br /><br /><br />
274
  </td>
275
  <td align="center">
276
<?php
277
echo Display::select(
278
    'elements_in_name[]',
279
    $elements_in,
280
    '',
281
    ['style' => 'width:360px', 'multiple' => 'multiple', 'id' => 'elements_in', 'size' => '15px'],
282
    false
283
);
284
unset($sessionUsersList);
285
?>
286
 </td>
287
</tr>
288
<tr>
289
    <td colspan="3" align="center">
290
        <br />
291
        <?php
292
        echo '<button class="btn btn-primary" type="button" value="" onclick="valide()" >'.get_lang('SubscribeClassToCourses').'</button>';
293
        ?>
294
    </td>
295
</tr>
296
</table>
297
</form>
298
<script>
299
function moveItem(origin , destination) {
300
    for(var i = 0 ; i<origin.options.length ; i++) {
301
        if(origin.options[i].selected) {
302
            destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
303
            origin.options[i]=null;
304
            i = i-1;
305
        }
306
    }
307
    destination.selectedIndex = -1;
308
    sortOptions(destination.options);
309
}
310
311
function sortOptions(options) {
312
    newOptions = new Array();
313
    for (i = 0 ; i<options.length ; i++)
314
        newOptions[i] = options[i];
315
316
    newOptions = newOptions.sort(mysort);
317
    options.length = 0;
318
    for(i = 0 ; i < newOptions.length ; i++)
319
        options[i] = newOptions[i];
320
}
321
322
function mysort(a, b) {
323
    if(a.text.toLowerCase() > b.text.toLowerCase()){
324
        return 1;
325
    }
326
    if(a.text.toLowerCase() < b.text.toLowerCase()){
327
        return -1;
328
    }
329
    return 0;
330
}
331
332
function valide() {
333
    var options = document.getElementById('elements_in').options;
334
    for (i = 0 ; i<options.length ; i++)
335
        options[i].selected = true;
336
    document.forms.formulaire.submit();
337
}
338
339
</script>
340
<?php
341
Display::display_footer();
342