/
subscribe_user.php
executable file
·849 lines (769 loc) · 32.4 KB
/
subscribe_user.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
<?php
/* For licensing terms, see /license.txt*/
use \ExtraField as ExtraFieldModel;
use Chamilo\CoreBundle\Entity\ExtraField;
/**
* This script allows teachers to subscribe existing users
* to their course.
* @package chamilo.user
*/
require_once __DIR__.'/../inc/global.inc.php';
$current_course_tool = TOOL_USER;
// the section (for the tabs)
$this_section = SECTION_COURSES;
// notice for unauthorized people.
api_protect_course_script(true);
if (api_get_setting('allow_user_course_subscription_by_course_admin') === 'false') {
if (!api_is_platform_admin()) {
api_not_allowed(true);
}
}
// Access restriction
if (!api_is_allowed_to_edit()) {
api_not_allowed(true);
}
$tool_name = get_lang("SubscribeUserToCourse");
$type = isset($_REQUEST['type']) ? intval($_REQUEST['type']) : STUDENT;
$keyword = isset($_REQUEST['keyword']) ? Security::remove_XSS($_REQUEST['keyword']) : null;
$courseInfo = api_get_course_info();
if ($type == COURSEMANAGER) {
$tool_name = get_lang("SubscribeUserToCourseAsTeacher");
}
//extra entries in breadcrumb
$interbreadcrumb[] = array(
"url" => "user.php?".api_get_cidreq(),
"name" => get_lang("ToolUser")
);
if ($keyword) {
$interbreadcrumb[] = array(
"url" => "subscribe_user.php?type=".$type.'&'.api_get_cidreq(),
"name" => $tool_name
);
$tool_name = get_lang('SearchResults');
}
$sessionId = api_get_session_id();
$list_register_user='';
$list_not_register_user='';
if (isset($_REQUEST['register'])) {
$userInfo = api_get_user_info($_REQUEST['user_id']);
$message = $userInfo['complete_name'].' '.get_lang('AddedToCourse');
if ($type === COURSEMANAGER) {
if (!empty($sessionId)) {
$result_simple_sub = SessionManager::set_coach_to_course_session(
$_REQUEST['user_id'],
$sessionId,
$courseInfo['real_id']
);
Display::addFlash(Display::return_message($message));
} else {
$result_simple_sub = CourseManager:: subscribe_user(
$_REQUEST['user_id'],
$courseInfo['code'],
COURSEMANAGER
);
Display::addFlash(Display::return_message($message));
}
} else {
$result_simple_sub = CourseManager:: subscribe_user(
$_REQUEST['user_id'],
$courseInfo['code']
);
Display::addFlash(Display::return_message($message));
}
header('Location:'.api_get_path(WEB_CODE_PATH).'user/user.php?'.api_get_cidreq().'&type='.$type);
exit;
}
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'subscribe':
if (is_array($_POST['user'])) {
foreach ($_POST['user'] as $index => $user_id) {
$userInfo = api_get_user_info($user_id);
if ($type === COURSEMANAGER) {
if (!empty($sessionId)) {
$is_suscribe[] = SessionManager::set_coach_to_course_session(
$user_id,
$sessionId,
$courseInfo['real_id']
);
} else {
$is_suscribe[] = CourseManager::subscribe_user(
$user_id,
$courseInfo['code'],
COURSEMANAGER
);
}
} else {
$is_suscribe[] = CourseManager::subscribe_user(
$user_id,
$courseInfo['code']
);
}
$message = $userInfo['complete_name'].' '.get_lang('AddedToCourse');
Display::addFlash(Display::return_message($message));
}
}
header('Location:'.api_get_path(WEB_CODE_PATH).'user/user.php?'.api_get_cidreq().'&type='.$type);
exit;
break;
}
}
$is_western_name_order = api_is_western_name_order();
$sort_by_first_name = api_sort_by_first_name();
// Build table
$table = new SortableTable(
'subscribe_users',
'get_number_of_users',
'get_user_data',
($is_western_name_order xor $sort_by_first_name) ? 3 : 2
);
$parameters['keyword'] = $keyword;
$parameters['type'] = $type;
$table->set_additional_parameters($parameters);
$col = 0;
$table->set_header($col++, '', false);
$table->set_header($col++, get_lang('OfficialCode'));
if (api_is_western_name_order()) {
$table->set_header($col++, get_lang('FirstName'));
$table->set_header($col++, get_lang('LastName'));
} else {
$table->set_header($col++, get_lang('LastName'));
$table->set_header($col++, get_lang('FirstName'));
}
if (api_get_setting('show_email_addresses') == 'true') {
$table->set_header($col++, get_lang('Email'));
$table->set_column_filter($col - 1, 'email_filter');
}
$table->set_header($col++, get_lang('Active'), false);
$table->set_column_filter($col -1, 'active_filter');
$table->set_header($col++, get_lang('Actions'), false);
$table->set_column_filter($col -1, 'reg_filter');
$table->set_form_actions(array ('subscribe' => get_lang('reg')), 'user');
if (!empty($_POST['keyword'])) {
$keyword_name = Security::remove_XSS($_POST['keyword']);
echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
}
Display :: display_header($tool_name, "User");
// Build search-form
switch ($type) {
case STUDENT:
$url = api_get_path(WEB_CODE_PATH).'user/user.php?'.api_get_cidreq().'';
break;
case COURSEMANAGER:
$url = api_get_path(WEB_CODE_PATH).'user/user.php?'.api_get_cidreq().'&type='.COURSEMANAGER;
break;
}
$actionsLeft = '';
$actionsLeft = Display::url(
Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM),
$url
);
if (isset($_GET['subscribe_user_filter_value']) && !empty($_GET['subscribe_user_filter_value'])) {
$actionsLeft .= '<a href="subscribe_user.php?type='.$type.'">'.
Display::return_icon('clean_group.gif').' '.get_lang('ClearFilterResults').'</a>';
}
$extraForm = '';
if (api_get_setting('ProfilingFilterAddingUsers') === 'true') {
$extraForm = display_extra_profile_fields_filter();
}
// Build search-form
$form = new FormValidator(
'search_user',
'get',
api_get_self().'?'.api_get_cidreq(),
'',
null,
FormValidator::LAYOUT_INLINE
);
$form->addText('keyword', '', false);
$form->addElement('hidden', 'type', $type);
$form->addElement('hidden', 'cidReq', api_get_course_id());
$form->addButtonSearch(get_lang('Search'));
echo Display::toolbarAction('toolbar-subscriber', [$actionsLeft.$extraForm, $form->returnForm()]);
$option = $type == COURSEMANAGER ? 2 : 1;
echo UserManager::getUserSubscriptionTab($option);
// Display table
$table->display();
Display::display_footer();
/* SHOW LIST OF USERS */
/**
** Get the users to display on the current page.
*/
function get_number_of_users()
{
// Database table definition
$user_table = Database::get_main_table(TABLE_MAIN_USER);
$course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
$tbl_session_rel_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
$table_user_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
$courseCode = api_get_course_id();
$sessionId = api_get_session_id();
if (isset($_REQUEST['type']) && $_REQUEST['type'] === 'teacher') {
if (api_get_session_id() != 0) {
$sql = "SELECT COUNT(u.id)
FROM $user_table u
LEFT JOIN $tbl_session_rel_course_user cu
ON
u.user_id = cu.user_id AND
c_id = '".api_get_course_int_id()."' AND
session_id ='".$sessionId."'
WHERE
cu.user_id IS NULL AND
u.status = 1 AND
(u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
if (api_is_multiple_url_enabled()) {
$url_access_id = api_get_current_access_url_id();
if ($url_access_id !=-1) {
$tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
$sql = "SELECT COUNT(u.id)
FROM $user_table u
LEFT JOIN $tbl_session_rel_course_user cu
ON
u.user_id = cu.user_id AND cu.c_id = '".api_get_course_int_id()."' AND
session_id ='".$sessionId."'
INNER JOIN $tbl_url_rel_user as url_rel_user
ON (url_rel_user.user_id = u.user_id)
WHERE
cu.user_id IS NULL AND
access_url_id= $url_access_id AND
u.status = 1 AND
(u.official_code <> 'ADMIN' OR u.official_code IS NULL)
";
}
}
} else {
$sql = "SELECT COUNT(u.id)
FROM $user_table u
LEFT JOIN $course_user_table cu
ON u.user_id = cu.user_id and c_id='".api_get_course_int_id()."'
WHERE cu.user_id IS NULL AND u.status<>".DRH." ";
if (api_is_multiple_url_enabled()) {
$url_access_id = api_get_current_access_url_id();
if ($url_access_id !=-1) {
$tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
$sql = "SELECT COUNT(u.id)
FROM $user_table u
LEFT JOIN $course_user_table cu
ON u.user_id = cu.user_id AND c_id='".api_get_course_int_id()."'
INNER JOIN $tbl_url_rel_user as url_rel_user
ON (url_rel_user.user_id = u.user_id)
WHERE cu.user_id IS NULL AND u.status<>".DRH." AND access_url_id= $url_access_id ";
}
}
}
} else {
// students
if ($sessionId != 0) {
$sql = "SELECT COUNT(u.id)
FROM $user_table u
LEFT JOIN $tbl_session_rel_course_user cu
ON
u.user_id = cu.user_id AND
c_id='".api_get_course_int_id()."' AND
session_id ='".$sessionId."'
WHERE
cu.user_id IS NULL AND
u.status<>".DRH." AND
(u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
if (api_is_multiple_url_enabled()) {
$url_access_id = api_get_current_access_url_id();
if ($url_access_id !=-1) {
$tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
$sql = "SELECT COUNT(u.id)
FROM $user_table u
LEFT JOIN $tbl_session_rel_course_user cu
ON
u.user_id = cu.user_id AND
c_id='".api_get_course_int_id()."' AND
session_id ='".$sessionId."'
INNER JOIN $tbl_url_rel_user as url_rel_user
ON (url_rel_user.user_id = u.id)
WHERE
cu.user_id IS NULL AND
u.status<>".DRH." AND
access_url_id= $url_access_id AND
(u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
}
}
} else {
$sql = "SELECT COUNT(u.id)
FROM $user_table u
LEFT JOIN $course_user_table cu
ON u.user_id = cu.user_id AND c_id='".api_get_course_int_id()."'";
// we change the SQL when we have a filter
if (isset($_GET['subscribe_user_filter_value']) &&
!empty($_GET['subscribe_user_filter_value']) &&
api_get_setting('ProfilingFilterAddingUsers') === 'true'
){
$field_identification = explode('*',$_GET['subscribe_user_filter_value']);
$sql .= "
LEFT JOIN $table_user_field_values field_values
ON field_values.item_id = u.user_id
WHERE
cu.user_id IS NULL AND
u.status <> ".DRH." AND
field_values.field_id = '".intval($field_identification[0])."' AND
field_values.value = '".Database::escape_string($field_identification[1])."'
";
} else {
$sql .= "WHERE cu.user_id IS NULL AND u.status <> ".DRH." ";
}
if (api_is_multiple_url_enabled()) {
$url_access_id = api_get_current_access_url_id();
if ($url_access_id !=-1) {
$tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
$sql = "SELECT COUNT(u.id)
FROM $user_table u
LEFT JOIN $course_user_table cu
ON u.user_id = cu.user_id AND c_id='".api_get_course_int_id()."'
INNER JOIN $tbl_url_rel_user as url_rel_user
ON (url_rel_user.user_id = u.id)
WHERE cu.user_id IS NULL AND access_url_id= $url_access_id AND u.status <> ".DRH." ";
}
}
}
}
// when there is a keyword then we are searching and we have to change the SQL statement
if (isset($_GET['keyword']) && !empty($_GET['keyword'])) {
$keyword = Database::escape_string(trim($_REQUEST['keyword']));
$sql .= " AND (
firstname LIKE '%".$keyword."%' OR
lastname LIKE '%".$keyword."%' OR
email LIKE '%".$keyword."%' OR
username LIKE '%".$keyword."%' OR
official_code LIKE '%".$keyword."%'
)";
// we also want to search for users who have something in their profile fields that matches the keyword
if (api_get_setting('ProfilingFilterAddingUsers') === 'true') {
$additional_users = search_additional_profile_fields($keyword);
}
// getting all the users of the course (to make sure that we do not display users that are already in the course)
if (!empty($sessionId)) {
$a_course_users = CourseManager:: get_user_list_from_course_code(
$courseCode,
$sessionId
);
} else {
$a_course_users = CourseManager:: get_user_list_from_course_code(
$courseCode,
0
);
}
foreach ($a_course_users as $user_id => $course_user) {
$users_of_course[] = $course_user['user_id'];
}
}
$sql .=" AND u.status <> ".ANONYMOUS." ";
$res = Database::query($sql);
$count_user = 0;
if ($res) {
$row = Database::fetch_row($res);
$count_user = $row[0];
}
return $count_user;
}
/**
* Get the users to display on the current page.
*/
function get_user_data($from, $number_of_items, $column, $direction)
{
$url_access_id = api_get_current_access_url_id();
$course_code = api_get_course_id();
$sessionId = api_get_session_id();
$courseId = api_get_course_int_id();
// Database table definitions
$user_table = Database::get_main_table(TABLE_MAIN_USER);
$course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
$tbl_session_rel_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
$table_user_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
$tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
// adding teachers
$is_western_name_order = api_is_western_name_order();
if (api_get_setting('show_email_addresses') === 'true') {
$select_fields = "u.id AS col0,
u.official_code AS col1,
".($is_western_name_order
? "u.firstname AS col2,
u.lastname AS col3,"
: "u.lastname AS col2,
u.firstname AS col3,")."
u.email AS col4,
u.active AS col5,
u.user_id AS col6";
} else {
$select_fields = "u.user_id AS col0,
u.official_code AS col1,
".($is_western_name_order
? "u.firstname AS col2,
u.lastname AS col3,"
: "u.lastname AS col2,
u.firstname AS col3,")."
u.active AS col4,
u.user_id AS col5";
}
if (isset($_REQUEST['type']) && $_REQUEST['type'] == COURSEMANAGER) {
// adding a teacher through a session
if (!empty($sessionId)) {
$sql = "SELECT $select_fields
FROM $user_table u
LEFT JOIN $tbl_session_rel_course_user cu
ON
u.user_id = cu.user_id AND
c_id ='".$courseId."' AND
session_id ='".$sessionId."'
INNER JOIN $tbl_url_rel_user as url_rel_user
ON (url_rel_user.user_id = u.user_id) ";
// applying the filter of the additional user profile fields
if (isset($_GET['subscribe_user_filter_value']) &&
!empty($_GET['subscribe_user_filter_value']) &&
api_get_setting('ProfilingFilterAddingUsers') == 'true'
) {
$field_identification = explode('*',$_GET['subscribe_user_filter_value']);
$sql .= "
LEFT JOIN $table_user_field_values field_values
ON field_values.item_id = u.user_id
WHERE
cu.user_id IS NULL AND
u.status = 1 AND
(u.official_code <> 'ADMIN' OR u.official_code IS NULL) AND
field_values.field_id = '".intval($field_identification[0])."' AND
field_values.value = '".Database::escape_string($field_identification[1])."'";
} else {
$sql .= "WHERE cu.user_id IS NULL AND u.status=1 AND (u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
}
$sql .= " AND access_url_id = $url_access_id";
} else {
// adding a teacher NOT through a session
$sql = "SELECT $select_fields
FROM $user_table u
LEFT JOIN $course_user_table cu
ON u.user_id = cu.user_id AND c_id = '".$courseId."'";
// applying the filter of the additional user profile fields
if (isset($_GET['subscribe_user_filter_value']) &&
!empty($_GET['subscribe_user_filter_value']) &&
api_get_setting('ProfilingFilterAddingUsers') == 'true'
) {
$field_identification = explode('*',$_GET['subscribe_user_filter_value']);
$sql .= "
LEFT JOIN $table_user_field_values field_values
ON field_values.item_id = u.user_id
WHERE
cu.user_id IS NULL AND u.status<>".DRH." AND
field_values.field_id = '".intval($field_identification[0])."' AND
field_values.value = '".Database::escape_string($field_identification[1])."'";
} else {
$sql .= "WHERE cu.user_id IS NULL AND u.status<>".DRH." ";
}
// adding a teacher NOT trough a session on a portal with multiple URLs
if (api_is_multiple_url_enabled()) {
if ($url_access_id !=-1) {
$sql = "SELECT $select_fields
FROM $user_table u
LEFT JOIN $course_user_table cu
ON u.user_id = cu.user_id and c_id='".$courseId."'
INNER JOIN $tbl_url_rel_user as url_rel_user
ON (url_rel_user.user_id = u.user_id) ";
// applying the filter of the additional user profile fields
if (isset($_GET['subscribe_user_filter_value']) &&
!empty($_GET['subscribe_user_filter_value']) &&
api_get_setting('ProfilingFilterAddingUsers') == 'true'
){
$field_identification = explode('*', $_GET['subscribe_user_filter_value']);
$sql .= "
LEFT JOIN $table_user_field_values field_values
ON field_values.item_id = u.user_id
WHERE
cu.user_id IS NULL AND
u.status<>".DRH." AND
field_values.field_id = '".intval($field_identification[0])."' AND
field_values.value = '".Database::escape_string($field_identification[1])."'";
} else {
$sql .= "WHERE cu.user_id IS NULL AND u.status<>".DRH." AND access_url_id= $url_access_id ";
}
}
}
}
} else {
// adding a student
if (!empty($sessionId)) {
$sql = "SELECT $select_fields
FROM $user_table u
LEFT JOIN $tbl_session_rel_course_user cu
ON
u.user_id = cu.user_id AND
c_id ='".$courseId."' AND
session_id ='".$sessionId."' ";
if (api_is_multiple_url_enabled()) {
$sql .= " INNER JOIN $tbl_url_rel_user as url_rel_user ON (url_rel_user.user_id = u.user_id) ";
}
// applying the filter of the additional user profile fields
if (isset($_GET['subscribe_user_filter_value']) AND !empty($_GET['subscribe_user_filter_value'])){
$field_identification = explode('*',$_GET['subscribe_user_filter_value']);
$sql .= "
LEFT JOIN $table_user_field_values field_values
ON field_values.item_id = u.user_id
WHERE
cu.user_id IS NULL AND
u.status<>".DRH." AND
(u.official_code <> 'ADMIN' OR u.official_code IS NULL) AND
field_values.field_id = '".intval($field_identification[0])."' AND
field_values.value = '".Database::escape_string($field_identification[1])."'";
} else {
$sql .= "WHERE
cu.user_id IS NULL AND
u.status<>".DRH." AND
(u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
}
if (api_is_multiple_url_enabled()) {
$sql .= "AND access_url_id = $url_access_id";
}
} else {
$sql = "SELECT $select_fields
FROM $user_table u
LEFT JOIN $course_user_table cu
ON
u.user_id = cu.user_id AND
c_id ='".$courseId."'";
// applying the filter of the additional user profile fields
if (isset($_GET['subscribe_user_filter_value']) && !empty($_GET['subscribe_user_filter_value'])) {
$field_identification = explode('*',$_GET['subscribe_user_filter_value']);
$sql .= "
LEFT JOIN $table_user_field_values field_values
ON field_values.item_id = u.user_id
WHERE
cu.user_id IS NULL AND
u.status<>".DRH." AND
field_values.field_id = '".intval($field_identification[0])."' AND
field_values.value = '".Database::escape_string($field_identification[1])."'";
} else {
$sql .= "WHERE cu.user_id IS NULL AND u.status<>".DRH." ";
}
//showing only the courses of the current Chamilo access_url_id
if (api_is_multiple_url_enabled()) {
if ($url_access_id !=-1) {
$sql = "SELECT $select_fields
FROM $user_table u
LEFT JOIN $course_user_table cu
ON u.user_id = cu.user_id AND c_id='".$courseId."'
INNER JOIN $tbl_url_rel_user as url_rel_user
ON (url_rel_user.user_id = u.user_id) ";
// applying the filter of the additional user profile fields
if (isset($_GET['subscribe_user_filter_value']) &&
!empty($_GET['subscribe_user_filter_value']) &&
api_get_setting('ProfilingFilterAddingUsers') == 'true'
){
$field_identification = explode('*', $_GET['subscribe_user_filter_value']);
$sql .= "
LEFT JOIN $table_user_field_values field_values
ON field_values.item_id = u.user_id
WHERE
cu.user_id IS NULL AND
u.status<>".DRH." AND
field_values.field_id = '".intval($field_identification[0])."' AND
field_values.value = '".Database::escape_string($field_identification[1])."' AND
access_url_id = $url_access_id
";
} else {
$sql .= "WHERE cu.user_id IS NULL AND u.status<>".DRH." AND access_url_id= $url_access_id ";
}
}
}
}
}
// adding additional WHERE statements to the SQL for the search functionality
if (isset($_REQUEST['keyword'])) {
$keyword = Database::escape_string(trim($_REQUEST['keyword']));
$sql .= " AND (
firstname LIKE '%".$keyword."%' OR
lastname LIKE '%".$keyword."%' OR
email LIKE '%".$keyword."%' OR
username LIKE '%".$keyword."%' OR
official_code LIKE '%".$keyword."%'
)
";
if (api_get_setting('ProfilingFilterAddingUsers') === 'true') {
// we also want to search for users who have something in
// their profile fields that matches the keyword
$additional_users = search_additional_profile_fields($keyword);
}
// getting all the users of the course (to make sure that we do not
// display users that are already in the course)
if (!empty($sessionId)) {
$a_course_users = CourseManager :: get_user_list_from_course_code($course_code, $sessionId);
} else {
$a_course_users = CourseManager :: get_user_list_from_course_code($course_code, 0);
}
foreach ($a_course_users as $user_id=>$course_user) {
$users_of_course[] = $course_user['user_id'];
}
}
$sql .=" AND u.status != ".ANONYMOUS." ";
// Sorting and pagination (used by the sortable table)
$sql .= " ORDER BY col$column $direction ";
$from = (int) $from;
$number_of_items = (int) $number_of_items;
$sql .= " LIMIT $from, $number_of_items";
$res = Database::query($sql);
$users = array ();
while ($user = Database::fetch_row($res)) {
$users[] = $user;
}
return $users;
}
/**
* Returns a mailto-link
* @param string $email An email-address
* @return string HTML-code with a mailto-link
*/
function email_filter($email)
{
return Display :: encrypted_mailto_link($email, $email);
}
/**
* Build the reg-column of the table
* @param int $user_id The user id
* @return string Some HTML-code
*/
function reg_filter($user_id)
{
if (isset($_REQUEST['type']) && $_REQUEST['type'] == COURSEMANAGER) {
$type = COURSEMANAGER;
} else {
$type = STUDENT;
}
$user_id = (int) $user_id;
$result = '<a class="btn btn-small btn-primary" href="'.api_get_self().'?'.api_get_cidreq().'®ister=yes&type='.$type.'&user_id='.$user_id.'">'.
get_lang("reg").'</a>';
return $result;
}
/**
* Build the active-column of the table to lock or unlock a certain user
* lock = the user can no longer use this account
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @param int $active the current state of the account
* @param int $user_id The user id
* @param string $url_params
* @return string Some HTML-code with the lock/unlock button
*/
function active_filter($active, $url_params, $row)
{
$_user = api_get_user_info();
if ($active == '1') {
$action = 'AccountActive';
$image = 'accept';
}
if ($active == '0') {
$action = 'AccountInactive';
$image = 'error';
}
$result = '';
if ($row['0'] <> $_user['user_id']) {
// you cannot lock yourself out otherwise you could disable all the accounts
// including your own => everybody is locked out and nobody can change it anymore.
$result = Display::return_icon($image.'.png', get_lang(ucfirst($action)), array() , ICON_SIZE_TINY);
}
return $result;
}
/**
* Search the additional user profile fields defined by the platform administrator in
* platform administration > profiling for a given keyword.
* We not only search in the predefined options but also in the input fields wherer
* the user can enter some text.
*
* For this we get the additional profile field options that match the (search) keyword,
* then we find all the users who have entered the (search)keyword in a input field of the
* additional profile fields or have chosen one of the matching predefined options
*
* @param string $keyword a keyword we are looking for in the additional profile fields
* @return array $additional_users an array with the users who have an additional profile field that matches the keyword
*/
function search_additional_profile_fields($keyword)
{
// database table definitions
$table_user_field_options = Database :: get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
$table_user_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
$tableExtraField = Database::get_main_table(TABLE_EXTRA_FIELD);
$table_user = Database::get_main_table(TABLE_MAIN_USER);
$keyword = Database::escape_string($keyword);
// getting the field option text that match this keyword (for radio buttons and checkboxes)
$sql = "SELECT * FROM $table_user_field_options
WHERE display_text LIKE '%".$keyword."%'";
$result_profiling = Database::query($sql);
while ($profiling_field_options = Database::fetch_array($result_profiling)) {
$profiling_field_options_exact_values[] = $profiling_field_options;
}
$profiling_field_options_exact_values_sql = '';
foreach ($profiling_field_options_exact_values as $profilingkey => $profilingvalue) {
$profiling_field_options_exact_values_sql .= " OR (field_id = '".$profilingvalue['field_id']."' AND value='".$profilingvalue['option_value']."') ";
}
$extraFieldType = ExtraField::USER_FIELD_TYPE;
// getting all the user ids of the users who have chosen on of the predefined fields that contain the keyword
// or all the users who have entered the keyword in a free-form field
$sql = "SELECT
user.user_id as col0,
user.official_code as col1,
user.lastname as col2,
user.firstname as col3,
user.email as col4,
user.active as col5,
user.user_id as col6
FROM $table_user user, $table_user_field_values user_values, $tableExtraField e
WHERE
user.user_id = user_values.item_id AND
user_values.field_id = e.id AND
e.extra_field_type = $extraFieldType AND
(value LIKE '%".$keyword."%'".$profiling_field_options_exact_values_sql.")";
$result = Database::query($sql);
$additional_users = array();
while ($profiled_users = Database::fetch_array($result)) {
$additional_users[$profiled_users['col0']] = $profiled_users;
}
return $additional_users;
}
/**
* This function displays a dropdown list with all the additional user
* profile fields defined by the platform administrator in
* platform administration > profiling.
* Only the fields that have predefined fields are usefull for such a filter.
*
*/
function display_extra_profile_fields_filter()
{
// getting all the additional user profile fields
$extra = UserManager::get_extra_fields(0, 50, 5, 'ASC');
$return = '<option value="">'.get_lang('SelectFilter').'</option>';
// looping through the additional user profile fields
foreach ($extra as $id => $field_details) {
// $field_details[2] contains the type of the additional user profile field
switch ($field_details[2]) {
// text fields cannot be used as a filter
case ExtraFieldModel::FIELD_TYPE_TEXT:
break;
// text area fields cannot be used as a filter
case ExtraFieldModel::FIELD_TYPE_TEXTAREA:
break;
case ExtraFieldModel::FIELD_TYPE_RADIO:
case ExtraFieldModel::FIELD_TYPE_SELECT:
case ExtraFieldModel::FIELD_TYPE_SELECT_MULTIPLE:
$return .= '<optgroup label="'.$field_details[3].'">';
foreach ($field_details[9] as $option_id => $option_details) {
if (isset($_GET['subscribe_user_filter_value']) &&
$_GET['subscribe_user_filter_value'] == $field_details[0].'*'.$option_details[1]
) {
$selected = 'selected="selected"';
} else {
$selected = false;
}
$return .= '<option value="'.$field_details[0].'*'.$option_details[1].'" '.$selected.'>'.$option_details[2].'</option>';
}
$return .= '</optgroup>';
break;
}
}
$html = '<form class="form-inline" id="subscribe_user_filter" name="subscribe_user_filter" method="get" action="'.api_get_self().'?'.api_get_cidreq().'">';
$html .= '<input type="hidden" name="type" id="type" value="'.Security::remove_XSS($_REQUEST['type']).'" />';
$html .= '<select name="subscribe_user_filter_value" id="subscribe_user_filter_value">'.$return.'</select>';
$html .= '<button type="submit" name="submit_filter" id="submit_filter" value="" class="search">'.get_lang('Filter').'</button>';
$html .= '</form>';
return $html;
}