<%
# Copyright (C) 2013 - 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/>.
%>

<div id="quiz_details">
  <table class="table">
    <thead>
      <th>
        <%= submitted_students_title(@quiz, @submitted_students, @submissions_from_logged_out) %>
      </th>
      <th>
        <%= unsubmitted_students_title(@quiz, @unsubmitted_students) %>
      </th>
    </thead>
    <tbody>
      <%
        [(@submitted_students.size + @submissions_from_logged_out.size), @unsubmitted_students.size, 1].max.times do |index|
          student = @submitted_students[index]
          unsubmitted_student = @unsubmitted_students[index]
          submission = student.present? ? @submissions_from_users[student.id] : nil
          #logged out users are displayed after all students
          anon_index = index - @submitted_students.size
          logged_out = @submissions_from_logged_out[anon_index] if anon_index >= 0
      %>
      <tr>
        <td>
          <% if @submitted_students.empty? && @submissions_from_logged_out.empty? && index == 0 %>
            <%= no_submitted_students_msg(@quiz) %>
          <% elsif student.present? && submission.present? %>
            <% if submission.pending_review? %>
              <i class="icon-warning"><span class="screenreader-only"><%= t('titles.submission_needs_review', "This submission needs review") %></span></i>
            <% end %>
          <a href="<%= course_quiz_history_path(@context, @quiz, quiz_submission_id: submission.id) %>" class="name">
            <% if @quiz.anonymous_survey? %>
              <%= t(:anonymous_student, "Student %{user_counter}", :user_counter => index + 1) %>
            <% else %>
              <%= student.name %>
            <% end %>
          </a>
          <% elsif logged_out.present? %>
            <a href="<%= course_quiz_history_path(@context, @quiz, :history, quiz_submission_id: logged_out.id) %>&u_index=<%= anon_index + 1 %>" class="name">
              <%= t(:logged_out_user, "Logged Out User %{user_counter}", :user_counter => anon_index + 1) %>
            </a>
          <% end %>
        </td>
        <td>
          <span>
            <% if @unsubmitted_students.empty? && !@submitted_students.empty? && index == 0 %>
              <%= no_unsubmitted_students_msg(@quiz) %>
            <% elsif unsubmitted_student.present? && !@quiz.anonymous_survey? %>
              <%= unsubmitted_student.name %>
            <% end %>
          </span>
        </td>
      </tr>
      <% end %>
    </tbody>
  </table>
</div>

<script>
<%# Doing this here instead of js_env because js_env doesn't get called
  with render :layout => false %>
window.ENV = window.ENV || {};
window.ENV.QUIZ_SUBMISSION_LIST = <%= raw(@quiz_submission_list) %>;
</script>
