diff --git a/app/assets/javascripts/tutor_swap.js.coffee b/app/assets/javascripts/tutor_swap.js.coffee new file mode 100644 index 00000000..76156794 --- /dev/null +++ b/app/assets/javascripts/tutor_swap.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/tutor_swap.css.scss b/app/assets/stylesheets/tutor_swap.css.scss new file mode 100644 index 00000000..1a35b74e --- /dev/null +++ b/app/assets/stylesheets/tutor_swap.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the tutor_swap controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/.DS_Store b/app/controllers/.DS_Store new file mode 100644 index 00000000..5008ddfc Binary files /dev/null and b/app/controllers/.DS_Store differ diff --git a/app/controllers/tutor_controller.rb b/app/controllers/tutor_controller.rb index 53324e88..88bfc87e 100644 --- a/app/controllers/tutor_controller.rb +++ b/app/controllers/tutor_controller.rb @@ -1,7 +1,7 @@ class TutorController < ApplicationController #caches_action :schedule, :layout => false - + def schedule prop = Property.get_or_create @tutoring_enabled = prop.tutoring_enabled diff --git a/app/controllers/tutor_swap_controller.rb b/app/controllers/tutor_swap_controller.rb new file mode 100644 index 00000000..6ff2410d --- /dev/null +++ b/app/controllers/tutor_swap_controller.rb @@ -0,0 +1,121 @@ +class TutorSwapController < ApplicationController + before_filter :require_login # check if user is logged in + before_filter :require_tutor # check if logged-in user is a valid tutor + before_filter :setup, :only => [:new, :create] + + def require_login # check if logged in, otherwise redirect + return if logged_in + redirect_to login_path + end + + def logged_in # return whether or not logged in + @current_user + end + + def require_tutor # check if valid tutor, otherwise redirect + return if valid_tutor + redirect_to tutor_path + end + + def valid_tutor # return whether or not logged-in person is valid tutor + Tutor.find_by_person_id(@current_user.id) != nil + end + + def setup # setup list of slots to display when creating swap + @mytutor = Tutor.find_by_person_id(@current_user.id) + + @myslots = [] + Slot.all.each do |s| + if s.tutors.include?(@mytutor) + @myslots << s + end + end + return true + end + + def new # initialize new TutorSwap + setup + @newswap = TutorSwap.new + end + + def create # instantiate new TutorSwap and save + @mytutor = Tutor.find_by_person_id(@current_user.id) + + @myslots = [] + Slot.all.each do |s| + if s.tutors.include?(@mytutor) + @myslots << s + end + end + + @newswap = TutorSwap.new + + swap_slot = Slot.find_by_id(params[:slot][:slot_id]) # slot that is supposed to be swapped + + # check if submitted name is actually a person + @newswap.orig_tutor_id = @mytutor.id # ASSIGN TutorSwap.orig_tutor_id + first = params[:tutor].split(" ")[0] + last = params[:tutor].split(" ")[1] + flash[:notice] = params[:tutor] + new_tutor_person = Person.find_by_first_name_and_last_name(first, last) + unless new_tutor_person + flash[:notice] = "#{first}|#{last}" + # flash[:notice] = "The person you have specified does not exist" + redirect_to :action => :new + return + end + + # check if specified person is a valid tutor + new_tutor = Tutor.find_by_person_id(new_tutor_person.id) + unless new_tutor + flash[:notice] = "The person you have specified is not a tutor" + redirect_to :action => :new + return + end + + # check if specified person is the same as the submitting person + unless new_tutor != @mytutor + flash[:notice] = "You cannot swap tutoring slots with yourself" + redirect_to :action => :new + return + end + + # check if specified tutor has a conflicting slot, i.e. same time + Slot.all.each do |s| + if s.tutors.include?(new_tutor) + if s.wday == swap_slot.wday && s.hour == swap_slot.hour + flash[:notice] = "Tutor has a conflicting slot" + redirect_to :action => :new + return + end + end + end + + @newswap.new_tutor_id = new_tutor.id # ASSIGN TutorSwap.new_tutor_id + + @newswap.slot_id = params[:slot][:slot_id] # ASSIGN TutorSwap.slot_id + + # reformat inputted date string + date = Date.today + begin + date = Date.strptime(params[:date], "%m/%d/%Y") + rescue ArgumentError + flash[:notice] = "Date is formatted incorrectly" + redirect_to :action => :new + return + end + + @newswap.swap_date = date # ASSIGN TutorSwap.swap_date + + @newswap.save # SAVE TutorSwap + + # finish + flash[:notice] = "Swap submitted!" + redirect_to :action => :new + end + + def to_s + "TutorSwap #{orig_tutor_id} #{new_tutor_id} #{slot_id}" + end + +end diff --git a/app/helpers/tutor_swap_helper.rb b/app/helpers/tutor_swap_helper.rb new file mode 100644 index 00000000..f0e2d90f --- /dev/null +++ b/app/helpers/tutor_swap_helper.rb @@ -0,0 +1,2 @@ +module TutorSwapHelper +end diff --git a/app/models/event.rb b/app/models/event.rb index 1f33ead1..0d534b84 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -94,9 +94,9 @@ def nice_time_range(year = false) def can_view? user if user.nil? - view_permission_group.nil? and Event.current.include? self + view_permission_group.nil? else - (view_permission_group.nil? or user.groups.include? view_permission_group) and Event.current.include? self + view_permission_group.nil? or user.groups.include? view_permission_group end end diff --git a/app/models/tutor_swap.rb b/app/models/tutor_swap.rb new file mode 100644 index 00000000..fa8aca03 --- /dev/null +++ b/app/models/tutor_swap.rb @@ -0,0 +1,32 @@ +class TutorSwap < ActiveRecord::Base + + # === List of columns === + # id : integer + # slot_id : integer + # created_at : datetime + # updated_at : datetime + # orig_tutor_id : integer + # new_tutor_id : integer + # swap_date : date + # ======================= + + has_many :tutors + has_one :slot + + # validates :tutors, :presence => true + # validates :tutors, length: { is: 2 } + # validates :slot, :presence => true + + # validate :check_tutors + + TUTOR_ERROR = "swapping tutors cannot be the same tutor" + + # checks to see if the two tutors are different + def check_tutors(tutors) + my_tutors = self.tutors + if my_tutors[0] == my_tutors[1] then + errors[:tutor] << TUTOR_ERROR + end + end + +end diff --git a/app/views/people/show.html.erb b/app/views/people/show.html.erb index 5f633f6a..0c84c7ab 100644 --- a/app/views/people/show.html.erb +++ b/app/views/people/show.html.erb @@ -54,7 +54,7 @@ <% if @person.rsvps.count > 0 %>
<%= @tutoring_message %>
-<% end %> +<% end %> \ No newline at end of file diff --git a/app/views/tutor_swap/new.html.erb b/app/views/tutor_swap/new.html.erb new file mode 100644 index 00000000..4db7f9ae --- /dev/null +++ b/app/views/tutor_swap/new.html.erb @@ -0,0 +1,51 @@ +<% content_for :header do %> + <%= javascript_include_tag "jquery-autocomplete.min" %> + <%= stylesheet_link_tag "jquery-autocomplete" %> +<%- end # header -%> + +