Example #1
0
 /**
  * @param array referentiel array from xml tree
  * @return object import_referentiel object
  * modifie la base de donnees
  */
 function import_referentiel($xmlreferentiel)
 {
     // recupere le fichier xml
     // selon les parametres soit cree une nouvelle occurence
     // soit modifie une occurrence courante de referentiel
     global $SESSION;
     global $USER;
     global $CFG;
     $nbdomaines = 0;
     // compteur
     $nbcompetences = 0;
     // compteur
     $nbitems = 0;
     // compteur
     // print_r($xmlreferentiel);
     if (!isset($this->action) || isset($this->action) && $this->action != "selectreferentiel" && $this->action != "importreferentiel") {
         if (!(isset($this->course->id) && $this->course->id > 0) || !(isset($this->rreferentiel->id) && $this->rreferentiel->id > 0) || !(isset($this->coursemodule->id) && $this->coursemodule->id > 0)) {
             $this->error(get_string('incompletedata', 'referentiel'));
             return false;
         }
     } else {
         if (isset($this->action) && $this->action == "selectreferentiel") {
             if (!(isset($this->course->id) && $this->course->id > 0)) {
                 $this->error(get_string('incompletedata', 'referentiel'));
                 return false;
             }
         } else {
             if (isset($this->action) && $this->action == "importreferentiel") {
                 if (!(isset($this->course->id) && $this->course->id > 0)) {
                     $this->error(get_string('incompletedata', 'referentiel'));
                     return false;
                 }
             }
         }
     }
     $risque_ecrasement = false;
     // get some error strings
     $error_noname = get_string('xmlimportnoname', 'referentiel');
     $error_nocode = get_string('xmlimportnocode', 'referentiel');
     $error_override = get_string('overriderisk', 'referentiel');
     // this routine initialises the import object
     $re = $this->defaultreferentiel();
     //
     // $re->id = $this->getpath( $xmlreferentiel, array('#','id',0,'#'), '', false, '');
     $re->name = $this->getpath($xmlreferentiel, array('#', 'name', '0', '#'), '', true, $error_noname);
     $re->code_referentiel = $this->getpath($xmlreferentiel, array('#', 'code_referentiel', 0, '#'), '', true, $error_nocode);
     $re->description_referentiel = $this->getpath($xmlreferentiel, array('#', 'description_referentiel', 0, '#', 'text', 0, '#'), '', true, '');
     $re->url_referentiel = $this->getpath($xmlreferentiel, array('#', 'url_referentiel', 0, '#'), '', true, '');
     $re->seuil_certificat = $this->getpath($xmlreferentiel, array('#', 'seuil_certificat', 0, '#'), '', false, '');
     $re->minima_certificat = $this->getpath($xmlreferentiel, array('#', 'minima_certificat', 0, '#'), '', false, '');
     $re->timemodified = $this->getpath($xmlreferentiel, array('#', 'timemodified', 0, '#'), '', false, '');
     $re->nb_domaines = $this->getpath($xmlreferentiel, array('#', 'nb_domaines', 0, '#'), '', false, '');
     $re->liste_codes_competence = $this->getpath($xmlreferentiel, array('#', 'liste_codes_competence', 0, '#'), '', true, '');
     $re->liste_empreintes_competence = $this->getpath($xmlreferentiel, array('#', 'liste_empreintes_competence', 0, '#'), '', true, '');
     $re->logo_referentiel = $this->getpath($xmlreferentiel, array('#', 'logo_referentiel', 0, '#'), '', true, '');
     // $re->local = $this->getpath( $xmlreferentiel, array('#','course',0,'#'), '', false, '');
     /*
     // traitement d'une image associee
     // non implante
             $image = $this->getpath( $xmlreferentiel, array('#','image',0,'#'), $re->image );
             $image_base64 = $this->getpath( $xmlreferentiel, array('#','image_base64','0','#'),'' );
             if (!empty($image_base64)) {
                 $re->image = $this->importimagefile( $image, stripslashes($image_base64) );
             }
     */
     $re->export_process = false;
     $re->import_process = true;
     // le referentiel est toujours place dans le cours local d'appel
     $re->course = $this->course->id;
     $risque_ecrasement = false;
     if (!isset($this->action) || $this->action != "importreferentiel") {
         // importer dans le cours courant en remplacement du referentiel courant
         // Verifier si ecrasement referentiel local
         if (isset($re->name) && $re->name != "" && isset($re->code_referentiel) && $re->code_referentiel != "" && isset($re->id) && $re->id > 0 && isset($re->course) && $re->course > 0) {
             // sauvegarder ?
             if ($this->course->id == $re->course) {
                 if (isset($this->rreferentiel->id) && $this->rreferentiel->id == $re->id || isset($this->rreferentiel->name) && $this->rreferentiel->name == $re->name && (isset($this->rreferentiel->code_referentiel) && $this->rreferentiel->code_referentiel == $re->code_referentiel)) {
                     $risque_ecrasement = true;
                 }
             }
         }
     }
     if ($risque_ecrasement == false || $this->newinstance == 1) {
         // Enregistrer dans la base comme un nouveau referentiel_referentiel du cours courant
         $new_referentiel_id = referentiel_add_referentiel($re);
         $this->setReferentielId($new_referentiel_id);
         // DEBUG
         // echo "<br />DEBUG xml/format.php ligne 572<br />NEW REFERENTIEL ID ENREGISTRE : ".$this->new_referentiel_id."\n";
     } else {
         if ($risque_ecrasement == true && $this->override == 1) {
             // Enregistrer dans la base en remplaçant la version courante (update)
             // NE FAUDRAIT IL PAS SUPPRIMER LE REFERENTIEL AVANT DE LA RECHARGER ?
             $re->instance = $this->rreferentiel->id;
             // en realite instance est ici occurrence
             $re->referentiel_id = $this->rreferentiel->id;
             $ok = referentiel_update_referentiel($re);
             $new_referentiel_id = $this->rreferentiel->id;
         } else {
             // ni nouvelle instance ni recouvrement
             $this->error($error_override);
             return false;
         }
     }
     // MODIF JF 2012/03/09
     // importer le protocole
     $pindex = 0;
     $nbprotocoles = 0;
     // compteur
     $re->protocole = array();
     if (!empty($xmlreferentiel['#']['protocole'])) {
         $xmlprotocole = $xmlreferentiel['#']['protocole'];
         foreach ($xmlprotocole as $protocole) {
             // PROTOCOLE
             // print_r($protocole);
             $pindex++;
             $new_protocole = array();
             $new_protocole = $this->defaultprotocole();
             // $new_protocole->id=$this->getpath( $protocole, array('#','p_id',0,'#'), '', false, '');
             // $new_protocole->ref_occurrence=$this->getpath( $protocole, array('#','p_ref_occurrence',0,'#'), '', false, '');
             $new_protocole->seuil_referentiel = $this->getpath($protocole, array('#', 'p_seuil_referentiel', 0, '#'), '', false, '');
             $new_protocole->minima_referentiel = $this->getpath($protocole, array('#', 'p_minima_referentiel', 0, '#'), '', false, '');
             // La suite initialise en chargeant les domaines / compétences / items
             // $new_protocole->l_domaines_oblig=$this->getpath( $protocole, array('#','p_domaines_oblig',0,'#','text',0,'#'), '', true, '');
             // $new_protocole->l_seuils_domaines=$this->getpath( $protocole, array('#','p_seuils_domaines',0,'#','text',0,'#'), '', true, '');
             // $new_protocole->l_competences_oblig=$this->getpath( $protocole, array('#','p_competences_oblig',0,'#','text',0,'#'), '', true, '');
             // $new_protocole->l_seuils_competences=$this->getpath( $protocole, array('#','p_seuils_competences',0,'#','text',0,'#'), '', true, '');
             // $new_protocole->l_minimas_competences=$this->getpath( $protocole, array('#','p_minimas_competences',0,'#','text',0,'#'), '', true, '');
             // $new_protocole->l_items_oblig=$this->getpath( $protocole, array('#','p_items_oblig',0,'#','text',0,'#'), '', true, '');
             $new_protocole->timemodified = $this->getpath($protocole, array('#', 'p_timemodified', 0, '#'), '', false, '');
             $new_protocole->actif = $this->getpath($protocole, array('#', 'p_actif', 0, '#'), '', false, '');
             $new_protocole->commentaire = $this->getpath($protocole, array('#', 'p_commentaire', 0, '#', 'text', 0, '#'), '', true, '');
             // enregistrer
             $re->protocoles[$pindex] = $new_protocole;
             // sauvegarder dans la base
             // remplacer l'id du referentiel importe par l'id du referentiel cree
             // trafiquer les donnees pour appeler la fonction ad hoc
             $new_protocole->ref_occurrence = $new_referentiel_id;
             // DEBUG
             // echo "<br />DEBUG ./format/xml/format.php :: 710<br />\n";
             // print_object($new_protocole);
             if (referentiel_add_protocol($new_protocole)) {
                 $nbprotocoles++;
             }
         }
     } else {
         $new_protocole = $this->defaultprotocole();
         $new_protocole->ref_occurrence = $new_referentiel_id;
         $re->protocoles[1] = $new_protocole;
         if (referentiel_add_protocol($new_protocole)) {
             $nbprotocoles++;
         }
     }
     // importer les domaines
     $xmldomaines = $xmlreferentiel['#']['domaine'];
     $dindex = 0;
     $re->domaines = array();
     $nbdomaines = 0;
     // compteur
     foreach ($xmldomaines as $domaine) {
         // DOMAINES
         // print_r($domaine);
         $dindex++;
         $new_domaine = $this->defaultdomaine();
         // $new_domaine->id=$this->getpath( $domaine, array('#','id',0,'#'), '', false, '');
         $new_domaine->code_domaine = $this->getpath($domaine, array('#', 'code_domaine', 0, '#'), '', true, $error_nocode);
         $new_domaine->description_domaine = $this->getpath($domaine, array('#', 'description_domaine', 0, '#', 'text', 0, '#'), '', true, '');
         // MODIF JF 2012/05/22
         // retablir des sauts de ligne
         $new_domaine->description_domaine = preg_replace("/\\|\\|\\|/", "\r\n", $new_domaine->description_domaine);
         $new_domaine->num_domaine = $this->getpath($domaine, array('#', 'num_domaine', 0, '#'), '', false, '');
         $new_domaine->nb_competences = $this->getpath($domaine, array('#', 'nb_competences', 0, '#'), '', false, '');
         // $new_domaine->ref_referentiel=$this->getpath( $domaine, array('#','ref_referentiel',0,'#'), '', false, '');
         // MODIF 2012/03/08
         $new_domaine->type_domaine = $this->getpath($domaine, array('#', 'type_domaine', 0, '#'), '', false, '');
         if (empty($new_domaine->type_domaine)) {
             $new_domaine->type_domaine = 0;
         }
         $new_domaine->seuil_domaine = $this->getpath($domaine, array('#', 'seuil_domaine', 0, '#'), '', false, '');
         if (empty($new_domaine->seuil_domaine)) {
             $new_domaine->seuil_domaine = '0.0';
         }
         // MODIF 2012/03/26
         $new_domaine->minima_domaine = $this->getpath($domaine, array('#', 'minima_domaine', 0, '#'), '', false, '');
         if (empty($new_domaine->minima_domaine)) {
             $new_domaine->minima_domaine = '0';
         }
         // enregistrer
         $re->domaines[$dindex] = $new_domaine;
         // sauvegarder dans la base
         // remplacer l'id du referentiel importe par l'id du referentiel cree
         // trafiquer les donnees pour appeler la fonction ad hoc
         $new_domaine->ref_referentiel = $new_referentiel_id;
         $new_domaine->instance = $new_referentiel_id;
         // pour que ca marche
         $new_domaine->new_code_domaine = $new_domaine->code_domaine;
         $new_domaine->new_description_domaine = $new_domaine->description_domaine;
         $new_domaine->new_num_domaine = $new_domaine->num_domaine;
         $new_domaine->new_nb_competences = $new_domaine->num_domaine;
         // MODIF 2012/03/08
         $new_domaine->new_type_domaine = $new_domaine->type_domaine;
         $new_domaine->new_seuil_domaine = $new_domaine->seuil_domaine;
         // MODIF 2012/03/26
         $new_domaine->new_minima_domaine = $new_domaine->minima_domaine;
         $new_domaine_id = referentiel_add_domaine($new_domaine);
         if ($new_domaine_id) {
             $nbdomaines++;
         }
         // importer les competences
         $xmlcompetences = $domaine['#']['competence'];
         $cindex = 0;
         $re->domaines[$dindex]->competences = array();
         $nbcompetences = 0;
         // compteur
         foreach ($xmlcompetences as $competence) {
             $cindex++;
             $new_competence = array();
             $new_competence = $this->defaultcompetence();
             // $new_competence->id = $this->getpath( $competence, array('#','id',0,'#'), '', false, '');
             $new_competence->code_competence = $this->getpath($competence, array('#', 'code_competence', 0, '#'), '', true, $error_nocode);
             $new_competence->description_competence = $this->getpath($competence, array('#', 'description_competence', 0, '#', 'text', 0, '#'), '', true, '');
             // MODIF JF 2012/05/22
             // retablir des sauts de ligne
             $new_competence->description_competence = preg_replace("/\\|\\|\\|/", "\r\n", $new_competence->description_competence);
             $new_competence->num_competence = $this->getpath($competence, array('#', 'num_competence', 0, '#'), '', false, '');
             $new_competence->nb_item_competences = $this->getpath($competence, array('#', 'nb_item_competences', 0, '#'), '', false, '');
             // $new_competence->ref_domaine=$this->getpath( $competence, array('#','ref_domaine',0,'#'), '', false, '');
             // MODIF 2012/03/08
             $new_competence->type_competence = $this->getpath($competence, array('#', 'type_competence', 0, '#'), '', false, '');
             $new_competence->seuil_competence = $this->getpath($competence, array('#', 'seuil_competence', 0, '#'), '', false, '');
             if (empty($new_competence->type_competence)) {
                 $new_competence->type_competence = 0;
             }
             if (empty($new_competence->seuil_competence)) {
                 $new_competence->seuil_competence = '0.0';
             }
             // MODIF 2012/03/26
             $new_competence->minima_competence = $this->getpath($competence, array('#', 'minima_competence', 0, '#'), '', false, '');
             if (empty($new_competence->minima_competence)) {
                 $new_competence->minima_competence = 0;
             }
             // enregistrer
             $re->domaines[$dindex]->competences[$cindex] = $new_competence;
             // sauvegarder dans la base
             // remplacer l'id du referentiel importe par l'id du referentiel cree
             $new_competence->ref_domaine = $new_domaine_id;
             // trafiquer les donnees pour appeler la fonction ad hoc
             $new_competence->instance = $new_referentiel_id;
             // pour que ca marche
             $new_competence->new_code_competence = $new_competence->code_competence;
             $new_competence->new_description_competence = $new_competence->description_competence;
             $new_competence->new_ref_domaine = $new_competence->ref_domaine;
             $new_competence->new_num_competence = $new_competence->num_competence;
             $new_competence->new_nb_item_competences = $new_competence->nb_item_competences;
             // MODIF 2012/03/08
             $new_competence->new_type_competence = $new_competence->type_competence;
             $new_competence->new_seuil_competence = $new_competence->seuil_competence;
             // MODIF 2012/03/26
             $new_competence->new_minima_competence = $new_competence->minima_competence;
             // creation
             $new_competence_id = referentiel_add_competence($new_competence);
             if ($new_competence_id) {
                 $nbcompetences++;
                 // compteur
             }
             // importer les items
             $xmlitems = $competence['#']['item'];
             $iindex = 0;
             $re->domaines[$dindex]->competences[$cindex]->items = array();
             $nbitems = 0;
             // compteur
             foreach ($xmlitems as $item) {
                 $iindex++;
                 $new_item = array();
                 $new_item = $this->defaultitem();
                 // $new_item->id = $this->getpath( $item, array('#','id',0,'#'), '', false, '');
                 $new_item->code_item = $this->getpath($item, array('#', 'code', 0, '#'), '', true, $error_nocode);
                 $new_item->description_item = $this->getpath($item, array('#', 'description_item', 0, '#', 'text', 0, '#'), '', true, '');
                 // MODIF JF 2012/05/22
                 // retablir des sauts de ligne
                 $new_item->description_item = preg_replace("/\\|\\|\\|/", "\r\n", $new_item->description_item);
                 $new_item->num_item = $this->getpath($item, array('#', 'num_item', 0, '#'), '', false, '');
                 $new_item->type_item = $this->getpath($item, array('#', 'type_item', 0, '#'), '', true, '');
                 $new_item->poids_item = $this->getpath($item, array('#', 'poids_item', 0, '#'), '', false, '');
                 // $new_item->ref_competence=$this->getpath( $item, array('#','ref_competence',0,'#'), '', false, '');
                 // $new_item->ref_referentiel=$this->getpath( $item, array('#','ref_referentiel',0,'#'), '', false, '');
                 $new_item->empreinte_item = $this->getpath($item, array('#', 'empreinte_item', 0, '#'), '', false, '');
                 // enregistrer
                 $re->domaines[$dindex]->competences[$cindex]->items[$iindex] = $new_item;
                 // sauvegarder dans la base
                 // remplacer l'id du referentiel importe par l'id du referentiel cree
                 $new_item->ref_referentiel = $new_referentiel_id;
                 $new_item->ref_competence = $new_competence_id;
                 // trafiquer les donnees pour pouvoir appeler la fonction ad hoc
                 $new_item->instance = $new_item->ref_referentiel;
                 $new_item->new_ref_competence = $new_item->ref_competence;
                 $new_item->new_code_item = $new_item->code_item;
                 $new_item->new_description_item = $new_item->description_item;
                 $new_item->new_num_item = $new_item->num_item;
                 $new_item->new_type_item = $new_item->type_item;
                 $new_item->new_poids_item = $new_item->poids_item;
                 $new_item->new_empreinte_item = $new_item->empreinte_item;
                 // creer
                 $new_item_id = referentiel_add_item($new_item);
                 if ($new_item_id) {
                     $nbitems++;
                 }
                 // that's all folks
             }
             // items
             if ($nbitems > 0) {
                 // mettre a jour
                 referentiel_set_competence_nb_item($new_competence_id, $nbitems);
             }
         }
         // competences
         if ($nbcompetences > 0) {
             // mettre a jour
             referentiel_set_domaine_nb_competence($new_domaine_id, $nbcompetences);
         }
     }
     // mettre a jour
     if ($nbdomaines > 0) {
         // mettre a jour
         referentiel_set_referentiel_nb_domaine($new_referentiel_id, $nbdomaines);
     }
     return $re;
 }
Example #2
0
 /**
  * @param array referentiel array from xml tree
  * @return object import_referentiel object
  * modifie la base de donnees
  */
 function import_referentiel($lines)
 {
     // recupere le tableau de lignes
     // selon les parametres soit cree une nouvelle instance
     // soit modifie une instance courante de referentiel
     global $SESSION;
     global $USER;
     global $CFG;
     if (!$this->importation_referentiel_possible()) {
         exit;
     }
     // initialiser les variables
     // id du nouveau referentiel si celui ci doit être cree
     $new_referentiel_id = 0;
     $auteur = "";
     $l_id_referentiel = "id_referentiel";
     $l_code_referentiel = "code_referentiel";
     $l_description_referentiel = "description_referentiel";
     $l_date_creation = "date_creation";
     $l_nb_domaine = "nb_domaine";
     $l_seuil_certification = "seuil_certificat";
     $l_local = "local";
     $l_name = "name";
     $l_url_referentiel = "url_referentiel";
     $l_liste_competences = "liste_competences";
     $l_liste_empreintes = "liste_empreintes";
     $l_logo = "logo_referentiel";
     $ok_referentiel_charge = false;
     // MODIF JF 2012/03/09
     $ok_protocole_charge = false;
     $ok_domaine_charge = false;
     $ok_competence_charge = false;
     $ok_item_charge = false;
     $risque_ecrasement = false;
     // get some error strings
     $error_noname = get_string('xmlimportnoname', 'referentiel');
     $error_nocode = get_string('xmlimportnocode', 'referentiel');
     $error_override = get_string('overriderisk', 'referentiel');
     // DEBUT
     // Decodage
     $line = 0;
     // TRAITER LA LIGNE D'ENTETE
     $nbl = count($lines);
     if ($nbl > 0) {
         // premiere ligne entete fichier csv
         // echo "<br />DEBUG :: forme/csv/format.php :: 424\n";
         // echo "<br />LIGNE $line --------------<br />\n".$lines[$line]."<br />\n";
         // "#Moodle Referentiel CSV Export;latin1;Prénom NOM\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]);
             }
             $auteur = trim($fields[2]);
         }
     } else {
         $this->error("ERROR : CSV File incorrect\n");
     }
     // echo "<br />DEBUG :: 991 : $this->table_caractere_input\n";
     if ($nbl > 1) {
         // deuxieme ligne : entete referentiel
         // echo "<br />$line : ".$lines[$line]."\n";
         // #code_referentiel;name;description_referentiel;url_referentiel;date_creation;
         // nb_domaines;seuil_certification;liste_competences
         $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");
             }
         }
         if (substr($lines[$line], 0, 1) == '#') {
             // labels
             $l_code_referentiel = trim($fields[0]);
             $l_name = trim($fields[1]);
             $l_description_referentiel = trim($fields[2]);
             if (isset($fields[3])) {
                 $l_url_referentiel = trim($fields[3]);
             } else {
                 $l_url_referentiel = "";
             }
             if (isset($fields[4])) {
                 $l_date_creation = trim($fields[4]);
             } else {
                 $l_date_creation = "";
             }
             if (isset($fields[5])) {
                 $l_nb_domaines = trim($fields[5]);
             } else {
                 $l_nb_domaines = "";
             }
             if (isset($fields[6])) {
                 $l_seuil_certificat = trim($fields[6]);
             } else {
                 $l_seuil_certificat = "";
             }
             if (isset($fields[7])) {
                 $l_liste_competences = trim($fields[7]);
             } else {
                 $l_liste_competences = "";
             }
             if (isset($fields[8])) {
                 $l_liste_empreintes = trim($fields[8]);
             } else {
                 $l_liste_empreintes = "";
             }
             if (isset($fields[9])) {
                 $l_logo = trim($fields[9]);
             } else {
                 $l_logo = "";
             }
             // MODIF JF 2012/03/26
             if (isset($fields[10])) {
                 $l_minima_certificat = trim($fields[10]);
             } else {
                 $l_minima_certificat = "";
             }
         } else {
             // data  : referentiel
             $code_referentiel = $this->input_codage_caractere(trim($fields[0]));
             $name = $this->input_codage_caractere(trim($fields[1]));
             $description_referentiel = $this->input_codage_caractere(trim($fields[2]));
             if (isset($fields[3])) {
                 $url_referentiel = trim($fields[3]);
             } else {
                 $url_referentiel = "";
             }
             if (isset($fields[4])) {
                 $date_creation = trim($fields[4]);
             } else {
                 $date_creation = "";
             }
             if (isset($fields[5])) {
                 $nb_domaines = trim($fields[5]);
             } else {
                 $nb_domaines = "";
             }
             if (isset($fields[6])) {
                 $seuil_certificat = trim($fields[6]);
             } else {
                 $seuil_certificat = 0.0;
             }
             if (isset($fields[7])) {
                 $liste_competences = $this->input_codage_caractere(trim($fields[7]));
             } else {
                 $liste_competences = "";
             }
             if (isset($fields[8])) {
                 $liste_empreintes = $this->input_codage_caractere(trim($fields[8]));
             } else {
                 $liste_empreintes = "";
             }
             if (isset($fields[9])) {
                 $logo_referentiel = trim($fields[9]);
             } else {
                 $logo_referentiel = "";
             }
             // MODIF JF 2012/03/26
             if (isset($fields[10])) {
                 $minima_certificat = trim($fields[10]);
             } else {
                 $minima_certificat = 0;
             }
             $ok_referentiel_charge = true;
         }
         $line++;
     }
     // maintenant les données indispensables
     while ($line < $nbl && $ok_referentiel_charge == false) {
         // data : referentiel
         // echo "<br />$line : ".$lines[$line]."\n";
         // #referentiel_id;code_referentiel;description_referentiel;date_creation;
         // nb_domaines;seuil_certificat;local;name;url_referentiel;liste_competences
         $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");
             }
             continue;
         }
         // DEBUG
         // echo "<br />DEBUG : ./mod/refrentiel/format/csv/format.php :: 560<br >\n";
         // print_r($fields);
         // data  : referentiel
         $code_referentiel = $this->input_codage_caractere(trim($fields[0]));
         $name = $this->input_codage_caractere(trim($fields[1]));
         $description_referentiel = $this->input_codage_caractere(trim($fields[2]));
         if (isset($fields[3])) {
             $url_referentiel = trim($fields[3]);
         } else {
             $url_referentiel = "";
         }
         if (isset($fields[4])) {
             $date_creation = trim($fields[4]);
         } else {
             $date_creation = "";
         }
         if (isset($fields[5])) {
             $nb_domaines = trim($fields[5]);
         } else {
             $nb_domaines = "";
         }
         if (isset($fields[6])) {
             $seuil_certificat = trim($fields[6]);
         } else {
             $seuil_certificat = 0.0;
         }
         if (isset($fields[7])) {
             $liste_competences = $this->input_codage_caractere(trim($fields[7]));
         } else {
             $liste_competences = "";
         }
         if (isset($fields[8])) {
             $liste_empreintes = $this->input_codage_caractere(trim($fields[8]));
         } else {
             $liste_empreintes = "";
         }
         if (isset($fields[9])) {
             $logo_referentiel = trim($fields[9]);
         } else {
             $logo_referentiel = "";
         }
         // MODIF JF 2012/03/26
         if (isset($fields[10])) {
             $minima_certificat = trim($fields[10]);
         } else {
             $minima_certificat = 0;
         }
         $ok_referentiel_charge = true;
         $line++;
     }
     if (!$ok_referentiel_charge) {
         $this->error(get_string('incompletedata', 'referentiel'));
     }
     // this routine initialises the import object
     $re = $this->defaultreferentiel();
     $re->name = str_replace("'", " ", $name);
     // $re->name=addslashes($name);
     $re->code_referentiel = $code_referentiel;
     $re->description_referentiel = str_replace("'", "`", $description_referentiel);
     // $re->description_referentiel=addslashes($description_referentiel);
     $re->url_referentiel = $url_referentiel;
     $re->seuil_certificat = $seuil_certificat;
     $re->minima_certificat = $minima_certificat;
     $re->timemodified = $date_creation;
     $re->nb_domaines = $nb_domaines;
     $re->liste_codes_competence = $liste_competences;
     $re->liste_empreintes_competence = $liste_empreintes;
     $re->logo_referentiel = $logo_referentiel;
     /*
     // GROS BUG
     if ($id_referentiel!=""){
     	$re->id=$id_referentiel;
     }
     */
     $re->id = 0;
     // DEBUG
     // print_r($re);
     // RISQUE ECRASEMENT ?
     $risque_ecrasement = false;
     //
     if ($this->rreferentiel && $this->rreferentiel->id > 0) {
         // charger le referentiel associé à l'instance
         $this->rreferentiel = referentiel_get_referentiel_referentiel($this->rreferentiel->id);
         if ($this->rreferentiel) {
             $risque_ecrasement = $this->rreferentiel->name == $re->name && $this->rreferentiel->code_referentiel == $re->code_referentiel;
         }
     }
     // SI OUI arrêter
     if ($risque_ecrasement == true) {
         if ($this->override != 1) {
             $this->error($error_override);
         } else {
             // le referentiel courant est remplace
             $new_referentiel_id = $this->rreferentiel->id;
             $re->id = $new_referentiel_id;
         }
     }
     $re->export_process = false;
     $re->import_process = true;
     // le referentiel est toujours place dans le cours local d'appel
     $re->course = $this->course->id;
     $risque_ecrasement = false;
     if (!isset($this->action) || $this->action != "importreferentiel") {
         // importer dans le cours courant en remplacement du referentiel courant
         // Verifier si ecrasement referentiel local
         if (isset($re->name) && $re->name != "" && isset($re->code_referentiel) && $re->code_referentiel != "" && isset($re->id) && $re->id > 0 && isset($re->course) && $re->course > 0) {
             // sauvegarder ?
             if ($this->course->id == $re->course) {
                 if (isset($this->rreferentiel->id) && $this->rreferentiel->id == $re->id || isset($this->rreferentiel->name) && $this->rreferentiel->name == $re->name && (isset($this->rreferentiel->code_referentiel) && $this->rreferentiel->code_referentiel == $re->code_referentiel)) {
                     $risque_ecrasement = true;
                 }
             }
         }
     }
     // DEBUG
     /*
     if ($risque_ecrasement)
     	echo "<br />DEBUG : 607 : Risque d'ecrasement N:$this->newinstance O:$this->override\n";
     else
     	echo "<br />DEBUG : 607 : Pas de risque d'ecrasement N:$this->newinstance O:$this->override\n";
     */
     if ($risque_ecrasement == false || $this->newinstance == 1) {
         // Enregistrer dans la base comme un nouveau referentiel du cours courant
         // DEBUG
         // echo "<br />DEBUG csv/format.php ligne 704<br />\n";
         // print_object($re);
         // exit;
         $new_referentiel_id = referentiel_add_referentiel($re);
         // retourne un id de la table refrentiel_referentiel
         $this->setReferentielId($new_referentiel_id);
     } else {
         if ($risque_ecrasement == true && $this->override == 1) {
             // Enregistrer dans la base en remplaçant la version courante (update)
             // NE FAUDRAIT IL PAS SUPPRIMER LE REFERENTIEL AVANT DE LA RECHARGER ?
             $re->instance = $this->rreferentiel->id;
             $re->referentiel_id = $this->rreferentiel->id;
             // DEBUG
             // echo "<br />DEBUG csv/format.php ligne 638<br />MISE A JOUR  : ".$r->rreferentiel_id."\n";
             $ok = referentiel_update_referentiel($re);
             // retourne un id de la table referentiel_referentiel
             $new_referentiel_id = $this->rreferentiel->id;
         } else {
             // ni nouvelle instance ni recouvrement
             $this->error("ERREUR 2 " . $error_override);
             return false;
         }
     }
     if (isset($new_referentiel_id) && $new_referentiel_id > 0) {
         // IMPORTER LE RESTE DU REFERENTIEL
         $dindex = 0;
         $cindex = 0;
         $iindex = 0;
         $re->domaines = array();
         $new_domaine_id = 0;
         $new_competence_id = 0;
         $numero_domaine = 0;
         // compteur pour suppleer le numero si non importe
         $numero_competence = 0;
         $numero_item = 0;
         $num_domaine = 0;
         $num_competence = 0;
         $type_domaine = 0;
         $seuil_domaine = 0.0;
         $type_competence = 0;
         $seuil_competence = 0.0;
         $minima_domaine = 0.0;
         $minima_competence = 0;
         $num_item = 0;
         $is = "";
         $pindex = 0;
         $is_protocole = false;
         $ok_protocole_charge = false;
         $nbprotocoles = 0;
         $is_domaine = false;
         $is_competence = false;
         $is_item = false;
         $mode = "add";
         while ($line < $nbl) {
             // echo "<br />DEBUG 652 :: <br />".$lines[$line]."\n";
             $fields = explode($this->sep, str_replace("\r", "", $lines[$line]));
             if (count($fields) < 2) {
                 if (count($fields) > 1 or strlen($fields[0]) > 1) {
                     // no error for blank lines
                     $this->error("ERROR " . $lines[$line] . ": Line " . $line . "incorrectly formatted");
                 }
                 continue;
             }
             // print_r($fields);
             // Label ou data ?
             // echo "<br />".substr($fields[0],0,1)."\n";
             if (substr($fields[0], 0, 1) == '#') {
                 // labels
                 // on s'en sert pour construire l'arbre
                 $is = trim($fields[0]);
                 $is_protocole = false;
                 $is_domaine = false;
                 $is_competence = false;
                 $is_item = false;
                 switch ($is) {
                     case '#protocole_seuil':
                         // #protocole_seuil;l_domaines_oblig;l_seuils_domaines;l_competences_oblig;l_seuils_competences;l_items_oblig;timemodified;actif;commentaire
                         $is_protocole = true;
                         break;
                     case '#code_domaine':
                         // #code_domaine;description_domaine;num_domaine;nb_competences;type_domaine;seuil_domaine
                         $is_domaine = true;
                         break;
                     case '#code_competence':
                         // #code_competence;description_competence;num_competence;nb_item_competences;type_competence;seuil_competence
                         $is_competence = true;
                         break;
                     case '#code_item':
                         // #code_item;description_item;type_item;poids_item;num_item
                         $is_item = true;
                         break;
                     default:
                         $this->error("ERROR : CSV File incorrect line number:" . $line . "\n");
                         break;
                 }
             } else {
                 if (isset($is) && $is != "") {
                     // data
                     switch ($is) {
                         // MODIF JF 2012/03/09
                         case '#protocole_seuil':
                             // Protocole
                             // data
                             $seuil_referentiel = trim($fields[0]);
                             $minima_referentiel = trim($fields[1]);
                             $l_domaines_oblig = addslashes($this->input_codage_caractere(trim($fields[2])));
                             $l_seuils_domaines = addslashes($this->input_codage_caractere(trim($fields[3])));
                             $l_minimas_domaines = addslashes($this->input_codage_caractere(trim($fields[4])));
                             $l_competences_oblig = addslashes($this->input_codage_caractere(trim($fields[5])));
                             $l_seuils_competences = addslashes($this->input_codage_caractere(trim($fields[6])));
                             $l_minimas_competences = addslashes($this->input_codage_caractere(trim($fields[7])));
                             $l_items_oblig = addslashes($this->input_codage_caractere(trim($fields[8])));
                             $timemodified = trim($fields[9]);
                             $actif = trim($fields[10]);
                             $commentaire = addslashes($this->input_codage_caractere(trim($fields[11])));
                             // Creer un enregistrement
                             $new_protocole = $this->defaultprotocole();
                             // sauvegarder dans la base
                             // remplacer l'id du referentiel importe par l'id du referentiel cree
                             // trafiquer les donnees pour appeler la fonction ad hoc
                             $new_protocole->ref_occurrence = $new_referentiel_id;
                             // $new_protocole->id=$this->getpath( $protocole, array('#','p_id',0,'#'), '', false, '');
                             // $new_protocole->ref_occurrence;
                             $new_protocole->seuil_referentiel = $seuil_referentiel;
                             $new_protocole->minima_referentiel = $minima_referentiel;
                             // A recreer a partir des domaines et competences pourr eviter incoherences
                             // La suite initialise en chargeant les domaines / compétences / items
                             // $new_protocole->l_domaines_oblig=$l_competences_oblig;
                             // $new_protocole->l_seuils_domaines= $l_seuils_domaines;
                             // $new_protocole->l_competences_oblig=$l_competences_oblig;
                             // $new_protocole->l_seuils_competences=$l_seuils_competences;
                             // $new_protocole->l_minimas_competences=$l_minimas_competences;
                             // $new_protocole->l_items_oblig= $l_items_oblig;
                             $new_protocole->timemodified = $timemodified;
                             $new_protocole->actif = $actif;
                             $new_protocole->commentaire = $commentaire;
                             // sauvegarder dans la base
                             // remplacer l'id du referentiel importe par l'id du referentiel cree
                             // trafiquer les donnees pour appeler la fonction ad hoc
                             $new_protocole->ref_occurrence = $new_referentiel_id;
                             // DEBUG
                             // echo "<br />DEBUG ./format/csv/format.php :: 710<br />\n";
                             // print_object($new_protocole);
                             if (referentiel_add_protocol($new_protocole)) {
                                 $nbprotocoles++;
                             }
                             // enregistrer
                             $pindex++;
                             $re->protocoles[$pindex] = $new_protocole;
                             $ok_protocole_charge = true;
                             break;
                         case '#code_domaine':
                             // $code_domaine;$description_domaine;$num_domaine;$nb_competences
                             // Domaines
                             // data
                             $code_domaine = addslashes($this->input_codage_caractere(trim($fields[0])));
                             $description_domaine = addslashes($this->input_codage_caractere(trim($fields[1])));
                             $num_domaine = trim($fields[2]);
                             $nb_competences = trim($fields[3]);
                             if (isset($fields[4])) {
                                 $type_domaine = trim($fields[4]);
                             } else {
                                 $type_domaine = 0;
                             }
                             if (isset($fields[5])) {
                                 $seuil_domaine = trim($fields[5]);
                             } else {
                                 $seuil_domaine = 0;
                             }
                             if (isset($fields[6])) {
                                 $minima_domaine = trim($fields[6]);
                             } else {
                                 $minima_domaine = 0;
                             }
                             if ($code_domaine != "") {
                                 // Creer un domaine
                                 $numero_domaine++;
                                 $new_domaine = array();
                                 $new_domaine = $this->defaultdomaine();
                                 $new_domaine->code_domaine = $code_domaine;
                                 if ($description_domaine != "") {
                                     $new_domaine->description_domaine = $description_domaine;
                                 }
                                 if ($num_domaine != "") {
                                     $new_domaine->num_domaine = $num_domaine;
                                 } else {
                                     $new_domaine->num_domaine = $numero_domaine;
                                 }
                                 if ($nb_competences != "") {
                                     $new_domaine->nb_competences = $nb_competences;
                                 } else {
                                     $new_domaine->nb_competences = 0;
                                 }
                                 if ($type_domaine != "") {
                                     $new_domaine->type_domaine = $type_domaine;
                                 } else {
                                     $new_domaine->type_domaine = 0;
                                 }
                                 if ($seuil_domaine != "") {
                                     $new_domaine->seuil_domaine = $seuil_domaine;
                                 } else {
                                     $new_domaine->seuil_domaine = 0.0;
                                 }
                                 if ($minima_domaine != "") {
                                     $new_domaine->minima_domaine = $minima_domaine;
                                 } else {
                                     $new_domaine->minima_domaine = 0;
                                 }
                                 $new_domaine->ref_referentiel = $new_referentiel_id;
                                 // sauvegarder dans la base
                                 // remplacer l'id du referentiel importe par l'id du referentiel cree
                                 // trafiquer les donnees pour appeler la fonction ad hoc
                                 $new_domaine->ref_referentiel = $new_referentiel_id;
                                 $new_domaine->instance = $new_referentiel_id;
                                 // pour que ca marche
                                 $new_domaine->new_code_domaine = $new_domaine->code_domaine;
                                 $new_domaine->new_description_domaine = $new_domaine->description_domaine;
                                 $new_domaine->new_num_domaine = $new_domaine->num_domaine;
                                 $new_domaine->new_nb_competences = $new_domaine->num_domaine;
                                 $new_domaine->new_type_domaine = $new_domaine->type_domaine;
                                 $new_domaine->new_seuil_domaine = $new_domaine->seuil_domaine;
                                 $new_domaine->new_minima_domaine = $new_domaine->minima_domaine;
                                 $new_domaine_id = referentiel_add_domaine($new_domaine);
                                 if (isset($new_domaine_id) && $new_domaine_id > 0) {
                                     $new_domaine->id = $new_domaine_id;
                                 } else {
                                     $new_domaine->id = 0;
                                 }
                                 // enregistrer
                                 $dindex++;
                                 $re->domaines[$dindex] = $new_domaine;
                                 $cindex = 0;
                                 $re->domaines[$dindex]->competences = array();
                                 $numero_competence = 0;
                                 $ok_domaine_charge = true;
                             } else {
                                 $ok_domaine_charge = false;
                             }
                             break;
                         case '#code_competence':
                             // $competence_id;$code_competence;$description_competence;$ref_domaine;
                             // $num_competence;$nb_item_competences
                             $code_competence = addslashes($this->input_codage_caractere(trim($fields[0])));
                             $description_competence = addslashes($this->input_codage_caractere(trim($fields[1])));
                             $num_competence = trim($fields[2]);
                             $nb_item_competences = trim($fields[3]);
                             if (isset($fields[4])) {
                                 $type_competence = trim($fields[4]);
                             } else {
                                 $type_competence = 0;
                             }
                             if (isset($fields[5])) {
                                 $seuil_competence = trim($fields[5]);
                             } else {
                                 $seuil_competence = 0;
                             }
                             if (isset($fields[6])) {
                                 $minima_competence = trim($fields[6]);
                             } else {
                                 $minima_competence = 0;
                             }
                             if ($code_competence != "" && $ok_domaine_charge && $new_domaine_id > 0) {
                                 // Creer une competence
                                 $new_competence_id = 0;
                                 $numero_competence++;
                                 $new_competence = array();
                                 $new_competence = $this->defaultcompetence();
                                 $new_competence->id = 0;
                                 $new_competence->code_competence = $code_competence;
                                 if ($description_competence != "") {
                                     $new_competence->description_competence = $description_competence;
                                 }
                                 if ($num_competence != "") {
                                     $new_competence->num_competence = $num_competence;
                                 } else {
                                     $new_competence->num_competence = $numero_competence;
                                 }
                                 if ($nb_item_competences != "") {
                                     $new_competence->nb_item_competences = $nb_item_competences;
                                 } else {
                                     $new_competence->nb_item_competences = 0;
                                 }
                                 if ($type_competence != "") {
                                     $new_competence->type_competence = $type_competence;
                                 } else {
                                     $new_competence->type_competence = 0;
                                 }
                                 if ($seuil_competence != "") {
                                     $new_competence->seuil_competence = $seuil_competence;
                                 } else {
                                     $new_competence->seuil_competence = 0.0;
                                 }
                                 if ($minima_competence != "") {
                                     $new_competence->minima_competence = $minima_competence;
                                 } else {
                                     $new_competence->minima_competence = 0;
                                 }
                                 if (isset($new_domaine_id) && $new_domaine_id > 0) {
                                     $new_competence->ref_domaine = $new_domaine_id;
                                 } else {
                                     $new_competence->ref_domaine = 0;
                                 }
                                 // sauvegarder dans la base
                                 // remplacer l'id du referentiel importe par l'id du referentiel cree
                                 $new_competence->ref_domaine = $new_domaine_id;
                                 // trafiquer les donnees pour appeler la fonction ad hoc
                                 $new_competence->instance = $new_referentiel_id;
                                 // pour que ca marche
                                 $new_competence->new_code_competence = $new_competence->code_competence;
                                 $new_competence->new_description_competence = $new_competence->description_competence;
                                 $new_competence->new_ref_domaine = $new_domaine_id;
                                 $new_competence->new_num_competence = $new_competence->num_competence;
                                 $new_competence->new_nb_item_competences = $new_competence->nb_item_competences;
                                 $new_competence->new_type_competence = $new_competence->type_competence;
                                 $new_competence->new_seuil_competence = $new_competence->seuil_competence;
                                 $new_competence->new_minima_competence = $new_competence->minima_competence;
                                 // creation
                                 $new_competence_id = referentiel_add_competence($new_competence);
                                 $new_competence->id = $new_competence_id;
                                 // enregistrer
                                 $cindex++;
                                 $re->domaines[$dindex]->competences[$cindex] = $new_competence;
                                 $iindex = 0;
                                 // nouveaux items à suivre
                                 $re->domaines[$dindex]->competences[$cindex]->items = array();
                                 $numero_item = 0;
                                 $ok_competence_charge = true;
                             } else {
                                 $ok_competence_charge = false;
                             }
                             break;
                         case '#code_item':
                             // $code_item;$description_item;$type_item;$poids_item;$num_item;$empreinte_item
                             $code_item = $this->input_codage_caractere(addslashes(trim($fields[0])));
                             $description_item = $this->input_codage_caractere(addslashes(trim($fields[1])));
                             $type_item = $this->input_codage_caractere(addslashes(trim($fields[2])));
                             $poids_item = trim($fields[3]);
                             $num_item = trim($fields[4]);
                             if (isset($fields[5]) && trim($fields[5]) != "") {
                                 $empreinte_item = trim($fields[5]);
                             } else {
                                 $empreinte_item = "1";
                             }
                             if ($code_item != "" && $ok_competence_charge && $new_competence_id > 0) {
                                 // Creer un domaine
                                 $numero_item++;
                                 $new_item = array();
                                 $new_item = $this->defaultitem();
                                 $new_item->code_item = $code_item;
                                 if ($description_item != "") {
                                     $new_item->description_item = $description_item;
                                 }
                                 $new_item->ref_referentiel = $new_referentiel_id;
                                 $new_item->ref_competence = $new_competence_id;
                                 $new_item->type_item = $type_item;
                                 $new_item->poids_item = $poids_item;
                                 if ($num_item != "") {
                                     $new_item->num_item = $num_item;
                                 } else {
                                     $new_item->num_item = $numero_item;
                                 }
                                 $new_item->empreinte_item = $empreinte_item;
                                 // sauvegarder dans la base
                                 // remplacer l'id du referentiel importe par l'id du referentiel cree
                                 $new_item->ref_referentiel = $new_referentiel_id;
                                 $new_item->ref_competence = $new_competence_id;
                                 // trafiquer les donnees pour pouvoir appeler la fonction ad hoc
                                 $new_item->instance = $new_item->ref_referentiel;
                                 $new_item->new_ref_competence = $new_item->ref_competence;
                                 $new_item->new_code_item = $new_item->code_item;
                                 $new_item->new_description_item = $new_item->description_item;
                                 $new_item->new_num_item = $new_item->num_item;
                                 $new_item->new_type_item = $new_item->type_item;
                                 $new_item->new_poids_item = $new_item->poids_item;
                                 $new_item->new_empreinte_item = $new_item->empreinte_item;
                                 // creer
                                 $new_item_id = referentiel_add_item($new_item);
                                 $new_item->id = $new_item_id;
                                 $iindex++;
                                 $re->domaines[$dindex]->competences[$cindex]->items[$iindex] = $new_item;
                                 $ok_item_charge = true;
                             } else {
                                 $ok_item_charge = false;
                             }
                             break;
                         default:
                             $this->error("ERROR : CSV File incorrect line number:" . $line . "\n");
                             break;
                     }
                 }
             }
             // that's all folks
             $line++;
         }
         // end of while loop
         if ($mode == "add") {
             // rien de special ici ?
         }
         return $re;
     }
     return false;
 }