search

Home  >  Q&A  >  body text

How to get users for each category and subcategory in Moodle

I want to get the users of each category. I am using moodle version 3.8. Is there any way to get users by categories.

P粉818306280P粉818306280261 days ago373

reply all(1)I'll reply

  • P粉567281015

    P粉5672810152024-02-27 09:21:08

    If you are looking for a Moodle function to do this then you can call:

    $cat = core_course_category::get($categoryid);
    $courseids = $cat->get_courses(['recursive', 'idonly']);
    $userids = [];
    foreach ($courseids as $courseid) {
        $context = context_course::instance($courseid);
        $courseusers = get_enrolled_users($context, '', 0, 'u.id');
        $userids = array_merge($userids, array_keys($courseusers));
    }

    However, this is very inefficient - you'd be better off writing a custom SQL query that, based on a list of course IDs, will generate a list of registered users for all those courses (look inside the code) get_enrolled_users() to see how it's constructed Such SQL query).

    reply
    0
  • Cancelreply