diff --git a/course/importstudents.php b/course/importstudents.php index 91f61173552..6e9d820a7e1 100644 --- a/course/importstudents.php +++ b/course/importstudents.php @@ -110,13 +110,12 @@ /// If no search results then get potential students for this course excluding users already in course if (empty($searchcourses)) { + $numcourses = count_courses_notin_metacourse($course->id); - $numcourses = get_courses_notin_metacourse($course->id,true); - - $courses = array(); - - if ($numcourses <= MAX_COURSES_PER_PAGE) { + if ($numcourses > 0 and $numcourses <= MAX_COURSES_PER_PAGE) { $courses = get_courses_notin_metacourse($course->id); + } else { + $courses = array(); } } diff --git a/lib/datalib.php b/lib/datalib.php index e0e11364ba2..eff8d5d9e73 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -97,6 +97,22 @@ function get_courses_notin_metacourse($metacourseid,$count=false) { return get_records_sql($sql); } +function count_courses_notin_metacourse($metacourseid) { + global $CFG; + + $alreadycourses = get_courses_in_metacourse($metacourseid); + + $sql = "SELECT 1, COUNT(c.id) AS notin FROM {$CFG->prefix}course c + WHERE ".((!empty($alreadycourses)) ? "c.id NOT IN (".implode(',',array_keys($alreadycourses)).") + AND " : "")." c.id !=$metacourseid and c.id != ".SITEID." and c.metacourse != 1"; + + if (!$result = get_records_sql($sql)) { + return 0; + } + + return $result[1]->notin; +} + /** * Search through course users *