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?