require 'oauth2' require 'json' class SurveysController < ApplicationController before_action :authenticate_oauth2, except: :callback #respond_to :html # List all surveys def home @surveys1 = Survey.all @surveys2 = Survey.all.includes(:answers).where( promo2016: true, answers: { uid: @user["uid"] } ) /case @user["promo"] when 2016 @surveys1 = Survey.all.where( promo2016: true).includes(:answers).where.not( :answers => { :uid => @user["uid"] } ) @surveys2 = Survey.all.includes(:answers).where( promo2016: true, answers: { uid: @user["uid"] } ) when 2017 @surveys1 = Survey.all.where( promo2017: true).includes(:answers).where.not( :answers => { :uid => @user["uid"] } ) @surveys2 = Survey.all.includes(:answers).where( promo2017: true, answers: { uid: @user["uid"] } ) when 2018 @surveys1 = Survey.all.where( promo2018: true).includes(:answers).where.not( :answers => { :uid => @user["uid"] } ) @surveys2 = Survey.all.includes(:answers).where( promo2018: true, answers: { uid: @user["uid"] } ) end/ end # Show a single survey by id : GET /surveys/3 def show @survey = Survey.find( params[:id] ) end # List surveys created by the user def created case @user["promo"] when 2016 @surveys = Survey.all.where( promo2016: true, creator_uid: @user["uid"] ) when 2017 @surveys = Survey.all.where( promo2017: true, creator_uid: @user["uid"] ) when 2018 @surveys = Survey.all.where( promo2018: true, creator_uid: @user["uid"] ) end end def my_answers case @user["promo"] when 2016 @surveys = Survey.all.includes(:answers).where( promo2016: true, answers: { uid: @user["uid"] } ) when 2017 @surveys = Survey.all.includes(:answers).where( promo2017: true, answers: { uid: @user["uid"] } ) when 2018 @surveys = Survey.all.includes(:answers).where( promo2018: true, answers: { uid: @user["uid"] } ) end end def to_respond case @user["promo"] when 2016 @surveys = Survey.all.where( promo2016: true).includes(:answers).where.not( answers: { uid: @user["uid"] } ) when 2017 @surveys = Survey.all.where( promo2017: true).includes(:answers).where.not( answers: { uid: @user["uid"] } ) when 2018 @surveys = Survey.all.where( promo2018: true).includes(:answers).where.not( answers: { uid: @user["uid"] } ) end end def respond @survey = Survey.find( params[:id] ) end # Show a new survey form def new @survey = Survey.new 2.times { @survey.questions.build } end # Create a new survey : POST /surveys def create @survey = Survey.new(survey_params) @survey.creator_uid = @user["uid"] respond_to do |format| if @survey.save format.html { redirect_to root_path, notice: 'Survey was successfully created.' } format.json { render json: root_path, status: :created, location: @survey } else puts @survey.errors.full_messages format.html { render action: "new" } format.json { render json: @survey.errors, status: :unprocessable_entity } end end end def edit @survey.questions.build end def add @survey = Survey.find(params[:id]) params.each do |p| puts p a = Answer.new a.text = p.text a.uid = @user["uid"] a.question = p.question_id a.save end end # Update a survey : PUT /surveys/3 def update @survey = Survey.find(params[:id]) @survey.update_attributes(params[:survey]) @survey.questions.build end # Destroy a survey by id : DELETE /surveys/3 def destroy @survey = Survey.find( params[:id] ) respond_to do |format| if @survey.destroy format.html { redirect_to root_path, notice: 'Survey was successfully deleted.' } format.js { Materialize.toast('I am a toast!', 4000) } else puts @survey.errors.full_messages format.html { render action: "destroy" } end end end def callback set_oauth if params[:error] != nil redirect_to "https://ares-ensiie.eu/" else puts params[:code] access_token = @oauth.auth_code.get_token( params[:code], :redirect_uri =>callback_url ) # access_token = @oauth.auth_code.get_token(params[:code], :redirect_uri => "urn:ietf:wg:oauth:2.0:oob") session[:access_token] = access_token.token redirect_to root_path end end protected def set_oauth @oauth = OAuth2::Client.new(OAUTH_CONFIG['APP_ID'], OAUTH_CONFIG['APP_SECRET'], :site => OAUTH_CONFIG['OAUTH_PROVIDER'], :ssl => {:verify => false}) end def authenticate_oauth2 set_oauth if session[:access_token] access_token = OAuth2::AccessToken.new(@oauth, session[:access_token]) begin @user = JSON.parse(access_token.get("/api/v1/me.json").body) return rescue Exception => e puts e.message end end redirect_to @oauth.auth_code.authorize_url(:redirect_uri => callback_url ) #redirect_to @oauth.auth_code.authorize_url(:redirect_uri => "urn:ietf:wg:oauth:2.0:oob") end def survey_params params.require(:survey).permit(:title, :description, :visibility, :accepted_after, :definitive_answer, :limit_date, :promo2018, :promo2017, :promo2016, questions_attributes: [ :text, :question_type, :_destroy ] ) end end