Question

I'm trying to extract some data from an URL and store it into a table I created in Moodle, and it shows the error in Title. My goal is to extract all the values inside the attribute "nome" on cursos>curso and put them on my table ecoclipaluno. Example Link. parameters of build URL, when changed, can give more values to store. Or less.

https://clip.unl.pt/sprs?lg=pt&year=2013&uo=97747&srv=rsu&p=1&tp=s&md=3&rs=8145&it=5&it=1030123459

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=5&it=1030123459';
}
function doRequest_with_FileGetContents($url)
{
return file_get_contents($url);
}
function processXMLforCourse($xmlContent){
$xmlObj= new SimpleXMLElement($xmlContent);
$result=array();
foreach($xmlObj->unidade_curricular->cursos->curso as $curso){
$result[]= $curso->attributes()->nome;
}
return $result;
}
$context=get_context_instance(CONTEXT_COURSE,$courseid);
//$shortname=$course->shortname;
$idnumber=$course->idnumber;
$timecreated=$course->startdate;
$typeperiod='s';
        if(date("m",$timecreated) >07 && date("m",$timecreated) <13){
            $period='1';
            $url=buildURL(date("Y",strtotime('+1 year',$timecreated)),$period,$typeperiod,$idnumber); // 1st semester

        }
        else{
            $period='2';
            $url=buildURL(date("Y",$timecreated),$period,$typeperiod,$idnumber); // 2nd semester

        }
$content_b=doRequest_with_FileGetContents($url);
$dataClipCourse= processXMLforCourse($content_b);
$DB->insert_record('ecoclipaluno',$dataClipCourse);

From the Moodle API of data manipulation, the method insert_record has 4 parameters, the last 2 being optional, but none of them specifies the field. I wanted to insert $dataClipCourse to the field coursename. How can I pull that off?

enter image description here

array(8) { [0]=> object(SimpleXMLElement)#37 (1) { [0]=> string(54) "Licenciatura em Engenharia Informática (Tronco comum)" } 1=> object(SimpleXMLElement)#36 (1) { [0]=> string(38) "Mestrado em Matemática e Aplicações" } 2=> object(SimpleXMLElement)#44 (1) { [0]=> string(92) "Mestrado em Matemática e Aplicações, Especialização em Álgebra, Lógica e Computação" } 3=> object(SimpleXMLElement)#45 (1) { [0]=> string(82) "Mestrado em Matemática e Aplicações, Especialização em Matemática Financeira" } [4]=> object(SimpleXMLElement)#46 (1) { [0]=> string(104) "Mestrado em Matemática e Aplicações, Especialização em Análise Numérica e Equações Diferenciais" } [5]=> object(SimpleXMLElement)#47 (1) { [0]=> string(114) "Mestrado em Matemática e Aplicações - Especialização em Atuariado, Estatística e Investigação Operacional" } [6]=> object(SimpleXMLElement)#48 (1) { [0]=> string(74) "Licenciatura em Engenharia Informática, Perfil de Ciências da Engenharia" } [7]=> object(SimpleXMLElement)#49 (1) { [0]=> string(72) "Licenciatura em Engenharia Informática, Perfil de Informática Aplicada" } }

Edit:error

enter image description here

Was it helpful?

Solution

The second parameter for insert_record() needs be an object rather than an array and have the field names as properties. eg:

$result = new stdClass();
$result->codigo = xxx;
$result->qualificacao_atribuida = xxx;

http://docs.moodle.org/dev/Data_manipulation_API#Inserting_Records

EDIT: I just noticed the processXMLforCourse() function returns an array of names - so maybe something like this

$dataClipCourse= processXMLforCourse($content_b);
foreach ($dataClipCourse as $nome) {
    $data = new stdClass();
    $data->nome = $nome->__toString();
    $DB->insert_record('ecoclipaluno', $data);
}

OTHER TIPS

The error message means that, after removing all member variables in $dataClipCourse that didn't match a fieldname in the table 'mdl_ecoclipaluno', there was no data left to insert.

I suggest you call 'var_dump($dataClipCourse);' to see what data was being inserted, then compare this to the definition of the table 'mdl_ecoclipaluno' in your database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top