forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deprecatedlib.php
1680 lines (1445 loc) · 56 KB
/
deprecatedlib.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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 1999 onwards Martin Dougiamas, Moodle http://moodle.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
/**
* deprecatedlib.php - Old functions retained only for backward compatibility
*
* Old functions retained only for backward compatibility. New code should not
* use any of these functions.
*
* @author Martin Dougiamas
* @version $Id$
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package moodlecore
*/
/**
* Ensure that a variable is set
*
* If $var is undefined throw an error, otherwise return $var.
*
* @param mixed $var the variable which may be unset
* @param mixed $default the value to return if $var is unset
*/
function require_variable($var) {
global $CFG;
if (!empty($CFG->disableglobalshack)) {
print_error( 'The require_variable() function is deprecated.' );
}
if (! isset($var)) {
print_error('A required parameter was missing');
}
}
/**
* Ensure that a variable is set
*
* If $var is undefined set it (by reference), otherwise return $var.
*
* @param mixed $var the variable which may be unset
* @param mixed $default the value to return if $var is unset
*/
function optional_variable(&$var, $default=0) {
global $CFG;
if (!empty($CFG->disableglobalshack)) {
print_error( "The optional_variable() function is deprecated ($var, $default)." );
}
if (! isset($var)) {
$var = $default;
}
}
/**
* Ensure that a variable is set
*
* Return $var if it is defined, otherwise return $default,
* This function is very similar to {@link optional_variable()}
*
* @param mixed $var the variable which may be unset
* @param mixed $default the value to return if $var is unset
* @return mixed
*/
function nvl(&$var, $default='') {
global $CFG;
if (!empty($CFG->disableglobalshack)) {
print_error( "The nvl() function is deprecated ($var, $default)." );
}
return isset($var) ? $var : $default;
}
/**
* Determines if a user an admin
*
* @uses $USER
* @param int $userid The id of the user as is found in the 'user' table
* @staticvar array $admins List of users who have been found to be admins by user id
* @staticvar array $nonadmins List of users who have been found not to be admins by user id
* @return bool
*/
function isadmin($userid=0) {
global $USER, $CFG;
if (empty($CFG->rolesactive)) { // Then the user is likely to be upgrading NOW
if (!$userid) {
if (empty($USER->id)) {
return false;
}
if (!empty($USER->admin)) {
return true;
}
$userid = $USER->id;
}
return record_exists('user_admins', 'userid', $userid);
}
$context = get_context_instance(CONTEXT_SYSTEM);
return has_capability('moodle/legacy:admin', $context, $userid, false);
}
/**
* Determines if a user is a teacher (or better)
*
* @uses $CFG
* @param int $courseid The id of the course that is being viewed, if any
* @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
* @param bool $obsolete_includeadmin Not used any more
* @return bool
*/
function isteacher($courseid=0, $userid=0, $obsolete_includeadmin=true) {
/// Is the user able to access this course as a teacher?
global $CFG;
if (empty($CFG->rolesactive)) { // Teachers are locked out during an upgrade to 1.7
return false;
}
if ($courseid) {
$context = get_context_instance(CONTEXT_COURSE, $courseid);
} else {
$context = get_context_instance(CONTEXT_SYSTEM);
}
return (has_capability('moodle/legacy:teacher', $context, $userid, false)
or has_capability('moodle/legacy:editingteacher', $context, $userid, false)
or has_capability('moodle/legacy:admin', $context, $userid, false));
}
/**
* Determines if a user is a teacher in any course, or an admin
*
* @uses $USER
* @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
* @param bool $includeadmin Include anyone wo is an admin as well
* @return bool
*/
function isteacherinanycourse($userid=0, $includeadmin=true) {
global $USER, $CFG;
if (empty($CFG->rolesactive)) { // Teachers are locked out during an upgrade to 1.7
return false;
}
if (!$userid) {
if (empty($USER->id)) {
return false;
}
$userid = $USER->id;
}
if (!record_exists('role_assignments', 'userid', $userid)) { // Has no roles anywhere
return false;
}
/// If this user is assigned as an editing teacher anywhere then return true
if ($roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW)) {
foreach ($roles as $role) {
if (record_exists('role_assignments', 'roleid', $role->id, 'userid', $userid)) {
return true;
}
}
}
/// If this user is assigned as a non-editing teacher anywhere then return true
if ($roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW)) {
foreach ($roles as $role) {
if (record_exists('role_assignments', 'roleid', $role->id, 'userid', $userid)) {
return true;
}
}
}
/// Include admins if required
if ($includeadmin) {
$context = get_context_instance(CONTEXT_SYSTEM);
if (has_capability('moodle/legacy:admin', $context, $userid, false)) {
return true;
}
}
return false;
}
/**
* Determines if a user is allowed to edit a given course
*
* @param int $courseid The id of the course that is being edited
* @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
* @return bool
*/
function isteacheredit($courseid, $userid=0, $obsolete_ignorestudentview=false) {
global $CFG;
if (empty($CFG->rolesactive)) {
return false;
}
if (empty($courseid)) {
$context = get_context_instance(CONTEXT_SYSTEM);
} else {
$context = get_context_instance(CONTEXT_COURSE, $courseid);
}
return (has_capability('moodle/legacy:editingteacher', $context, $userid, false)
or has_capability('moodle/legacy:admin', $context, $userid, false));
}
/**
* Determines if a user can create new courses
*
* @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
* @return bool
*/
function iscreator ($userid=0) {
global $CFG;
if (empty($CFG->rolesactive)) {
return false;
}
$context = get_context_instance(CONTEXT_SYSTEM);
return (has_capability('moodle/legacy:coursecreator', $context, $userid, false)
or has_capability('moodle/legacy:admin', $context, $userid, false));
}
/**
* Determines if a user is a student in the specified course
*
* If the course id specifies the site then this determines
* if the user is a confirmed and valid user of this site.
*
* @uses $CFG
* @uses SITEID
* @param int $courseid The id of the course being tested
* @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
* @return bool
*/
function isstudent($courseid=0, $userid=0) {
global $CFG;
if (empty($CFG->rolesactive)) {
return false;
}
if ($courseid == 0) {
$context = get_context_instance(CONTEXT_SYSTEM);
} else {
$context = get_context_instance(CONTEXT_COURSE, $courseid);
}
return has_capability('moodle/legacy:student', $context, $userid, false);
}
/**
* Determines if the specified user is logged in as guest.
*
* @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
* @return bool
*/
function isguest($userid=0) {
global $CFG;
if (empty($CFG->rolesactive)) {
return false;
}
$context = get_context_instance(CONTEXT_SYSTEM);
return has_capability('moodle/legacy:guest', $context, $userid, false);
}
/**
* Enrols (or re-enrols) a student in a given course
*
* NOTE: Defaults to 'manual' enrolment - enrolment plugins
* must set it explicitly.
*
* @uses $CFG
* @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
* @param int $courseid The id of the course that is being viewed
* @param int $timestart ?
* @param int $timeend ?
* @param string $enrol ?
* @return bool
*/
function enrol_student($userid, $courseid, $timestart=0, $timeend=0, $enrol='manual') {
global $CFG;
if (!$user = get_record('user', 'id', $userid)) { // Check user
return false;
}
if (!$roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
return false;
}
$role = array_shift($roles); // We can only use one, let's use the first one
if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
return false;
}
$res = role_assign($role->id, $user->id, 0, $context->id, $timestart, $timeend, 0, $enrol);
return $res;
}
/**
* Unenrols a student from a given course
*
* @param int $courseid The id of the course that is being viewed, if any
* @param int $userid The id of the user that is being tested against.
* @return bool
*/
function unenrol_student($userid, $courseid=0) {
global $CFG;
$status = true;
if ($courseid) {
/// First delete any crucial stuff that might still send mail
if ($forums = get_records('forum', 'course', $courseid)) {
foreach ($forums as $forum) {
delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid);
}
}
/// remove from all legacy student roles
if ($courseid == SITEID) {
$context = get_context_instance(CONTEXT_SYSTEM);
} else if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
return false;
}
if (!$roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
return false;
}
foreach($roles as $role) {
$status = role_unassign($role->id, $userid, 0, $context->id) and $status;
}
} else {
// recursivelly unenroll student from all courses
if ($courses = get_records('course')) {
foreach($courses as $course) {
$status = unenrol_student($userid, $course->id) and $status;
}
}
}
return $status;
}
/**
* Add a teacher to a given course
*
* @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
* @param int $courseid The id of the course that is being viewed, if any
* @param int $editall Can edit the course
* @param string $role Obsolete
* @param int $timestart The time they start
* @param int $timeend The time they end in this role
* @param string $enrol The type of enrolment this is
* @return bool
*/
function add_teacher($userid, $courseid, $editall=1, $role='', $timestart=0, $timeend=0, $enrol='manual') {
global $CFG;
if (!$user = get_record('user', 'id', $userid)) { // Check user
return false;
}
$capability = $editall ? 'moodle/legacy:editingteacher' : 'moodle/legacy:teacher';
if (!$roles = get_roles_with_capability($capability, CAP_ALLOW)) {
return false;
}
$role = array_shift($roles); // We can only use one, let's use the first one
if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
return false;
}
$res = role_assign($role->id, $user->id, 0, $context->id, $timestart, $timeend, 0, $enrol);
return $res;
}
/**
* Removes a teacher from a given course (or ALL courses)
* Does not delete the user account
*
* @param int $courseid The id of the course that is being viewed, if any
* @param int $userid The id of the user that is being tested against.
* @return bool
*/
function remove_teacher($userid, $courseid=0) {
global $CFG;
$roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW);
if ($roles) {
$roles += get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW);
} else {
$roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW);
}
if (empty($roles)) {
return true;
}
$return = true;
if ($courseid) {
if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
return false;
}
/// First delete any crucial stuff that might still send mail
if ($forums = get_records('forum', 'course', $courseid)) {
foreach ($forums as $forum) {
delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid);
}
}
/// No need to remove from groups now
foreach ($roles as $role) { // Unassign them from all the teacher roles
$newreturn = role_unassign($role->id, $userid, 0, $context->id);
if (empty($newreturn)) {
$return = false;
}
}
} else {
delete_records('forum_subscriptions', 'userid', $userid);
$return = true;
foreach ($roles as $role) { // Unassign them from all the teacher roles
$newreturn = role_unassign($role->id, $userid, 0, 0);
if (empty($newreturn)) {
$return = false;
}
}
}
return $return;
}
/**
* Add an admin to a site
*
* @uses SITEID
* @param int $userid The id of the user that is being tested against.
* @return bool
* @TODO: remove from cvs
*/
function add_admin($userid) {
return true;
}
function get_user_info_from_db($field, $value) { // For backward compatibility
return get_complete_user_data($field, $value);
}
/**
* Get the guest user information from the database
*
* @return object(user) An associative array with the details of the guest user account.
* @todo Is object(user) a correct return type? Or is array the proper return type with a note that the contents include all details for a user.
*/
function get_guest() {
return get_complete_user_data('username', 'guest');
}
/**
* Returns $user object of the main teacher for a course
*
* @uses $CFG
* @param int $courseid The course in question.
* @return user|false A {@link $USER} record of the main teacher for the specified course or false if error.
* @todo Finish documenting this function
*/
function get_teacher($courseid) {
global $CFG;
$context = get_context_instance(CONTEXT_COURSE, $courseid);
// Pass $view=true to filter hidden caps if the user cannot see them
if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
'', '', '', '', false, true)) {
$users = sort_by_roleassignment_authority($users, $context);
return array_shift($users);
}
return false;
}
/**
* Searches logs to find all enrolments since a certain date
*
* used to print recent activity
*
* @uses $CFG
* @param int $courseid The course in question.
* @return object|false {@link $USER} records or false if error.
* @todo Finish documenting this function
*/
function get_recent_enrolments($courseid, $timestart) {
global $CFG;
$context = get_context_instance(CONTEXT_COURSE, $courseid);
return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, l.time
FROM {$CFG->prefix}user u,
{$CFG->prefix}role_assignments ra,
{$CFG->prefix}log l
WHERE l.time > '$timestart'
AND l.course = '$courseid'
AND l.module = 'course'
AND l.action = 'enrol'
AND l.info = u.id
AND u.id = ra.userid
AND ra.contextid ".get_related_contexts_string($context)."
ORDER BY l.time ASC");
}
/**
* Returns array of userinfo of all students in this course
* or on this site if courseid is id of site
*
* @uses $CFG
* @uses SITEID
* @param int $courseid The course in question.
* @param string $sort ?
* @param string $dir ?
* @param int $page ?
* @param int $recordsperpage ?
* @param string $firstinitial ?
* @param string $lastinitial ?
* @param ? $group ?
* @param string $search ?
* @param string $fields A comma separated list of fields to be returned from the chosen table.
* @param string $exceptions ?
* @return object
* @todo Finish documenting this function
*/
function get_course_students($courseid, $sort='ul.timeaccess', $dir='', $page='', $recordsperpage='',
$firstinitial='', $lastinitial='', $group=NULL, $search='', $fields='', $exceptions='') {
global $CFG;
// make sure it works on the site course
$context = get_context_instance(CONTEXT_COURSE, $courseid);
/// For the site course, old way was to check if $CFG->allusersaresitestudents was set to true.
/// The closest comparible method using roles is if the $CFG->defaultuserroleid is set to the legacy
/// student role. This function should be replaced where it is used with something more meaningful.
if (($courseid == SITEID) && !empty($CFG->defaultuserroleid) && empty($CFG->nodefaultuserrolelists)) {
if ($roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW, $context)) {
$hascap = false;
foreach ($roles as $role) {
if ($role->id == $CFG->defaultuserroleid) {
$hascap = true;
break;
}
}
if ($hascap) {
// return users with confirmed, undeleted accounts who are not site teachers
// the following is a mess because of different conventions in the different user functions
$sort = str_replace('s.timeaccess', 'lastaccess', $sort); // site users can't be sorted by timeaccess
$sort = str_replace('timeaccess', 'lastaccess', $sort); // site users can't be sorted by timeaccess
$sort = str_replace('u.', '', $sort); // the get_user function doesn't use the u. prefix to fields
$fields = str_replace('u.', '', $fields);
if ($sort) {
$sort = $sort .' '. $dir;
}
// Now we have to make sure site teachers are excluded
if ($teachers = get_course_teachers(SITEID)) {
foreach ($teachers as $teacher) {
$exceptions .= ','. $teacher->userid;
}
$exceptions = ltrim($exceptions, ',');
}
return get_users(true, $search, true, $exceptions, $sort, $firstinitial, $lastinitial,
$page, $recordsperpage, $fields ? $fields : '*');
}
}
}
$LIKE = sql_ilike();
$fullname = sql_fullname('u.firstname','u.lastname');
$groupmembers = '';
$select = "c.contextlevel=".CONTEXT_COURSE." AND "; // Must be on a course
if ($courseid != SITEID) {
// If not site, require specific course
$select.= "c.instanceid=$courseid AND ";
}
$select.="rc.capability='moodle/legacy:student' AND rc.permission=".CAP_ALLOW." AND ";
$select .= ' u.deleted = \'0\' ';
if (!$fields) {
$fields = 'u.id, u.confirmed, u.username, u.firstname, u.lastname, '.
'u.maildisplay, u.mailformat, u.maildigest, u.email, u.city, '.
'u.country, u.picture, u.idnumber, u.department, u.institution, '.
'u.emailstop, u.lang, u.timezone, ul.timeaccess as lastaccess';
}
if ($search) {
$search = ' AND ('. $fullname .' '. $LIKE .'\'%'. $search .'%\' OR email '. $LIKE .'\'%'. $search .'%\') ';
}
if ($firstinitial) {
$select .= ' AND u.firstname '. $LIKE .'\''. $firstinitial .'%\' ';
}
if ($lastinitial) {
$select .= ' AND u.lastname '. $LIKE .'\''. $lastinitial .'%\' ';
}
if ($group === 0) { /// Need something here to get all students not in a group
return array();
} else if ($group !== NULL) {
$groupmembers = "INNER JOIN {$CFG->prefix}groups_members gm on u.id=gm.userid";
$select .= ' AND gm.groupid = \''. $group .'\'';
}
if (!empty($exceptions)) {
$select .= ' AND u.id NOT IN ('. $exceptions .')';
}
if ($sort) {
$sort = ' ORDER BY '. $sort .' ';
}
$students = get_records_sql("SELECT $fields
FROM {$CFG->prefix}user u INNER JOIN
{$CFG->prefix}role_assignments ra on u.id=ra.userid INNER JOIN
{$CFG->prefix}role_capabilities rc ON ra.roleid=rc.roleid INNER JOIN
{$CFG->prefix}context c ON c.id=ra.contextid LEFT OUTER JOIN
{$CFG->prefix}user_lastaccess ul on ul.userid=ra.userid
$groupmembers
WHERE $select $search $sort $dir", $page, $recordsperpage);
return $students;
}
/**
* Counts the students in a given course (or site), or a subset of them
*
* @param object $course The course in question as a course object.
* @param string $search ?
* @param string $firstinitial ?
* @param string $lastinitial ?
* @param ? $group ?
* @param string $exceptions ?
* @return int
* @todo Finish documenting this function
*/
function count_course_students($course, $search='', $firstinitial='', $lastinitial='', $group=NULL, $exceptions='') {
if ($students = get_course_students($course->id, '', '', 0, 999999, $firstinitial, $lastinitial, $group, $search, '', $exceptions)) {
return count($students);
}
return 0;
}
/**
* Returns list of all teachers in this course
*
* If $courseid matches the site id then this function
* returns a list of all teachers for the site.
*
* @uses $CFG
* @param int $courseid The course in question.
* @param string $sort ?
* @param string $exceptions ?
* @return object
* @todo Finish documenting this function
*/
function get_course_teachers($courseid, $sort='t.authority ASC', $exceptions='') {
global $CFG;
$sort = 'ul.timeaccess DESC';
$context = get_context_instance(CONTEXT_COURSE, $courseid);
/// For the site course, if the $CFG->defaultuserroleid is set to the legacy teacher role, then all
/// users are teachers. This function should be replaced where it is used with something more
/// meaningful.
if (($courseid == SITEID) && !empty($CFG->defaultuserroleid) && empty($CFG->nodefaultuserrolelists)) {
if ($roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW, $context)) {
$hascap = false;
foreach ($roles as $role) {
if ($role->id == $CFG->defaultuserroleid) {
$hascap = true;
break;
}
}
if ($hascap) {
if (empty($fields)) {
$fields = '*';
}
return get_users(true, '', true, $exceptions, 'lastname ASC', '', '', '', '', $fields);
}
}
}
$users = get_users_by_capability($context, 'moodle/course:update',
'u.*, ul.timeaccess as lastaccess',
$sort, '','','',$exceptions, false);
return sort_by_roleassignment_authority($users, $context);
/// some fields will be missing, like authority, editall
/*
return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.maildigest,
u.email, u.city, u.country, u.lastlogin, u.picture, u.lang, u.timezone,
u.emailstop, t.authority,t.role,t.editall,t.timeaccess as lastaccess
FROM {$CFG->prefix}user u,
{$CFG->prefix}user_teachers t
WHERE t.course = '$courseid' AND t.userid = u.id
AND u.deleted = '0' AND u.confirmed = '1' $exceptions $sort");
*/
}
/**
* Returns all the users of a course: students and teachers
*
* @param int $courseid The course in question.
* @param string $sort ?
* @param string $exceptions ?
* @param string $fields A comma separated list of fields to be returned from the chosen table.
* @return object
* @todo Finish documenting this function
*/
function get_course_users($courseid, $sort='ul.timeaccess DESC', $exceptions='', $fields='u.*, ul.timeaccess as lastaccess') {
global $CFG;
$context = get_context_instance(CONTEXT_COURSE, $courseid);
/// If the course id is the SITEID, we need to return all the users if the "defaultuserroleid"
/// has the capbility of accessing the site course. $CFG->nodefaultuserrolelists set to true can
/// over-rule using this.
if (($courseid == SITEID) && !empty($CFG->defaultuserroleid) && empty($CFG->nodefaultuserrolelists)) {
if ($roles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $context)) {
$hascap = false;
foreach ($roles as $role) {
if ($role->id == $CFG->defaultuserroleid) {
$hascap = true;
break;
}
}
if ($hascap) {
if (empty($fields)) {
$fields = '*';
}
return get_users(true, '', true, $exceptions, 'lastname ASC', '', '', '', '', $fields);
}
}
}
return get_users_by_capability($context, 'moodle/course:view', $fields, $sort, '','','',$exceptions, false);
}
/**
* Returns an array of user objects
*
* @uses $CFG
* @param int $groupid The group(s) in question.
* @param string $sort How to sort the results
* @return object (changed to groupids)
*/
function get_group_students($groupids, $sort='ul.timeaccess DESC') {
if (is_array($groupids)){
$groups = $groupids;
// all groups must be from one course anyway...
$group = groups_get_group(array_shift($groups));
} else {
$group = groups_get_group($groupids);
}
if (!$group) {
return NULL;
}
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
return get_users_by_capability($context, 'moodle/legacy:student', 'u.*, ul.timeaccess as lastaccess', $sort, '','',$groupids, '', false);
}
/**
* Returns list of all the teachers who can access a group
*
* @uses $CFG
* @param int $courseid The course in question.
* @param int $groupid The group in question.
* @return object
*/
function get_group_teachers($courseid, $groupid) {
/// Returns a list of all the teachers who can access a group
if ($teachers = get_course_teachers($courseid)) {
foreach ($teachers as $key => $teacher) {
if ($teacher->editall) { // These can access anything
continue;
}
if (($teacher->authority > 0) and groups_is_member($groupid, $teacher->id)) { // Specific group teachers
continue;
}
unset($teachers[$key]);
}
}
return $teachers;
}
########### FROM weblib.php ##########################################################################
/**
* Creates a nicely formatted table and returns it.
*
* @param array $table is an object with several properties.
* <ul<li>$table->head - An array of heading names.
* <li>$table->align - An array of column alignments
* <li>$table->size - An array of column sizes
* <li>$table->wrap - An array of "nowrap"s or nothing
* <li>$table->data[] - An array of arrays containing the data.
* <li>$table->class - A css class name
* <li>$table->fontsize - Is the size of all the text
* <li>$table->tablealign - Align the whole table
* <li>$table->width - A percentage of the page
* <li>$table->cellpadding - Padding on each cell
* <li>$table->cellspacing - Spacing between cells
* </ul>
* @return string
* @todo Finish documenting this function
*/
function make_table($table) {
return print_table($table, true);
}
/**
* Print a message in a standard themed box.
* This old function used to implement boxes using tables. Now it uses a DIV, but the old
* parameters remain. If possible, $align, $width and $color should not be defined at all.
* Preferably just use print_box() in weblib.php
*
* @param string $align, alignment of the box, not the text (default center, left, right).
* @param string $width, width of the box, including units %, for example '100%'.
* @param string $color, background colour of the box, for example '#eee'.
* @param int $padding, padding in pixels, specified without units.
* @param string $class, space-separated class names.
* @param string $id, space-separated id names.
* @param boolean $return, return as string or just print it
*/
function print_simple_box($message, $align='', $width='', $color='', $padding=5, $class='generalbox', $id='', $return=false) {
$output = '';
$output .= print_simple_box_start($align, $width, $color, $padding, $class, $id, true);
$output .= stripslashes_safe($message);
$output .= print_simple_box_end(true);
if ($return) {
return $output;
} else {
echo $output;
}
}
/**
* This old function used to implement boxes using tables. Now it uses a DIV, but the old
* parameters remain. If possible, $align, $width and $color should not be defined at all.
* Even better, please use print_box_start() in weblib.php
*
* @param string $align, alignment of the box, not the text (default center, left, right). DEPRECATED
* @param string $width, width of the box, including % units, for example '100%'. DEPRECATED
* @param string $color, background colour of the box, for example '#eee'. DEPRECATED
* @param int $padding, padding in pixels, specified without units. OBSOLETE
* @param string $class, space-separated class names.
* @param string $id, space-separated id names.
* @param boolean $return, return as string or just print it
*/
function print_simple_box_start($align='', $width='', $color='', $padding=5, $class='generalbox', $id='', $return=false) {
$output = '';
$divclasses = 'box '.$class.' '.$class.'content';
$divstyles = '';
if ($align) {
$divclasses .= ' boxalign'.$align; // Implement alignment using a class
}
if ($width) { // Hopefully we can eliminate these in calls to this function (inline styles are bad)
if (substr($width, -1, 1) == '%') { // Width is a % value
$width = (int) substr($width, 0, -1); // Extract just the number
if ($width < 40) {
$divclasses .= ' boxwidthnarrow'; // Approx 30% depending on theme
} else if ($width > 60) {
$divclasses .= ' boxwidthwide'; // Approx 80% depending on theme
} else {
$divclasses .= ' boxwidthnormal'; // Approx 50% depending on theme
}
} else {
$divstyles .= ' width:'.$width.';'; // Last resort
}
}
if ($color) { // Hopefully we can eliminate these in calls to this function (inline styles are bad)
$divstyles .= ' background:'.$color.';';
}
if ($divstyles) {
$divstyles = ' style="'.$divstyles.'"';
}
if ($id) {
$id = ' id="'.$id.'"';
}
$output .= '<div'.$id.$divstyles.' class="'.$divclasses.'">';
if ($return) {
return $output;
} else {
echo $output;
}
}
/**
* Print the end portion of a standard themed box.
* Preferably just use print_box_end() in weblib.php
*/
function print_simple_box_end($return=false) {
$output = '</div>';
if ($return) {
return $output;
} else {
echo $output;
}
}
/**
* deprecated - use clean_param($string, PARAM_FILE); instead
* Check for bad characters ?
*
* @param string $string ?
* @param int $allowdots ?
* @todo Finish documenting this function - more detail needed in description as well as details on arguments
*/
function detect_munged_arguments($string, $allowdots=1) {
if (substr_count($string, '..') > $allowdots) { // Sometimes we allow dots in references
return true;
}
if (ereg('[\|\`]', $string)) { // check for other bad characters
return true;
}
if (empty($string) or $string == '/') {
return true;
}
return false;
}