submissions_controller.rb 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. class SubmissionsController < ApplicationController
  2. before_action :set_assignment, only: [:create, :new, :index]
  3. before_action :set_submission, only: [:show, :edit, :update, :destroy]
  4. before_action :check_view_permission, only: [:show]
  5. before_action :check_group_membership, only: [:update, :edit]
  6. before_action :check_assignment_membership, only: [:create]
  7. # GET /assignments/1/submissions
  8. # GET /assignments/1/submissions.json
  9. def index
  10. @submissions = @assignment.submissions
  11. end
  12. # GET /submissions/1
  13. # GET /submissions/1.json
  14. def show
  15. end
  16. # GET /assignments/1/submissions/new
  17. def new
  18. @submission = Submission.new
  19. end
  20. # GET /submissions/1/edit
  21. def edit
  22. end
  23. # POST /assignments/1/submissions
  24. # POST /assignments/1/submissions.json
  25. def create
  26. @submission = @assignment.submissions.new(submission_params)
  27. respond_to do |format|
  28. if @submission.save && (@submission.users << current_user)
  29. format.html { redirect_to @submission, notice: 'Submission was successfully created.' }
  30. format.json { render :show, status: :created, location: @submission }
  31. else
  32. format.html { render :new }
  33. format.json { render json: @submission.errors, status: :unprocessable_entity }
  34. end
  35. end
  36. end
  37. # PATCH/PUT /submissions/1
  38. # PATCH/PUT /submissions/1.json
  39. def update
  40. respond_to do |format|
  41. if @submission.update(submission_params)
  42. format.html { redirect_to @submission, notice: 'Submission was successfully updated.' }
  43. format.json { render :show, status: :ok, location: @submission }
  44. else
  45. format.html { render :edit }
  46. format.json { render json: @submission.errors, status: :unprocessable_entity }
  47. end
  48. end
  49. end
  50. # DELETE /submissions/1
  51. # DELETE /submissions/1.json
  52. def destroy
  53. @submission.destroy
  54. respond_to do |format|
  55. format.html { redirect_to assignment_submissions_url(@submission.assignment), notice: 'Submission was successfully destroyed.' }
  56. format.json { head :no_content }
  57. end
  58. end
  59. private
  60. def set_assignment
  61. @assignment = Assignment.find(params[:assignment_id])
  62. end
  63. # Use callbacks to share common setup or constraints between actions.
  64. def set_submission
  65. @submission = Submission.find(params[:id])
  66. end
  67. # Never trust parameters from the scary internet, only allow the white list through.
  68. def submission_params
  69. params.require(:submission).permit(:title, :description)
  70. end
  71. def check_view_permission
  72. redirect_to root_url unless @submission.users.include?(current_user) || @submission.assignment.course.users.merge(CourseParticipation.teachers).include?(current_user)
  73. end
  74. def check_group_membership
  75. redirect_to root_url unless @submission.users.include?(current_user)
  76. end
  77. def check_assignment_membership
  78. redirect_to root_url unless current_user.assignments.include?(@assignment)
  79. end
  80. end