Availability conditions - handling sessions and course modules

Availability conditions - handling sessions and course modules

by Daniel Neis Araujo -
Number of replies: 0
Picture of Core developers Picture of Plugin developers Picture of Translators

Hello, everybody


I was working on a new availability condition to make a "paywall" with paypal to course modules and couse sections (is there anything else that can be restricted by "availability/condition" plugns?) and this plugin has a "view" page that shows a button if the user has not yet paid for that part and otherwise redirects to the activity (originated on https://moodle.org/mod/forum/discuss.php?d=311316).

The display message of the activity includes a link to this page with contextid from "$info->contextid" (link to code: https://github.com/danielneis/moodle-availability_paypal/blob/master/classes/condition.php#L113).

In the view, I am handling this context with the following code (https://github.com/danielneis/moodle-availability_paypal/blob/master/view.php#L35):

$context = context::instance_by_id($contextid);
$instanceid = $context->__get('instanceid');
if ($context instanceof context_module) {
    $availability = $DB->get_field('course_modules', 'availability', array('id' => $instanceid), MUST_EXIST);
    $availability = json_decode($availability);
    foreach ($availability->c as $condition) {
        if ($condition->type == 'paypal') {
            // TODO: handle more than one paypal for this context
            $paypal = $condition;
            break;
        } else {
            // TODO: no paypal for this context
        }
    }
} else {
    //TODO: handle sections
}
It is already working for course modules with only one paypal condition, but I'm afraid it is not the best code from to continue to work with and then I would like some advices on how to hande these things.

About the link, I can change the contextid, no problem, feel free to advice for the better, no matter how much rework would be needed. This is already free software on github and it is intended to be on Moodle plugins directory.

Kind regards,
Daniel

Average of ratings: -