IVE出席率及缺席率計算器

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

Från och med 2018-01-21. Se den senaste versionen.

/*
 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      2.0.3
// @description  幫助IVE學生計算出席率及缺席率
// @author       Miklet
// @license      MIT
// @match        *://myportal.vtc.edu.hk/*
// @require      https://code.jquery.com/jquery-latest.js
// @require      https://greasyforks.org/scripts/33537-grant-none-shim-js/code/grant-none-shimjs.js?version=220694
// @run-at       document-start
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_listValues
// @grant        GM_registerMenuCommand
// @grant        unsafeWindow
// @grant        GM_setClipboard
// @supportURL   https://greasyforks.org/zh-TW/scripts/32663-ive出席率及缺席率計算器
// ==/UserScript==
(function () {
    'use strict';
    var version = "2.0.3";
    var calc_btn, sbj_info_div, sbj_info, sbj_edit_time_btn, toggle_btn, sbj_dashboard_div, sbj_dashboard, sbj_select_btn, sbj_id, sbj_name;
    var sbj_datas = JSON.parse(GM_getValue("sbj_datas"));
    var sbj_dashboard_datas = JSON.parse(GM_getValue("sbj_dashboard"));
    //Start all necessary function
    function initialize() {
        updateCheck();
        addToolbarCommand();
        createElement();
    }
    //Update global variable
    function updateVar(){
        sbj_datas = JSON.parse(GM_getValue("sbj_datas"));
        sbj_dashboard_datas = JSON.parse(GM_getValue("sbj_dashboard"));
    }
    //Check if the calc programme updated or not
    function updateCheck() {
        if (GM_getValue("calc_version") === null || GM_getValue("calc_version") !== version) {
            GM_setValue("calc_version", version);
            //deleteAll();
            //setDefault();
            alert("IVE出缺率計算器已更新到版本 " + version + " , 這次更新『沒有』重設所有資料。\n\n" + version + " 更新: \n\t - 增加了數據導入導出功能");
        }
    }
    //Set default value of all saved variable
    function setDefault() {
        GM_setValue("sbj_dashboard", "{}");
        GM_setValue("sbj_datas", "{}");
        GM_setValue("calc_version", version);
    }
    //Reset all saved data, then initialize all value
    function deleteAll() {
        var listValues = GM_listValues();
        for (var x in listValues) {
            GM_deleteValue(listValues[x]);
        }
    }
    //Add two button to the Tampermonkey menu
    function addToolbarCommand() {
        GM_registerMenuCommand('重設報告表', function () {
            GM_setValue("sbj_dashboard", "{}");
            calcDashboard();
        });
        GM_registerMenuCommand('重設全部', function () {
            if (confirm("確認重設全部資料?")) {
                setDefault();
                alert("重設完成!請刷新頁面!");
            }
        });
        GM_registerMenuCommand('導出數據', function () {
            exportDatas();
        });
        GM_registerMenuCommand('導入數據', function () {
            importDatas();
        });
    }
    //Create element
    function createElement() {
        //calc button if no total hours set
        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", "calcBtn");
        calc_btn.innerHTML = "計算出缺率";

        //Info, edit time button, and toggle button
        sbj_info_div = document.createElement("div");
        sbj_info_div.setAttribute("style", "width:15vw;position:fixed;right:0;bottom:0;border:0;font-family:'Microsoft jhenghei';z-index:999");
        sbj_info_div.setAttribute("id", "sbjDiv");
        sbj_info = document.createElement("p");
        sbj_info.setAttribute("style", "margin:0;width:100%;background-color:#555;color:#fff;border:0;border-bottom:1px solid #fff;line-height:14px;font-size:13px;");
        sbj_info.setAttribute("id", "sbjInfo");
        sbj_edit_time_btn = document.createElement("button");
        sbj_edit_time_btn.setAttribute("style", "width:85%;padding:10px;background-color:#555;color:#fff;border:0;line-height:14px;font-size:13px;");
        sbj_edit_time_btn.setAttribute("id", "editTimeBtn");
        sbj_edit_time_btn.innerHTML = "編輯總時數";
        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 = "-";

        //Dashboard
        sbj_dashboard_div = document.createElement("div");
        sbj_dashboard_div.setAttribute("style", "min-width:24vw;position:fixed;left:0;bottom:0;border:0;font-family:'Microsoft jhenghei';z-index:999");
        sbj_dashboard_div.setAttribute("id", "sbjDashboardDiv");
        sbj_dashboard = document.createElement("table");
        sbj_dashboard.setAttribute("style", "width:100%;padding:10px;background-color:#555;color:#fff;border:0;line-height:14px;font-size:13px;");
        sbj_dashboard.setAttribute("id", "sbjDashboard");
        sbj_select_btn = document.createElement("button");
        sbj_select_btn.setAttribute("id", "sbjSelectBtn");
        sbj_select_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;");
    }
    //Update the subject hours
    function sbjHoursUpdate_Click() {
        $("#editTimeBtn").click(function () {
            var sbj_hours = window.prompt("編輯該單元的總時數", sbj_datas[sbj_id]["hours"]);
            console.log(sbj_hours);
            if (sbj_hours !== "" && sbj_hours == parseInt(sbj_hours, 10)) {
                sbj_datas[sbj_id]["hours"] = sbj_hours;
                GM_setValue("sbj_datas", JSON.stringify(sbj_datas));
                sbj_chk();
            } else {
                alert('發生錯誤');
            }
        });
    }
    //Ask the user to input the total hours of this subject
    function sbjInputHours_Click() {
        $("#calcBtn").click(function () {
            var sbj_hours = window.prompt("請輸入該單元的總時數");
            if (sbj_hours !== "" && sbj_hours == parseInt(sbj_hours, 10)) {
                sbjDatasInsert(sbj_hours);
                sbj_chk();
            } else {
                alert('發生錯誤');
            }
        });
    }
    //Function for toggle button
    function calcToggle() {
        $("#btnToggle").click(function () {
            $("#sbjInfo").slideToggle(500);
            $("#sbjDashboardDiv").slideToggle(500);
            document.getElementById("btnToggle").innerHTML = document.getElementById("btnToggle").innerHTML === "+" ? "-" : "+";
        });
    }
    //Insert an array to sbj_datas
    function sbjDatasInsert(sbj_hours) {
        sbj_datas[sbj_id] = {
            id: sbj_id,
            name: sbj_name,
            hours: sbj_hours,
            att: 0,
            abs: 0,
            remain: 0
        };
        GM_setValue("sbj_datas", JSON.stringify(sbj_datas));
    }
    //!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT FUNCTION !!!!!!!!!!!!!!!!!!!!!!!!!!!//
    //!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT FUNCTION !!!!!!!!!!!!!!!!!!!!!!!!!!!//
    //!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT FUNCTION !!!!!!!!!!!!!!!!!!!!!!!!!!!//
    //Check if the subject have record or not, if yes then append and calculate the result
    function sbj_chk() {
        if (sbj_datas[sbj_id]) {
            $("#calcBtn").remove();
            document.getElementById("sbjDiv").append(sbj_info);
            document.getElementById("sbjDiv").append(sbj_edit_time_btn);
            document.getElementById("sbjDiv").append(toggle_btn);
            document.getElementById("sbjDashboardDiv").append(sbj_dashboard);
            document.getElementById("sbjDashboardDiv").append(sbj_select_btn);
            sbjHoursUpdate_Click();
            sbjDashboardArray_Click();
            calcToggle();
            sbj_calc();
        } else {
            document.getElementById("sbjDiv").append(calc_btn);
            sbjInputHours_Click();
        }
    }
    //!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT FUNCTION !!!!!!!!!!!!!!!!!!!!!!!!!!!//
    //!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT FUNCTION !!!!!!!!!!!!!!!!!!!!!!!!!!!//
    //!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT FUNCTION !!!!!!!!!!!!!!!!!!!!!!!!!!!//
    // 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 the result
    function sbj_calc() {
        //Get Table Data to Arrays
        var tb_array = [],
            headers = [],
            sbj_hours = sbj_datas[sbj_id]["hours"];
        $('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 (sbj_hours * 60 * 0.3 >= tt_abs_time) {
            time_remain = (sbj_hours * 60 * 0.3) - tt_abs_time;
            time_remain = time_convert(time_remain);
        } else {
            time_remain = "-/-";
        }
        //var sbj_array = JSON.parse(GM_getValue(sbj));
        sbj_datas[sbj_id]["att"] = (tt_att_time / (sbj_hours * 60) * 100).toFixed(2) + "%";
        sbj_datas[sbj_id]["abs"] = (tt_abs_time / (sbj_hours * 60) * 100).toFixed(2) + "%";
        sbj_datas[sbj_id]["remain"] = time_remain;
        GM_setValue("sbj_datas", JSON.stringify(sbj_datas));
        var calcResult = "<p style='padding:18px 25px;margin:0;line-height:130%'><b>計算結果 :</b><br>" +
            "<br>總時數 : " + sbj_hours + "小時\n" +
            "<br>已上課堂時數 : " + time_convert(tt_lesson_time) +
            "<br><br>總出席時數 : " + time_convert(tt_att_time) +
            "<br>出席率 : " + (tt_att_time / (sbj_hours * 60) * 100).toFixed(2) + "%" +
            "<br><br>總缺席時數 : " + time_convert(tt_abs_time) + "\n" +
            "<br>缺席率 : " + (tt_abs_time / (sbj_hours * 60) * 100).toFixed(2) + "%" +
            "<br><br>每小時缺席率 : " + (60 / (sbj_hours * 60) * 100).toFixed(2) + "%" +
            "<br>剩餘可缺席時數 : " + time_remain + "</p>" +
            "<p style=\"text-align:right;font-size:8px;margin:0;color:#777\">version " + version + "</p>";
        document.getElementById("sbjInfo").innerHTML = calcResult;
        sbjDashboard();
    }
    // Check is Subject inside array, if NOT push array
    function sbjDashboardArray_Click() {
        $("#sbjSelectBtn").click(function () {
            document.getElementById("sbjSelectBtn").innerHTML = document.getElementById("sbjSelectBtn").innerHTML === "加入" ? "清除" : "加入";
            if (sbj_dashboard_datas[sbj_id]) {
                delete sbj_dashboard_datas[sbj_id];
            } else {
                sbj_dashboard_datas[sbj_id] = sbj_id;
            }
            GM_setValue("sbj_dashboard", JSON.stringify(sbj_dashboard_datas));
            sbjDashboard();
        });
    }
    //Out put the dashboard record
    function sbjDashboard() {
        var isExist = false;
        if (!(jQuery.isEmptyObject(sbj_dashboard_datas))) {
            document.getElementById("sbjDashboard").innerHTML = "<tr><td>單元</td><td>出席率</td><td>缺席率</td><td>剩餘</td></tr>";
            for (var id in sbj_dashboard_datas) {
                if (sbj_id === sbj_dashboard_datas[id]) {
                    isExist = true;
                    document.getElementById("sbjDashboard").innerHTML += "<tr><td><b><i>" + sbj_datas[id]["name"] + "</i></b></td><td><b><i>" + sbj_datas[id]["att"] + "</i></b></td><td><b><i>" + sbj_datas[id]["abs"] + "</i></b></td><td><b><i>" + sbj_datas[id]["remain"] + "</i></b></td></tr>";
                } else {
                    document.getElementById("sbjDashboard").innerHTML += "<tr><td>" + sbj_datas[id]["name"] + "</td><td>" + sbj_datas[id]["att"] + "</i></b></td><td><b><i>" + sbj_datas[id]["abs"] + "</td><td>" + sbj_datas[id]["remain"] + "</td></tr>";
                }
            }
        } else {
            document.getElementById("sbjDashboard").innerHTML = "<tr><td>單元</td><td>出席率</td><td>缺席率</td><td>剩餘</td></tr><tr><td colspan=\"4\" style=\"text-align:center\"><b><i>暫無資料</i></b></td></tr>";
        }
        document.getElementById("sbjSelectBtn").innerHTML = isExist ? "清除" : "加入";
    }
    //Function for export
    function exportDatas() {
        GM_setClipboard(JSON.stringify({
            sbj: sbj_datas,
            dashboard: sbj_dashboard_datas
        }));
        window.prompt("數據已經復製到您的剪貼板。如果不成功,請將以下數據複製到並貼到導入文本框。", JSON.stringify({
            sbj: sbj_datas,
            dashboard: sbj_dashboard_datas
        }));
    }
    //Function for import
    function importDatas() {
        var import_datas = window.prompt("導入數據:");
        try {
            import_datas = JSON.parse(import_datas);
            if (window.confirm("這會覆蓋當前數據,繼續?")) {
                console.log(import_datas);
                GM_setValue("sbj_datas", JSON.stringify(import_datas["sbj"]));
                GM_setValue("sbj_dashboard", JSON.stringify(import_datas["dashboard"]));
                updateVar();
                sbj_chk();
            } else {
                window.alert("導入取消!");
            }
        } catch (error) {
            window.alert("發生錯誤! " + error);
        }
    }
    //Document ready function 
    $(function () {
        initialize();
        if ($('table.hkvtcsp_wording').length) {
            document.body.insertBefore(sbj_info_div, document.body.firstChild);
            document.body.insertBefore(sbj_dashboard_div, document.body.firstChild);
            var sbjCls = document.getElementsByClassName("hkvtcsp_textInput");
            sbj_id = sbjCls[0].options[sbjCls[0].selectedIndex].value;
            sbj_name = sbjCls[0].options[sbjCls[0].selectedIndex].text;
            sbj_chk();
        }
    });
})(jQuery);
长期地址
遇到问题?请前往 GitHub 提 Issues。