Moodle: Checking visibility of Specific activity to users

Author - Harleen Kaur

In moodle, there can be various conditions based on which we can restrict visibility of an activity to all or some specific users even when activity has been created in course and is available to users.

First Red Box area in the screenshot below depicts the restrictions based on date settings. Other options include restrictions based on grade condition and user field such as user email, user city etc:
Now if you want to check the if an activity in a course is available to users or not then there are some availability/visibility moodle functions which can be used for this purpose. Suppose I have name of Item Module – $itemmodule and item instance – $iteminstancewith me from grade items table.

Now the first step is to retrieve the id of a module:

$moduleid = $DB->get_field('modules', 'id', array('name'=>$itemmodule));

Second step would be to retrieve id of module (making the module unique) specific to a course from instance value of module and its module id, In other words to distinguish the activity in the course from all other activities acting as primary key of moodle table “mdl_course_module”:

$coursemoduleid = $DB->get_field('course_modules', 'id', 

Now retrieve the course module/Activity information and from that information, we can find visibility of an activity to a user:

1. Testing the availability/visibility of Quiz (helps in finding visibility based on date

// retrieve the module( activity) information specific to course:
$cm=get_coursemodule_from_id('',$coursemoduleid,$courseid); if ($cm->showavailability){ echo " Quiz Available"; } else{ echo " Quiz NOT Available"; }

2. Testing the availability/visibility of Quiz specific to a user (helps in finding visibility
based on both date and specific user restrictions (such as based on city)

$userid=’12’;//Store some user id in a userid variable or fetch the id of login user
from User API with $userid=$USER->id
$modinfo = get_fast_modinfo($course);
// To retrieve the module( activity) information specific to course.
$cm = $modinfo->get_cm($coursemoduleid);
// Or $cm =$modinfo->cms[$coursemoduleid];
// to track the conditions or restriction information specific to course activity
$ci=new condition_info($cm,CONDITION_MISSING_EXTRATABLE);
if(!$ci->is_available($cm->availableinfo,false,$userid)) {
echo "
Quiz NOT  Available";
else{echo "
Quiz Available";}

3. This simpler method also tests the availability/visibility of Quiz specific to a user:

echo "
Quiz is  Available to User";
else { 
echo "
Quiz NOT  Available";		 
Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published.