<commit_before>require Pathname(File.dirname(__FILE__)) + "../../../sfu_api/app/model/sfu/sfu"

class CourseFormController < ApplicationController

  before_filter :require_user

  def new
    @user = User.find(@current_user.id)
    @sfuid = @user.pseudonym.unique_id
    @sfuid.slice! "@sfu.ca"
    @course_list = Array.new
    @current_term = current_term
    @terms = [@current_term] + future_terms
    # only show current term plus next 2 terms (3 in total)
    @term_options = @terms.take(3).map { |term| [term.name, term.sis_source_id] }
    roles = SFU::User.roles @sfuid
    @is_student = %w(undergrad grad).any? { |role| roles.include? role }
    # deny access to undergrad-only users
    if roles == %w(undergrad)
      flash[:error] = "You don't have permission to access that page"
      redirect_to dashboard_url
    end
  end

  def create
    req_user = User.find(@current_user.id).pseudonym.unique_id
    selected_courses = []
    account_id = Account.find_by_name("<NAME>").id
    teacher_username = params[:username]
    teacher2_username = params[:enroll_me]
    teacher_sis_user_id = sis_user_id(teacher_username, account_id)
    teacher2_sis_user_id = sis_user_id(teacher2_username, account_id) unless teacher2_username.nil?
    teacher2_role = sanitize_role(params[:enroll_me_as])
    cross_list = params[:cross_list]

    params.each do |key, value|
      if key.to_s.starts_with? "selected_course"
        selected_courses.push value
      end
    end

    course_array = ["\"course_id\",\"short_name\",\"long_name\",\"account_id\",\"term_id\",\"status\""]
    section_array = ["\"section_id\",\"course_id\",\"name\",\"status\",\"start_date\",\"end_date\""]
    enrollment_array = ["\"course_id\",\"user_id\",\"role\",\"section_id\",\"status\""]

    unless cross_list

      selected_courses.compact.uniq.each do |course|
        if course.starts_with? "sandbox"
          logger.info "[SFU Course Form] Creating sandbox for #{teacher_username} requested by #{req_user}"
          sandbox = sandbox_info(course, teacher_username, teacher_sis_user_id, teacher2_sis_user_id, teacher2_role)

          course_array.push sandbox["csv"]
          enrollment_array.push sandbox["enrollment_csv_1"]
          enrollment_array.push sandbox["enrollment_csv_2"] unless teacher2_sis_user_id.nil?
        elsif course.starts_with? "ncc"
          logger.info "[SFU Course Form] Creating ncc course for #{teacher_username} requested by #{req_user}"
          ncc_course = ncc_info(course, teacher_sis_user_id, teacher2_sis_user_id, teacher2_role)

          course_array.push ncc_course["csv"]
          enrollment_array.push ncc_course["enrollment_csv_1"]
          enrollment_array.push ncc_course["enrollment_csv_2"] unless teacher2_sis_user_id.nil?

        else
          logger.info "[SFU Course Form] Creating single course container : #{course} requested by #{req_user}"
          course_info = course_info(course, account_id, teacher_sis_user_id, teacher2_sis_user_id, teacher2_role)

          # create course csv
          course_array.push course_info["course_csv"]

          # create section csv
          course_info["sections"].compact.uniq.each do  |section|
            section_info = section.split(":_:")
            section_array.push "#{section_info[0]},#{course_info["course_id"]},#{section_info[1]},active,,,"
          end

          enrollment_array.push course_info["enrollment_csv_1"]
          enrollment_array.push course_info["enrollment_csv_2"] unless teacher2_username.nil?
        end
      end

    else

      logger.info "[SFU Course Form] Creating cross-list container : #{selected_courses.inspect} requested by #{req_user}"
      course_id = ""
      short_name = ""
      long_name = ""
      term = ""
      sections = []

      selected_courses.each do |course|
        course_info = course_info(course, account_id, teacher_sis_user_id, teacher2_sis_user_id, teacher2_role)

        course_id.concat "#{course_info["course_id"]}:"
        short_name.concat "#{course_info["short_name"]} / "
        long_name.concat  "#{course_info["long_name"]} / "
        term = course_info["term"]

        sections.push course_info["sections"]
      end

      # create course csv
      course_id.concat "::course"
      course_array.push "\"#{course_id}\",\"#{short_name[0..-4]}\",\"#{long_name[0..-4]}\",\"#{account_id}\",\"#{term}\",\"active\""

      # create section csv
      sections.compact.uniq.each do  |section|
        section_info = section.first.split(":_:")
        section_array.push "\"#{section_info[0]}\",\"#{course_id}\",\"#{section_info[1]}\",\"active\",\"\",\"\","
      end

      # create enrollment csv to default section<commit_msg>Handle overlength cross-listed course names<commit_after>require Pathname(File.dirname(__FILE__)) + "../../../sfu_api/app/model/sfu/sfu"

class CourseFormController < ApplicationController

  # Canvas course names have a limit of 255 characters max.
  CANVAS_COURSE_NAME_MAX = 255

  before_filter :require_user

  def new
    @user = User.find(@current_user.id)
    @sfuid = @user.pseudonym.unique_id
    @sfuid.slice! "@sfu.ca"
    @course_list = Array.new
    @current_term = current_term
    @terms = [@current_term] + future_terms
    # only show current term plus next 2 terms (3 in total)
    @term_options = @terms.take(3).map { |term| [term.name, term.sis_source_id] }
    roles = SFU::User.roles @sfuid
    @is_student = %w(undergrad grad).any? { |role| roles.include? role }
    # deny access to undergrad-only users
    if roles == %w(undergrad)
      flash[:error] = "You don't have permission to access that page"
      redirect_to dashboard_url
    end
  end

  def create
    req_user = User.find(@current_user.id).pseudonym.unique_id
    selected_courses = []
    account_id = Account.find_by_name("<NAME>").id
    teacher_username = params[:username]
    teacher2_username = params[:enroll_me]
    teacher_sis_user_id = sis_user_id(teacher_username, account_id)
    teacher2_sis_user_id = sis_user_id(teacher2_username, account_id) unless teacher2_username.nil?
    teacher2_role = sanitize_role(params[:enroll_me_as])
    cross_list = params[:cross_list]

    params.each do |key, value|
      if key.to_s.starts_with? "selected_course"
        selected_courses.push value
      end
    end

    course_array = ["\"course_id\",\"short_name\",\"long_name\",\"account_id\",\"term_id\",\"status\""]
    section_array = ["\"section_id\",\"course_id\",\"name\",\"status\",\"start_date\",\"end_date\""]
    enrollment_array = ["\"course_id\",\"user_id\",\"role\",\"section_id\",\"status\""]

    unless cross_list

      selected_courses.compact.uniq.each do |course|
        if course.starts_with? "sandbox"
          logger.info "[SFU Course Form] Creating sandbox for #{teacher_username} requested by #{req_user}"
          sandbox = sandbox_info(course, teacher_username, teacher_sis_user_id, teacher2_sis_user_id, teacher2_role)

          course_array.push sandbox["csv"]
          enrollment_array.push sandbox["enrollment_csv_1"]
          enrollment_array.push sandbox["enrollment_csv_2"] unless teacher2_sis_user_id.nil?
        elsif course.starts_with? "ncc"
          logger.info "[SFU Course Form] Creating ncc course for #{teacher_username} requested by #{req_user}"
          ncc_course = ncc_info(course, teacher_sis_user_id, teacher2_sis_user_id, teacher2_role)

          course_array.push ncc_course["csv"]
          enrollment_array.push ncc_course["enrollment_csv_1"]
          enrollment_array.push ncc_course["enrollment_csv_2"] unless teacher2_sis_user_id.nil?

        else
          logger.info "[SFU Course Form] Creating single course container : #{course} requested by #{req_user}"
          course_info = course_info(course, account_id, teacher_sis_user_id, teacher2_sis_user_id, teacher2_role)

          # create course csv
          course_array.push course_info["course_csv"]

          # create section csv
          course_info["sections"].compact.uniq.each do  |section|
            section_info = section.split(":_:")
            section_array.push "#{section_info[0]},#{course_info["course_id"]},#{section_info[1]},active,,,"
          end

          enrollment_array.push course_info["enrollment_csv_1"]
          enrollment_array.push course_info["enrollment_csv_2"] unless teacher2_username.nil?
        end
      end

    else

      logger.info "[SFU Course Form] Creating cross-list container : #{selected_courses.inspect} requested by #{req_user}"
      course_ids = []
      short_names = []
      long_names = []
      term = ""
      sections = []

      selected_courses.each do |course|
        course_info = course_info(course, account_id, teacher_sis_user_id, teacher2_sis_user_id, teacher2_role)

        course_ids.push course_info["course_id"]
        short_names.push course_info["short_name"]
        long_names.push course_info["long_name"]
        term = course_info["term"]

        sections.push course_info["sections"]
      end

      course_id = "#{course_ids.join(':')}:::course"
      short_name = short_names.join(' / ')
      long_name = long_names.join(' / ')

      # Use a shorter version (omit subsequent titles) of the long name if it's too long.
      # Original long name: IAT100 D100 Example Course / IAT100 D200 Example Course / IAT100 D300 Example Course
      # Shorter version:    IAT100 D100 Example Course / IAT100 D200 / IAT100 D300
      long_name = (long_names[0, 1] + short_names[1..-1]).join(' / ') if long_name.length > CANVAS_COURSE_NAME_MAX

      # create course csv
      course_array.push "\"#{course_id}\",\"#{short_name}\",\"#{long_name}\",\"#{account_id}\",\"#{term}\",\"active\""

      # create section csv
      sections.compact.uniq.each do  |section|
        section_info = section.first.split(":_:")
        section_array.push "\"#{section_info[0]}\",\"#{course_id}\",\"#{section_info[1]}\",\"active\",\"\",\"\","
      end

      # create enrollment csv to default section
{'fraction_non_alphanumeric': 0.1298428271098208, 'fraction_numerical': 0.01081189308239063, 'mean_word_length': 3.3719912472647704, 'pattern_counts': {'":': 5, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1696087', 'n_tokens_mistral': 3277, 'n_tokens_neox': 3202, 'n_words': 614}