Skip to content

Committee Project - Tutor Swap #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/assets/javascripts/tutor_swap.js.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/tutor_swap.css.scss
Original file line number Diff line number Diff line change
@@ -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/
Binary file added app/controllers/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion app/controllers/tutor_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class TutorController < ApplicationController

#caches_action :schedule, :layout => false

def schedule
prop = Property.get_or_create
@tutoring_enabled = prop.tutoring_enabled
Expand Down
121 changes: 121 additions & 0 deletions app/controllers/tutor_swap_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/helpers/tutor_swap_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TutorSwapHelper
end
4 changes: 2 additions & 2 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions app/models/tutor_swap.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/views/people/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<% if @person.rsvps.count > 0 %>
<h2><%= @person.fullname %>'s RSVPs</h2>
<ul>
<% @person.rsvps.ordered_desc.each do |rsvp| %>
<% @person.rsvps.ordered_desc.limit(10).each do |rsvp| %>
<% if rsvp.event.can_view? @current_user %>
<li><%= link_to "#{rsvp.event.start_date} - #{rsvp.event.name}", rsvp.event %></li>
<% end %>
Expand Down
6 changes: 6 additions & 0 deletions app/views/slots/_slot.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<% if slot.tutors.any? %>
<% slot.tutors.each do |tutor| %>

<% swap = TutorSwap.find_by_orig_tutor_id_and_slot_id(tutor.id, slot.id) %>
<% dates = (Date.today .. (Date.today + 6)) %>
<% if swap && dates.include?(swap.swap_date) %>
<% tutor = Tutor.find_by_id(swap.new_tutor_id) %>
<% end %>

<div title="<%= tutor.person.fullname %>" id="tutor-<%= slot.id %>-<%= tutor.id %>" class="person
<% for pref in tutor.course_preferences %>
<%= pref.course.course_abbr + '_' + pref.level.to_s %>
Expand Down
39 changes: 22 additions & 17 deletions app/views/tutor/schedule.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,33 @@
</div>

<div style="float: left; max-width: 18%; padding-left: 2%; padding-top: 70px; line-height: 1.75em;">
<div class="small">
<% courses = CoursePreference.all_courses(tutors.uniq) %>
<% for key in courses.keys %>
<span class="dept-heading"><%= key %></span>
<% for course in courses[key] %>
<% c = "'" + key + course + "'" %>
<a href="#" class=<%= c %> onmouseover="highlight(<%= c %>)" onmouseout="unhighlight(<%= c %>)"
onclick="return locklight(<%= c %>)"><%= course %></a>
<div class="small">
<% courses = CoursePreference.all_courses(tutors.uniq) %>
<% for key in courses.keys %>
<span class="dept-heading"><%= key %></span>
<% for course in courses[key] %>
<% c = "'" + key + course + "'" %>
<a href="#" class=<%= c %> onmouseover="highlight(<%= c %>)" onmouseout="unhighlight(<%= c %>)"
onclick="return locklight(<%= c %>)"><%= course %></a>
<%= ', ' unless course == courses[key].last %>
<% end %>
<br/>
<% end %>
<br/>
<% end %>
<br/>
<% end %>
<br/>

<div class="legend-item legend-pref"></div><span class="small">= preferred</span><br/>
<div class="legend-item legend-comp"></div><span class="small">= completed</span><br/>
<div class="legend-item legend-curr"></div><span class="small">= in progress</span><br/>
<div class="legend-item legend-pref"></div><span class="small">= preferred</span><br/>
<div class="legend-item legend-comp"></div><span class="small">= completed</span><br/>
<div class="legend-item legend-curr"></div><span class="small">= in progress</span><br/>

</div>
</div>

<br/>
<div class="swap_button">
<%= button_to "SWAP SLOT", tutor_swap_new_path, :class => "swap", :method => :get %>
</div>
</div>
<div style="clear: both; height: 150px"></div>
<%# End tutoring enabled %>
<% else %>
<p><%= @tutoring_message %></p>
<% end %>
<% end %>
51 changes: 51 additions & 0 deletions app/views/tutor_swap/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<% content_for :header do %>
<%= javascript_include_tag "jquery-autocomplete.min" %>
<%= stylesheet_link_tag "jquery-autocomplete" %>
<%- end # header -%>

<h1>Swapping Tutoring Slot</h1>

<div id="tutor_swap">

<%= form_for @newswap, url: {action: "create"} do |s| %>

<div id="autocomplete-0">
<%= label_tag "New tutor (e.g. Bobby Tutor):"%>
<br>
<%= text_field_tag (l="tutor"), "", :id => l%>
<%= hidden_field_tag (l="tutor_hidden"), params[l], :id => l %>
</div>

<br>

<div id="autocomplete-1">
<%= label_tag "Slot:"%>
<br>
<%- @myslots.each_with_index do |s, i| -%>
<div>
<%= radio_button :slot, :slot_id, s.id%>
<% hour = s.hour % 12 %>
<% if hour == 0 %>
<% hour = 12 %>
<% end %>
<%= label_tag "#{Date.strptime(s.wday.to_s, "%w").strftime("%A")}, #{hour}-#{(hour + 1) % 12} pm"%>
</div>
<%- end -%>
</div>

<br>

<div id="autocomplete-2">
<%= label_tag "Date (e.g. 07/04/1776):"%>
<br>
<%= text_field_tag (l="date"), "", :id => l%>
<%= hidden_field_tag (l="date_hidden"), params[l], :id => l %>
</div>

<br>

<%= s.submit 'SUBMIT SWAP'%>

<%- end -%>

</div>
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@
match "calendar" => "tutor#calendar"
end


# EDIT: route /tutor/swap.html to swap.html.erb in app/views/tutor_swap/
scope "tutor_swap" do
get "new" => "tutor_swap#new", :as => :tutor_swap_new
post "create" => "tutor_swap#create", :as => :tutor_swap_create
end

# Exams
scope "exams" do
match '/' => "exams#index",
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20131124004300_create_tutor_swaps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateTutorSwaps < ActiveRecord::Migration
def change
create_table :tutor_swaps do |t|
t.references :tutors
t.references :slot

t.timestamps
end
add_index :tutor_swaps, :tutors_id
add_index :tutor_swaps, :slot_id
end
end
5 changes: 5 additions & 0 deletions db/migrate/20131129011008_add_orig_tutor_id_to_tutor_swap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddOrigTutorIdToTutorSwap < ActiveRecord::Migration
def change
add_column :tutor_swaps, :orig_tutor_id, :integer
end
end
5 changes: 5 additions & 0 deletions db/migrate/20131129011034_add_new_tutor_id_to_tutor_swap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddNewTutorIdToTutorSwap < ActiveRecord::Migration
def change
add_column :tutor_swaps, :new_tutor_id, :integer
end
end
5 changes: 5 additions & 0 deletions db/migrate/20131129011122_add_swap_date_to_tutor_swap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSwapDateToTutorSwap < ActiveRecord::Migration
def change
add_column :tutor_swaps, :swap_date, :date
end
end
9 changes: 9 additions & 0 deletions db/migrate/20131129044108_remove_tutors_id_from_tutor_swap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class RemoveTutorsIdFromTutorSwap < ActiveRecord::Migration
def up
remove_column :tutor_swaps, :tutors_id
end

def down
add_column :tutor_swaps, :tutors_id, :integer
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130519233001) do
ActiveRecord::Schema.define(:version => 20131129044108) do

create_table "alumnis", :force => true do |t|
t.string "grad_semester"
Expand Down Expand Up @@ -502,6 +502,17 @@
t.integer "keyword", :default => 0
end

create_table "tutor_swaps", :force => true do |t|
t.integer "slot_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "orig_tutor_id"
t.integer "new_tutor_id"
t.date "swap_date"
end

add_index "tutor_swaps", ["slot_id"], :name => "index_tutor_swaps_on_slot_id"

create_table "tutors", :force => true do |t|
t.integer "person_id", :null => false
t.string "languages"
Expand Down
Loading