From 6d32e812aa95b637902d5e54bcbdb36dbf2bb20d Mon Sep 17 00:00:00 2001 From: cxt Date: Mon, 9 May 2016 11:06:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=AF=BE=E7=A8=8B?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E5=92=8C=E4=BD=9C=E4=B8=9A=E7=9A=84=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/migrate/20160509025404_update_activity.rb | 24 ++++++++++++++++++++ db/schema.rb | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20160509025404_update_activity.rb diff --git a/db/migrate/20160509025404_update_activity.rb b/db/migrate/20160509025404_update_activity.rb new file mode 100644 index 000000000..2a959f5a2 --- /dev/null +++ b/db/migrate/20160509025404_update_activity.rb @@ -0,0 +1,24 @@ +class UpdateActivity < ActiveRecord::Migration + def up + homeworks = HomeworkCommon.where("course_id = 342") + unless homeworks.nil? + homeworks.each do |h| + course_activity = CourseActivity.where("course_act_type=? and course_act_id =?", 'HomeworkCommon', h.id).first + user_activity = UserActivity.where("act_type=? and act_id =?", 'HomeworkCommon', h.id).first + org_activity = OrgActivity.where("org_act_type=? and org_act_id =?", 'HomeworkCommon', h.id).first + if course_activity && h.updated_at > course_activity.updated_at + course_activity.update_column('updated_at', h.updated_at) + end + if user_activity && h.updated_at > user_activity.updated_at + user_activity.update_column('updated_at', h.updated_at) + end + if org_activity && h.updated_at > org_activity.updated_at + org_activity.update_column('updated_at', h.updated_at) + end + end + end + end + + def down + end +end diff --git a/db/schema.rb b/db/schema.rb index 3ffa35872..c96524c5e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20160506104128) do +ActiveRecord::Schema.define(:version => 20160509025404) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false From 644e097dc8bb17ac881b3a3e0d7d14f38b67e107 Mon Sep 17 00:00:00 2001 From: cxt Date: Mon, 9 May 2016 16:16:03 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=85=B3=E8=81=94=E9=A1=B9=E7=9B=AE=E7=9A=84=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/homework_common_controller.rb | 6 +- lib/tasks/update_homework.rake | 65 +++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 lib/tasks/update_homework.rake diff --git a/app/controllers/homework_common_controller.rb b/app/controllers/homework_common_controller.rb index e724846d6..02a6992e8 100644 --- a/app/controllers/homework_common_controller.rb +++ b/app/controllers/homework_common_controller.rb @@ -11,9 +11,9 @@ class HomeworkCommonController < ApplicationController before_filter :member_of_course, :only => [:index] def index - unless params[:page] - update_homework_time(@course.homework_commons) - end + #unless params[:page] + # update_homework_time(@course.homework_commons) + #end @new_homework = HomeworkCommon.new @new_homework.homework_detail_manual = HomeworkDetailManual.new @new_homework.course = @course diff --git a/lib/tasks/update_homework.rake b/lib/tasks/update_homework.rake new file mode 100644 index 000000000..229337dc5 --- /dev/null +++ b/lib/tasks/update_homework.rake @@ -0,0 +1,65 @@ +#coding=utf-8 + +namespace :update_homework do + desc "update homework project time" + def update_homework_time homeworks + unless homeworks.nil? + homeworks.each do |h| + if h.homework_type == 3 + student_works = h.student_work_projects.where("is_leader = 1 && project_id != -1") + time = h.updated_at + unless student_works.nil? + student_works.each do |s| + project = Project.find s.project_id + unless project.nil? && project.gpid.nil? + project_time=project.updated_on + project_time=ForgeActivity.where("project_id=?",project.id).last.updated_at if ForgeActivity.where("project_id=?",project.id).last + if time.strftime('%Y-%m-%d %H:%M:%S') < project_time.strftime('%Y-%m-%d %H:%M:%S') + time = project_time + end + begin + # gitlab端获取默认分支 + g = Gitlab.client + default_branch = g.project(project.gpid).default_branch + changesets = g.commits(project.gpid, :ref_name => default_branch) + changesets_latest_coimmit = changesets[0] + unless changesets[0].blank? + if time.strftime('%Y-%m-%d %H:%M:%S') h.updated_at.strftime('%Y-%m-%d %H:%M:%S') + h.update_column('updated_at', s_time) + course_activity = CourseActivity.where("course_act_type=? and course_act_id =?", 'HomeworkCommon', h.id).first + if course_activity && (time.strftime('%Y-%m-%d %H:%M:%S') > course_activity.updated_at.strftime('%Y-%m-%d %H:%M:%S')) + course_activity.update_column('updated_at', s_time) + end + user_activity = UserActivity.where("act_type=? and act_id =?", 'HomeworkCommon', h.id).first + if user_activity && (time.strftime('%Y-%m-%d %H:%M:%S') > user_activity.updated_at.strftime('%Y-%m-%d %H:%M:%S')) + user_activity.update_column('updated_at', s_time) + end + org_activity = OrgActivity.where("org_act_type=? and org_act_id =?", 'HomeworkCommon', h.id).first + if org_activity && (time.strftime('%Y-%m-%d %H:%M:%S') > org_activity.updated_at.strftime('%Y-%m-%d %H:%M:%S')) + org_activity.update_column('updated_at', s_time) + end + end + end + end + end + end + task :update_homework => :environment do + year = Time.now.year + courses = Course.where("time > ? and is_delete = 0", (year.to_i - 2)) + course_ids = courses.nil? ? '(-1)' : '(' + courses.map{|c| c.id}.join(',') + ')' + homeworks = HomeworkCommon.where("course_id in #{course_ids} and homework_type = 3") + unless homeworks.nil? + update_homework_time homeworks + end + end +end