Skip to content
Snippets Groups Projects
Commit f111faf5 authored by Mauricio Giacomini Girardello's avatar Mauricio Giacomini Girardello
Browse files

refactoring learning object controller

parent 8d5467c3
No related branches found
No related tags found
No related merge requests found
......@@ -2,12 +2,20 @@ module Reportable
extend ActiveSupport::Concern
included do
before_action :set_complaint_messages, only: :show
end
def report_object
learning_object_repository.report current_user, @learning_object, message, description
end
def set_complaint_messages
@complaint = Complaint.new
@messages = [
Complaint.copyrights,
Complaint.ofensive_content,
Complaint.ofensive_user,
Complaint.fake_user
Complaint.copyrights,
Complaint.ofensive_content,
Complaint.ofensive_user,
Complaint.fake_user
]
end
......
require 'uri'
class LearningObjectsController < ApplicationController
include Reportable
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
......@@ -24,6 +18,7 @@ class LearningObjectsController < ApplicationController
@learning_object = LearningObject.new
@school_levels = ['Educação Infantil', 'Ensino Fundamental', 'Ensino Médio']
@subjects = Subject.default_list
@types = learning_object_repository.types
end
# GET /learning_objects/1/edit
......@@ -33,66 +28,18 @@ class LearningObjectsController < ApplicationController
# 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.' }
# go to file submission page
#format.html { redirect_to me_users_path, notice: 'Learning object was successfully created.' }
else
format.html { render :new }
#format.html { redirect_to , notice: 'Learning object wasnt successfully created.' }
end
end
end
# PATCH/PUT /learning_objects/1
# PATCH/PUT /learning_objects/1.json
def update
......@@ -115,10 +62,6 @@ class LearningObjectsController < ApplicationController
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
......@@ -150,59 +93,11 @@ class LearningObjectsController < ApplicationController
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
def build_subjects(subjects)
_subjects = []
subjects.each do |subject|
_subjects << subject_repository.find_by_name(subject)
end
h
end
# Use callbacks to share common setup or constraints between actions.
......@@ -212,7 +107,7 @@ class LearningObjectsController < ApplicationController
# 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)
params[:learning_object].permit(:author, :name, :type, :description, :school_level, :language, subjects: [])
end
def increment_learning_object_views
......@@ -221,14 +116,4 @@ class LearningObjectsController < ApplicationController
end
end
def set_complaint_messages
@complaint = Complaint.new
@messages = [
Complaint.copyrights,
Complaint.ofensive_content,
Complaint.ofensive_user,
Complaint.fake_user
]
end
end
<p id="notice"><%= notice %></p>
<h1>Listing Learning Objects</h1>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @learning_objects.each do |learning_object| %>
<tr>
<td><%= link_to 'Show', learning_object %></td>
<td><%= link_to 'Edit', edit_learning_object_path(learning_object) %></td>
<td><%= link_to 'Destroy', learning_object, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Learning object', new_learning_object_path %>
json.array!(@learning_objects) do |learning_object|
json.extract! learning_object, :id
json.url learning_object_url(learning_object, format: :json)
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment