Completed
Push — master ( c9546d...95f607 )
by Julito
09:41
created

public/main/admin/add_sessions_to_usergroup.php (1 issue)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
// resetting the course id
6
$cidReset = true;
7
8
// including some necessary files
9
require_once __DIR__.'/../inc/global.inc.php';
10
11
$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
12
$usergroup = new UserGroup();
13
$data = $usergroup->get($id);
14
$usergroup->protectScript($data);
15
16
$xajax = new xajax();
17
$xajax->registerFunction('search_usergroup_sessions');
18
19
// setting the section (for the tabs)
20
$this_section = SECTION_PLATFORM_ADMIN;
21
22
// setting breadcrumbs
23
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
24
$interbreadcrumb[] = ['url' => 'usergroups.php', 'name' => get_lang('Classes')];
25
26
// setting the name of the tool
27
$tool_name = get_lang('Subscribe class to sessions');
28
29
$add_type = 'multiple';
30
if (isset($_REQUEST['add_type']) && '' != $_REQUEST['add_type']) {
31
    $add_type = Security::remove_XSS($_REQUEST['add_type']);
32
}
33
34
$htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
35
$htmlHeadXtra[] = '<script>
36
function add_user_to_session (code, content) {
37
    document.getElementById("user_to_add").value = "";
38
    document.getElementById("ajax_list_users_single").innerHTML = "";
39
    destination = document.getElementById("elements_in");
40
    for (i=0;i<destination.length;i++) {
41
        if(destination.options[i].text == content) {
42
                return false;
43
        }
44
    }
45
    destination.options[destination.length] = new Option(content,code);
46
    destination.selectedIndex = -1;
47
    sortOptions(destination.options);
48
}
49
function remove_item(origin) {
50
    for(var i = 0 ; i<origin.options.length ; i++) {
51
        if(origin.options[i].selected) {
52
            origin.options[i]=null;
53
            i = i-1;
54
        }
55
    }
56
}
57
58
function display_advanced_search () {
59
    if ($("#advancedSearch").css("display") == "none") {
60
        $("#advancedSearch").css("display","block");
61
        $("#img_plus_and_minus").html(\'&nbsp;'.Display::return_icon('div_hide.gif', get_lang('Hide'), ['style' => 'vertical-align:middle']).'&nbsp;'.get_lang('Advanced search').'\');
62
    } else {
63
        $("#advancedSearch").css("display","none");
64
        $("#img_plus_and_minus").html(\'&nbsp;'.Display::return_icon('div_show.gif', get_lang('Show'), ['style' => 'vertical-align:middle']).'&nbsp;'.get_lang('Advanced search').'\');
65
    }
66
}
67
68
function validate_filter() {
69
    document.formulaire.add_type.value = \''.$add_type.'\';
70
    document.formulaire.form_sent.value=0;
71
    document.formulaire.submit();
72
}
73
</script>';
74
75
$errorMsg = '';
76
if (isset($_POST['form_sent']) && $_POST['form_sent']) {
77
    $form_sent = $_POST['form_sent'];
78
    $elements_posted = $_POST['elements_in_name'];
79
    if (!is_array($elements_posted)) {
80
        $elements_posted = [];
81
    }
82
    if (1 == $form_sent) {
83
        //added a parameter to send emails when registering a user
84
        $usergroup->subscribe_sessions_to_usergroup($id, $elements_posted);
85
        header('Location: usergroups.php');
86
        exit;
87
    }
88
}
89
$session_list_in = $usergroup->get_sessions_by_usergroup($id);
90
91
$onlyThisSessionList = [];
92
if ($usergroup->allowTeachers()) {
93
    $userId = api_get_user_id();
94
    $sessionList = SessionManager::getSessionsFollowedByUser($userId, COURSEMANAGER);
95
    if (!empty($sessionList)) {
96
        $onlyThisSessionList = array_column($sessionList, 'id');
97
    }
98
}
99
$session_list = SessionManager::get_sessions_list([], ['name'], null, null, 0, $onlyThisSessionList);
100
$elements_not_in = $elements_in = [];
101
102
if (!empty($session_list)) {
103
    foreach ($session_list as $session) {
104
        if (in_array($session['id'], $session_list_in)) {
105
            $elements_in[$session['id']] = $session['name'];
106
        } else {
107
            $elements_not_in[$session['id']] = $session['name'];
108
        }
109
    }
110
}
111
112
$ajax_search = 'unique' === $add_type ? true : false;
113
114
// checking for extra field with filter on
115
function search_usergroup_sessions($needle, $type)
116
{
117
    global $elements_in;
118
    $xajax_response = new xajaxResponse();
119
    $return = '';
120
    if (!empty($needle) && !empty($type)) {
121
        if ('searchbox' == $type) {
122
            $session_list = SessionManager::get_sessions_list(
123
                ['s.name' => ['operator' => 'LIKE', 'value' => "%$needle%"]]
124
            );
125
        } elseif ('single' != $type) {
126
            $session_list = SessionManager::get_sessions_list(
127
                ['s.name' => ['operator' => 'LIKE', 'value' => "$needle%"]]
128
            );
129
        }
130
        if ('single' != $type) {
131
            $return .= '<select id="elements_not_in" name="elements_not_in_name[]" multiple="multiple" size="15" style="width:360px;">';
132
            foreach ($session_list as $row) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $session_list does not seem to be defined for all execution paths leading up to this point.
Loading history...
133
                if (!in_array($row['id'], array_keys($elements_in))) {
134
                    $return .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
135
                }
136
            }
137
            $return .= '</select>';
138
            $xajax_response->addAssign(
139
                'ajax_list_multiple',
140
                'innerHTML',
141
                api_utf8_encode($return)
142
            );
143
        }
144
    }
145
146
    return $xajax_response;
147
}
148
$xajax->processRequests();
149
150
Display::display_header($tool_name);
151
152
$add = (empty($_GET['add']) ? '' : Security::remove_XSS($_GET['add']));
153
if ('multiple' == $add_type) {
154
    $link_add_type_unique = '<a href="'.api_get_self().'?add='.$add.'&add_type=unique">'.
155
        Display::return_icon('single.gif').get_lang('Single registration').'</a>';
156
    $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('Multiple registration');
157
} else {
158
    $link_add_type_unique = Display::return_icon('single.gif').get_lang('Single registration');
159
    $link_add_type_multiple = '<a href="'.api_get_self().'?add='.$add.'&add_type=multiple">'.
160
        Display::return_icon('multiple.gif').get_lang('Multiple registration').'</a>';
161
}
162
163
echo '<div class="actions">';
164
echo '<a href="usergroups.php">'.
165
    Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'</a>';
166
echo '<a href="javascript://" class="advanced_parameters" style="margin-top: 8px" onclick="display_advanced_search();"><span id="img_plus_and_minus">&nbsp;'.
167
    Display::return_icon('div_show.gif', get_lang('Show'), ['style' => 'vertical-align:middle']).' '.get_lang('Advanced search').'</span></a>';
168
echo '</div>';
169
echo '<div id="advancedSearch" style="display: none">'.get_lang('Session Search'); ?> :
170
     <input name="SearchSession" onchange = "xajax_search_usergroup_sessions(this.value,'searchbox')" onkeyup="this.onchange()">
171
     </div>
172
<form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?id=<?php echo $id; if (!empty($_GET['add'])) {
173
    echo '&add=true';
174
} ?>" style="margin:0px;" <?php if ($ajax_search) {
175
    echo ' onsubmit="valide();"';
176
}?>>
177
<?php
178
echo '<legend>'.$data['name'].': '.$tool_name.'</legend>';
179
echo Display::input('hidden', 'id', $id);
180
echo Display::input('hidden', 'form_sent', '1');
181
echo Display::input('hidden', 'add_type', null);
182
if (!empty($errorMsg)) {
183
    echo Display::return_message($errorMsg, 'normal'); //main API
184
}
185
?>
186
187
<table border="0" cellpadding="5" cellspacing="0" width="100%">
188
<tr>
189
  <td align="center"><b><?php echo get_lang('Sessions not subscribed'); ?> :</b>
190
  </td>
191
  <td></td>
192
  <td align="center"><b><?php echo get_lang('Sessions in group'); ?> :</b></td>
193
</tr>
194
195
<?php if ('multiple' == $add_type) {
196
    ?>
197
<tr>
198
<td align="center">
199
<?php echo get_lang('First letter of session name'); ?> :
200
     <select name="firstLetterUser" onchange = "xajax_search_usergroup_sessions(this.value,'multiple')" >
201
      <option value = "%">--</option>
202
      <?php
203
        echo Display :: get_alphabet_options(); ?>
204
     </select>
205
<?php echo '<br />'; ?>
206
</td>
207
<td align="center">&nbsp;</td>
208
</tr>
209
<?php
210
} ?>
211
<tr>
212
  <td align="center">
213
  <div id="content_source">
214
      <?php
215
      if (!('multiple' == $add_type)) {
216
          ?>
217
        <input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')" />
218
        <div id="ajax_list_users_single"></div>
219
        <?php
220
      } else {
221
          ?>
222
      <div id="ajax_list_multiple">
223
          <?php echo Display::select(
224
              'elements_not_in_name',
225
              $elements_not_in,
226
              '',
227
              ['style' => 'width:360px', 'multiple' => 'multiple', 'id' => 'elements_not_in', 'size' => '15px'],
228
              false
229
          ); ?>
230
      </div>
231
    <?php
232
      }
233
     ?>
234
  </div>
235
  </td>
236
  <td width="10%" valign="middle" align="center">
237
  <?php
238
  if ($ajax_search) {
239
      ?>
240
    <button class="btn btn-default" type="button" onclick="remove_item(document.getElementById('elements_in'))" >
241
        <em class="fa fa-arrow-left"></em>
242
    </button>
243
  <?php
244
  } else {
245
      ?>
246
    <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'))">
247
        <em class="fa fa-arrow-right"></em>
248
    </button>
249
    <br /><br />
250
    <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'))">
251
        <em class="fa fa-arrow-left"></em>
252
    </button>
253
    <?php
254
  }
255
  ?>
256
    <br /><br /><br /><br /><br /><br />
257
  </td>
258
  <td align="center">
259
<?php
260
echo Display::select(
261
    'elements_in_name[]',
262
    $elements_in,
263
    '',
264
    ['style' => 'width:360px', 'multiple' => 'multiple', 'id' => 'elements_in', 'size' => '15px'],
265
    false
266
);
267
?>
268
 </td>
269
</tr>
270
<tr>
271
    <td colspan="3" align="center">
272
        <br />
273
        <?php
274
        echo '<button class="btn btn-primary" type="button" value="" onclick="valide()" >'.get_lang('Subscribe class to sessions').'</button>';
275
        ?>
276
    </td>
277
</tr>
278
</table>
279
</form>
280
281
<script>
282
function moveItem(origin , destination) {
283
    for(var i = 0 ; i<origin.options.length ; i++) {
284
        if(origin.options[i].selected) {
285
            destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
286
            origin.options[i]=null;
287
            i = i-1;
288
        }
289
    }
290
    destination.selectedIndex = -1;
291
    sortOptions(destination.options);
292
}
293
294
function sortOptions(options) {
295
    newOptions = new Array();
296
    for (i = 0 ; i<options.length ; i++)
297
        newOptions[i] = options[i];
298
299
    newOptions = newOptions.sort(mysort);
300
    options.length = 0;
301
    for(i = 0 ; i < newOptions.length ; i++)
302
        options[i] = newOptions[i];
303
}
304
305
function mysort(a, b){
306
    if(a.text.toLowerCase() > b.text.toLowerCase()){
307
        return 1;
308
    }
309
    if(a.text.toLowerCase() < b.text.toLowerCase()){
310
        return -1;
311
    }
312
    return 0;
313
}
314
315
function valide(){
316
    var options = document.getElementById('elements_in').options;
317
    for (i = 0 ; i<options.length ; i++)
318
        options[i].selected = true;
319
    document.forms.formulaire.submit();
320
}
321
322
function loadUsersInSelect(select) {
323
    var xhr_object = null;
324
    if(window.XMLHttpRequest) // Firefox
325
        xhr_object = new XMLHttpRequest();
326
    else if(window.ActiveXObject) // Internet Explorer
327
        xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
328
    else  // XMLHttpRequest non supporté par le navigateur
329
    alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
330
331
    xhr_object.open("POST", "loadUsersInSelect.ajax.php");
332
    xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
333
    nosessionUsers = makepost(document.getElementById('elements_not_in'));
334
    sessionUsers = makepost(document.getElementById('elements_in'));
335
    nosessionClasses = makepost(document.getElementById('origin_classes'));
336
    sessionClasses = makepost(document.getElementById('destination_classes'));
337
    xhr_object.send("nosessionusers="+nosessionUsers+"&sessionusers="+sessionUsers+"&nosessionclasses="+nosessionClasses+"&sessionclasses="+sessionClasses);
338
339
    xhr_object.onreadystatechange = function() {
340
        if(xhr_object.readyState == 4) {
341
            document.getElementById('content_source').innerHTML = result = xhr_object.responseText;
342
        }
343
    }
344
}
345
346
function makepost(select){
347
    var options = select.options;
348
    var ret = "";
349
    for (i = 0 ; i<options.length ; i++)
350
        ret = ret + options[i].value +'::'+options[i].text+";;";
351
352
    return ret;
353
}
354
</script>
355
<?php
356
Display::display_footer();
357