@extends('layouts.master')

@section('navigation')
    @include('local.managers.admins._new_navigation')
@stop

@section('main')
    <div class="row">
        <div class="col-md-12">

            <p>This report contains performance information for all Schools and Programs with assessed courses during the
                following semester(s):</p>
            <ul>
                @foreach (Session::get('semesters_info') as $semester_info)
                    <li>{{ $semester_info }}</li>
                @endforeach
            </ul>

            <p class="hidden-print">Unlinked programs did not assess this outcome.</p>


            <!-- <h3>Table of Contents</h3> -->
            <!-- <ol id="table-of-contents" class="upper-roman">

                        </ol> -->


            <h3 id="{{ $outcome->id }}" class="outcome">{{ $outcome->name }}</h3>
            <table class="table table-condensed table-bordered datatable">
                <thead>
                    <tr class="center-text">
                        <th>School or College</th>
                        <th>Academic Program</th>
                        <th>Assesses Learning Outcome</th>
                        <th>Findings</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr class="column-search">
                        <th><select class="column-search-select form-control">
                                <option value=""></option>
                            </select></th>
                        <th><select class="column-search-select form-control">
                                <option value=""></option>
                            </select></th>
                        <th><select class="column-search-select form-control">
                                <option value=""></option>
                            </select></th>
                        <th><input class="column-search-bar form-control" type="text" placeholder="Buscar" /></th>
                    </tr>
                </tfoot>
                <tbody>
                    @foreach ($schools as $school)
                        @foreach ($school->programs as $program)
                            @if ($program->assessesOutcome($outcome->id))
                                <tr>
                                    <td>{{ $school->name }}</td>
                                    <td>{{ $program->name }}</td>
                                    <td>Yes</td>
                                    <td>

                                        <!-- For each grouped course -->
                                        @foreach ($program->courses as $course_index => $course)
                                            <!-- If grouped course has activities that evaluate the outcome -->

                                            <?php
                                            
                                            /* $sections_evaluating_outcome = Course::has('activities')
                                                                                                                                    //                                             ->whereNotNull('outcomes_attempted')
                                                                                                                                    //                                             ->whereRaw('outcomes_attempted not like \'%"'.$outcome->id.'":0%\'')
                                                                                                                                                                                ->with(array('activities'=>function($query) use(&$outcome){
                                                                                                                                    //                                                 $query->whereNotNull('outcomes_attempted');
                                                                                                                                    //                                                 $query->whereRaw('outcomes_attempted not like \'%"'.$outcome->id.'":0%\'');} ))
                                                                                                                                    //                                                 $query->whereRaw('outcomes_attempted not like \'%"'.$outcome->id.'":0%\'');
                                                                                                                                                                                    } ))
                                                                                                                                                                                ->where('code', $course->code)->where('number',$course->number)
                                                                                                                                                                                ->whereIn('semester_id', Session::get('semesters_ids'))
                                                                                                                                                                                ->get();*/
                                            
                                            $sections_evaluating_outcome = Course::has('activities')
                                            
                                                ->with([
                                                    'activities' => function ($query) use (&$outcome, &$course) {
                                                        $activities = DB::table('activities')
                                                            ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
                                                            ->join('assessments', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
                                                            ->join('criterion_objective_outcome', 'activity_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
                                                            ->join('courses', 'courses.id', '=', 'activities.course_id')
                                                            ->where('courses.code', $course->code)
                                                            ->where('courses.number', $course->number)
                                                            ->where('criterion_objective_outcome.outcome_id', $outcome->id)
                                                            ->select('activity_id')
                                                            ->lists('activity_id');
                                            
                                                        $query->whereIn('id', $activities);
                                                    },
                                                ])
                                            
                                                ->where('code', $course->code)
                                                ->where('number', $course->number)
                                                ->whereIn('semester_id', Session::get('semesters_ids'))
                                                ->get();
                                            
                                            ?>

                                            @if (count($sections_evaluating_outcome))
                                                <h4>{{ $course->code }}-{{ $course->number }}</h4>

                                                <!-- For each section -->
                                                @foreach ($sections_evaluating_outcome as $section_index => $section)
                                                    <h5><u>Instance {{ $section_index + 1 }}</u></h5>

                                                    <!-- For each activity in the section -->
                                                    @foreach ($section->activities as $activity_index => $activity)
                                                        <?php
                                                        
                                                        ?>
                                                        <!-- If activity has a rubric and the rubric has the outcome being evaluated -->
                                                        @if ($activity->draft == 0 && $activity->diagnostic == 0 && array_key_exists($outcome->id, $activity->o_att_array) && $activity->o_att_array[$outcome->id] >= 1 && isset($section) && isset($activity->rubric[0]))
                                                            <h5>Measure {{ $activity_index + 1 }}</h5>
                                                            <?php
                                                            /*
                                                                                                                                                                                                                                            var_dump($section->code);
                                                                                                                                                                                                                                            var_dump($section->number);
                                                                                                                                                                                                                                            var_dump($section->name);
                                                                                                                                                                                                                                            var_dump($outcome->name);
                                                                                                                                                                                                                                            var_dump(date('M Y', strtotime($activity->date)));
                                                                                                                                                                                                                                            var_dump($activity->name);
                                                                                                                                                                                                                                            var_dump(count($section->students));
                                                                                                                                                                                                                                            print"<br>";
                                                                                                                                                                                                                                            print "A rubric was used in the $section->code-$section->number ($section->name) course (". date('M Y', strtotime($activity->date)).") to assess students’ <u>". strtolower($outcome->name) ."</u> in the activity: '<strong>$activity->name </strong>'. N= ". count($section->students);
                                                                                                                                                                                                                                            exit();
                                                                                                                                                                                                                                            */
                                                            ?>


                                                            <p>A rubric was used in the
                                                                {{ $section->code }}-{{ $section->number }}
                                                                ({{ $section->name }})
                                                                course
                                                                ({{ date('M Y', strtotime($activity->date)) }}) to assess
                                                                students’ <u>{{ strtolower($outcome->name) }}</u> in the
                                                                activity: "<strong>{{ $activity->name }}</strong>". N=
                                                                {{ count($section->students) }}. </p>

                                                            <p>The expected performance level was that
                                                                <strong>{{ $activity->rubric[0]->expected_percentage }}%</strong>
                                                                of students participating in the activity would score
                                                                <strong>{{ $activity->rubric[0]->expected_points }}
                                                                    points</strong> or more in the
                                                                1-{{ $activity->rubric[0]->max_score }} point scale used.
                                                            </p>

                                                            <p>The results for each criterion were as follows:</p>

                                                            <?php
                                                            
                                                            //$rubric_contents = json_decode($activity->rubric->contents);
                                                            ?>

                                                            <table class="table table-condensed table-bordered">
                                                                <tbody>
                                                                    @foreach ((array) $activity->cap_array as $criterion_id => $criterion)
                                                                        @if (in_array($outcome->id, json_decode($criterion->outcome_id)) /*== $outcome->id*/ && $criterion->score_percentage >= $activity->rubric[0]->expected_percentage)
                                                                            <tr>
                                                                                <td>{{ $criterion->name }}</td>
                                                                                <td class="col-md-1 success">
                                                                                    {{ $criterion->score_percentage }}%
                                                                                </td>
                                                                            </tr>
                                                                        @elseif(in_array($outcome->id, json_decode($criterion->outcome_id)) && $criterion->score_percentage < $activity->rubric[0]->expected_percentage)
                                                                            <tr>
                                                                                <td>{{ $criterion->name }}</td>
                                                                                <td class="col-md-1 danger">
                                                                                    {{ $criterion->score_percentage }}%
                                                                                </td>
                                                                            </tr>
                                                                        @endif
                                                                    @endforeach
                                                                </tbody>
                                                            </table>

                                                            <p>
                                                                <?php
                                                                $o_att_array = $activity->o_att_array;
                                                                $o_ach_array = $activity->o_ach_array;
                                                                $percentage = ($o_ach_array[$outcome->id] / $o_att_array[$outcome->id]) * 100;
                                                                ?>
                                                                @if ($percentage >= 100)
                                                                    The expected goal was reached in <strong>all</strong>
                                                                    (100%) of the criteria assessed. Therefore, the goal for
                                                                    this outcome ({{ $outcome->expected_outcome }}%) was
                                                                    <strong>met</strong>.
                                                                @elseif ($percentage < 1)
                                                                    The expected goal was reached in <strong>none</strong>
                                                                    (0%) of the criteria assessed. Therefore, the goal for
                                                                    this outcome ({{ $outcome->expected_outcome }}%) was
                                                                    <strong>not met</strong>.
                                                                @elseif ($percentage >= $outcome->expected_outcome)
                                                                    The expected goal was reached in
                                                                    <strong>{{ $o_ach_array[$outcome->id] }}</strong> out
                                                                    of the
                                                                    <strong>{{ $o_att_array[$outcome->id] }}</strong>
                                                                    ({{ round($percentage, 2) }}%) criteria assessed.
                                                                    Therefore, the goal for this outcome
                                                                    ({{ $outcome->expected_outcome }}%) was
                                                                    <strong>met</strong>.
                                                                @elseif ($percentage < $outcome->expected_outcome)
                                                                    The expected goal was reached in
                                                                    <strong>{{ $o_ach_array[$outcome->id] }}</strong> out
                                                                    of the
                                                                    <strong>{{ $o_att_array[$outcome->id] }}</strong>
                                                                    ({{ round($percentage, 2) }}%) criteria assessed.
                                                                    Therefore, the goal for this outcome
                                                                    ({{ $outcome->expected_outcome }}%) was <strong> not
                                                                        met</strong>.
                                                                @endif
                                                            </p>

                                                            <h5><strong>Transformative Actions</strong></h5>
                                                            @if ($activity->transforming_actions)
                                                                {{ $activity->transforming_actions }}
                                                            @else
                                                                None
                                                            @endif
                                                            <br><br>
                                                        @else
                                                            <h5>Measure {{ $activity_index + 1 }}</h5>
                                                            <em>Learning Outcome not measured.</em>
                                                        @endif
                                                    @endforeach
                                                @endforeach
                                                <hr>
                                            @endif
                                        @endforeach
                                    </td>
                                </tr>
                            @else
                                <tr>
                                    <td>{{ $school->name }}</td>
                                    <td>{{ $program->name }}</td>
                                    <td>No</td>
                                    <td>
                                        This program did not assess {{ $outcome->name }}.
                                    </td>
                                </tr>
                            @endif
                        @endforeach
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
    <span class="js-vars" data-pdf-url="{{ URL::action('OutcomesController@assessmentReport') }}"></span>
@stop

@section('included-js')
    @include('global._datatables_js')
@stop


@section('javascript')

    // Build table of contents
    var outcome = $('.outcome');
    var str ='';
    str+='<li><a href="#'+outcome.attr('id')+'">'+outcome.text()+'</a>
        <ol class="schools upper-alpha">';

            /*$('[id^='+outcome.attr('id')+'-].school:visible').each(function(e){
            var school = $(this);
            str+='<li><a href="#'+school.attr('id')+'">'+school.text()+'</a>
                <ol class="programs">';

                    $('[id^='+school.attr('id')+'-].program:visible').each(function(e){

                    var program = $(this);
                    if(!program.hasClass('no-courses'))
                    str+='<li><a href="#'+program.attr('id')+'">'+program.text()+'</a></li>';
                    else
                    str+='<li>'+program.text()+'</li>';

                    });

                    str+='</ol>
            </li>';
            });*/

            str+='</ol>
    </li>';

    //$('#table-of-contents').append(str);


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

    $('.to-top').on('click', function(e) {
    e.preventDefault();

    $(this).scrollTop(0);
    $('html').animate({scrollTop:0}, 1);
    $('body').animate({scrollTop:0}, 1);
    })

@stop
