<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>
<% provide_blank :right_side %>
<h1 class='screenreader-only'><%= t(:page_title, 'Grades') %></h1>
<% unless @presenter.student_enrollments.empty? %>
<h2><%= t('titles.current_student_courses', "Courses I'm Taking") %></h2>
<table class="course_details student_grades">
<% @presenter.student_enrollments.each do |course, enrollment| %>
  <tr>
    <td class="course"><a href="<%= course_student_grades_path(course.id, enrollment.user_id) %>"><%= course.name %></a></td>
    <td class="percent">
      <% if course.hide_final_grades? || (@grading_periods[course.id] && @grading_periods[course.id][:selected_period_id] == 0 && !course.display_totals_for_all_grading_periods?)%>
        --
      <% elsif @grades[:student_enrollments][course.id] %>
        <%= I18n.n(@grades[:student_enrollments][course.id], percentage: true) %>
      <% else %>
        <%= t('no_grade', 'no grade') %>
      <% end %>
    </td>
    <td style="display: none;"><a href="#" title="<%= t('send_teacher_message', 'Send a Message to the Teacher') %>" class="no-hover"><%= image_tag "email.png", :alt => t('Send a Message to the Teacher') %></a></td>
    <td class="grading_period_dropdown">
      <% if @grading_periods[course.id] %>
          <%= render partial: "shared/grading_periods_selector", locals: {selected_period_id: @grading_periods[course.id][:selected_period_id], enrollment_id: enrollment.global_id, grading_periods: @grading_periods[course.id][:periods], all_grading_periods_option: course.display_totals_for_all_grading_periods?} %>
      <% end %>
    </td>
  </tr>
<% end %>
</table>
<% end %>
<% unless @presenter.observed_enrollments.empty? %>
<h2 style="margin-top: 20px;"><%= t('linked_student_accounts', 'Linked Student Accounts') %></h2>
<table class="course_details observer_grades">
<% @presenter.observed_enrollments.each do |enrollment| %>
  <tr>
    <td class="course"><a href="<%= course_student_grades_path(enrollment.course_id, enrollment.user_id) %>"><%= context_user_name(enrollment.course, enrollment.user_id) %>, <%= enrollment.course.name %></a></td>
    <td class="percent">
      <% if enrollment.course.hide_final_grades? || (@grading_periods[enrollment.course_id] && @grading_periods[enrollment.course_id][:selected_period_id] == 0) && !enrollment.course.display_totals_for_all_grading_periods?%>
        --
      <% elsif @grades&.dig(:observed_enrollments, enrollment.course_id, enrollment.user_id) %>
        <%= @grades[:observed_enrollments][enrollment.course_id][enrollment.user_id] %>%
      <% else %>
        <%= t('no_grade', 'no grade') %>
      <% end %>
    </td>
    <td style="display: none;"><a href="#" title="<%= t('send_teacher_message', 'Send a Message to the Teacher') %>" class="no-hover"><%= image_tag "email.png", :alt => t('Send a Message to the Teacher') %></a></td>
    <td class="grading_period_dropdown">
      <% if @grading_periods[enrollment.course_id] %>
          <%= render partial: "shared/grading_periods_selector", locals: {selected_period_id: @grading_periods[enrollment.course_id][:selected_period_id], enrollment_id: enrollment.global_id, grading_periods: @grading_periods[enrollment.course_id][:periods], all_grading_periods_option: enrollment.course.display_totals_for_all_grading_periods?} %>
      <% end %>
    </td>
  </tr>
<% end %>
</table>
<% end %>
<% unless @presenter.teacher_enrollments.empty? %>
<h2 style="margin-top: 20px;"><%= t('current_teacher_courses', "Courses I'm Teaching") %></h2>
<table class="course_details teacher_grades">
<% @presenter.teacher_enrollments.each do |enrollment| %>
  <tr>
    <td class="course" style="vertical-align: middle;"><a href="<%= course_grades_path(enrollment.course_id) %>"><%= enrollment.course.name %></a></td>
    <td class="percent">
      <% if @presenter.course_grade_summaries[enrollment.course_id][:score] %>
        <%= n(@presenter.course_grade_summaries[enrollment.course_id][:score], percentage: true, precision: 2) %>
        <div style="font-size: 0.6em; font-weight: normal;"><%= t('average_grades', {:one => "average for 1 student", :other => "average for %{count} students"}, :count => @presenter.course_grade_summaries[enrollment.course_id][:students]) %></div>
      <% else %>
        <span style="font-weight: normal; font-size: 0.9em;"><%= t('no_grades', 'no grades') %></span>
      <% end %>
    </td>
    <td style="display: none;"><a href="#" title="<%= t('send_teacher_message', 'Send a Message to the Teacher') %>" class="no-hover"><%= image_tag "email.png", :alt => t('Send a Message to the Teacher') %></a></td>
    <td class="report">
      <%= link_to(t('links.teacher_activity', 'Student Interactions Report'), user_course_teacher_activity_url(@current_user, enrollment.course_id)) unless @presenter.course_grade_summaries[enrollment.course_id][:students].try(:zero?) %>
    </td>
  </tr>
<% end %>
</table>
<% end %>
<% unless @presenter.prior_enrollments.empty? %>
<div style="margin-top: 20px; display: none;" id="previous_courses">
<h2><%= t('titles.previous_courses', 'Previous Courses') %></h2>
<table class="summary" style="margin-<%= direction('left') %>: 20px; min-width: 350px;">
  <thead><tr>
    <th><%= t('course', 'Course') %></th>
    <th><%= t('percent', 'Percent') %></th>
  </tr></thead>
  <% @presenter.prior_enrollments.each do |enrollment| %>
    <tr>
      <td><%= enrollment.course.name %></td>
      <td>
        <% if enrollment.course.hide_final_grades? %>
          --
        <% elsif enrollment.computed_current_score %>
          <%= enrollment.computed_current_score %>%
        <% else %>
          --
        <% end %>
      </td>
    </tr>
  <% end %>
</table>
</div>
<% end %>
