Programatically add access restriction to a course section

Programatically add access restriction to a course section

by Pablo Escobar -
Number of replies: 3

Hi,

I need to add Programatically access restrictions to course sections, for example a grouping restriction. How can i do that?, any method or API to handle this?

Thanks in advance.

Average of ratings: -
In reply to Pablo Escobar

Re: Programatically add access restriction to a course section

by Pablo Escobar -

I tried to manipulate the availability field on course_section table, adding the JSON dinamically to the table register.

{"op":"&","c":[{"type":"grouping","id":6}],"showc":[true]}
But this did not work. Any idea how to do this correctly?

Thanks in advance

In reply to Pablo Escobar

Re: Programatically add access restriction to a course section

by Jose R. Aguilar -

I have to do the same, add access restrictions  to course sections for a group. Did you find the solution?

In reply to Jose R. Aguilar

Re: Programatically add access restriction to a course section

by Jose R. Aguilar -

I found a way to restrict access to a section of a course for a group, I don't know if this is the best way to do that, but it works for my purpose.

This is the code:

/**
 * giving permits to a group for a particular section of a course
 *
 * @param $course course that contains the section to restrict access
 * @param $sectionid id of the section to restrict access
 * @param $groupid id of the group will have access
 *
 */
function grantPermission($course, $sectionid, $groupid ){

    global $DB;

    $restriction = '{"op":"&","c":[{"type":"group","id":'. $groupid .'}],"showc":[true]}';

    $module = $DB->get_record('course_modules', array('course' => $course , 'section' => $sectionid ), '*', MUST_EXIST);

    $course_module = new stdClass();
    $course_module->id = $module->id;
    $course_module->course = $course;
    $course_module->section = $sectionid;
    $course_module->availability = $restriction;

    $res = $DB->update_record('course_modules', $course_module);

    if($res)
        rebuild_course_cache($course, true);    

    return $res;
}