<commit_before>class CanvasLoad < ActiveRecord::Base
  belongs_to :user
  has_many :courses
  
  attr_accessor :response

  accepts_nested_attributes_for :courses, reject_if: proc { |a| a['is_selected'] != '1' }

  def welcome_to_canvas_name
    'Welcome to Canvas'
  end

  def check_sis_id
    sis_profile
  rescue Canvas::ApiError => ex
    if CanvasWrapper.no_exist_fail?(ex)
      false
    else
      raise ex
    end
  end

  def sis_profile
      @sis_profile ||= self.canvas.get_profile_by_sis_id(self.sis_id)
    end

  def setup_welcome(sub_account_id = nil)
    # Check to see if Welcome course exists already
    if welcome_course = search_courses(sub_account_id, welcome_to_canvas_name).find{|c| c['name'] == welcome_to_canvas_name}
      false
    else
      self.courses.create!(content: {
        course_code: "welcome-to-canvas",
        name: welcome_to_canvas_name,
        sis_course_id: nil,
        status: "active",
        cartridge: "https://s3.amazonaws.com/SSL_Assets/sales/demo_courses/2015/welcome-to-canvas-export.imscc"
      }.to_json)
      true
    end
  end

  def find_course_by_course_code(sub_account_id, course_code)
    @courses_for_course_code ||= search_courses(sub_account_id)
    @courses_for_course_code.find{|cc| course_code == cc['course_code']}
  end

  def search_courses(sub_account_id = nil, search_term = nil)
    if canvas.is_account_admin
      found_courses = canvas.get_courses_for_account(sub_account_id, search_term)
    else
      found_courses = canvas.get_courses
      # Filter courses to only the ones we have control over.
      found_courses = found_courses.reject{|course| course['enrollments'].none?{|enrollment| ['teacher', 'designer'].include?(enrollment['type']) } }
    end

    while found_courses.more? && found_courses.length < 500 # Just in case we don't want to loop over courses forever
      found_courses.next_page!
    end

    found_courses
  rescue Canvas::ApiError => ex
    raise Canvas::ApiError.new("Could not get current courses: #{ex}")
  end

  def find_or_create_sub_account(name)
    sub_accounts = canvas.sub_accounts
    if sub_account = sub_accounts.find{|sa| sa['name'] == name}
      sub_account
    else
      canvas.create_subaccount({account: {name: name}})
    end
    rescue Canvas::ApiError => ex
      if CanvasWrapper.authorized_fail?(ex)
        nil
      else
        raise ex
      end
  end

  def find_or_create_course(course, sub_account_id)
    if existing_course = search_courses(sub_account_id).find{|cc| course.course_code == cc['course_code']}
      {
        course: existing_course,
        existing: true
      }
    else
      course_params = course.parsed
      course_params.delete(:sis_course_id)
      canvas_course = canvas.create_course({course: course_params}, sub_account_id)
      course.update_attributes!(canvas_course_id: canvas_course['id'], canvas_account_id: canvas_course['account_id'])
      
      migration = canvas.migrate_content(canvas_course['id'], {
        migration_type: 'common_cartridge_importer',
        settings: {
          file_url: course.cartridge
        }
      })

      {
        course: canvas_course,
        migration: migration,
        existing: false
      }
    end
  end

  def current_users
    return @current_users if @current_users
    @current_users = canvas.list_users # canvas.list_users(sub_account_id)
    while @current_users.more? && @current_users.length < 500 # Just in case we don't want to loop over users forever
      @current_users.next_page!
    end
    @current_users<commit_msg>Append suffix to course name.<commit_after>class CanvasLoad < ActiveRecord::Base
  belongs_to :user
  has_many :courses
  
  attr_accessor :response

  accepts_nested_attributes_for :courses, reject_if: proc { |a| a['is_selected'] != '1' }

  def welcome_to_canvas_name
    'Welcome to Canvas'
  end

  def check_sis_id
    sis_profile
  rescue Canvas::ApiError => ex
    if CanvasWrapper.no_exist_fail?(ex)
      false
    else
      raise ex
    end
  end

  def sis_profile
      @sis_profile ||= self.canvas.get_profile_by_sis_id(self.sis_id)
    end

  def setup_welcome(sub_account_id = nil)
    # Check to see if Welcome course exists already
    if welcome_course = search_courses(sub_account_id, welcome_to_canvas_name).find{|c| c['name'] == welcome_to_canvas_name}
      false
    else
      self.courses.create!(content: {
        course_code: "welcome-to-canvas",
        name: welcome_to_canvas_name,
        sis_course_id: nil,
        status: "active",
        cartridge: "https://s3.amazonaws.com/SSL_Assets/sales/demo_courses/2015/welcome-to-canvas-export.imscc"
      }.to_json)
      true
    end
  end

  def find_course_by_course_code(sub_account_id, course_code)
    @courses_for_course_code ||= search_courses(sub_account_id)
    @courses_for_course_code.find{|cc| course_code == cc['course_code']}
  end

  def search_courses(sub_account_id = nil, search_term = nil)
    if canvas.is_account_admin
      found_courses = canvas.get_courses_for_account(sub_account_id, search_term)
    else
      found_courses = canvas.get_courses
      # Filter courses to only the ones we have control over.
      found_courses = found_courses.reject{|course| course['enrollments'].none?{|enrollment| ['teacher', 'designer'].include?(enrollment['type']) } }
    end

    while found_courses.more? && found_courses.length < 500 # Just in case we don't want to loop over courses forever
      found_courses.next_page!
    end

    found_courses
  rescue Canvas::ApiError => ex
    raise Canvas::ApiError.new("Could not get current courses: #{ex}")
  end

  def find_or_create_sub_account(name)
    sub_accounts = canvas.sub_accounts
    if sub_account = sub_accounts.find{|sa| sa['name'] == name}
      sub_account
    else
      canvas.create_subaccount({account: {name: name}})
    end
    rescue Canvas::ApiError => ex
      if CanvasWrapper.authorized_fail?(ex)
        nil
      else
        raise ex
      end
  end

  def find_or_create_course(course, sub_account_id)

    if existing_course = search_courses(sub_account_id).find{|cc| course.course_code == cc['course_code']}
      {
        course: existing_course,
        existing: true
      }
    else
      course_params = course.parsed
      course_params.delete(:sis_course_id)
      canvas_course = canvas.create_course({course: course_params}, sub_account_id)
      course_params[:name] << " - #{self.suffix}" # Add suffix to course name
      course.update_attributes!(canvas_course_id: canvas_course['id'], canvas_account_id: canvas_course['account_id'])
      
      migration = canvas.migrate_content(canvas_course['id'], {
        migration_type: 'common_cartridge_importer',
        settings: {
          file_url: course.cartridge
        }
      })

      {
        course: canvas_course,
        migration: migration,
        existing: false
      }
    end
  end

  def current_users
    return @current_users if @current_users
    @current_users = canvas.list_users # canvas.list_users(sub_account_id)
    while @current_users.more? && @current_users.length < 500 # Just in case we don't want to loop over users forever
      @current_users.next_page!
    end
    @current_users
{'fraction_non_alphanumeric': 0.0929589154793194, 'fraction_numerical': 0.00331996126711855, 'mean_word_length': 3.3138424821002386, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '3795639', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2322, 'n_words': 478}