Newer
Older

Israel Barreto Sant'Anna
committed
# Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana
#
# This file is part of portalmec.
#
# portalmec is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# portalmec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with portalmec. If not, see <http://www.gnu.org/licenses/>.
class V1::SubmissionsController < ApplicationController
include ::Paginator
before_action :set_new_submission , only: :index
before_action :set_submission , only: [:show, :answer, :destroy]

Israel Barreto Sant'Anna
committed
before_action :authenticate_user!
before_action :authorize!, except: [:create, :user_submissions, :all_users_submissions]

Israel Barreto Sant'Anna
committed
def index
submissions = paginate Submission.where(status: Submission.statuses[:submitted])

Israel Barreto Sant'Anna
committed
render json: submissions
end
def show
render json: @submission
end
def user_submissions
submission_user = Submission.where(submitter_id: params[:user_id]).where(status: 0)
all_submission_user = paginate submission_user
render json: all_submission_user
end
def all_users_submissions
# show all submissions of all users without from the current user
submissions_users = Submission.where(status: 0).where.not(submitter_id: params[:user_id])
all_submissions_users = paginate submissions_users
render json: all_submissions_users
end

Israel Barreto Sant'Anna
committed
def create
learning_object = LearningObject.where(id: submission_params[:learning_object_id]).first
if learning_object.blank?
render :json, status: :not_found
else
@submission = Submission.new(learning_object: learning_object, submitter: current_user, status: :submitted)
authorize @submission
if @submission.save
CuratorAssignmentsService.new(@submission).assign

Israel Barreto Sant'Anna
committed
@submission.learning_object.submitted!
render json: @submission, status: :created
else
render json: @submission.errors, status: :unprocessable_entity
end
end
end
# GET /v1/submission/:id/answer

Israel Barreto Sant'Anna
committed
def answer
@submission.justification = answer_params[:justification]
answers = []
questions = Question.active.pluck(:id)
answers = answer_params[:answers]
if questions.size > answers.size
render json: { "error": "Active questions missing" }, status: :unprocessable_entity
else
answers.each do |answer|
if !questions.include?(answer[:question_id])
render json: {"error": "Inactive questions not permitted"}, status: :unprocessable_entity
return
else
@submission.answers << Answer.create(answer)
end
end
@submission.curator = current_user
if @submission.save
if @submission.accepted?def user_submissions
render status: :ok
end

Israel Barreto Sant'Anna
committed
@submission.learning_object.destroy

Israel Barreto Sant'Anna
committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
end
render json: @submission, status: :ok
else
render json: @submission.errors, status: :unprocessable_entity
end
end
end
def destroy
@submission.destroy
response = { 'status': 'deleted' }
render status: :ok, json: response
end
private
def answer_params
return nil if params[:submission].nil?
params[:submission].permit(:justification, answers: [:question_id, :accepted])
end
def submission_params
return nil if params[:submission].nil?
params[:submission].permit(:learning_object_id)
end
def set_new_submission
@submission ||= Submission.new
end
def set_submission
@submission ||= Submission.where(id: params[:id]).first
render status: :not_found if @submission.blank?
@submission

Israel Barreto Sant'Anna
committed
end
def authorize!
authorize @submission
end