Example #1
0
 /**
  * @param array referentiel array from xml tree
  * @return object import_referentiel object
  * modifie la base de donnees
  */
 function import_pedagogies($xmlpedagos)
 {
     global $DB;
     // recupere le tableau de lignes
     // selon les parametres soit cree une nouvelle instance
     // soit modifie une instance courante de la table referentiel_a_user_scol
     // get some error strings
     $error_message = '';
     // importer les pedagos
     $index = 0;
     $nbpedagos = 0;
     // compteur
     foreach ($xmlpedagos as $pedago) {
         // PEDAGOS
         // echo "<br />\n";
         // print_r($pedago);
         $index++;
         // $id=$this->getpath( $pedago, array('#','id',0,'#'), '', false, '');
         $login = $this->getpath($pedago, array('#', 'username', 0, '#'), '', false, '');
         $firstname = $this->getpath($pedago, array('#', 'firstname', 0, '#', 'text', 0, '#'), '', true, '');
         $lastname = $this->getpath($pedago, array('#', 'lastname', 0, '#', 'text', 0, '#'), '', true, '');
         $date_cloture = $this->getpath($pedago, array('#', 'date_cloture', 0, '#', 'text', 0, '#'), '', false, '');
         $promotion = $this->getpath($pedago, array('#', 'promotion', 0, '#', 'text', 0, '#'), '', false, '');
         $formation = $this->getpath($pedago, array('#', 'formation', 0, '#', 'text', 0, '#'), '', true, '');
         $pedagogie = $this->getpath($pedago, array('#', 'pedagogie', 0, '#', 'text', 0, '#'), '', true, '');
         $composante = $this->getpath($pedago, array('#', 'composante', 0, '#', 'text', 0, '#'), '', true, '');
         $num_groupe = $this->getpath($pedago, array('#', 'num_groupe', 0, '#', 'text', 0, '#'), '', true, '');
         $commentaire = $this->getpath($pedago, array('#', 'commentaire', 0, '#', 'text', 0, '#'), '', true, '');
         $code_referentiel = $this->getpath($pedago, array('#', 'code_referentiel', 0, '#', 'text', 0, '#'), '', true, '');
         // rechercher la formation
         if (!empty($formation) && !empty($pedagogie) && !empty($composante)) {
             $import_pedago = new stdClass();
             $import_pedago->date_cloture = $date_cloture;
             $import_pedago->promotion = addslashes($promotion);
             $import_pedago->formation = addslashes($formation);
             $import_pedago->pedagogie = addslashes($pedagogie);
             $import_pedago->composante = addslashes($composante);
             $import_pedago->num_groupe = addslashes($num_groupe);
             $import_pedago->commentaire = addslashes($commentaire);
             $userid = 0;
             if ($login != '') {
                 $userid = referentiel_get_userid_by_login($login);
             }
             if ($userid) {
                 // this routine initialises the import object
                 $import_association = new stdClass();
                 $import_association->userid = $userid;
                 // $tref=referentiel_get_infos_from_code_ref($code_referentiel);     // probablement vide
                 $import_association->refrefid = $this->ref_referentiel;
             }
             // verification dans la base
             $trouve_pedago = 0;
             $creerasso = 0;
             // insertion association necessaire
             if ($userid) {
                 $rec_assos = referentiel_get_a_user_pedago($userid, $this->ref_referentiel);
                 if ($rec_assos) {
                     // un enregistrement existe
                     $creerasso = 1;
                     // un enregistrement existe : update necessaire
                     foreach ($rec_assos as $rec_asso) {
                         if ($rec_asso) {
                             $import_association->id = $rec_asso->id;
                             $rec_pedago = referentiel_get_pedagogie($rec_asso->refpedago);
                             if ($rec_pedago && !$trouve_pedago) {
                                 // sinon pas utile de chercher plus loin
                                 if ($rec_pedago->num_groupe == $num_groupe && $rec_pedago->promotion == $promotion && $rec_pedago->formation == $formation && $rec_pedago->pedagogie == $pedagogie && $rec_pedago->composante == $composante) {
                                     // deja connu
                                     $trouve_pedago = $rec_pedago->id;
                                     $creerasso = 2;
                                     // rien a faire pour l'association
                                 }
                             }
                         }
                     }
                 }
             } else {
                 // MODIF JF 2013/05/23
                 // il faut probablement ajouter cet utilisateur à Moodle, mais comment faire ?
                 // A tout le moins signaler la difficulté
                 $error_message .= '<br/>' . get_string('user_unknown', 'referentiel', $login . ' ' . $firstname . ' ' . $lastname);
             }
             if (!$trouve_pedago) {
                 // verifier si une pedagogie identique existe
                 $rec_pedago = referentiel_get_id_pedago_from_data($num_groupe, $date_cloture, $promotion, $formation, $pedagogie, $composante);
                 if ($rec_pedago) {
                     $trouve_pedago = $rec_pedago->id;
                 }
             }
             if (!$trouve_pedago) {
                 // enregistrer  pedagogie
                 $trouve_pedago = $DB->insert_record("referentiel_pedagogie", $import_pedago);
             }
             if ($trouve_pedago) {
                 // mettre a jour association si necessaire
                 // faut-il mettre quelque chose à jour ?
                 $rec_pedago = referentiel_get_pedagogie($trouve_pedago);
                 if (!empty($commentaire) && $commentaire != $rec_pedago->commentaire) {
                     $rec_pedago->commentaire = $commentaire;
                     referentiel_update_pedagogie_record($rec_pedago);
                 }
                 if (!empty($date_cloture) && $date_cloture != $rec_pedago->date_cloture) {
                     $rec_pedago->date_cloture = $date_cloture;
                     referentiel_update_pedagogie_record($rec_pedago);
                 }
                 if ($userid) {
                     $import_association->refpedago = $trouve_pedago;
                     if ($creerasso == 1) {
                         // update
                         $DB->update_record("referentiel_a_user_pedagogie", $import_association);
                     } elseif ($creerasso == 0) {
                         // insertion
                         $id_asso = $DB->insert_record("referentiel_a_user_pedagogie", $import_association);
                     }
                 }
             }
         }
     }
     if (!empty($error_message)) {
         echo '<div class="error">';
         echo get_string('import_pedagogie_error', 'referentiel') . "\n";
         echo $error_message . "\n";
         echo '</div>' . "\n";
     }
     return true;
 }
Example #2
0
 /**
  * @param array referentiel array from xml tree
  * @return object import_referentiel object
  * modifie la base de donnees 
  */
 function import_pedagogies($lines)
 {
     // recupere le tableau de lignes
     // selon les parametres soit cree une nouvelle instance
     // soit modifie une instance courante de la table referentiel_a_user_scol
     global $SESSION;
     global $USER;
     global $CFG;
     global $DB;
     // initialiser les variables
     $date_creation = "";
     $in_pedago = false;
     // drapeau
     // get some error strings
     $error_noname = get_string('xmlimportnoname', 'referentiel');
     $error_nocode = get_string('xmlimportnocode', 'referentiel');
     $error_override = get_string('overriderisk', 'referentiel');
     $error_message = '';
     // DEBUT
     // Decodage
     $line = 0;
     // TRAITER LA LIGNE D'ENTETE
     $nbl = count($lines);
     if ($nbl > 0) {
         // premiere ligne entete fichier csv
         // echo "<br />$line : ".$lines[$line]."\n";
         //"#Moodle Referentiel pedagos CSV Export;latin1;Y:2009m:06d:11\n"
         $fields = explode($this->sep, str_replace("\r", "", $lines[$line]));
         $line++;
         if (substr($lines[$line], 0, 1) == '#') {
             // labels
             /// If a line is incorrectly formatted
             if (count($fields) < 3) {
                 if (count($fields) > 1 or strlen($fields[0]) > 1) {
                     // no error for blank lines
                     $this->error("ERROR " . $lines[$line] . ": Line " . $line . "incorrectly formatted - ignoring\n");
                 }
             }
             if (isset($fields[1]) && $fields[1] != "") {
                 $this->table_caractere_input = trim($fields[1]);
             }
             $date_creation = trim($fields[2]);
         }
     } else {
         $this->error("ERROR : CSV File incorrect\n");
     }
     if ($nbl > 1) {
         // deuxieme ligne
         // echo "<br />$line : ".$lines[$line]."\n";
         while ($line < $nbl) {
             // data : referentiel
             $fields = explode($this->sep, str_replace("\r", "", $lines[$line]));
             /// If a line is incorrectly formatted
             if (count($fields) < 3) {
                 if (count($fields) > 1 or strlen($fields[0]) > 1) {
                     // no error for blank lines
                     $this->error("ERROR " . $lines[$line] . ": Line " . $line . "incorrectly formatted");
                 }
             } else {
                 if (substr($lines[$line], 0, 1) == '#') {
                     // labels
                     // $id;$username;".$this->output_codage_caractere(referentiel_get_user_prenom($record->userid)).";".$this->output_codage_caractere(referentiel_get_user_nom($record->userid)).";$num_groupe;$date_cloture;$promotion;$formation;$pedagogie;$composante;$refrefid\n";
                     // #username;firstname;lastname;num_groupe;date_cloture;promotion;formation;pedagogie;composante;referentiel;
                     $l_username = trim($fields[0]);
                     $l_firstname = trim($fields[1]);
                     $l_lastname = trim($fields[2]);
                     $l_date_cloture = trim($fields[3]);
                     $l_promotion = trim($fields[4]);
                     $l_formation = trim($fields[5]);
                     $l_pedagogie = trim($fields[6]);
                     $l_composante = trim($fields[7]);
                     if (!empty($fields[8])) {
                         $l_num_groupe = trim($fields[8]);
                     } else {
                         $l_num_groupe = '';
                     }
                     if (!empty($fields[9])) {
                         $l_commentaire = trim($fields[9]);
                     } else {
                         $l_commentaire = '';
                     }
                     if (!empty($fields[10])) {
                         $l_referentiel = trim($fields[10]);
                     } else {
                         $l_referentiel = '';
                     }
                 } else {
                     $login = $this->input_codage_caractere(trim($fields[0]));
                     // username
                     $firstname = $this->input_codage_caractere(trim($fields[1]));
                     $lastname = $this->input_codage_caractere(trim($fields[2]));
                     if (!empty($fields[3])) {
                         $date_cloture = $this->input_codage_caractere(trim($fields[3]));
                     } else {
                         $date_cloture = '';
                     }
                     if (!empty($fields[4])) {
                         $promotion = $this->input_codage_caractere(trim($fields[4]));
                     } else {
                         $promotion = '';
                     }
                     if (!empty($fields[5])) {
                         $formation = $this->input_codage_caractere(trim($fields[5]));
                     } else {
                         $formation = '';
                     }
                     if (!empty($fields[6])) {
                         $pedagogie = $this->input_codage_caractere(trim($fields[6]));
                     } else {
                         $pedagogie = '';
                     }
                     if (!empty($fields[7])) {
                         $composante = $this->input_codage_caractere(trim($fields[7]));
                     } else {
                         $composante = '';
                     }
                     if (!empty($fields[8])) {
                         $num_groupe = $this->input_codage_caractere(trim($fields[8]));
                     } else {
                         $num_groupe = '';
                     }
                     if (!empty($fields[9])) {
                         $commentaire = $this->input_codage_caractere(trim($fields[9]));
                     } else {
                         $commentaire = '';
                     }
                     if (!empty($fields[10])) {
                         $code_referentiel = $this->input_codage_caractere(trim($fields[10]));
                     } else {
                         $code_referentiel = '';
                     }
                     // rechercher la formation
                     if (!empty($formation) && !empty($pedagogie) && !empty($composante)) {
                         $import_pedago = new stdClass();
                         $import_pedago->promotion = addslashes($promotion);
                         $import_pedago->num_groupe = addslashes($num_groupe);
                         $import_pedago->date_cloture = $date_cloture;
                         $import_pedago->formation = addslashes($formation);
                         $import_pedago->pedagogie = addslashes($pedagogie);
                         $import_pedago->composante = addslashes($composante);
                         $import_pedago->commentaire = addslashes($commentaire);
                         $userid = 0;
                         if ($login != '') {
                             $userid = referentiel_get_userid_by_login($login);
                         }
                         if ($userid) {
                             // this routine initialises the import object
                             $import_association = new stdClass();
                             $import_association->userid = $userid;
                             // $tref=referentiel_get_infos_from_code_ref($code_referentiel);     // probablement vide
                             $import_association->refrefid = $this->ref_referentiel;
                         }
                         // verification dans la base
                         $trouve_pedago = 0;
                         $creerasso = 0;
                         // insertion association necessaire
                         if ($userid) {
                             $rec_assos = referentiel_get_a_user_pedago($userid, $this->ref_referentiel);
                             if ($rec_assos) {
                                 // un enregistrement existe
                                 $creerasso = 1;
                                 // un enregistrement existe : update necessaire
                                 foreach ($rec_assos as $rec_asso) {
                                     if ($rec_asso) {
                                         $import_association->id = $rec_asso->id;
                                         $rec_pedago = referentiel_get_pedagogie($rec_asso->refpedago);
                                         if ($rec_pedago && !$trouve_pedago) {
                                             // sinon pas utile de chercher plus loin
                                             if ($rec_pedago->num_groupe == $num_groupe && $rec_pedago->promotion == $promotion && $rec_pedago->formation == $formation && $rec_pedago->pedagogie == $pedagogie && $rec_pedago->composante == $composante) {
                                                 // deja connu
                                                 $trouve_pedago = $rec_pedago->id;
                                                 $creerasso = 2;
                                                 // rien a faire pour l'association
                                             }
                                         }
                                     }
                                 }
                             }
                         } else {
                             // MODIF JF 2013/05/23
                             // il faut probablement ajouter cet utilisateur à Moodle, mais comment faire ?
                             // A tout le moins signaler la difficulté
                             $error_message .= '<br/>' . get_string('user_unknown', 'referentiel', $login . ' ' . $firstname . ' ' . $lastname);
                         }
                         if (!$trouve_pedago) {
                             // verifier si une pedagogie identique existe
                             $rec_pedago = referentiel_get_id_pedago_from_data($num_groupe, $date_cloture, $promotion, $formation, $pedagogie, $composante);
                             if ($rec_pedago) {
                                 $trouve_pedago = $rec_pedago->id;
                             }
                         }
                         if (!$trouve_pedago) {
                             // enregistrer  pedagogie
                             $trouve_pedago = $DB->insert_record("referentiel_pedagogie", $import_pedago);
                         }
                         if ($trouve_pedago) {
                             // mettre a jour association si necessaire
                             // faut-il mettre quelque chose à jour ?
                             $rec_pedago = referentiel_get_pedagogie($trouve_pedago);
                             if (!empty($commentaire) && $commentaire != $rec_pedago->commentaire) {
                                 $rec_pedago->commentaire = $commentaire;
                                 referentiel_update_pedagogie_record($rec_pedago);
                             }
                             if (!empty($date_cloture) && $date_cloture != $rec_pedago->date_cloture) {
                                 $rec_pedago->date_cloture = $date_cloture;
                                 referentiel_update_pedagogie_record($rec_pedago);
                             }
                             if ($userid) {
                                 $import_association->refpedago = $trouve_pedago;
                                 if ($creerasso == 1) {
                                     // update
                                     $DB->update_record("referentiel_a_user_pedagogie", $import_association);
                                 } elseif ($creerasso == 0) {
                                     // insertion
                                     $id_asso = $DB->insert_record("referentiel_a_user_pedagogie", $import_association);
                                 }
                             }
                         }
                     }
                 }
             }
             $line++;
         }
     }
     if (!empty($error_message)) {
         echo '<div class="error">';
         echo get_string('import_pedagogie_error', 'referentiel') . "\n";
         echo $error_message . "\n";
         echo '</div>' . "\n";
     }
     return true;
 }