Select Git revision
Forked from
PortalMEC / portalmec
60 commits behind the upstream repository.
-
Israel Barreto Sant'Anna authored
Fixed subject and educational stages update error. Added 404 return for inexistent ids in package controller.
Israel Barreto Sant'Anna authoredFixed subject and educational stages update error. Added 404 return for inexistent ids in package controller.
learning_objects_controller.rb 7.01 KiB
# 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/>.
require 'uri'
class V1::LearningObjectsController < ApplicationController
include ::SociableController
include ::DownloadableController
include ::TaggableController
include ::Paginator
include ::DeletedObjectsController
include ::HighlightsController
include ::SubjectableController
include ::StageableController
before_action :authenticate_user!, only: [:create, :update, :destroy, :tagging, :untagging, :submit, :submission, :show_submission]
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]
def index
# 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)
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
end
# GET /learning_objects/1
# GET /learning_objects/1.json
def show
serializer = params[:obaa].nil? ? LearningObjectSerializer : LearningObjectObaaSerializer
render json: @learning_object, serializer: serializer
end
# POST /learning_objects
# POST /learning_objects.json
def create
learning_object = LearningObject.new(learning_object_params)
authorize learning_object
publisher = LearningObjectPublisher.new(DspaceService.create_client)
if publisher.create_draft(learning_object, current_user)
learning_object_associations(learning_object, false)
render json: learning_object, status: :created
else
render json: learning_object.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /learning_objects/1
# PATCH/PUT /learning_objects/1.json
def update
lo_params = learning_object_params
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)
render json: @learning_object, status: :ok
else
render json: @learning_object.errors, status: :unprocessable_entity
end
end
# DELETE /learning_objects/1
# DELETE /learning_objects/1.json
def destroy
@learning_object.destroy
response = { 'status': 'deleted' }
render status: :ok, json: response
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
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
# Use callbacks to share common setup or constraints between actions.
def set_learning_object
#check if user is admin to show destroyed object
if current_user.try(:is_admin?)
@learning_object ||= LearningObject.unscoped.where(id: params[:id]).first
else
@learning_object ||= LearningObject.where(id: params[:id]).first
end
render status: :not_found if @learning_object.blank?
@learning_object
end
def set_new_learning_object
@learning_object ||= LearningObject.new
end
# 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: [])
end
def extra_params
return {} if params[:learning_object].nil?
params[:learning_object].permit(subjects: [], educational_stages: [], tags: [:name])
end
def learning_object_associations(learning_object, change_object_type_id=false)
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: extra_params[:subjects]) unless extra_params[:subjects].nil?
learning_object.add_educational_stages(ids: extra_params[:educational_stages]) unless extra_params[:educational_stages].nil?
if change_object_type_id
learning_object.link = nil
end
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?
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
def authorize!
authorize @learning_object
end
end