10

I need to show all moodle courses in menu listing. Can anyone suggest me that how can I get all courses using php code or moodle inbuilt functions.

Thanks

Rahul Saini
  • 101
  • 1
  • 1
  • 5

3 Answers3

12

Assuming you are writing code to be run within Moodle, you can use the get_courses() function defined within lib/datalib.php. For example:

<?php
require_once(PATH_TO_MOODLE_ROOT . '/config.php');
$courses = get_courses();
print_r($courses);

will print out a data-dump of the returned array, showing details of all the courses in your Moodle site. This example is obviously not appropriate to use on a production site!

If you check the function definition in lib/datalib.php you will see the options available for restricting the result set to particular fields or controlling the sort order.

Chris Throup
  • 651
  • 1
  • 4
  • 19
3

Include this file

require_once($CFG->dirroot . '/lib/coursecatlib.php');

Use this function to get all courses in menu listing.

$allcourses = coursecat::get(0)->get_courses(array('recursive' => true));

var_dump($allcourses);exit;
Foramkumar Patel
  • 299
  • 3
  • 10
1

If you want to show only enrolled course to student you can use following method.

require_once($CFG->dirroot.'/blocks/course_overview/locallib.php');
global $USER,$DB;
$courses = enrol_get_users_courses($USER->id, true);

OR If you want list all courses..

global $DB;
$query = "SELECT id, fullname, shortname from {course}";
$courselist = $DB->get_records_sql($query);
foreach ($courselist as $course) {
    echo $course->fullname;
}

Thanks

Dnyanesh
  • 31
  • 8