General developer forum

I would like to display list of courses in which user entered in my plugin.

 
Picture of Sanya Gold
I would like to display list of courses in which user entered in my plugin.
 
I would like to display list of courses in which user entered in my plugin.
 
Average of ratings: -
Picture of Sanya Gold
Re: I would like to display list of courses in which user entered in my plugin.
 

How to display it on the page?

function user_get_courses($user,$userdetails, 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['shortname'] = format_string($mycourse->shortname, true, array('context' => $coursecontext));

                    $enrolledcourses[] = $enrolledcourse;

                }

            }

            $userdetails['enrolledcourses'] = $enrolledcourses;

        }

    }

return $userdetails;

}


 
Average of ratings: -
Picture of Darko Miletić
Re: I would like to display list of courses in which user entered in my plugin.
Core developersParticularly helpful Moodlers

To create a custom page in Moodle you need to use Page API and Output API. In output API page you have a barebone example of custom page.

 
Average of ratings: -
Picture of Sanya Gold
Re: I would like to display list of courses in which user entered in my plugin.
 

I didn't ask to create for a page. I need a list of courses in my plugin. (Example: user profile). List in which the user is enrolled

 
Average of ratings: -
Picture of Darko Miletić
Re: I would like to display list of courses in which user entered in my plugin.
Core developersParticularly helpful Moodlers

For that you can use enrol_get_users_courses API. Here is an example:

require_once('/path/to/config.php');

require_login();
require_capability('moodle/site:config', context_system::instance());

$PAGE->set_context(context_system::instance());
$PAGE->set_url('path/to/myfile.php');
$PAGE->set_title('my title');
$PAGE->set_title('my heading');

echo $OUTPUT->header();

$courses = enrol_get_users_courses($USER->id, true);
if (!empty($courses)) {
    $ht = new html_table();
    $ht->size = ['10%', '70%', '20%'];
    $ht->caption = 'User courses';
    $ht->head = ['Course id', 'Course title', 'Start date'];
    $ht->data = [];
    foreach ($courses as $courseid => $course) {
        $ht->data[] = [$course->id, $course->fullname, userdate($course->startdate)];
    }
    echo html_writer::table($ht);
}

echo $OUTPUT->footer();





 
Average of ratings: Useful (2)
Picture of Sanya Gold
Re: I would like to display list of courses in which user entered in my plugin.
 

Thank you so much!


 
Average of ratings: -