Events Moodle 2.8中的事件处理
目标是在用户使用相同名称的事件触发器创建或更新时,将两个自定义概要文件字段复制到用户表中。但出于某种原因,这些事件并未触发。我用所需的三个文件为/local/newuser创建了一个新模块,如下所示 如有任何提示,将不胜感激 来自events.php的代码Events Moodle 2.8中的事件处理,events,handler,moodle,Events,Handler,Moodle,目标是在用户使用相同名称的事件触发器创建或更新时,将两个自定义概要文件字段复制到用户表中。但出于某种原因,这些事件并未触发。我用所需的三个文件为/local/newuser创建了一个新模块,如下所示 如有任何提示,将不胜感激 来自events.php的代码 defined('MOODLE_INTERNAL') || die(); $handlers = array ( 'user_updated' => array ( 'handlerfile' =&g
defined('MOODLE_INTERNAL') || die();
$handlers = array (
'user_updated' => array (
'handlerfile' => '/local/newuser/lib.php',
'handlerfunction' => 'local_newuser_user_updated',
'schedule' => 'instant',
),
'user_created' => array (
'handlerfile' => '/local/newuser/lib.php',
'handlerfunction' => 'local_newuser_user_created',
'schedule' => 'instant',
),
);
来自lib.php的代码
defined('MOODLE_INTERNAL') || die();
function local_newuser_user_updated($user) {
require('../../config.php');
global $DB;
$userid = $user->id;
$data1 = $DB->get_field_select('user_info_data', 'data', 'userid='.$userid.' AND fieldid=1', null);
$data2 = $DB->get_field_select('user_info_data', 'data', 'userid='.$userid.' AND fieldid=2', null);
if ($data1 !='' ) {
$sql="UPDATE {user} set idnumber='$data1',department='$data2' where id=$userid";
$DB->execute($sql);
}
return true;
}
function local_newuser_user_created($user) {
require('../../config.php');
global $DB;
$userid = $user->id;
$data1 = $DB->get_field_select('user_info_data', 'data', 'userid='.$userid.' AND fieldid=1', null);
$data2 = $DB->get_field_select('user_info_data', 'data', 'userid='.$userid.' AND fieldid=2', null);
if ($data1 !='' ) {
$sql="UPDATE {user} set idnumber='$data1',department='$data2' where id=$userid";
$DB->execute($sql);
}
return true;
}
当然,还有version.php
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2014030200; // Plugin version.
$plugin->requires = 2013051402; // Moodle version.
$plugin->component = 'local_newuser'; // Full name of the plugin (used for diagnostics).
这是lib.php的require(),应该是require_一次(dirname(dirname(DIR)。/config.php');并把它放在页面的顶部
defined('MOODLE_INTERNAL') || die();
require_once(dirname(dirname(__DIR__)) . '/config.php');
function local_newuser_user_updated($user) {
global $DB;
$userid = $user->id;
$data1 = $DB->get_field_select('user_info_data', 'data', 'userid='.$userid.' AND fieldid=1', null);
$data2 = $DB->get_field_select('user_info_data', 'data', 'userid='.$userid.' AND fieldid=2', null);
if ($data1 !='' ) {
$sql="UPDATE {user} set idnumber='$data1',department='$data2' where id=$userid";
$DB->execute($sql);
}
return true;
}
function local_newuser_user_created($user) {
global $DB;
$userid = $user->id;
$data1 = $DB->get_field_select('user_info_data', 'data', 'userid='.$userid.' AND fieldid=1', null);
$data2 = $DB->get_field_select('user_info_data', 'data', 'userid='.$userid.' AND fieldid=2', null);
if ($data1 !='' ) {
$sql="UPDATE {user} set idnumber='$data1',department='$data2' where id=$userid";
$DB->execute($sql);
}
return true;
}
从Moodle 2.7开始,这些事件已经完全改变。他们现在使用类,因此事件“user_updated”不存在。很遗憾,您需要重写代码 比如说
$observers = array(
array(
'eventname' => '\core\event\user_updated',
'callback' => 'yourclass::yourfunction',
)
);
此外,您不需要在lib或类文件中包含config.php。您只需要在用户可以查看的页面中包含config.php。我已经做了更改,但仍然没有任何更改
code
$observer=array(array('eventname'=>'core\event\user\u created','callback'=>'local\u newuser\u user\u observer::user\u created','includefile'=>'/local/newuser/lib.php''internal'=>false,//仅在事务提交后获取事件。'priority'=>1000,)您是否在version.php中碰撞了版本号?这将更新eventsYes。调用lib.php是因为如果我将错误的路径放入config.php,它会抱怨,但我无法触发任何一个函数。因此我缺少smething。基于上面的lib.php,回调行应该是什么样子的:“callback”=>“local_newuser::local_newuser”_user_created';是的,你需要在classes文件夹中为此创建一个obsever类-我将尝试提供一个示例,但时间很短,搜索moodle代码中更新的user_以获取示例-moodle调用lib.php无论如何不仅仅是为了事件,它还将查找诸如cron之类的特定函数。理想情况下是本地函数应该存储在locallib.php而不是lib.php中