2012-04-10 14:06:46 +00:00
|
|
|
class History < ActiveRecord::Base
|
|
|
|
self.table_name = 'histories'
|
|
|
|
belongs_to :history_type, :class_name => 'History::Type'
|
|
|
|
belongs_to :history_object, :class_name => 'History::Object'
|
|
|
|
belongs_to :history_attribute, :class_name => 'History::Attribute'
|
|
|
|
# before_validation :check_type, :check_object
|
|
|
|
# attr_writer :history_type, :history_object
|
|
|
|
|
2012-11-07 23:47:05 +00:00
|
|
|
@@cache_type = {}
|
|
|
|
@@cache_object = {}
|
|
|
|
@@cache_attribute = {}
|
|
|
|
|
2012-07-10 08:09:58 +00:00
|
|
|
def self.history_create(data)
|
|
|
|
|
|
|
|
# lookups
|
2012-11-07 23:47:05 +00:00
|
|
|
if data[:history_type]
|
|
|
|
history_type = self.history_type_lookup( data[:history_type] )
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
if data[:history_object]
|
|
|
|
history_object = self.history_object_lookup( data[:history_object] )
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
|
|
|
related_history_object_id = nil
|
|
|
|
if data[:related_history_object]
|
2012-11-07 23:47:05 +00:00
|
|
|
related_history_object = self.history_object_lookup( data[:related_history_object] )
|
2012-07-10 08:09:58 +00:00
|
|
|
related_history_object_id = related_history_object.id
|
|
|
|
end
|
|
|
|
history_attribute_id = nil
|
|
|
|
if data[:history_attribute]
|
2012-11-07 23:47:05 +00:00
|
|
|
history_attribute = self.history_attribute_lookup( data[:history_attribute] )
|
2012-07-10 08:09:58 +00:00
|
|
|
history_attribute_id = history_attribute.id
|
|
|
|
end
|
|
|
|
|
|
|
|
# create history
|
|
|
|
History.create(
|
|
|
|
:o_id => data[:o_id],
|
|
|
|
:history_type_id => history_type.id,
|
|
|
|
:history_object_id => history_object.id,
|
|
|
|
:history_attribute_id => history_attribute_id,
|
|
|
|
:related_history_object_id => related_history_object_id,
|
|
|
|
:related_o_id => data[:related_o_id],
|
|
|
|
:value_from => data[:value_from],
|
|
|
|
:value_to => data[:value_to],
|
|
|
|
:id_from => data[:id_from],
|
|
|
|
:id_to => data[:id_to],
|
|
|
|
:created_by_id => data[:created_by_id]
|
|
|
|
)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-11-07 23:47:05 +00:00
|
|
|
def self.history_destroy( requested_object, requested_object_id )
|
2012-07-10 08:09:58 +00:00
|
|
|
History.where( :history_object_id => History::Object.where( :name => requested_object ) ).
|
2012-04-10 14:06:46 +00:00
|
|
|
where( :o_id => requested_object_id ).
|
2012-07-10 08:09:58 +00:00
|
|
|
destroy_all
|
|
|
|
end
|
|
|
|
|
2012-11-07 23:47:05 +00:00
|
|
|
def self.history_list( requested_object, requested_object_id, related_history_object = nil )
|
2012-07-10 08:09:58 +00:00
|
|
|
if !related_history_object
|
2012-11-07 23:47:05 +00:00
|
|
|
history_object = self.history_object_lookup( requested_object )
|
|
|
|
history = History.where( :history_object_id => history_object.id ).
|
2012-07-10 08:09:58 +00:00
|
|
|
where( :o_id => requested_object_id ).
|
2012-11-18 11:06:55 +00:00
|
|
|
where( :history_type_id => History::Type.where( :name => ['created', 'updated', 'notification', 'email', 'added', 'removed'] ) ).
|
2012-07-10 08:09:58 +00:00
|
|
|
order('created_at ASC, id ASC')
|
|
|
|
else
|
2012-11-07 23:47:05 +00:00
|
|
|
history_object_requested = self.history_object_lookup( requested_object )
|
|
|
|
history_object_related = self.history_object_lookup( related_history_object )
|
2012-07-10 08:09:58 +00:00
|
|
|
history = History.where(
|
|
|
|
'((history_object_id = ? AND o_id = ?) OR (history_object_id = ? AND related_o_id = ? )) AND history_type_id IN (?)',
|
2012-11-07 23:47:05 +00:00
|
|
|
history_object_requested.id,
|
2012-07-10 08:09:58 +00:00
|
|
|
requested_object_id,
|
2012-11-07 23:47:05 +00:00
|
|
|
history_object_related.id,
|
2012-07-10 08:09:58 +00:00
|
|
|
requested_object_id,
|
2012-11-18 11:06:55 +00:00
|
|
|
History::Type.where( :name => ['created', 'updated', 'notification', 'email', 'added', 'removed'] )
|
2012-07-10 08:09:58 +00:00
|
|
|
).
|
|
|
|
order('created_at ASC, id ASC')
|
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2012-07-10 08:09:58 +00:00
|
|
|
list = []
|
|
|
|
history.each { |item|
|
|
|
|
item_tmp = item.attributes
|
|
|
|
item_tmp['history_type'] = item.history_type.name
|
|
|
|
item_tmp['history_object'] = item.history_object.name
|
|
|
|
if item.history_attribute
|
|
|
|
item_tmp['history_attribute'] = item.history_attribute.name
|
|
|
|
end
|
|
|
|
item_tmp.delete( 'history_attribute_id' )
|
|
|
|
item_tmp.delete( 'history_object_id' )
|
|
|
|
item_tmp.delete( 'history_type_id' )
|
|
|
|
item_tmp.delete( 'o_id' )
|
|
|
|
item_tmp.delete( 'updated_at' )
|
|
|
|
if item_tmp['id_to'] == nil && item_tmp['id_from'] == nil
|
|
|
|
item_tmp.delete( 'id_to' )
|
2012-11-07 23:47:05 +00:00
|
|
|
item_tmp.delete( 'id_from' )
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
|
|
|
if item_tmp['value_to'] == nil && item_tmp['value_from'] == nil
|
|
|
|
item_tmp.delete( 'value_to' )
|
2012-11-07 23:47:05 +00:00
|
|
|
item_tmp.delete( 'value_from' )
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
|
|
|
if item_tmp['related_history_object_id'] == nil
|
|
|
|
item_tmp.delete( 'related_history_object_id' )
|
|
|
|
end
|
|
|
|
if item_tmp['related_o_id'] == nil
|
|
|
|
item_tmp.delete( 'related_o_id' )
|
|
|
|
end
|
|
|
|
list.push item_tmp
|
|
|
|
}
|
|
|
|
return list
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2012-10-18 19:29:56 +00:00
|
|
|
def self.activity_stream( user, limit = 10 )
|
2012-04-10 14:06:46 +00:00
|
|
|
# g = Group.where( :active => true ).joins(:users).where( 'users.id' => user.id )
|
|
|
|
# stream = History.select("distinct(histories.o_id), created_by_id, history_attribute_id, history_type_id, history_object_id, value_from, value_to").
|
2012-04-29 21:37:49 +00:00
|
|
|
# where( :history_type_id => History::Type.where( :name => ['created', 'updated']) ).
|
2012-04-10 14:06:46 +00:00
|
|
|
stream = History.select("distinct(histories.o_id), created_by_id, history_type_id, history_object_id").
|
2012-07-10 08:09:58 +00:00
|
|
|
where( :history_object_id => History::Object.where( :name => [ 'Ticket', 'Ticket::Article' ] ) ).
|
|
|
|
where( :history_type_id => History::Type.where( :name => [ 'created', 'updated' ]) ).
|
2012-04-10 14:06:46 +00:00
|
|
|
order('created_at DESC, id DESC').
|
2012-04-29 21:37:49 +00:00
|
|
|
limit(limit)
|
2012-04-20 12:25:13 +00:00
|
|
|
datas = []
|
2012-04-10 14:06:46 +00:00
|
|
|
stream.each do |item|
|
2012-04-20 12:25:13 +00:00
|
|
|
data = item.attributes
|
2012-11-07 23:54:06 +00:00
|
|
|
data['history_object'] = self.history_object_lookup_id( data['history_object_id'] ).name
|
|
|
|
data['history_type'] = self.history_type_lookup_id( data['history_type_id'] ).name
|
2012-07-10 08:09:58 +00:00
|
|
|
data.delete('history_object_id')
|
|
|
|
data.delete('history_type_id')
|
2012-04-20 12:25:13 +00:00
|
|
|
datas.push data
|
2012-04-10 14:06:46 +00:00
|
|
|
# item['history_attribute'] = item.history_attribute
|
|
|
|
end
|
2012-04-20 12:25:13 +00:00
|
|
|
return datas
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2012-10-18 19:29:56 +00:00
|
|
|
def self.activity_stream_fulldata( user, limit = 10 )
|
2012-07-23 22:22:23 +00:00
|
|
|
activity_stream = History.activity_stream( user, limit )
|
|
|
|
|
|
|
|
# get related users
|
|
|
|
users = {}
|
|
|
|
tickets = []
|
|
|
|
articles = []
|
|
|
|
activity_stream.each {|item|
|
|
|
|
|
|
|
|
# load article ids
|
|
|
|
if item['history_object'] == 'Ticket'
|
|
|
|
ticket = Ticket.find( item['o_id'] ).attributes
|
|
|
|
tickets.push ticket
|
|
|
|
|
|
|
|
# load users
|
|
|
|
if !users[ ticket['owner_id'] ]
|
|
|
|
users[ ticket['owner_id'] ] = User.user_data_full( ticket['owner_id'] )
|
|
|
|
end
|
|
|
|
if !users[ ticket['customer_id'] ]
|
|
|
|
users[ ticket['customer_id'] ] = User.user_data_full( ticket['customer_id'] )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if item['history_object'] == 'Ticket::Article'
|
|
|
|
article = Ticket::Article.find( item['o_id'] ).attributes
|
|
|
|
if !article['subject'] || article['subject'] == ''
|
|
|
|
article['subject'] = Ticket.find( article['ticket_id'] ).title
|
|
|
|
end
|
|
|
|
articles.push article
|
|
|
|
|
|
|
|
# load users
|
|
|
|
if !users[ article['created_by_id'] ]
|
|
|
|
users[ article['created_by_id'] ] = User.user_data_full( article['created_by_id'] )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if item['history_object'] == 'User'
|
|
|
|
users[ item['o_id'] ] = User.user_data_full( item['o_id'] )
|
|
|
|
end
|
|
|
|
|
|
|
|
# load users
|
|
|
|
if !users[ item['created_by_id'] ]
|
|
|
|
users[ item['created_by_id'] ] = User.user_data_full( item['created_by_id'] )
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
:activity_stream => activity_stream,
|
|
|
|
:tickets => tickets,
|
|
|
|
:articles => articles,
|
|
|
|
:users => users,
|
|
|
|
}
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-11-07 23:47:05 +00:00
|
|
|
def self.log_view ( object, current_user )
|
|
|
|
history_type = self.history_type_lookup( 'viewed' )
|
|
|
|
history_object = self.history_object_lookup( object.class.name )
|
|
|
|
History.create(
|
|
|
|
:o_id => object.id,
|
|
|
|
:history_type_id => history_type.id,
|
|
|
|
:history_object_id => history_object.id,
|
|
|
|
:created_by_id => current_user.id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2012-10-18 19:23:05 +00:00
|
|
|
def self.recent_viewed( user, limit = 10 )
|
2012-04-10 14:06:46 +00:00
|
|
|
# g = Group.where( :active => true ).joins(:users).where( 'users.id' => user.id )
|
2012-11-07 23:47:05 +00:00
|
|
|
history_type = self.history_type_lookup( 'viewed' )
|
|
|
|
history_object = self.history_object_lookup( 'Ticket' )
|
|
|
|
|
2012-07-23 22:22:23 +00:00
|
|
|
stream = History.select("distinct(o_id), created_by_id, history_type_id, history_object_id, created_at").
|
2012-11-07 23:47:05 +00:00
|
|
|
where( :history_object_id => history_object.id ).
|
|
|
|
where( :history_type_id => history_type.id ).
|
2012-05-02 11:57:23 +00:00
|
|
|
where( :created_by_id => user.id ).
|
2012-07-23 22:22:23 +00:00
|
|
|
order('created_at DESC, id ASC').
|
2012-10-18 19:23:05 +00:00
|
|
|
limit(limit)
|
2012-04-20 12:25:13 +00:00
|
|
|
datas = []
|
2012-04-10 14:06:46 +00:00
|
|
|
stream.each do |item|
|
2012-04-20 12:25:13 +00:00
|
|
|
data = item.attributes
|
2012-11-07 23:54:06 +00:00
|
|
|
data['history_object'] = self.history_object_lookup_id( data['history_object_id'] ).name
|
|
|
|
data['history_type'] = self.history_type_lookup_id( data['history_type_id'] ).name
|
2012-04-20 12:25:13 +00:00
|
|
|
datas.push data
|
2012-04-10 14:06:46 +00:00
|
|
|
# item['history_attribute'] = item.history_attribute
|
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
# puts 'pppppppppp'
|
|
|
|
# puts datas.inspect
|
2012-04-20 12:25:13 +00:00
|
|
|
return datas
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2012-10-18 19:29:56 +00:00
|
|
|
def self.recent_viewed_fulldata( user, limit = 10 )
|
2012-10-18 19:23:05 +00:00
|
|
|
recent_viewed = History.recent_viewed( user, limit )
|
2012-07-23 22:22:23 +00:00
|
|
|
|
|
|
|
# get related users
|
|
|
|
users = {}
|
|
|
|
tickets = []
|
|
|
|
recent_viewed.each {|item|
|
|
|
|
|
|
|
|
# load article ids
|
|
|
|
# if item.history_object == 'Ticket'
|
|
|
|
ticket = Ticket.find( item['o_id'] ).attributes
|
|
|
|
tickets.push ticket
|
|
|
|
# end
|
|
|
|
# if item.history_object 'Ticket::Article'
|
|
|
|
# tickets.push Ticket::Article.find(item.o_id)
|
|
|
|
# end
|
|
|
|
# if item.history_object 'User'
|
|
|
|
# tickets.push User.find(item.o_id)
|
|
|
|
# end
|
|
|
|
|
|
|
|
# load users
|
|
|
|
if !users[ ticket['owner_id'] ]
|
|
|
|
users[ ticket['owner_id'] ] = User.user_data_full( ticket['owner_id'] )
|
|
|
|
end
|
|
|
|
if !users[ ticket['created_by_id'] ]
|
|
|
|
users[ ticket['created_by_id'] ] = User.user_data_full( ticket['created_by_id'] )
|
|
|
|
end
|
|
|
|
if !users[ item['created_by_id'] ]
|
|
|
|
users[ item['created_by_id'] ] = User.user_data_full( item['created_by_id'] )
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
:recent_viewed => recent_viewed,
|
|
|
|
:tickets => tickets,
|
|
|
|
:users => users,
|
|
|
|
}
|
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
private
|
2012-11-07 23:47:05 +00:00
|
|
|
|
|
|
|
def self.history_type_lookup_id( id )
|
|
|
|
|
|
|
|
# use cache
|
|
|
|
return @@cache_type[ id ] if @@cache_type[ id ]
|
|
|
|
|
|
|
|
# lookup
|
|
|
|
history_type = History::Type.find(id)
|
|
|
|
@@cache_type[ id ] = history_type
|
|
|
|
return history_type
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.history_type_lookup( name )
|
|
|
|
|
|
|
|
# use cache
|
|
|
|
return @@cache_type[ name ] if @@cache_type[ name ]
|
|
|
|
|
|
|
|
# lookup
|
|
|
|
history_type = History::Type.where( :name => name ).first
|
|
|
|
if history_type
|
|
|
|
@@cache_type[ name ] = history_type
|
|
|
|
return history_type
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
|
|
|
# create
|
|
|
|
history_type = History::Type.create(
|
|
|
|
:name => name
|
|
|
|
)
|
|
|
|
@@cache_type[ name ] = history_type
|
|
|
|
return history_type
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
|
|
|
def self.history_object_lookup_id( id )
|
|
|
|
|
|
|
|
# use cache
|
|
|
|
return @@cache_object[ id ] if @@cache_object[ id ]
|
|
|
|
|
|
|
|
# lookup
|
|
|
|
history_object = History::Object.find(id)
|
|
|
|
@@cache_object[ id ] = history_object
|
|
|
|
return history_object
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.history_object_lookup( name )
|
|
|
|
|
|
|
|
# use cache
|
|
|
|
return @@cache_object[ name ] if @@cache_object[ name ]
|
|
|
|
|
|
|
|
# lookup
|
|
|
|
history_object = History::Object.where( :name => name ).first
|
|
|
|
if history_object
|
|
|
|
@@cache_object[ name ] = history_object
|
|
|
|
return history_object
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
|
|
|
# create
|
|
|
|
history_object = History::Object.create(
|
|
|
|
:name => name
|
|
|
|
)
|
|
|
|
@@cache_object[ name ] = history_object
|
|
|
|
return history_object
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.history_attribute_lookup( name )
|
|
|
|
|
|
|
|
# use cache
|
|
|
|
return @@cache_attribute[ name ] if @@cache_attribute[ name ]
|
|
|
|
|
|
|
|
# lookup
|
|
|
|
history_attribute = History::Attribute.where( :name => name ).first
|
|
|
|
if history_attribute
|
|
|
|
@@cache_attribute[ name ] = history_attribute
|
|
|
|
return history_attribute
|
|
|
|
end
|
|
|
|
|
|
|
|
# create
|
|
|
|
history_attribute = History::Attribute.create(
|
|
|
|
:name => name
|
|
|
|
)
|
|
|
|
@@cache_attribute[ name ] = history_attribute
|
|
|
|
return history_attribute
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class Object < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
|
|
|
|
class Type < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
|
|
|
|
class Attribute < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|