correct formatting of "Show Legend" -> "No" and added missing <p> in "Encoding" expla...
[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
40
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
59
60/**
61 * Ensure that a variable is set
62 *
63 * If $var is undefined set it (by reference), otherwise return $var.
64 *
65 * @param mixed $var the variable which may be unset
66 * @param mixed $default the value to return if $var is unset
67 */
68function optional_variable(&$var, $default=0) {
69 global $CFG;
70 if (!empty($CFG->disableglobalshack)) {
71 error( "The optional_variable() function is deprecated ($var, $default)." );
72 }
73 if (! isset($var)) {
74 $var = $default;
75 }
76}
77
78
79
80/**
81 * Determines if a user an admin
82 *
83 * @uses $USER
84 * @param int $userid The id of the user as is found in the 'user' table
85 * @staticvar array $admins List of users who have been found to be admins by user id
86 * @staticvar array $nonadmins List of users who have been found not to be admins by user id
87 * @return bool
88 */
89function isadmin($userid=0) {
90 global $USER, $CFG;
91
92 if (empty($CFG->rolesactive)) { // Then the user is likely to be upgrading NOW
93 if (!$userid) {
94 if (empty($USER->id)) {
95 return false;
96 }
97 if (!empty($USER->admin)) {
98 return true;
99 }
100 $userid = $USER->id;
101 }
102
103 return record_exists('user_admins', 'userid', $userid);
104 }
105
106 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
107
c2da0757 108 return has_capability('moodle/legacy:admin', $context, $userid, false);
c4d0753b 109}
110
111/**
112 * Determines if a user is a teacher (or better)
113 *
c4d0753b 114 * @uses $CFG
115 * @param int $courseid The id of the course that is being viewed, if any
116 * @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.
117 * @param bool $obsolete_includeadmin Not used any more
118 * @return bool
119 */
120
121function isteacher($courseid=0, $userid=0, $obsolete_includeadmin=true) {
122/// Is the user able to access this course as a teacher?
c2da0757 123 global $CFG;
c4d0753b 124
125 if (empty($CFG->rolesactive)) { // Teachers are locked out during an upgrade to 1.7
126 return false;
127 }
128
129 if ($courseid) {
130 $context = get_context_instance(CONTEXT_COURSE, $courseid);
131 } else {
132 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
133 }
134
c2da0757 135 return (has_capability('moodle/legacy:teacher', $context, $userid, false)
136 or has_capability('moodle/legacy:editingteacher', $context, $userid, false)
137 or has_capability('moodle/legacy:admin', $context, $userid, false));
c4d0753b 138}
139
140/**
141 * Determines if a user is a teacher in any course, or an admin
142 *
143 * @uses $USER
144 * @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 145 * @param bool $includeadmin Include anyone wo is an admin as well
c4d0753b 146 * @return bool
147 */
c2da0757 148function isteacherinanycourse($userid=0, $includeadmin=true) {
c4d0753b 149 global $USER, $CFG;
150
151 if (empty($CFG->rolesactive)) { // Teachers are locked out during an upgrade to 1.7
152 return false;
153 }
154
155 if (!$userid) {
156 if (empty($USER->id)) {
157 return false;
158 }
159 $userid = $USER->id;
160 }
161
162 if (!record_exists('role_assignments', 'userid', $userid)) { // Has no roles anywhere
163 return false;
164 }
165
166/// If this user is assigned as an editing teacher anywhere then return true
167 if ($roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW)) {
168 foreach ($roles as $role) {
169 if (record_exists('role_assignments', 'roleid', $role->id, 'userid', $userid)) {
170 return true;
171 }
172 }
173 }
174
175/// If this user is assigned as a non-editing teacher anywhere then return true
176 if ($roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW)) {
177 foreach ($roles as $role) {
178 if (record_exists('role_assignments', 'roleid', $role->id, 'userid', $userid)) {
179 return true;
180 }
181 }
182 }
183
c2da0757 184/// Include admins if required
185 if ($includeadmin) {
186 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
187 if (has_capability('moodle/legacy:admin', $context, $userid, false)) {
188 return true;
189 }
190 }
c4d0753b 191
192 return false;
193}
194
195
196/**
197 * Determines if a user is allowed to edit a given course
198 *
c4d0753b 199 * @param int $courseid The id of the course that is being edited
200 * @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.
201 * @return bool
202 */
203function isteacheredit($courseid, $userid=0, $obsolete_ignorestudentview=false) {
c2da0757 204 global $CFG;
c4d0753b 205
206 if (empty($CFG->rolesactive)) {
207 return false;
208 }
209
210 if (empty($courseid)) {
211 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
212 } else {
213 $context = get_context_instance(CONTEXT_COURSE, $courseid);
214 }
215
c2da0757 216 return (has_capability('moodle/legacy:editingteacher', $context, $userid, false)
217 or has_capability('moodle/legacy:admin', $context, $userid, false));
c4d0753b 218}
219
220/**
221 * Determines if a user can create new courses
222 *
c4d0753b 223 * @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
224 * @return bool
225 */
226function iscreator ($userid=0) {
c2da0757 227 global $CFG;
c4d0753b 228
229 if (empty($CFG->rolesactive)) {
230 return false;
231 }
232
233 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
234
c2da0757 235 return (has_capability('moodle/legacy:coursecreator', $context, $userid, false)
236 or has_capability('moodle/legacy:admin', $context, $userid, false));
c4d0753b 237}
238
239/**
240 * Determines if a user is a student in the specified course
241 *
242 * If the course id specifies the site then this determines
243 * if the user is a confirmed and valid user of this site.
244 *
c4d0753b 245 * @uses $CFG
246 * @uses SITEID
247 * @param int $courseid The id of the course being tested
248 * @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
249 * @return bool
250 */
251function isstudent($courseid=0, $userid=0) {
c2da0757 252 global $CFG;
c4d0753b 253
254 if (empty($CFG->rolesactive)) {
255 return false;
256 }
257
258 if ($courseid == 0) {
259 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
260 } else {
261 $context = get_context_instance(CONTEXT_COURSE, $courseid);
262 }
263
c2da0757 264 return has_capability('moodle/legacy:student', $context, $userid, false);
c4d0753b 265}
266
267/**
268 * Determines if the specified user is logged in as guest.
269 *
c4d0753b 270 * @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
271 * @return bool
272 */
273function isguest($userid=0) {
c2da0757 274 global $CFG;
c4d0753b 275
276 if (empty($CFG->rolesactive)) {
277 return false;
278 }
279
280 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
281
c2da0757 282 return has_capability('moodle/legacy:guest', $context, $userid, false);
c4d0753b 283}
284
285
286
287/**
288 * Enrols (or re-enrols) a student in a given course
289 *
290 * NOTE: Defaults to 'manual' enrolment - enrolment plugins
291 * must set it explicitly.
292 *
293 * @uses $CFG
294 * @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.
295 * @param int $courseid The id of the course that is being viewed
296 * @param int $timestart ?
297 * @param int $timeend ?
298 * @param string $enrol ?
299 * @return bool
300 */
301function enrol_student($userid, $courseid, $timestart=0, $timeend=0, $enrol='manual') {
302
303 global $CFG;
304
305 if (!$user = get_record('user', 'id', $userid)) { // Check user
306 return false;
307 }
308
309 if (!$roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
310 return false;
311 }
312
313 $role = array_shift($roles); // We can only use one, let's use the first one
314
315 if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
316 return false;
317 }
318
319/// Enrol the student in any parent meta courses...
320 if ($parents = get_records('course_meta', 'child_course', $courseid)) {
321 foreach ($parents as $parent) {
322 if ($metacontext = get_context_instance(CONTEXT_COURSE, $parent->parent_course)) {
323 role_assign($role->id, $user->id, 0, $metacontext->id, $timestart, $timeend, 0, 'metacourse');
324 }
325 }
326 }
327
328 return role_assign($role->id, $user->id, 0, $context->id, $timestart, $timeend, 0, $enrol);
329}
330
331
332/**
333 * Unenrols a student from a given course
334 *
335 * @param int $courseid The id of the course that is being viewed, if any
336 * @param int $userid The id of the user that is being tested against.
337 * @return bool
338 */
339function unenrol_student($userid, $courseid=0) {
340 global $CFG;
341
342 if ($courseid) {
343 /// First delete any crucial stuff that might still send mail
344 if ($forums = get_records('forum', 'course', $courseid)) {
345 foreach ($forums as $forum) {
346 delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid);
347 }
348 }
349 if ($groups = get_groups($courseid, $userid)) {
350 foreach ($groups as $group) {
351 delete_records('groups_members', 'groupid', $group->id, 'userid', $userid);
352 }
353 }
354 // unenrol the student from any parent meta courses...
355 if ($parents = get_records('course_meta','child_course',$courseid)) {
356 foreach ($parents as $parent) {
357 if (!record_exists_sql('SELECT us.id FROM '.$CFG->prefix.'user_students us, '
358 .$CFG->prefix.'course_meta cm WHERE cm.child_course = us.course
359 AND us.userid = '.$userid .' AND us.course != '.$courseid)) {
360 unenrol_student($userid, $parent->parent_course);
361 }
362 }
363 }
364 return delete_records('user_students', 'userid', $userid, 'course', $courseid);
365
366 } else {
367 delete_records('forum_subscriptions', 'userid', $userid);
368 delete_records('groups_members', 'userid', $userid);
369 return delete_records('user_students', 'userid', $userid);
370 }
371}
372
373/**
374 * Add a teacher to a given course
375 *
c4d0753b 376 * @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.
377 * @param int $courseid The id of the course that is being viewed, if any
378 * @param int $editall Can edit the course
379 * @param string $role Obsolete
380 * @param int $timestart The time they start
381 * @param int $timeend The time they end in this role
382 * @param string $enrol The type of enrolment this is
383 * @return bool
384 */
385function add_teacher($userid, $courseid, $editall=1, $role='', $timestart=0, $timeend=0, $enrol='manual') {
386 global $CFG;
387
388 if (!$user = get_record('user', 'id', $userid)) { // Check user
389 return false;
390 }
391
392 $capability = $editall ? 'moodle/legacy:editingteacher' : 'moodle/legacy:teacher';
393
394 if (!$roles = get_roles_with_capability($capability, CAP_ALLOW)) {
395 return false;
396 }
397
398 $role = array_shift($roles); // We can only use one, let's use the first one
399
400 if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
401 return false;
402 }
403
404 return role_assign($role->id, $user->id, 0, $context->id, $timestart, $timeend, 0, $enrol);
405}
406
407/**
408 * Removes a teacher from a given course (or ALL courses)
409 * Does not delete the user account
410 *
411 * @param int $courseid The id of the course that is being viewed, if any
412 * @param int $userid The id of the user that is being tested against.
413 * @return bool
414 */
415function remove_teacher($userid, $courseid=0) {
416 global $CFG;
417
c4d0753b 418 $roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW);
419
420 if ($roles) {
421 $roles += get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW);
c2da0757 422 } else {
423 $roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW);
c4d0753b 424 }
425
426 if (empty($roles)) {
427 return true;
428 }
429
430 $return = true;
431
432 if ($courseid) {
433
434 if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
435 return false;
436 }
437
438 /// First delete any crucial stuff that might still send mail
439 if ($forums = get_records('forum', 'course', $courseid)) {
440 foreach ($forums as $forum) {
441 delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid);
442 }
443 }
444
445 /// Next if the teacher is not registered as a student, but is
446 /// a member of a group, remove them from the group.
447 if (!isstudent($courseid, $userid)) {
448 if ($groups = get_groups($courseid, $userid)) {
449 foreach ($groups as $group) {
450 delete_records('groups_members', 'groupid', $group->id, 'userid', $userid);
451 }
452 }
453 }
454
455 foreach ($roles as $role) { // Unassign them from all the teacher roles
456 $newreturn = role_unassign($role->id, $userid, 0, $context->id);
457 if (empty($newreturn)) {
458 $return = false;
459 }
460 }
461
462 } else {
463 delete_records('forum_subscriptions', 'userid', $userid);
464 $return = true;
465 foreach ($roles as $role) { // Unassign them from all the teacher roles
466 $newreturn = role_unassign($role->id, $userid, 0, 0);
467 if (empty($newreturn)) {
468 $return = false;
469 }
470 }
471 }
472
473 return $return;
474}
475
476/**
477 * Add a creator to the site
478 *
479 * @param int $userid The id of the user that is being tested against.
480 * @return bool
481 */
482function add_creator($userid) {
483
484 if (!record_exists('user_admins', 'userid', $userid)) {
485 if (record_exists('user', 'id', $userid)) {
486 $creator->userid = $userid;
487 return insert_record('user_coursecreators', $creator);
488 }
489 return false;
490 }
491 return true;
492}
493
494/**
495 * Remove a creator from a site
496 *
497 * @uses $db
498 * @param int $userid The id of the user that is being tested against.
499 * @return bool
500 */
501function remove_creator($userid) {
502 global $db;
503
504 return delete_records('user_coursecreators', 'userid', $userid);
505}
506
507/**
508 * Add an admin to a site
509 *
510 * @uses SITEID
511 * @param int $userid The id of the user that is being tested against.
512 * @return bool
513 */
514function add_admin($userid) {
515
516 if (!record_exists('user_admins', 'userid', $userid)) {
517 if (record_exists('user', 'id', $userid)) {
518 $admin->userid = $userid;
519
520 // any admin is also a teacher on the site course
521 if (!record_exists('user_teachers', 'course', SITEID, 'userid', $userid)) {
522 if (!add_teacher($userid, SITEID)) {
523 return false;
524 }
525 }
526
527 return insert_record('user_admins', $admin);
528 }
529 return false;
530 }
531 return true;
532}
533
534/**
535 * Removes an admin from a site
536 *
537 * @uses $db
538 * @uses SITEID
539 * @param int $userid The id of the user that is being tested against.
540 * @return bool
541 */
542function remove_admin($userid) {
543 global $db;
544
545 // remove also from the list of site teachers
546 remove_teacher($userid, SITEID);
547
548 return delete_records('user_admins', 'userid', $userid);
549}
550
551
552
553function get_user_info_from_db($field, $value) { // For backward compatibility
554 return get_complete_user_data($field, $value);
555}
556
557?>