<%
# 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 :page_title, t(:page_title, "Statistics for %{account}", :account => @account.name)
  set_active_tab "statistics"
  css_bundle :statistics
  js_bundle :account_statistics
%>
<% @customLinks = account_custom_links; if @customLinks %>
  <% provide :right_side do %>
    <% @customLinks.each do |link| %>
      <a href="<%= link[:url] %>" role="button" class="btn button-sidebar-wide"><i class="<%= link[:icon_class] %>" role="presentation"></i> <%= link[:text] %></a>
    <% end %>
  <% end %>
<% end %>
<h1 class="screenreader-only"><%= t(:page_header_title, "Account Statistics") %></h1>
<% if @counts_report %>
  <h2><%= t(:general_numbers_title, "General Numbers") %></h2>
  <table class="numbers_table">
    <thead class="screenreader-only">
      <tr>
        <th scope="col"><%= t("Category") %></th>
        <th scope="col"><%= t("Value") %></th>
        <th scope="col"><%= t("Actions") %></th>
      </tr>
    </thead>
    <tr>
      <th scope="row"><%= before_label(:generated_at, "Generated") %></th>
      <td><%= datetime_string(@counts_report['generated_at']) %></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <th scope="row"><%= before_label(:courses, "Courses") %></th>
      <td><%= n(@counts_report['courses']) %></td>
      <td style="font-size: 0.8em; padding-<%= direction('left') %>: 20px;">
        <a aria-label="<%= t("courses over time") %>" href="#" class="over_time_link" data-key="courses" data-name="<%= t('labels.courses', "Courses") %>">
          <span aria-hidden="true"><%= t(:over_time_link, "over time") %></span>
        </a></td>
    </tr>
    <tr>
      <th scope="row"><%= before_label(:teachers, "Teachers") %></th>
      <td><%= n(@counts_report['teachers']) %></td>
      <td style="font-size: 0.8em; padding-<%= direction('left') %>: 20px;">
        <a aria-label="<%= t("teachers over time") %>" href="#" class="over_time_link" data-key="teachers" data-name="<%= t('labels.teachers', "Teachers") %>">
          <span aria-hidden="true"><%= t(:over_time_link, "over time") %></span>
        </a></td>
    </tr>
    <tr>
      <th scope="row"><%= before_label(:students, "Students") %></th>
      <td><%= n(@counts_report['students']) %></td>
      <td style="font-size: 0.8em; padding-<%= direction('left') %>: 20px;">
        <a aria-label="<%= t("students over time") %>" href="#" class="over_time_link" data-key="students" data-name="<%= t('labels.students', "Students") %>">
          <span aria-hidden="true"><%= t(:over_time_link, "over time") %></span>
        </a></td>
    </tr>
    <tr>
      <th scope="row"><%= before_label(:users, "Users") %></th>
      <td><%= n(@counts_report['users']) %></td>
      <td style="font-size: 0.8em; padding-<%= direction('left') %>: 20px;">
        <a aria-label="<%= t("users over time") %>"href="#" class="over_time_link" data-key="users" data-name="<%= t('labels.users', "Users") %>">
          <span aria-hidden="true"><%= t(:over_time_link, "over time") %></span>
        </a></td>
    </tr>
  </table>
  <h2><%= t("File Storage") %></h2>
  <table class="numbers_table">
    <thead class="screenreader-only">
      <tr>
        <th scope="col"><%= t("Category") %></th>
        <th scope="col"><%= t("Value") %></th>
        <th scope="col"><%= t("Actions") %></th>
      </tr>
    </thead>
    <tr>
      <th scope="row">&nbsp;&nbsp;<%= before_label(:uploaded_storage, "Uploaded") %></th>
      <td class='number'>
        <%= t(:uploaded_data, { :one => "%{file_size} *from one file*", :other => "%{file_size} *from %{count} files*" },
             :file_size => number_to_human_size(@counts_report['files_size'] || 0), :count => @counts_report['files'],
             :wrapper => '<span style="font-size: 0.8em;">\1</span>') %>
      </td>
      <td style="font-size: 0.8em; padding-<%= direction('left') %>: 20px;">
        <a aria-label="<%= t("uploaded over time") %>" href="#" class="over_time_link" data-key="files_size" data-name="<%= t(:uploaded_file_storage_header, "Uploaded File Storage") %>">
          <span aria-hidden="true"><%= t(:over_time_link, "over time") %></span>
        </a>
      </td>
    </tr>
    <tr>
      <th scope="row">&nbsp;&nbsp;<%= before_label(:media_storage, "Media") %></th>
      <td class='number'>
        <%= t(:media_data, { :one => "%{file_size} *from one file*", :other => "%{file_size} *from %{count} files*" },
             :file_size => number_to_human_size(@counts_report['media_files_size'] || 0), :count => @counts_report['media_files'],
             :wrapper => '<span style="font-size: 0.8em;">\1</span>') %>
      </td>
      <td style="font-size: 0.8em; padding-<%= direction('left') %>: 20px;">
        <a aria-label="<%= t("media over time") %>" href="#" class="over_time_link" data-key="media_files_size" data-name="<%= t(:media_file_storage_header, "Media File Storage") %>">
          <span aria-hidden="true"><%= t(:over_time_link, "over time") %></span>
        </a>
      </td>
    </tr>
  </table>
<% end %>

<% if can_do @account, @current_user, :read_course_list %>
<% if @account == Account.default %>
<h2><%= t(:recently_created_courses_title, "Recently Created Courses") %></h2>
<ul id="recently_created_item_list" class="item_list">
  <% @recently_created_courses.each do |course| %>
    <li>
      <a href="<%= course_path(course.id) %>" class="header"><%= course.name %></a>
      <div class="sub_header">
        <% teacher = course.teachers.first %>
        <% if teacher %>
          <%= mt(:created_at_by, "created %{at} by [%{teacher}](%{url})",
              :at => datetime_string(course.created_at),
              :teacher => teacher.name,
              :url => user_url(teacher.id)) %>
        <% else %>
          <%= t(:created_at, "created %{at}",
              :at => datetime_string(course.created_at)) %>
        <% end %>
      </div>
    </li>
  <% end %>
  <% if @recently_started_courses.empty? %>
    <li><%= t(:none_message, "None to show") %></li>
  <% end %>
</ul>
<% end %>

<h2><%= t(:recently_started_courses_title, "Recently Started Courses") %></h2>
<ul id="recently_started_item_list" class="item_list">
  <% @recently_started_courses.each do |course| %>
    <li>
      <a href="<%= course_path(course.id) %>" class="header"><%= course.name %></a>
      <div class="sub_header"><%= t(:started_at, "started %{at}", :at => datetime_string(course.start_at) ) %></div>
    </li>
  <% end %>
  <% if @recently_started_courses.empty? %>
    <li><%= t(:none_message, "None to show") %></li>
  <% end %>
</ul>

<h2><%= t(:recently_ended_courses_title, "Recently Ended Courses") %></h2>
<ul id="recently_ended_item_list" class="item_list">
  <% @recently_ended_courses.each do |course| %>
    <li>
      <a href="<%= course_path(course.id) %>" class="header"><%= course.name %></a>
      <div class="sub_header"><%= t(:concluded_at, "concluded %{at}", :at => datetime_string(course.conclude_at) ) %></div>
    </li>
  <% end %>
  <% if @recently_ended_courses.empty? %>
    <li><%= t(:none_message, "None to show") %></li>
  <% end %>
</ul>
<% end %>

<% if can_do @account, @current_user, :read_roster %>
<h2><%= t(:recently_logged_in_users_title, "Recently Logged-In Users") %></h2>
<ul id="recently_logged_in_item_list" class="item_list">
  <% @recently_logged_users.each do |user| %>
    <li>
      <a href="<%= user_path(user.id) %>" class="header"><%= user.name %></a>
      <div class="sub_header"><%= t(:last_logged_in_at, "last logged in %{at}", :at => datetime_string(user.pseudonyms.map{|p| [p.current_login_at, p.last_login_at]}.flatten.compact.max) ) %></div>
    </li>
  <% end %>
  <% if @recently_logged_users.empty? %>
    <li><%= t(:none_message, "None to show") %></li>
  <% end %>
</ul>
<% end %>

<div id="over_time_dialog" style="display: none;">
  <div id='over_time' style='width: 600px; height: 240px;'></div>
  <div style="text-align: <%= direction('right') %>;">
    <a href="#" class="csv_url xls"><%= t(:download_csv_link, "Download csv") %></a>
  </div>
  <a href="<%= context_url(@context, :context_statistics_graph_url, '{{ attribute }}') %>" class="over_time_url" style="display: none;">&nbsp;</a>
</div>

<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script> google.load('visualization', '1', {'packages':['annotatedtimeline']}); </script>
