MDL-7861 xhtml strict fix in notice() for & in referer, proper string quoting in...
[moodle.git] / lib / deprecatedlib.php
CommitLineData
c4d0753b 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
10// Copyright (C) 1999-2999 Martin Dougiamas, Moodle http://moodle.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
26/**
27 * deprecatedlib.php - Old functions retained only for backward compatibility
28 *
29 * Old functions retained only for backward compatibility. New code should not
30 * use any of these functions.
31 *
32 * @author Martin Dougiamas
33 * @version $Id$
34 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
35 * @package moodlecore
36 */
37
38
39
d76a5a7f 40
c4d0753b 41/**
42 * Ensure that a variable is set
43 *
44 * If $var is undefined throw an error, otherwise return $var.
45 *
46 * @param mixed $var the variable which may be unset
47 * @param mixed $default the value to return if $var is unset
48 */
49function require_variable($var) {
50 global $CFG;
51 if (!empty($CFG->disableglobalshack)) {
52 error( 'The require_variable() function is deprecated.' );
53 }
54 if (! isset($var)) {
55 error('A required parameter was missing');
56 }
57}
58
c4d0753b 59/**
60 * Ensure that a variable is set
61 *
62 * If $var is undefined set it (by reference), otherwise return $var.
63 *
64 * @param mixed $var the variable which may be unset
65 * @param mixed $default the value to return if $var is unset
66 */
67function optional_variable(&$var, $default=0) {
68 global $CFG;
69 if (!empty($CFG->disableglobalshack)) {
70 error( "The optional_variable() function is deprecated ($var, $default)." );
71 }
72 if (! isset($var)) {
73 $var = $default;
74 }
75}
76
5d776ecb 77/**
78 * Ensure that a variable is set
79 *
80 * Return $var if it is defined, otherwise return $default,
81 * This function is very similar to {@link optional_variable()}
82 *
83 * @param mixed $var the variable which may be unset
84 * @param mixed $default the value to return if $var is unset
85 * @return mixed
86 */
87function nvl(&$var, $default='') {
88 global $CFG;
89
90 if (!empty($CFG->disableglobalshack)) {
91 error( "The nvl() function is deprecated ($var, $default)." );
92 }
93 return isset($var) ? $var : $default;
94}
95
c4d0753b 96/**
97 * Determines if a user an admin
98 *
99 * @uses $USER
100 * @param int $userid The id of the user as is found in the 'user' table
101 * @staticvar array $admins List of users who have been found to be admins by user id
102 * @staticvar array $nonadmins List of users who have been found not to be admins by user id
103 * @return bool
104 */
105function isadmin($userid=0) {
106 global $USER, $CFG;
107
108 if (empty($CFG->rolesactive)) { // Then the user is likely to be upgrading NOW
109 if (!$userid) {
110 if (empty($USER->id)) {
111 return false;
112 }
113 if (!empty($USER->admin)) {
114 return true;
115 }
116 $userid = $USER->id;
117 }
118
119 return record_exists('user_admins', 'userid', $userid);
120 }
121
122 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
123
c2da0757 124 return has_capability('moodle/legacy:admin', $context, $userid, false);
c4d0753b 125}
126
127/**
128 * Determines if a user is a teacher (or better)
129 *
c4d0753b 130 * @uses $CFG
131 * @param int $courseid The id of the course that is being viewed, if any
132 * @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.
133 * @param bool $obsolete_includeadmin Not used any more
134 * @return bool
135 */
136
137function isteacher($courseid=0, $userid=0, $obsolete_includeadmin=true) {
138/// Is the user able to access this course as a teacher?
c2da0757 139 global $CFG;
c4d0753b 140
141 if (empty($CFG->rolesactive)) { // Teachers are locked out during an upgrade to 1.7
142 return false;
143 }
144
145 if ($courseid) {
146 $context = get_context_instance(CONTEXT_COURSE, $courseid);
147 } else {
148 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
149 }
150
c2da0757 151 return (has_capability('moodle/legacy:teacher', $context, $userid, false)
152 or has_capability('moodle/legacy:editingteacher', $context, $userid, false)
153 or has_capability('moodle/legacy:admin', $context, $userid, false));
c4d0753b 154}
155
156/**
157 * Determines if a user is a teacher in any course, or an admin
158 *
159 * @uses $USER
160 * @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.
c2da0757 161 * @param bool $includeadmin Include anyone wo is an admin as well
c4d0753b 162 * @return bool
163 */
c2da0757 164function isteacherinanycourse($userid=0, $includeadmin=true) {
c4d0753b 165 global $USER, $CFG;
166
167 if (empty($CFG->rolesactive)) { // Teachers are locked out during an upgrade to 1.7
168 return false;
169 }
170
171 if (!$userid) {
172 if (empty($USER->id)) {
173 return false;
174 }
175 $userid = $USER->id;
176 }
177
178 if (!record_exists('role_assignments', 'userid', $userid)) { // Has no roles anywhere
179 return false;
180 }
181
182/// If this user is assigned as an editing teacher anywhere then return true
183 if ($roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW)) {
184 foreach ($roles as $role) {
185 if (record_exists('role_assignments', 'roleid', $role->id, 'userid', $userid)) {
186 return true;
187 }
188 }
189 }
190
191/// If this user is assigned as a non-editing teacher anywhere then return true
192 if ($roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW)) {
193 foreach ($roles as $role) {
194 if (record_exists('role_assignments', 'roleid', $role->id, 'userid', $userid)) {
195 return true;
196 }
197 }
198 }
199
c2da0757 200/// Include admins if required
201 if ($includeadmin) {
202 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
203 if (has_capability('moodle/legacy:admin', $context, $userid, false)) {
204 return true;
205 }
206 }
c4d0753b 207
208 return false;
209}
210
c4d0753b 211/**
212 * Determines if a user is allowed to edit a given course
213 *
c4d0753b 214 * @param int $courseid The id of the course that is being edited
215 * @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.
216 * @return bool
217 */
218function isteacheredit($courseid, $userid=0, $obsolete_ignorestudentview=false) {
c2da0757 219 global $CFG;
c4d0753b 220
221 if (empty($CFG->rolesactive)) {
222 return false;
223 }
224
225 if (empty($courseid)) {
226 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
227 } else {
228 $context = get_context_instance(CONTEXT_COURSE, $courseid);
229 }
230
c2da0757 231 return (has_capability('moodle/legacy:editingteacher', $context, $userid, false)
232 or has_capability('moodle/legacy:admin', $context, $userid, false));
c4d0753b 233}
234
235/**
236 * Determines if a user can create new courses
237 *
c4d0753b 238 * @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
239 * @return bool
240 */
241function iscreator ($userid=0) {
c2da0757 242 global $CFG;
c4d0753b 243
244 if (empty($CFG->rolesactive)) {
245 return false;
246 }
247
248 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
249
c2da0757 250 return (has_capability('moodle/legacy:coursecreator', $context, $userid, false)
251 or has_capability('moodle/legacy:admin', $context, $userid, false));
c4d0753b 252}
253
254/**
255 * Determines if a user is a student in the specified course
256 *
257 * If the course id specifies the site then this determines
258 * if the user is a confirmed and valid user of this site.
259 *
c4d0753b 260 * @uses $CFG
261 * @uses SITEID
262 * @param int $courseid The id of the course being tested
263 * @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
264 * @return bool
265 */
266function isstudent($courseid=0, $userid=0) {
c2da0757 267 global $CFG;
c4d0753b 268
269 if (empty($CFG->rolesactive)) {
270 return false;
271 }
272
273 if ($courseid == 0) {
274 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
275 } else {
276 $context = get_context_instance(CONTEXT_COURSE, $courseid);
277 }
278
c2da0757 279 return has_capability('moodle/legacy:student', $context, $userid, false);
c4d0753b 280}
281
282/**
283 * Determines if the specified user is logged in as guest.
284 *
c4d0753b 285 * @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
286 * @return bool
287 */
288function isguest($userid=0) {
c2da0757 289 global $CFG;
c4d0753b 290
291 if (empty($CFG->rolesactive)) {
292 return false;
293 }
294
295 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
296
c2da0757 297 return has_capability('moodle/legacy:guest', $context, $userid, false);
c4d0753b 298}
299
c4d0753b 300/**
301 * Enrols (or re-enrols) a student in a given course
302 *
303 * NOTE: Defaults to 'manual' enrolment - enrolment plugins
304 * must set it explicitly.
305 *
306 * @uses $CFG
307 * @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.
308 * @param int $courseid The id of the course that is being viewed
309 * @param int $timestart ?
310 * @param int $timeend ?
311 * @param string $enrol ?
312 * @return bool
313 */
314function enrol_student($userid, $courseid, $timestart=0, $timeend=0, $enrol='manual') {
315
316 global $CFG;
317
318 if (!$user = get_record('user', 'id', $userid)) { // Check user
319 return false;
320 }
321
322 if (!$roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
323 return false;
324 }
325
326 $role = array_shift($roles); // We can only use one, let's use the first one
327
328 if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
329 return false;
330 }
331
c4d0753b 332 return role_assign($role->id, $user->id, 0, $context->id, $timestart, $timeend, 0, $enrol);
333}
334
c4d0753b 335/**
336 * Unenrols a student from a given course
337 *
338 * @param int $courseid The id of the course that is being viewed, if any
339 * @param int $userid The id of the user that is being tested against.
340 * @return bool
341 */
342function unenrol_student($userid, $courseid=0) {
343 global $CFG;
8f8ead90 344
345 $status = true;
c4d0753b 346
347 if ($courseid) {
348 /// First delete any crucial stuff that might still send mail
349 if ($forums = get_records('forum', 'course', $courseid)) {
350 foreach ($forums as $forum) {
351 delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid);
352 }
353 }
4e5f3064 354 /// remove from all legacy student roles
8f8ead90 355 if ($courseid == SITEID) {
356 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
357 } else if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
358 return false;
359 }
360 if (!$roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
361 return false;
362 }
363 foreach($roles as $role) {
4e5f3064 364 $status = role_unassign($role->id, $userid, 0, $context) and $status;
8f8ead90 365 }
c4d0753b 366 } else {
4e5f3064 367 // recursivelly unenroll student from all courses
8f8ead90 368 if ($courses = get_records('course')) {
369 foreach($courses as $course) {
4e5f3064 370 $status = unenrol_student($userid, $course->id) and $status;
8f8ead90 371 }
372 }
c4d0753b 373 }
8f8ead90 374
375 return $status;
c4d0753b 376}
377
378/**
379 * Add a teacher to a given course
380 *
c4d0753b 381 * @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.
382 * @param int $courseid The id of the course that is being viewed, if any
383 * @param int $editall Can edit the course
384 * @param string $role Obsolete
385 * @param int $timestart The time they start
386 * @param int $timeend The time they end in this role
387 * @param string $enrol The type of enrolment this is
388 * @return bool
389 */
390function add_teacher($userid, $courseid, $editall=1, $role='', $timestart=0, $timeend=0, $enrol='manual') {
391 global $CFG;
392
393 if (!$user = get_record('user', 'id', $userid)) { // Check user
394 return false;
395 }
396
397 $capability = $editall ? 'moodle/legacy:editingteacher' : 'moodle/legacy:teacher';
398
399 if (!$roles = get_roles_with_capability($capability, CAP_ALLOW)) {
400 return false;
401 }
402
403 $role = array_shift($roles); // We can only use one, let's use the first one
404
405 if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
406 return false;
407 }
408
409 return role_assign($role->id, $user->id, 0, $context->id, $timestart, $timeend, 0, $enrol);
410}
411
412/**
413 * Removes a teacher from a given course (or ALL courses)
414 * Does not delete the user account
415 *
416 * @param int $courseid The id of the course that is being viewed, if any
417 * @param int $userid The id of the user that is being tested against.
418 * @return bool
419 */
420function remove_teacher($userid, $courseid=0) {
421 global $CFG;
422
c4d0753b 423 $roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW);
424
425 if ($roles) {
426 $roles += get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW);
c2da0757 427 } else {
428 $roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW);
c4d0753b 429 }
430
431 if (empty($roles)) {
432 return true;
433 }
434
435 $return = true;
436
437 if ($courseid) {
438
439 if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
440 return false;
441 }
442
443 /// First delete any crucial stuff that might still send mail
444 if ($forums = get_records('forum', 'course', $courseid)) {
445 foreach ($forums as $forum) {
446 delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid);
447 }
448 }
449
dc558690 450 /// No need to remove from groups now
c4d0753b 451
452 foreach ($roles as $role) { // Unassign them from all the teacher roles
453 $newreturn = role_unassign($role->id, $userid, 0, $context->id);
454 if (empty($newreturn)) {
455 $return = false;
456 }
457 }
458
459 } else {
460 delete_records('forum_subscriptions', 'userid', $userid);
461 $return = true;
462 foreach ($roles as $role) { // Unassign them from all the teacher roles
463 $newreturn = role_unassign($role->id, $userid, 0, 0);
464 if (empty($newreturn)) {
465 $return = false;
466 }
467 }
468 }
469
470 return $return;
471}
472
c4d0753b 473/**
474 * Add an admin to a site
475 *
476 * @uses SITEID
477 * @param int $userid The id of the user that is being tested against.
478 * @return bool
71dea306 479 * @TODO: remove from cvs
c4d0753b 480 */
481function add_admin($userid) {
c4d0753b 482 return true;
483}
484
c4d0753b 485function get_user_info_from_db($field, $value) { // For backward compatibility
486 return get_complete_user_data($field, $value);
487}
488
613bbd7c 489
490/**
491 * Get the guest user information from the database
492 *
493 * @return object(user) An associative array with the details of the guest user account.
494 * @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.
495 */
496function get_guest() {
497 return get_complete_user_data('username', 'guest');
498}
499
613bbd7c 500/**
501 * Returns $user object of the main teacher for a course
502 *
503 * @uses $CFG
504 * @param int $courseid The course in question.
505 * @return user|false A {@link $USER} record of the main teacher for the specified course or false if error.
506 * @todo Finish documenting this function
507 */
508function get_teacher($courseid) {
509
510 global $CFG;
511
888fb649 512 $context = get_context_instance(CONTEXT_COURSE, $courseid);
513
f94ced10 514 if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*,ra.hidden', 'r.sortorder ASC',
1d546bb1 515 '', '', '', '', false)) {
888fb649 516 foreach ($users as $user) {
517 if (!$user->hidden || has_capability('moodle/role:viewhiddenassigns', $context)) {
518 return $user;
613bbd7c 519 }
520 }
613bbd7c 521 }
888fb649 522
523 return false;
613bbd7c 524}
525
526/**
527 * Searches logs to find all enrolments since a certain date
528 *
529 * used to print recent activity
530 *
531 * @uses $CFG
532 * @param int $courseid The course in question.
533 * @return object|false {@link $USER} records or false if error.
534 * @todo Finish documenting this function
535 */
536function get_recent_enrolments($courseid, $timestart) {
537
538 global $CFG;
71dea306 539
540 $context = get_context_instance(CONTEXT_COURSE, $courseid);
613bbd7c 541
542 return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, l.time
543 FROM {$CFG->prefix}user u,
71dea306 544 {$CFG->prefix}role_assignments ra,
613bbd7c 545 {$CFG->prefix}log l
546 WHERE l.time > '$timestart'
547 AND l.course = '$courseid'
548 AND l.module = 'course'
549 AND l.action = 'enrol'
550 AND l.info = u.id
71dea306 551 AND u.id = ra.userid
552 AND ra.contextid ".get_related_contexts_string($context)."
613bbd7c 553 ORDER BY l.time ASC");
554}
555
556/**
557 * Returns array of userinfo of all students in this course
558 * or on this site if courseid is id of site
559 *
560 * @uses $CFG
561 * @uses SITEID
562 * @param int $courseid The course in question.
563 * @param string $sort ?
564 * @param string $dir ?
565 * @param int $page ?
566 * @param int $recordsperpage ?
567 * @param string $firstinitial ?
568 * @param string $lastinitial ?
569 * @param ? $group ?
570 * @param string $search ?
571 * @param string $fields A comma separated list of fields to be returned from the chosen table.
572 * @param string $exceptions ?
573 * @return object
574 * @todo Finish documenting this function
575 */
36075e09 576function get_course_students($courseid, $sort='ul.timeaccess', $dir='', $page='', $recordsperpage='',
613bbd7c 577 $firstinitial='', $lastinitial='', $group=NULL, $search='', $fields='', $exceptions='') {
578
579 global $CFG;
580
581 if ($courseid == SITEID and $CFG->allusersaresitestudents) {
582 // return users with confirmed, undeleted accounts who are not site teachers
583 // the following is a mess because of different conventions in the different user functions
584 $sort = str_replace('s.timeaccess', 'lastaccess', $sort); // site users can't be sorted by timeaccess
585 $sort = str_replace('timeaccess', 'lastaccess', $sort); // site users can't be sorted by timeaccess
586 $sort = str_replace('u.', '', $sort); // the get_user function doesn't use the u. prefix to fields
587 $fields = str_replace('u.', '', $fields);
588 if ($sort) {
589 $sort = $sort .' '. $dir;
590 }
591 // Now we have to make sure site teachers are excluded
71dea306 592
1d546bb1 593 if ($teachers = get_course_teachers(SITEID)) {
613bbd7c 594 foreach ($teachers as $teacher) {
595 $exceptions .= ','. $teacher->userid;
596 }
71dea306 597 $exceptions = ltrim($exceptions, ',');
598
599 }
600
613bbd7c 601 return get_users(true, $search, true, $exceptions, $sort, $firstinitial, $lastinitial,
602 $page, $recordsperpage, $fields ? $fields : '*');
603 }
604
613bbd7c 605 $LIKE = sql_ilike();
606 $fullname = sql_fullname('u.firstname','u.lastname');
607
608 $groupmembers = '';
609
610 // make sure it works on the site course
71dea306 611 $context = get_context_instance(CONTEXT_COURSE, $courseid);
acf2052b 612
613 $select = "c.contextlevel=".CONTEXT_COURSE." AND "; // Must be on a course
614 if ($courseid != SITEID) {
615 // If not site, require specific course
616 $select.= "c.instanceid=$courseid AND ";
613bbd7c 617 }
acf2052b 618 $select.="rc.capability='moodle/legacy:student' AND rc.permission=".CAP_ALLOW." AND ";
613bbd7c 619
71dea306 620 $select .= ' u.deleted = \'0\' ';
613bbd7c 621
622 if (!$fields) {
623 $fields = 'u.id, u.confirmed, u.username, u.firstname, u.lastname, '.
624 'u.maildisplay, u.mailformat, u.maildigest, u.email, u.city, '.
625 'u.country, u.picture, u.idnumber, u.department, u.institution, '.
71dea306 626 'u.emailstop, u.lang, u.timezone, ul.timeaccess as lastaccess';
613bbd7c 627 }
628
629 if ($search) {
630 $search = ' AND ('. $fullname .' '. $LIKE .'\'%'. $search .'%\' OR email '. $LIKE .'\'%'. $search .'%\') ';
631 }
632
633 if ($firstinitial) {
634 $select .= ' AND u.firstname '. $LIKE .'\''. $firstinitial .'%\' ';
635 }
636
637 if ($lastinitial) {
638 $select .= ' AND u.lastname '. $LIKE .'\''. $lastinitial .'%\' ';
639 }
640
641 if ($group === 0) { /// Need something here to get all students not in a group
642 return array();
643
644 } else if ($group !== NULL) {
71dea306 645 $groupmembers = "INNER JOIN {$CFG->prefix}groups_members gm on u.id=gm.userid";
646 $select .= ' AND gm.groupid = \''. $group .'\'';
613bbd7c 647 }
648
649 if (!empty($exceptions)) {
650 $select .= ' AND u.id NOT IN ('. $exceptions .')';
651 }
652
653 if ($sort) {
654 $sort = ' ORDER BY '. $sort .' ';
655 }
656
657 $students = get_records_sql("SELECT $fields
acf2052b 658 FROM {$CFG->prefix}user u INNER JOIN
659 {$CFG->prefix}role_assignments ra on u.id=ra.userid INNER JOIN
660 {$CFG->prefix}role_capabilities rc ON ra.roleid=rc.roleid INNER JOIN
661 {$CFG->prefix}context c ON c.id=ra.contextid LEFT OUTER JOIN
662 {$CFG->prefix}user_lastaccess ul on ul.userid=ra.userid
663 $groupmembers
664 WHERE $select $search $sort $dir", $page, $recordsperpage);
613bbd7c 665
71dea306 666 return $students;
613bbd7c 667}
668
613bbd7c 669/**
670 * Counts the students in a given course (or site), or a subset of them
671 *
672 * @param object $course The course in question as a course object.
673 * @param string $search ?
674 * @param string $firstinitial ?
675 * @param string $lastinitial ?
676 * @param ? $group ?
677 * @param string $exceptions ?
678 * @return int
679 * @todo Finish documenting this function
680 */
681function count_course_students($course, $search='', $firstinitial='', $lastinitial='', $group=NULL, $exceptions='') {
682
683 if ($students = get_course_students($course->id, '', '', 0, 999999, $firstinitial, $lastinitial, $group, $search, '', $exceptions)) {
684 return count($students);
685 }
686 return 0;
687}
688
613bbd7c 689/**
690 * Returns list of all teachers in this course
691 *
692 * If $courseid matches the site id then this function
693 * returns a list of all teachers for the site.
694 *
695 * @uses $CFG
696 * @param int $courseid The course in question.
697 * @param string $sort ?
698 * @param string $exceptions ?
699 * @return object
700 * @todo Finish documenting this function
701 */
702function get_course_teachers($courseid, $sort='t.authority ASC', $exceptions='') {
703
704 global $CFG;
71dea306 705
706 $sort = 'ul.timeaccess DESC';
707
708 $context = get_context_instance(CONTEXT_COURSE, $courseid);
1d546bb1 709 return get_users_by_capability($context, 'moodle/course:update', 'u.*, ul.timeaccess as lastaccess, ra.hidden', $sort, '','','',$exceptions, false);
71dea306 710 /// some fields will be missing, like authority, editall
711 /*
613bbd7c 712 return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.maildigest,
713 u.email, u.city, u.country, u.lastlogin, u.picture, u.lang, u.timezone,
714 u.emailstop, t.authority,t.role,t.editall,t.timeaccess as lastaccess
715 FROM {$CFG->prefix}user u,
716 {$CFG->prefix}user_teachers t
717 WHERE t.course = '$courseid' AND t.userid = u.id
718 AND u.deleted = '0' AND u.confirmed = '1' $exceptions $sort");
71dea306 719 */
613bbd7c 720}
721
722/**
723 * Returns all the users of a course: students and teachers
724 *
725 * @param int $courseid The course in question.
726 * @param string $sort ?
727 * @param string $exceptions ?
728 * @param string $fields A comma separated list of fields to be returned from the chosen table.
729 * @return object
730 * @todo Finish documenting this function
731 */
71dea306 732function get_course_users($courseid, $sort='ul.timeaccess DESC', $exceptions='', $fields='') {
613bbd7c 733
71dea306 734 $context = get_context_instance(CONTEXT_COURSE, $courseid);
1d546bb1 735 return get_users_by_capability($context, 'moodle/course:view', 'u.*, ul.timeaccess as lastaccess', $sort, '','','',$exceptions, false);
613bbd7c 736
737}
738
613bbd7c 739/**
740 * Returns an array of user objects
741 *
742 * @uses $CFG
743 * @param int $groupid The group(s) in question.
744 * @param string $sort How to sort the results
745 * @return object (changed to groupids)
746 */
7b6a42f5 747function get_group_students($groupids, $sort='ul.timeaccess DESC') {
748
613bbd7c 749 if (is_array($groupids)){
750 $groups = $groupids;
7b6a42f5 751 // all groups must be from one course anyway...
f3f7610c 752 $group = groups_get_group(array_shift($groups));
7b6a42f5 753 } else {
f3f7610c 754 $group = groups_get_group($groupids);
613bbd7c 755 }
7b6a42f5 756 if (!$group) {
757 return NULL;
613bbd7c 758 }
759
7b6a42f5 760 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
1d546bb1 761 return get_users_by_capability($context, 'moodle/legacy:student', 'u.*, ul.timeaccess as lastaccess', $sort, '','',$groupids, '', false);
613bbd7c 762}
763
764/**
765 * Returns list of all the teachers who can access a group
766 *
767 * @uses $CFG
768 * @param int $courseid The course in question.
769 * @param int $groupid The group in question.
770 * @return object
771 */
772function get_group_teachers($courseid, $groupid) {
773/// Returns a list of all the teachers who can access a group
774 if ($teachers = get_course_teachers($courseid)) {
775 foreach ($teachers as $key => $teacher) {
776 if ($teacher->editall) { // These can access anything
777 continue;
778 }
779 if (($teacher->authority > 0) and ismember($groupid, $teacher->id)) { // Specific group teachers
780 continue;
781 }
782 unset($teachers[$key]);
783 }
784 }
785 return $teachers;
786}
787
788
2123b644 789
790########### FROM weblib.php ##########################################################################
791
792
793/**
794 * Print a message in a standard themed box.
795 * This old function used to implement boxes using tables. Now it uses a DIV, but the old
796 * parameters remain. If possible, $align, $width and $color should not be defined at all.
797 * Preferably just use print_box() in weblib.php
798 *
799 * @param string $align, alignment of the box, not the text (default center, left, right).
800 * @param string $width, width of the box, including units %, for example '100%'.
801 * @param string $color, background colour of the box, for example '#eee'.
802 * @param int $padding, padding in pixels, specified without units.
803 * @param string $class, space-separated class names.
804 * @param string $id, space-separated id names.
805 * @param boolean $return, return as string or just print it
806 */
807function print_simple_box($message, $align='', $width='', $color='', $padding=5, $class='generalbox', $id='', $return=false) {
808 $output = '';
809 $output .= print_simple_box_start($align, $width, $color, $padding, $class, $id, true);
810 $output .= stripslashes_safe($message);
811 $output .= print_simple_box_end(true);
812
813 if ($return) {
814 return $output;
815 } else {
816 echo $output;
817 }
818}
819
820
821
822/**
823 * This old function used to implement boxes using tables. Now it uses a DIV, but the old
824 * parameters remain. If possible, $align, $width and $color should not be defined at all.
825 * Even better, please use print_box_start() in weblib.php
826 *
827 * @param string $align, alignment of the box, not the text (default center, left, right). DEPRECATED
828 * @param string $width, width of the box, including % units, for example '100%'. DEPRECATED
829 * @param string $color, background colour of the box, for example '#eee'. DEPRECATED
830 * @param int $padding, padding in pixels, specified without units. OBSOLETE
831 * @param string $class, space-separated class names.
832 * @param string $id, space-separated id names.
833 * @param boolean $return, return as string or just print it
834 */
835function print_simple_box_start($align='', $width='', $color='', $padding=5, $class='generalbox', $id='', $return=false) {
836
837 $output = '';
838
839 $divclasses = $class.' '.$class.'content';
840 $divstyles = '';
841
842 if ($align) {
843 $divclasses .= ' boxalign'.$align; // Implement alignment using a class
844 }
845 if ($width) { // Hopefully we can eliminate these in calls to this function (inline styles are bad)
846 $divstyles .= ' width:'.$width.';';
847 }
848 if ($color) { // Hopefully we can eliminate these in calls to this function (inline styles are bad)
849 $divstyles .= ' background:'.$color.';';
850 }
851 if ($divstyles) {
852 $divstyles = ' style="'.$divstyles.'"';
853 }
854
855 if ($id) {
856 $id = ' id="'.$id.'"';
857 }
858
859 $output .= '<div'.$id.$divstyles.' class="'.$divclasses.'">';
860
861 if ($return) {
862 return $output;
863 } else {
864 echo $output;
865 }
866}
867
868
869/**
870 * Print the end portion of a standard themed box.
871 * Preferably just use print_box_end() in weblib.php
872 */
873function print_simple_box_end($return=false) {
874 $output = '</div>';
875 if ($return) {
876 return $output;
877 } else {
878 echo $output;
879 }
880}
881
882
883
c4d0753b 884?>