Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: cf38360f58
Fetching contributors…

Cannot retrieve contributors at this time

file 199 lines (163 sloc) 7.369 kb
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
<?PHP // $Id$
      // Admin-only script to assign teachers to courses

require("../config.php");
require("../user/lib.php");

    optional_variable($id); // course id

    if (! $site = get_site()) {
        redirect("$CFG->wwwroot/admin/");
    }

    require_login();

    if (!isadmin()) {
        error("You must be an administrator to use this page.");
    }

    $strassignteachers = get_string("assignteachers");
    $stradministration = get_string("administration");
    $strexistingteachers = get_string("existingteachers");
    $strnoexistingteachers = get_string("noexistingteachers");
    $strpotentialteachers = get_string("potentialteachers");
    $strnopotentialteachers = get_string("nopotentialteachers");
    $straddteacher = get_string("addteacher");
    $strremoveteacher = get_string("removeteacher");
    $strsearch = get_string("search");
    $strsearchagain = get_string("searchagain");
    $strtoomanytoshow = get_string("toomanytoshow");

    if (!$id) {
print_header("$site->fullname: $strassignteachers", "$site->fullname",
                     "<A HREF=\"$CFG->wwwroot/admin\">$stradministration</A> -> $strassignteachers");

        if ($courses = get_records_sql("SELECT * from course WHERE category > 0 ORDER BY fullname")) {

            print_heading(get_string("choosecourse"));
            print_simple_box_start("CENTER");
            foreach ($courses as $course) {
                echo "<A HREF=\"teacher.php?id=$course->id\">$course->fullname</A><BR>";
}
print_simple_box_end();
} else {
print_heading(get_string("nocoursesyet"));
print_continue("$CFG->wwwroot/admin/");
}
print_footer();
exit;
}

if (! $course = get_record("course", "id", $id)) {
error("Course ID was incorrect (can't find it)");
}


print_header("$site->fullname: $course->shortname: $strassignteachers",
"$site->fullname",
"<A HREF=\"$CFG->wwwroot/admin\">$stradministration</A> ->
<A HREF=\"teacher.php\">$strassignteachers</A> ->
$course->shortname", "");
print_heading("<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</A>");


/// Get all existing teachers for this course.
$teachers = get_records_sql("SELECT u.*,t.authority,t.id as teachid FROM user u, user_teachers t
WHERE t.course = '$course->id'
AND t.user = u.id
ORDER BY t.authority ASC");

/// Add a teacher if one is specified

if ($add) {
if (! $user = get_record("user", "id", $add)) {
error("That teacher (id = $add) doesn't exist", "teacher.php?id=$course->id");
}

if ($teachers) {
foreach ($teachers as $tt) {
if ($tt->id == $user->id) {
error("That user is already a teacher for this course.", "teacher.php?id=$course->id");
}
}
}

$teacher->user = $user->id;
$teacher->course = $course->id;
if ($teachers) {
$teacher->authority = 2;
} else {
$teacher->authority = 1; // First teacher is the main teacher
}
$teacher->id = insert_record("user_teachers", $teacher);
if (! $teacher->id) {
error("Could not add that teacher to this course!");
}
$user->authority = $teacher->authority;
$teachers[] = $user;
}

/// Remove a teacher if one is specified.

if ($remove) {
if (! $user = get_record("user", "id", $remove)) {
error("That teacher (id = $remove) doesn't exist", "teacher.php?id=$course->id");
}
if ($teachers) {
foreach ($teachers as $tt) {
if ($tt->id == $user->id) {
delete_records("user_teachers", "id", "$tt->teachid");
}
}
}
$teachers = get_records_sql("SELECT u.*,t.authority,t.id as teachid FROM user u, user_teachers t
WHERE t.course = '$course->id'
AND t.user = u.id
ORDER BY t.authority ASC");
}


/// Print the lists of existing and potential teachers

echo "<TABLE CELLPADDING=2 CELLSPACING=10 ALIGN=CENTER>";
echo "<TR><TH WIDTH=50%>$strexistingteachers</TH><TH WIDTH=50%>$strpotentialteachers</TH></TR>";
echo "<TR><TD WIDTH=50% NOWRAP VALIGN=TOP>";

/// First, show existing teachers for this course

if (! $teachers) {
echo "<P ALIGN=CENTER>$strnoexistingteachers</A>";

} else {
foreach ($teachers as $teacher) {
echo "<P ALIGN=right>$teacher->firstname $teacher->lastname, $teacher->email &nbsp;&nbsp; <A HREF=\"teacher.php?id=$course->id&remove=$teacher->id\" TITLE=\"$strremoveteacher\"><IMG SRC=\"../pix/t/right.gif\" BORDER=0></A></P>";
}
}

echo "<TD WIDTH=50% NOWRAP VALIGN=TOP>";

/// Print list of potential teachers

if ($search) {
$users = get_records_sql("SELECT * from user WHERE confirmed = 1
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 username <> 'guest' AND username <> 'changeme'");
}

if ($users) {
foreach ($users as $user) { // Remove users who are already teachers
if ($teachers) {
foreach ($teachers as $teacher) {
if ($teacher->id == $user->id) {
continue 2;
}
}
}
$potential[] = $user;
}
}

if (! $potential) {
echo "<P ALIGN=CENTER>$strnopotentialteachers</A>";
if ($search) {
echo "<FORM ACTION=teacher.php METHOD=GET>";
echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
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=\"teacher.php?id=$course->id&add=$user->id\" TITLE=\"$straddteacher\"><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=teacher.php METHOD=GET>";
            echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
echo "<INPUT TYPE=text NAME=search SIZE=20>";
echo "<INPUT TYPE=submit VALUE=\"$strsearch\">";
echo "</FORM>";
}
}

echo "</TR></TABLE>";

print_footer();

?>
Something went wrong with that request. Please try again.