2016-04-18 17:33:36 +00:00
|
|
|
<?php
|
|
|
|
|
2016-10-18 13:11:37 +00:00
|
|
|
namespace api\module;
|
2016-04-18 17:33:36 +00:00
|
|
|
use \manager\sessionManager;
|
2016-11-05 10:56:03 +00:00
|
|
|
use \database\core\DatabaseDriver;
|
2016-04-18 17:33:36 +00:00
|
|
|
use \manager\ManagerError;
|
2016-10-18 13:11:37 +00:00
|
|
|
use \database\core\Repo;
|
|
|
|
use \lightdb\core\lightdb;
|
2016-04-18 17:33:36 +00:00
|
|
|
|
|
|
|
class subject{
|
|
|
|
|
|
|
|
|
2016-11-05 13:58:31 +00:00
|
|
|
/* FETCHES NEW SUBJECTS FROM Lab-Surveys Database
|
|
|
|
*
|
|
|
|
* @note: will store new subjects to localStorage
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private static function fetchNewSubjects(){
|
|
|
|
/* [1] Fetch subjects which have answer this survey
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Section Title */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-18 17:33:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE LA LISTE DE TOUS LES SUJETS
|
|
|
|
*
|
|
|
|
* @return subjects<Array> Tableau contenant les informations de tous les utilisateurs
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getAll(){
|
2016-05-27 10:13:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Contiendra les sujets
|
2016-09-12 16:04:53 +00:00
|
|
|
$subjects = [];
|
2016-05-27 10:13:19 +00:00
|
|
|
|
|
|
|
/* [1] On récupére la liste des sujets
|
2016-04-18 17:33:36 +00:00
|
|
|
=========================================================*/
|
2016-05-27 10:13:19 +00:00
|
|
|
/* (1) On initialise et ouvre la bd */
|
2016-11-22 14:05:28 +00:00
|
|
|
$db = new lightdb('subject');
|
2016-05-27 10:13:19 +00:00
|
|
|
$ids = array_keys( $db->index() );
|
2016-04-18 17:33:36 +00:00
|
|
|
|
2016-05-27 10:13:19 +00:00
|
|
|
/* (2) On récupère tous les sujets */
|
|
|
|
foreach($ids as $id){
|
|
|
|
$sub = $db->fetch($id)['subject'];
|
2016-05-27 08:39:39 +00:00
|
|
|
|
2016-05-27 10:50:32 +00:00
|
|
|
$sub['creation'] = date('d/m/Y H:i:s', $sub['creation']);
|
2016-05-27 10:13:19 +00:00
|
|
|
$subjects[$id] = $sub;
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (3) Si enquête PHONE passée */
|
|
|
|
if( isset($sub['surveys']) && is_array($sub['surveys']) && in_array('phone', $sub['surveys']) )
|
2016-05-27 10:13:19 +00:00
|
|
|
$subjects[$id]['phone'] = true;
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (4) Si enquête FACEBOOK passée */
|
|
|
|
if( isset($sub['surveys']) && is_array($sub['surveys']) && in_array('facebook', $sub['surveys']) )
|
|
|
|
$subjects[$id]['facebook'] = true;
|
2016-05-27 10:13:19 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
}
|
2016-05-27 10:13:19 +00:00
|
|
|
$db->close();
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] Gestion du retour
|
2016-04-18 17:33:36 +00:00
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-04-18 17:33:36 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
2016-05-27 10:13:19 +00:00
|
|
|
'subjects' => $subjects
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-04-18 17:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-27 10:50:32 +00:00
|
|
|
/* RETOURNE LA LISTE DE TOUS LES AMIS D'UN SUJET
|
|
|
|
*
|
|
|
|
* @subject_id<int> Identifiant du sujet d'enquête
|
|
|
|
*
|
|
|
|
* @return subjects<Array> Tableau contenant les informations de tous les utilisateurs
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getFriends($params){
|
|
|
|
extract($params);
|
|
|
|
|
|
|
|
|
|
|
|
// Contiendra les sujets
|
2016-11-23 15:36:25 +00:00
|
|
|
$contacts = [];
|
2016-05-27 10:50:32 +00:00
|
|
|
|
|
|
|
/* [1] On récupére la liste des sujets
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On initialise et ouvre la bd */
|
2016-11-23 15:36:25 +00:00
|
|
|
$db = new lightdb('subject');
|
2016-05-27 10:50:32 +00:00
|
|
|
$fetch = $db->fetch($subject_id);
|
|
|
|
$db->close();
|
|
|
|
|
|
|
|
/* (2) Si on trouve personne, on renvoie une erreur */
|
|
|
|
if( $fetch === false )
|
2016-09-12 16:04:53 +00:00
|
|
|
return [ 'ModuleError' => ManagerError::ModuleError ];
|
2016-05-27 10:50:32 +00:00
|
|
|
|
|
|
|
/* (3) On enregistre ses contacts s'il en a */
|
2016-11-23 15:36:25 +00:00
|
|
|
$db = new lightdb('contact');
|
2016-05-27 10:50:32 +00:00
|
|
|
|
2016-11-23 15:36:25 +00:00
|
|
|
if( isset($fetch['contacts']) ){
|
2016-05-27 10:50:32 +00:00
|
|
|
|
2016-11-23 15:36:25 +00:00
|
|
|
foreach($fetch['contacts'] as $contactId){
|
2016-05-27 10:50:32 +00:00
|
|
|
|
2016-11-23 15:36:25 +00:00
|
|
|
$contact = $db->fetch($contactId);
|
|
|
|
// si le contact n'est pas trouvé -> passe au suivant
|
|
|
|
if( $contact === false )
|
|
|
|
continue;
|
2016-05-27 10:50:32 +00:00
|
|
|
|
2016-11-23 15:36:25 +00:00
|
|
|
$contacts[$contactId] = $contact;
|
2016-05-27 10:50:32 +00:00
|
|
|
|
2016-11-23 15:36:25 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-27 10:50:32 +00:00
|
|
|
$db->close();
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] Gestion du retour
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-05-27 10:50:32 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
2016-11-23 15:36:25 +00:00
|
|
|
'subjects' => $contacts
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-05-27 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-18 17:33:36 +00:00
|
|
|
|
|
|
|
/* CREATION D'UN SUJET
|
|
|
|
*
|
2016-05-27 08:39:39 +00:00
|
|
|
* @name<String> Pseudo du sujet
|
2016-04-18 17:33:36 +00:00
|
|
|
*
|
|
|
|
* @return id_subject<int> Renvoie l'id du sujet cree
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function create($params){
|
|
|
|
extract($params);
|
|
|
|
|
2016-05-27 08:39:39 +00:00
|
|
|
|
|
|
|
/* [1] On récupère l'id unique actuel
|
2016-04-18 17:33:36 +00:00
|
|
|
=========================================================*/
|
2016-10-18 13:11:37 +00:00
|
|
|
$funiq = fopen( __BUILD__.'/src/dynamic/uniqid', 'r+' );
|
2016-05-27 08:39:39 +00:00
|
|
|
flock($funiq, LOCK_EX); // On verrouille le fichier
|
2016-06-01 15:06:18 +00:00
|
|
|
$uniqid = trim( fgets( $funiq ) );
|
|
|
|
|
2016-05-27 08:39:39 +00:00
|
|
|
|
|
|
|
if( !is_numeric($uniqid) )
|
|
|
|
$uniqid = 0;
|
2016-04-18 17:33:36 +00:00
|
|
|
|
2016-05-27 08:39:39 +00:00
|
|
|
// Décalage à appliquer à tous les ids
|
|
|
|
$newId = intval($uniqid) + 1;
|
|
|
|
|
2016-05-27 10:50:32 +00:00
|
|
|
// On crée notre sujet
|
2016-09-12 16:04:53 +00:00
|
|
|
$data = [ 'subject' => [
|
2016-05-27 08:39:39 +00:00
|
|
|
'id' => $newId,
|
|
|
|
'name' => $name,
|
2016-11-22 14:05:28 +00:00
|
|
|
'creation' => time(),
|
|
|
|
'surveys' => []
|
2016-09-12 16:04:53 +00:00
|
|
|
]];
|
2016-05-27 08:39:39 +00:00
|
|
|
|
2016-05-27 10:50:32 +00:00
|
|
|
/* [2] On crée le sujet dans SURVEYS
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On initialise et ouvre la bd */
|
2016-11-23 15:36:25 +00:00
|
|
|
$db = new lightdb('subject');
|
2016-05-27 08:39:39 +00:00
|
|
|
$db->insert( $newId, $data );
|
|
|
|
$db->close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On met à jour le nouvel ID unique
|
|
|
|
=========================================================*/
|
|
|
|
rewind($funiq); // On revient au début du fichier
|
|
|
|
fwrite($funiq, $newId); // On écrit la nouvelle valeur (forcément plus grande)
|
|
|
|
flock($funiq, LOCK_UN); // On débloque le verrou
|
|
|
|
fclose($funiq);
|
|
|
|
|
2016-04-18 17:33:36 +00:00
|
|
|
|
|
|
|
/* [2] Gestion du retour
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-04-18 17:33:36 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
2016-05-27 08:39:39 +00:00
|
|
|
'id_subject' => $newId
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-04-18 17:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-26 14:33:00 +00:00
|
|
|
/* RECHERCHE DE SUJETS
|
|
|
|
*
|
|
|
|
* @name<String> Nom du sujet recherché
|
|
|
|
*
|
|
|
|
* @return results<Array> Tableau contenant les résultats
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function search($params){
|
|
|
|
extract($params);
|
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
|
2016-05-26 14:33:00 +00:00
|
|
|
// Contiendra les sujets
|
2016-09-12 16:04:53 +00:00
|
|
|
$subjects = [];
|
2016-05-26 14:33:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [0] Notre fonction de recherche (comparaison)
|
|
|
|
=========================================================*/
|
|
|
|
function compareSearch($A, $B){
|
2016-11-05 10:59:42 +00:00
|
|
|
// Returns all if no search keyword
|
|
|
|
if( $A == '' )
|
|
|
|
return true;
|
|
|
|
|
2016-05-26 14:33:00 +00:00
|
|
|
// {1} On supprime les espaces et tout en minuscule //
|
|
|
|
$A = str_replace(' ', '', strtolower($A));
|
|
|
|
$B = str_replace(' ', '', strtolower($B));
|
|
|
|
|
|
|
|
// {2} On vérifie si A est dans B et inversement //
|
|
|
|
if( strpos($A, $B) !== false ) return true;
|
|
|
|
if( strpos($B, $A) !== false ) return true;
|
|
|
|
|
|
|
|
return $A == $B;
|
|
|
|
}
|
|
|
|
|
2016-05-27 08:51:05 +00:00
|
|
|
/* [1] On récupére la liste des sujets
|
2016-05-26 14:33:00 +00:00
|
|
|
=========================================================*/
|
|
|
|
/* (1) On initialise et ouvre la bd */
|
2016-11-22 14:05:28 +00:00
|
|
|
$db = new lightdb('subject');
|
2016-05-26 14:33:00 +00:00
|
|
|
$ids = array_keys( $db->index() );
|
|
|
|
|
|
|
|
/* (2) On récupère tous les sujets */
|
|
|
|
foreach($ids as $id){
|
|
|
|
$sub = $db->fetch($id)['subject'];
|
2016-05-26 17:43:50 +00:00
|
|
|
if( compareSearch($name, $sub['name']) ){
|
|
|
|
|
2016-05-27 08:51:05 +00:00
|
|
|
$sub['creation'] = date('d/m/Y H:i:s', $sub['creation']);
|
|
|
|
$subjects[$id] = $sub;
|
2016-05-26 14:33:00 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (3) Si enquête PHONE passée */
|
|
|
|
if( isset($sub['surveys']) && is_array($sub['surveys']) && in_array('phone', $sub['surveys']) )
|
|
|
|
$subjects[$id]['phone'] = true;
|
2016-05-26 14:33:00 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (4) Si enquête FACEBOOK passée */
|
|
|
|
if( isset($sub['surveys']) && is_array($sub['surveys']) && in_array('facebook', $sub['surveys']) )
|
|
|
|
$subjects[$id]['facebook'] = true;
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 08:51:05 +00:00
|
|
|
$db->close();
|
2016-05-26 17:43:50 +00:00
|
|
|
|
2016-05-26 14:33:00 +00:00
|
|
|
/* [4] Retour des données
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-05-26 14:33:00 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
|
|
|
'results' => $subjects
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-05-26 14:33:00 +00:00
|
|
|
|
|
|
|
}
|
2016-04-18 17:33:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|