Skip to content
Snippets Groups Projects
learning_objects_controller.rb 6.88 KiB
Newer Older
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
require 'uri'

class LearningObjectsController < ApplicationController
  before_action :set_learning_object, only: [:show, :edit, :update, :destroy, :like, :bookmarks, :download, :collections]
  after_action :increment_learning_object_views, only: [:show]
  before_action :authenticate_user!, except: [:index, :show]
  before_action :set_complaint_messages, only: :show
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
  before_action :set_dspace_collection, :create_repositories, :process_params, :get_selected_subjects, only: :create
  # GET /learning_objects
  # GET /learning_objects.json
    @learning_objects = learning_object_repository.all
  # GET /learning_objects/1
  # GET /learning_objects/1.json
    @liked = learning_object_repository.liked?(current_user, @learning_object) if user_signed_in?
  # GET /learning_objects/new
    @learning_object = LearningObject.new
    @school_levels = ['Educação Infantil', 'Ensino Fundamental', 'Ensino Médio']
    @subjects = Subject.default_list
  # GET /learning_objects/1/edit
  def edit
  # POST /learning_objects
  # POST /learning_objects.json
  def create
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
    # Create the item inside Dspace
    lo = DSpaceRest::Item.new({
      "name"=> learning_object_params[:name],
      "type"=> learning_object_params[:type],
      "last_modified"=> Time.now.to_s,
      "metadata"=> []
    })
    dspace_keys = get_dspace_metadata_names("invert")
    params[:learning_object].each do |k,v|
      unless dspace_keys[k].nil?
        if v.kind_of?(Array)
          v.each do |item|
            lo.add_metadata(dspace_keys[k],item,"pt-BR")
          end
        else
          lo.add_metadata(dspace_keys[k], v, "pt-BR")
        end
      end
    end
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
    response = @col_repository.create_item_for(@collection, lo)


    # Now upload the file to the created item in DSpace
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
    strategy = DSpaceRest::Strategies::Uploads::RestStrategy.new(Dspace::Client.instance.rest_client)
    file = params[:learning_object][:file]
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
    bitstream_file = Struct.new(:name, :path, :description)
    dspace_f = bitstream_file.new
    dspace_f.path = URI.encode(file.tempfile.path)
    dspace_f.name = URI.encode(file.original_filename)
    dspace_f.description = 'file.description'

    dspace_bitstream_response = @item_repository.create_bitstream_for(response, dspace_f, strategy).as_json
    dspace_bitstream_response.each do |v,k|
      bitstream_response[v.camelize(:lower)] = k
    end
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed

Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
    # Create the object inside OrientDB
    @learning_object = LearningObject.new(learning_object_params)
    @learning_object.type = get_file_type file
    @learning_object.id_dspace = response.id
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
    @learning_object.subjects = @subjects
    @learning_object.created_at = Time.now
    @learning_object.last_modified = Time.now
    @learning_object.publisher = current_user
    @learning_object.metadata = lo.to_h[:metadata]
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
    @learning_object.attachment = LearningObject::Attachment.new([bitstream_response])
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed

      if learning_object_repository.create @learning_object
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
        learning_object_repository.create_relations @learning_object
        format.html { redirect_to me_users_path, notice: 'Learning object was successfully created.' }
      else
        format.html { render :new }
      end
    end
  # PATCH/PUT /learning_objects/1
  # PATCH/PUT /learning_objects/1.json
      if learning_object_repository.update(learning_object_params)
        format.html { redirect_to @learning_object, notice: 'Learning object was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  # DELETE /learning_objects/1
  # DELETE /learning_objects/1.json
    learning_object_repository.destroy @learning_object

    respond_to do |format|
      format.html { redirect_to learning_objects_url, notice: 'Learning object was successfully destroyed.' }
    end
  def report_object
    learning_object_repository.report current_user, @learning_object, message, description
  end

    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, id: params[:id]}
    end
  end  

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

    if request.xhr?
      render json: {id: params[:id]}
    end
  # GET /learning_objects/1/collections.json
  def collections
    @collections = @learning_object.collections
  end

Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
  def get_selected_subjects
    @subjects = []
    params[:learning_object][:subjects].each do |subject|
      @subjects << subject_repository.find_by_name(subject)
    end
  end

  def process_params
    params[:learning_object][:date_created] = Time.now
    params[:learning_object][:date_available] = Time.now
    params[:learning_object][:type] = get_file_type params[:learning_object][:file]
  end

  def create_repositories
    @col_repository = Dspace::Client.instance.repository.collection_repository
    @item_repository = Dspace::Client.instance.repository.item_repository
  end

  def set_dspace_collection
    # create struct with the id of the collection
    # Avoiding an unecessary request to the DSpace
    collection_struct = Struct.new(:id)
    @collection = collection_struct.new('4')
  end

  def get_file_type(file)
    type = file.content_type.split('/').first
    case type
    when 'video'
      "Vídeo"
    when 'image'
      "Imagem"
    when 'audio'
      "Áudio"
    else
      "Outros"
    end
  end

  def get_dspace_metadata_names(invert=nil)
    h = {
        "dc.contributor.author" => "author",
        "dc.date.accessioned" => "date_created",
        "dc.date.available" => "date_available",
        "dc.language" => "language",
        "dc.title" => "name",
        "dc.type" => "type",
        "dc.subject.category" => "subjects"
    }
    if (invert != nil)
      return h.invert
    end
    h
  end

  # Use callbacks to share common setup or constraints between actions.
  def set_learning_object
    @learning_object = learning_object_repository.find params[:id]
  # Never trust parameters from the scary internet, only allow the white list through.
  def learning_object_params
    params[:learning_object].permit(:author, :name, :type, :description, :subjects, :school_level)
    if user_signed_in?
      learning_object_repository.increment_views current_user, @learning_object
    end
  def set_complaint_messages
    @complaint = Complaint.new
    @messages = [
        Complaint.copyrights,
        Complaint.ofensive_content,
        Complaint.ofensive_user,
        Complaint.fake_user
    ]
  end