Newer
Older
class LearningObjectsController < ApplicationController
Mauricio Giacomini Girardello
committed
before_action :set_learning_object, only: [:show, :edit, :update, :destroy, :like, :bookmarks, :collections]
Mauricio Giacomini Girardello
committed
after_action :increment_learning_object_views, only: [:show]
before_action :authenticate_user!, except: [:index, :show, :like]
before_action :set_complaint_messages, only: :show
before_action :login_on_dspace, only: :create
# GET /learning_objects
# GET /learning_objects.json
@learning_objects = learning_object_repository.all
# GET /learning_objects/1
# GET /learning_objects/1.json
def show
end
# 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
collection_struct = Struct.new(:id)
collection = collection_struct.new('4')
repo = Dspace::Client.instance.repository.collection_repository
args = {}
args['name'] = learning_object_params[:name]
args['type'] = learning_object_params[:type]
args['last_modified'] = Time.now.to_s
# trata o form do learning object
lo = DSpaceRest::Item.new(args)
dspace_keys = get_dspace_metadata_names("invert")
learning_object_params.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
response = repo.create_item_for(collection, lo)
subjects = []
learning_object_params[:subjects].each do |subject|
subjects << subject_repository.find_by_name(subject)
end
@learning_object = LearningObject.new(learning_object_params)
@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]
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
# PATCH/PUT /learning_objects/1
# PATCH/PUT /learning_objects/1.json
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
# 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
Mauricio Giacomini Girardello
committed
# POST /learning_objects/1/like
def like
Mauricio Giacomini Girardello
committed
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
Mauricio Giacomini Girardello
committed
end
# POST /learning_objects/1/bookmarks
def bookmarks
bookmarks = current_user.bookmarks
bookmarks.add @learning_object
Mauricio Giacomini Girardello
committed
collection_repository.save_learning_objects bookmarks
if request.xhr?
render json: {id: params[:id]}
end
Mauricio Giacomini Girardello
committed
# GET /learning_objects/1/collections.json
def collections
@collections = @learning_object.collections
end
def get_dspace_metadata_names(invert=nil)
h = {
"dc.title" => "name",
"dc.contributor.author" => "author",
"dc.description" => "description",
"dc.language" => "language",
"dc.source" => "source",
"dc.type" => "type",
"dc.date.issued" => "date",
"dc.rights.license" => "license",
"dc.rights.holder" => "copyright",
"dc.location.country" => "country",
"dc.date.accessioned" => "date_created",
"dc.date.available" => "date_available",
"dc.subject.category" => "subjects"
# "dc.subject.keyword" => "keywords",
# "dc.date.issued" => "date_issued",
# "dc.date.submitted" => "date_submitted",
# "dc.publisher" => "publisher"
# TODO: dc.audience.mediator
}
if (invert != nil)
return h.invert
end
h
end
def login_on_dspace
Dspace::Client.instance.login 'admin@mecdb3.c3sl.ufpr.br', 'admin'
end
Mauricio Giacomini Girardello
committed
# Use callbacks to share common setup or constraints between actions.
def set_learning_object
Mauricio Giacomini Girardello
committed
@learning_object = learning_object_repository.find params[:id]
Mauricio Giacomini Girardello
committed
end
Mauricio Giacomini Girardello
committed
# Never trust parameters from the scary internet, only allow the white list through.
def learning_object_params
params[:learning_object]
end
def increment_learning_object_views
if user_signed_in?
learning_object_repository.increment_views current_user, @learning_object
end
Mauricio Giacomini Girardello
committed
end
def set_complaint_messages
@complaint = Complaint.new
@messages = [
Complaint.copyrights,
Complaint.ofensive_content,
Complaint.ofensive_user,
Complaint.fake_user
]
end