Skip to content
Snippets Groups Projects
submissions_controller.rb 4.05 KiB
Newer Older

# 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

root's avatar
root committed
  before_action :set_new_submission , only: :index
  before_action :set_submission , only: [:show, :answer, :destroy]
root's avatar
root committed
  before_action :authorize!, except: [:create, :user_submissions, :all_users_submissions]
root's avatar
root committed
    

    submissions = paginate Submission.where(status: Submission.statuses[:submitted])
root's avatar
root committed
  
root's avatar
root committed
  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

  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
        @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
  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
root's avatar
root committed
        if @submission.accepted?def user_submissions
          render status: :ok
        end
        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
root's avatar
root committed