Non-editing teachers shouldn't be able to assign students
[moodle.git] / course / student.php
blob416f1bb61eabc8d3d81ce0f422dba76ca893d55f
1 <?PHP // $Id$
2 // Script to assign students to courses
4 require_once("../config.php");
6 define("MAX_USERS_PER_PAGE", 50);
8 require_variable($id); // course id
9 optional_variable($add, "");
10 optional_variable($remove, "");
11 optional_variable($search, ""); // search string
13 if (! $site = get_site()) {
14 redirect("$CFG->wwwroot/$CFG->admin/index.php");
17 if (! $course = get_record("course", "id", $id)) {
18 error("Course ID was incorrect (can't find it)");
21 require_login($course->id);
23 if (!isteacheredit($course->id)) {
24 error("You must be an editing teacher in this course, or an admin");
27 $strassignstudents = get_string("assignstudents");
28 $strexistingstudents = get_string("existingstudents");
29 $strnoexistingstudents = get_string("noexistingstudents");
30 $strpotentialstudents = get_string("potentialstudents");
31 $strnopotentialstudents = get_string("nopotentialstudents");
32 $straddstudent = get_string("addstudent");
33 $strremovestudent = get_string("removestudent");
34 $strsearch = get_string("search");
35 $strsearchresults = get_string("searchresults");
36 $strsearchagain = get_string("searchagain");
37 $strtoomanytoshow = get_string("toomanytoshow");
38 $strstudents = get_string("students");
40 if ($search) {
41 $searchstring = $strsearchagain;
42 } else {
43 $searchstring = $strsearch;
46 if ($course->students != $strstudents) {
47 $parastudents = " ($course->students)";
48 } else {
49 $parastudents = "";
52 print_header("$course->shortname: $strassignstudents",
53 "$site->fullname",
54 "<a href=\"view.php?id=$course->id\">$course->shortname</a> -> $strassignstudents", "");
56 /// Add a student if one is specified
58 if (!empty($add)) {
59 if (! enrol_student($add, $course->id)) {
60 error("Could not add that student to this course!");
64 /// Remove a student if one is specified.
66 if (!empty($remove)) {
67 if (! unenrol_student($remove, $course->id)) {
68 error("Could not remove that student from this course!");
72 /// Print a help notice about the need to use this page
74 if (empty($add) and empty($remove) and empty($search)) {
75 $note = get_string("assignstudentsnote");
76 if ($course->password) {
77 $note .= "<p>".get_string("assignstudentspass", "", "<a href=\"edit.php?id=$course->id\">$course->password</a>");
79 print_simple_box($note, "center", "50%");
82 /// Get all existing students for this course.
83 $students = get_course_students($course->id, "u.lastname ASC, u.firstname ASC");
85 /// Print the lists of existing and potential students
87 echo "<table cellpadding=1 cellspacing=5 align=center>";
88 echo "<tr><th width=50%>$strexistingstudents$parastudents</th><td>&nbsp;</td><th width=50%>$strpotentialstudents</th></tr>";
89 echo "<tr><td width=50% nowrap valign=top>";
91 /// First, show existing students for this course
93 if (empty($students)) {
94 echo "<p align=center>$strnoexistingstudents</a>";
95 $studentlist = "";
97 } else {
98 $studentarray = array();
99 foreach ($students as $student) {
100 $studentarray[] = $student->id;
101 $fullname = fullname($student, true);
102 echo "<p align=right>$fullname, $student->email &nbsp;&nbsp; <a href=\"student.php?id=$course->id&remove=$student->id\" title=\"$strremovestudent\"><img src=\"../pix/t/right.gif\" border=0></a></p>";
104 $studentlist = implode(",",$studentarray);
105 unset($studentarray);
108 echo "<td>&nbsp;</td>";
109 echo "<td width=50% nowrap valign=top>";
111 /// Print list of potential students
113 $usercount = get_users(false, $search, true, $studentlist, "lastname ASC, firstname ASC");
115 if ($usercount == 0) {
116 echo "<p align=center>$strnopotentialstudents</p>";
118 } else if ($usercount > MAX_USERS_PER_PAGE) {
119 echo "<p align=center>$strtoomanytoshow ($usercount) </p>";
121 } else {
123 if ($search) {
124 echo "<p align=center>($strsearchresults : $search)</p>";
127 if (!$users = get_users(true, $search, true, $studentlist)) {
128 error("Could not get users!");
131 foreach ($users as $user) {
132 $fullname = fullname($user, true);
133 echo "<p align=left><a href=\"student.php?id=$course->id&add=$user->id\"".
134 "title=\"$straddstudent\"><img src=\"../pix/t/left.gif\"".
135 "border=0></a>&nbsp;&nbsp;$fullname, $user->email";
139 if ($search or $usercount > MAX_USERS_PER_PAGE) {
140 echo "<form action=student.php method=post>";
141 echo "<input type=hidden name=id value=\"$course->id\">";
142 echo "<input type=text name=search size=20>";
143 echo "<input type=submit value=\"$searchstring\">";
144 echo "</form>";
147 echo "</tr></table>";
149 print_footer();