Greasy Fork is available in English.

IVE出席率及缺席率計算器

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

2017/09/29時点のページです。最新版はこちら。

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
/*
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.1.2
// @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        unsafeWindow
// ==/UserScript==
(function() {
    'use strict';
    // Create Elements
    $(function() {
        var btn_div = document.createElement("div");
        btn_div.setAttribute("style","width:15vw;position:absolute;right:0;bottom:0;border:0;font-family:'Microsoft jhenghei';");
        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 = "-";

        // Check Table Exist
        if($('table.hkvtcsp_wording').length){
            document.body.insertBefore(btn_div, document.body.firstChild);
            var sbjCls = document.getElementsByClassName("hkvtcsp_textInput");
            var sbj = sbjCls[0].options[sbjCls[0].selectedIndex].text;
            sbjChk(sbj);
            console.log("Table Exist");
        }
        // Check Subject Value Status
        function sbjChk(sbj){
            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)){
                            GM_setValue(sbj, sbjTime);
                            sbjChk(sbj);
                        }else{
                            alert('發生錯誤');
                        }
                    }else{
                        calculate(GM_getValue(sbj));
                    }
                });
            }else{
                if($("#btnCalc").length!==0){
                    $("#btnCalc").hide();
                }
                document.getElementById("btnDiv").append(info_div);
                document.getElementById("btnDiv").append(setting_btn);
                document.getElementById("btnDiv").append(toggle_btn);
                calculate(GM_getValue(sbj));
                $("#btnSet").click(function(){
                    var editSbjTime = prompt("編輯該單元的總時數",GM_getValue(sbj));
                    if(editSbjTime !== "" && editSbjTime == parseInt(editSbjTime, 10)){
                        GM_setValue(sbj, editSbjTime);
                        alert("編輯成功");
                        calculate(editSbjTime);
                    }else{
                        alert("發生錯誤");
                    }
                });
                $("#btnToggle").click(function(){
                    $("#infoDiv").slideToggle(500);
                    document.getElementById("btnToggle").innerHTML = document.getElementById("btnToggle").innerHTML=="+" ? "-" : "+";
                });
            }
        }
        // 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(sbjTime) {
            //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 = "-/-";
            }
            document.getElementById("infoDiv").innerHTML = "<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>";
            console.log(document.getElementById("infoDiv").innerText);
        }
    });
})(jQuery);