Source: edit_shift.js

(
/**
* Contains the functionalities related to the edit shift page.
*
* @module edit_shift
* @author Joonas Konki
* @author Anu Koskela
* @author Mikko Kuhno
* @author Henrik Paananen
* @author Atte Räty
* @license BSD 3-clause, see LICENSE for more details.
* @copyright 2015 Kepler project authors
*/
function(){

/**
* Parses the ID parameter from the URL and displays the corresponding time
* slot's information.
* @memberof module:edit_shift
* @param {object} timeslots - A list of all time slots.
*/
function setCurrentWork(timeslots){
    var currentId = parseInt(kepler.getQueryParam('id'));
    for (var i = 0; i < timeslots.length; i++){
        if (timeslots[i].id === currentId){
            fillFields(timeslots[i]);
        }
    }
}

/**
* Sets the information of the time slot to the correct fields.
* @memberof module:edit_shift
* @param {object} timeslot - The selected time slot.
*/
function fillFields(timeslot){
    //TODO (Anu)var res = kepler.getReservations(timeslot.id);
    var contentArea = $("#content");
    contentArea.empty();
    var instructors = "";
    for (var i = 0; i < timeslot.supervisors.length; i++){
        if (i > 0) instructors += ",";
        instructors += " " + timeslot.supervisors[i].call_name + " " +
            timeslot.supervisors[i].last_name;
    }
    contentArea.append($("<h3>"));
    var dateS = new Date(timeslot.start_time);
     dateS += ', ' + timeslot.unit_type_name + ', ' + instructors;
    contentArea.text(kepler.dateString(dateS));

    for (var j = 0; j < timeslot.reservations.length; j++){
        var res = timeslot.reservations[j];
        contentArea.append($("<div id=experiment" + j + ">"));
        var expHeading = "<br>" + res.unit_group_name;
         expHeading += "/" + res.unit_qualifier + ", " + res.unit_name;
        $("#experiment" + j).append($("<h4>").html(espHeading));
        $("#experiment" + j).append($("<ol id=studentList" + j + ">"));
        fillStudentList(timeslot.reservations[j], j);
    }

}

/**
* Fills the studentList element with the names of the students related to the
* reservation. Creates a drop-down menu for each student, which is filled
* with all the available statuses (present, arrived late etc.).
* @memberof module:edit_shift
* @param {object} reservation - The current time slot.
* @param {number} index - The index of the time slot (not its ID).
*/
function fillStudentList(reservation, index){
    var students = reservation.user_group.members;

    for (var j = 0; j < students.length; j++){
        $("#studentList" + index).append($("<li id=student" + index + "_" +
            j + ">").html(students[j].name + " &#160"));
    }

    for(var i = 0; i<2;i++){

        var selectionStudentStatus =
            $("<select class='form-control-small' id=selectionStudent" +
            index + "_" + i + "></select>");

        var presentText = kepler.translate('content.edit_shift_present');
        var leftText = kepler.translate('content.edit_shift_left_early');
        var notpresentText = kepler.translate('content.edit_shift_not_present');
        var brokeText = kepler.translate('content.edit_shift_broke');

        selectionStudentStatus.append($("<option>").text(presentText));
        selectionStudentStatus.append($("<option>").text(leftText));
        selectionStudentStatus.append($("<option>").text(notpresentText));
        selectionStudentStatus.append($("<option>").text(brokeText));

        $("#student" + index + "_" + i).append(selectionStudentStatus);

    }
}

/**
* This is executed after the HTML page has been loaded.
* This is a common procedure of all of the client-side modules to
* initialise the page content.
* @memberof module:edit_shift
*/
function doc_ready() {
    $("#navSupervisionShifts").addClass("active");
    var currentId = parseInt(kepler.getQueryParam('id'));
    var params = {
        'time_slot_id': currentId
    };
    kepler.getTimeSlots(params, function (data) {
        var timeslots = data.time_slots;
        setCurrentWork(timeslots);
    });
    /*kepler.getTimeSlotReservations(params, function (data) {
        var reservations = data.time_slot_reservations;
        //TODO (Anu)
    });*/
}

$(document).ready(doc_ready);
}());