0

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?

user3178356
  • 73
  • 1
  • 7
  • Could you explain your code: if $allUsers and $dataClip are arrays of usernames (strings, right?), what contains in $newdata and $dataMoodle in your foreach-loops? – S Korolev Feb 05 '14 at 12:30
  • I guess I'm not understanding why you don't compare the arrays before starting the foreachs. Why not first find and remove the duplicates (or use array_diff to find the uniques) and if that give you a non empty array process those? – Elin Feb 05 '14 at 12:39
  • @S Korolev allUsers is a function which gets me all Moodle users. and dataClip is a function which gets me all values inside a tag called Identificador from an url. I will post the methods as well and the url – user3178356 Feb 05 '14 at 12:40
  • Why are you doing array_intersect when you've converted everything to scalars and you already know they are not duplicates? Why not $results[] = $newData; ? Also would you mind fixing your indenting? – Elin Feb 05 '14 at 12:44
  • @Elin I need to ensure that there are duplicates between the 2 arrays, because i want to add the student, registered on Moodle, to the course page. If the user is already enrolled on that course page, I would skip him and go to the next. If intersect ABC, i would like to see ABC. But if A is already enrolled on the course, i would like to see BC, not []. – user3178356 Feb 05 '14 at 13:04
  • Ok so you have $allUsers == array(A,B,C) and $dataClip == array(A) and you want to end up with $results == array (B,C), is that right? – Elin Feb 05 '14 at 13:24
  • @Elin yes. I erased all the looping and followed your advice on your first comment. After the declarations I only have array intersect now. I'm still fuzzy about this, since my doubts are mostly Moodle specific, yet I get no answers from Moodle.org. – user3178356 Feb 05 '14 at 13:34

1 Answers1

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) 
}
Russell England
  • 9,436
  • 1
  • 27
  • 41
  • Hello, there is no enrol_user_bulk method in /lib/enrollib.php. Perhaps it is deprecated? I'm using Moodle 2.5. Or did you mean the enrol_user function? And is there any way i can enroll them using the usernames I've stored in an array, instead of the userids? – user3178356 Feb 06 '14 at 10:04
  • Ah just realised the function is part of Totara not Moodle - there is a copy of the function on Moodle HQ github - https://github.com/moodlehq/totara/blob/t2-release-2.5/lib/enrollib.php - pretty much most of Moodle works on ID's, so you will need to user the ID's rather than the usernames. – Russell England Feb 06 '14 at 14:36
  • Ok, thank you. Going to dig in and probably make a new post in the next few days. – user3178356 Feb 06 '14 at 15:06
  • While you are there, you might want to take a look at Totara Sync - https://github.com/moodlehq/totara/tree/t2-release-2.5/admin/tool/totara_sync - this uses a csv file or a database table to synchronise users – Russell England Feb 06 '14 at 18:33
  • I saw a function call insert_records_via_batch on the enrol_bulk_user method, in lib/enrollib.php from the first totara link you provided. Where does this come from? – user3178356 Feb 07 '14 at 11:11