Al_Caughey / Student Checklist

// ==UserScript==
// @name         Student Checklist
// @namespace    http://tampermonkey.net/
// @version      0.9
// @description  tweaks the SIDs student checklist page with a larger first row and blank last row
// @author       allan.caughey@ocdsb.ca
// @match        https://staffapps.ocdsb.ca/sid/ClassCheckList.aspx*
// @grant        none
// @require http://code.jquery.com/jquery-3.3.1.min.js
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    var $ = window.jQuery;
    $(`<style type='text/css'>
       #p-date-picker { border: 2px solid #999; border-radius: 20px; box-shadow: 3px 4px #ccc; padding: 10px 26px; width: fit-content; }
       .thr th{ text-transform: capitalize; }
       #prev-week, #next-week{ display:none;}
       .cohort-x{ background: red!important; }
       [data-ctw="true"] .cohort-a .cohort-a{ background: #F9B233!important; color:#FFF; }
       [data-ctw="true"] .cohort-b .cohort-b{ background: #29D3E3!important; color:#FFF; }
       [data-ctw="true"] .cohort-a .cohort-a:after,[data-ctw="true"] .cohort-b .cohort-b:after{ content: "(in-person)"; display: block; padding-top:8px; }
       [data-ctw="true"] .cohort-a .cohort-b:after,[data-ctw="true"] .cohort-b .cohort-a:after{ content: "(at-home)"; display: block; padding-top:8px; }
       #not-this-week{ background-color:yellow; font-weight: bold; margin-left:20px; padding: 2px 12px;}
       [data-ctw="true"] #not-this-week{ display:none; }
       [data-week="2020-11-16"][data-timeslot="QA1"] th:nth-child(5):after, [data-week="2020-11-16"][data-timeslot="QA1"] th:nth-child(6):after, [data-week="2020-11-16"][data-timeslot="QA1"] th:nth-child(7):after { display: none; }
       [data-week="2020-11-16"][data-timeslot="QA1"] th:nth-child(5), [data-week="2020-11-16"][data-timeslot="QA1"] th:nth-child(6), [data-week="2020-11-16"][data-timeslot="QA1"] th:nth-child(7) { background-color: #ccc!important; color: #ccc!important; }
       [data-week="2020-11-16"][data-timeslot="QB1"] th:nth-child(3):after, [data-week="2020-11-16"][data-timeslot="QB1"] th:nth-child(4):after { display: none; }
       [data-week="2020-11-16"][data-timeslot="QB1"] th:nth-child(3), [data-week="2020-11-16"][data-timeslot="QB1"] th:nth-child(4) { background-color: #ccc!important; color: #ccc!important; }
       td.cohort-x {background: linear-gradient(to top right, white 48%,#333, white 52%)!important;}
       .end-of-table td {background: #EEE!important;}

     @media print {
         #p-date-picker {
           display: none;
         }
       }
</style>`).appendTo("head");

    // old: let cohortExceptions=["Adams,Maya", "Wilson,Finn", "Earle,Talia", "Baxter,Sienna"]
    // updated for QM1
    // let cohortExceptions=[ "Adams,Maya", "Baxter,Sienna", "Earle,Talia", "Martel,Jayden", "McCallum,Rory", "Newell,Trent", "O'Brien,Alice", "Wilson,Finn" ]
    //updated for Fall 2021
    let cohortExceptions=[ ]

    let mondays={
        'sep 6' : {s:'x,x,x,1,1'},
        'oct 11' : {s:'x,1,1,1,1'},
        'nov 8' : {s:'1,1,1,1,x'},
        'dec 20' : {s:'x,x,x,x,x'},
        'dec 27' : {s:'x,x,x,x,x'},
        'feb 14' : {s:'1,1,1,1,x'},
        'feb 21' : {s:'x,1,1,1,1'},
        'mar 14' : {s:'x,x,x,x,x'},
        'apr 11' : {s:'1,1,1,1,x'},
        'apr 18' : {s:'x,1,1,1,1'},
        'may 23' : {s:'x,1,1,1,1'},
        'jun 27' : {s:'1,1,x,x,x'},

    }

    let timeslot = $('#lblInfo').text().indexOf('QA1')>-1 ? 'QA1' : 'QB1'
    function twoD( v ){
        return ('0'+Number(v)).slice(-2)
    }

    function getMonday(d) {
        let da=d.split('-')
        //console.log('getMonday - da', da)
        let td = new Date( da[0], da[1]-1, da[2] );
        var day = td.getDay(),
            diff = td.getDate() - day + (day == 0 ? 1:1); // adjust when day is sunday
        td.setDate(diff)
        let nd = td.getFullYear()+'-'+twoD(td.getMonth()*1+1)+'-'+twoD(td.getDate())
        //console.log('getMonday', d, td, nd)
        $('#date-picker').val( nd )

        return new Date( td );
    }

    $('<p>').attr('id', 'p-date-picker').insertBefore($('#grvClassCheckList'))
    $('<button>').attr('id', 'prev-week').attr( 'title', 'Jump two weeks back in the calendar ').text('<').appendTo($('#p-date-picker'))
    $('<input>').attr('id', 'date-picker').attr( 'type', 'date').appendTo($('#p-date-picker'))
    $('<button>').attr('id', 'next-week').attr( 'title', 'Jump two weeks ahead in the calendar').text('>').appendTo($('#p-date-picker'))
    $('<span>').attr('id', 'not-this-week').text('Not this week!').appendTo($('#p-date-picker'))

    $('tr').last().clone().addClass('end-of-table').appendTo($('table'))
    $('tr:last td').text('').css({"height":"64px","border-top":"3px double"})
    $('tr:odd').css({"background-color":"#eeee"})
    $('tr:last td').text('').css({"height":"64px","border-top":"3px double","background-color":"#ccc"})
    $('tr:first').css({"height":"96px","border-bottom":"3px double"})
    $('tr:first-of-type').children().css({"background-color":"#ccc"})

    //break into cohorts
    let found = false
    /*
    $( 'tr' ).each( function( a, b ){
        if ( found ) return
        let cn = $( b ).find( 'td' )[ 1 ]
        if ( $( cn ).text().toUpperCase() < 'L' ){
            return
        }
        $( 'tr' ).first().clone().addClass( 'start-of-cohort-b' ).insertBefore( $( 'table' ).find( 'tr' )[ a ])
        $( 'tr' ).last().clone().removeClass( 'end-of-table' ).addClass( 'end-of-cohort-a' ).insertBefore( $( 'table').find( 'tr' )[ a ] )
        found = true
    })
    */
    let thcn = $( 'th:contains("Name")' )
    thcn.parents( 'tr' ).addClass( 'thr' )
    //$( '.thr' ).first().addClass( 'cohort-a' )
    //$( '.start-of-cohort-b' ).last().addClass( 'cohort-b' )
    //thcn.first().text( 'Cohort A' ).addClass( 'thr' )
    //thcn.last().text( 'Cohort B' ).addClass( 'thr' )

    $('tr').each(function(a,b){
        if( $(b).hasClass('thr') ){
            //console.log( 'skip thr' )
            return
        }
        let cn = $( b ).find( 'td' )[ 1 ], sn = $( cn ).text()
        //console.log( 'sn', sn )
        if( sn.toUpperCase() < 'L' ){
            // $(b).addClass('cohort-a')
        }
        else{
            // $(b).addClass('cohort-b')
        }
        if( cohortExceptions.includes( sn ) ){
            console.log( 'exception', sn )
            if( sn.toUpperCase() < 'L' ){
                $(b).addClass('cohort-a')
                //console.log('Append ' + sn + ' to Cohort B')
                $(b).detach().removeClass('cohort-b').addClass('cohort-a').insertAfter($('.start-of-cohort-b'))
            }
            else{
                //console.log('Append ' + sn + ' to Cohort A')
                $(b).detach().removeClass('cohort-a').addClass('cohort-b').insertBefore($('.end-of-cohort-a'))
            }
        }
    })

    let dow=[ 'Sun' , 'Mon' , 'Tues' , 'Wed' , 'Thu' , 'Fri' , 'Sat' ]
    let moy=[ 'jan' , 'feb' , 'mar' , 'apr' , 'may' , 'jun' , 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' ]
    $('#date-picker').change(function() {
        $('th').removeClass('cohort-a cohort-b')
        $('.cohort-x').removeClass('cohort-x')
        $('#prev-week, #next-week').fadeIn('slow')
        let cd=$('#date-picker').val()
        let mocw = getMonday( cd )
        $('#grvClassCheckList').attr('data-week', cd )
        let dn=mocw.getDate(), mn=mocw.getMonth()
        let period = (!!mondays[ moy[ mn ] + ' ' + dn ] ? mondays[ moy[ mn ] + ' ' + dn ].p : '')
        let classes = (!!mondays[ moy[ mn ] + ' ' + dn ] ? mondays[ moy[ mn ] + ' ' + dn ].s : '').split(',')

        $('#grvClassCheckList').attr('data-timeslot', timeslot )
        $('body').attr('data-ctw', period == timeslot || cd == '2020-11-16' )
        let chrc = $('#grvClassCheckList th')
        //console.log( 'chrc', chrc )
        for( let x=0; x<5; x++ ){
            let dn=mocw.getDate(), mn=mocw.getMonth()
            $( chrc[ x+2 ] ).html( dow[ x+1 ] + '<br/>' + moy[ mn ] + ' ' + dn ).addClass('cohort-'+classes[ x ] )
            $( chrc[ x+9 ] ).html( dow[ x+1 ] + '<br/>' + moy[ mn ] + ' ' + dn ).addClass('cohort-'+classes[ x ] )
            mocw.setDate( mocw.getDate() + 1 )
        }
        for(let n=2;n<7;n++){
            $('tr td:nth-child('+(n+1)+')').addClass($($('.thr th')[n]).attr('class'))
        }
    } )

    $('#next-week').click(function() {
        let da=$( '#date-picker' ).val().split( '-' )
        let nv = new Date( da[0], da[1]-1, da[2]*1 + 7 )
        let nd = nv.getFullYear() + '-' + twoD( nv.getMonth()+1 ) + '-' + twoD( nv.getDate() )
        $('#date-picker').val( nd ).change()
        return false
    })
    $('#prev-week').click(function() {
        let da=$('#date-picker').val().split( '-' )
        let nv = new Date( da[0], da[1]-1, da[2]*1 - 7 )
        let nd = nv.getFullYear() + '-' + twoD( nv.getMonth()+1 ) + '-' + twoD( nv.getDate() )
        $('#date-picker').val( nd ).change()
        return false
    })

})();