【问题标题】:MOODLE. Problem with definition user role for course模型。课程定义用户角色的问题
【发布时间】:2019-08-28 11:41:03
【问题描述】:

我正在尝试通过Using MOODLE create users and enroll them in courses via SQL提出解决方案

添加用户 - 完成

将用户添加到课程 - 完成

但是没有角色的用户进入课程。角色列中没有角色。

'googleoauth2' 作为使用社交网络 API 登录的身份验证值。这是工作。 如果我将 'auth' 值更改为 'manual',问题仍然存在。

查询中的所有记录进入数据库。

请帮忙。

UPD:抱歉,我需要在 instanceid 字段中将 enlorID 更改为 courseID...

代码:

    async function test(){
        var query = `INSERT INTO mdl_user (auth, confirmed, username, password, firstname, lastname, email, mnethostid) 
            VALUES ('googleoauth2', 1, '${pseudo}', 'not cached', '${name}', 
                   '${secondname}', '${email}', 1);`;
        var insertOutput = await getMysqlQuery(query);
        var userId = insertOutput.results.insertId;
        var courseRecords = await getMysqlQuery("SELECT id FROM mdl_course WHERE idnumber=\"" + shortname + "\"");  

        if(courseRecords.length < 1)
            throw 'Course not found';
        var courseId = courseRecords.results[0].id;
        var enrolRecords = await getMysqlQuery(`SELECT id FROM mdl_enrol WHERE courseid=${courseId} AND enrol='manual';`);

        if(enrolRecords.length < 1)
            throw 'Enrol not found';
        var enrolId = enrolRecords.results[0].id;
        var contextRecords = await getMysqlQuery(`SELECT id FROM mdl_context WHERE contextlevel=50 AND instanceid=${courseId};`);

        if(contextRecords.length < 1)
            throw 'Context not found';
        var now = (new Date()).getTime() / 1000 ;
        var contextId = contextRecords.results[0].id;

        await getMysqlQuery(`INSERT INTO mdl_user_enrolments (status, enrolid, userid, 
                              timestart, timeend, timecreated, timemodified) VALUES 
       (0, ${enrolId}, ${userId}, '${now}', '${now + 60*60*24*2}', '${now}', '${now}')`);

        await getMysqlQuery(`INSERT INTO mdl_role_assignments 
                                    (roleid, contextid, userid, timemodified) 
                               VALUES (5, ${contextId}, '${userId}', '${now}');`);
    }

【问题讨论】:

    标签: mysql node.js database moodle


    【解决方案1】:

    通过查看您的代码,您似乎正在使用 Javascript。 我有一个解决方案,可以让学生参加 PHP 编程语言的课程,如下所示。

    $plugin = enrol_get_plugin('manual');
    
    $course = $DB->get_record('course', array('id' => $courseid)); // Get course object by courseid
    
    $instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
    if (empty($instance)) {
        // Only add an enrol instance to the course if non-existent.
        $enrolid = $plugin->add_instance($course);
        $instance = $DB->get_record('enrol', array('id' => $enrolid));
    }
    
    // Take care of timestart/timeend in course settings.
       $timestart = time();
       // Remove time part from the timestamp and keep only the date part.
       $timestart = make_timestamp(date('Y', $timestart), date('m', $timestart), date('d', $timestart), 0, 0, 0);
       if ($instance->enrolperiod) {
           $timeend = $timestart + $instance->enrolperiod;
       } else {
           $timeend = 0;
       }
        // Enrol the user with this plugin instance.
        $plugin->enrol_user($instance, $user->id, $roleid); // This will enroll user in course with roleid you provided.
    

    希望这能帮助您解决问题。

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-10
      • 2011-05-23
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 2016-12-12
      相关资源
      最近更新 更多