<commit_before>class UserCoursesController < ApplicationController
  before_filter :require_login!

  DatatableFields = ["remove", "course_number", "section", "title","instructor", 
                     "seats_left", "seats", "building", "room", "begin_time",
                     "end_time", "days", "friends"]

  GeneratedList = ["course_number", "section", "title","instructor", 
                     "seats_left", "seats", "building", "room", "begin_time",
                     "end_time", "days", "friends"]

  def generate_course_list
    # u = Scheduler.new("monday,00:01,1:00", ['COP3014', 'COP3252']); u.find_courses_in_slices; u.get_courses
    # builder = Scheduler.new(current_user.preferences.workdays, current_user.user_courses.collect {|uc| uc.tag })
    # builder.find_courses_in_slices
    if current_user.courses.count == 0 
      flash[:alert] = "You must add a course first before you can generate a schedule."
      redirect_to courses_path  and return
    end
    respond_to do |f|
      f.html
      f.json { 
        s = Scheduler.new(current_user.preferences.workdays.split("|"), current_user.courses)
        s.build

        available_course_ids = s.available_courses.collect {|c| c.id }
        @courses = Course.where(:id => available_course_ids)
        json = DatatablesRails::Structify.new(@courses)
        json.formatter do |u|
          course = Course.find(u['id'])
          u['begin_time'] = u['begin_time'].localtime.strftime("%I:%M %p")
          u['end_time'] = u['end_time'].localtime.strftime("%I:%M %p")
          u['instructor'] = course.try(:instructor).try(:name)
          u['friends'] = User.friend_ids_to_names( current_user.find_friends_in_course(u['id']) )
        end
        render :json => json.struct
      }

    end
    # @unavailable_courses = builder.get_unavailable_courses
  end

  # GET /user_courses
  # GET /user_courses.json
  def index
    @user_courses = current_user.user_courses
    @courses = current_user.courses
    respond_to do |format|
      format.html # index.html.haml
      format.json { 

        page = 1

        page = params[:iDisplayStart].to_i/10+1 if params[:iDisplayStart]
        e = @courses
        e = e.joins(:instructor, :college)

        total_record_size = 0
        display_length = 10

        if params[:iDisplayLength].to_i > 10
          display_length = params[:iDisplayLength].to_i
        end

        if params[:iSortCol_0].to_i != 0
          sort_column = DatatableFields[params[:iSortCol_0].to_i]
          sort_direction = params[:sSortDir_0]
          if ["instructor", "college"].include?(sort_column)
            sc = ""
            case sort_column
            when /instructor/
              sc = "instructors.name"
            when /college/
              sc = "colleges.college_tag"
            end
            e = e.order("#{sc} #{sort_direction}")
          else
            e = e.order("#{sort_column} #{sort_direction}")
          end
        end


        if params[:sSearch] != "" && !params[:sSearch].nil?

          search = DatatableFields.collect{|d| "#{d} LIKE '%#{params[:sSearch]}%'" unless ["college","instructor", "add", "friends"].include?(d) }.compact.join(' OR ')
          search2 = " OR instructors.name LIKE '%SUB%' OR colleges.college_tag LIKE '%SUB%' ".gsub('SUB', params[:sSearch])
          search += search2

          e = e.where(search)
          total_record_size = e.count
          e = e.page page
          #logger.info "EQ: " + e.to_sql
        else
          total_record_size = e.count
          e = e.page page
        end


        json = DatatablesRails::Structify.new(e)

        json.formatter do |u|
          course = Course.find(u['id'])
          ucourse = current_user.user_courses.where(:course_id => course.id).first
          u['remove'] = self.class.helpers.link_to "Remove", user_course_path(ucourse), 
            :method => :delete
          u['instructor'] = course.try(:instructor).try(:name)
          u['college'] = course.college.college_tag.upcase
          u['begin_time'] = u['begin_time'].localtime.strftime("%I:%M %p")
          u['end_time'] = u['end_time'].localtime.strftime("%I:%M %p")
          u['friends'] = current_user.find_friends_in_course(u['id']).size
        end
        json_struct = json.struct
        json_struct["iTotalRecords"] = total_record_size
        json_struct["iTotalDisplayRecords"] = total_record_size
        json_struct["sEcho"] = params[:sEcho]
        render :json => json_struct<commit_msg>updates to course controller<commit_after>class UserCoursesController < ApplicationController
  before_filter :require_login!

  DatatableFields = ["remove", "course_number", "section", "title","instructor", 
                     "seats_left", "seats", "building", "room", "begin_time",
                     "end_time", "days", "friends"]

  GeneratedList = ["course_number", "section", "title","instructor", 
                     "seats_left", "seats", "building", "room", "begin_time",
                     "end_time", "days", "friends"]

  def generate_course_list
    # u = Scheduler.new("monday,00:01,1:00", ['COP3014', 'COP3252']); u.find_courses_in_slices; u.get_courses
    # builder = Scheduler.new(current_user.preferences.workdays, current_user.user_courses.collect {|uc| uc.tag })
    # builder.find_courses_in_slices
    if current_user.courses.count == 0 
      flash[:alert] = "You must add a course first before you can generate a schedule."
      redirect_to courses_path  and return
    end
    respond_to do |f|
      f.html
      f.json { 
        s = Scheduler.new(current_user.preferences.workdays.split("|"), current_user.courses)
        s.build

        available_course_ids = s.available_courses.collect {|c| c.id }
        @courses = Course.where(:id => available_course_ids)
        json = DatatablesRails::Structify.new(@courses)
        json.formatter do |u|
          course = Course.find(u['id'])
          u['begin_time'] = u['begin_time'].localtime.strftime("%I:%M %p")
          u['end_time'] = u['end_time'].localtime.strftime("%I:%M %p")
          u['instructor'] = course.try(:instructor).try(:name)
          u['friends'] = User.friend_ids_to_names( current_user.find_friends_in_course(u['id']) )
        end
        render :json => json.struct
      }

    end
    # @unavailable_courses = builder.get_unavailable_courses
  end

  # GET /user_courses
  # GET /user_courses.json
  def index
    @user_courses = current_user.user_courses
    @courses = current_user.courses
    respond_to do |format|
      format.html # index.html.haml
      format.json { 

        page = 1

        page = params[:iDisplayStart].to_i/10+1 if params[:iDisplayStart]
        e = @courses
        e = e.joins(:instructor, :college)

        total_record_size = 0
        display_length = 10

        if params[:iDisplayLength].to_i > 10
          display_length = params[:iDisplayLength].to_i
        end

        if params[:iSortCol_0].to_i != 0
          sort_column = DatatableFields[params[:iSortCol_0].to_i]
          sort_direction = params[:sSortDir_0]
          if ["instructor", "college"].include?(sort_column)
            sc = ""
            case sort_column
            when /instructor/
              sc = "instructors.name"
            when /college/
              sc = "colleges.college_tag"
            end
            e = e.order("#{sc} #{sort_direction}")
          else
            e = e.order("#{sort_column} #{sort_direction}")
          end
        end


        if params[:sSearch] != "" && !params[:sSearch].nil?

          search = DatatableFields.collect{|d| "#{d} LIKE '%#{params[:sSearch]}%'" unless ["college","instructor", "add", "friends"].include?(d) }.compact.join(' OR ')
          search2 = " OR instructors.name LIKE '%SUB%' OR colleges.college_tag LIKE '%SUB%' ".gsub('SUB', params[:sSearch])
          search += search2

          e = e.where(search)
          total_record_size = e.count
          e = e.page page
          #logger.info "EQ: " + e.to_sql
        else
          total_record_size = e.count
          e = e.page page
        end


        json = DatatablesRails::Structify.new(e)

        json.formatter do |u|
          course = Course.find(u['id'])
          ucourse = current_user.user_courses.where(:course_id => course.id).first
          u['remove'] = self.class.helpers.link_to "Remove", user_course_path(ucourse), 
            :method => :delete
          u['course_number'] = self.class.helpers.link_to u['course_number'], course_path(course.id)
          u['instructor'] = course.try(:instructor).try(:name)
          u['college'] = course.college.college_tag.upcase
          u['begin_time'] = u['begin_time'].localtime.strftime("%I:%M %p")
          u['end_time'] = u['end_time'].localtime.strftime("%I:%M %p")
          u['friends'] = current_user.find_friends_in_course(u['id']).size
        end
        json_struct = json.struct
        json_struct["iTotalRecords"] = total_record_size
        json_struct["iTotalDisplayRecords"] = total_record_size
        json_struct["sEcho"] = params[:sEcho]
        render :json => json_struct
{'fraction_non_alphanumeric': 0.13872452333990795, 'fraction_numerical': 0.006793776024545255, 'mean_word_length': 2.514439738159415, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '5439243', 'n_tokens_mistral': 2967, 'n_tokens_neox': 2845, 'n_words': 524}