2016-04-30 10:19:51 +00:00
|
|
|
<?php
|
|
|
|
|
2016-10-18 13:11:37 +00:00
|
|
|
namespace api\module;
|
2016-04-30 10:19:51 +00:00
|
|
|
use \manager\sessionManager;
|
2016-11-05 10:56:03 +00:00
|
|
|
use \database\core\DatabaseDriver;
|
2016-04-30 10:19:51 +00:00
|
|
|
use \manager\ManagerError;
|
2016-10-18 13:11:37 +00:00
|
|
|
use \api\core\ModuleRequest;
|
|
|
|
use \database\core\Repo;
|
|
|
|
use \lightdb\core\lightdb;
|
2016-04-30 10:19:51 +00:00
|
|
|
|
2016-06-20 05:54:40 +00:00
|
|
|
|
2016-04-30 10:19:51 +00:00
|
|
|
class input{
|
|
|
|
|
2016-11-23 15:36:25 +00:00
|
|
|
|
|
|
|
/* RETOURNE SI UNE RELATION EST DEJA DANS LA LISTE
|
|
|
|
*
|
|
|
|
* @relset<Array> Liste des relations existantes
|
|
|
|
* @idA<int> id du premier sujet concerné
|
|
|
|
* @idB<int> id du second sujet concerné
|
|
|
|
* @type<int> type de relation
|
|
|
|
*
|
|
|
|
* @return already<Boolean> VRAI si déja dans la liste
|
|
|
|
*
|
|
|
|
*/
|
2016-11-23 15:57:52 +00:00
|
|
|
private static function relAlready(&$relset, $idA, $idB, $type){
|
|
|
|
|
|
|
|
// relation alter-alter (0 aucun lien ; 1 lien)
|
|
|
|
$alteralter = in_array($type, [0, 1]);
|
2016-11-23 15:36:25 +00:00
|
|
|
|
|
|
|
/* (1) Pour chaque relation existante */
|
2016-11-23 15:57:52 +00:00
|
|
|
foreach($relset as $r=>$rel){
|
|
|
|
|
2016-11-23 15:36:25 +00:00
|
|
|
|
2016-11-23 15:57:52 +00:00
|
|
|
// {2} si même A / B //
|
2016-11-23 15:36:25 +00:00
|
|
|
$sameAB = $rel['idA'] == $idA && $rel['idB'] == $idB;
|
|
|
|
|
2016-11-23 15:57:52 +00:00
|
|
|
// {3} si même B / A //
|
2016-11-23 15:36:25 +00:00
|
|
|
$sameBA = $rel['idA'] == $idB && $rel['idB'] == $idA;
|
|
|
|
|
2016-11-23 15:57:52 +00:00
|
|
|
// {4} si même type //
|
2016-11-23 15:36:25 +00:00
|
|
|
$sameType = $rel['type'] == $type;
|
|
|
|
|
2016-11-23 15:57:52 +00:00
|
|
|
// {4} Cas particulier si on va mettre à jour une relation alter-alter //
|
|
|
|
if( ($sameAB || $sameBA) && $alteralter && in_array($rel['type'], [0,1]) ){
|
|
|
|
// On remplace la valeur
|
|
|
|
$relset[$r]['type'] = $type;
|
|
|
|
|
|
|
|
// On fait comme si la relation existait déja
|
2016-11-23 15:36:25 +00:00
|
|
|
return true;
|
2016-11-23 15:57:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// {5} si relation existante -> TRUE //
|
|
|
|
if( ($sameAB || $sameBA) && $sameType ){
|
|
|
|
echo "already $idA and $idB with $type<br>";
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-23 15:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Si rien trouvé -> */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-23 15:57:52 +00:00
|
|
|
|
|
|
|
|
2016-12-20 18:26:52 +00:00
|
|
|
/* TRAITE LES DONNÉES D'UN FORMULAIRE DE TYPE LAB-SURVEYS
|
|
|
|
*
|
|
|
|
* @subject<Array> Données du sujet concerné
|
|
|
|
* @contacts<Array> Tableau contenant les données des contacts
|
|
|
|
* @mini<Array> Tableau contenant les données des mini fiches relation
|
|
|
|
* @fiches<Array> Tableau contenant les données des fiches relation
|
|
|
|
*
|
|
|
|
* @return subject_id<int> Retourne l'id sujet de l'enquête
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function survey($params){
|
|
|
|
extract($params);
|
|
|
|
|
|
|
|
$START_TIME = microtime(true);
|
|
|
|
|
|
|
|
/* [1] On récupère l'id unique actuel
|
|
|
|
=========================================================*/
|
|
|
|
$funiq = fopen( __BUILD__.'/lightdb/storage/uniqid', 'r+' );
|
|
|
|
flock($funiq, LOCK_EX); // On verrouille le fichier
|
|
|
|
$uniqid = trim( fgets( $funiq ) );
|
|
|
|
|
|
|
|
|
|
|
|
if( !is_numeric($uniqid) )
|
|
|
|
$uniqid = 0;
|
|
|
|
|
|
|
|
// Décalage à appliquer à tous les ids
|
|
|
|
$offset = intval($uniqid) + 1;
|
|
|
|
|
|
|
|
// on enregistre l'id du sujet
|
|
|
|
$subject_id = intval($subject['subject_id']);
|
|
|
|
|
|
|
|
// Contiendra la valeur de l'id maximum
|
|
|
|
$maxId = $offset;
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On initialise nos variables (lightdb + autres)
|
|
|
|
=========================================================*/
|
|
|
|
|
|
|
|
/* (2) Bases de données */
|
|
|
|
$subjectdb = new lightdb('subject');
|
|
|
|
$contactdb = new lightdb('contact');
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Données du sujet
|
|
|
|
=========================================================*/
|
|
|
|
$subject_set = $subjectdb->fetch($subject_id);
|
|
|
|
|
|
|
|
/* (1) Si le sujet n'existe pas -> ERROR */
|
|
|
|
if( $subject_set === false )
|
|
|
|
return ['ModuleError'=>ManagerError::UnreachableResource];
|
|
|
|
|
|
|
|
/* (2) Initialisation des contacts si vide */
|
|
|
|
if( !isset($subject_set['contacts']) )
|
|
|
|
$subject_set['contacts'] = [];
|
|
|
|
|
|
|
|
/* (3) Initialisation des relations si vide */
|
|
|
|
if( !isset($subject_set['relations']) )
|
|
|
|
$subject_set['relations'] = [];
|
|
|
|
|
|
|
|
/* (4) Ajout de l'enquête */
|
|
|
|
//if( !in_array('phone', $subject_set['subject']['surveys']) )
|
|
|
|
// $subject_set['subject']['surveys'][] = 'phone';
|
|
|
|
|
|
|
|
/* (5) On met à jour si le sujet veut renouveller l'enquête plus tard */
|
|
|
|
if( !empty($subject['coords']) )
|
|
|
|
$subject_set['subject']['coords'] = $subject['coords'];
|
|
|
|
|
|
|
|
/* (6) On récupère les noms des contacts */
|
|
|
|
$contactsById = []; // idContact -> nomContact, typeEnquete ('mini' ou 'fiche')
|
|
|
|
|
|
|
|
foreach($subject_set['contacts'] as $contactId){
|
|
|
|
$contactId = intval($contactId);
|
|
|
|
$contactData = $contactdb->fetch( $contactId );
|
|
|
|
|
|
|
|
$contactsById[$contactId] = [
|
|
|
|
'name' => $contactData['name'],
|
|
|
|
'type' => isset($contactData['studies2']) ? 'fiche' : 'mini'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] On saisit les fiches + les contacts des fiches
|
|
|
|
=========================================================*/
|
|
|
|
$contactsDone = [
|
|
|
|
"existing" => [], // Contacts exportés déja enregistrés
|
|
|
|
"username" => [] // Nouveaux contacts déja enregistrés
|
|
|
|
];
|
|
|
|
$finalid = []; // id_contact -> id_final_contact (existant ou nouveau)
|
|
|
|
|
|
|
|
foreach($fiches as $f=>$ficheData){
|
|
|
|
|
|
|
|
/* (1) On récupère les données du contact associé */
|
|
|
|
if( !isset($contacts[$ficheData['uid']]) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
$contact = $contacts[ $ficheData['uid'] ];
|
|
|
|
|
|
|
|
/* (2) Si le contact a été importé d'une autre enquête */
|
|
|
|
if( isset($contact['existing']) && is_numeric($contact['existing']) ){
|
|
|
|
|
|
|
|
$newId = (int) $contact['existing'];
|
|
|
|
$existingData = null;
|
|
|
|
|
|
|
|
// si le contact existe bien déja -> on recup. ses données
|
|
|
|
if( isset($contactsById[ $newId ]) )
|
|
|
|
$existingData = $contactsById[$newId];
|
|
|
|
|
|
|
|
$uname = $existingData['name'];
|
|
|
|
|
|
|
|
// si aucune référence existante -> on oublie ce contact
|
|
|
|
if( $existingData === null ){
|
|
|
|
$finalid[ intval($contact['uid']) ] = null;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
|
|
|
// S'il a déja une fiche ou qu'on a déja fait une fiche, on quitte
|
|
|
|
if( $existingData['type'] === 'fiche' || isset($contactsDone['existing'][$newId]) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// On l'ajoute à la liste des contacts 'existing' fait
|
|
|
|
$contactsDone['existing'][$newId] = $newId;
|
|
|
|
|
|
|
|
/* (3) Si nouveau contact */
|
|
|
|
}else{
|
|
|
|
|
|
|
|
$newId = (int) ($offset+$contact['uid']);
|
|
|
|
$uname = $contact['username'];
|
|
|
|
|
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
|
|
|
// S'il a déja été saisi, on met l'ancienne référence puis on passe au suivant
|
|
|
|
if( isset($contactsDone['username'][$uname]) ){
|
|
|
|
$finalid[ intval($contact['uid']) ] = $contactsDone['username'][$uname];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// On l'ajoute à la liste des contacts 'username' fait
|
|
|
|
$contactsDone['username'][$uname] = $newId;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (4) Update du @maxId */
|
|
|
|
if( $newId > $maxId )
|
|
|
|
$maxId = (int) $newId;
|
|
|
|
|
|
|
|
/* (5) On enregistre les données du contact */
|
|
|
|
$contactdb->insert($newId, [
|
|
|
|
'id' => $newId,
|
|
|
|
'name' => $uname,
|
|
|
|
'sexe' => $ficheData['sexe'],
|
|
|
|
'age' => $ficheData['age'],
|
|
|
|
'studies2' => $ficheData['studies'],
|
|
|
|
'reltype' => ($ficheData['reltype']==10) ? $ficheData['reltypeSpecial'] : $ficheData['reltype'], // si 'autre' -> valeur, sinon le code
|
|
|
|
'dist' => $ficheData['loc'],
|
|
|
|
'job' => $ficheData['job'],
|
|
|
|
'famsit' => $ficheData['famsit'],
|
|
|
|
'city' => $ficheData['city'],
|
|
|
|
'cp' => $ficheData['cp'],
|
|
|
|
'quartier' => $ficheData['quartier'],
|
|
|
|
'duration' => $ficheData['duration'],
|
|
|
|
'context' => $ficheData['context'],
|
|
|
|
'contextExtra' => $ficheData['contextSpecial'],
|
|
|
|
'freq' => $ficheData['freq'],
|
|
|
|
'connect' => $ficheData['connect'],
|
|
|
|
'connectExtra' => $ficheData['connectSpecial'],
|
|
|
|
'medsoc' => $ficheData['medsoc'],
|
|
|
|
'interest' => $ficheData['interest'],
|
|
|
|
'irlfreq' => $ficheData['irlfreq'],
|
|
|
|
'relmark' => $ficheData['relmark'],
|
|
|
|
'medrel' => $ficheData['medrel']
|
|
|
|
] );
|
|
|
|
|
|
|
|
// On ajoute le contact dans la liste des contacts du sujet (si pas déja)
|
|
|
|
if( !in_array($newId, $subject_set['contacts']) )
|
|
|
|
$subject_set['contacts'][] = $newId;
|
|
|
|
|
|
|
|
// On enregistre la relation avec EGO
|
|
|
|
$reltype = ($f<20) ? 4 : 5; // 4->appels 5->sms
|
|
|
|
if( !self::relAlready($subject_set['relations'], $subject_id, $newId, $reltype) )
|
|
|
|
$subject_set['relations'][] = [
|
|
|
|
'idA' => $subject_id,
|
|
|
|
'idB' => $newId,
|
|
|
|
'type' => $reltype
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [5] On enregistre les mini + les contacts des mini
|
|
|
|
=========================================================*/
|
|
|
|
foreach($mini as $miniData){
|
|
|
|
|
|
|
|
/* (1) On récupère les données du contact associé */
|
|
|
|
if( !isset($contacts[$miniData['uid']]) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$contact = $contacts[ $miniData['uid'] ];
|
|
|
|
|
|
|
|
/* (2) Si le contact a été importé d'une autre enquête */
|
|
|
|
if( isset($contact['existing']) && is_numeric($contact['existing']) ){
|
|
|
|
|
|
|
|
$newId = (int) $contact['existing'];
|
|
|
|
$existingData = null;
|
|
|
|
|
|
|
|
// si le contact existe bien déja -> on recup. ses données
|
|
|
|
if( isset($contactsById[ $newId ]) )
|
|
|
|
$existingData = $contactsById[$newId];
|
|
|
|
|
|
|
|
$uname = $existingData['name'];
|
|
|
|
|
|
|
|
// si aucune référence existante -> on oublie ce contact
|
|
|
|
if( $existingData === null ){
|
|
|
|
$finalid[ intval($contact['uid']) ] = null;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
|
|
|
// S'il a déja une fiche ou qu'on a déja fait une fiche, on quitte
|
|
|
|
if( $existingData['type'] === 'fiche' || isset($contactsDone['existing'][$newId]) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// On l'ajoute à la liste des contacts 'existing' fait
|
|
|
|
$contactsDone['existing'][$newId] = $newId;
|
|
|
|
|
|
|
|
/* (3) Si nouveau contact */
|
|
|
|
}else{
|
|
|
|
|
|
|
|
$newId = (int) ($offset+$contact['uid']);
|
|
|
|
$uname = $contact['username'];
|
|
|
|
|
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
|
|
|
// S'il a déja été saisi, on met l'ancienne référence puis on passe au suivant
|
|
|
|
if( isset($contactsDone['username'][$uname]) ){
|
|
|
|
$finalid[ intval($contact['uid']) ] = $contactsDone['username'][$uname];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// On l'ajoute à la liste des contacts 'username' fait
|
|
|
|
$contactsDone['username'][$uname] = $newId;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (4) Update du @maxId */
|
|
|
|
if( $newId > $maxId )
|
|
|
|
$maxId = (int) $newId;
|
|
|
|
|
|
|
|
|
|
|
|
// On remplit les données qui iront dans le fichier pour ce contact
|
|
|
|
$contactdb->insert($newId, [
|
|
|
|
'id' => $newId,
|
|
|
|
'name' => $uname,
|
|
|
|
'sexe' => $miniData['sexe'],
|
|
|
|
'age' => $miniData['age'],
|
|
|
|
'studies1' => $miniData['studies'],
|
|
|
|
'reltype' => ($miniData['reltype']==10) ? $miniData['reltypeSpecial'] : $miniData['reltype'], // si 'autre' -> valeur, sinon le code
|
|
|
|
'dist' => $miniData['loc']
|
|
|
|
] );
|
|
|
|
|
|
|
|
// On ajoute le contact dans la liste des contacts du sujet (si pas déja)
|
|
|
|
if( !in_array($newId, $subject_set['contacts']) )
|
|
|
|
$subject_set['contacts'][] = $newId;
|
|
|
|
|
|
|
|
// On enregistre la relation avec EGO
|
|
|
|
if( !self::relAlready($subject_set['relations'], $subject_id, $newId, 2) )
|
|
|
|
$subject_set['relations'][] = [
|
|
|
|
'idA' => $subject_id,
|
|
|
|
'idB' => $newId,
|
|
|
|
'type' => 2 // relation cellulaire mineure
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [6] On enregistre les relations de la MATRICE
|
|
|
|
=========================================================*/
|
|
|
|
$clen = count($finalid);
|
|
|
|
|
|
|
|
/* (1) On récupére les ids (initiaux) des contacts de manière unique */
|
|
|
|
$doneNewId = [];
|
|
|
|
$cIdList = [];
|
|
|
|
foreach($finalid as $lastid=>$newid){
|
|
|
|
|
|
|
|
// si id (ou reference) déja fait -> suivant
|
|
|
|
if( array_key_exists($newid, $doneNewId) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$cIdList[$lastid] = null;
|
|
|
|
$doneNewId[$newid] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ksort($cIdList);
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) On remplit les relations */
|
|
|
|
foreach($cIdList as $x=>$yNull){
|
|
|
|
foreach($cIdList as $y=>$xNull)
|
|
|
|
if( $x < $y ){ // On affiche que sous la diagonale
|
|
|
|
|
|
|
|
// Si relation alter-alter
|
|
|
|
$relation_type = isset($matrice[$y]) && in_array($x, $matrice[$y])
|
|
|
|
|| ( isset($matrice[$x]) && in_array($y, $matrice[$x]) );
|
|
|
|
|
|
|
|
$relation_type = $relation_type ? 1 : 0; // 0->aucune relation 1->relation alter alter
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Si la relation existe déja, on ne fait rien */
|
|
|
|
if( self::relAlready($subject_set['relations'], $finalid[$x], $finalid[$y], $relation_type) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$subject_set['relations'][] = [
|
|
|
|
'idA' => $finalid[$x],
|
|
|
|
'idB' => $finalid[$y],
|
|
|
|
'type' => $relation_type
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [6] On les données du sujet
|
|
|
|
=========================================================*/
|
|
|
|
$subjectdb->delete($subject_id);
|
|
|
|
$subjectdb->insert($subject_id, $subject_set);
|
|
|
|
|
|
|
|
|
|
|
|
/* [7] Ferme les bases de données
|
|
|
|
=========================================================*/
|
|
|
|
$subjectdb->close();
|
|
|
|
$contactdb->close();
|
|
|
|
|
|
|
|
|
|
|
|
/* [7] On met à jour le nouvel ID unique
|
|
|
|
=========================================================*/
|
|
|
|
rewind($funiq); // On revient au début du fichier
|
|
|
|
fwrite($funiq, $maxId); // On écrit la nouvelle valeur (forcément plus grande)
|
|
|
|
flock($funiq, LOCK_UN); // On débloque le verrou
|
|
|
|
fclose($funiq);
|
|
|
|
|
|
|
|
|
|
|
|
/* [9] Gestion du retour
|
|
|
|
=========================================================*/
|
|
|
|
return [
|
|
|
|
'ModuleError' => ManagerError::Success,
|
|
|
|
'subject_id' => $subject_id,
|
|
|
|
'nb_contacts' => count($subject_set['contacts']),
|
|
|
|
'nb_relations' => count($subject_set['relations']),
|
|
|
|
'elapsed_time' => microtime(true) - $START_TIME
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-30 10:19:51 +00:00
|
|
|
/* TRAITE LES DONNÉES D'UN FORMULAIRE DE TYPE TÉLÉPHONIQUE
|
|
|
|
*
|
2016-12-13 18:37:44 +00:00
|
|
|
* @subject<Array> Données du sujet concerné
|
2016-04-30 14:13:37 +00:00
|
|
|
* @contacts<Array> Tableau contenant les données des contacts
|
2016-12-17 23:12:23 +00:00
|
|
|
* @mini<Array> Tableau contenant les données des mini fiches relation
|
2016-04-30 14:13:37 +00:00
|
|
|
* @fiches<Array> Tableau contenant les données des fiches relation
|
2016-04-30 10:19:51 +00:00
|
|
|
*
|
|
|
|
* @return subject_id<int> Retourne l'id sujet de l'enquête
|
|
|
|
*
|
|
|
|
*/
|
2016-11-22 13:30:34 +00:00
|
|
|
public static function phone($params){
|
2016-04-30 10:19:51 +00:00
|
|
|
extract($params);
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
$START_TIME = microtime(true);
|
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* [1] On récupère l'id unique actuel
|
2016-04-30 10:19:51 +00:00
|
|
|
=========================================================*/
|
2016-11-06 13:22:15 +00:00
|
|
|
$funiq = fopen( __BUILD__.'/lightdb/storage/uniqid', 'r+' );
|
2016-05-17 15:05:32 +00:00
|
|
|
flock($funiq, LOCK_EX); // On verrouille le fichier
|
2016-11-23 12:38:15 +00:00
|
|
|
$uniqid = trim( fgets( $funiq ) );
|
|
|
|
|
2016-05-17 14:51:24 +00:00
|
|
|
|
|
|
|
if( !is_numeric($uniqid) )
|
|
|
|
$uniqid = 0;
|
2016-05-09 09:01:43 +00:00
|
|
|
|
2016-05-17 14:51:24 +00:00
|
|
|
// Décalage à appliquer à tous les ids
|
2016-05-26 17:43:50 +00:00
|
|
|
$offset = intval($uniqid) + 1;
|
2016-05-09 09:01:43 +00:00
|
|
|
|
2016-05-18 08:08:34 +00:00
|
|
|
// on enregistre l'id du sujet
|
2016-12-13 18:37:44 +00:00
|
|
|
$subject_id = intval($subject['subject_id']);
|
2016-05-18 08:08:34 +00:00
|
|
|
|
2016-05-17 14:51:24 +00:00
|
|
|
// Contiendra la valeur de l'id maximum
|
2016-11-23 12:38:15 +00:00
|
|
|
$maxId = $offset;
|
2016-05-17 14:51:24 +00:00
|
|
|
|
2016-05-09 09:01:43 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* [2] On initialise nos variables (lightdb + autres)
|
2016-11-22 10:06:03 +00:00
|
|
|
=========================================================*/
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* (2) Bases de données */
|
|
|
|
$subjectdb = new lightdb('subject');
|
|
|
|
$contactdb = new lightdb('contact');
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* [3] Données du sujet
|
2016-05-13 16:26:30 +00:00
|
|
|
=========================================================*/
|
2016-11-22 13:30:34 +00:00
|
|
|
$subject_set = $subjectdb->fetch($subject_id);
|
2016-05-09 12:12:02 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* (1) Si le sujet n'existe pas -> ERROR */
|
|
|
|
if( $subject_set === false )
|
|
|
|
return ['ModuleError'=>ManagerError::UnreachableResource];
|
2016-05-09 12:12:02 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* (2) Initialisation des contacts si vide */
|
|
|
|
if( !isset($subject_set['contacts']) )
|
|
|
|
$subject_set['contacts'] = [];
|
2016-05-09 12:12:02 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* (3) Initialisation des relations si vide */
|
|
|
|
if( !isset($subject_set['relations']) )
|
|
|
|
$subject_set['relations'] = [];
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (4) Ajout de l'enquête */
|
|
|
|
if( !in_array('phone', $subject_set['subject']['surveys']) )
|
|
|
|
$subject_set['subject']['surveys'][] = 'phone';
|
|
|
|
|
2016-12-13 18:37:44 +00:00
|
|
|
/* (5) On met à jour si le sujet veut renouveller l'enquête plus tard */
|
2016-12-15 12:18:37 +00:00
|
|
|
if( !empty($subject['coords']) )
|
|
|
|
$subject_set['subject']['coords'] = $subject['coords'];
|
2016-12-13 18:37:44 +00:00
|
|
|
|
|
|
|
/* (6) On récupère les noms des contacts */
|
2016-11-23 12:38:15 +00:00
|
|
|
$contactsById = []; // idContact -> nomContact, typeEnquete ('mini' ou 'fiche')
|
2016-05-09 09:01:43 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
foreach($subject_set['contacts'] as $contactId){
|
|
|
|
$contactId = intval($contactId);
|
|
|
|
$contactData = $contactdb->fetch( $contactId );
|
2016-05-09 09:01:43 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
$contactsById[$contactId] = [
|
|
|
|
'name' => $contactData['name'],
|
|
|
|
'type' => isset($contactData['studies2']) ? 'fiche' : 'mini'
|
|
|
|
];
|
2016-11-22 13:30:34 +00:00
|
|
|
}
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* [4] On saisit les fiches + les contacts des fiches
|
2016-05-09 17:02:57 +00:00
|
|
|
=========================================================*/
|
2016-11-06 13:22:15 +00:00
|
|
|
$contactsDone = [
|
|
|
|
"existing" => [], // Contacts exportés déja enregistrés
|
|
|
|
"username" => [] // Nouveaux contacts déja enregistrés
|
|
|
|
];
|
2016-11-22 13:30:34 +00:00
|
|
|
$finalid = []; // id_contact -> id_final_contact (existant ou nouveau)
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-05-30 14:54:41 +00:00
|
|
|
foreach($fiches as $f=>$ficheData){
|
2016-11-22 16:17:04 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (1) On récupère les données du contact associé */
|
2016-11-23 12:38:15 +00:00
|
|
|
if( !isset($contacts[$ficheData['uid']]) )
|
2016-11-23 11:12:53 +00:00
|
|
|
continue;
|
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
$contact = $contacts[ $ficheData['uid'] ];
|
2016-05-09 17:02:57 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (2) Si le contact a été importé d'une autre enquête */
|
|
|
|
if( isset($contact['existing']) && is_numeric($contact['existing']) ){
|
2016-11-22 13:30:34 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
$newId = (int) $contact['existing'];
|
2016-11-23 12:38:15 +00:00
|
|
|
$existingData = null;
|
2016-11-22 13:30:34 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
// si le contact existe bien déja -> on recup. ses données
|
|
|
|
if( isset($contactsById[ $newId ]) )
|
|
|
|
$existingData = $contactsById[$newId];
|
|
|
|
|
|
|
|
$uname = $existingData['name'];
|
|
|
|
|
|
|
|
// si aucune référence existante -> on oublie ce contact
|
|
|
|
if( $existingData === null ){
|
|
|
|
$finalid[ intval($contact['uid']) ] = null;
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-22 13:30:34 +00:00
|
|
|
|
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
|
|
|
// S'il a déja une fiche ou qu'on a déja fait une fiche, on quitte
|
2016-11-23 12:38:15 +00:00
|
|
|
if( $existingData['type'] === 'fiche' || isset($contactsDone['existing'][$newId]) )
|
2016-11-23 11:12:53 +00:00
|
|
|
continue;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
// On l'ajoute à la liste des contacts 'existing' fait
|
|
|
|
$contactsDone['existing'][$newId] = $newId;
|
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (3) Si nouveau contact */
|
|
|
|
}else{
|
2016-11-22 13:30:34 +00:00
|
|
|
|
|
|
|
$newId = (int) ($offset+$contact['uid']);
|
2016-11-06 13:22:15 +00:00
|
|
|
$uname = $contact['username'];
|
2016-11-22 13:30:34 +00:00
|
|
|
|
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
// S'il a déja été saisi, on met l'ancienne référence puis on passe au suivant
|
|
|
|
if( isset($contactsDone['username'][$uname]) ){
|
|
|
|
$finalid[ intval($contact['uid']) ] = $contactsDone['username'][$uname];
|
2016-11-23 11:12:53 +00:00
|
|
|
continue;
|
2016-11-23 12:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// On l'ajoute à la liste des contacts 'username' fait
|
|
|
|
$contactsDone['username'][$uname] = $newId;
|
2016-11-06 13:22:15 +00:00
|
|
|
}
|
2016-05-17 14:51:24 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (4) Update du @maxId */
|
|
|
|
if( $newId > $maxId )
|
|
|
|
$maxId = (int) $newId;
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* (5) On enregistre les données du contact */
|
|
|
|
$contactdb->insert($newId, [
|
2016-05-30 14:54:41 +00:00
|
|
|
'id' => $newId,
|
2016-11-06 13:22:15 +00:00
|
|
|
'name' => $uname,
|
2016-05-13 16:26:30 +00:00
|
|
|
'sexe' => $ficheData['sexe'],
|
|
|
|
'age' => $ficheData['age'],
|
|
|
|
'studies2' => $ficheData['studies'],
|
|
|
|
'reltype' => ($ficheData['reltype']==10) ? $ficheData['reltypeSpecial'] : $ficheData['reltype'], // si 'autre' -> valeur, sinon le code
|
|
|
|
'dist' => $ficheData['loc'],
|
|
|
|
'job' => $ficheData['job'],
|
|
|
|
'famsit' => $ficheData['famsit'],
|
|
|
|
'city' => $ficheData['city'],
|
|
|
|
'cp' => $ficheData['cp'],
|
2016-05-29 15:51:06 +00:00
|
|
|
'quartier' => $ficheData['quartier'],
|
2016-05-13 16:26:30 +00:00
|
|
|
'duration' => $ficheData['duration'],
|
|
|
|
'context' => $ficheData['context'],
|
|
|
|
'contextExtra' => $ficheData['contextSpecial'],
|
|
|
|
'freq' => $ficheData['freq'],
|
|
|
|
'connect' => $ficheData['connect'],
|
2016-11-28 10:50:26 +00:00
|
|
|
'connectExtra' => $ficheData['connectSpecial'],
|
|
|
|
'medsoc' => $ficheData['medsoc'],
|
|
|
|
'interest' => $ficheData['interest'],
|
|
|
|
'irlfreq' => $ficheData['irlfreq'],
|
|
|
|
'relmark' => $ficheData['relmark'],
|
|
|
|
'medrel' => $ficheData['medrel']
|
2016-11-22 13:30:34 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
// On ajoute le contact dans la liste des contacts du sujet (si pas déja)
|
|
|
|
if( !in_array($newId, $subject_set['contacts']) )
|
|
|
|
$subject_set['contacts'][] = $newId;
|
2016-05-09 17:02:57 +00:00
|
|
|
|
2016-05-30 14:54:41 +00:00
|
|
|
// On enregistre la relation avec EGO
|
2016-11-23 15:36:25 +00:00
|
|
|
$reltype = ($f<20) ? 4 : 5; // 4->appels 5->sms
|
|
|
|
if( !self::relAlready($subject_set['relations'], $subject_id, $newId, $reltype) )
|
|
|
|
$subject_set['relations'][] = [
|
|
|
|
'idA' => $subject_id,
|
|
|
|
'idB' => $newId,
|
|
|
|
'type' => $reltype
|
|
|
|
];
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-05-13 16:26:30 +00:00
|
|
|
}
|
2016-05-09 17:02:57 +00:00
|
|
|
|
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* [5] On enregistre les mini + les contacts des mini
|
2016-11-06 13:22:15 +00:00
|
|
|
=========================================================*/
|
|
|
|
foreach($mini as $miniData){
|
2016-11-22 16:17:04 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (1) On récupère les données du contact associé */
|
2016-11-23 12:38:15 +00:00
|
|
|
if( !isset($contacts[$miniData['uid']]) )
|
2016-11-23 11:12:53 +00:00
|
|
|
continue;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
$contact = $contacts[ $miniData['uid'] ];
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
/* (2) Si le contact a été importé d'une autre enquête */
|
|
|
|
if( isset($contact['existing']) && is_numeric($contact['existing']) ){
|
2016-11-22 13:30:34 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
$newId = (int) $contact['existing'];
|
2016-11-23 12:38:15 +00:00
|
|
|
$existingData = null;
|
|
|
|
|
|
|
|
// si le contact existe bien déja -> on recup. ses données
|
|
|
|
if( isset($contactsById[ $newId ]) )
|
|
|
|
$existingData = $contactsById[$newId];
|
2016-11-22 13:30:34 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
$uname = $existingData['name'];
|
|
|
|
|
|
|
|
// si aucune référence existante -> on oublie ce contact
|
|
|
|
if( $existingData === null ){
|
|
|
|
$finalid[ intval($contact['uid']) ] = null;
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-22 13:30:34 +00:00
|
|
|
|
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
// S'il a déja une fiche ou qu'on a déja fait une fiche, on quitte
|
|
|
|
if( $existingData['type'] === 'fiche' || isset($contactsDone['existing'][$newId]) )
|
2016-11-23 11:12:53 +00:00
|
|
|
continue;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
// On l'ajoute à la liste des contacts 'existing' fait
|
|
|
|
$contactsDone['existing'][$newId] = $newId;
|
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (3) Si nouveau contact */
|
|
|
|
}else{
|
2016-11-22 13:30:34 +00:00
|
|
|
|
2016-11-22 16:17:04 +00:00
|
|
|
$newId = (int) ($offset+$contact['uid']);
|
2016-11-06 13:22:15 +00:00
|
|
|
$uname = $contact['username'];
|
2016-11-22 13:30:34 +00:00
|
|
|
|
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
// S'il a déja été saisi, on met l'ancienne référence puis on passe au suivant
|
|
|
|
if( isset($contactsDone['username'][$uname]) ){
|
|
|
|
$finalid[ intval($contact['uid']) ] = $contactsDone['username'][$uname];
|
2016-11-23 11:12:53 +00:00
|
|
|
continue;
|
2016-11-23 12:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// On l'ajoute à la liste des contacts 'username' fait
|
|
|
|
$contactsDone['username'][$uname] = $newId;
|
2016-11-06 13:22:15 +00:00
|
|
|
}
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (4) Update du @maxId */
|
|
|
|
if( $newId > $maxId )
|
|
|
|
$maxId = (int) $newId;
|
|
|
|
|
|
|
|
|
|
|
|
// On remplit les données qui iront dans le fichier pour ce contact
|
2016-11-22 13:30:34 +00:00
|
|
|
$contactdb->insert($newId, [
|
2016-11-06 13:22:15 +00:00
|
|
|
'id' => $newId,
|
|
|
|
'name' => $uname,
|
|
|
|
'sexe' => $miniData['sexe'],
|
|
|
|
'age' => $miniData['age'],
|
|
|
|
'studies1' => $miniData['studies'],
|
|
|
|
'reltype' => ($miniData['reltype']==10) ? $miniData['reltypeSpecial'] : $miniData['reltype'], // si 'autre' -> valeur, sinon le code
|
|
|
|
'dist' => $miniData['loc']
|
2016-11-22 13:30:34 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
// On ajoute le contact dans la liste des contacts du sujet (si pas déja)
|
|
|
|
if( !in_array($newId, $subject_set['contacts']) )
|
|
|
|
$subject_set['contacts'][] = $newId;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
// On enregistre la relation avec EGO
|
2016-11-23 15:36:25 +00:00
|
|
|
if( !self::relAlready($subject_set['relations'], $subject_id, $newId, 2) )
|
|
|
|
$subject_set['relations'][] = [
|
|
|
|
'idA' => $subject_id,
|
|
|
|
'idB' => $newId,
|
|
|
|
'type' => 2 // relation cellulaire mineure
|
|
|
|
];
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* [6] On enregistre les relations de la MATRICE
|
2016-05-09 17:02:57 +00:00
|
|
|
=========================================================*/
|
2016-11-22 13:30:34 +00:00
|
|
|
$clen = count($finalid);
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
/* (1) On récupére les ids (initiaux) des contacts de manière unique */
|
|
|
|
$doneNewId = [];
|
2016-09-12 16:04:53 +00:00
|
|
|
$cIdList = [];
|
2016-11-22 13:30:34 +00:00
|
|
|
foreach($finalid as $lastid=>$newid){
|
2016-11-06 15:50:29 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
// si id (ou reference) déja fait -> suivant
|
|
|
|
if( array_key_exists($newid, $doneNewId) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$cIdList[$lastid] = null;
|
|
|
|
$doneNewId[$newid] = false;
|
2016-05-30 14:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ksort($cIdList);
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
|
2016-05-30 14:54:41 +00:00
|
|
|
/* (2) On remplit les relations */
|
2016-11-23 12:38:15 +00:00
|
|
|
foreach($cIdList as $x=>$yNull){
|
|
|
|
foreach($cIdList as $y=>$xNull)
|
2016-05-30 14:54:41 +00:00
|
|
|
if( $x < $y ){ // On affiche que sous la diagonale
|
2016-05-12 19:47:52 +00:00
|
|
|
|
2016-05-30 14:54:41 +00:00
|
|
|
// Si relation alter-alter
|
2016-11-23 12:38:15 +00:00
|
|
|
$relation_type = isset($matrice[$y]) && in_array($x, $matrice[$y])
|
|
|
|
|| ( isset($matrice[$x]) && in_array($y, $matrice[$x]) );
|
|
|
|
|
|
|
|
$relation_type = $relation_type ? 1 : 0; // 0->aucune relation 1->relation alter alter
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Si la relation existe déja, on ne fait rien */
|
2016-11-23 15:36:25 +00:00
|
|
|
if( self::relAlready($subject_set['relations'], $finalid[$x], $finalid[$y], $relation_type) )
|
|
|
|
continue;
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
$subject_set['relations'][] = [
|
2016-11-22 13:34:05 +00:00
|
|
|
'idA' => $finalid[$x],
|
|
|
|
'idB' => $finalid[$y],
|
2016-11-23 12:38:15 +00:00
|
|
|
'type' => $relation_type
|
2016-11-22 13:30:34 +00:00
|
|
|
];
|
2016-05-30 14:54:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2016-05-12 19:47:52 +00:00
|
|
|
|
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* [6] On les données du sujet
|
2016-05-17 14:51:24 +00:00
|
|
|
=========================================================*/
|
2016-11-22 14:05:28 +00:00
|
|
|
$subjectdb->delete($subject_id);
|
2016-11-22 13:30:34 +00:00
|
|
|
$subjectdb->insert($subject_id, $subject_set);
|
|
|
|
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [7] Ferme les bases de données
|
2016-11-22 13:30:34 +00:00
|
|
|
=========================================================*/
|
2016-11-22 14:05:28 +00:00
|
|
|
$subjectdb->close();
|
|
|
|
$contactdb->close();
|
2016-11-22 13:30:34 +00:00
|
|
|
|
2016-05-18 14:04:17 +00:00
|
|
|
|
|
|
|
/* [7] On met à jour le nouvel ID unique
|
|
|
|
=========================================================*/
|
|
|
|
rewind($funiq); // On revient au début du fichier
|
2016-11-06 13:22:15 +00:00
|
|
|
fwrite($funiq, $maxId); // On écrit la nouvelle valeur (forcément plus grande)
|
2016-05-18 14:04:17 +00:00
|
|
|
flock($funiq, LOCK_UN); // On débloque le verrou
|
|
|
|
fclose($funiq);
|
|
|
|
|
|
|
|
|
|
|
|
/* [9] Gestion du retour
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-05-18 14:04:17 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
2016-11-23 12:38:15 +00:00
|
|
|
'subject_id' => $subject_id,
|
|
|
|
'nb_contacts' => count($subject_set['contacts']),
|
|
|
|
'nb_relations' => count($subject_set['relations']),
|
|
|
|
'elapsed_time' => microtime(true) - $START_TIME
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-05-18 14:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* TRAITE LES DONNÉES D'UN FORMULAIRE DE TYPE FACEBOOK
|
|
|
|
*
|
2016-12-13 18:37:44 +00:00
|
|
|
* @subject<Array> Données du sujet concerné
|
2016-05-18 14:04:17 +00:00
|
|
|
* @contacts<Array> Tableau contenant les données des contacts
|
2016-12-13 18:37:44 +00:00
|
|
|
* @mini<Array> Tableau contenant les données des mini fiches relation
|
2016-05-18 14:04:17 +00:00
|
|
|
* @fiches<Array> Tableau contenant les données des fiches relation
|
|
|
|
*
|
|
|
|
* @return subject_id<int> Retourne l'id sujet de l'enquête
|
|
|
|
*
|
|
|
|
*/
|
2016-11-22 14:05:28 +00:00
|
|
|
public static function facebook($params){
|
2016-05-18 14:04:17 +00:00
|
|
|
extract($params);
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
$START_TIME = microtime(true);
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [1] On récupère l'id unique actuel
|
2016-05-18 14:04:17 +00:00
|
|
|
=========================================================*/
|
2016-11-06 13:22:15 +00:00
|
|
|
$funiq = fopen( __BUILD__.'/lightdb/storage/uniqid', 'r+' );
|
2016-05-18 14:04:17 +00:00
|
|
|
flock($funiq, LOCK_EX); // On verrouille le fichier
|
2016-11-23 12:38:15 +00:00
|
|
|
$uniqid = trim( fgets( $funiq ) );
|
|
|
|
|
2016-05-18 14:04:17 +00:00
|
|
|
|
|
|
|
if( !is_numeric($uniqid) )
|
|
|
|
$uniqid = 0;
|
|
|
|
|
|
|
|
// Décalage à appliquer à tous les ids
|
2016-05-26 17:43:50 +00:00
|
|
|
$offset = intval($uniqid) + 1;
|
2016-05-18 14:04:17 +00:00
|
|
|
|
|
|
|
// on enregistre l'id du sujet
|
2016-12-13 18:37:44 +00:00
|
|
|
$subject_id = intval($subject['subject_id']);
|
2016-05-18 14:04:17 +00:00
|
|
|
|
|
|
|
// Contiendra la valeur de l'id maximum
|
2016-11-23 12:38:15 +00:00
|
|
|
$maxId = $offset;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [2] On initialise nos variables (lightdb + autres)
|
|
|
|
=========================================================*/
|
2016-11-22 10:06:03 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (2) Bases de données */
|
|
|
|
$subjectdb = new lightdb('subject');
|
|
|
|
$contactdb = new lightdb('contact');
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [3] Données du sujet
|
2016-05-18 14:04:17 +00:00
|
|
|
=========================================================*/
|
2016-11-22 14:05:28 +00:00
|
|
|
$subject_set = $subjectdb->fetch($subject_id);
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (1) Si le sujet n'existe pas -> ERROR */
|
|
|
|
if( $subject_set === false )
|
|
|
|
return ['ModuleError'=>ManagerError::UnreachableResource];
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (2) Initialisation des contacts si vide */
|
|
|
|
if( !isset($subject_set['contacts']) )
|
|
|
|
$subject_set['contacts'] = [];
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (3) Initialisation des relations si vide */
|
|
|
|
if( !isset($subject_set['relations']) )
|
|
|
|
$subject_set['relations'] = [];
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (4) Ajout de l'enquête */
|
2016-11-23 12:38:15 +00:00
|
|
|
if( !in_array('facebook', $subject_set['subject']['surveys']) )
|
|
|
|
$subject_set['subject']['surveys'][] = 'facebook';
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-12-13 18:37:44 +00:00
|
|
|
/* (5) On met à jour si le sujet veut renouveller l'enquête plus tard */
|
2016-12-15 12:18:37 +00:00
|
|
|
if( !empty($subject['coords']) )
|
|
|
|
$subject_set['subject']['coords'] = $subject['coords'];
|
2016-12-13 18:37:44 +00:00
|
|
|
|
|
|
|
/* (6) On récupère les noms des contacts */
|
2016-11-23 12:38:15 +00:00
|
|
|
$contactsById = []; // idContact -> nomContact, typeEnquete ('mini' ou 'fiche')
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
foreach($subject_set['contacts'] as $contactId){
|
|
|
|
$contactId = intval($contactId);
|
|
|
|
$contactData = $contactdb->fetch( $contactId );
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
$contactsById[$contactId] = [
|
|
|
|
'name' => $contactData['name'],
|
|
|
|
'type' => isset($contactData['studies2']) ? 'fiche' : 'mini'
|
|
|
|
];
|
2016-11-22 14:05:28 +00:00
|
|
|
}
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [4] On saisit les fiches + les contacts des fiches
|
2016-05-18 14:04:17 +00:00
|
|
|
=========================================================*/
|
2016-11-06 13:22:15 +00:00
|
|
|
$contactsDone = [
|
|
|
|
"existing" => [], // Contacts exportés déja enregistrés
|
|
|
|
"username" => [] // Nouveaux contacts déja enregistrés
|
|
|
|
];
|
2016-11-22 14:05:28 +00:00
|
|
|
$finalid = []; // id_contact -> id_final_contact (existant ou nouveau)
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-05-30 14:54:41 +00:00
|
|
|
foreach($fiches as $f=>$ficheData){
|
2016-11-23 12:38:15 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (1) On récupère les données du contact associé */
|
2016-11-23 12:38:15 +00:00
|
|
|
if( !isset($contacts[$ficheData['uid']]) )
|
|
|
|
continue;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
|
|
|
|
$contact = $contacts[ $ficheData['uid'] ];
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (2) Si le contact a été importé d'une autre enquête */
|
|
|
|
if( isset($contact['existing']) && is_numeric($contact['existing']) ){
|
2016-11-22 14:05:28 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
$newId = (int) $contact['existing'];
|
2016-11-23 12:38:15 +00:00
|
|
|
$existingData = null;
|
|
|
|
|
|
|
|
// si le contact existe bien déja -> on recup. ses données
|
|
|
|
if( isset($contactsById[ $newId ]) )
|
|
|
|
$existingData = $contactsById[$newId];
|
2016-11-22 14:05:28 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
$uname = $existingData['name'];
|
|
|
|
|
|
|
|
// si aucune référence existante -> on oublie ce contact
|
|
|
|
if( $existingData === null ){
|
|
|
|
$finalid[ intval($contact['uid']) ] = null;
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-22 14:05:28 +00:00
|
|
|
|
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
|
|
|
// S'il a déja une fiche ou qu'on a déja fait une fiche, on quitte
|
2016-11-23 12:38:15 +00:00
|
|
|
if( $existingData['type'] === 'fiche' || isset($contactsDone['existing'][$newId]) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// On l'ajoute à la liste des contacts 'existing' fait
|
|
|
|
$contactsDone['existing'][$newId] = $newId;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
/* (3) Si nouveau contact */
|
|
|
|
}else{
|
2016-11-22 14:05:28 +00:00
|
|
|
|
|
|
|
$newId = (int) ($offset+$contact['uid']);
|
2016-11-06 13:22:15 +00:00
|
|
|
$uname = $contact['username'];
|
2016-11-22 14:05:28 +00:00
|
|
|
|
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
// S'il a déja été saisi, on met l'ancienne référence puis on passe au suivant
|
|
|
|
if( isset($contactsDone['username'][$uname]) ){
|
|
|
|
$finalid[ intval($contact['uid']) ] = $contactsDone['username'][$uname];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// On l'ajoute à la liste des contacts 'username' fait
|
|
|
|
$contactsDone['username'][$uname] = $newId;
|
2016-11-06 13:22:15 +00:00
|
|
|
}
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (4) Update du @maxId */
|
|
|
|
if( $newId > $maxId )
|
|
|
|
$maxId = (int) $newId;
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (5) On enregistre les données du contact */
|
|
|
|
$contactdb->insert($newId, [
|
2016-05-30 14:54:41 +00:00
|
|
|
'id' => $newId,
|
2016-11-06 13:22:15 +00:00
|
|
|
'name' => $uname,
|
2016-05-18 14:04:17 +00:00
|
|
|
'sexe' => $ficheData['sexe'],
|
|
|
|
'age' => $ficheData['age'],
|
|
|
|
'studies2' => $ficheData['studies'],
|
|
|
|
'reltype' => ($ficheData['reltype']==10) ? $ficheData['reltypeSpecial'] : $ficheData['reltype'], // si 'autre' -> valeur, sinon le code
|
|
|
|
'dist' => $ficheData['loc'],
|
|
|
|
'job' => $ficheData['job'],
|
|
|
|
'famsit' => $ficheData['famsit'],
|
|
|
|
'city' => $ficheData['city'],
|
|
|
|
'cp' => $ficheData['cp'],
|
2016-05-30 14:54:41 +00:00
|
|
|
'quartier' => $ficheData['quartier'],
|
2016-05-18 14:04:17 +00:00
|
|
|
'duration' => $ficheData['duration'],
|
|
|
|
'context' => $ficheData['context'],
|
|
|
|
'contextExtra' => $ficheData['contextSpecial'],
|
|
|
|
'freq' => $ficheData['freq'],
|
|
|
|
'connect' => $ficheData['connect'],
|
|
|
|
'connectExtra' => $ficheData['connectSpecial']
|
2016-11-22 14:05:28 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
// On ajoute le contact dans la liste des contacts du sujet (si pas déja)
|
|
|
|
if( !in_array($newId, $subject_set['contacts']) )
|
|
|
|
$subject_set['contacts'][] = $newId;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
// On enregistre la relation avec EGO
|
2016-11-23 15:36:25 +00:00
|
|
|
$reltype = ($f<20) ? 6 : 7; // 4->historique 5->messenger
|
|
|
|
if( !self::relAlready($subject_set['relations'], $subject_id, $newId, $reltype) )
|
|
|
|
$subject_set['relations'][] = [
|
|
|
|
'idA' => $subject_id,
|
|
|
|
'idB' => $newId,
|
|
|
|
'type' => $reltype
|
|
|
|
];
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [5] On enregistre les mini + les contacts des mini
|
2016-11-06 13:22:15 +00:00
|
|
|
=========================================================*/
|
|
|
|
foreach($mini as $miniData){
|
2016-11-23 12:38:15 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (1) On récupère les données du contact associé */
|
2016-11-23 12:38:15 +00:00
|
|
|
if( !isset($contacts[$miniData['uid']]) )
|
|
|
|
continue;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
$contact = $contacts[ $miniData['uid'] ];
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
/* (2) Si le contact a été importé d'une autre enquête */
|
|
|
|
if( isset($contact['existing']) && is_numeric($contact['existing']) ){
|
2016-11-22 14:05:28 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
$newId = (int) $contact['existing'];
|
2016-11-23 12:38:15 +00:00
|
|
|
$existingData = null;
|
|
|
|
|
|
|
|
// si le contact existe bien déja -> on recup. ses données
|
|
|
|
if( isset($contactsById[ $newId ]) )
|
|
|
|
$existingData = $contactsById[$newId];
|
2016-11-22 14:05:28 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
$uname = $existingData['name'];
|
|
|
|
|
|
|
|
// si aucune référence existante -> on oublie ce contact
|
|
|
|
if( $existingData === null ){
|
|
|
|
$finalid[ intval($contact['uid']) ] = null;
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
// S'il a déja une fiche ou qu'on a déja fait une fiche, on quitte
|
|
|
|
if( $existingData['type'] === 'fiche' || isset($contactsDone['existing'][$newId]) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// On l'ajoute à la liste des contacts 'existing' fait
|
|
|
|
$contactsDone['existing'][$newId] = $newId;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
/* (3) Si nouveau contact */
|
|
|
|
}else{
|
2016-11-22 14:05:28 +00:00
|
|
|
|
2016-11-22 16:17:04 +00:00
|
|
|
$newId = (int) ($offset+$contact['uid']);
|
2016-11-06 13:22:15 +00:00
|
|
|
$uname = $contact['username'];
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
// S'il a déja été saisi, on met l'ancienne référence puis on passe au suivant
|
|
|
|
if( isset($contactsDone['username'][$uname]) ){
|
|
|
|
$finalid[ intval($contact['uid']) ] = $contactsDone['username'][$uname];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// On l'ajoute à la liste des contacts 'username' fait
|
|
|
|
$contactsDone['username'][$uname] = $newId;
|
2016-11-06 13:22:15 +00:00
|
|
|
}
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (4) Update du @maxId */
|
|
|
|
if( $newId > $maxId )
|
|
|
|
$maxId = (int) $newId;
|
|
|
|
|
|
|
|
|
|
|
|
// On remplit les données qui iront dans le fichier pour ce contact
|
2016-11-22 14:05:28 +00:00
|
|
|
$contactdb->insert($newId, [
|
2016-11-06 13:22:15 +00:00
|
|
|
'id' => $newId,
|
|
|
|
'name' => $uname,
|
|
|
|
'sexe' => $miniData['sexe'],
|
|
|
|
'age' => $miniData['age'],
|
|
|
|
'studies1' => $miniData['studies'],
|
|
|
|
'reltype' => ($miniData['reltype']==10) ? $miniData['reltypeSpecial'] : $miniData['reltype'], // si 'autre' -> valeur, sinon le code
|
|
|
|
'dist' => $miniData['loc']
|
2016-11-22 14:05:28 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
// On ajoute le contact dans la liste des contacts du sujet (si pas déja)
|
|
|
|
if( !in_array($newId, $subject_set['contacts']) )
|
|
|
|
$subject_set['contacts'][] = $newId;
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-05-30 14:54:41 +00:00
|
|
|
// On enregistre la relation avec EGO
|
2016-11-23 15:36:25 +00:00
|
|
|
if( !self::relAlready($subject_set['relations'], $subject_id, $newId, 3) )
|
|
|
|
$subject_set['relations'][] = [
|
|
|
|
'idA' => $subject_id,
|
|
|
|
'idB' => $newId,
|
|
|
|
'type' => 3 // relation facebook mineure
|
|
|
|
];
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
|
2016-05-18 14:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [6] On enregistre les relations de la MATRICE
|
2016-05-18 14:04:17 +00:00
|
|
|
=========================================================*/
|
2016-11-22 14:05:28 +00:00
|
|
|
$clen = count($finalid);
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
/* (1) On récupére les ids (initiaux) des contacts de manière unique */
|
|
|
|
$doneNewId = [];
|
2016-11-06 13:22:15 +00:00
|
|
|
$cIdList = [];
|
2016-11-22 14:05:28 +00:00
|
|
|
foreach($finalid as $lastid=>$newid){
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
// si id (ou reference) déja fait -> suivant
|
|
|
|
if( array_key_exists($newid, $doneNewId) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$cIdList[$lastid] = null;
|
|
|
|
$doneNewId[$newid] = false;
|
2016-11-06 13:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ksort($cIdList);
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (2) On remplit les relations */
|
2016-11-23 12:38:15 +00:00
|
|
|
foreach($cIdList as $x=>$yNull){
|
|
|
|
foreach($cIdList as $y=>$xNull)
|
2016-05-30 14:54:41 +00:00
|
|
|
if( $x < $y ){ // On affiche que sous la diagonale
|
|
|
|
|
|
|
|
// Si relation alter-alter
|
2016-11-23 12:38:15 +00:00
|
|
|
$relation_type = isset($matrice[$y]) && in_array($x, $matrice[$y])
|
|
|
|
|| ( isset($matrice[$x]) && in_array($y, $matrice[$x]) );
|
|
|
|
|
|
|
|
$relation_type = $relation_type ? 1 : 0; // 0->aucune relation 1->relation alter alter
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Si la relation existe déja, on ne fait rien */
|
2016-11-23 15:36:25 +00:00
|
|
|
if( self::relAlready($subject_set['relations'], $finalid[$x], $finalid[$y], $relation_type) )
|
|
|
|
continue;
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
$subject_set['relations'][] = [
|
|
|
|
'idA' => $finalid[$x],
|
|
|
|
'idB' => $finalid[$y],
|
2016-11-23 12:38:15 +00:00
|
|
|
'type' => $relation_type
|
2016-11-06 13:22:15 +00:00
|
|
|
];
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-05-30 14:54:41 +00:00
|
|
|
}
|
2016-11-06 13:22:15 +00:00
|
|
|
}
|
2016-05-18 14:04:17 +00:00
|
|
|
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [6] On les données du sujet
|
2016-05-18 14:04:17 +00:00
|
|
|
=========================================================*/
|
2016-11-23 12:38:15 +00:00
|
|
|
$subjectdb->delete($subject_id);
|
2016-11-22 14:05:28 +00:00
|
|
|
$subjectdb->insert($subject_id, $subject_set);
|
|
|
|
|
|
|
|
|
2016-11-23 12:38:15 +00:00
|
|
|
/* [7] Ferme les bases de données
|
2016-11-22 14:05:28 +00:00
|
|
|
=========================================================*/
|
2016-11-23 12:38:15 +00:00
|
|
|
$subjectdb->close();
|
|
|
|
$contactdb->close();
|
2016-11-22 14:05:28 +00:00
|
|
|
|
2016-05-12 19:47:52 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* [7] On met à jour le nouvel ID unique
|
2016-05-17 14:18:45 +00:00
|
|
|
=========================================================*/
|
2016-05-17 15:05:32 +00:00
|
|
|
rewind($funiq); // On revient au début du fichier
|
2016-11-06 13:22:15 +00:00
|
|
|
fwrite($funiq, $maxId); // On écrit la nouvelle valeur (forcément plus grande)
|
2016-05-17 15:05:32 +00:00
|
|
|
flock($funiq, LOCK_UN); // On débloque le verrou
|
|
|
|
fclose($funiq);
|
2016-05-17 14:51:24 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
/* [9] Gestion du retour
|
2016-04-30 10:19:51 +00:00
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-04-30 10:19:51 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
2016-11-23 12:38:15 +00:00
|
|
|
'subject_id' => $subject_id,
|
|
|
|
'nb_contacts' => count($subject_set['contacts']),
|
|
|
|
'nb_relations' => count($subject_set['relations']),
|
|
|
|
'elapsed_time' => microtime(true) - $START_TIME
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-04-30 10:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|