<?php

echo '<html>';
echo '<body>';

//Inline styles (only for printing)
echo
'<style type="text/css">
    body
    {
        font-family: "Arial", sans-serif;
        width:90%;
        margin: 0 auto;
    }
    .header-text
    {
        text-align:center;
        font-weight: bold;
        margin:0;
    }

    h1.header-text
    {
      margin: 15px 0;
    }

    table
    {
        border-collapse: collapse;
        border: 1px solid black;
        width: 100%;
        margin: 30px auto;
        font-size:1vw;
    }
    td, th
    {
        border: 1px solid black;
        padding: 5px;
    }

    .activity-name-row
    {
      background:black;
      color:white;
    }

    .activity-headers-row
    {
      background:lightgrey;
      font-weight:bold;
    }

    .report-info
    {
      margin:5px 0;
      font-size: 16px;
    }

    .criterion-field
    {
      text-align:left;
    }

    .score-field, .total, .percentage
    {
      text-align:center;
    }

    .header
    {
      margin: 30px 0;
    }

    .content
    {
      font-size: 12px;
    }

    .logo
    {
      position:absolute;
      right:0;
      top: 30px;
      width: 100px;
    }

    @media print{@page {size: landscape}}
</style>';

?>


<img class="logo" src="{{ asset('images/logo_uprrp_bw.png') }}" alt="UPRRP Logo">


<div class="header">
  <p class="header-text">University of Puerto Rico, Río Piedras Campus</p>
  <p class="header-text">Online Learning Assessment System</p>
  <p class="header-text">{{$course->program->name}} Program</p>
  <h1 class="header-text">Activity Report</h1>
</div>


<div class="content">
  <p class="report-info">Activity: {{{ $activity->name}}}</p>
  <p class="report-info">Passing Criteria: <span id="expected_percentage">{{{ $rubric->expected_percentage }}}% of students must obtain at least </span><span id="expected_points">{{{$rubric->expected_points}}}</span> points. </p>
  <p class="report-info">Course: {{{ $course->code }}} {{{ $course->number }}}-{{{ $course->section }}} - {{$course->name}}</p>
  <p class="report-info">Professor: {{{ $course->user->surnames }}}, {{{ $course->user->first_name }}} </p>
  <p class="report-info">Report Date: {{ date('M/d/Y')}}</p>
</div>


<div class="row">
    <div class="col-md-12">


        <table id="assessment-table" class="table table-striped table-condensed table-bordered">
            <thead>
                    <tr>
                        <th>Student</th>
                        @foreach ($rubric_contents as $index=>$criterion)
                            <th class="criterion-field" data-criterion-id="{{{ $criterion->criterion_id }}}">{{ $criterion->name}}</th>
                            <input type="hidden" id="weight-{{$index}}" value = "{{$criterion->weight}}">
                        @endforeach
                        <th>Student Percentage</th>
                        <th>Student % per Weight<th>
                    </tr>
            </thead>
            <tbody>

            <!-- If the activity was assessed, load the assessment.-->
            @if(sizeof($assessments)!=0)
                <!-- For each assessment -->
                @foreach ($students as $student)
                    <tr class="student-row">
                        <!-- Fetch student name -->
                        <td class="student-field" data-student-id="{{ $student->id }}">
                            {{{ $student->name }}}
                        </td>

                        <!-- For each criterion in the rubric, there's a score field -->
                        @for ($i = 0; $i<sizeof($rubric_contents); $i++)
                            <td class="score-field">
                                @if(array_key_exists($student->id, $scores_array)&& isset($scores_array[$student->id][$rubric_contents[$i]->activity_criterion_id]))
                                {{ $scores_array[$student->id][$rubric_contents[$i]->activity_criterion_id] }}
                                @else
                                N/A
                                @endif
                            </td>
                        @endfor
                        <td class="percentage"></td>
                        <td class="percentage-per-weight"></td>
                     </tr>
                @endforeach
            @endif
            </tbody>
            <tfoot>
                <tr>
                    <td>
                        <strong>Passed Criteria Percentage </strong>
                    </td>
                    @for ($i = 0; $i<sizeof($rubric_contents); $i++)
                        <td class="total"><strong><span class="total-value"></span>%</strong>
                        </td>
                    @endfor
                    <td></td>

                 </tr>
            </tfoot>
        </table>
    </div>
</div>


<?php

echo '</body>';
echo '</html>';
?>

<script src="{{ asset('vendor/jquery.min.js') }}"></script>
<script>

  $(document).ready(function(){
    // --------------------------------------------------------------------------
    // Page load
    // --------------------------------------------------------------------------

    $('.total').each(function(index)
    {
        percentagePerCriterionPlain(index+1);
    });

    $('.student-row').each(function(index)
    {
        percentagePerStudentPlain($(this));
    });

    window.print();

    // --------------------------------------------------------------------------
    // Functions
    // --------------------------------------------------------------------------

    // Calculate average of students that passed a specific criterion
    function percentagePerCriterionPlain(columnIndex)
    {
        // Object to hold the score sum of each criterion
        var sum = 0 ;
        var total = 0;

        columnIndex+=1;

        // Iterate through rows of column
        $('table tbody tr td:nth-child('+columnIndex+')').each(function( index )
        {
            var val = parseInt($(this).text());
            /* If N/A or 0 are chosen, they are ignored in the calculation.  */
            if(val % 1 === 0 && val!=0) //Check if number is integer and not 0
            {
                if(val >= parseInt($('#expected_points').text()))
                {
                    sum+=1;
                }

                total+=1;
            }
        });

        var percentage= (sum/total*100).toFixed(2);

        // If no criteria were assessed, set the percentage to 0.
        // This is to avoid show NaN%
        if(total==0)
            percentage="0.00";

        $('.total:nth-child('+columnIndex+') span').html(percentage);

    }


    // Calculate total for a specific student
    function percentagePerStudentPlain(row)
    {
        // Object to hold the score student's total score
        var sum = 0 ;
        var total = 0;
        var percentage = 0;
        var max_score = {{$rubric->max_score}};
        var per_of_weight =0;
        var sum_of_weight =0;

        row.find('td.score-field').each(function(index)
        {
            var val = parseInt($(this).text());
            if(val % 1 === 0) //Check if number is integer
            {
                sum += val;
                total+=1;
              

                per_of_weight += val * parseInt($('#weight-'+index).val()); 
            sum_of_weight += parseInt($('#weight-'+index).val());
            }
        });
        percentage_per_weight = (100 *(per_of_weight/(max_score*sum_of_weight))).toFixed(2);

        percentage =((sum/(total*max_score))*100).toFixed(2);

        //If percentage is not a number, set it to 0.
        if(isNaN(percentage))
            percentage="0.00";
        if(isNaN(percentage_per_weight))
        percentage_per_weight ="0.00";

        row.find('.percentage').html('<strong>'+percentage+'%</strong>');
        row.find('.percentage-per-weight').html('<strong>'+percentage_per_weight+'%</strong>');

    }
  });
</script>
