Moodle Customization’s : Enrolled courses and Related Search functions​


The focus of our study is regarding customizing the course category such that only enrolled courses will be visible when we select a category and related search function in Moodle.

Course category

In Moodle the courses are categorized into various categories. We can create multiple course categories, usually the course categories would be categorized based on the term or the type of courses.



Enrolled courses

In order to access the content of a course in Moodle one needs to enroll into the course with any defined standard role or customized role in Moodle. A user would be enrolled to a list of courses which may belong to various course categories.

Course search 

In Moodle we can search any course based on the name of the course from the Home page. 

We can also search courses when we select any course category and search course from the search box provided at the far right corner of the page as displayed here.


Standard Moodle functionality

As per the standard Moodle functionality  when a user selects any course category after logging in he will see his enrolled courses under the block ‘Available courses’, where a list of courses are available.

But when he selects ‘My courses’ or any Course category he can see all the courses populated in the category and also the same will resemble in the navigation bar present beside the list of courses within the course as observed here.

Customized Moodle functionality

We have to customize Moodle such that a user would only be view his/her enrolled courses when he selects the ‘My Courses’ or any Courses category in Moodle and also  the related search function i.e whenever we select a course category,the related search results must only display courses  to which the user had been enrolled in that course category and also the navigation bar  provided beside  must also display only the enrolled courses.

Below is the code which we have customized in these files.

Step1 :Find the file /moodle/course/renderer.php At linenumber 1875
Step2 :You can find the  search_courses($searchcriteria)method definition as below
publicfunction search_courses($searchcriteria) {
Step3 :Now comment the code at line number 1907, 1908 as below
//$totalcount = coursecat::search_courses_count($searchcriteria);
//$courseslist = $this->coursecat_courses($chelper, $courses,$totalcount);
Step4 : Now add below code
        global $USER;
                $admins= get_admins(); $isadmin = false;
                foreach($admins as $admin) {
                        if($USER->id == $admin->id) {
                        $isadmin= true; break; } }
                                $categoryidval= $_SESSION[‘categoryid’];
                        }else{$categoryidval = -1; }
                        $coursesCriteria= array();
                        if($courses!= null){
                        foreach($coursesas $key => $course){
                                if(!$isadmin && isloggedin()) { // Show all
                        $context= context_course::instance($course->id);
           $enrolled = is_enrolled($context,$USER->id, ”, true);
                                if(!($categoryidval== -1) && ($categoryidval !=0)){
                                if($course->category== $categoryidval){
                                $coursesCriteria[$key]= $course;
                                $coursesCriteria[$key]= $course;
                        $courses= $coursesCriteria;
                        $totalcount= count($courses);