General developer forum

Showing availability of activities

Picture of Nikki Macfarlane
Showing availability of activities

I produce a report for my teachers that shows the progress of a specific student within a course. It should show them if there are activities not yet completed and are dependent on another activity before they can be completed. It is working for some activities but not for others. 

For example, I have three assignments:

Assignment A: No conditions (should return a flag of 1)
Assignment B: Cannot be accessed until Assignment A has a pass grade (should return a flag of 2)
Assignment C: Cannot be access until Assignment A & B have a pass grade (should return a flag of 2)

My code has this:

$sections = $DB->get_records_sql('SELECT * FROM {course_sections} WHERE course = ? AND visible = ? AND sequence <> ? ORDER BY section', array("$course", "1", ""));

foreach($sections AS $section) {

    $tags = explode(",", $sequence);

    foreach ($tags AS $key) {

        // find the module type
        $modules = $DB->get_records_sql('SELECT * FROM {course_modules} WHERE id = ? AND completion = ? AND (module = ? OR module = ? OR module = ?)', array("$key", "2", "18", "50", "10"));

        foreach ($modules AS $module) {


            $modinfo = get_fast_modinfo($course);
            $cm = $modinfo->get_cm($moduleid);

            if ($cm->available) {
            } else {

I have attached a screenshot of the students page showing that Assignment A is available, but Assignment B and C are not. The teachers report though is showing that Assignment A & B are both available, and C is not. 

I have checked the settings for Assignments B and C and they are exactly the same - both are reliant on the previous activity being completed with a pass grade. 

Does anyone have any ideas on why $cm->available is incorrectly identifying Assignment B as being available (flag=1 but should have flag=2) when there is a conditional activity for its access in the restriction settings?



Average of ratings: -
Re: Showing availability of activities
Core developersParticularly helpful MoodlersPlugin developers

The information about availability of a module can be checked from the info_module class:

$infomodule = new \core_availability\info_module($cm);
Then additional functions are available to check if the module is available for a specific user, not only the current user.

But the availability of a module can also be related to the availability of a section, so perhaps an extra check about the section could be required:

$infosection = new \core_availability\info_section($cm->section);

Average of ratings: -
Picture of Nikki Macfarlane
Re: Showing availability of activities

Thanks Renaut - so looking at what you had suggested I now realise I was not identifying the specific user so it was picking up the availability for me! Duh!

It was a super easy fix:

I changed 

$modinfo = get_fast_modinfo($course);


$modinfo = get_fast_modinfo($course, $studentid);

Thanks for getting me on the right track!

Average of ratings: -