Skip to content
Snippets Groups Projects
learning_objects_controller.rb 7.89 KiB
Newer Older
bfs15's avatar
bfs15 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/>.

require 'uri'

class V1::LearningObjectsController < ApplicationController
  include ::SociableController
  include ::DownloadableController
Giovanne Marcelo's avatar
Giovanne Marcelo committed
  include ::Paginator
  include ::DeletedObjectsController
  include ::HighlightsController
  include ::SubjectableController
  before_action :authenticate_user!, only: [:create, :update, :destroy, :tagging, :untagging, :submit, :submission, :show_submission]
  before_action :set_learning_object, only: [:show, :update, :destroy, :subjecting, :unsubjecting, :add_stages, :remove_stages]
  before_action :set_new_learning_object, only: [:index, :submissions]
  before_action :set_new_submission, only: :submit
  before_action :set_submission, only: :show_submission
  before_action :authorize!, except: [:create, :tagging, :untagging, :download, :magnetlink, :validate]
  before_action :set_paper_trail_whodunnit, except: [:index, :show]
    # learning_objects = policy_scope(LearningObject).includes(:tags, :publisher, :language, :license, :subjects, :educational_stages, :reviews)
    learning_objects = LearningObject.published.includes(:tags, :publisher, :language, :license, :subjects, :educational_stages, :reviews)
    learning_objects = learning_objects.order(score: :desc) if params[:sort].blank?
    learning_objects = paginate learning_objects
    serializer = params[:obaa].nil? ? LearningObjectSerializer : LearningObjectObaaSerializer
    http_cache_forever do
      render json: learning_objects, each_serializer: serializer
    end
  end

  # GET /learning_objects/1
  # GET /learning_objects/1.json
  def show
    serializer = params[:obaa].nil? ? LearningObjectSerializer : LearningObjectObaaSerializer
    render json: @learning_object, serializer: serializer
  end

  # POST /learning_objects
  # POST /learning_objects.json
  def create
    learning_object = LearningObject.new(learning_object_params)
    authorize learning_object
    publisher = LearningObjectPublisher.new(DspaceService.create_client)
    learning_object = publisher.create_draft(learning_object, current_user)
    if learning_object.errors.errors.blank?
Clarissa's avatar
Clarissa committed
      learning_object_associations(learning_object, false)
      render json: learning_object, status: :created
      render json: learning_object.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /learning_objects/1
  # PATCH/PUT /learning_objects/1.json
  def update
    lo_params = learning_object_params
lmtd21's avatar
lmtd21 committed
    language_ids_lo = params[:language_ids]
    @learning_object.language_ids = language_ids_lo
    current_user.update_tags(@learning_object, with: params[:tags].map { |t| t['name'] }) unless params[:tags].nil?
    @learning_object.update_educational_stages(ids: params[:educational_stages]) unless params[:educational_stages].nil?
lmtd21's avatar
lmtd21 committed
    @learning_object.update_subjects(ids: params[:subjects]) unless params[:subjects].nil?

    if !lo_params[:object_type_id].blank? && lo_params[:object_type_id] != @learning_object.object_type_id && lo_params[:link].blank?
      change_object_type_id = true
    end

    if lo_params[:thumbnail] == "null"
      @learning_object.thumbnail.clear
      lo_params.delete(:thumbnail)
    end

    if @learning_object.update(lo_params)
      update_learning_object_associations(@learning_object, change_object_type_id)
      publisher = LearningObjectPublisher.new(DspaceService.create_client)
      publisher.update_dspace(@learning_object)

      if lo_params[:thumbnail] != "null"
        @learning_object.thumbnail = params[:thumbnail]
        render json: @learning_object, status: :ok
      else
        render json: @learning_object.errors, status: :unprocessable_entity
      end
    end
  end

  # DELETE /learning_objects/1
  # DELETE /learning_objects/1.json
  def destroy
lmtd21's avatar
lmtd21 committed
    @learning_object.update(state: LearningObject.states[:deleted])
    @learning_object.destroy
    Submission.where(learning_object_id: @learning_object.id).update_all(status: Submission.statuses[:rejected])
    Submission.where(learning_object_id: @learning_object.id).destroy_all
    response = { 'status': 'deleted' }
    render status: :ok, json: response
  # GET /v1/learning_objects/magnetlink/:magnetlink
  def magnetlink
    render json: LearningObject.where(magnetlink: params[:magnetlink])
  end

  # GET /learning_objects/validate?infohash=""
  def validate
    infohash = LearningObject::Attachment.find_by_infohash(params["infohash"])
    if infohash
        render json: infohash, status: :ok
    else
        render status: :not_found
    end
  end
  def deleted_resource; LearningObject; end
  def highlights_resource; LearningObject; end
  def sociable; set_learning_object; end
  def downloadable; set_learning_object; end
  def taggable; set_learning_object; end
  def subjectable; set_learning_object; end
  def stageable; set_learning_object; end
  # Use callbacks to share common setup or constraints between actions.
  def set_learning_object
Marcela Ribeiro de Oliveira's avatar
Marcela Ribeiro de Oliveira committed
    #check if user is admin to show destroyed object
      @learning_object ||= LearningObject.unscoped.where(id: params[:id]).first
      @learning_object ||= LearningObject.where(id: params[:id]).first
    render status: :not_found if @learning_object.blank?

  def set_new_learning_object
    @learning_object ||= LearningObject.new
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def learning_object_params
    return nil if params[:learning_object].nil?
    params[:learning_object].permit(:author, :name, :curator, :object_type_id, :description, :license_id, :terms_of_service, :thumbnail, :software, :link,  :magnetlink, language_ids: [])
Giovanne Marcelo's avatar
Giovanne Marcelo committed
  end

  def extra_params
    return {} if params[:learning_object].nil?
    params[:learning_object].permit(subjects: [], educational_stages: [], tags: [:name])
Giovanne Marcelo's avatar
Giovanne Marcelo committed
  end

  def learning_object_associations(learning_object, change_object_type_id=false)
    if extra_params[:tags] == []
      current_user.untag(learning_object, with: @learning_object.tags.map { |t| t['name'] })
    elsif !extra_params[:tags].nil?
      current_user.tag(learning_object, with: extra_params[:tags].map { |t| t['name'] })
    end
lmtd21's avatar
lmtd21 committed
    learning_object.add_subjects(ids: params[:subjects]) unless params[:subjects].nil?
    #learning_object.add_educational_stages(ids: extra_params[:educational_stages]) unless extra_params[:educational_stages].nil?
    if change_object_type_id
      learning_object.link = nil
    end
  def update_learning_object_associations(learning_object, change_object_type_id=false)
    current_user.update_tags(learning_object, with: extra_params[:tags].map { |t| t['name'] }) unless extra_params[:tags].nil?
    learning_object.update_subjects(ids: extra_params[:subjects].map {|s| s.to_i}) unless extra_params[:subjects].nil?
    learning_object.update_educational_stages(ids: extra_params[:educational_stages].map {|es| es.to_i}) unless extra_params[:educational_stages].nil?

    if change_object_type_id
      learning_object.link = nil
    end
  end