Answer the question
In order to leave comments, you need to log in
How to sign up a user for a course in moodle?
I'm trying to make a php script that executes a sql query to add a user to a specific course. Namely, in the 'mdl_user_enrolments' table , the course is displayed on the user's main page, everything is as it should be, but for some reason such an element of the course as testing is not displayed.
In which table do you need to add something else?
Answer the question
In order to leave comments, you need to log in
Thanks to alexdjachenko for the tip, I made a function, here are the sources
$userid = $_GET['userid']; //id студента
$courseid = $_GET['courseid']; //id курса
require_once('config.php');
require_once('lib/setup.php');
function enroll_to_course($courseid, $userid, $roleid, $extendbase, $extendperiod) {
global $DB;
$instance = $DB->get_record('enrol', array('courseid'=>$courseid, 'enrol'=>'manual'), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST);
$today = time();
$today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
if(!$enrol_manual = enrol_get_plugin('manual')) { throw new coding_exception('Can not instantiate enrol_manual'); }
switch($extendbase) {
case 2:
$timestart = $course->startdate;
break;
case 3:
default:
$timestart = $today;
break;
}
if ($extendperiod <= 0) { $timeend = 0; } // extendperiod are seconds
else { $timeend = $timestart + $extendperiod; }
$enrolled = $enrol_manual->enrol_user($instance, $userid, $roleid, $timestart, $timeend);
add_to_log($course->id, 'course', 'enrol', '../enrol/users.php?id='.$course->id, $course->id);
return $enrolled;
}
enroll_to_course($courseid, $userid, 5, 3, 0); // 5,3,0 - $roleid, $extendbase, $extendperiod
Moodle has a rather complex database structure, so accessing it via SQL queries is not recommended: there are many API options for this, from writing a php subscription module to a ready-made synchronization script with a SQL database.
In your case, the easiest way is to create an intermediate database with a subscription table and set the course subscription plugin on it through an external DBMS: you only need to add pairs of user id and course id to the table, Moodle will do the rest.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question