0

私は2つの配列を持っています。1 つは Moodle からのユーザー名 ($allUsers) で、もう 1 つは外部ソース ($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 は、2 つの配列間で共通のユーザー名を提供しますが、そのうちの 1 つをコース ページに追加すると、出力が得られません。つまり、abc と ab の交点が ab ではなく [] だったようです。

編集: dataCLIP には 300 を超える名前がありますが、その中には

a.マイア

a.カブラル

d.mateus

そして、これはMoodleのすべてのユーザーです

ゲスト

管理者

xpto.xy

a.マイア

d.マノ

a.カブラル

d.mateus

私のロジックはどこで失敗しますか?

4

1 に答える 1

1

ユーザーを登録するには、/lib/enrollib.php の関数 enrol_user_bulk(stdClass $instance, $userids, ...) を参照してください。

このようなもの

$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 に答える