@extends('layouts.master')

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

@section('main')

<!-- New Activity Modal -->
<div class="modal fade" id="newActivityModal" tabindex="-1" role="dialog" aria-labelledby="newActivityModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="newActivityModalLabel">New Activity</h4>
            </div>
            <div class="modal-body">
                {{ Form::open(array('action' => array('ActivitiesController@create', $course->id))) }}
                    <div class="form-group">
                        {{ Form::label('name', 'Name') }}
                        {{ Form::text('name', Input::old('name'), array('class' => 'form-control')) }}
                    </div>
                    <div class="form-group">
                        {{ Form::label('description', 'Description') }}
                        {{ Form::textarea('description', Input::old('description'), array('class' => 'form-control', 'rows'=> 5, 'placeholder'=>'Minimum 10 characters')) }}
                    </div>
                    <div class="btn-group" role="group">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-default btn-primary">Submit</button>
                    </div>
                {{ Form::close() }}
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <p>{{$course->name}}</p>

    </div>
</div>

<div id="dummy-graph-container" class="row">
    @if(is_array($outcomes_attempted))
    <div class="col-md-12" id="graph"></div>
    @else
    <div id="overlay"><h2>No Data</h2></div>
    <div class="col-md-12" id="dummy"></div>
    @endif
</div>



<div class="row">
    <div class="col-md-7">
        <h3>Activities</h3>

        @if(!$activities->isEmpty())

            @if (in_array($course->semester->id, $active_semesters))
                <button data-toggle="modal" data-target="#newActivityModal" class="btn btn-sm btn-default pull-right"> New Activity</button>
            @endif

            <table class="table table-striped table-condensed">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Date</th>
                        <th>Updated</th>
                        <th>Assessed</th>
                        <th>Published</th>
                        <th>Diagnostic</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($activities as $activity)
                    <tr>
                        <td>{{ link_to_action('ActivitiesController@show', $activity->name, $parameters = array('id'=>$activity->id)) }}</td>
                        <td>{{ date('M d, Y', strtotime($activity->date)) }}</td>
                        <td>{{ date('M d, Y', strtotime($activity->updated_at)) }}</td>
                        <td>
                            @if($activity->o_att_array)
                            <?php Log::info($activity->id);?>
                                <span class="glyphicon glyphicon-ok"></span>
                            @endif
                        </td>
                        <td>
                            @if($activity->o_att_array && !$activity->draft && !$activity->diagnostic)
                                <span class="glyphicon glyphicon-ok"></span>
                            @endif
                        </td>
                        <td>
                        @if($activity->o_att_array && !$activity->draft && $activity->diagnostic)
                        <span class="glyphicon glyphicon-ok"></span>
                        
                    @endif
                        </td>
                    </tr>
                    @endforeach

                    @if (!in_array($course->semester->id, $active_semesters))
                    <tr>
                        <td colspan="3"><div class="alert alert-info">The semester for this course is inactive. You cannot create any more activities.</div></td>
                    </tr>
                    @endif
                </tbody>
            </table>

            <p>Unplublished activity results are <strong>not considered</strong> in the graph above.</p>

        @else
            <div class="alert alert-info">
                @if(in_array($course->semester->id, $active_semesters))
                <p>
                    No activities.
                    <a data-toggle="modal" data-target="#newActivityModal">
                        Create one.
                    </a>
                </p>
                @else
                    <p>The semester for this course is inactive. You cannot create any activities.</p>
                @endif
            </div>
        @endif
        
        <button class="btn-lg btn-primary" style="margin:5px;" data-toggle="modal" data-target="#compareActivities">Compare Activities</button>
        <div class="modal fade" id="compareActivities">
            <div class="modal-dialog modal-sm">
              <div class="modal-content">
                <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                  <h4 class="modal-title">Compare Two Activities</h4>
                </div>
                  <div class="modal-body">
                                           
          
                          <p></p>
                          
                          <h5>Select two activities to compare and present their data. <br></h5>
          
                          <div class="form-group">
          
                             <label>Activity 1</label>
                              
                              <select id='select-activity-1' data-count = "1" class="form-control selectpicker" >
                                  @foreach ($activities as $activity)
                                 @if($activity->is_assessed())
                                  <option value='{{$activity->id}}' >{{$activity->name}}</option>
                                @endif
                                  @endforeach
                              </select>
                              <hr>
                              <label>Activity 2</label>

                              <select id='select-activity-2' data-count = "1" class="form-control selectpicker" >
                                @foreach ($activities as $activity)
                                @if($activity->is_assessed())
                                <option value='{{$activity->id}}' >{{$activity->name}}</option>
                                @endif
                                @endforeach
                            </select>
                            <hr>
                          </div>
          
          
                         
                         
                          
                         
                  </div>
                <div class="modal-footer">
                      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                     <button type ="button" class= 'btn btn-primary' onclick ="goToURL()" 
                     @if(!count($activities))
                     disabled
                     @endif
                     >Compare</button>
                  {{ Form::close() }}
                </div>
              </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
          </div>
    </div>
       
    <div class="col-md-5">
        <h3>Students</h3>
        @if(!$students->isEmpty())

            <!-- If any section is assessed -->
            @if($course->outcomes_attempted!=NULL)
                {{ HTML::linkAction('CoursesController@exportGrades', 'Export grades to CSV file', array('id'=>$course->id), array('class'=>'btn btn-default btn-sm pull-right')) }}
            @endif
                {{ HTML::linkAction('CoursesController@exportGrades', 'Export grades to CSV file', array('id'=>$course->id), array('class'=>'btn btn-default btn-sm pull-right')) }}


            <table class="table table-striped table-condensed">
                <thead><tr>
                    <th></th>
                    <th>Name</th>
                    <th>Number</th>
                    <th>School</th>
                    <th>Major</th>
                </tr></thead>
                <tbody>
                @foreach ($students as $i => $student)
                    <?php $formatted_student_number = substr($student->number, 0, 3).'-'.substr($student->number, 3, -4).'-'.substr($student->number, 5, 8); ?>
                    <tr>
                        <td>{{$i+1}}</td>
                        <td><a href="{{ URL::action('StudentsController@show', array('semester'=>$course->semester_id, 'id'=>$course->code.$course->number.'-'.$course->section, 'number'=>$student->number)) }}">{{ $student->name }}</a></td>
                        <td>{{ $formatted_student_number }}</td>
                        <td>{{ $student->school_code }}</td>
                        <td>{{ $student->conc_code }}</td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        @else
            <div class="alert alert-info"><p>No students. <a href=""></a></p></div>
        @endif
    </div>
</div>
<script>
    function goToURL(){
        activity1 = $('#select-activity-1').val();
        activity2 = $('#select-activity-2').val();
        if(activity1 && activity2)
        window.open('../compare_activities/'+activity1+'/'+activity2,'_self');

    }
</script>
@stop

@section('included-js')

<!-- HighCharts -->
<script src="{{ asset('vendor/highcharts/highcharts.js') }}"></script>
<!--script src="http://code.highcharts.com/modules/exporting.js"></script -->

@stop

@section('javascript')

<?php
$student_ach = $course->student_report_for_outcome;
?>
$(function () {
    $('#graph').highcharts({
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Performance by Learning Outcome Criteria in {{ $title }}'
        },
        xAxis: {
            categories: [
                @foreach($outcomes as $outcome)

                @if(
                        is_array($student_ach)
                        && array_key_exists($outcome->id, $student_ach)
                        && $student_ach[$outcome->id]['calculations']['student_attempted']!=0)
                    <?php
                        $attempted = $student_ach[$outcome->id]['calculations']['student_attempted'];
                        
                        if( isset($student_ach[$outcome->id]['calculations']['student_achieved']))
                            $achieved =$student_ach[$outcome->id]['calculations']['student_achieved'];

                        else $achieved = 0;
                    ?>
                @else 

                    <?php 
                    
                        $attempted =0;
                        $achieved = 0;
                        
                    ?>

                @endif
            "{{{ $outcome->name }}} <br> (N = {{$attempted}} , {{$achieved}} ) ",

                

                    

                @endforeach
            ],
            labels: {
                style: {
                    fontSize:'11px'
                },
                step:1,
                useHTML:true,
                formatter: function() {
                    return '<div style="width:200px; word-break:break; text-overflow:ellipsis; overflow:hidden;">'+this.value+'</div>';
                },
            }
        },
        yAxis: {
            min: 0,
            max: 100,
            title: {
                text: 'Percentage'
            },
            plotLines:[{
                value:66.67,
                color: '#000',
                width:3,
                zIndex:4,
                label:{
                    text: 'Goal (66.67%)',
                    style: {
                        color: '#000',
                        fontSize: '14px',
                    }
        
                }
            }]
        },
        tooltip: {
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b>{point.y:.2f}</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        plotOptions: {
            bar: {
                //grouping: false,
                shadow: false,
                borderWidth: 0,
            },
            series: {
            pointPadding: 0,
            groupPadding: 0.075
            },
        },
        series: [{
            name: 'Obtained Value',
            color: '#e70033',
            dataLabels: {
                enabled: true,
                fontSize: 8,
                color: '#fff',
                align: 'right',
                format: '{y:.1f}%',
                style: {
                    //fontWeight: 'bold'
                },
                y:-1
            },
            data:[
                @foreach($outcomes as $index => $outcome)
                    @if(
                        is_array($student_ach)
                        && array_key_exists($outcome->id, $student_ach)
                        && $student_ach[$outcome->id]['calculations']['student_attempted']!=0
                        && $student_ach[$outcome->id]['calculations']['student_achieved'])


                        {{{ ($student_ach[$outcome->id]['calculations']['student_achieved']/$student_ach[$outcome->id]['calculations']['student_attempted'])*100 }}},
                    @else
                        0,
                    @endif
                @endforeach

            ],
            pointPadding: 0,

        }]

    });

    // Include dummy graph for outcomes
    @include('global.dummy-outcomes')
});

@stop

