1

I am trying to create access restrictions to a folder or resource inside of a section in a Moodle course using a Script. This restriction must be for groups of users and if you don't belong to that group you can not see the existing resources.

The operation programmatically I want to do is as follows: enter image description here

I searched for information and there is very little documentation, in the documentation only puts how to do just that by the web link.

I know how to create groups, folders and sections programmatically from script, but I can not identify the tables to be used for these restrictions or what are the steps to follow.

So if anyone knows how to do it or have any examples or documentation that may be useful, it would be helpful.

Thanks in advance.

Joacer
  • 568
  • 13
  • 32

2 Answers2

1

The classes which will be used to create the UI and check whether a user has access to your resource are located at:

availability/condition/group/classes/condition.php
availability/condition/group/classes/frontend.php

The data related to the conditions are formatted to JSON from the UI in Javascript, then sent and saved. My first guess would be that you need to recreate the JSON structure and save it into the table/column course_modules::availability. Once that is done I think you'll have to purge the cache from cm_info where the availability data is used to confirm whether the current user can access your resource.

FMCorz
  • 2,586
  • 1
  • 21
  • 18
0

I followed your advice @FMC and I have done this function to my script that is responsible for giving permits to a group for a particular section of a course

and 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
 * @param $module id of the mdl_module to restrict access
 *
 */
function grantPermission($course, $sectionid, $groupid, $module ){

    global $DB;

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

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

    $course_module = new stdClass();
    $course_module->id = $cm->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;
}

Can you check if is this what you meant @FMC? It works but I don't know if this is the best way.

Thank you!

Joacer
  • 568
  • 13
  • 32
  • 1
    Yes, though I would suggest to use `cm_info` to save the data if possible. Also did you notice that you fetch a random module from a section in your query? – FMCorz Oct 14 '16 at 09:11
  • You are right, I forgot specify the module in the query, thank you! – Joacer Oct 14 '16 at 10:31
  • I don't know how to use the cm_info, I'm a little new creating Scripts without UI and I don't know how to use them, Can you put me some example? Thank you – Joacer Oct 14 '16 at 10:34
  • My bad, it doesn't seem that `cm_info` can build the update query for you. – FMCorz Oct 14 '16 at 10:36
  • Ok, Thanks for the help!! – Joacer Oct 14 '16 at 10:41