Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update LOs in LOEP after publishing
  • Loading branch information
agordillo committed May 1, 2016
1 parent 802a636 commit d82c63d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 32 deletions.
9 changes: 9 additions & 0 deletions app/models/excursion.rb
Expand Up @@ -17,6 +17,7 @@ class Excursion < ActiveRecord::Base
before_validation :fill_license
after_save :parse_for_meta
after_save :fix_post_activity_nil
# after_save :send_to_loep
after_destroy :remove_scorms
after_destroy :remove_pdf

Expand Down Expand Up @@ -1330,5 +1331,13 @@ def fix_post_activity_nil
a.save!
end
end

def send_to_loep
#If LOEP is enabled, send the excursion to LOEP.
#It will be created or updated
if self.public_scope? and !Vish::Application.config.APP_CONFIG['loep'].nil?
VishLoep.sendActivityObject(self.activity_object) rescue nil
end
end

end
6 changes: 5 additions & 1 deletion app/models/loep.rb
Expand Up @@ -15,7 +15,7 @@ def self.getLO(lo_id)
end

#Create LO
def self.createLO(lo)
def self.createOrUpdateLO(lo)
params = getParams
params["lo"] = lo

Expand Down Expand Up @@ -53,6 +53,8 @@ def self.callAPI(method,apiPath,params={})
response = RestClient::Request.execute(
:method => :post,
:url => apiMethodURL,
:timeout => 8,
:open_timeout => 8,
:payload => params,
:headers => {:'Authorization' => getBasicAuthHeader, :content_type => :json, :accept => :json}
){ |response|
Expand All @@ -62,6 +64,8 @@ def self.callAPI(method,apiPath,params={})
response = RestClient::Request.execute(
:method => :get,
:url => apiMethodURL,
:timeout => 8,
:open_timeout => 8,
:headers => {:'Authorization' => getBasicAuthHeader}
){ |response|
yield JSON(response),response.code if block_given?
Expand Down
49 changes: 25 additions & 24 deletions app/models/vish_loep.rb
Expand Up @@ -34,13 +34,9 @@ def self.fillActivityObjectMetrics(ao,loepData)
ao.calculate_qscore
end

def self.registerActivityObject(ao)
if ao.nil? or ao.object.nil?
yield "Activity Object is nil", nil if block_given?
return "Activity Object is nil"
end

#Compose the object to be sent to LOEP
def self.getLoepHashForActivityObject(ao)
return {} if ao.blank?

lo = Hash.new
lo["name"] = ao.title unless ao.title.blank?
lo["description"] = ao.description unless ao.description.blank?
Expand Down Expand Up @@ -109,36 +105,41 @@ def self.registerActivityObject(ao)
end

#Interactions
unless ao.lo_interaction.nil?
lo["interactions"] = ao.lo_interaction.extended_attributes
lo["interactions"] = ao.lo_interaction.extended_attributes unless ao.lo_interaction.nil?

lo
end

def self.sendActivityObject(ao)
if ao.nil? or ao.object.nil?
yield "Activity Object is nil", nil if block_given?
return "Activity Object is nil"
end

#Compose the object to be sent to LOEP
lo = VishLoep.getLoepHashForActivityObject(ao)

Loep.createLO(lo){ |response,code|
Loep.createOrUpdateLO(lo){ |response,code|
# Get quality metrics from automatic evaluation methods.
# Not necessary because Loep::LosController:update will be called after publishing by LOEP.
# VishLoep.fillActivityObjectMetrics(ao,response)
# (Optional) Create assignments through LOEP
if block_given?
yield response, code
end
yield response, code if block_given?
}
end

def self.registerActivityObjects(aos,options=nil)
def self.sendActivityObjects(aos,options=nil)
unless !options.nil? and options[:async]==true
return _registerActivityObjectsSync(aos,options)
return _sendActivityObjectsSync(aos,options)
else
_registerActivityObjectsAsync(aos,options){
if block_given?
yield "Finish"
end
_sendActivityObjectsAsync(aos,options){
yield "Finish" if block_given?
}
end
end

def self._registerActivityObjectsSync(aos,options=nil)
def self._sendActivityObjectsSync(aos,options=nil)
aos.each do |ao|
VishLoep.registerActivityObject(ao){ |response,code|
VishLoep.sendActivityObject(ao){ |response,code|
if !options.nil? and options[:trace]==true
puts "Activity Object with id: " + ao.getGlobalId
puts response.to_s
Expand All @@ -149,7 +150,7 @@ def self._registerActivityObjectsSync(aos,options=nil)
return "Finish"
end

def self._registerActivityObjectsAsync(aos,options=nil)
def self._sendActivityObjectsAsync(aos,options=nil)
eChunks = aos.each_slice(25).to_a
_rChunks(0,eChunks,options){
yield "F"
Expand All @@ -167,7 +168,7 @@ def self._rChunks(cA,eChunks,options=nil)
end

def self._rChunk(cB,aos,options=nil)
VishLoep.registerActivityObjects(aos[cB]){ |response,code|
VishLoep.sendActivityObjects(aos[cB]){ |response,code|
if !options.nil? and options[:trace]==true
puts "Activity Object with id: " + aos[cB].getGlobalId
puts response.to_s
Expand Down
13 changes: 6 additions & 7 deletions lib/tasks/loep.rake
Expand Up @@ -3,12 +3,12 @@
namespace :loep do

#Usage
#Development: bundle exec rake loep:registerLOs
#In production: bundle exec rake loep:registerLOs RAILS_ENV=production
task :registerLOs => :environment do
#Development: bundle exec rake loep:sendLOs
#In production: bundle exec rake loep:sendLOs RAILS_ENV=production
task :sendLOs => :environment do

puts "#####################################"
puts "Bringing LOs from ViSH to LOEP"
puts "Sending LOs from ViSH to LOEP"
puts "#####################################"

#Select an array of excursions to be registered in LOEP
Expand All @@ -30,10 +30,9 @@ namespace :loep do
# excursions = Excursion.where(:draft=> false, :created_at => startDate..endDate)

aos = excursions.map{|ex| ex.activity_object}
VishLoep.registerActivityObjects(aos,{:sync=>true,:trace=>true})
VishLoep.sendActivityObjects(aos,{:sync=>true,:trace=>true})
# Async
# VishLoep.registerActivityObjects(aos,{:async=>true,:trace=>true})

# VishLoep.sendActivityObjects(aos,{:async=>true,:trace=>true})
end

#Usage
Expand Down

0 comments on commit d82c63d

Please sign in to comment.