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
);
27 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
28 require_capability('moodle/course:managemetacourse', $context);
30 if (!$course->metacourse
) {
31 redirect("$CFG->wwwroot/course/view.php?id=$course->id");
34 $strassigncourses = get_string('metaassigncourses');
35 $stralreadycourses = get_string('metaalreadycourses');
36 $strnoalreadycourses = get_string('metanoalreadycourses');
37 $strpotentialcourses = get_string('metapotentialcourses');
38 $strnopotentialcourses = get_string('metanopotentialcourses');
39 $straddcourses = get_string('metaaddcourse');
40 $strremovecourse = get_string('metaremovecourse');
41 $strsearch = get_string("search");
42 $strsearchresults = get_string("searchresults");
43 $strcourses = get_string("courses");
44 $strshowall = get_string("showall");
46 print_header("$course->shortname: $strassigncourses",
48 build_navigation(array(array('name' => $strassigncourses, 'link' => null, 'type' => 'misc'))), "searchtext");
51 /// Print a help notice about the need to use this page
53 print_heading(get_string('childcourses'));
55 if (!$frm = data_submitted()) {
56 $note = get_string("importmetacoursenote");
57 print_simple_box($note, "center", "50%");
59 /// A form was submitted so process the input
62 if ($add and !empty($frm->addselect
) and confirm_sesskey()) {
63 $timestart = $timeend = 0;
64 foreach ($frm->addselect
as $addcourse) {
65 $addcourse = clean_param($addcourse, PARAM_INT
);
67 if (!add_to_metacourse($course->id
,$addcourse)) {
68 error("Could not add the selected course to this meta course!");
71 } else if ($remove and !empty($frm->removeselect
) and confirm_sesskey()) {
72 foreach ($frm->removeselect
as $removecourse) {
74 $removecourse = clean_param($removecourse, PARAM_INT
);
75 if (! remove_from_metacourse($course->id
,$removecourse)) {
76 error("Could not remove the selected course from this meta course!");
79 } else if ($showall and confirm_sesskey()) {
86 /// Get all existing students and teachers for this course.
87 if(! $alreadycourses = get_courses_in_metacourse($course->id
)) {
88 $alreadycourses = array();
94 /// Get search results excluding any users already in this course
95 if (($searchtext != '') and $previoussearch and confirm_sesskey()) {
96 if ($searchcourses = get_courses_search(explode(" ",$searchtext),'fullname ASC',0,99999,$numcourses)) {
97 foreach ($searchcourses as $tmp) {
98 if (array_key_exists($tmp->id
,$alreadycourses)) {
99 unset($searchcourses[$tmp->id
]);
101 if (!empty($tmp->metacourse
)) {
102 unset($searchcourses[$tmp->id
]);
105 if (array_key_exists($course->id
,$searchcourses)) {
106 unset($searchcourses[$course->id
]);
108 $numcourses = count($searchcourses);
112 /// If no search results then get potential students for this course excluding users already in course
113 if (empty($searchcourses)) {
114 $numcourses = count_courses_notin_metacourse($course->id
);
116 if ($numcourses > 0 and $numcourses <= MAX_COURSES_PER_PAGE
) {
117 $courses = get_courses_notin_metacourse($course->id
);
123 print_simple_box_start("center");
125 include('importstudents.html');
127 print_simple_box_end();