Skip to content
Snippets Groups Projects
Select Git revision
  • issue/70-hotfix-error-collection-resource-deleted
  • develop default protected
  • issue-68/streak-high_streak-last_action_at
  • issue/65-govbr
  • issue/54-curator-queue
  • issue/58-testar-s3
  • master protected
  • pre-submission
  • pre-languages
  • video-cache
  • tag-clustering-task
  • pre-api
  • orientdb
13 results

learning_objects_controller.rb

Blame
  • Forked from PortalMEC / portalmec
    537 commits behind the upstream repository.
    learning_objects_controller.rb 5.93 KiB
    
    # 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
      include ::TaggableController
      include ::Paginator
      include ::DeletedObjectsController
      include ::HighlightsController
      include ::SubjectableController
      include ::StageableController
      include ::SubmissionController
    
      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]
    
      def index
        learning_objects = paginate LearningObject.includes(:tags, :publisher, :language, :license, :subjects, :educational_stages, :reviews)
        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)
    
        if publisher.create_draft(learning_object, current_user)
          learning_object_associations(learning_object, false)
          render json: learning_object, status: :created
        else
          render json: learning_object.errors, status: :unprocessable_entity
        end
      end
    
      # PATCH/PUT /learning_objects/1
      # PATCH/PUT /learning_objects/1.json
      def update
        if !learning_object_params[:object_type_id].blank? && learning_object_params[:object_type_id] != @learning_object.object_type_id && learning_object_params[:link].blank?
          change_object_type_id = true
        end
        if @learning_object.update(learning_object_params)
          learning_object_associations(@learning_object, change_object_type_id)
          publisher = LearningObjectPublisher.new(DspaceService.create_client)
          publisher.update_dspace(@learning_object)
    
          render json: @learning_object, status: :ok
        else
          render json: @learning_object.errors, status: :unprocessable_entity
        end
      end
    
      # DELETE /learning_objects/1
      # DELETE /learning_objects/1.json
      def destroy
        @learning_object.destroy
        render status: :ok
      end
    
      # 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
    
      private
    
      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
        #check if user is admin to show destroyed object
        if current_user.try(:is_admin?)
          @learning_object ||= LearningObject.unscoped.find(params[:id])
        else
          @learning_object ||= LearningObject.find(params[:id])
        end
      end
    
      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, :thumbnail, :software, :language_id, :link,  :magnetlink)
      end
    
      def extra_params
        return {} if params[:learning_object].nil?
        params[:learning_object].permit(subjects: [], educational_stages: [], tags: [:name])
      end
    
      def learning_object_associations(learning_object, change_object_type_id)
        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
        learning_object.add_subjects(ids: extra_params[:subjects]) unless extra_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
      end
    
      def authorize!
        authorize @learning_object
      end
    end