From 9407d4563f187cf82f9e15b3c350b7e983ff0f0f Mon Sep 17 00:00:00 2001 From: defacer Date: Sun, 23 Jan 2005 21:38:01 +0000 Subject: [PATCH] According to the final comments in SC#65: Made isteacher() require that the first parameter (course id) be specified and non-empty. If it is empty, [i.e., 0, which was used to simulate what has now become isteacherinanycourse()], then the return value IS correct but a warning is printed on screen. This should allow us to track down any such calls in legacy modules without breaking Moodle. The correct way to check for teacher status in ANY course is now to call isteacherinanycourse(). --- blocks/participants/block_participants.php | 2 +- lib/datalib.php | 2 +- lib/moodlelib.php | 31 ++++++++++++++++++---- mod/quiz/preview.php | 2 +- mod/quiz/quizfile.php | 4 +-- user/index.php | 2 +- user/view.php | 2 +- 7 files changed, 33 insertions(+), 12 deletions(-) diff --git a/blocks/participants/block_participants.php b/blocks/participants/block_participants.php index bc2e4aaa5a..476a09f87b 100644 --- a/blocks/participants/block_participants.php +++ b/blocks/participants/block_participants.php @@ -28,7 +28,7 @@ class block_participants extends block_base { $course = get_record('course', 'id', $this->instance->pageid); - if ($this->instance->pageid != SITEID || $CFG->showsiteparticipantslist > 1 || ($CFG->showsiteparticipantslist == 1 && isteacher()) || isteacher(SITEID)) { + if ($this->instance->pageid != SITEID || $CFG->showsiteparticipantslist > 1 || ($CFG->showsiteparticipantslist == 1 && isteacherinanycourse()) || isteacher(SITEID)) { $this->content->items[]=''.get_string('participants').''; $this->content->icons[]=''; } diff --git a/lib/datalib.php b/lib/datalib.php index c3c88e0ec8..bcea6cc210 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -2765,7 +2765,7 @@ function count_login_failures($mode, $username, $lastlogin) { $count->accounts = count_records_select('log', $select, 'COUNT(DISTINCT info)'); return $count; } - } else if ($mode == 'everybody' or ($mode == 'teacher' and isteacher())) { + } else if ($mode == 'everybody' or ($mode == 'teacher' and isteacherinanycourse())) { if ($count->attempts = count_records_select('log', $select .' AND info = \''. $username .'\'')) { return $count; } diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 675e2c0e2d..cbdf458cac 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -1079,20 +1079,25 @@ function isadmin($userid=0) { /** * Determines if a user is a teacher or an admin * - * @uses $USER + * @uses $USER * @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 boolean $includeadmin If true this function will return true when it encounters an admin user. * @return boolean * @todo Finish documenting this function */ -function isteacher($courseid=0, $userid=0, $includeadmin=true) { +function isteacher($courseid, $userid=0, $includeadmin=true) { global $USER; if ($includeadmin and isadmin($userid)) { // admins can do anything the teacher can return true; } + if (empty($courseid)) { + notify('isteacher() should not be used without a valid course id as argument'); + return isteacherinanycourse($userid, $includeadmin); + } + if (!$userid) { if ($courseid) { return !empty($USER->teacher[$courseid]); @@ -1103,11 +1108,27 @@ function isteacher($courseid=0, $userid=0, $includeadmin=true) { $userid = $USER->id; } - if (!$courseid) { - return record_exists('user_teachers', 'userid', $userid); + return record_exists('user_teachers', 'userid', $userid, 'course', $courseid); +} + +/** + * 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 boolean $includeadmin If true this function will return true when it encounters an admin user. + * @return boolean + * @todo Finish documenting this function + */ +function isteacherinanycourse($userid = 0, $includeadmin = true) { + if(empty($userid)) { + if(empty($USER) || empty($USER->id)) { + return false; + } + $userid = $USER->id; } - return record_exists('user_teachers', 'userid', $userid, 'course', $courseid); + return record_exists('user_teachers', 'userid', $userid); } /** diff --git a/mod/quiz/preview.php b/mod/quiz/preview.php index 292a874a97..af3bf42b75 100644 --- a/mod/quiz/preview.php +++ b/mod/quiz/preview.php @@ -47,7 +47,7 @@ $qtype = $question->qtype; require_login(); -if (!isteacher()) { +if (!isteacherinanycourse()) { error('This page is for teachers only'); } diff --git a/mod/quiz/quizfile.php b/mod/quiz/quizfile.php index f38c0f4374..a1ab10de66 100644 --- a/mod/quiz/quizfile.php +++ b/mod/quiz/quizfile.php @@ -40,10 +40,10 @@ ///////////////////////////////////// // Check access ///////////////////////////////////// - if ($quizid == 0) { // teache doing preview during quiz creation + if ($quizid == 0) { // teacher doing preview during quiz creation if ($questioncategory->publish) { require_login(); - if (!isteacher()) { + if (!isteacherinanycourse()) { error('No valid arguments supplied'); } } else { diff --git a/user/index.php b/user/index.php index dc26787a75..77629e442e 100644 --- a/user/index.php +++ b/user/index.php @@ -28,7 +28,7 @@ if (!$CFG->showsiteparticipantslist and !isteacher(SITEID)) { notice(get_string('sitepartlist0')); } - if ($CFG->showsiteparticipantslist < 2 and !isteacher()) { + if ($CFG->showsiteparticipantslist < 2 and !isteacherinanycourse()) { notice(get_string('sitepartlist1')); } } diff --git a/user/view.php b/user/view.php index 7e2fff741d..633ef04cfb 100644 --- a/user/view.php +++ b/user/view.php @@ -69,7 +69,7 @@ } if (!$course->category and !$currentuser) { // To reduce possibility of "browsing" userbase at site level - if (!isteacher() and !isteacher(0, $user->id) ) { // Teachers can browse and be browsed at site level + if (!isteacherinanycourse() and !isteacherinanycourse($user->id) ) { // Teachers can browse and be browsed at site level print_header("$personalprofile: ", "$personalprofile: ", "id\">$participants", "", "", true, " ", navmenu($course)); -- 2.39.5