displaying list of courses

displaying list of courses

by Amit Kulkarni -
Number of replies: 6

Hi,

I am using the user profile field to classify users. I have added restricted settings for the course to ensure only certain users are eligible for certain courses. On the front page I have enabled display all courses (after the user has logged in) so when a user logs in he/she sees all the courses that are currently available. However I want to filter out the courses for which the user is not eligible to reduce unnecessary confusion.

How can I achieve that? 


Any guidance would be very helpful


Thanks,


Average of ratings: -
In reply to Amit Kulkarni

Re: displaying list of courses

by Amit Kulkarni -

Any takers? Any suggestions pointers will be highly appreciated.

In reply to Amit Kulkarni

Re: displaying list of courses

by tim st.clair -
Picture of Plugin developers

What denotes "eligibility" in your case? Is it a profile field? Does this prevent the user from enrolling, or is it that they are already enrolled somehow and you want to list only the courses they are already in? It can't really be a capability since all users on the front page and catalogue pages and so on are in the site context, so its not until a user gets into a course that they get a specific role (e.g. student or manager, etc).

Sounds like a custom block or a theme-overridden renderer. For instance, in a theme you can override the routine that draws the category dropdown on the course category page. Any function in /course/renderer.php you can override in your theme's renderer file. Ive not done it myself but I imagine it would be something like:

include_once($CFG->dirroot . "/course/renderer.php");

class theme_mytheme_core_course_renderer extends core_course_renderer {

   public function frontpage_available_courses() {

      /// ... custom code to select all courses then filter them based on your requirements

In reply to tim st.clair

Re: displaying list of courses

by Amit Kulkarni -

Hi Tim,


Thanks for the reply.

Yes the eligibility is based on the user profile field and it prevents the user from enrolling.


I looked at frontpage_available_courses and would like to filter that output somehow by showing only those courses whose restriction settings match the values in user profile fields for the given user. 

I am not sure how to 

1. find the restricted settings for a given course

2. what is the correct way to access user's profile field

3. Where is the best place to put such a code.

I think your response addresses #3 above (Thank you). I will look into my theme's renderer code. Any help on the other two? (especially #1) 


Thanks again.

In reply to Amit Kulkarni

Re: displaying list of courses

by tim st.clair -
Picture of Plugin developers

What are the restrictions you've put on the course and how did you do it?

To access user profile fields, you could load it into the user object, something like (untested)

global $USER, $CFG;
require_once($CFG->dirroot.'/user/lib.php');
$obj = user_get_user_details($USER, null, array('id','customfields');
// $obj->customfields is now an array of visible custom fields and their values

See `user_get_user_details()` in /user/lib.php,  and `profile_load_data()` in /user/profile/lib.php

You can see in `frontpage_my_courses()` there's a method for getting the course list

$courses  = enrol_get_my_courses('summary, summaryformat', $sortorder);

In your renderer's version of this function, you could then do something like (untested)

foreach ($courses as &$course) {

  if (!$obj->customfields->specialcriteria == true) unset $course;

}


In reply to tim st.clair

Re: displaying list of courses

by Amit Kulkarni -

Thanks this is perfect !!


The only remaining question I have is how to access the course's restrictions. 


The restrictions I have put on the course are by going to Edit the course and adding restrictions through add restrictions section. The add restrictions option shows me a user profile field and I have added 3 restrictions 1. Based on user skill 2. based on user location and 3.  based on user grade (all three fields I have added to user profile). I looked through the ode and could find the front end of the addrestrictions piece but not the back end functionality or an example of where that might be stored..


Thanks again. Your help has been invaluable.