General developer forum

View all courses without categories

 
Picture of Lythande Lister
View all courses without categories
 

Hello,

I'm searching a function to get all courses on Moodle without categories.

I have never coded on Moodle and some indications would be very useful.

Here my problem :

$chelper = new coursecat_helper();
$chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->
set_courses_display_options(array(
'recursive' => true,
'limit' => $CFG->frontpagecourselimit,
'sort' => array('sortorder' => 0),
'viewmoreurl' => new moodle_url('/course/index.php'),
'viewmoretext' => new lang_string('fulllistofcourses'),
));
In this way I see all the courses but they are sorted by the categories and if I filter for example by name it will be for each category and not for all the courses.

This function exists or I need to create my sql request ?

Thanks for your help !!!

 
Average of ratings: -
Picture of Lythande Lister
Re: View all courses without categories
 

I find the function :D

It's something like :

get_courses('all', "c.fullname ASC")
And we can read it in course/externallib.php :

/**
* Get courses
*
* @param array $options It contains an array (list of ids)
* @return array
* @since Moodle 2.2
*/
public static function get_courses($options = array()) {
....
}

 
Average of ratings: -
Picture of Sandip Shrestha
Re: View all courses without categories
 

After you have created a new course helper and set the display option by doing

$chelper = new coursecat_helper();

$chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->

set_courses_display_options(array(

    'recursive' => true,

    'limit' => $CFG->frontpagecourselimit,

    'sort' => array('sortorder' => 0),

    'viewmoreurl' => new moodle_url('/course/index.php'),

    'viewmoretext' => new lang_string('fulllistofcourses'),

));

you will need to fetch all courses by doing

$courses = coursecat::get(0)->get_courses($chelper->get_courses_display_options());
it returns an indexed array of objects (key is courseid and value is course object). Then you can sort it using something like
arsort($courses);
 
Average of ratings: -