0

我有 2 个数组。一个使用来自 Moodle 的用户名($allUsers),另一个使用来自外部源的用户名($dataClip)。如果尚未注册,我需要比较它们并批量添加它们。

function buildURL($year, $period, $typeperiod,$course)
{
return 'https://clip.unl.pt/sprs?lg=pt&year='.$year.'&uo=97747&srv=rsu&p='.$period.'&tp='.$typeperiod.'&md=3&rs='.$course.'&it=1030123459';
}

function doRequest_with_FileGetContents($url)
{
return file_get_contents($url);
}

function getallUsers(){
global $DB;
$allusers=array();
$users= $DB->get_records('user');
foreach($users as $user){
$allusers[]= $user->username."<br/>";

}
return $allusers;
}
function processXML($xmlContent){
$xmlObj= new SimpleXMLElement($xmlContent);
$result=array();
foreach($xmlObj->unidade_curricular->inscritos->aluno as $aluno){
$result[]= $aluno->identificador."<br/>";

}
return $result;
}

$allUsers= getallUsers();
$dataClip= processXML($content_b);
$courseid= required_param('id', PARAM_INT); 
$context= get_context_instance(CONTEXT_COURSE, $courseid);//Getting students who are already enrolled                                                     
$students= get_role_users(5,$context);

if(is_array($dataClip)){ //eliminates warnings of Invalid Argument supplied in foreach
foreach($dataClip as $newdata){
    $duplicate=false;
    if(is_array($allUsers)){
    foreach($allUsers as $dataMoodle){
        // if there is a match
        if($newdata==$dataMoodle){
            // if student is enrolled on moodle course page.
           if($students){
           $duplicate=true;
        continue;
            }
    else {
        $duplicate=false;
        $results=array_intersect((array)$newdata,(array)$dataMoodle); // complains about not being an array
        //print_r($results);
        echo implode('<br/>',$results);
}

else{
    $duplicate= false;
    continue;
    } 
}
}
}
}

array_intersect 为我提供了两个数组之间的通用用户名,但是当我将其中一个添加到我的课程页面时,我没有得到任何输出。所以,就像 abc 和 ab 之间的交集是 [] 而不是 ab。

编辑:dataCLIP 有 300 多个名称,但其中有

玛雅

卡布拉尔

d.mateus

这是 Moodle 的所有用户

来宾

行政

xpto.xy

玛雅

d.mano

卡布拉尔

d.mateus

我的逻辑在哪里失败?

4

1 回答 1

1

For enrolling users, you might want to take a look at function enrol_user_bulk(stdClass $instance, $userids, ...) in /lib/enrollib.php

Something like this

$plugin = enrol_get_plugin('manual');
$courses = ... // get the distinct course ids
foreach ($courses as $course) {
    $instance = $DB->get_record('enrol', array('courseid' => $courseid, 'enrol' => 'manual');
    $userids = ... // get userid's to enrol on this course
    $plugin->enrol_user_bulk($instance, $userids) 
}
于 2014-02-05T20:27:32.997 回答