Enrollment plugin development question

Enrollment plugin development question

על ידי Zoran Jančić בתאריך
מספר תגובות: 3
תמונה של Particularly helpful Moodlers

Hi! We have developed an activity plugin that also enables user to self enrol in a course. The course also has guest acces so a user can acces the activity within the course before he enrolls himself to the course. After he enrolls himself, other activities and resources should become available to him. We use standard Moodle restrictions functionalities to restric user from accesing certain activites and resources if he is not enrolled in the course with student role. The problem is that the student can't access theese additional activities until he logges out of the system and logges in again.  Afterward he can access everything he should. So the problem is, I guess, in our code that enrolls user to the course. Somehow he stays in authenticated user role untill next login when he actually gets the student role permissions. We would want him to gain student role's permissions in the course immediately after he enrolls himself to the course. So, I guess something is missing in our code, something like refresh user's permission or something simmiliar. Moodle version is 2.9 and an upgrade to higher version is not an option at the moment. Her is the code that we use to enroll user to the course (this is from lib.php file of our plugin):


            $enrolid = $DB->get_field('enrol', 'id', array('courseid' => $record->courseid, 'enrol' => 'manual'));

            if(!$DB->record_exists('user_enrolments', array('enrolid' => $enrolid, 'userid' => $record->userid))) {

                // Enrolling user into course.

                $ue = new stdClass();

                $ue->enrolid = $enrolid;

                $ue->userid = $record->userid;

                $ue->modifierid = $createdby;

                $ue->timecreated = time();

                $ue->timemodified = $ue->timecreated;

                $ue->timestart = strtotime(date("Y-m-d", $ue->timecreated));

                $ue->timeend = 0;

                $DB->insert_record('user_enrolments', $ue);

            }


            $roleid = $DB->get_field('role', 'id', array('shortname' => 'student'));

            $context = context_course::instance($record->courseid);

            if(!$DB->record_exists('role_assignments', array('roleid' => $roleid, 'contextid' => $context->id, 'userid' => $record->userid))) {

                // Giving user the role of 'student' within the course.

                $ra = new stdClass();

                $ra->roleid = $DB->get_field('role', 'id', array('shortname' => 'student'));

                $ra->contextid = $context->id;

                $ra->userid = $record->userid;

                $ra->timemodified = time();

                $ra->modifierid = $createdby;

                $DB->insert_record('role_assignments', $ra);

            }

ממוצע דרוגים: -
בתגובה ל: Zoran Jančić

Re: Enrollment plugin development question

על ידי Emma Richardson בתאריך
תמונה של Documentation writers תמונה של Particularly helpful Moodlers תמונה של Plugin developers
I am going to move this to the developer forum - I think you will get more help there.
בתגובה ל: Zoran Jančić

Re: Enrollment plugin development question

על ידי Benjamin Ellis בתאריך
תמונה של Particularly helpful Moodlers
Hi,

You would have been probably been better of using the Enrolment API - https://docs.moodle.org/dev/Enrolment_API instead of writing to the database directly. You might get an idea of how to achieve that by looking in /lib/enrollib.php specifically the enrol_user() function.