duanchigeng4313 2014-10-17 13:33
浏览 48
已采纳

Moodle cohort_add_cohort错误

I'm developing a custom Moodle authentification plugin for Moodle 2.7.

When a user is authenticated I want them to be added to a specific cohort. If that cohort does not exist I need it to be created automatically. I use the user_authenticated_hook() function in my authentification plugin to achieve this.

My code for creating the cohort is this

$data = new stdClass();
$data->name = 'Name string';
$data->idnumber = 'ID string';
$data->description = 'Description string';
$cohortId = cohort_add_cohort($data);

I have included cohort/lib.php in the auth.php file and I have declared the global variables $DB, $CFG and $SESSION at the first line of the user_authenticated_hook() function.

The authentification works without the part about cohorts. But with the cohort part in place authentication fails and I am redirected to the login page.

The page title is changed to "Error" but that is the only error message I get.

What Am I doing wrong? I hope somebody will be able to help me create cohorts and add members.

  • 写回答

1条回答 默认 最新

  • douzhai7873 2014-10-18 07:25
    关注

    It might be because the global $USER object doesn't exist yet or hasn't been populated.

    Do you have debug switched on in your config.php?

    $CFG->debug = 32767;
    $CFG->debugdisplay = 1;
    

    It might be better to respond to the user_created event. So if a user is created by another method, they will still be added to the cohort. eg:

    Create a local plugin eg:

    /local/add_cohorts
    

    Create an events.php

    /local/add_cohorts/db/events.php
    

    Which has something like this

    $handlers = array (
        'user_created' => array (
            'handlerfile'      => '/local/add_cohorts/lib.php',
            'handlerfunction'  => 'local_add_cohorts_user_created',
            'schedule'         => 'instant',
            'internal'         => 1,
        ),
    );
    

    Then in /local/add_cohorts/lib.php have

    function local_add_cohorts_user_created($user) {
        // Do your cohort processing here and add the user
        // Use $user->id to add to the cohort members
    }
    

    Then create a version.php and install the plugin, then the event handler will be registered.

    Events api - https://docs.moodle.org/dev/Events_API

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?