require 'uri' class LearningObjectsController < ApplicationController before_action :set_learning_object, only: [:show, :edit, :update, :destroy, :like, :bookmarks, :collections] after_action :increment_learning_object_views, only: [:show] before_action :authenticate_user!, except: [:index, :show] before_action :set_complaint_messages, only: :show before_action :set_dspace_collection, :create_repositories, :process_params, :get_selected_subjects, only: :create # GET /learning_objects # GET /learning_objects.json def index @learning_objects = learning_object_repository.all end # GET /learning_objects/1 # GET /learning_objects/1.json def show @liked = learning_object_repository.liked?(current_user, @learning_object) if user_signed_in? end # GET /learning_objects/new def new @learning_object = LearningObject.new @school_levels = ['Educação Infantil', 'Ensino Fundamental', 'Ensino Médio'] @subjects = Subject.default_list end # GET /learning_objects/1/edit def edit end # POST /learning_objects # POST /learning_objects.json def create # 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 response = @col_repository.create_item_for(@collection, lo) # Now upload the file to the created item in DSpace strategy = DSpaceRest::Strategies::Uploads::RestStrategy.new(Dspace::Client.instance.rest_client) file = params[:learning_object][:file] bitstream_response = {} 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 # 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 @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] @learning_object.attachment = LearningObject::Attachment.new([bitstream_response]) respond_to do |format| if learning_object_repository.create @learning_object 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 end # PATCH/PUT /learning_objects/1 # PATCH/PUT /learning_objects/1.json def update respond_to do |format| 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 end # DELETE /learning_objects/1 # DELETE /learning_objects/1.json def destroy learning_object_repository.destroy @learning_object respond_to do |format| format.html { redirect_to learning_objects_url, notice: 'Learning object was successfully destroyed.' } end end def report_object learning_object_repository.report current_user, @learning_object, message, description 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, id: params[:id]} end end # 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 end # GET /learning_objects/1/collections.json def collections @collections = @learning_object.collections end private 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] end # 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) end def increment_learning_object_views if user_signed_in? learning_object_repository.increment_views current_user, @learning_object end end def set_complaint_messages @complaint = Complaint.new @messages = [ Complaint.copyrights, Complaint.ofensive_content, Complaint.ofensive_user, Complaint.fake_user ] end end