From d73a2c100336f9f24e488cbfe9513e81ed2e7d8a Mon Sep 17 00:00:00 2001 From: lizanle <491823689@qq.com> Date: Fri, 19 Jun 2015 15:05:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E7=A8=8B=E5=8A=A8=E6=80=81=E6=96=B0?= =?UTF-8?q?=E7=89=88api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/apis/courses.rb | 2 +- app/api/mobile/entities/attachment.rb | 8 +- app/api/mobile/entities/course_dynamic.rb | 103 ++++++++++++++++++++++ app/api/mobile/entities/homework.rb | 10 ++- app/services/courses_service.rb | 95 +++++++++++++++++++- 5 files changed, 214 insertions(+), 4 deletions(-) diff --git a/app/api/mobile/apis/courses.rb b/app/api/mobile/apis/courses.rb index 73d1e357c..c325639f9 100644 --- a/app/api/mobile/apis/courses.rb +++ b/app/api/mobile/apis/courses.rb @@ -247,7 +247,7 @@ module Mobile end get "course_dynamic/:id" do cs = CoursesService.new - count = cs.course_dynamic(params,current_user) + count = cs.all_course_dynamics(params,current_user) present :data, count, with: Mobile::Entities::CourseDynamic present :status, 0 end diff --git a/app/api/mobile/entities/attachment.rb b/app/api/mobile/entities/attachment.rb index 080b24558..b7424fac9 100644 --- a/app/api/mobile/entities/attachment.rb +++ b/app/api/mobile/entities/attachment.rb @@ -1,13 +1,18 @@ module Mobile module Entities class Attachment < Grape::Entity + include Redmine::I18n def self.attachment_expose(field) expose field do |f,opt| if f.is_a?(Hash) && f.key?(field) f[field] elsif f.is_a?(::Attachment) if f.respond_to?(field) - f.send(field) + if field == :created_on + format_time(f.send(field)) + else + f.send(field) + end else #case field # when "" @@ -21,6 +26,7 @@ module Mobile attachment_expose :description attachment_expose :downloads attachment_expose :quotes + attachment_expose :created_on end end end \ No newline at end of file diff --git a/app/api/mobile/entities/course_dynamic.rb b/app/api/mobile/entities/course_dynamic.rb index b6e4630e4..56b83d513 100644 --- a/app/api/mobile/entities/course_dynamic.rb +++ b/app/api/mobile/entities/course_dynamic.rb @@ -6,6 +6,62 @@ module Mobile 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 + obj = nil + c[:dynamics].each do |d| + if d[:type] == 1 + obj = d[:count] + end + end + obj + elsif field == :document_count + obj = nil + c[:dynamics].each do |d| + if d[:type] == 3 + obj = d[:count] + end + end + obj + elsif field == :jour_message_count + obj = nil + c[:dynamics].each do |d| + if d[:type] == 2 + obj = d[:count] + end + end + obj + elsif field == :homework_count + obj = nil + c[:dynamics].each do |d| + if d[:type] == 4 + obj = d[:count] + end + end + obj + elsif field == :homework_submit_num + obj = nil + c[:dynamics].each do |d| + if d[:type] == 4 + obj = d[:submit_count] + end + end + obj + elsif field == :homework_submit_students + obj = nil + c[:dynamics].each do |d| + if d[:type] == 4 + obj = d[:studentlist] + end + end + obj + elsif field == :homework_status + obj = nil + c[:dynamics].each do |d| + if d[:type] == 4 + obj = d[:homework_status] + end + end + obj else c[field] if (c.is_a?(Hash) && c.key?(field)) end @@ -21,6 +77,53 @@ module Mobile 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 :jour_message_count + course_dynamic_expose :homework_count + course_dynamic_expose :homework_submit_students + course_dynamic_expose :homework_submit_num + course_dynamic_expose :homework_status + #在dynamics里解析出四种动态 + expose :document,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 :jour_message,using:Mobile::Entities::Jours do |f,opt| + obj = nil + f[:dynamics].each do |d| + if d[:type] == 2 + obj = d[:jour_message] + end + end + obj + end + expose :homework,using:Mobile::Entities::Homework do |f,opt| + obj = nil + f[:dynamics].each do |d| + if d[:type] == 4 + obj = d[:homework] + end + end + obj + end + + expose :news,using:Mobile::Entities::News do |f,opt| + obj = nil + f[:dynamics].each do |d| + if d[:type] == 1 + obj = d + end + end + obj + end + end end end \ No newline at end of file diff --git a/app/api/mobile/entities/homework.rb b/app/api/mobile/entities/homework.rb index ee623d9ff..18dbe0d3b 100644 --- a/app/api/mobile/entities/homework.rb +++ b/app/api/mobile/entities/homework.rb @@ -1,3 +1,4 @@ +# 这个模块由于作业模块的改变,里边的注释以及属性不可信 module Mobile module Entities class Homework < Grape::Entity @@ -14,7 +15,12 @@ module Mobile if f.respond_to?(field) f.send(field) else - + case field + when :homework_name + f.send(:name) + when :homework_notsubmit_num + f.course.members.count - f.student_works.count + end end end end @@ -60,6 +66,8 @@ module Mobile f[:homework_for_anonymous_comments] if f.is_a?(Hash) && f.key?(:homework_for_anonymous_comments) end + homework_expose :homework_notsubmit_num + end end end \ No newline at end of file diff --git a/app/services/courses_service.rb b/app/services/courses_service.rb index d4a197aff..3856a7642 100644 --- a/app/services/courses_service.rb +++ b/app/services/courses_service.rb @@ -615,9 +615,102 @@ class CoursesService homework_scores end + #app新版api + # + # + #课程动态 + public + def all_course_dynamics params, current_user + #获取当前用户的所有课程 + @user = User.find(params[:id]) + if current_user.nil? && !current_user.admin? && !@user.active? + raise '404' + return + end + if current_user == @user || current_user.admin? + membership = @user.coursememberships.all + else + 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 } + #定义一个数组集合,存放hash数组,该hash数组包括课程的信息,并包含课程的最新发布的资源,最新的讨论区留言,最新的作业,最新的通知 + result = [] + #对用户所有的课程进行循环,找到每个课程最新发布的资源,最新的讨论区留言,最新的作业,最新的通知,并存进数组 + membership.each do |mp| + course = mp.course + latest_course_dynamics = [] + dynamics_count = 0 + # 课程通知 + latest_news = course.news.order("created_on desc").first + unless latest_news.nil? + latest_course_dynamics << {:type => 1, :time => latest_news.created_on,:count=>course.news.count, + :news => latest_news} + dynamics_count += 1 + end + # 课程讨论区 + latest_message = course.journals_for_messages.where("m_parent_id is null").order("created_on desc").first + unless latest_message.nil? + latest_course_dynamics << {:type => 2, :time => latest_message.created_on, :count =>course.journals_for_messages.where('m_parent_id is null').count, :jour_message => latest_message} + dynamics_count += 1 + end + # 课程资源 + latest_attachment = course.attachments.order("created_on desc").first + unless latest_attachment.nil? + latest_course_dynamics << {:type => 3, :time => latest_attachment.created_on,:count =>course.attachments.count , :documents=>latest_attachment} + dynamics_count += 1 + end + #课程作业 已经交的学生列表(暂定显示6人),未交的学生列表,作业的状态 + homework = course.homework_commons.order('created_at desc').first + homework_status = ""; + # 判断作业所处的状态,如果是刚发布,就获取剩余时间 + #如果是匿评状态,显示正在匿评 + #如果是匿评结束,显示匿评结束 + #获取作业提交的前6个人,不足6个显示所有 + studentlist = [] + if !homework.nil? + if homework.homework_type == 1 && homework.homework_detail_manual + case homework.homework_detail_manual.comment_status + when 1 + homework_status = show_homework_deadline homework + when 2 + homework_status = "正在匿评中" + when 3 + homework_status = "匿评已结束" + end + elsif homework.homework_type == 0 + homework_status = "未启用匿评" + elsif homework.homework_type == 2 + homework_status = "编程作业" + else + end + # 获取提交作业的前六个学生的名字 和 头像路径 + homework.student_works.order("created_at desc").page(1).per(6).each do |work| + studentlist << {:image_url=> url_to_avatar(work.user),:user_name=>work.user.realname} + end + latest_course_dynamics << {:type => 4, :time => homework.updated_at, :count=>course.homework_commons.count,:submit_count => homework.student_works.count , :homework => homework, :homework_status => homework_status, :studentlist => studentlist} + dynamics_count += 1 + end + latest_course_dynamics.sort! { |order, newer| newer[:time] <=> order[:time] } + latest_course_dynamic = latest_course_dynamics.first + unless latest_course_dynamic.nil? + result << {:course_name => course.name, :course_id => course.id, :course_img_url => url_to_avatar(course), :course_time => course.time, :course_term => course.term,:message => dynamics_count, :dynamics => latest_course_dynamics, :count => dynamics_count} + end + end + #返回数组集合 + result.sort! { |order, newer| newer[:update_time] <=> order[:update_time] } + result + end + + #计算作业的截止日期,剩余日期 + def show_homework_deadline homework + "截止日期:" << homework.end_time.to_s << ",剩余时间:" << (Date.parse(Time.now.to_s) - Date.parse(homework.end_time.to_s)).to_i.to_s << "天" + end + end -end \ No newline at end of file