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.

by Sanya Gold -
Number of replies: 5
I would like to display list of courses in which user entered in my plugin.
Average of ratings: -
In reply to Sanya Gold

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

by Sanya Gold -

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;

}


In reply to Sanya Gold

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

by Darko Miletić -

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.

In reply to Darko Miletić

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

by Sanya Gold -

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

In reply to Sanya Gold

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

by Darko Miletić -

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)
In reply to Darko Miletić

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

by Sanya Gold -

Thank you so much!