<commit_before>class RegistrationsController < ApplicationController
  before_filter :authenticated_as_user, :only => [:new, :create, :edit, :update, :new_or_edit]
  before_filter :authenticated_as_admin, :only => [:index, :show, :destroy]
   
  # GET /registrations
  def index
    @prerequisites = Prerequisite.all
    @registrations = Registration.all
    @leaders = User.find(:all, :conditions => {:admin => true}, :include => :profile)

    respond_to do |format|
      format.html # index.html.erb
    end
  end

  # GET /registrations/1
  def show
    @registration = Registration.find(params[:id])
    @recitation_conflicts =
        @registration.recitation_conflicts.index_by(&:timeslot)

    respond_to do |format|
      format.html
    end
  end

  def new_edit
    prepare_for_editing

    # Disallow random record updates.
    unless @registration.can_edit? current_user
      notice[:error] = 'That is not yours to play with! Attempt logged.'
      redirect_to root_path
      return
    end
    
    respond_to do |format|
      format.html { render :action => :new_edit }
      format.js   { render :action => :edit }
    end    
  end
  private :new_edit
  
  def prepare_for_editing
    @prerequisites = @registration.prerequisite_answers.
                                   index_by(&:prerequisite_id)
    Prerequisite.all.each do |prereq|
      next if @prerequisites.has_key? prereq.id
      @prerequisites[prereq.id] =
          @registration.prerequisite_answers.build(:prerequisite => prereq)
    end

    @recitation_conflicts =
        @registration.recitation_conflicts.index_by &:timeslot
  end
  private :prepare_for_editing
    
  # GET /registrations/new
  def new
    user = User.find params[:user_id]
    course = Course.find params[:course_id]
    @registration = Registration.new :user => user, :course => course
    new_edit
  end

  # GET /registrations/1/edit
  def edit
    @registration = Registration.find(params[:id])
    new_edit
  end
  
  def create_update
    #logger.info "---------------------------------"
    unless @registration.can_edit? current_user
      # Disallow random record updates.
      notice[:error] = 'That is not yours to play with! Your attempt has been logged.'
      redirect_to root_path
      return
    end
    
    if Course.main.has_recitations?
      old_recitation_conflicts =
          @registration.recitation_conflicts.index_by &:timeslot
    end
    
    #  # Update recitation conflicts.
    #  params[:recitation_conflicts].each_value do |rc|
    #    next if rc[:class_name].blank?
    #    timeslot = rc[:timeslot].to_i
    #    if old_recitation_conflicts.has_key? timeslot
    #      old_recitation_conflicts.delete(timeslot).update_attributes rc
    #    else
    #      rc[:registration] = @registration
    #      conflict = RecitationConflict.new(rc)
    #      @registration.recitation_conflicts << conflict
    #    end
    #  end
    #  # Wipe cleared conflicts.
    #  old_recitation_conflicts.each_value { |orc| @registration.recitation_conflicts.delete orc }
    #end
    
    if @new_record = @registration.new_record?
      success = @registration.save
    else
      # recitation_section is attr_protected, set manually
      if params.has_key? :recitation_section
        @registration.recitation_section = RecitationSection.where(:serial => params[:recitation_section][:serial]).first
        success = @registration.save
      else
        # Disallow structural changes to the record.
        params[:registration].delete :user_id
        params[:registration].delete :course_id
        success = @registration.update_attributes params[:registration]
      end
      
    end<commit_msg>adding in recitation_conflict processing back into the registrations controller<commit_after>class RegistrationsController < ApplicationController
  before_filter :authenticated_as_user, :only => [:new, :create, :edit, :update, :new_or_edit]
  before_filter :authenticated_as_admin, :only => [:index, :show, :destroy]
   
  # GET /registrations
  def index
    @prerequisites = Prerequisite.all
    @registrations = Registration.all
    @leaders = User.find(:all, :conditions => {:admin => true}, :include => :profile)

    respond_to do |format|
      format.html # index.html.erb
    end
  end

  # GET /registrations/1
  def show
    @registration = Registration.find(params[:id])
    @recitation_conflicts =
        @registration.recitation_conflicts.index_by(&:timeslot)

    respond_to do |format|
      format.html
    end
  end

  def new_edit
    prepare_for_editing

    # Disallow random record updates.
    unless @registration.can_edit? current_user
      notice[:error] = 'That is not yours to play with! Attempt logged.'
      redirect_to root_path
      return
    end
    
    respond_to do |format|
      format.html { render :action => :new_edit }
      format.js   { render :action => :edit }
    end    
  end
  private :new_edit
  
  def prepare_for_editing
    @prerequisites = @registration.prerequisite_answers.
                                   index_by(&:prerequisite_id)
    Prerequisite.all.each do |prereq|
      next if @prerequisites.has_key? prereq.id
      @prerequisites[prereq.id] =
          @registration.prerequisite_answers.build(:prerequisite => prereq)
    end

    @recitation_conflicts =
        @registration.recitation_conflicts.index_by &:timeslot
  end
  private :prepare_for_editing
    
  # GET /registrations/new
  def new
    user = User.find params[:user_id]
    course = Course.find params[:course_id]
    @registration = Registration.new :user => user, :course => course
    new_edit
  end

  # GET /registrations/1/edit
  def edit
    @registration = Registration.find(params[:id])
    new_edit
  end
  
  def create_update
    #logger.info "---------------------------------"
    unless @registration.can_edit? current_user
      # Disallow random record updates.
      notice[:error] = 'That is not yours to play with! Your attempt has been logged.'
      redirect_to root_path
      return
    end
    
    if Course.main.has_recitations?
      old_recitation_conflicts =
          @registration.recitation_conflicts.index_by &:timeslot
    
      # Update recitation conflicts.
      params[:recitation_conflicts].each_value do |rc|
        next if rc[:class_name].blank?
        timeslot = rc[:timeslot].to_i
        if old_recitation_conflicts.has_key? timeslot
          old_recitation_conflicts.delete(timeslot).update_attributes rc
        else
          rc[:registration] = @registration
          conflict = RecitationConflict.new(rc)
          @registration.recitation_conflicts << conflict
        end
      end
      # Wipe cleared conflicts.
      old_recitation_conflicts.each_value { |orc| @registration.recitation_conflicts.delete orc }
    end
    
    if @new_record = @registration.new_record?
      success = @registration.save
    else
      # recitation_section is attr_protected, set manually
      if params.has_key? :recitation_section
        @registration.recitation_section = RecitationSection.where(:serial => params[:recitation_section][:serial]).first
        success = @registration.save
      elsif params.has_key? :recitation_conflicts
      else
        # Disallow structural changes to the record.
        params[:registration].delete :user_id
        params[:registration].delete :course_id
        success = @registration.update_attributes params[:registration]
      end
      
    end
{'fraction_non_alphanumeric': 0.09554395824969891, 'fraction_numerical': 0.0005352602703064365, 'mean_word_length': 2.813265306122449, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1073410', 'n_tokens_mistral': 2313, 'n_tokens_neox': 2177, 'n_words': 491}