diff --git a/build/api/module/historyDefault.php b/build/api/module/historyDefault.php new file mode 100644 index 0000000..521fd0d --- /dev/null +++ b/build/api/module/historyDefault.php @@ -0,0 +1,114 @@ + UID de l'utilisateur + * @id_machine UID la machine + * @id_action UID de l'action + * @timestamp timestamp de l'action + * + * @return status Retourne si oui ou non, tout s'est bien passe + * + */ + public static function create($params){ + extract($params); + + /* [1] Creation de l'utilisateur + =========================================================*/ + $create_entry = new Repo('history/create', [ + $id_entry, + $id_machine, + $id_action, + $timestamp + ]); + $id_entry = $create_entry->answer(); + + // Si une erreur est retournee, on retourne une erreur + if( $id_entry === false ) + return ['ModuleError' => Error::ModuleError]; + + + /* [2] Gestion du retour + =========================================================*/ + return [ + 'id_history' => $id_entry + ]; + } + + + + + + /* RENVOIE UN UTILISATEUR EN FONCTION D'UN MOT CLE + * + * @keywords Element de recherche + * + * @return users Retourne la liste des utilisateurs trouves + * + */ + public static function search($params){ + extract($params); + + // On recupere les donnees + $user = new Repo('history/search', [ + $_SESSION['WAREHOUSE']['id'], + $keywords + ]); + + return [ 'history' => $user->answer() ]; + } + + + + + + /* RENVOIE LA LISTE EXHAUSTIVE DES ACCES + * + * @return history Liste des entrées de l'historique + * + */ + public static function getAll(){ + // On recupere les donnees + $entries = new Repo('history/getAll', [ $_SESSION['WAREHOUSE']['id'] ]); + + return [ 'history' => $entries->answer() ]; + } + + + /* SUPPRIME UNE ENTREE + * + * @id_history UID de l'entree en question + * + * @return status Retourne si oui ou non tout s'est bien deroule + * + */ + public static function delete($params){ + extract($params); + + /* [1] On supprime l'utilisateur + =========================================================*/ + $del_entry = new Repo('history/delete', [ + $_SESSION['WAREHOUSE']['id'], + $id_history + ]); + $deleted_entry = $del_entry->answer(); + + + return [ 'status' => $deleted_entry ]; + } + + + } + + +?> diff --git a/build/database/repo/history.php b/build/database/repo/history.php new file mode 100644 index 0000000..eb06f2f --- /dev/null +++ b/build/database/repo/history.php @@ -0,0 +1,164 @@ + UID de l'utilisateur + * @id_machine UID de la machine + * @id_action UID de l'action + * @timestamp Timestamp de l'action + * + * @return id_history Renvoie l'UID de l'entree cree + * Renvoie FALSE si une erreur occure + * + */ + public static function create($id_user, $id_machine, $id_action, $timestamp){ + /* [1] On retourne l'id_history ou FALSE si erreur + =========================================================*/ + $inserted = Table::get('history')->insert([ + 'id_history' => Rows::INSERT_DEFAULT, + 'id_user' => $id_user, + 'id_machine' => $id_machine, + 'id_action' => $id_action, + 'timestamp' => $timestamp + ]); + + // Si erreur d'insertion, erreur + if( !$inserted ) + return false; // Si pb d'unicité du code ou username (car sont uniques) ou autre + + + /* [2] On récupère l'id_history + =========================================================*/ + $check_history = self::getByTimestamp($timestamp); + + // Si on trouve pas, erreur + if( $check_history === false ) + return false; + + // Sinon, on retourne son id + return $check_history['id_history']; + + } + + + + + /* SUPPRIME UNE ENTREE DONNEE + * + * @id_history UID de l'entreee + * + * @return status Retourne si oui ou non l'entree a bien ete supprimee + * + */ + public static function delete($id_history){ + /* [1] On redige/execute la requete + =========================================================*/ + $delete = Table::get('history') + ->whereId($id_history); + + // On retourne l'état + return $delete->delete(); + + } + + + + + + + + + /* RETOURNE UNE ENTREE SPECIFIQUE + * + * @id_history UID de l'entree + * + * @return entry Données de l'entree + * FALSE si aucun résultat + * + */ + public static function getById($id_history){ + /* [1] On rédige/execute la requête + =========================================================*/ + $user = Table::get('user') + ->whereIdWarehouse($id_warehouse) + ->whereIdUser($id_user) + ->select('*') + ->unique(); + + return $user->fetch(); + } + + + + /* RETOURNE TOUT L'HISTORIQUE DE L'ENTREPOT + * + * @id_warehouse UID de l'entrepot + * + * @return entries Entrees de l'historique + * + */ + public static function getAll($id_warehouse){ + /* [1] On rédige/execute la requête + =========================================================*/ + $users = Table::get('user') + ->whereIdWarehouse($id_warehouse) + ->select('id_user') + ->select('username', null, null, 'user_name') + ->select('firstname', null, null, 'user_firstname') + ->select('lastname', null, null, 'user_lastname'); + + $machines = Table::get('machine') + ->whereIdWarehouse($id_warehouse) + ->select('id_machine') + ->select('name', null, null, 'machine_name'); + + $actions = Table::get('action') + ->select('id_action') + ->select('name', null, null, 'action_name'); + + + $history = Table::get('history') + ->join('id_user', $users) + ->join('id_machine', $machines) + ->join('id_action', $actions) + ->select('id_history') + ->select('timestamp') + ->orderby('timestamp', Rows::ORDER_ASC); + + return $history->fetch(); + } + + + + + + + + + + + + + + + + + + + + + + + } + + +?> diff --git a/build/viewer/view/history/history_view.php b/build/viewer/view/history/history_view.php new file mode 100644 index 0000000..eaf866e --- /dev/null +++ b/build/viewer/view/history/history_view.php @@ -0,0 +1,127 @@ + $_SESSION['WAREHOUSE']['theme'] + ]; + + /* [3] Store functions + =========================================================*/ + $twig->addFunction(new \Twig_Function('f_history', function(){ + $req = new ModuleRequest('historyDefault/getAll', []); + + $res = $req->dispatch(); + + // si erreur, on retourne rien par défaut + if( $res->error != Error::Success ) + return []; + + return $res->get('history'); + })); + + + $twig->addFilter(new \Twig_Filter('f_tsformat', function($ts){ + return date('d/m/Y H:i:s', $ts); + })); + + + $twig->addFilter(new \Twig_Filter('f_tsrelative', function($ts){ + $r = self::relativetime($ts); + + // if only one + $o = $r[0] <= 1; + + switch($r[1]){ + + case 'Y': return $r[0].' an'.($o?'':'s'); break; + case 'm': return $r[0].' mois'; break; + case 'd': return $r[0].' jour'.($o?'':'s'); break; + + case 'H': return $r[0].' heure'.($o?'':'s'); break; + case 'i': return $r[0].' minute'.($o?'':'s'); break; + case 's': return $r[0].' seconde'.($o?'':'s'); break; + + default: return 'peu de temps'; break; + } + })); + + /* [4] Build the whole stuff + =========================================================*/ + return $twig->render('history/history_view.twig', [ + 'p_theme' => $variables['p_theme'] + ]); + } + + + + /* RETURNS A RELATIVE HUMAN-READABLE TIME + * + * @ts Timestamp to process + * + * @return relative human-readable relative time [value, unit] + * + */ + private static function relativetime($ts){ + + /* [1] Explode time into human-readable time units + =========================================================*/ + $units = []; + + /* (1) Date units */ + $units['year'] = (int) date('Y', $ts); + $units['month'] = (int) date('m', $ts); + $units['day'] = (int) date('d', $ts); + + /* (2) Time units */ + $units['hour'] = (int) date('H', $ts); + $units['minute'] = (int) date('i', $ts); + $units['second'] = (int) date('s', $ts); + + + /* [2] Calculate relative time for each unit + =========================================================*/ + /* (1) Date units */ + $units['year'] = intval(date('Y')) - $units['year']; + $units['month'] = intval(date('m')) - $units['month']; + $units['day'] = intval(date('d')) - $units['day']; + + /* (2) Time units */ + $units['hour'] = intval(date('H')) - $units['hour']; + $units['minute'] = intval(date('i')) - $units['minute']; + $units['second'] = intval(date('s')) - $units['second']; + + + /* [3] Return significative relative time + =========================================================*/ + /* (1) Date units */ + if( $units['year'] > 0 ) return [ $units['year'], 'Y' ]; + if( $units['month'] > 0 ) return [ $units['month'], 'm' ]; + if( $units['day'] > 0 ) return [ $units['day'], 'd' ]; + + /* (2) Time units */ + if( $units['hour'] > 0 ) return [ $units['hour'], 'H' ]; + if( $units['minute'] > 0 ) return [ $units['minute'], 'i' ]; + if( $units['second'] > 0 ) return [ $units['second'], 's' ]; + + /* (3) Default value */ + return [0, '.']; + } + + + } diff --git a/build/viewer/view/history/history_view.twig b/build/viewer/view/history/history_view.twig new file mode 100644 index 0000000..7c9c72d --- /dev/null +++ b/build/viewer/view/history/history_view.twig @@ -0,0 +1,37 @@ + + +
+ Machine + Dernière utilisation + Utilisateur + Action + Historique détaillé +
+ + +{% for entry in f_history() %} +
+ + #{{ entry.machine_name }} + + + {{ entry.timestamp | f_tsformat }} + Il y a {{ entry.timestamp | f_tsrelative }} + + + + + {{ entry.user_name }} + {{ entry.user_firstname }} {{ entry.user_lastname }} + + + + {{ entry.action_name }} + + + + + + +
+{% endfor %} diff --git a/config/modules.json b/config/modules.json index 8ea7308..7200a1a 100755 --- a/config/modules.json +++ b/config/modules.json @@ -452,6 +452,17 @@ } } + }, + + "historyDefault": { + "getAll": { + "description": "Retourne l'historique complet", + "permissions": ["warehouse", "admin"], + "parameters": {}, + "output": { + "history": { "description": "Données de l'historique", "type": "array" } + } + } } } diff --git a/config/repositories.json b/config/repositories.json index 44c7ea2..c18d293 100755 --- a/config/repositories.json +++ b/config/repositories.json @@ -1,4 +1,17 @@ { + "history": [ + "create", + "delete", + + "search", + + "getAll", + "getByUser", + "getByMachine", + "getByCluster" + + ], + "user": [ "create", "edit", diff --git a/public_html/view/history.php b/public_html/view/history.php index 85673a8..7daec1c 100755 --- a/public_html/view/history.php +++ b/public_html/view/history.php @@ -4,6 +4,7 @@ use \error\core\Error; use \database\core\DatabaseDriver; use \database\core\Repo; + use \viewer\core\Viewer; use \orm\core\Table; use \orm\core\Rows; ?> @@ -41,62 +42,11 @@ - /* [1] VIEW -> Liste des utilisateurs + /* [1] VIEW -> Liste des acces =========================================================*/ - $getusers = new ModuleRequest('userDefault/getAll'); // On utilise la methode 'getAll' du module 'userDefault' - $nbusers = count( $getusers->dispatch()->get('users') ); // On recupere la reponse - - $getmachines = new ModuleRequest('machineDefault/getAll'); // On utilise la methode 'getAll' du module 'machineDefault' - $nbmachines = count( $getmachines->dispatch()->get('machines') ); // On recupere la reponse - echo "
"; - // Barre de recherche - echo ""; - - - /* (1) On récupère les données - ---------------------------------------------------------*/ - $mac = Table::get('machine') - ->select('*'); - - $selected = Table::get('history') - ->join('id_machine', $mac) - ->select('timestamp', Rows::SEL_MAX) - ->select('id_history') - ->fetch(); - - - echo "
"; - echo "Machine"; - echo "Dernière utilisation"; - echo "Conducteurs"; - echo "Historique détaillé"; - echo "
"; - - foreach($selected as $m=>$mac){ - - echo "
"; - - echo "#".$mac['name'].""; - - echo ""; - echo "".date('d/m/Y H:i:s', $mac['timestamp']).""; - echo "Il y a xx jours et yy heures"; - echo ""; - - - echo ""; - echo "".$mac['agg_id_history']." conducteur(s)"; - echo "test"; - echo ""; - - echo ""; - echo ""; - echo ""; - - echo "
"; - } - + $view = new Viewer('history.view', []); + $view->view(); echo '
';