Skip to content
Snippets Groups Projects
Forked from PortalMEC / portalmec
1989 commits behind the upstream repository.
learning_objects_controller.rb 4.40 KiB
require 'uri'

class LearningObjectsController < ApplicationController
  include Reportable
  layout :resolve_layout

  before_action :authenticate_user!, except: [:index, :show]
  before_action :set_learning_object, only: [:show, :edit, :update,
                                             :destroy, :like, :bookmarks,
                                             :collections, :upload, :upload_link, :download,
                                             :user_not_authorized]
  after_action :increment_learning_object_views, only: [:show]
  before_action :set_form_objects, only: [:new, :edit, :create]
  before_action :authorize_action

  # GET /learning_objects/1
  # GET /learning_objects/1.json
  def show
    @liked = !@learning_object.liked?(current_user) if user_signed_in?
    @reviews = Review.where(reviewable: @learning_object)
  end

  # GET /learning_objects/new
  def new
    @learning_object = LearningObject.new
  end

  # GET /learning_objects/1/edit
  def edit
  end

  # POST /learning_objects
  # POST /learning_objects.json
  def create
    @learning_object = ::LearningObject::DraftBuilder.build current_user, learning_object_params
    publisher = LearningObjectPublisher.new(DspaceService.create_client)

    respond_to do |format|
      if publisher.create_draft @learning_object
        # go to file submission page
        format.html { success_redirect @learning_object }
      else
        format.html { render :new, notice: 'Não foi possível criar o objeto.' }
      end
    end

  end

  # PATCH/PUT /learning_objects/1
  # PATCH/PUT /learning_objects/1.json
  def update
    respond_to do |format|
      if LearningObject.update(learning_object_params)
        format.html { redirect_to learning_object_build_path(learning_object_id: @learning_object.id, id: :upload_attachments), notice: 'Learning object was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  end

  # DELETE /learning_objects/1
  # DELETE /learning_objects/1.json
  def destroy
    LearningObject.destroy @learning_object

    respond_to do |format|
      format.html { redirect_to learning_objects_url, notice: 'Learning object was successfully destroyed.' }
    end
  end
  # POST /learning_objects/1/like
  def like
    if @learning_object.liked? current_user
      @learning_object.dislike current_user
    else
      @learning_object.like current_user
    end

    if request.xhr?
      render json: {count: @learning_object.likes.count, id: params[:id]}
    end
  end

  def download
    @learning_object.download current_user

    redirect_to @learning_object.retrieve_link
  end

  # POST /learning_objects/1/bookmarks
  def bookmarks
    bookmarks = current_user.bookmarks
    bookmarks.add @learning_object
    Collection.save_learning_objects bookmarks

    if request.xhr?
      render json: {id: params[:id]}
    end
  end

  # GET /learning_objects/1/collections.json
  def collections
    @collections = @learning_object.collections
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_learning_object
    @learning_object = LearningObject.unscoped.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def learning_object_params
    params[:learning_object].permit(:author, :name, :object_type_id, :description, :school_level, :language, :link, topics: [])
  end

  def increment_learning_object_views
    View.create(viewable: @learning_object, user: current_user) if user_signed_in?
  end

  def success_redirect(learning_object)
    #if 'Website externo' == learning_object.object_type.name
    redirect_to learning_object_build_path(learning_object, :upload_attachments), notice: 'Seu objeto foi criado com sucesso!'
  end

  def set_form_objects
    @school_levels = ['Educação Infantil', 'Ensino Fundamental', 'Ensino Médio']
    @topics = Topic.defaults
    @types = ObjectType.all
    @languages = Language.all
  end

  def user_not_authorized
    flash[:notice] = "Este objeto está suspenso!"
    flash[:alert] = "Razões: #{Complaint.where(complaintable_id: @learning_object.id).map(&:reason).join(',').to_s}"
    redirect_to (root_path)
  end

  def resolve_layout
    case action_name
      when 'new', 'edit', 'create', 'update'
        'learning_object_studio'
      else
        'application'
    end
  end

  def authorize_action
    @learning_object ||= LearningObject.new
    authorize @learning_object
  end

end