Function to get all students

Re: Function to get all students

by Lina Ahmed Kamal -
Number of replies: 1
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);