Function to get all students

Function to get all students

by Lina Ahmed Kamal -
Number of replies: 4

Hi,

I'm trying to create a plugin to list all students from all courses for a specific teacher, so I've been searching for a function to get all students from all courses for this specific teacher.

Does such function exist or do I have to create a sql query for that, I'm not really good with sql so I'm trying to avoid it.

Thanks in Advance,

Average of ratings: -
In reply to Lina Ahmed Kamal

Re: Function to get all students

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
It depends what you mean. Do you mean "everybody enrolled in every course with the specific role 'student'" or something else? Consider what happens if now (or in the future) you add a new role that's like student but different.

A dig through lib/accesslib.php may help you.
In reply to Lina Ahmed Kamal

Re: Function to get all students

by Lina Ahmed Kamal -
I used this code and got the students as needed, is there anything wrong with it or that requires improvements:

//Get courses of current user
$courses = enrol_get_users_courses($USER->id, true, 'id, visible, shortname');
$students = array();

foreach ($courses as $courseid => $course){

$coursecontext = context_course::instance($course->id);

// Get Enrolled users
$enrolledstudents = get_enrolled_users($coursecontext, 'moodle/course:isincompletionreports');


$coursename = $course->shortname ;

foreach ($enrolledstudents as $user) {

$studentid = $user->id;
array_push($students, $user);

}
}
$students = array_unique($students,SORT_REGULAR);