Newer
Older
# 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/>.
Mauricio Giacomini Girardello
committed
require 'uri'
class V1::LearningObjectsController < ApplicationController
include ::SociableController
include ::DownloadableController
include ::TaggableController
include ::DeletedObjectsController
include ::HighlightsController
include ::SubjectableController
include ::StageableController
Mauricio Giacomini Girardello
committed
before_action :authenticate_user!, only: [:create, :update, :destroy, :tagging, :untagging, :submit, :submission, :show_submission]
Marcela Ribeiro de Oliveira
committed
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]
Mauricio Giacomini Girardello
committed
def index
Marcela Ribeiro de Oliveira
committed
# learning_objects = policy_scope(LearningObject).includes(:tags, :publisher, :language, :license, :subjects, :educational_stages, :reviews)
learning_objects = LearningObject.published.includes(:tags, :publisher, :language, :license, :subjects, :educational_stages, :reviews)
Marcela Ribeiro de Oliveira
committed
learning_objects = learning_objects.order(score: :desc) if params[:sort].blank?
learning_objects = paginate learning_objects
serializer = params[:obaa].nil? ? LearningObjectSerializer : LearningObjectObaaSerializer
http_cache_forever do
render json: learning_objects, each_serializer: serializer
end
Mauricio Giacomini Girardello
committed
end
# GET /learning_objects/1
# GET /learning_objects/1.json
def show

Israel Barreto Sant'Anna
committed
serializer = params[:obaa].nil? ? LearningObjectSerializer : LearningObjectObaaSerializer
render json: @learning_object, serializer: serializer
Mauricio Giacomini Girardello
committed
end
# POST /learning_objects
# POST /learning_objects.json
def create
learning_object = LearningObject.new(learning_object_params)
Mauricio Giacomini Girardello
committed
publisher = LearningObjectPublisher.new(DspaceService.create_client)
learning_object = publisher.create_draft(learning_object, current_user)
if learning_object.errors.errors.blank?
learning_object_associations(learning_object, false)
render json: learning_object, status: :created
Mauricio Giacomini Girardello
committed
else
render json: learning_object.errors, status: :unprocessable_entity
Mauricio Giacomini Girardello
committed
end
end
# PATCH/PUT /learning_objects/1
# PATCH/PUT /learning_objects/1.json
def update
lo_params = learning_object_params
language_ids_lo = params[:language_ids]
@learning_object.language_ids = language_ids_lo
current_user.update_tags(@learning_object, with: params[:tags].map { |t| t['name'] }) unless params[:tags].nil?
@learning_object.update_educational_stages(ids: params[:educational_stages]) unless params[:educational_stages].nil?
@learning_object.update_subjects(ids: params[:subjects]) unless params[:subjects].nil?
if !lo_params[:object_type_id].blank? && lo_params[:object_type_id] != @learning_object.object_type_id && lo_params[:link].blank?
change_object_type_id = true
end
if lo_params[:thumbnail] == "null"
@learning_object.thumbnail.clear
lo_params.delete(:thumbnail)
end
if @learning_object.update(lo_params)
update_learning_object_associations(@learning_object, change_object_type_id)
publisher = LearningObjectPublisher.new(DspaceService.create_client)
publisher.update_dspace(@learning_object)
Mauricio Giacomini Girardello
committed
render json: @learning_object, status: :ok
else
if lo_params[:thumbnail] != "null"
@learning_object.thumbnail = params[:thumbnail]
render json: @learning_object, status: :ok
else
render json: @learning_object.errors, status: :unprocessable_entity
end
Mauricio Giacomini Girardello
committed
end
end
# DELETE /learning_objects/1
# DELETE /learning_objects/1.json
def destroy
@learning_object.update(state: LearningObject.states[:deleted])
@learning_object.destroy
Submission.where(learning_object_id: @learning_object.id).update_all(status: Submission.statuses[:rejected])
Submission.where(learning_object_id: @learning_object.id).destroy_all
response = { 'status': 'deleted' }
render status: :ok, json: response
Mauricio Giacomini Girardello
committed
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
Mauricio Giacomini Girardello
committed
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
Mauricio Giacomini Girardello
committed
# Use callbacks to share common setup or constraints between actions.
def set_learning_object
if current_user.try(:is_admin?)
@learning_object ||= LearningObject.unscoped.where(id: params[:id]).first
@learning_object ||= LearningObject.where(id: params[:id]).first
render status: :not_found if @learning_object.blank?
Mauricio Giacomini Girardello
committed
end
def set_new_learning_object
@learning_object ||= LearningObject.new
end
Mauricio Giacomini Girardello
committed
# 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, :terms_of_service, :thumbnail, :software, :link, :magnetlink, language_ids: [])
def extra_params
return {} if params[:learning_object].nil?
params[:learning_object].permit(subjects: [], educational_stages: [], tags: [:name])
def learning_object_associations(learning_object, change_object_type_id=false)

Eduardo Machado
committed
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: params[:subjects]) unless params[:subjects].nil?
#learning_object.add_educational_stages(ids: extra_params[:educational_stages]) unless extra_params[:educational_stages].nil?
learning_object.link = nil
end
Mauricio Giacomini Girardello
committed
end
def update_learning_object_associations(learning_object, change_object_type_id=false)
current_user.update_tags(learning_object, with: extra_params[:tags].map { |t| t['name'] }) unless extra_params[:tags].nil?

Israel Barreto Sant'Anna
committed
learning_object.update_subjects(ids: extra_params[:subjects].map {|s| s.to_i}) unless extra_params[:subjects].nil?
learning_object.update_educational_stages(ids: extra_params[:educational_stages].map {|es| es.to_i}) unless extra_params[:educational_stages].nil?
if change_object_type_id
learning_object.link = nil
end
end
Mauricio Giacomini Girardello
committed
def authorize!
authorize @learning_object
end
end