Created
April 20, 2010 16:29
-
-
Save dinjas/1939eb902d15f0aa98b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CoursesController < ApplicationController | |
require "rexml/document" | |
require "net/http" | |
require "multipart" | |
include REXML | |
before_filter :check_login, :only => ['new', 'create', 'admin_pub'] | |
before_filter :login_required, :except => ['index', 'list', 'search', 'show'] | |
before_filter :check_permissions, :only => ['edit', 'update'] | |
before_filter :check_admin, :only => ['destroy'] | |
# Declare exception to handler methods | |
rescue_from ActiveRecord::RecordNotFound, :with => :bad_record | |
rescue_from NoMethodError, :with => :show_error | |
def bad_record; render :file => 'public/_course_404.html.erb', :status => 404, :layout => 'application'; end | |
def show_error(exception); render :text => exception.message; end | |
def index | |
@courses = Course.find(:all) | |
session[:return_to] = request.request_uri # kind of an ugly fix for redirecting back to a page after logging in/out | |
end | |
def approve | |
@course = Course.find(params[:id]) | |
@course.approved = '1' | |
if @course.save | |
flash[:notice] = 'Successfully approved course.' | |
else | |
flash[:error] = 'Unable to save course approval.' | |
end | |
redirect_to admin_path | |
end | |
def list | |
@courses = Course.paginate :all, :page => params[:page], :order => 'name' | |
session[:return_to] = request.request_uri | |
respond_to do |format| | |
format.html # list.html.erb | |
format.xml { render :xml => @courses } | |
end | |
end | |
def search | |
per_page = 10 | |
r = params[:q].split(' ') | |
q = "" | |
r.each do |s| | |
if (s != '*' && s.casecmp("OR") != 0 && s.casecmp("AND") != 0) | |
s = '*' + s + '*' | |
end | |
q << ' ' << s | |
end | |
@courses = Course.find_with_ferret(q, {:page => params[:page], :per_page => per_page}, {:order => 'name'}) | |
respond_to do |format| | |
format.html # search.html.erb | |
format.xml { render :xml => @courses } | |
end | |
end | |
def show | |
@course = Course.find(params[:id]) | |
@pic = @course.picture | |
@comments = @course.course_comments | |
session[:return_to] = request.request_uri | |
if @course # && @course.published || (logged_in? && current_user.has_role?('administrator')) | |
respond_to do |format| | |
format.html # show.html.erb | |
format.xml { render :xml => @course } | |
end | |
else | |
render :file => "#{RAILS_ROOT}/public/404.html"#, :status => 404 and return | |
end | |
end | |
def new | |
@course = Course.new | |
end | |
def edit | |
@course = Course.find(params[:id]) | |
end | |
def create | |
@course = Course.new(params[:course]) | |
pic = params[:picture] | |
if (pic[:uploaded_data] != nil) | |
@picture = @course.build_picture(params[:picture]) | |
if @picture != nil | |
@course.picture = @picture | |
else | |
@course.picture = nil | |
end | |
else | |
@course.picture = nil | |
end | |
if @course.save | |
flash[:notice] = 'Successfully created course.' | |
#p = publish | |
redirect_to :action => 'show', :id => @course | |
else | |
flash[:error] = "Unable to save course." | |
render :action => 'new' | |
end | |
end | |
def update | |
@course = Course.find(params[:id]) | |
if @course.update_attributes(params[:course]) | |
if !params[:picture][:uploaded_data].blank? | |
if @course.picture | |
@course.picture.destroy | |
end | |
@course.picture = Picture.new(params[:picture]) | |
end | |
flash[:notice] = 'Course was successfully updated.' | |
redirect_to :action => 'show', :id => @course | |
else | |
render :action => 'edit' | |
end | |
end | |
def destroy | |
Course.destroy(params[:id]) | |
flash[:notice] = "Successfully deleted course" | |
redirect_to courses_path | |
end | |
def admin_pub | |
@course = Course.find(params[:id]) | |
if @course.published == true | |
flash[:error] = 'Course is already published.' | |
else | |
map_id = "1160" # the id of the map to upload content to | |
course_url = "http://folfing.com/courses/" + @course.id.to_s # url for the folfing course | |
@course.published = 1 | |
if @course.name.length > 23 | |
name = @course.name[0..19] + "..." | |
else | |
name = @course.name | |
end | |
if $OAUTH_ACCESS_TOKEN == nil | |
if current_user.has_role?('administrator') | |
flash[:error] = 'OAuth Access token is invalid. Re-OAuthorize' | |
else | |
flash[:error] = "We are unable to publish your course to <i>folfing's</i> main map at this time.<br/> | |
Your course has been added to the listings and we will add it to the home page map as soon as possible." | |
end | |
else | |
url = $OAUTH_URL + '/api/map_item/add' | |
@response = $OAUTH_ACCESS_TOKEN.post(url, {:title=>name, :map_id=>map_id, :lat=>@course.latitude, :lon=>@course.longitude, | |
:display_title=>'false', :cluster=>'true', :display_icon =>'true', :display_id=>'4008'}) | |
if @response != nil | |
doc = Document.new @response.body | |
if (doc != nil && doc.root != nil && doc.root.name != nil) | |
root = doc.root.name | |
if root.eql?("success") | |
item_id = doc.root.get_elements('item_id')[0].text.to_s | |
# add a thumbnail component | |
url = URI.parse $OAUTH_URL + '/api/map_component/file/add' | |
req = Net::HTTP::Post.new( url.path ) | |
file = Net::HTTP::FileForPost.new("#{RAILS_ROOT}" + "/public"+ @course.picture.public_filename, @course.picture.content_type) | |
req.set_multipart_data( {:file => file}, | |
{:map_id => map_id, :item_id => item_id} ) | |
$OAUTH_ACCESS_TOKEN.sign!( req ) | |
@response = Net::HTTP.new(url.host, url.port).start do |http| | |
http.request(req) | |
end | |
# add a link component | |
url = URI.parse $OAUTH_URL + '/api/map_component/link/add' | |
req = Net::HTTP::Post.new( url.path ) | |
link_url = URI.parse "http://folfing.com/courses/" + @course.id.to_s | |
req.set_form_data( {:map_id => map_id, :item_id => item_id, :url => link_url, :title => @course.name} ) | |
$OAUTH_ACCESS_TOKEN.sign!( req ) | |
@response = Net::HTTP.new(url.host, url.port).start do |http| | |
http.request(req) | |
end | |
url = URI.parse $OAUTH_URL + '/api/map_component/text/add' | |
req = Net::HTTP::Post.new( url.path ) | |
req.set_form_data( {:map_id => map_id, :item_id => item_id, :title => "Description:", :description=>@course.description} ) | |
$OAUTH_ACCESS_TOKEN.sign!( req ) | |
@response = Net::HTTP.new(url.host, url.port).start do |http| | |
http.request(req) | |
end | |
if @course.save(false) | |
flash[:notice] = 'Successfully published "' + @course.name + '".' | |
else | |
flash[:error] = 'Unable to publish "' + @course.name + '".' | |
end | |
elsif root.eql?("error") | |
flash[:error] = 'Unable to publish "' + @course.name + '". Error response from server.' | |
puts $OAUTH_ACCESS_TOKEN.inspect | |
end | |
else | |
flash[:error] = 'Unable to publish "' + @course.name + '". Invalid response from server.' | |
end | |
end | |
end | |
end | |
#return 0 | |
redirect_to admin_path | |
end | |
protected | |
def check_permissions | |
@course = Course.find(params[:id]) | |
if !logged_in? || (current_user.id != @course.creator && !current_user.has_role?('administrator')) | |
flash[:error] = 'You are not authorized to perform this action.' | |
respond_to do |format| | |
format.html { redirect_to root_path } | |
format.xml { head :ok } | |
end | |
end | |
end | |
def check_admin | |
if !(logged_in? && current_user.has_role?('administrator')) | |
flash[:error] = 'You are not authorized to perform this action.' | |
respond_to do |format| | |
format.html { redirect_to root_path } | |
format.xml { head :ok } | |
end | |
end | |
end | |
def check_login | |
if !logged_in? | |
flash[:error] = 'You must have an account and be logged in to create a course.<br /> | |
You will also need to have a user account at <a href="http://mapwith.us">MapWith.Us</a> | |
in order to create a map of your course.' | |
respond_to do |format| | |
format.html { redirect_to login_path } | |
format.xml { head :ok } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment