dongyuji7309 2016-03-18 02:46
浏览 59
已采纳

创建moodle用户并以编程方式在课程中注册它们

I need to create users and register users on moodle from the outside. The Moodle database is really complex and it have like 0 documentation. So far I understand that the mdl_users have the users, but I dont get what to query there, and the courses are a headache.

  • 写回答

1条回答 默认 最新

  • dongshan7708 2016-03-18 03:27
    关注

    I see you add "php" to your tags, so I'll give you the php answer, but the query work in any 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
    }
    

    E finito. There you got a new user name enroled into course 123.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?