Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

Commit

Permalink
lots of code changes for the cseducation week questions. NO DELTAS. D…
Browse files Browse the repository at this point in the history
…O NOT USE
  • Loading branch information
mhseiden committed Nov 28, 2012
1 parent d629b3e commit abfa3ef
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Ignore some config files
facebook.yml
smtp.yml
cseducation_questions.yml

# Ignore all assets. These are compiled by the developer
public/assets
Expand All @@ -23,3 +24,4 @@ public/assets
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp

Binary file modified app/assets/.DS_Store
Binary file not shown.
Binary file added app/assets/images/cseducation_banner.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions app/assets/stylesheets/cseducation.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@import "variables";

.cse-banner {
height: 210px;
width: 700px;
margin: 0px auto;
display: block;
}

h3.heading {
margin-top: 20px;
}

.link-section {
margin: 20px 0px 100px 0px;

h2 {
font-size: 20px;
}

a:visited {
color: #000000;
}

a:hover {
color: $hackers-orange;
}

}

.question-id, .question-text {
margin-bottom: 15px;
}

.leaderboard {
margin-bottom: 100px;

h3 {
margin-bottom: 15px;
}

p {
margin-bottom: 5px;
}
}
1 change: 1 addition & 0 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class EventsController < ApplicationController
def index
@upcoming_events = Event.upcoming_events
@past_events = Event.past_events
pp @past_events
end

def show_sign_in
Expand Down
34 changes: 34 additions & 0 deletions app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class StaticPagesController < ApplicationController

CS_EDU_QS = YAML.load_file(Rails.root.join("config/cseducation_questions.yml"))


def home
@upcoming_events = Event.upcoming_events.slice(0,2)
end
Expand All @@ -10,4 +13,35 @@ def error
def sponsors
end

def cseducation
# Nothing to do if the main page is hit or the qid is invalid
return if params[:qid].nil?

# Render the "cseducation_question" page if the qid is valid
render "cseducation_question" unless (@question = CS_EDU_QS[params[:qid]]).nil?
end

def cseducation_submit
end

def cseducation_process
candidate = params[:question]
question = CS_EDU_QS[candidate[:qid]]

# Redirect if the conditions are not met
redirect_to cs_edu_submit_path and return if question.nil?
redirect_to cs_edu_submit_path and return if question["ans"] != candidate[:answer]

# Create a new answer

# Save the answer and redirect back to the leader board
CseducationAnswer.persist_answer candidate, question
redirect_to cs_edu_leaderboard_path and return
end

def cseducation_leaderboard
@leaderboard = CseducationAnswer.get_leaderboard
pp @leaderboard
end

end
41 changes: 41 additions & 0 deletions app/models/cseducation_answer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class CseducationAnswer < ActiveRecord::Base

# The number of trivia questions
NUM_QUESTIONS = 4

attr_accessible :uniqname, :value, :qid

def self.persist_answer(cand, ques)
# If the user has already answered, do not double count
return unless CseducationAnswer.find_by_uniqname_and_qid(cand[:uniqname], cand[:qid]).nil?
CseducationAnswer.new({uniqname: cand[:uniqname], value: ques["val"], qid: cand[:qid]}).save
end

def self.get_leaderboard()

# Build the ordered results array
results = CseducationAnswer.sum(:value, group: :uniqname).to_a.sort do |e1, e2|
# Check if the sums are equal
if e1[1] == e2[1]
# Use the string ordering
e2[0] <=> e1[0]
else
# Use the number of ones to sort
e1_count = e1[1].to_s.split("").count(1)
e2_count = e2[1].to_s.split("").count(1)
# Use the one with the most ones, else the one with the higher value
e1_count == e2_count ? e2[1] <=> e1[1] : e2_count <=> e1_count
end
end

# Map the question numbers into question answered - unanswered values
results.map do |e|
# Split the number into a char array
e_new = e[1].to_s.split("").reverse
e_new.concat(Array.new(NUM_QUESTIONS - e_new.count, 0)) if e_new.count < NUM_QUESTIONS
e[1] = e_new.map {|e| e == "1" }
e
end
end

end
2 changes: 1 addition & 1 deletion app/views/events/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
.event-block
%h2.text-large= "Past Events"
%ul
- @past_events.each do |event|
- @past_events.reverse.each do |event|
= render partial: "events/event_li", locals: {event: event}
5 changes: 4 additions & 1 deletion app/views/static_pages/_home_events.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
= render partial: "events/event_li", locals: {event: event, display: :preview}
%span.see-more
%a{href: "#{events_url}"}
%i= "See More &rArr;".html_safe
- if @upcoming_events.any?
%i= "See More &rArr;".html_safe
- else
%i= "Check Out Past Events &rArr;".html_safe
15 changes: 15 additions & 0 deletions app/views/static_pages/cseducation.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- css_for_head "cseducation"

%img.cse-banner{src: "/assets/images/cseducation_banner.jpg"}

%h3.heading.center= "This year's CSEducation Week Puzzles are sponsored by Michigan Hackers!"
%p.center= "To compete, find the 4 puzzles, submit your answers, and score points, all before December 7th."

.link-section
.col1.spacer
%h2
%a.col3{href: "/cseducation/question1"}= "Question One"
%h2
%a.col3{href: "/cseducation/submit"}= "Submit Answers"
%h2
%a.col3{href: "/cseducation/leaderboard"}= "Leaderboard"
12 changes: 12 additions & 0 deletions app/views/static_pages/cseducation_leaderboard.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- css_for_head "cseducation"

.leaderboard
%h3.col2= "Uniqname"
%h3.col2= "Question 1"
%h3.col2= "Question 2"
%h3.col2= "Question 3"
%h3.col2= "Question 4"
- @leaderboard.each do |elem|
%p.col2= elem[0]
- elem[1].each do |is_solved|
%p.col2= is_solved ? "Solved" : "----------"
7 changes: 7 additions & 0 deletions app/views/static_pages/cseducation_question.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- css_for_head "cseducation"

.col10.question-id
%strong= "Question ID"
= "#{@question["qid"]}"
.col10.question-text
%p= @question["txt"]
15 changes: 15 additions & 0 deletions app/views/static_pages/cseducation_submit.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
= form_tag cs_edu_process_path, method: :post do
.row
.col4
%fieldset
= label :question, :uniqname, class: "line-break"
= text_field :question, :uniqname
%fieldset
= label :question, :qid, "Question ID", class: "line-break"
= text_field :question, :qid
%fieldset
= label :question, :answer, class: "line-break"
= password_field :question, :answer
%fieldset
%input{type: :submit, value: "Submit Your Answer"}
.col6
7 changes: 2 additions & 5 deletions app/views/static_pages/home.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
.row.hero-pane
.col10
%p.text-large.center
%strong MHacks:Impact
is this weekend!
%h5.center{style:"font-weight:normal;font-size:12px;"}
%a{href: "http://mhacks.org", style: "color:#000;"}
%i visit mhacks.org to register
A community for all
%strong Hackers @ Michigan

.row.second-pane
.col5
Expand Down
11 changes: 10 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@
post "/events/signin/:eid" => "events#record_sign_in", as: :record_event_sign_in
resources :events, only: [:index]

# CSEducation Week Routing
get "/cseducation/submit" => "static_pages#cseducation_submit", as: :cs_edu_submit
post "/cseducation/submit" => "static_pages#cseducation_process", as: :cs_edu_process
get "/cseducation/leaderboard" => "static_pages#cseducation_leaderboard", as: :cs_edu_leaderboard

# Needs to go after the static routing
get "/cseducation/(:qid)" => "static_pages#cseducation", as: :cs_edu



# Resources
# TODO - Rename or repurpose this
resources :resources

# Users
get "/members/activate/:confirmation_hash" => "users#activate_user", :as => :activate_user
get "/members/activate/:confirmation_hash" => "users#activate_user", as: :activate_user
resources :users, path: :members, except: [:create, :new]

# No-Auth User Details
Expand Down
1 change: 1 addition & 0 deletions lib/umich_ldap/umich_ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def self.get_name_parts_by_uniqname(uniqname)
first_name: entry.givenName.any? ? entry.givenName.first : "",
last_name: entry.sn.any? ? entry.sn.first : ""
}

rescue Exception => e
puts e
{first_name: "", last_name: ""}
Expand Down

0 comments on commit abfa3ef

Please sign in to comment.