123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- /* Authors : Carlos C. Corrada-Bravo
- David J. Ortiz-Rivera
-
- Organization : Centro de Desarrollo y Consultoria Computacional
- Project : OPASO Material Registry
- File : manage-personnel.js
- Description : Generate personnel table */
-
- /* global values */
- var auth = {};
- var labs = {};
- var access_levels = {};
- var current_person_id;
- var next;
- var current_lab_id;
-
- /* set listeners when document is ready */
- $(document).ready(fetch_personnel);
-
- /* display_form(type: string,transaction: string) - configure/display form */
- function display_form(type,transaction){
- let form = `.${type}-form`;
- let submit = `#${type}-submit`;
- $(".form-shader").addClass("fade-in");
- $(form).addClass("c-form");
- $(form).addClass("slide-down");
- $(submit).attr("value",transaction);
- }
-
- /* hideForm(clear: bool) - hide/clear form */
- function hide_form(clear){
- $(".c-form").removeClass("slide-down");
- $(".c-form").removeClass("c-form");
-
- /* wait for animation */
- setTimeout(function(){
- $(".form-shader").removeClass("fade-in");
- $(".laboratories").html("");
- if(clear){
- $(".ap-field").val("");
- }
- },250);
- return false;
- }
-
- /* fetch_access_level(action) - extract user's acess level for lab */
- function fetch_access_level(action){
- /* extract access level */
- let lab_id = $(".laboratories").val();
- if(current_person_id in access_levels){
- if(lab_id in access_levels[current_person_id]){
- var access_level = access_levels[current_person_id][lab_id]["access_level"];
- }
-
- else{
- var access_level = "none";
- }
- }
-
- else{
- var access_level = "none";
- }
-
-
- /* restrict options */
- if(action === "restrict" && access_level === "technician"){
- $("option[value=investigator]").attr("disabled",true);
- }
-
- else{
- $("option[value=investigator]").attr("disabled",false);
- }
-
- if(action === "authorize" && access_level === "investigator"){
- $("option[value=technician]").attr("disabled",true);
- }
-
- else{
- $("option[value=technician]").attr("disabled",false);
- }
-
- $(".access_level").val(access_level);
- }
-
- /* action - handle material transactions */
- function action(){
- /* extract args */
- let action = $(this).attr("value");
- let r = $(this).parent().parent().attr("id");
- let s = `#${r}`;
- var restrict = true;
- let name = $(s).find("td[value=person_name]").text();
- current_person_id = $(s).find("td[value=person_name]").attr("person_id");
-
- /* set user info */
- $(".person").text(name);
- $(".main-header").text(`${action} user`);
-
- /* authorize */
- if(action === "authorize"){
- for(var lab in labs){
- var option = element_gen("option",{class: "option",text: labs[lab]["lab_room"],value: labs[lab]["lab_id"]});
- $(".laboratories").append(option);
- }
-
- $("option[value=none]").attr("disabled",true);
- }
-
- /* restrict */
- else{
- if(current_person_id in access_levels){
- var access_level = access_levels[current_person_id];
- for(var l in labs){
- for(var lab_id in access_level){
- if(labs[l]["lab_id"] == lab_id){
- var option = element_gen("option",{class: "option",text: access_level[lab_id]["lab_room"],value: lab_id});
- $(".laboratories").append(option);
- break;
- }
- }
- }
- }
-
- /* no authorized labs */
- else{
- restrict = false;
- }
-
- $("option[value=none]").attr("disabled",false);
- }
-
- /* user is being authorized/has authorized labs */
- if(restrict){
- fetch_access_level(action);
- display_form("main",action);
- }
-
- else{
- set_alert("warning","No authorized labs found");
- }
- }
-
- /* manage_personnel() - authorize/restrict labs for a user */
- function manage_personnel(){
- let action = $(this).attr("value");
- let access_level = $(".access_level").val();
- current_lab_id = $(".laboratories").val();
- let lab_room = $(".laboratories option:selected").text();
-
- /* validate and submit */
- if(is_valid(current_lab_id) && is_valid(access_level) && is_valid(action) && is_valid(current_person_id)){
- $.post("/scripts/opaso",{query: 16,lab_id: current_lab_id,access_level: access_level,action: action,person_id: current_person_id},function(data){
- // console.log(data);
- /* extract response */
- let response = JSON.parse(data);
- let status = response["status"];
-
- /* handle by status */
- switch(status){
- case "expired": /* on expired, redirect to index */
- window.location.href = "/?error=session_expired";
- break;
-
- case "success": /* on success, update access level */
- /* determine authorized labs/highest access level */
- if(current_person_id in access_levels){
- var authorized = [];
- var highest_access_level = "technician";
-
- if(action === "restrict" && access_level === "none"){
- delete access_levels[current_person_id][current_lab_id];
- }
-
- else{
- access_levels[current_person_id][current_lab_id] = {lab_room: lab_room,access_level: access_level};
- }
-
- for(var lab_id in access_levels[current_person_id]){
- authorized.push(access_levels[current_person_id][lab_id]["lab_room"]);
-
- if(access_levels[current_person_id][lab_id]["access_level"] === "investigator"){
- highest_access_level = "investigator";
- }
- }
-
- if(!authorized.length){
- authorized = "-";
- }
-
- else{
- authorized = authorized.join(", ");
- }
- }
-
- /* no authorized labs */
- else{
- var authorized = lab_room;
- var highest_access_level = access_level;
- access_levels[current_person_id] = {};
- access_levels[current_person_id][current_lab_id] = {lab_room: lab_room,access_level: access_level};
- }
-
- $(`td[person_id=${current_person_id}]`).parent().find("td[value=authorized]").text(authorized);
- $(`td[person_id=${current_person_id}]`).parent().find("td[value=access_level]").text(highest_access_level);
- hide_form();
-
- default: /* on success/error, display message */
- /* display alert */
- set_alert(status,response["message"]);
- break;
- }
- });
- }
-
- /* missing args */
- else{
- set_alert("error","One or more missing args");
- }
- }
-
- /* set_listeners() - listen for events */
- function set_listeners(){
-
- /* authorize/restrict access */
- $("#main-submit").click(manage_personnel);
-
- /* register user */
- $("#add-submit").click(register_user);
-
- /* fetch access level for lab */
- $(".laboratories").change(function(){
- let action = $("#main-submit").attr("value");
- fetch_access_level(action);
- });
-
- /* handle actions */
- $("tbody").on("click",".action",action);
-
- /* add material to inventory */
- $("#add").click(function(){
- display_form("add","add-material");
- });
-
- /* close form */
- $(".close-form").click(function(event){
- if(event.target === event.currentTarget){
- hide_form(false);
- }
- });
- }
-
- /* table_gen(data: dictionary) - generate table body */
- function table_gen(data){
- for(var d in data){
- /* generate person row */
- let person_id = data[d]["person_id"];
- let tr = element_gen("tr",{id: d});
- let person = element_gen("td",{person_id: person_id,text: data[d]["person_name"],value: "person_name"});
-
- /* determine authorized labs/highest access level */
- var authorized = [];
- let highest_access_level = "technician";
- if(person_id in access_levels){
- for(var lab_id in access_levels[person_id]){
- authorized.push(access_levels[person_id][lab_id]["lab_room"]);
-
- if(access_levels[person_id][lab_id]["access_level"] === "investigator"){
- highest_access_level = "investigator";
- }
- }
- }
-
- if(!authorized.length){
- authorized = "-";
- }
-
- else{
- authorized = authorized.join(", ");
- }
-
- let labs = element_gen("td",{text: authorized,value: "authorized"});
- let access_level = element_gen("td",{text: highest_access_level,value: "access_level"});
- tr.append(person,labs,access_level);
-
- /* generate actions */
- let actions = {authorize:{class: "t-icon material-icons",icon: "add"},restrict: {class: "t-icon material-icons",icon: "remove"}};
- var actions_cell = element_gen("td",{class: "actions-cell"});
- for(var action in actions){
- var icon = element_gen("i",{class: actions[action]["class"],text: actions[action]["icon"]});
- var button = element_gen("button",{class: "button action",id: action,value: action,title: action,childs: {icon}});
- actions_cell.append(button);
- }
-
- /* append */
- tr.append(actions_cell);
- $("tbody").append(tr);
- }
-
- $(".processing").hide();
- setTimeout(function(){
- set_listeners();
- $(".main-wrapper").show();
- },250);
- }
-
- /* fetch_personnel() - fetch personnel list */
- function fetch_personnel(){
- $.post("/scripts/opaso",{query: 15},function(data){
- // console.log(data);
- /* extract reponse */
- let response = JSON.parse(data);
- let status = response["status"];
-
- /* handle by status */
- switch(status){
- case "success": /* on success, generate table */
- /* extract args */
- let personnel = response["personnel"];
- auth = response["authorized"];
- access_levels = response["access_levels"];
- labs = response["labs"];
- next = Object.keys(personnel).length;
-
- /* generate personnel table */
- table_gen(personnel);
- $(".table-total").text(`Total: ${next}`);
- break;
-
- case "expired": /* on expired, redirect to index */
- window.location.href = "/?error=session_expired";
- break;
-
- default: /* on error, display message */
- set_alert(status,response["message"]);
- break;
- }
- });
- }
-
- /* register_user() - validate input and register user */
- function register_user(){
- /* extract args */
- let email = $("#email").val();
- let person_name = $("#person_name").val();
- var phone_number = $("#phone_number").val();
-
- /* validate user data */
- if(is_valid(person_name,"text") && is_valid(email,"email")){
- /* validate phone number */
- if(!is_valid(phone_number,"number")){
- phone_number = "n/a"
- }
-
- /* reset fields */
- $(".invalid-feedback").hide();
- $(".invalid").removeClass("invalid");
-
- /* query server */
- $.post("/scripts/opaso",{query: 0,person_name: person_name,email: email,phone_number: phone_number},function(data){
- // console.log(data);
- /* extract response */
- let response = JSON.parse(data);
- let status = response["status"];
-
- /* handle by status */
- switch(status){
- case "expired": /* on expired, redirect to index */
- window.location.href = "/?error=session_expired";
- break;
-
- case "success": /* on success, add user */
- /* extract person id */
- let person_id = response["person_id"];
-
- /* generate table row */
- let tr = element_gen("tr",{class: "personnel-row",id: next});
- let person = element_gen("td",{class: "personnell-cell",person_id: person_id,text: person_name,value: "person_name"});
- let labs = element_gen("td",{class: "personnell-cell",text: "-",value: "authorized"});
- let access_level = element_gen("td",{class: "personnell-cell",text: "technician",value: "access_level"});
- tr.append(person,labs,access_level);
-
- /* generate actions */
- let actions = {authorize:{class: "t-icon material-icons",icon: "add"},restrict: {class: "t-icon material-icons",icon: "remove"}};
- var actions_cell = element_gen("td",{class: "actions-cell"});
- for(var action in actions){
- var icon = element_gen("i",{class: actions[action]["class"],text: actions[action]["icon"]});
- var button = element_gen("button",{class: "button action",id: action,value: action,title: action,childs: {icon}});
- actions_cell.append(button);
- }
-
- /* append */
- tr.append(actions_cell);
- $("tbody").append(tr);
-
- /* sort */
- next += 1;
- full_sort(next,"person_name");
- $(".table-total").text(`Total: ${next}`);
- hide_form(true);
-
- default: /* on success/error, display message */
- set_alert(status,response["message"]);
- break;
- }
- });
- }
-
- /* invalid user info */
- else{
- $(".required").each(function(){
- /* extract id */
- var id = $(this).attr("id");
- var s = `.${id}`;
-
- /* highlight */
- if(!is_valid($(this).val(),id)){
- $(s).show();
- $(this).addClass("invalid");
- }
-
- /* unhighlight */
- else{
- $(s).hide();
- $(this).removeClass("invalid");
- }
- });
-
- set_alert("warning","Please correct highlighted fields");
- }
- }
|