@extends('layouts.master')

@section('navigation')
    @if($role==1)
        @include('local.managers.admins._navigation')
    @elseif($role==2)
        @include('local.managers.sCoords._navigation')
    @elseif($role==3)
        @include('local.managers.pCoords._navigation')
    @else
        @include('local.professors._navigation')
    @endif
@stop

@section('main')

<div class="row">
    <div class="col-md-12">
        <h3 id="course">Course: {{{ $course->code }}}{{{ $course->number }}}-{{{ $course->section }}}</h3>
        {{ HTML::linkAction('ActivitiesController@show', 'Back to Activity', array($activity->id), array('class'=>'btn btn-default btn-sm pull-right')) }}
        <h3 id="activity" data-activity-id="{{{ $activity->id }}}">Activity: {{{ $activity->name}}} </h3>


        <table class="table table-striped table-condensed">
            <thead>
                    <tr>
                    <th></th>
                    @foreach ($rubric_contents as $criterion)

                        <th class="criterion-field" data-criterion-id="{{{ $criterion->id }}}">{{ $criterion->name}}</th>

                    @endforeach
                    </tr>
                </thead>
            <tbdody>



            <!-- For each student, display names and score selects -->
            @foreach ($students as $student)
                <tr>
                    <td class="student-field" data-student-id={{ $student->id }}>{{{ $student->name }}}</td>
                    @for ($i = 0; $i<sizeof($rubric_contents); $i++)
                        <td class="score-field">
                            <select name="" id="" class="form-control">
                                <option value="0">0</option>
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>
                                <option value="6">6</option>
                                <option value="7">7</option>
                                <option value="8">8</option>
                            </select>
                        </td>
                    @endfor
                 </tr>
            @endforeach
            </tbdody>
            <tfoot>
                <tr>
                    <td><strong>Total</strong></td>
                    @for ($i = 0; $i<sizeof($rubric_contents); $i++)
                        <td class="total"><strong></strong></td>
                    @endfor
                 </tr>
            </tfoot>
        </table>

        <div class="text-center">
            <button id="button-submit-assessment" class="btn btn-primary btn-lg">Save Assessment</button>
        </div>
    </div>
</div>

@stop

@section ('javascript')


// --------------------------------------------------------------------------
// Events
// --------------------------------------------------------------------------


// Submit button is clicked
$('#button-submit-assessment').on('click', function(e)
{
    //TODO
    var expected_points = 6;
    var expected_percentage = 70;

    //Prevent page refresh
    e.preventDefault();

    // Row in the database
    var activity_id   =  $('#activity').data('activity-id');

    // Object to hold the score sum of each criterion
    var criteriaSumObject = new Object();

    // Object to hold how many students got at least the expected points
    var CriteriaAchievedCounter = new Object();

    // Object to hold all student evaluations
    var studentAssessments = new Array();

    // Iterate through all students
    $('tbody tr').each(function( index )
    {
        var ScoresObject     = new Object();        // Scores column in database
        var CriterionObject = new Object();     // Objects inside ScoresObject
        var SingleStudentAssessment = new Object();
        SingleStudentAssessment.student_id = $(this).find('.student-field').data('student-id');

        // For each criterion, store the score in array
        $(this).children('td.score-field').each(function( index )
        {
            var scoreField = $(this);
            var criterion_id = $('.criterion-field').eq(index).data('criterion-id');
            var score = scoreField.children('select').find(':selected').val();

            ScoresObject[criterion_id]=score;

            // Initialize the index for the sum object, if it's undefined
            if(typeof(criteriaSumObject[criterion_id]) == 'undefined')
            {
                criteriaSumObject[criterion_id]=0;
            }

            // Add to this criterion's total
            criteriaSumObject[criterion_id]+=parseInt(score);

            // Initialize the index for the achieved criteria count object, if it's undefined
            if(typeof(CriteriaAchievedCounter[criterion_id]) == 'undefined')
            {
                CriteriaAchievedCounter[criterion_id]=0;
            }

            // Add to the achieved criteria count for this criterion, if the
            // score reaches or exceeds the expected points
            if(score >= expected_points)
            {
                CriteriaAchievedCounter[criterion_id]+=1;
            }
        });

        SingleStudentAssessment.scores = ScoresObject;
        console.log('student object: '+JSON.stringify(SingleStudentAssessment));

        var clone = jQuery.extend({}, SingleStudentAssessment);
        studentAssessments.push(clone);


    });

    console.log('students: '+JSON.stringify(studentAssessments));
    console.log('total points per criteria: '+JSON.stringify(criteriaSumObject));
    console.log('total of students that achieved each criterion ' +JSON.stringify(CriteriaAchievedCounter));

    // Iterate through all evaluated criteria, determining which were achieved
    // by comparing the completion percentage to the expected percentage

    var CriteriaAchievedResults = new Object();
    $.each(CriteriaAchievedCounter, function( index, value )
    {
        var x = CriteriaAchievedCounter[index];
        var y = $('tbody tr').length;

        if((x/y)*100 >= expected_percentage)
        {
            CriteriaAchievedResults[index]=1;
        }
        else
        {
            CriteriaAchievedResults[index]=0;
        }
    });

    console.log('criteria results: '+JSON.stringify(CriteriaAchievedResults));

    // Save activity to the database
    $.post
    (
        "{{ URL::action('ActivitiesController@saveAssessment') }}",
        {
            activity_id: activity_id,
            criteria_achievement: JSON.stringify(CriteriaAchievedResults),
            student_scores: JSON.stringify(studentAssessments)
        },
        function(data)
        {
            location.reload();
        }
    );

});



@stop