首页动态优化

This commit is contained in:
lizanle 2015-07-24 09:37:53 +08:00
parent ffe12b92c8
commit 8350d5f10b
5 changed files with 73 additions and 23 deletions

View File

@ -250,8 +250,10 @@ module Mobile
end
get "course_dynamic/:id" do
cs = CoursesService.new
p Time.now.to_s
count = cs.all_course_dynamics(params,current_user)
present :data, count, with: Mobile::Entities::CourseDynamic
p Time.now.to_s
present :status, 0
end

View File

@ -4,9 +4,7 @@ module Mobile
include Redmine::I18n
def self.course_dynamic_expose(field)
expose field do |c,opt|
if field == :update_time
(format_time(c[field]) if (c.is_a?(Hash) && c.key?(field)))
elsif field == :news_count
if field == :news_count
obj = nil
c[:dynamics].each do |d|
if d[:type] == 1
@ -45,15 +43,12 @@ module Mobile
end
end
course_dynamic_expose :type
course_dynamic_expose :count
course_dynamic_expose :course_name
course_dynamic_expose :course_term
course_dynamic_expose :course_time
course_dynamic_expose :course_id
course_dynamic_expose :course_img_url
course_dynamic_expose :message
course_dynamic_expose :update_time
course_dynamic_expose :count
course_dynamic_expose :news_count
course_dynamic_expose :document_count
course_dynamic_expose :topic_count
@ -63,15 +58,15 @@ module Mobile
course_dynamic_expose :current_user_is_member
course_dynamic_expose :current_user_is_teacher
expose :documents,using:Mobile::Entities::Attachment do |f,opt|
obj = nil
f[:dynamics].each do |d|
if d[:type] == 3
obj = d[:documents]
end
end
obj
end
# expose :documents,using:Mobile::Entities::Attachment do |f,opt|
# obj = nil
# f[:dynamics].each do |d|
# if d[:type] == 3
# obj = d[:documents]
# end
# end
# obj
# end
expose :topics,using:Mobile::Entities::Message do |f,opt|
obj = nil
f[:dynamics].each do |d|

View File

@ -77,7 +77,7 @@ class NewsController < ApplicationController
scope_order = scope.all(:include => [:author, :course],
:order => "#{News.table_name}.created_on DESC")
else
scope_order = scope.where("news.title like '#{'%' << params[:subject].to_s << '%'}'").all(:include => [:author, :course],
scope_order = scope.where("#{News.table_name}.title like '#{'%' << params[:subject].to_s << '%'}'").all(:include => [:author, :course],
:order => "#{News.table_name}.created_on DESC")
end

View File

@ -1,5 +1,11 @@
# encoding: utf-8
module ApiHelper
ONE_MINUTE = 60 * 1000
ONE_HOUR = 60 * ONE_MINUTE
ONE_DAY = 24 * ONE_HOUR
ONE_MONTH = 30 * ONE_DAY
ONE_YEAR = 12 * ONE_MONTH
#获取用户的工作单位
def get_user_work_unit user
work_unit = ""
@ -163,5 +169,37 @@ module ApiHelper
end
# 获取当前时间
def time_from_now time
lastUpdateTime = time.to_i*1000
currentTime = Time.now.to_i*1000
timePassed = currentTime - lastUpdateTime;
timeIntoFormat = 0
updateAtValue = ""
if timePassed < 0
updateAtValue = "时间有问题"
elsif timePassed < ONE_MINUTE
updateAtValue = "一分钟前"
elsif timePassed < ONE_HOUR
timeIntoFormat = timePassed / ONE_MINUTE
updateAtValue = timeIntoFormat.to_s + "分钟前"
elsif (timePassed < ONE_DAY)
timeIntoFormat = timePassed / ONE_HOUR
updateAtValue = timeIntoFormat.to_s + "小时前"
elsif (timePassed < ONE_MONTH)
timeIntoFormat = timePassed / ONE_DAY
updateAtValue = timeIntoFormat.to_s + "天前"
elsif (timePassed < ONE_YEAR)
timeIntoFormat = timePassed / ONE_MONTH
updateAtValue = timeIntoFormat.to_s + "个月前"
else
timeIntoFormat = timePassed / ONE_YEAR
updateAtValue = timeIntoFormat.to_s + "年前"
end
updateAtValue
end
end

View File

@ -654,18 +654,19 @@ class CoursesService
return
end
if current_user == @user || current_user.admin?
membership = @user.coursememberships.page(1).per(15)
membership = @user.coursememberships
else
membership = @user.coursememberships.page(1).per(15).all(:conditions => Course.visible_condition(current_user))
membership = @user.coursememberships.all(:conditions => Course.visible_condition(current_user))
end
if membership.nil? || membership.count == 0
raise l(:label_no_courses, :locale => get_user_language(current_user))
end
membership.sort! { |older, newer| newer.created_on <=> older.created_on }
#membership.sort! { |older, newer| newer.created_on <=> older.created_on }
#定义一个数组集合存放hash数组该hash数组包括课程的信息并包含课程的最新发布的资源最新的讨论区留言最新的作业最新的通知
result = []
#对用户所有的课程进行循环,找到每个课程最新发布的资源,最新的讨论区留言,最新的作业,最新的通知,并存进数组
membership.each do |mp|
course = mp.course
latest_course_dynamics = []
@ -676,14 +677,12 @@ class CoursesService
latest_course_dynamics << {:type => 1, :time => latest_news.first.created_on,:count=>course.news.count,
:news => latest_news.all}
end
# 课程讨论区
latest_message = course.boards.first.topics.page(1).per(2)
unless latest_message.first.nil?
latest_course_dynamics << {:type => 2, :time => latest_message.first.created_on, :count =>course.boards.nil? ? 0 : course.boards.first.topics.count,
:topics => latest_message.all}
end
# 课程资源
# latest_attachment = course.attachments.order("created_on desc").page(1).per(2)
# unless latest_attachment.first.nil?
@ -701,7 +700,9 @@ class CoursesService
homework_count = course.homework_commons.count
sql = "select users.*,ROUND(sum(student_works.final_score),2) score from student_works left outer join users on student_works.user_id = users.id" <<
" where homework_common_id in ( select id from homework_commons where homework_commons.course_id = #{course.id}) GROUP BY student_works.user_id ORDER BY score desc limit 0,4"
better_students = User.find_by_sql(sql)
# 找出在课程讨论区发帖回帖数最多的
active_students = []
sql1 = " select users.*,count(author_id)*2 active_count from messages " <<
@ -710,6 +711,7 @@ class CoursesService
" GROUP BY messages.author_id ORDER BY count(author_id) desc " <<
" limit 0,4"
active_students = User.find_by_sql(sql1)
if homework_count != 0 && !better_students.empty?
latest_course_dynamics <<{:type=> 6,:time=>"1970-01-01 0:0:0 +0800",:count=> 4,:better_students=> better_students}
end
@ -718,12 +720,25 @@ class CoursesService
end
latest_course_dynamic = latest_course_dynamics.first
unless latest_course_dynamic.nil?
result << {:course_name => course.name,:current_user_is_member => current_user.member_of_course?(course),:current_user_is_teacher => is_course_teacher(current_user,course), :course_id => course.id, :course_img_url => url_to_avatar(course), :course_time => course.time, :course_term => course.term,:message => "", :dynamics => latest_course_dynamics,
:course_student_num=>course ? course.members.count : 0,:time_from_now=> distance_of_time_in_words(Time.now, latest_course_dynamic[:time].to_time) << "",:time=>latest_course_dynamic[:time].to_time}
result << {:course_name => course.name,
:current_user_is_member => current_user.member_of_course?(course),
:current_user_is_teacher => is_course_teacher(current_user,course),
:course_id => course.id,
:course_img_url => url_to_avatar(course),
:course_time => course.time,
:course_term => course.term,
:message => "",
:dynamics => latest_course_dynamics,
:course_student_num=>course ? course.members.count : 0,
#:time_from_now=> distance_of_time_in_words(Time.now, latest_course_dynamic[:time].to_time) << "前",
:time_from_now=>time_from_now(latest_course_dynamic[:time].to_time), #.strftime('%Y-%m-%d %H:%M:%S').to_s,
:time=>latest_course_dynamic[:time].to_time}
end
end
#返回数组集合
result.sort! { |order, newer| newer[:time] <=> order[:time] }
result
end