Your IP : 216.73.216.91


Current Path : /var/www/html/llcfapp/public/http/
Upload File :
Current File : /var/www/html/llcfapp/public/http/attend.js

// $(document).ready(function(){
//     $('body').css('zoom','90%'); /* Webkit browsers */
//     $('body').css('zoom','0.9'); /* Other non-webkit browsers */
//     if ($ && $.browser && $.browser.mozilla){
//         $('body').css('-moz-transform',scale(0.9, 0.9)); /* Moz-browsers */
//     }
// });

var form_props = getFormProps("att");
var form_props_stu = getFormProps("stu");
function daysInThisMonth(date) {
var d = new Date(date);
    return new Date(d.getFullYear(), d.getMonth()+1, 0).getDate();
}
function getDaysInMonthExcludeWeekend(month, year) {
    month--; // lets fix the month once, here and be done with it
    var date = new Date(year, month, 1);
    var days = [];
    var day_names = {0: "Sun", 1: "Mon", 2: "Tue", 3: "Wed", 4: "Thu", 5: "Fri", 6: "Sat" };
    while (date.getMonth() === month) {

        // Exclude weekends
        var tmpDate = new Date(date);            
        var weekDay = tmpDate.getDay(); // week day
        var day = tmpDate.getDate(); // day

        if (weekDay%6) { // exclude 0=Sunday and 6=Saturday
            days.push(`${day}-${day_names[weekDay]}`);
        }

        date.setDate(date.getDate() + 1);
    }
    return days;
}  
// alert(daysInThisMonth(new Date()));
// alert(daysInThisMonth('2023-06-01'));

//#region Get students by class
url = getConfig("baseUrl") + `resource=${form_props_stu.resource}&action=${form_props_stu.getStudentsByClass}&class=1`;
// $.toast({heading: "Information", text: "Fetching student's data...", showHideTransition: "fade", icon: "info", hideAfter: 3000});
    $.post(url, [], (res) => {
      var json = JSON.parse(res);
      // console.log(json);
      if (json.status == "success") {
        generateAttendanceView(json['result'], json['rowsAffected']);

        // $.toast({heading: "Success", text: "Fetched all donors successfully", showHideTransition: "slide", icon: "success", hideAfter: getConfig("toastHideAfter")});
      } else if (json.status == "failure") {
        $.toast({heading: "Information", text: json.msg, showHideTransition: "slide", icon: "info", hideAfter: getConfig("toastHideAfter")});
      } else {
        $.toast({heading: "Error", text: "Some thing went wrong", showHideTransition: "fade", icon: "error", hideAfter: getConfig("toastHideAfter")});
      }
    }).done(function () {
    //   if (window.location.href.indexOf(form_props.input_gr_num.replace("#", "")) > -1) {
    //     // getDataByGrNum();
    //   } else {
    //     // $(form_props.submit_btn).attr("disabled", false);
    //   }
    });
//#endregion

function generateAttendanceView(students, stu_count){
    var d = new Date();
    var working_days = getDaysInMonthExcludeWeekend(d.getMonth()+1, d.getFullYear());
    var headRow = `<th class="theader" scope="col">Student Name</th>`;
    for(var i = 0; i < working_days.length; i++){
        headRow += `<th class="theader" scope="col">${working_days[i].split('-')[0]}<br>${working_days[i].split('-')[1]}</th>`;
    }
    headRow += `<th class="bg-danger theader">Days Absent</th><th class="bg-success">Days Present</th>`;
    var studentRow = '';
    for(var j = 0; j < stu_count; j++){
        studentRow += `<tr class="student">`;
        studentRow += `<td class="name-col">${students[j]['stu_full_name']}</td>`;    
        for(var k = 0; k < working_days.length; k++){
            studentRow += `<td class="attend-col"><input type="checkbox" class="big-checkbox"></td>`;
        }
        studentRow += `<td class="missed-col">0</td><td class="success-content">8</td>`;
        studentRow += `</tr>`;
    }
    $('#head-row').html(headRow);
    $('#student-rows').html(studentRow);
}

function getRandom() {
    return (Math.random() >= 0.5);
}
(function() {
    if (!localStorage.attendance) {
        console.log('Creating attendance records...');
        

        var nameColumns = $('tbody .name-col'),
        attendance = {};

        nameColumns.each(function() {
            var name = this.innerText;
            attendance[name] = [];

            for (var i = 0; i <= num_of_days; i++) {
                attendance[name].push(getRandom());
            }
        });

        localStorage.attendance = JSON.stringify(attendance);
    }
}());


/* STUDENT APPLICATION */
$(function() {
  var attendance = JSON.parse(localStorage.attendance),
      $allMissed = $('tbody .missed-col'),
      $allCheckboxes = $('tbody input');

  // Count a student's missed days
  function countMissing() {
      $allMissed.each(function() {
          var studentRow = $(this).parent('tr'),
              dayChecks = $(studentRow).children('td').children('input'),
              numMissed = 0;

          dayChecks.each(function() {
              if (!$(this).prop('checked')) {
                  numMissed++;
              }
          });

          $(this).text(numMissed);
      });
  }

  // Check boxes, based on attendace records
  $.each(attendance, function(name, days) {
      var studentRow = $('tbody .name-col:contains("' + name + '")').parent('tr'),
          dayChecks = $(studentRow).children('.attend-col').children('input');

      dayChecks.each(function(i) {
          $(this).prop('checked', days[i]);
      });
  });

  // When a checkbox is clicked, update localStorage
  $allCheckboxes.on('click', function() {
      var studentRows = $('tbody .student'),
          newAttendance = {};

      studentRows.each(function() {
          var name = $(this).children('.name-col').text(),
              $allCheckboxes = $(this).children('td').children('input');

          newAttendance[name] = [];

          $allCheckboxes.each(function() {
              newAttendance[name].push($(this).prop('checked'));
          });
      });

      countMissing();
      localStorage.attendance = JSON.stringify(newAttendance);
  });

  countMissing();
}());