<commit_before>require 'application_responder'

class ApplicationController < ActionController::Base
  self.responder = ApplicationResponder
  #Canable details
  include Canable::Enforcers
  include Omniauth::Lti::Context
  include CustomNamedRoutes
  include CurrentScopes
  include CourseTerms
  include ZipUtils

  delegate :can_view?, :to => :current_user
  helper_method :can_view?
  hide_action :can_view?

  respond_to :html

  protect_from_forgery with: :reset_session

  Rails.env.production? do
    before_filter :check_url
    force_ssl except: :ping
  end

  def check_url
    redirect_to request.protocol + "www." + request.host_with_port + request.fullpath if !/^www/.match(request.host)
  end

  before_filter :require_login, :except => [:not_authenticated]

  before_filter :increment_page_views

  before_filter :get_course_scores

  include ApplicationHelper

  def not_authenticated
    if request.env["REMOTE_USER"] != nil
      @user = User.find_by_username(request.env["REMOTE_USER"])
      if @user
        auto_login(@user)
        User.increment_counter(:visit_count, @user.id)
        redirect_to dashboard_path
      else
        redirect_to root_url, :alert => "Please login first."
        #We ultimately need to handle Cosign approved users who don't have GradeCraft accounts
      end
    else
      redirect_to root_path, :alert => "Please login first."
    end
  end

  # Getting the course scores to display the box plot results
  def get_course_scores
    if current_user.present? && current_user_is_student?
      @scores_for_current_course = current_student.scores_for_course(current_course)
    end
  end

  protected

  # Core role authentication
  def ensure_student?
    return not_authenticated unless current_user_is_student?
  end

  def ensure_staff?
    return not_authenticated unless current_user_is_staff?
  end

  def ensure_prof?
    return not_authenticated unless current_user_is_professor?
  end

  def ensure_admin?
    return not_authenticated unless current_user_is_admin?
  end

  def export_zip(export_name, temp_dir, &file_creation)
    begin
      file_creation.call<commit_msg>comment on export zip usage<commit_after>require 'application_responder'

class ApplicationController < ActionController::Base
  self.responder = ApplicationResponder
  #Canable details
  include Canable::Enforcers
  include Omniauth::Lti::Context
  include CustomNamedRoutes
  include CurrentScopes
  include CourseTerms
  include ZipUtils

  delegate :can_view?, :to => :current_user
  helper_method :can_view?
  hide_action :can_view?

  respond_to :html

  protect_from_forgery with: :reset_session

  Rails.env.production? do
    before_filter :check_url
    force_ssl except: :ping
  end

  def check_url
    redirect_to request.protocol + "www." + request.host_with_port + request.fullpath if !/^www/.match(request.host)
  end

  before_filter :require_login, :except => [:not_authenticated]

  before_filter :increment_page_views

  before_filter :get_course_scores

  include ApplicationHelper

  def not_authenticated
    if request.env["REMOTE_USER"] != nil
      @user = User.find_by_username(request.env["REMOTE_USER"])
      if @user
        auto_login(@user)
        User.increment_counter(:visit_count, @user.id)
        redirect_to dashboard_path
      else
        redirect_to root_url, :alert => "Please login first."
        #We ultimately need to handle Cosign approved users who don't have GradeCraft accounts
      end
    else
      redirect_to root_path, :alert => "Please login first."
    end
  end

  # Getting the course scores to display the box plot results
  def get_course_scores
    if current_user.present? && current_user_is_student?
      @scores_for_current_course = current_student.scores_for_course(current_course)
    end
  end

  protected

  # Core role authentication
  def ensure_student?
    return not_authenticated unless current_user_is_student?
  end

  def ensure_staff?
    return not_authenticated unless current_user_is_staff?
  end

  def ensure_prof?
    return not_authenticated unless current_user_is_professor?
  end

  def ensure_admin?
    return not_authenticated unless current_user_is_admin?
  end

  # To use: First create the temp directory you will be generating files in.
  # A copy of the directory will be created as a zip download, and the tempdir
  # will be deleted.
  #
  # example:
  #   export_dir = Dir.mktmpdir
  #   export_zip "my_zip", export_dir do
  #     open( "#{export_dir}/my_file.txt",'w' ) do |f|
  #       f.puts ...
  #     end
  #   end
  #
  def export_zip(export_name, temp_dir, &file_creation)
    begin
      file_creation.call
{'fraction_non_alphanumeric': 0.06608024029178287, 'fraction_numerical': 0.0, 'mean_word_length': 3.8260869565217392, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6633994', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1397, 'n_words': 374}