IVE出席率及缺席率計算器

幫助IVE學生計算出席率及缺席率

目前為 2017-12-06 提交的版本,檢視 最新版本

/*
Copyright (C) 2017 Miklet

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// ==UserScript==
// @name         IVE出席率及缺席率計算器
// @namespace    undefined
// @version      1.3.0
// @description  幫助IVE學生計算出席率及缺席率
// @author       Miklet
// @license      MIT
// @match        *://myportal.vtc.edu.hk/*
// @require      https://code.jquery.com/jquery-latest.js
// @require      https://greasyfork.org/scripts/33537-grant-none-shim-js/code/grant-none-shimjs.js?version=220694
// @run-at       document-start
// @grant        GM_listValues
// @grant        GM_registerMenuCommand
// @grant        unsafeWindow
// ==/UserScript==
(function () {
    'use strict';
    // Create Elements
    $(function () {
        GM_registerMenuCommand('重設報告表', function() { 
            GM_setValue("sbjList", "[]");
            calcTable();
        });
        GM_registerMenuCommand('重設全部', function() { 
            var isConfirm = confirm("確認重設全部資料?");
            if(isConfirm){
                var allValue = GM_listValues();
                for(var i = 0; i < allValue.length; i++){
                    GM_deleteValue(allValue[i]);
                }
                GM_setValue("sbjList", "[]");
                alert("重設完成!請刷新頁面!");
            }
        });
        //Bottom Right
        var btn_div = document.createElement("div");
        btn_div.setAttribute("style", "width:15vw;position:fixed;right:0;bottom:0;border:0;font-family:'Microsoft jhenghei';z-index:999");
        btn_div.setAttribute("id", "btnDiv");
        var info_div = document.createElement("p");
        info_div.setAttribute("style", "margin:0;width:100%;background-color:#555;color:#fff;border:0;border-bottom:1px solid #fff;line-height:14px;font-size:13px;");
        info_div.setAttribute("id", "infoDiv");
        var calc_btn = document.createElement("button");
        calc_btn.setAttribute("style", "width:100%;padding:10px;background-color:#555;color:#fff;border:0;line-height:14px;font-size:13px;");
        calc_btn.setAttribute("id", "btnCalc");
        calc_btn.innerHTML = "計算出缺率";
        var setting_btn = document.createElement("button");
        setting_btn.setAttribute("style", "width:85%;padding:10px;background-color:#555;color:#fff;border:0;line-height:14px;font-size:13px;");
        setting_btn.setAttribute("id", "btnSet");
        setting_btn.innerHTML = "編輯總時數";
        var toggle_btn = document.createElement("button");
        toggle_btn.setAttribute("style", "width:15%;padding:10px;background-color:#555;color:#fff;border:0;line-height:14px;font-size:13px;");
        toggle_btn.setAttribute("id", "btnToggle");
        toggle_btn.innerHTML = "-";

        //Bottom Left
        var result_div = document.createElement("div");
        result_div.setAttribute("style", "min-width:24vw;position:fixed;left:0;bottom:0;border:0;font-family:'Microsoft jhenghei';z-index:999");
        result_div.setAttribute("id", "resultDiv");
        var result_table = document.createElement("table");
        result_table.setAttribute("style", "width:100%;padding:10px;background-color:#555;color:#fff;border:0;line-height:14px;font-size:13px;");
        result_table.setAttribute("id", "resultTable");
        //result_table.innerHTML = "<tr><td>單元</td><td>缺席率</td><td>剩餘</td></tr>";
        var result_table_btn = document.createElement("button");
        result_table_btn.setAttribute("id", "resultTableBtn");
        result_table_btn.setAttribute("style", "width:100%;padding:10px;background-color:#555;color:#fff;border:0;line-height:14px;font-size:13px;border-top: 1px solid #fff;");

        // Check Table Exist
        if ($('table.hkvtcsp_wording').length) {
            document.body.insertBefore(btn_div, document.body.firstChild);
            document.body.insertBefore(result_div, document.body.firstChild);

            var sbjCls = document.getElementsByClassName("hkvtcsp_textInput");
            var sbj = sbjCls[0].options[sbjCls[0].selectedIndex].value;
            var sbj_name = sbjCls[0].options[sbjCls[0].selectedIndex].text;
            if (GM_getValue("sbjList") === null) {
                GM_setValue("sbjList", "[]");
            }
            //GM_deleteValue(sbj);
            //GM_deleteValue("sbjList");
            sbjChk(sbj, sbj_name);
        }

        // Check Subject Value Status
        function sbjChk(sbj, sbj_name) {
            var sbj_array;
            if (GM_getValue(sbj) === null) {
                document.getElementById("btnDiv").append(calc_btn);
                $("#btnCalc").click(function(){
                    if (GM_getValue(sbj) === null) {
                        var sbjTime = prompt("請輸入該單元的總時數");
                        if (sbjTime !== "" && sbjTime == parseInt(sbjTime, 10)) {
                            var sbj_data = '{"name":"' + sbj_name + '","time":"' + sbjTime + '","percent":"","remain":""}';
                            GM_setValue(sbj, sbj_data);
                            sbjChk(sbj,sbj_name); //update the calculated result
                        } else {
                            alert('發生錯誤');
                        }
                    } else {
                        sbj_array = JSON.parse(GM_getValue(sbj));
                        calculate(sbj, sbj_array.time);
                    }
                });
            } else {
                // Append Bottom Right
                document.getElementById("btnDiv").append(info_div);
                document.getElementById("btnDiv").append(setting_btn);
                document.getElementById("btnDiv").append(toggle_btn);
                // Append Bottom Left
                document.getElementById("resultDiv").append(result_table);
                document.getElementById("resultDiv").append(result_table_btn);
                //Hide calculate button
                $("#btnCalc").hide();

                $("#resultTableBtn").click(function () {
                    sbjListArray(sbj);
                });

                sbj_array = JSON.parse(GM_getValue(sbj));
                calculate(sbj, sbj_array.time);
                $("#btnSet").click(function () {
                    var editSbjTime = prompt("編輯該單元的總時數", sbj_array.time);
                    if (editSbjTime !== "" && editSbjTime == parseInt(editSbjTime, 10)) {
                        sbj_array.time = editSbjTime;
                        GM_setValue(sbj, JSON.stringify(sbj_array));
                        alert("編輯成功");
                        calculate(sbj, editSbjTime);
                    } else {
                        alert("發生錯誤");
                    }
                });
                $("#btnToggle").click(function () {
                    $("#infoDiv").slideToggle(500);
                    $("#resultDiv").slideToggle(500);
                    document.getElementById("btnToggle").innerHTML = document.getElementById("btnToggle").innerHTML === "+" ? "-" : "+";
                });
            }
        }
        // Check is Subject inside array, if NOT push array
        function sbjListArray(sbj) {
            document.getElementById("resultTableBtn").innerHTML = document.getElementById("resultTableBtn").innerHTML === "加入" ? "清除" : "加入";
            var list_array = JSON.parse(GM_getValue("sbjList"));
            var isExist = false;
            for (var i = 0; i < list_array.length; i++) {
                if (list_array[i] == sbj) {
                    isExist = true;
                    break;
                }
            }
            if (!isExist) {
                list_array.push(sbj);
                GM_setValue("sbjList", JSON.stringify(list_array));
            } else {
                var list_array_remove = $.grep(list_array, function (x) {
                    return x !== sbj;
                });
                GM_setValue("sbjList",JSON.stringify(list_array_remove));                
            }
            calculate(sbj, JSON.parse(GM_getValue(sbj)).time);
        }
        // Time Convert
        function time_convert(input_min) {
            var output_hr = Math.round(Math.floor(input_min / 60));
            var output_min = Math.round(input_min % 60);
            return output_min !== 0 ? output_hr + "小時" + output_min + "分鐘" : output_hr + "小時";
        }
        // Calculate Output
        function calculate(sbj, sbjTime) {
            //document.getElementById("resultTable").innerHTML = GM_getValue("sbjList") == "[]" ?  : ;
            //Get Table Data to Arrays
            var tb_array = [],
                headers = [];
            $('table.hkvtcsp_wording th').each(function (index, item) {
                headers[index] = $(item).text();
            });
            $('table.hkvtcsp_wording tr').has('td').each(function () {
                var arrayItem = {};
                $('td', $(this)).each(function (index, item) {
                    arrayItem[headers[index]] = $(item).text();
                });
                tb_array.push(arrayItem);
            });
            var att_lesson = 0,
                abs_lesson = 0,
                late_lesson = 0,
                tt_lesson_time = 0,
                tt_att_time = 0,
                tt_abs_time = 0,
                att_time, abs_time, i = 0;
            for (i = 0; i < tb_array.length; i++) {
                var lesson_time_array = tb_array[i]['課堂時間'].split("-");
                //Lesson Count
                switch (tb_array[i]['']) {
                    case 'Present':
                        att_lesson++;
                        break;
                    case 'Late':
                        late_lesson++;
                        break;
                    case 'Absent':
                        abs_lesson++;
                        break;
                }
                //ABS & ATT Caculate
                var arrived_time = tb_array[i]['出席時間'];
                var row_lesson_time_start = new Date();
                var lesson_time_array_0 = lesson_time_array[0].split(':');
                row_lesson_time_start.setHours(lesson_time_array_0[0], lesson_time_array_0[1]);
                var row_lesson_time_end = new Date();
                var lesson_time_array_1 = lesson_time_array[1].split(':');
                row_lesson_time_end.setHours(lesson_time_array_1[0], lesson_time_array_1[1]);
                var row_lesson_time_arrived = new Date();
                if (arrived_time != '-') {
                    arrived_time = arrived_time.split(':');
                    row_lesson_time_arrived.setHours(arrived_time[0], arrived_time[1]);
                    if (row_lesson_time_arrived > row_lesson_time_start.setMinutes(row_lesson_time_start.getMinutes() + 10)) {
                        row_lesson_time_start.setHours(lesson_time_array_0[0], lesson_time_array_0[1]);
                        att_time = (row_lesson_time_end - row_lesson_time_arrived) / 1000 / 60;
                        abs_time = (row_lesson_time_arrived - row_lesson_time_start) / 1000 / 60;
                    } else {
                        row_lesson_time_start.setHours(lesson_time_array_0[0], lesson_time_array_0[1]);
                        att_time = (row_lesson_time_end - row_lesson_time_start) / 1000 / 60;
                        abs_time = 0;
                    }
                } else {
                    att_time = 0;
                    abs_time = (row_lesson_time_end - row_lesson_time_start) / 1000 / 60;
                }
                tt_lesson_time = tt_lesson_time + ((row_lesson_time_end - row_lesson_time_start) / 1000 / 60);
                tt_att_time = tt_att_time + att_time;
                tt_abs_time = tt_abs_time + abs_time;
            }
            var time_remain;
            if (sbjTime * 60 * 0.3 >= tt_abs_time) {
                time_remain = (sbjTime * 60 * 0.3) - tt_abs_time;
                time_remain = time_convert(time_remain);
            } else {
                time_remain = "-/-";
            }
            var sbj_array = JSON.parse(GM_getValue(sbj));
            sbj_array.percent = (tt_abs_time / (sbjTime * 60) * 100).toFixed(2) + "%";
            sbj_array.remain = time_remain;
            GM_setValue(sbj, JSON.stringify(sbj_array));
            //console.log(JSON.stringify(sbj_array));
            var calcResult = "<p style='padding:25px;line-height:130%'><b>計算結果 :</b><br>" +
                "<br>總時數 : " + sbjTime + "小時\n" +
                "<br>已上課堂時數 : " + time_convert(tt_lesson_time) +
                "<br><br>總出席時數 : " + time_convert(tt_att_time) +
                "<br>出席率 : " + (tt_att_time / (sbjTime * 60) * 100).toFixed(2) + "%" +
                "<br><br>總缺席時數 : " + time_convert(tt_abs_time) + "\n" +
                "<br>缺席率 : " + (tt_abs_time / (sbjTime * 60) * 100).toFixed(2) + "%" +
                "<br><br>每小時缺席率 : " + (60 / (sbjTime * 60) * 100).toFixed(2) + "%" +
                "<br>剩餘可缺席時數 : " + time_remain + "</p>";
            document.getElementById("infoDiv").innerHTML = calcResult;
            calcTable(sbj);
        }
        // Calculate result table
        function calcTable(sbj){
            var isExist = false;
            var sbjList = JSON.parse(GM_getValue("sbjList"));
            if (sbjList.length > 0) {
                document.getElementById("resultTable").innerHTML = "<tr><td>單元</td><td>缺席率</td><td>剩餘</td></tr>";
                for (var j = 0; j < sbjList.length; j++) {
                    var sbjData = JSON.parse(GM_getValue(sbjList[j]));
                    if(sbj == sbjList[j]){
                        isExist = true;
                        document.getElementById("resultTable").innerHTML += "<tr><td><b><i>" + sbjData.name + "</i></b></td><td><b><i>" + sbjData.percent + "</i></b></td><td><b><i>" + sbjData.remain + "</i></b></td></tr>";
                    }else{
                        document.getElementById("resultTable").innerHTML += "<tr><td>" + sbjData.name + "</td><td>" + sbjData.percent + "</td><td>" + sbjData.remain + "</td></tr>";
                    }
                }
            }else{
                document.getElementById("resultTable").innerHTML = "<tr><td>單元</td><td>缺席率</td><td>剩餘</td></tr><tr><td colspan=\"3\" style=\"text-align:center\"><b><i>暫無資料</i></b></td></tr>";
            }
            document.getElementById("resultTableBtn").innerHTML = isExist ? "清除" : "加入";
        }
    });
})(jQuery);