dttl3933 2014-02-05 12:14
浏览 34

关于array_intersect

I have 2 arrays. One with usernames from Moodle($allUsers), and the other one with usernames from an external source($dataClip). I need to compare them and add them in bulk, if not already enrolled.

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;
    } 
}
}
}
}

the array_intersect gives me the common usernames between the two arrays, but when I add one of them to my course page, I get no output. So, it´s like the intersection between abc and ab was [] instead of ab.

EDIT: dataCLIP has over 300 names, but among them there are

a.maia

a.cabral

d.mateus

And this is all users from Moodle

guest

admin

xpto.xy

a.maia

d.mano

a.cabral

d.mateus

Where does my logic fail?

  • 写回答

1条回答 默认 最新

  • doushi1960 2014-02-05 20:27
    关注

    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) 
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作