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

class ApiController < ApplicationController
  before_filter :require_user
  include Common

  def course
    sis_id = params[:sis_id]
    property = params[:property]
    course_hash = {}
    if property.nil?
      course_hash = course_info sis_id
    elsif property.eql? "id"
      course_hash = course_info sis_id, "id"
    end

    respond_to do |format|
      format.json { render :json => course_hash }
    end
  end

  def user
    account_id = Account.find_by_name('<NAME>').id
    sfu_id = params[:sfu_id]
    pseudonym = Pseudonym.where(:unique_id => sfu_id, :account_id => account_id).all
    
    raise(ActiveRecord::RecordNotFound) if pseudonym.empty?
    
    user_hash = {}
    unless pseudonym.empty?
      user = User.find pseudonym.first.user_id
      if params[:property].nil?
        if user.nil?
	        raise(ActiveRecord::RecordNotFound)
	      else
	        user_hash["exists"] = "true"
	      end
      elsif params[:property].eql? "profile"
        user_hash["id"] = user.id
        user_hash["name"] = user.name
        user_hash["uuid"] = user.uuid
      elsif params[:property].eql? "groups"
        user_hash = group_membership_for user
      elsif params[:property].eql? "mysfu"
        user_hash = mysfu_enrollments_for user
      elsif params[:property].eql? "sandbox"
        user_hash = sandbox_for(user)
      end
    end

    if params[:property].to_s.eql? "profile"
      return unless authorized_action(user, @current_user, :read)
    end

    respond_to do |format|
      format.json { render :json => user_hash }
    end
  end

  def terms
    term_arr = []
    if params[:term].nil?
      terms = Account.find_by_name('Simon Fraser University').enrollment_terms.find(:all, :conditions => "workflow_state = 'active'", :order => 'sis_source_id DESC').delete_if {|t| t.name == 'Default Term'}
      terms.each do |term|
        term_info = {}
        term_info["name"] = term.name
        term_info["sis_source_id"] = term.sis_source_id
        term_info["start_at"] = term.start_at
        term_info["end_at"] = term.end_at
        term_arr.push term_info
      end
    else
      term = Account.find_by_name('<NAME>').enrollment_terms.find(:all, :conditions => "sis_source_id = '#{params[:term]}'")
      term_info = {}
      term_info["name"] = term.first.name
      term_info["sis_source_id"] = term.first.sis_source_id
      term_info["start_at"] = term.first.start_at
      term_info["end_at"] = term.first.end_at
      term_arr.push term_info
    end

    raise(ActiveRecord::RecordNotFound) if term_arr.empty?

    respond_to do |format|
      format.json { render :json => term_arr }
    end

  end

  # Deprecated. Moved to amaint_controller
  def courses
    course_array = []
    exclude_sectionCode = ["STL", "LAB", "TUT"]

    if params[:term].nil?
      courses = SFU::Course.for_instructor params[:sfuid]
    else
      courses = SFU::Course.for_instructor params[:sfuid], params[:term]
    end

    courses.compact.each do |course|
      course.compact.each do |c|
        course_hash = {}
        course_hash["name"] = c["course"].first["name"]
        course_hash["title"] = c["course"].first["title"]
        course_hash["number"] = c["course"].first["number"]
        course_hash["section"] = c["course"].first["section"]
        course_hash["peopleSoftCode"] = c["course"].first["peopleSoftCode"].to_s
        course_hash["sis_source_id"] = course_hash["peopleSoftCode"] + "-" +
                                       course_hash["name"].downcase +  "-" +
                                       course_hash["number"] + "-" +
                                       course_hash["section"].downcase
        course_hash["sectionTutorials"] = ""
        course_hash["sectionCode"] = c["course"].first["sectionCode"]

        course_code = course_hash["name"]+course_hash["number"]

        if course_hash["section"].end_with? "00"
          if params[:term].nil?
            course_hash["sectionTutorials"] = SFU::Course.section_tutorials(course_code, course_hash["peopleSoftCode"], course_hash["section"])
          else
            course_hash["sectionTutorials"] = SFU::Course.section_tutorials(course_code, params[:term], course_hash["section"])
          end
        end

        course_hash["key"] = course_hash["peopleSoftCode"] + ":::" +
                             course_hash["name"].downcase +  ":::" +
                             course_hash["number"] + ":::" +
                             course_hash["section"].downcase + ":::" +
                             course_hash["title"]
        course_hash["key"].concat ":::" + course_hash["sectionTutorials"].downcase.delete(" ") unless course_hash["sectionTutorials"].empty?

        # hide course if already exists in Canvas or is a Tutorial/Lab
        course_array.push course_hash unless course_exists? course_hash["sis_source_id"] unless exclude_sectionCode.include? c["course"].first["sectionCode"].to_s
      end
    end

    respond_to do |format|
      format.json { render :json => course_array }
    end
  end

  def course_info(sis_id, property = nil)
    course = Course.where(:sis_source_id => sis_id.downcase).all
    if course.empty?
      raise(ActiveRecord::RecordNotFound)
    end
    course_hash = {}
    if course.length == 1
      if property.nil?
        course_hash["id"] = course.first.id
        course_hash["name"] = course.first.name<commit_msg>Fix inability to re-create deleted course<commit_after>require Pathname(File.dirname(__FILE__)) + "../model/sfu/sfu"

class ApiController < ApplicationController
  before_filter :require_user
  include Common

  def course
    sis_id = params[:sis_id]
    property = params[:property]
    course_hash = {}
    if property.nil?
      course_hash = course_info sis_id
    elsif property.eql? "id"
      course_hash = course_info sis_id, "id"
    end

    respond_to do |format|
      format.json { render :json => course_hash }
    end
  end

  def user
    account_id = Account.find_by_name('<NAME> University').id
    sfu_id = params[:sfu_id]
    pseudonym = Pseudonym.where(:unique_id => sfu_id, :account_id => account_id).all
    
    raise(ActiveRecord::RecordNotFound) if pseudonym.empty?
    
    user_hash = {}
    unless pseudonym.empty?
      user = User.find pseudonym.first.user_id
      if params[:property].nil?
        if user.nil?
	        raise(ActiveRecord::RecordNotFound)
	      else
	        user_hash["exists"] = "true"
	      end
      elsif params[:property].eql? "profile"
        user_hash["id"] = user.id
        user_hash["name"] = user.name
        user_hash["uuid"] = user.uuid
      elsif params[:property].eql? "groups"
        user_hash = group_membership_for user
      elsif params[:property].eql? "mysfu"
        user_hash = mysfu_enrollments_for user
      elsif params[:property].eql? "sandbox"
        user_hash = sandbox_for(user)
      end
    end

    if params[:property].to_s.eql? "profile"
      return unless authorized_action(user, @current_user, :read)
    end

    respond_to do |format|
      format.json { render :json => user_hash }
    end
  end

  def terms
    term_arr = []
    if params[:term].nil?
      terms = Account.find_by_name('<NAME> University').enrollment_terms.find(:all, :conditions => "workflow_state = 'active'", :order => 'sis_source_id DESC').delete_if {|t| t.name == 'Default Term'}
      terms.each do |term|
        term_info = {}
        term_info["name"] = term.name
        term_info["sis_source_id"] = term.sis_source_id
        term_info["start_at"] = term.start_at
        term_info["end_at"] = term.end_at
        term_arr.push term_info
      end
    else
      term = Account.find_by_name('<NAME> University').enrollment_terms.find(:all, :conditions => "sis_source_id = '#{params[:term]}'")
      term_info = {}
      term_info["name"] = term.first.name
      term_info["sis_source_id"] = term.first.sis_source_id
      term_info["start_at"] = term.first.start_at
      term_info["end_at"] = term.first.end_at
      term_arr.push term_info
    end

    raise(ActiveRecord::RecordNotFound) if term_arr.empty?

    respond_to do |format|
      format.json { render :json => term_arr }
    end

  end

  # Deprecated. Moved to amaint_controller
  def courses
    course_array = []
    exclude_sectionCode = ["STL", "LAB", "TUT"]

    if params[:term].nil?
      courses = SFU::Course.for_instructor params[:sfuid]
    else
      courses = SFU::Course.for_instructor params[:sfuid], params[:term]
    end

    courses.compact.each do |course|
      course.compact.each do |c|
        course_hash = {}
        course_hash["name"] = c["course"].first["name"]
        course_hash["title"] = c["course"].first["title"]
        course_hash["number"] = c["course"].first["number"]
        course_hash["section"] = c["course"].first["section"]
        course_hash["peopleSoftCode"] = c["course"].first["peopleSoftCode"].to_s
        course_hash["sis_source_id"] = course_hash["peopleSoftCode"] + "-" +
                                       course_hash["name"].downcase +  "-" +
                                       course_hash["number"] + "-" +
                                       course_hash["section"].downcase
        course_hash["sectionTutorials"] = ""
        course_hash["sectionCode"] = c["course"].first["sectionCode"]

        course_code = course_hash["name"]+course_hash["number"]

        if course_hash["section"].end_with? "00"
          if params[:term].nil?
            course_hash["sectionTutorials"] = SFU::Course.section_tutorials(course_code, course_hash["peopleSoftCode"], course_hash["section"])
          else
            course_hash["sectionTutorials"] = SFU::Course.section_tutorials(course_code, params[:term], course_hash["section"])
          end
        end

        course_hash["key"] = course_hash["peopleSoftCode"] + ":::" +
                             course_hash["name"].downcase +  ":::" +
                             course_hash["number"] + ":::" +
                             course_hash["section"].downcase + ":::" +
                             course_hash["title"]
        course_hash["key"].concat ":::" + course_hash["sectionTutorials"].downcase.delete(" ") unless course_hash["sectionTutorials"].empty?

        # hide course if already exists in Canvas or is a Tutorial/Lab
        course_array.push course_hash unless course_exists? course_hash["sis_source_id"] unless exclude_sectionCode.include? c["course"].first["sectionCode"].to_s
      end
    end

    respond_to do |format|
      format.json { render :json => course_array }
    end
  end

  def course_info(sis_id, property = nil)
    course = Course.active.where(:sis_source_id => sis_id.downcase).all
    if course.empty?
      raise(ActiveRecord::RecordNotFound)
    end
    course_hash = {}
    if course.length == 1
      if property.nil?
        course_hash["id"] = course.first.id
        course_hash["name"] = course.first.name
{'fraction_non_alphanumeric': 0.1315621872441088, 'fraction_numerical': 0.0005459011918842689, 'mean_word_length': 2.7072512647554805, 'pattern_counts': {'":': 10, '<': 10, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6813047', 'n_tokens_mistral': 3429, 'n_tokens_neox': 3339, 'n_words': 613}