Show enrollment information in course overviews

Show enrollment information in course overviews

by Gertjan van Heijst -
Number of replies: 0

Hi,

I hope i'm putting question into the right forum, but enrollment seems to be the closest topic to what we are looking for.

In our installation when someone views all courses (on the fronpage when (s)he is not enrolled in any course or when viewing 'all courses'. In that view we would like to provide the student with information about the enrollment possibilities of the course and perhaps other metaimformation of the course as well. What we envisage is something like the following:

When a person enters the main page he/she sees a list of all courses.
We would like to see an icon (or something like that) behind each course.
The icon must indicate the current status of the course with respect to that
person.

Possible status icons:
a) Green-icon: indicates the person is already enrolled in the course.
b) Yellow-icon: indicates the person is not yet enrolled in the course, but
it is possible for him/her to
   be enrolled in the course if he/she wishes.
c) Red-icon: indicates the person is not yet enrolled in the course and it
is not possible for him/her
   to be enrolled in the course either.

We could encode the feature in moodle myself, but this means we would have to
change the moodle-source each time I upgrade
my moodle installation.

My suggestion to make it work is:
a) Wrap the contents of the page "course/enrol.php" in a function, where you
only keep the checks
   and remove the actual enrollment-actions.
   This function has one parameter: the course_id.
   It syntax could be stated as:
   function check_user_enrollment($id) {
b) There is a page called "course/lib.php" which contains a function with
the syntax shown below:
   function print_category_info($category, $depth, $files = false) {
   It has got a statement like this:
   echo '<a '.$linkcss.'
href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.
format_string($course->fullname).'</a>';
  
   Behind this course link you could place the icon.
   It could be done as shown below:


   
   $icon = '';
   $enrol_msg = check_user_enrollment($id);
   if($enrol_msg === null)
   {
     $icon = "<img src='green.gif' title='You are already enrolled in this
course' />";
   }
   else if(is_string($enrol_msg))
  {
    $icon = "<img src='red.gif' title='".htmlspecialchars($enrol_msg,
ENT_QUOTES)."' />";
   }
   else
   {
     //it's an array with one element
     $icon = "<img src='yellow.gif' title='".htmlspecialchars($enrol_msg[0],
ENT_QUOTES)."' />";
   }
   echo '<a '.$linkcss.'
href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.
format_string($course->fullname).'</a>'.$icon;
function check_user_enrollment($id) {
    global $CFG, $SESSION, $USER;
  
    require_once("$CFG->dirroot/enrol/enrol.class.php");
    if (! $course = get_record('course', 'id', $id) ) {
        return 'Invalid course id';
    }
    if (! $context = get_context_instance(CONTEXT_COURSE, $course->id) ) {
        return 'Invalid course id';
    }
/// do not use when in course login as
    if (!empty($USER->realuser) and $USER->loginascontext->contextlevel ==
CONTEXT_COURSE) {
        return 'You can not use enrol or unenrol when in course Login as
session';
    }
    $enrol = enrolment_factory::factory($course->enrol); // do not use if
(!$enrol... here, it can not work in PHP4 - see MDL-7529
/// Refreshing all current role assignments for the current user
    load_all_capabilities();
    if (has_capability('moodle/course:view'))
    {
      return null;
    }
/// Check if the course is a meta course  (bug 5734)
    if ($course->metacourse) {
        return 'This course does not allow public access';
    }
/// Users can't enroll to site course
    if ($course->id == SITEID) {
        return 'You have to enrol in one of the courses before you can use
the site activities';
    }
/// Double check just in case they are enrolled to start in the future
    if ($course->enrolperiod) {   // Only active if the course has an
enrolment period in effect
        if ($roles = get_user_roles($context, $USER->id)) {
           $bzb_minus = null;
            foreach ($roles as $role) {
                if ($role->timestart and ($role->timestart >= time())) {
                    if($bzb_minus === null || $student->timestart <
$bzb_minus)
                    {
                      $bzb_minus = $student->timestart;
                    }
                }
            }
            return 'Sorry, you can not access this course until
'.userdate($bzb_minus);
       }
    }
/// Check if the course is enrollable
    if (!method_exists($enrol, 'print_entry')) {
      return 'Manual enrolments are currently not enabled';
    }
    if (!$course->enrollable ||
            ($course->enrollable == 2 && $course->enrolstartdate > 0 &&
$course->enrolstartdate > time()) ||
            ($course->enrollable == 2 && $course->enrolenddate > 0 &&
$course->enrolenddate <= time())
            ) {
        return 'This course is not enrollable at the moment.';
    }
   $bzb_enrolment_period = ''.
    if($course->enrollable == 2 && $course->enrolstartdate > 0)
    {
      $bzb_enrolment_period .= 'Enrollment possible from
'.userdate($course->enrolstartdate).'. ';
    }
    if($course->enrollable == 2 && $course->enrolenddate > 0)
    {
      $bzb_enrolment_period .= 'Enrollment possible until'.userdate(
$course->enrolenddate).'. ';
    }
    return array($bzb_enrolment_period);
}
  

So here is the question...

a) Is this feature already available in some way without having to change the source code?

b) if not, whet is the proper way to get it in a next version of Moodle so we don't have to change code after installing a new release?

Kind regards,
Robert Sieverink

Gertjan van Heijst

Average of ratings: -