From a328425d17a3539c0b3590a91cf93752824f71fb Mon Sep 17 00:00:00 2001 From: moodler Date: Tue, 4 Nov 2003 12:09:52 +0000 Subject: [PATCH] New feature based on code from Gustav Delius -> A-Z subsets on the participant list. http://moodle.org/mod/forum/discuss.php?d=3162 I also made a new datalib function called count_course_students() --- lang/en/moodle.php | 2 ++ lib/datalib.php | 52 ++++++++++++++++++++++++++++-- user/index.php | 79 +++++++++++++++++++++++++++++++++++++++------- 3 files changed, 119 insertions(+), 14 deletions(-) diff --git a/lang/en/moodle.php b/lang/en/moodle.php index f1cdb560f5..1105e226eb 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -56,6 +56,7 @@ $string['allow'] = "Allow"; $string['allowguests'] = "This course allows guest users to enter"; $string['allownot'] = "Do not allow"; $string['allparticipants'] = "All participants"; +$string['alphabet'] = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; $string['alphanumerical'] = "Can only contain alphabetical letters or numbers"; $string['alreadyconfirmed'] = "Registration has already been confirmed"; $string['answer'] = "Answer"; @@ -565,6 +566,7 @@ $string['nopotentialcreators'] = "No potential course creators"; $string['nopotentialstudents'] = "No potential students"; $string['nopotentialteachers'] = "No potential teachers"; $string['normal'] = "Normal"; +$string['nostudentsfound'] = "No \$a found"; $string['nostudentsyet'] = "No students enrolled in this course yet"; $string['nosuchemail'] = "No such email address"; $string['notavailable'] = "Not available"; diff --git a/lib/datalib.php b/lib/datalib.php index 436a22b8c3..c5dc1ba7f4 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -1112,29 +1112,77 @@ function get_recent_enrolments($courseid, $timestart) { * * @param type description */ -function get_course_students($courseid, $sort="s.timeaccess", $dir="", $page=0, $recordsperpage=99999) { +function get_course_students($courseid, $sort="s.timeaccess", $dir="", $page=0, $recordsperpage=99999, + $firstinitial="", $lastinitial="") { global $CFG; switch ($CFG->dbtype) { case "mysql": $limit = "LIMIT $page,$recordsperpage"; + $LIKE = "LIKE"; break; case "postgres7": $limit = "LIMIT $recordsperpage OFFSET ".($page); + $LIKE = "ILIKE"; break; default: $limit = "LIMIT $recordsperpage,$page"; + $LIKE = "ILIKE"; + } + + $select = "s.course = '$courseid' AND s.userid = u.id AND u.deleted = '0' "; + + if ($firstinitial) { + $select .= " AND u.firstname $LIKE '$firstinitial%' "; + } + + if ($lastinitial) { + $select .= " AND u.lastname $LIKE '$lastinitial%' "; } return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.email, u.city, u.country, u.lastlogin, u.picture, s.timeaccess as lastaccess FROM {$CFG->prefix}user u, {$CFG->prefix}user_students s - WHERE s.course = '$courseid' AND s.userid = u.id AND u.deleted = '0' + WHERE $select ORDER BY $sort $dir $limit"); } +/** +* Counts the students in a given course, or a subset of them +* +* @param type description +*/ +function count_course_students($course, $search="", $firstinitial="", $lastinitial="") { + + global $CFG; + + switch ($CFG->dbtype) { + case "mysql": + $LIKE = "LIKE"; + break; + default: + $LIKE = "ILIKE"; + } + + $select = "s.course = '$course->id' AND s.userid = u.id AND u.deleted = '0'"; + + if ($search) { + $select .= " AND u.firstname $LIKE '%$search%' OR u.lastname $LIKE '%$search%'"; + } + if ($firstinitial) { + $select .= " AND u.firstname $LIKE '$firstinitial%'"; + } + if ($lastinitial) { + $select .= " AND u.lastname $LIKE '$lastinitial%'"; + } + + return count_records_sql("SELECT COUNT(*) FROM {$CFG->prefix}user u,{$CFG->prefix}user_students s WHERE $select"); + +} + + /** * Returns list of all teachers in this course * diff --git a/user/index.php b/user/index.php index d4d2750f1d..8cfc2a2382 100644 --- a/user/index.php +++ b/user/index.php @@ -9,6 +9,8 @@ optional_variable($sort, "lastaccess"); //how to sort students optional_variable($dir,"desc"); //how to sort students optional_variable($page, "0"); // which page to show + optional_variable($lastinitial, ""); // only show students with this last initial + optional_variable($firstinitial, ""); // only show students with this first initial optional_variable($perpage, "20"); // how many per page @@ -38,6 +40,7 @@ $string->mins = get_string("mins"); $string->sec = get_string("sec"); $string->secs = get_string("secs"); + $string->all = get_string("all"); $countries = get_list_of_countries(); @@ -78,28 +81,80 @@ $dsort = "u.$sort"; } - $totalcount = count_records("user_students", "course", $course->id); + $students = get_course_students($course->id, $dsort, $dir, $page*$perpage, + $perpage, $firstinitial, $lastinitial); + + $totalcount = $matchcount = count_records("user_students", "course", $course->id); echo "

$totalcount $course->students

"; - if ($CFG->longtimenosee < 500) { + if (($CFG->longtimenosee < 500) and (!$page) and ($sort == "lastaccess")) { echo "

("; print_string("unusedaccounts","",$CFG->longtimenosee); echo ")

"; } - if (0 < $totalcount and $totalcount < USER_SMALL_CLASS) { // Print simple listing + /// Print paging bars if necessary - if ($students = get_course_students($course->id, $dsort, $dir)) { - foreach ($students as $student) { - print_user($student, $course, $string, $countries); + if ($totalcount > $perpage) { + $alphabet = explode(',', get_string('alphabet')); + + /// Bar of first initials + + echo "

"; + echo get_string("firstname")." : "; + if ($firstinitial) { + echo " id&sort=firstname&dir=ASC&". + "perpage=$perpage&lastinitial=$lastinitial\">$string->all "; + } else { + echo " $string->all "; + } + foreach ($alphabet as $letter) { + if ($letter == $firstinitial) { + echo " $letter "; + } else { + echo " id&sort=firstname&dir=ASC&". + "perpage=$perpage&lastinitial=$lastinitial&firstinitial=$letter\">$letter "; } } + echo "
"; - } else if ($students = get_course_students($course->id, $dsort, $dir, $page*$perpage, $perpage)) { + /// Bar of last initials - print_paging_bar($totalcount, $page, $perpage, - "index.php?id=$course->id&sort=$sort&dir=$dir&perpage=$perpage&"); + echo get_string("lastname")." : "; + if ($lastinitial) { + echo " id&sort=lastname&dir=ASC&". + "perpage=$perpage&firstinitial=$firstinitial\">$string->all "; + } else { + echo " $string->all "; + } + foreach ($alphabet as $letter) { + if ($letter == $lastinitial) { + echo " $letter "; + } else { + echo " id&sort=lastname&dir=ASC&". + "perpage=$perpage&firstinitial=$firstinitial&lastinitial=$letter\">$letter "; + } + } + echo "

"; + echo "
"; + + $matchcount = count_course_students($course, "", $firstinitial, $lastinitial); + + print_paging_bar($matchcount, $page, $perpage, + "index.php?id=$course->id&sort=$sort&dir=$dir&perpage=$perpage&firstinitial=$firstinitial&lastinitial=$lastinitial&"); + + } + + if ($matchcount == 0) { + print_heading(get_string("nostudentsfound", "", $course->students)); + + } if (0 < $matchcount and $matchcount < USER_SMALL_CLASS) { // Print simple listing + foreach ($students as $student) { + print_user($student, $course, $string, $countries); + } + + } else if ($matchcount > 0) { // Print one big table with abbreviated info $columns = array("firstname", "lastname", "city", "country", "lastaccess"); @@ -139,6 +194,7 @@ $students = $nstudents; } + $table->head = array (" ", "$firstname / $lastname", $city, $country, $lastaccess); $table->align = array ("LEFT", "LEFT", "LEFT", "LEFT", "LEFT"); $table->size = array ("10", "*", "*", "*", "*"); @@ -168,7 +224,7 @@ } print_table($table); - print_paging_bar($totalcount, $page, $perpage, + print_paging_bar($matchcount, $page, $perpage, "index.php?id=$course->id&sort=$sort&dir=$dir&perpage=$perpage&"); if ($perpage != 99999) { @@ -176,8 +232,7 @@ echo "id&sort=$sort&dir=$dir&perpage=99999\">".get_string("showall", "", $totalcount).""; echo "

"; } - - } + } print_footer($course); -- 2.39.5