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
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
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.
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;
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