General developer forum

How to get a list of custom courses

 
Picture of Sanya Gold
Re: How to get a list of custom courses
 
This is my code: 

function user_get_user_courses($user, array $userfields = array()) {

  // List of courses where the user is enrolled.

    if (in_array('enrolledcourses', $userfields) && !isset($hiddenfields['mycourses'])) {

        $enrolledcourses = array();

        if ($mycourses = enrol_get_users_courses($user->id, true)) {

            foreach ($mycourses as $mycourse) {

                if ($mycourse->category) {

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

                    $enrolledcourse = array();

                    $enrolledcourse['id'] = $mycourse->id;

                    $enrolledcourse['fullname'] = format_string($mycourse->fullname, true, array('context' => $coursecontext));

                    $enrolledcourse['shortname'] = format_string($mycourse->shortname, true, array('context' => $coursecontext));

                    $enrolledcourses[] = $enrolledcourse;

                }

            }

            $userdetails['enrolledcourses'] = $enrolledcourses;

        }

    }

return $userdetails;

}

How to derive a function?

 
Average of ratings: -