]> git.mjollnir.org Git - moodle.git/commitdiff
Modifications from Greg Barnett at Crown College to add more admins.
authormoodler <moodler>
Wed, 11 Dec 2002 14:59:37 +0000 (14:59 +0000)
committermoodler <moodler>
Wed, 11 Dec 2002 14:59:37 +0000 (14:59 +0000)
See http://bugs.moodle.org/bug.php?op=show&bugid=202

admin/admin.php [new file with mode: 0644]
admin/index.php
course/lib.php
lang/en/moodle.php
lib/moodlelib.php

diff --git a/admin/admin.php b/admin/admin.php
new file mode 100644 (file)
index 0000000..d2d32b3
--- /dev/null
@@ -0,0 +1,184 @@
+<?PHP // $Id$
+      // Admin-only script to assign administrative rights to users
+      // !!! based on ../course/teacher.php (cut and pasted, then mangled)
+
+       require("../config.php");
+
+    # sanity checks
+    assert("!ereg('[^0-9]', \$_REQUEST['add'])");
+    assert("!ereg('[^0-9]', \$_REQUEST['remove'])");
+
+    if (! $site = get_site()) {
+        redirect("$CFG->wwwroot/admin/");
+    }
+
+    require_login();
+
+    if (!isadmin()) {
+        error("You must be an administrator to use this page.");
+    }
+
+    $primaryadmin = get_admin();
+
+/// If you want any administrator to have the ability to assign admin
+/// rights, then comment out the following if statement
+    if ($primaryadmin->id != $USER->id) {
+        error("You must be the primary administrator to use this page.");
+    }
+
+/// assign all of the configurable language strings
+    $stringstoload = array (
+        "assignadmins",
+        "administration",
+        "existingadmins",
+        "noexistingadmins",
+        "potentialadmins",
+        "nopotentialadmins",
+        "addadmin",
+        "removeadmin",
+        "search",
+        "searchagain",
+        "toomanytoshow",
+        );
+
+    foreach ($stringstoload as $stringtoload){
+        $strstringtoload = "str" . $stringtoload;
+        $$strstringtoload = get_string($stringtoload);
+    }
+
+       print_header("$site->shortname: $course->shortname: $strassignadmins", 
+                 "$site->fullname", 
+                 "<A HREF=\"$CFG->wwwroot/admin\">$stradministration</A> -> 
+                  <A HREF=\"{$_SERVER['PHP_SELF']}\">$strassignadmins</A>", "");
+
+/// Get all existing admins
+    $admins = get_admins();
+
+/// Add an admin if one is specified
+    if ($_REQUEST['add']) {
+        $user = @get_record("user", "id", $_REQUEST['add']) or
+            error("That account (id = {$_REQUEST['add']}) doesn't exist");
+
+        if ($admins) {
+            foreach ($admins as $aa) {
+                if ($aa->id == $user->id) {
+                    error("That user is already an admin.");
+                }            
+            }
+        }
+
+        $admin->user = $user->id;
+        $admin->id = insert_record("user_admins", $admin);
+        $admins[] = $user;
+    }
+
+/// Remove an admin if one is specified.
+    if ($_REQUEST['remove']) {
+
+        $user = @get_record("user", "id", $_REQUEST['remove']) or 
+            error("That account (id = {$_REQUEST['remove']}) doesn't exist");
+
+        if ($admins) {
+            foreach ($admins as $key => $aa) {
+                if ($aa->id == $user->id) {
+                    /// make sure that we don't delete the primary admin
+                    /// account, so that there is always at least on admin
+                    if ($aa->id == $primaryadmin->id) {
+                        error("That user is the primary admin, and shouldn't be removed.");
+                    } else {
+                        remove_admin($user->id);
+                        unset($admins[$key]);
+                    }
+                } 
+            }
+        }
+    }
+
+
+/// Print the lists of existing and potential admins
+    echo "<TABLE CELLPADDING=2 CELLSPACING=10 ALIGN=CENTER>";
+    echo "<TR><TH WIDTH=50%>$strexistingadmins</TH><TH WIDTH=50%>$strpotentialadmins</TH></TR>";
+    echo "<TR><TD WIDTH=50% NOWRAP VALIGN=TOP>";
+
+/// First, show existing admins
+
+    if (! $admins) { 
+        echo "<P ALIGN=CENTER>$strnoexistingadmins</A>";
+
+    } else {
+        foreach ($admins as $admin) {
+            echo "<P ALIGN=right>$admin->firstname $admin->lastname,
+            $admin->email &nbsp;&nbsp; ";
+            if ($primaryadmin->id == $admin->id){
+                print_spacer(10, 9, false);
+            } else {
+                echo "<A HREF=\"{$_SERVER['PHP_SELF']}?remove=$admin->id\"
+                TITLE=\"$strremoveadmin\"><IMG SRC=\"../pix/t/right.gif\"
+                BORDER=0></A>";
+            }
+            echo "</P>";
+        }
+    }
+
+    echo "<TD WIDTH=50% NOWRAP VALIGN=TOP>";
+
+/// Print list of potential admins
+
+    if ($search) {
+        $users = get_records_sql("SELECT * from user WHERE confirmed = 1 AND deleted = 0
+                                  AND (firstname LIKE '%$search%' OR 
+                                       lastname LIKE '%$search%' OR 
+                                       email LIKE '%$search%')
+                                  AND username <> 'guest' AND username <> 'changeme'");
+    } else {
+        $users = get_records_sql("SELECT * from user WHERE confirmed = 1 AND deleted = 0
+                                  AND username <> 'guest' AND username <> 'changeme'");
+    }
+
+    
+    if ($users) {
+        foreach ($users as $user) {  // Remove users who are already admins
+            if ($admins) {
+                foreach ($admins as $admin) {
+                    if ($admin->id == $user->id) {
+                        continue 2;
+                    }
+                }
+            }
+            $potential[] = $user;
+        }
+    }
+
+    if (! $potential) { 
+        echo "<P ALIGN=CENTER>$strnopotentialadmins</A>";
+        if ($search) {
+            echo "<FORM ACTION={$_SERVER['PHP_SELF']} METHOD=POST>";
+            echo "<INPUT TYPE=text NAME=search SIZE=20>";
+            echo "<INPUT TYPE=submit VALUE=\"$strsearchagain\">";
+            echo "</FORM>";
+        }
+
+    } else {
+        if ($search) {
+            echo "<P ALIGN=CENTER>($strsearchresults)</P>";
+        }
+        if (count($potential) <= 20) {
+            foreach ($potential as $user) {
+                echo "<P ALIGN=LEFT><A HREF=\"{$_SERVER['PHP_SELF']}?add=$user->id\"
+                TITLE=\"$straddadmin\"><IMG SRC=\"../pix/t/left.gif\" BORDER=0></A>&nbsp;&nbsp;$user->firstname $user->lastname, $user->email";
+            }
+        } else {
+            echo "<P ALIGN=CENTER>There are too many users to show.<BR>";
+            echo "Enter a search word here.";
+            echo "<FORM ACTION={$_SERVER['PHP_SELF']} METHOD=POST>";
+            echo "<INPUT TYPE=text NAME=search SIZE=20>";
+            echo "<INPUT TYPE=submit VALUE=\"$strsearch\">";
+            echo "</FORM>";
+        }
+    }
+
+    echo "</TR></TABLE>";
+
+    print_footer();
+
+?>
index e1649a3a0566ed3c104131825dec7e74858f3a32..d0932763784973a5425cc0447107a85ddc2449ab 100644 (file)
                          "<P><A HREF=\"../course/categories.php\">".get_string("categories")."</A></P>";
     $table->data[0][2] = "<P><A HREF=\"user.php?newuser=true\">".get_string("addnewuser")."</A></P>".
                          "<P><A HREF=\"user.php\">".get_string("edituser")."</A></P>".
+                         "<P><A HREF=\"admin.php\">".get_string("assignadmins")."</A></P>".
                          "<P><A HREF=\"auth.php\">".get_string("authentication")."</A></P>";
 
     print_table($table);
index 9bee070e1f030bd5b5e1a97f6cbcc2e1999884e4..2aa0eae4002e9f4f8b337690ef851aea38b63657 100644 (file)
@@ -625,6 +625,8 @@ function print_admin_links ($siteid, $width=180) {
     $modicon[]=$icon;
     $moddata[]="<A HREF=\"$CFG->wwwroot/admin/user.php\">".get_string("edituser")."</A>";
     $modicon[]=$icon;
+    $moddata[]="<A HREF=\"$CFG->wwwroot/admin/admin.php\">".get_string("assignadmins")."</A>";
+    $modicon[]=$icon;
     $moddata[]="<A HREF=\"$CFG->wwwroot/admin/auth.php\">".get_string("authentication")."</A>";
     $modicon[]=$icon;
     $fulladmin = "<P><A HREF=\"$CFG->wwwroot/admin/\">".get_string("admin")."</A>...";
index 08ad13e9649ce0ebafbba14442c54bc296ed0b2e..8e0ada0bd8cd90a81a7fb71fc7518a8b33ab05f3 100644 (file)
@@ -14,6 +14,7 @@ $string['addinganewto'] = "Adding a new \$a->what to \$a->to";
 $string['addnewcourse'] = "Add a new course";
 $string['addnewuser'] = "Add a new user";
 $string['address'] = "Address";
+$string['addadmin'] = "Add admin";
 $string['addteacher'] = "Add teacher";
 $string['admin'] = "Admin";
 $string['administration'] = "Administration";
@@ -30,6 +31,7 @@ $string['alphanumerical'] = "Can only contain alphabetical letters or numbers";
 $string['alreadyconfirmed'] = "Registration has already been confirmed";
 $string['answer'] = "Answer";
 $string['assessment'] = "Assessment";
+$string['assignadmins'] = "Assign admins";
 $string['assignteachers'] = "Assign teachers";
 $string['authentication'] = "Authentication";
 $string['availablecourses'] = "Available Courses";
@@ -159,6 +161,7 @@ $string['enrolmentkeyhint'] = "That enrolment key was incorrect, please try agai
 $string['entercourse'] = "Click to enter this course";
 $string['enteremailaddress'] = "Enter in your email address to reset your 
    password and have the new password sent to you via email.";
+$string['existingadmins'] = "Existing admins";
 $string['existingteachers'] = "Existing teachers";
 $string['error'] = "Error";
 $string['feedback'] = "Feedback";
@@ -355,11 +358,13 @@ $string['newsitemsnumber'] = "News items to show";
 $string['never'] = "Never";
 $string['no'] = "No";
 $string['nocoursesyet'] = "No courses in this category";
+$string['noexistingadmins'] = "No existing admins, this is a serious error and you should never have seen this message.";
 $string['noexistingteachers'] = "No existing teachers";
 $string['nofilesyet'] = "No files have been uploaded to your course yet";
 $string['nograde'] = "No grade";
 $string['noimagesyet'] = "No images have been uploaded to your course yet";
 $string['none'] = "None";
+$string['nopotentialadmins'] = "No potential admins";
 $string['nopotentialteachers'] = "No potential teachers";
 $string['normal'] = "Normal";
 $string['nostudentsyet'] = "No students enrolled in this course yet";
@@ -395,6 +400,7 @@ $string['passwordsenttext'] = "
 $string['people'] = "People";
 $string['personalprofile'] = "Personal profile";
 $string['phone'] = "Phone";
+$string['potentialadmins'] = "Potential admins";
 $string['potentialteachers'] = "Potential teachers";
 $string['preferredlanguage'] = "Preferred language";
 $string['preview'] = "Preview";
@@ -402,6 +408,7 @@ $string['previeworchoose'] = "Preview or choose a theme";
 $string['question'] = "Question";
 $string['readme'] = "README";   // This is a file name
 $string['recentactivity'] = "Recent activity";
+$string['removeadmin'] = "Remove admin";
 $string['removeteacher'] = "Remove teacher";
 $string['rename'] = "Rename";
 $string['resources'] = "Resources";
index a1a90bbc2bf189cfa70fa1dce6bc73520a37c9fe..afaf74c6c4475001a2210c12dd5b960048031f06 100644 (file)
@@ -1339,7 +1339,7 @@ function isadmin($userid=0) {
     global $USER;
 
     if (!$userid) {
-        return $USER->admin;
+        return record_exists_sql("SELECT * FROM user_admins WHERE user='{$USER->id}'");
     }
 
     return record_exists_sql("SELECT * FROM user_admins WHERE user='$userid'");
@@ -1519,6 +1519,13 @@ function get_admin () {
     }
 }
 
+function get_admins() {
+    return get_records_sql("SELECT u.* FROM user u, user_admins a
+                            WHERE a.user = u.id
+                            ORDER BY u.id ASC");
+}
+
+
 function get_teacher($courseid) {
 // Returns $user object of the main teacher for a course
     if ( $teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t