<commit_before>require 'zip'

class SubmissionsController < ApplicationController
  include SubmissionsHelper

  helper_method :all_assignments_marked?

  before_filter :authorize_only_for_admin,
                except: [:server_time,
                         :populate_file_manager_react,
                         :browse,
                         :file_manager,
                         :update_files,
                         :download,
                         :downloads,
                         :collect_and_begin_grading,
                         :download_groupings_files,
                         :manually_collect_and_begin_grading,
                         :collect_ta_submissions,
                         :repo_browser,
                         :update_submissions,
                         :populate_submissions_table]
  before_filter :authorize_for_ta_and_admin,
                only: [:browse,
                       :collect_and_begin_grading,
                       :manually_collect_and_begin_grading,
                       :collect_ta_submissions,
                       :repo_browser,
                       :download_groupings_files,
                       :update_submissions,
                       :populate_submissions_table]
  before_filter :authorize_for_student,
                only: [:file_manager,
                       :update_files,
                       :populate_file_manager_react]
  before_filter :authorize_for_user, only: [:download, :downloads]

  def repo_browser
    @assignment = Assignment.find(params[:assignment_id])
    @grouping = Grouping.find(params[:id])
    @assignment = @grouping.assignment
    @path = params[:path] || '/'
    @previous_path = File.split(@path).first
    @repository_name = @grouping.group.repository_name
    repo = @grouping.group.repo

    begin
      if params[:revision_number]
        @revision_number = params[:revision_number].to_i
      elsif params[:revision_timestamp]
        @revision_number = repo.get_revision_by_timestamp(
            Time.parse(params[:revision_timestamp])).revision_number
      else
        @revision_number = repo.get_latest_revision.revision_number
      end
      if @revision_number == 0
        @revision_number = 1
      end
      @revision = repo.get_revision(@revision_number)
      @revision_timestamp = @revision.timestamp
    rescue Exception => e
      flash[:error] = e.message
      @revision_number = repo.get_latest_revision.revision_number
      @revision_timestamp = repo.get_latest_revision.timestamp
    end
    # Generate a revisions' history with date and num
    @revisions_history = []

    # Good idea from git branch. But SubversionRepository has
    # no get_all_revisions method... yet (TODO)
    # hmm. Let's make rev_number a method and have it return an array.
    # repo.get_all_revisions.each do |revision|
    #  @revisions_history << {num: revision.revision_number,
    #                         date: revision.timestamp}
    rev_number = repo.get_latest_revision.revision_number + 1
    assign_path = File.join(@assignment.repository_folder, @path)
    rev_number.times do |rev|
      begin
        revision = repo.get_revision(rev)
        unless revision.path_exists?(assign_path)
          raise 'error'
        end
      rescue Exception
        revision = nil
      end
      if revision && !revision.changed_files_at_path(assign_path).empty?
        @revisions_history << { num: revision.revision_number,
                                date: revision.timestamp }
        unless params[:revision_number] || params[:revision_timestamp]
          @revision_number = revision.revision_number
          @revision_timestamp = revision.timestamp
        end
      end
    end

    if @revisions_history.empty?
      rev_number.times do |rev|
        begin
          revision = repo.get_revision(rev)
          unless revision.path_exists?(assign_path)
            raise 'error'
          end
        rescue Exception
          revision = nil
        end
        if revision
          @revisions_history << { num: revision.revision_number,
                                  date: revision.timestamp }
          unless params[:revision_number] || params[:revision_timestamp]
            @revision_number = revision.revision_number
            @revision_timestamp = revision.timestamp
          end
        end
      end
    end

    last_rev = @grouping.submissions
    @last_submission = nil
    if !last_rev.empty?
      selected = @revisions_history.select { |rev| rev[:num] == 
                                             last_rev.last.revision_number }
      @last_submission = selected.empty? ? nil : last_rev.last
    end

    respond_to do |format|
      format.html
      format.json do
        render json: get_repo_browser_table_info(@assignment, @revision,
                                                 @revision_number, @path,
                                                 @previous_path,
                                                 @grouping.id)
      end
    end

    repo.close
  end

  def file_manager
    @assignment = Assignment.find(params[:assignment_id])
    @grouping = current_user.accepted_grouping_for(@assignment.id)
    if @grouping.nil?
      redirect_to controller: 'assignments',
                  action: 'student_interface',
                  id: params[:id]
      return
    end<commit_msg>fixing hound complaints<commit_after>require 'zip'

class SubmissionsController < ApplicationController
  include SubmissionsHelper

  helper_method :all_assignments_marked?

  before_filter :authorize_only_for_admin,
                except: [:server_time,
                         :populate_file_manager_react,
                         :browse,
                         :file_manager,
                         :update_files,
                         :download,
                         :downloads,
                         :collect_and_begin_grading,
                         :download_groupings_files,
                         :manually_collect_and_begin_grading,
                         :collect_ta_submissions,
                         :repo_browser,
                         :update_submissions,
                         :populate_submissions_table]
  before_filter :authorize_for_ta_and_admin,
                only: [:browse,
                       :collect_and_begin_grading,
                       :manually_collect_and_begin_grading,
                       :collect_ta_submissions,
                       :repo_browser,
                       :download_groupings_files,
                       :update_submissions,
                       :populate_submissions_table]
  before_filter :authorize_for_student,
                only: [:file_manager,
                       :update_files,
                       :populate_file_manager_react]
  before_filter :authorize_for_user, only: [:download, :downloads]

  def repo_browser
    @assignment = Assignment.find(params[:assignment_id])
    @grouping = Grouping.find(params[:id])
    @assignment = @grouping.assignment
    @path = params[:path] || '/'
    @previous_path = File.split(@path).first
    @repository_name = @grouping.group.repository_name
    repo = @grouping.group.repo

    begin
      if params[:revision_number]
        @revision_number = params[:revision_number].to_i
      elsif params[:revision_timestamp]
        @revision_number = repo.get_revision_by_timestamp(
            Time.parse(params[:revision_timestamp])).revision_number
      else
        @revision_number = repo.get_latest_revision.revision_number
      end
      if @revision_number == 0
        @revision_number = 1
      end
      @revision = repo.get_revision(@revision_number)
      @revision_timestamp = @revision.timestamp
    rescue Exception => e
      flash[:error] = e.message
      @revision_number = repo.get_latest_revision.revision_number
      @revision_timestamp = repo.get_latest_revision.timestamp
    end
    # Generate a revisions' history with date and num
    @revisions_history = []

    # Good idea from git branch. But SubversionRepository has
    # no get_all_revisions method... yet (TODO)
    # hmm. Let's make rev_number a method and have it return an array.
    # repo.get_all_revisions.each do |revision|
    #  @revisions_history << {num: revision.revision_number,
    #                         date: revision.timestamp}
    rev_number = repo.get_latest_revision.revision_number + 1
    assign_path = File.join(@assignment.repository_folder, @path)
    rev_number.times do |rev|
      begin
        revision = repo.get_revision(rev)
        unless revision.path_exists?(assign_path)
          raise 'error'
        end
      rescue Exception
        revision = nil
      end
      if revision && !revision.changed_files_at_path(assign_path).empty?
        @revisions_history << { num: revision.revision_number,
                                date: revision.timestamp }
        unless params[:revision_number] || params[:revision_timestamp]
          @revision_number = revision.revision_number
          @revision_timestamp = revision.timestamp
        end
      end
    end

    if @revisions_history.empty?
      rev_number.times do |rev|
        begin
          revision = repo.get_revision(rev)
          unless revision.path_exists?(assign_path)
            raise 'error'
          end
        rescue Exception
          revision = nil
        end
        if revision
          @revisions_history << { num: revision.revision_number,
                                  date: revision.timestamp }
          unless params[:revision_number] || params[:revision_timestamp]
            @revision_number = revision.revision_number
            @revision_timestamp = revision.timestamp
          end
        end
      end
    end

    last_rev = @grouping.submissions
    @last_submission = nil
    if !last_rev.empty?
      selected = @revisions_history.select do |rev| 
        rev[:num] == last_rev.last.revision_number 
      end

      @last_submission = selected.empty? ? nil : last_rev.last
    end

    respond_to do |format|
      format.html
      format.json do
        render json: get_repo_browser_table_info(@assignment, @revision,
                                                 @revision_number, @path,
                                                 @previous_path,
                                                 @grouping.id)
      end
    end

    repo.close
  end

  def file_manager
    @assignment = Assignment.find(params[:assignment_id])
    @grouping = current_user.accepted_grouping_for(@assignment.id)
    if @grouping.nil?
      redirect_to controller: 'assignments',
                  action: 'student_interface',
                  id: params[:id]
      return
    end
{'fraction_non_alphanumeric': 0.07328869047619048, 'fraction_numerical': 0.0005580357142857143, 'mean_word_length': 1.8492315845257021, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5887907', 'n_tokens_mistral': 2952, 'n_tokens_neox': 2831, 'n_words': 537}