Php 创建Moodle用户并按程序将其注册到课程中

Php 创建Moodle用户并按程序将其注册到课程中,php,moodle,Php,Moodle,我需要从外部在Moodle上创建用户和注册用户。Moodle数据库非常复杂,它有0个文档。到目前为止,我知道mdl_用户拥有用户,但我不知道在那里查询什么,课程也令人头痛。我看到你在标记中添加了“php”,所以我会给你php答案,但查询在任何mysql中都能工作 $servername = 'localhost'; $username = 'username'; $password = 'password'; $dbname = 'moodle'; $u_moodle = 'theuserna

我需要从外部在Moodle上创建用户和注册用户。Moodle数据库非常复杂,它有0个文档。到目前为止,我知道mdl_用户拥有用户,但我不知道在那里查询什么,课程也令人头痛。

我看到你在标记中添加了“php”,所以我会给你php答案,但查询在任何mysql中都能工作

$servername = 'localhost';
$username = 'username';
$password = 'password';
$dbname = 'moodle';

$u_moodle = 'theusernameyouwant';
$hp_moodle = password_hash('thepasswordyouwant', PASSWORD_DEFAULT); ///IMPORTANT!
$name = 'first name';
$lname = 'last name';
$email = 'e@m.ail'; ///This have to be verified by you as we're inserting it directly
$course = '123'; //Id that you put in moodle admin, not the real id    

$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "INSERT INTO 'mdl_user' (auth, confirmed, mnethostid, username, password, firstname, lastname, email)
    VALUES ('manual', 1, 1, '$u_moodle', '$hp_moodle', '$name', '$lname', '$email')";
// auth = 'manual', confirmed = 1, mnethostid = 1 Always. the others are your variables

if ($conn->query($sql) === TRUE) {
    echo "OKTC";
} else {
    ////Manage your errors
}

$sql = "SELECT * FROM $m_user WHERE email='$email'";
$result = $conn2->query($sql);
if($row = $result->fetch_assoc()) {
    $id = $row['id']; //Id of newly created user. we're using that for to register him on the course
}

////You have to use this if your idnumber for the course is the one you put into moodle (thats not the real id)
$sql = "SELECT id FROM 'mdl_course' WHERE idnumber=$course";
$result = $conn->query($sql);
if(!$result){
    ///Not existing course, manage your error
}
if($row = $result->fetch_assoc()) {
    $idcourse = $row["id"];
}

///I need now the "enrol" id, so I do this:
$sql = "SELECT id FROM 'mdl_enrol' WHERE courseid=$idcourse AND enrol='manual'";
$result = $conn->query($sql);
if(!$result){
    ///Not enrol associated (this shouldn't happen and means you have an error in your moodle database)
}
if($row = $result->fetch_assoc()) {
    $idenrol = $row["id"];
}

///Lastly I need the context
$sql = "SELECT id FROM 'mdl_context' WHERE contextlevel=50 AND instanceid=$idcourse"; ///contextlevel = 50 means course in moodle
$result = $conn->query($sql);
if(!$result){
    ///Again, weird error, shouldnt happen to you
}
if($row = $result->fetch_assoc()) {
    $idcontext = $row["id"];
}

///We were just getting variables from moodle. Here is were the enrolment begins:

$time = time();
$ntime = $time + 60*60*24*$duration; //How long will it last enroled $duration = days, this can be 0 for unlimited.
$sql = "INSERT INTO 'mdl_user_enrolments' (status, enrolid, userid, timestart, timeend, timecreated, timemodified)
VALUES (0, $idenrol, $id, '$time', '$ntime', '$time', '$time')";
if ($conn->query($sql) === TRUE) {
} else {
    ///Manage your sql error
}

$sql = "INSERT INTO 'mdl_role_assignments' (roleid, contextid, userid, timemodified)
VALUES (5, $idcontext, '$id', '$time')"; //Roleid = 5, means student.
if ($conn->query($sql) === TRUE) {
} else {
    //manage your errors
}

菲尼托。在这里,您获得了一个新用户名,该用户名已被纳入课程123。

请参见此处的答案-效果非常好!非常感谢。有没有办法报名参加“所有课程”?这是一个非常糟糕的想法-Moodle有很多很棒的内置数据库功能,可以保护您免受各种SQL注入攻击,这些攻击是该脚本无法解决的,并且可以与Moodle支持的任何数据库后端一起使用。像这样直接使用mysql命令几乎肯定会在将来导致问题(例如,如果用户姓“O'Connor”,该脚本将立即中断)。我不是说这是个好主意,我是在回答这个问题。实数转义字符串($name);解决这个问题我同意davosmith,@Aschab在创建用户时Moodle中也有触发器,通过sql插入记录并不能解决这个问题。htmlguy:我最近看到同样的问题被问了好几次。你是同一个用户吗?我已经回答了好几次了。您需要直接使用web服务而不是sql—这个答案似乎是正确的。