<%-# Fedena
#Copyright 2010 Foradian Technologies Private Limited
#
#This product includes software developed at
#Project Fedena - http://www.projectfedena.org/
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing,
#software distributed under the License is distributed on an
#"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#KIND, either express or implied.  See the License for the
#specific language governing permissions and limitations
#under the License. -%>



<div id="page-yield">

  <div class="hor_line"></div>
  <h2><%= "#{t('student_wise_report_for')} "+@exam_group.name+", #{t('batch')}:"+@batch.full_name %></h2>
  <div class="hor_line"></div>

  <% @students.each_with_index do |student,i| %>
    <% general_subjects = Subject.find_all_by_batch_id(@batch.id, :conditions=>"elective_group_id IS NULL") %>
    <% student_electives = StudentsSubject.find_all_by_student_id(student.id,:conditions=>"batch_id = #{@batch.id}") %>
    <% elective_subjects = [] %>
    <% student_electives.each do |elect| %>
      <% elective_subjects.push Subject.find(elect.subject_id) %>
    <% end %>
    <% @subjects = general_subjects + elective_subjects %>
    <% @exams = [] %>
    <% @subjects.each do |sub| %>
      <% exam = Exam.find_by_exam_group_id_and_subject_id(@exam_group.id,sub.id) %>
      <% @exams.push exam unless exam.nil? %>
    <% end %>
    <div class="extender"> </div>
    <% if i == 0  %>
      <div class="report">
      <% else %>
        <div class="report1">
        <% end  %>
        <div id ="main_info">
          <h4> <%= student.full_name %></h4>
          <h4> <%= "#{t('exam_text')} : "+@exam_group.name %></h4>
        </div>
        <div id="pdf-info">
          <table id="pdf-table" width="100%" cellspacing="0">
            <% c = 'even'  %>
            <% if @batch.cwa_enabled? %>
              <% total_credits=0 %>
              <% total_weighted_marks=0 %>
              <tr class="table-header">
                <td class="col-pdf"><%= t('subject_code') %></td>
                <td class="col-pdf"><%= t('subject_title') %></td>
                <td class="col-pdf"><%= t('marks') %></td>
                <td class="col-pdf"><%= t('credit') %></td>
                <td class="col-pdf"><%= t('grade') %></td>
                <td class="col-pdf"><%= t('weighted_marks') %></td>
              </tr>
              <tr class="tr-blank"></tr>
              <% @exams.each_with_index do |exam,i|  %>
                <% exam_score = ExamScore.find_by_student_id_and_exam_id(student,exam) %>
                <% unless exam_score.nil? %>
                  <% mark = (exam_score.marks.to_f/exam.maximum_marks.to_f)*100 || "-" %>
                  <% total_credits += exam.subject.credit_hours.to_f || 0 %>
                  <% weighted_mark=0 %>
                  <% unless mark=="-" %>
                    <% weighted_mark = mark.to_f * exam.subject.credit_hours.to_f %>
                  <% end %>
                  <% total_weighted_marks += weighted_mark %>
                <% else %>
                  <% mark = "-" %>
                  <% weighted_mark=0 %>
                <% end %>
                <% if i== 50 %>
                  <% i= 0 %>
                  <tr class="page-break"><td colspan="3"></td></tr>
                  <tr class="table-header">
                    <td class="col-pdf"><%= t('subject_code') %></td>
                    <td class="col-pdf"><%= t('subject_title') %></td>
                    <td class="col-pdf"><%= t('marks') %></td>
                    <td class="col-pdf"><%= t('credit') %></td>
                    <td class="col-pdf"><%= t('grade') %></td>
                    <td class="col-pdf"><%= t('weighted_marks') %></td>
                  </tr>
                  <tr class="tr-blank"></tr>
                <% end %>
                <tr class="<%= cycle(c,(["odd","even"]-[c]).first) %>">
                  <td class="col-pdf"><%= exam.subject.code %></td>
                  <td class="col-pdf"><%= exam.subject.name %></td>
                  <td class="col-pdf"> <%= mark %> </td>
                  <td class="col-pdf"><%= exam.subject.credit_hours %></td>
                  <td class="col-pdf"><%= exam_score.present? ? (exam_score.grading_level.present? ? exam_score.grading_level : "-") : "-" %></td>
                  <td class="col-pdf"><%= weighted_mark %></td>
                </tr>
              <% end %>
              <tr class="tr-blank"></tr>
              <tr class="table-header">
                <td class="col-pdf" colspan="3" align="right">Total</td>
                <td class="col-pdf"><%= total_credits %></td>
                <td>-</td>
                <td class="col-pdf"><%= total_weighted_marks %></td>
              </tr>
            <% elsif @batch.gpa_enabled? %>
              <% total_credit_points=0 %>
              <% total_credit_hours=0 %>
              <tr class="table-header">
                <td class="col-pdf"><%= t('subject_code') %></td>
                <td class="col-pdf"><%= t('subject_title') %></td>
                <td class="col-pdf"><%= t('credits') %></td>
                <td class="col-pdf"><%= t('grade') %></td>
                <td class="col-pdf"><%= t('remarks') %></td>
              </tr>
              <tr class="tr-blank"></tr>
              <% @exams.each_with_index do |exam,i|  %>
                <% exam_score = ExamScore.find_by_student_id_and_exam_id(student,exam) %>
                <% credit_points = 0 %>
                <% unless exam_score.nil? or exam_score.grading_level.nil? or exam_score.grading_level.credit_points.nil? %>
                  <% credit_points = (exam_score.grading_level.credit_points.to_f)*(exam.subject.credit_hours.to_f)|| "-" %>
                <% end %>
                <% total_credit_hours += exam.subject.credit_hours.to_f || 0 %>
                <% total_credit_points += credit_points %>
                <% if i== 50 %>
                  <% i= 0 %>
                  <tr class="page-break"><td colspan="3"></td></tr>
                  <tr class="table-header">
                    <td class="col-pdf"><%= t('subject_code') %></td>
                    <td class="col-pdf"><%= t('subject_title') %></td>
                    <td class="col-pdf"><%= t('credits') %></td>
                    <td class="col-pdf"><%= t('grade') %></td>
                    <td class="col-pdf"><%= t('remarks') %></td>
                  </tr>
                  <tr class="tr-blank"></tr>
                <% end %>
                <tr class="<%= cycle(c,(["odd","even"]-[c]).first) %>">
                  <td class="col-pdf"><%= exam.subject.code %></td>
                  <td class="col-pdf"><%= exam.subject.name %></td>
                  <td class="col-pdf"><%= exam.subject.credit_hours %></td>
                  <td class="col-pdf"><%= exam_score.present? ?  ("#{exam_score.grading_level.present? ? exam_score.grading_level : "-"}"+" ["+"#{exam_score.grading_level.present? ? (exam_score.grading_level.credit_points.present? ? exam_score.grading_level.credit_points : "-") : "-"}"+"]") : "-" %></td>
                  <td class="col-pdf"><%= exam_score.present? ? (exam_score.grading_level.present? ? (exam_score.grading_level.description.present? ? exam_score.grading_level.description : "-") : "-") : "-" %></td>
                </tr>
              <% end %>
              <tr class="tr-blank"></tr>
              <tr class="table-header">
                <td class="col-pdf" colspan="2" align="right"><%= t('total') %></td>
                <td class="col-pdf"><%= total_credit_hours %></td>
                <td>-</td>
                <td>-</td>
              </tr>
            <% else %>
              <%  if @exam_group.exam_type == 'Marks' %>
                <tr class="table-header"><td class="col-pdf"><%= t('subject') %></td><td class="col-pdf"><%= t('marks_obtained') %></td><td class="col-pdf"><%= t('max_mark') %></td><td class="col-pdf"><%= t('percentage') %>(%)</td></tr>
                <% total_marks = 0 %>
                <% total_max_marks = 0 %>
                <% @exams.each_with_index do |exam,i|  %>
                  <% exam_score = ExamScore.find_by_student_id_and_exam_id(student,exam) %>
                  <% unless exam_score.nil? %>
                    <% mark = exam_score.marks || "-" %>
                    <% total_marks += exam_score.marks || 0 %>
                    <% total_max_marks += exam.maximum_marks %>
                  <% else %>
                    <% mark = "-" %>
                    <% @total_percentage = ((total_marks.to_f/total_max_marks.to_f)*100) %>
                  <% end %>
                  <% if i== 50 %>
                    <% i= 0 %>
                    <tr class="page-break"><td colspan="3"></td></tr>

                    <tr class="table-header"><td class="col-pdf"><%= t('subject') %></td><td class="col-pdf"><%= t('marks_obtained') %> </td><td class="col-pdf"><%= t('max_mark') %></td><td class="col-pdf"><%= t('percentage') %>(%)</td></tr>

                  <% end %>
                  <tr class="<%= cycle(c,(["odd","even"]-[c]).first) %>">
                    <td class="col-pdf"><%= exam.subject.name %></td>
                    <td class="col-pdf"> <%= mark %> </td>
                    <td class="col-pdf"><%= exam.maximum_marks %></td>
                    <td class="col-pdf"><%= (exam_score.calculate_percentage unless exam_score.nil?) %></td>
                  </tr>
                <% end %>
                <tr class="<%= cycle(c,(["odd","even"]-[c]).first) %>">
                  <td class="col-pdf"><%= t('total') %></td>
                  <td class="col-pdf"> <%= total_marks %> </td>
                  <td class="col-pdf"><%= total_max_marks %></td>
                  <td class="col-pdf"><%= @total_percentage %></td>
                </tr>
              <%  elsif @exam_group.exam_type == 'Grades' %>
                <tr class="table-header"><td class="col-pdf"><%= t('subject') %></td><td class="col-pdf"> <%= t('grade') %> </td></tr>
                <% @exams.each do |exam| %>
                  <% exam_score = ExamScore.find_by_student_id_and_exam_id(student,exam) %>
                  <% unless exam_score.nil? %>
                    <tr class="<%= cycle(c,(["odd","even"]-[c]).first) %>">
                      <td class="col-pdf"><%= exam.subject.name %></td>
                      <td class="col-pdf"> <%= exam_score.grading_level || "-" %> </td>
                    </tr>
                  <% else %>
                    <tr class="<%= cycle(c,(["odd","even"]-[c]).first) %>">
                      <td class="col-pdf"><%= exam.subject.name %></td>
                      <td class="col-pdf"> <%= "-" %> </td>
                    </tr>
                  <% end %>
                <% end %>

              <% else %>
                <tr class="table-header"><td class="col-pdf"><%= t('subject') %></td><td class="col-pdf"> <%= t('grade') %> </td><td class="col-pdf"><%= t('marks_obtained') %></td><td class="col-pdf"><%= t('max_mark') %></td><td class="col-pdf"><%= t('percentage') %>(%) </td></tr>
                <% total_marks = 0 %>
                <% total_max_marks = 0 %>
                <% @exams.each do |exam| %>
                  <% exam_score = ExamScore.find_by_student_id_and_exam_id(student,exam) %>
                  <% unless exam_score.nil? %>
                    <% mark = exam_score.marks || "-" %>
                    <% grade = exam_score.grading_level || "-" %>
                    <% total_marks += exam_score.marks || 0 %>
                    <% total_max_marks += exam.maximum_marks %>
                  <% else %>
                    <% mark = "-" %>
                    <% grade = "-" %>
                    <% @total_percentage = ((total_marks.to_f/total_max_marks.to_f)*100) %>
                  <% end %>
                  <tr class="<%= cycle(c,(["odd","even"]-[c]).first) %>">
                    <td class="col-pdf"><%= exam.subject.name %></td>
                    <td class="col-pdf"> <%= grade %> </td>
                    <td class="col-pdf"> <%= mark %> </td>
                    <td class="col-pdf"> <%= exam.maximum_marks %> </td>
                    <td class="col-pdf"> <%= (exam_score.calculate_percentage unless exam_score.nil?) %> </td>
                  </tr>
                <% end %>
                <tr class="<%= cycle(c,(["odd","even"]-[c]).first) %>">
                  <td class="col-pdf"><%= t('total') %></td>
                  <td class="col-pdf"> <%= "-" %> </td>
                  <td class="col-pdf"> <%= total_marks %> </td>
                  <td class="col-pdf"> <%= total_max_marks %> </td>
                  <td class="col-pdf"> <%= (@total_percentage) %> </td>
                </tr>
              <% end %>
            <% end %>
          </table>
        </div>
        <% if @batch.cwa_enabled? %>
          <h4><%= t('weighted_average') %> =
            <% unless total_credits==0 %>
              <%= "%.2f" %(total_weighted_marks.to_f/total_credits.to_f) %>
            <% else %>
              -
            <% end %>
          </h4>
        <% elsif @batch.gpa_enabled? %>
          <h4><%= t('grade_point_average_gpa') %> =
            <% unless total_credit_hours==0 %>
              <%= "%.2f" %(total_credit_points.to_f/total_credit_hours.to_f) %>
            <% else %>
              -
            <% end %></h4>
        <% end %>
      </div>

    <% end %>

  </div>
</div>
