<%@language = "javascript" %>
<% /*
     $Revision$
     $Id$

     ChatQueueGet.asp

     Haal Queue info op

*/

var JSON_Result = true;
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="./chat.inc" -->
<%

var chat_queue = JSON.parse(Application(chat_queue_name) || "{}");
var dis_key = getQParamInt("dis_key", -1); // discipline
var autfunction = "WEB_CHTOPR";
// Na 5 minuten uit de queue
for (var wantId in chat_queue.wanting)
{
    var wanting = chat_queue.wanting[wantId];
    var ago5 = new Date();
    ago5.setMinutes(ago5.getMinutes() - 5);
    if (wanting.since < ago5)
        delete chat_queue.wanting[wantId]
}
Application.Lock();
Application(chat_queue_name) = JSON.stringify(chat_queue);
Application.UnLock();
var discs = {};
if (dis_key < 0)
{
    var sql = "SELECT md.ins_discipline_key, td.ins_discipline_omschrijving "
            + "  FROM fac_v_my_disciplines md, ins_tab_discipline td"
            + " WHERE fac_functie_module = 'CHT'"
            + "   AND md.prs_perslid_key = " + user_key
            + "   AND md.fac_functie_code = " + safe.quoted_sql(autfunction)
            + "   AND fac_gebruiker_alg_level_write < 9"
            + "   AND fac_gebruiker_prs_level_write < 9"
            + "   AND md.ins_discipline_key = td.ins_discipline_key";
    var oRs = Oracle.Execute(sql);
    while (!oRs.eof)
    {
        discs[oRs("ins_discipline_key").Value] = oRs("ins_discipline_omschrijving").Value;
        oRs.MoveNext();
    }
    oRs.Close();
}

// remove other dis_key info
for (var wantId in chat_queue.wanting)
{
    var wanting = chat_queue.wanting[wantId];
    // only authorized discs
    if (dis_key < 0 && "dis_key" in wanting)
    {
        // delete
        if (!(wanting.dis_key in discs))
        {
            delete chat_queue.wanting[wantId];
        }
        // add description
        else
        {
            wanting.dis_oms = discs[wanting.dis_key];
        }
    }
    // only 1 dis_key
    else if (dis_key > 0 && (!("dis_key" in wanting) || wanting.dis_key != dis_key))
    {
        delete chat_queue.wanting[wantId];
    }
}

result = { chat_queue: chat_queue, since: new Date() };

Response.Write(JSON.stringify(result));

ASPPAGE_END();
%>
