2 // script to assign students to a meta course by selecting which courses the meta course comprises.
3 // this is basically a hack of student.php that uses courses instead.
5 require_once("../config.php");
6 require_once("lib.php");
8 define("MAX_COURSES_PER_PAGE", 1000);
10 $id = required_param('id',PARAM_INT
); // course id
11 $add = optional_param('add', 0, PARAM_BOOL
);
12 $remove = optional_param('remove', 0, PARAM_BOOL
);
13 $showall = optional_param('showall', 0, PARAM_BOOL
);
14 $searchtext = optional_param('searchtext', '', PARAM_RAW
); // search string
15 $previoussearch = optional_param('previoussearch', 0, PARAM_BOOL
);
16 $previoussearch = ($searchtext != '') or ($previoussearch) ?
1:0;
18 if (! $site = get_site()) {
19 redirect("$CFG->wwwroot/$CFG->admin/index.php");
22 if (! $course = get_record("course", "id", $id)) {
23 error("Course ID was incorrect (can't find it)");
26 require_login($course->id
);
28 if (!$course->metacourse
) {
29 redirect("$CFG->wwwroot/course/student.php?id=$course->id");
32 if (!isadmin() && !isteacheredit($course->id
)) {
33 error("You must be an admin or a teacher of this course");
37 $strassigncourses = get_string('metaassigncourses');
38 $stralreadycourses = get_string('metaalreadycourses');
39 $strnoalreadycourses = get_string('metanoalreadycourses');
40 $strpotentialcourses = get_string('metapotentialcourses');
41 $strnopotentialcourses = get_string('metanopotentialcourses');
42 $straddcourses = get_string('metaaddcourse');
43 $strremovecourse = get_string('metaremovecourse');
44 $strsearch = get_string("search");
45 $strsearchresults = get_string("searchresults");
46 $strcourses = get_string("courses");
47 $strshowall = get_string("showall");
49 print_header("$course->shortname: $strassigncourses",
51 "<a href=\"view.php?id=$course->id\">$course->shortname</a> -> $strassigncourses",
52 "studentform.searchtext");
54 /// Don't allow restricted teachers to even see this page (because it contains
55 /// a lot of email addresses and access to all student on the server
57 check_for_restricted_user($USER->username
, "$CFG->wwwroot/course/view.php?id=$course->id");
59 /// Print a help notice about the need to use this page
61 if (!$frm = data_submitted()) {
62 $note = get_string("importmetacoursenote");
63 print_simple_box($note, "center", "50%");
65 /// A form was submitted so process the input
68 if ($add and !empty($frm->addselect
) and confirm_sesskey()) {
69 $timestart = $timeend = 0;
70 foreach ($frm->addselect
as $addcourse) {
71 $addcourse = clean_param($addcourse, PARAM_INT
);
73 if (!add_to_metacourse($course->id
,$addcourse)) {
74 error("Could not add the selected course to this meta course!");
77 } else if ($remove and !empty($frm->removeselect
) and confirm_sesskey()) {
78 foreach ($frm->removeselect
as $removecourse) {
80 $removecourse = clean_param($removecourse, PARAM_INT
);
81 if (! remove_from_metacourse($course->id
,$removecourse)) {
82 error("Could not remove the selected course from this meta course!");
85 } else if ($showall and confirm_sesskey()) {
92 /// Get all existing students and teachers for this course.
93 if(! $alreadycourses = get_courses_in_metacourse($course->id
)) {
94 $alreadycourses = array();
100 /// Get search results excluding any users already in this course
101 if (($searchtext != '') and $previoussearch and confirm_sesskey()) {
102 if ($searchcourses = get_courses_search(explode(" ",$searchtext),'fullname ASC',0,99999,$numcourses)) {
103 foreach ($searchcourses as $tmp) {
104 if (array_key_exists($tmp->id
,$alreadycourses)) {
105 unset($searchcourses[$tmp->id
]);
107 if (!empty($tmp->metacourse
)) {
108 unset($searchcourses[$tmp->id
]);
111 if (array_key_exists($course->id
,$searchcourses)) {
112 unset($searchcourses[$course->id
]);
114 $numcourses = count($searchcourses);
118 /// If no search results then get potential students for this course excluding users already in course
119 if (empty($searchcourses)) {
121 $numcourses = get_courses_notin_metacourse($course->id
,true);
125 if ($numcourses <= MAX_COURSES_PER_PAGE
) {
126 $courses = get_courses_notin_metacourse($course->id
);
131 print_simple_box_start("center");
133 include('importstudents.html');
135 print_simple_box_end();