Merge branch 'dev_course' of https://bdgit.educoder.net/Hjqreturn/educoder into dev_course

dev_course
jingquan huang 6 years ago
commit 8e7859938d

@ -5,6 +5,8 @@ class ApplicationController < ActionController::Base
include RenderExpand
include RenderHelper
include ControllerRescueHandler
include GitHelper
include LoggerHelper
protect_from_forgery prepend: true, unless: -> { request.format.json? }
@ -202,55 +204,6 @@ class ApplicationController < ActionController::Base
# end
end
# 版本库目录空间
def repo_namespace(user_login, shixun_identifier)
"#{user_login}/#{shixun_identifier}.git"
end
# 版本库文件内容,带转码
def git_fle_content(repo_path, path)
begin
content = GitService.file_content(repo_path: repo_path, path: path)["content"]
logger.info("@@@@@@@@@@@@@@@@@@#{content}")
decode_content = nil
if content.present?
content = Base64.decode64(content)
cd = CharDet.detect(content)
logger.info "encoding: #{cd['encoding']} confidence: #{cd['confidence']}"
decode_content =
if cd["encoding"] == 'GB18030' && cd['confidence'] > 0.8
content.encode('UTF-8', 'GBK', {:invalid => :replace, :undef => :replace, :replace => ' '})
else
content.force_encoding('UTF-8')
end
end
decode_content
rescue Exception => e
uid_logger_error(e.message)
raise Educoder::TipException.new("文档内容获取异常")
end
end
# 更新文件代码
# content 文件内容message提交描述
def update_file_content(content, repo_path, path, mail, username, message)
GitService.update_file(repo_path: repo_path, file_path: path, message: message,
content: content, author_name: username, author_email: mail)
end
# 版本库Fork功能
def project_fork(container, original_rep_path, username)
raise Educoder::TipException.new("fork源路径为空,fork失败!") if original_rep_path.blank?
# 将要生成的仓库名字
new_repo_name = "#{username}/#{container.try(:identifier)}#{ Time.now.strftime("%Y%m%d%H%M%S")}"
uid_logger("start fork container: repo_name is #{new_repo_name}")
GitService.fork_repository(repo_path: original_rep_path, fork_repository_path: (new_repo_name + ".git"))
container.update_attributes!(:repo_name => new_repo_name)
end
def start_user_session(user)
session[:user_id] = user.id
session[:ctime] = Time.now.utc.to_i
@ -332,16 +285,6 @@ class ApplicationController < ActionController::Base
end
end
# 以用户id开始的日志定义
def uid_logger(message)
Rails.logger.info("##:#{current_user.try(:id)} --#{message}")
end
# 以用户id开始的日志定义
def uid_logger_error(message)
Rails.logger.error("##:#{current_user.try(:id)} --#{message}")
end
## 输出错误信息
def error_status(message = nil)
@status = -1

@ -0,0 +1,52 @@
module GitHelper
extend ActiveSupport::Concern
# 版本库目录空间
def repo_namespace(user_login, shixun_identifier)
"#{user_login}/#{shixun_identifier}.git"
end
# 版本库文件内容,带转码
def git_fle_content(repo_path, path)
begin
content = GitService.file_content(repo_path: repo_path, path: path)["content"]
logger.info("@@@@@@@@@@@@@@@@@@#{content}")
decode_content = nil
if content.present?
content = Base64.decode64(content)
cd = CharDet.detect(content)
logger.info "encoding: #{cd['encoding']} confidence: #{cd['confidence']}"
decode_content =
if cd["encoding"] == 'GB18030' && cd['confidence'] > 0.8
content.encode('UTF-8', 'GBK', {:invalid => :replace, :undef => :replace, :replace => ' '})
else
content.force_encoding('UTF-8')
end
end
decode_content
rescue Exception => e
uid_logger_error(e.message)
raise Educoder::TipException.new("文档内容获取异常")
end
end
# 更新文件代码
# content 文件内容message提交描述
def update_file_content(content, repo_path, path, mail, username, message)
GitService.update_file(repo_path: repo_path, file_path: path, message: message,
content: content, author_name: username, author_email: mail)
end
# 版本库Fork功能
def project_fork(container, original_rep_path, username)
raise Educoder::TipException.new("fork源路径为空,fork失败!") if original_rep_path.blank?
# 将要生成的仓库名字
new_repo_name = "#{username}/#{container.try(:identifier)}#{ Time.now.strftime("%Y%m%d%H%M%S")}"
uid_logger("start fork container: repo_name is #{new_repo_name}")
GitService.fork_repository(repo_path: original_rep_path, fork_repository_path: (new_repo_name + ".git"))
container.update_attributes!(:repo_name => new_repo_name)
end
end

@ -0,0 +1,16 @@
module LoggerHelper
extend ActiveSupport::Concern
extend LoggerHelper
# 以用户id开始的日志定义
def uid_logger(message)
Rails.logger.info("##:#{current_user.try(:id)} --#{message}")
end
# 以用户id开始的日志定义
def uid_logger_error(message)
Rails.logger.error("##:#{current_user.try(:id)} --#{message}")
end
end

@ -77,11 +77,17 @@ class CoursesController < ApplicationController
# 根据搜索关键字进一步筛选
if params[:search].present?
# REDO:Extension
user_ids = User.includes(user_extension: :school).where("schools.name like ?", "%#{params[:search]}%").pluck(:id)
course_ids = CourseMember.includes(:user, :course).where("course_members.course_id in (?) and course_members.role in (1,2,3)
and CONCAT(users.lastname, users.firstname) like ?", @courses.map(&:id), "%#{params[:search]}%")
.pluck(:course_id)
@courses = @courses.where("name like ?", "%#{params[:search]}%").or(@courses.where(tea_id: user_ids)).or(@courses.where(id: course_ids))
#user_ids = User.includes(user_extension: :school).where("schools.name like ?", "%#{params[:search]}%").pluck(:id)
#course_ids = CourseMember.includes(:user, :course).where("course_members.course_id in (?) and course_members.role in (1,2,3)
# and CONCAT(users.lastname, users.firstname) like ?", @courses.map(&:id), "%#{params[:search]}%")
# .pluck(:course_id)
#@courses = @courses.where("name like ?", "%#{params[:search]}%").or(@courses.where(tea_id: user_ids)).or(@courses.where(id: course_ids))
# 6:21 daiao
sql = %Q{
(course_members.role in(1,2,3) and CONCAT(users.lastname, users.firstname) like :keyword) or courses.name like :keyword
or schools.name like :keyword
}
@courses = @courses.joins(course_members: [user: [user_extension: :school]]).where("#{sql}", keyword: "%#{params[:search]}%").distinct
end
@courses_count = @courses.size
@ -931,7 +937,7 @@ class CoursesController < ApplicationController
@page = params[:page] || 1
@page_size = params[:page_size] || 15
@courses = Course.by_user(current_user).hidden(false).ended(false).deleted(0).by_keywords(params[:search]).distinct
@courses = Course.by_user(current_user).hidden(false).processing.not_deleted.by_keywords(params[:search]).distinct
# @total_count = @courses.count
# offset = 0
@ -988,6 +994,20 @@ class CoursesController < ApplicationController
end
end
def search_slim
courses = current_user.manage_courses.not_deleted.processing
keyword = params[:keyword].to_s.strip
if keyword.present?
courses = courses.where('name LIKE ?', "%#{keyword}%")
end
count = courses.count
courses = paginate(courses)
render_ok(count: count, courses: courses.select(:id, :name).as_json)
end
private
# Use callbacks to share common setup or constraints between actions.

@ -1140,6 +1140,7 @@ class ExercisesController < ApplicationController
exercise_ids = [@exercise.id]
@exercise_status = @exercise.get_exercise_status(current_user.id)
@course_all_members = @course.students
@c_group_counts = @course.course_groups_count
question_types = @exercise.exercise_questions.pluck(:question_type)
@exercise_publish_count = get_user_permission_course(exercise_ids,2).count #判断是否有已发布的分班
@exercise_unpublish_count = get_user_permission_course(exercise_ids,1).count #判断是否有未发布的分班

@ -24,8 +24,7 @@ class GraduationTopicsController < ApplicationController
end
# 当前用户是否已经选过题
# @user_selected = StudentGraduationTopic.where(graduation_topic_id: @graduation_topic, user_id: current_user.id).count > 0
user_graduation_topics = @course.student_graduation_topics.where(user_id: current_user.id, status: [0, 1], graduation_topic_id: @graduation_topic.pluck(:id)) #6.12 -hs
user_graduation_topics = @course.student_graduation_topics.where(user_id: current_user.id, status: [0, 1]) #6.12 -hs
@user_selected = user_graduation_topics.size > 0
## 分页参数
page = params[:page] || 1
@ -45,16 +44,9 @@ class GraduationTopicsController < ApplicationController
end
## 分页参数
@current_user = current_user
course_group_ids = @course.course_members.where(user_id: current_user.id, role: [1,2,3]).pluck(:course_group_id).uniq
#6.11 -hs
if course_group_ids.present?
if course_group_ids.include?(0)
course_group_ids = @course.course_groups.pluck(:id)
end
end
@group_list = CourseGroup.where(id: course_group_ids)
user_graduation_topics = @course.student_graduation_topics.where(user_id: current_user.id,
status: [0, 1])
@user_selected = user_graduation_topics.size > 0
page = params[:page] || 1
limit = params[:limit] || 50
@users_count = @student_graduation_topics.try(:count).to_i

@ -31,11 +31,13 @@ class PollVotesController < ApplicationController
if question_type == 1
if user_votes.present? #用户曾经回答过的,答案选择不一样,否则新建
current_user_answer = user_votes.first
if current_user_answer.poll_answer_id != question_answer_id #如果说更换了答案,则以前的答案删除,并新建记录
if current_user_answer&.poll_answer_id != question_answer_id #如果说更换了答案,则以前的答案删除,并新建记录
current_user_answer.destroy
PollVote.create(vote_answer_params)
else
if question_answer_text.present?
current_user_answer.update_attribute("vote_text", question_answer_text)
end
end
@ -98,9 +100,10 @@ class PollVotesController < ApplicationController
@current_question_necessary = @poll_question.is_necessary
#问答记录存在,且有值,才会有返回值。
@current_question_status = 0
if user_votes.present?
vote_answer_id = user_votes.pluck(:poll_answer_id).reject(&:blank?).size
vote_text_count = user_votes.pluck(:vote_text).reject(&:blank?).size
new_user_votes = question_votes.where(user_id: current_user.id)
if new_user_votes.present?
vote_answer_id = new_user_votes.pluck(:poll_answer_id).reject(&:blank?).size
vote_text_count = new_user_votes.pluck(:vote_text).reject(&:blank?).size
if vote_text_count > 0 || vote_answer_id > 0
@current_question_status = 1
end

@ -956,6 +956,8 @@ class PollsController < ApplicationController
@poll_publish_count = get_user_permission_course(poll_ids,2).count
@poll_unpublish_count = get_user_permission_course(poll_ids,1).count
@course_all_members = @course.students
@poll_group_counts = @course.course_groups_count
if @user_course_identity < Course::STUDENT #当前为老师,而且老师只能查看自己班级的/课堂的问卷
@poll_current_user_status = 0
@poll_users_list = @poll.all_poll_users(current_user.id).distinct #该老师分班的全部学生

@ -240,7 +240,7 @@ class QuestionBanksController < ApplicationController
ActiveRecord::Base.transaction do
new_task = GraduationTask.new
new_task.attributes = task.attributes.dup.except("id", "course_id", "user_id", "quotes", "graduation_task_id",
"course_list_id", "gtask_bank_id")
"course_list_id", "gtask_bank_id", "created_at", "updated_at")
new_task.course_id = course.id
new_task.gtask_bank_id = task.id
new_task.user_id = current_user.id
@ -266,7 +266,7 @@ class QuestionBanksController < ApplicationController
ActiveRecord::Base.transaction do
new_topic = GraduationTopic.new
new_topic.attributes = topic.attributes.dup.except("id", "course_id", "user_id", "graduation_topic_id",
"course_list_id", "gtopic_bank_id")
"course_list_id", "gtopic_bank_id", "created_at", "updated_at")
new_topic.course_id = course.id
new_topic.gtopic_bank_id = topic.id
new_topic.user_id = current_user.id

@ -61,7 +61,7 @@ class UsersController < ApplicationController
# 新消息数
@new_message = @user.tidings.where("created_at > '#{@user.click_time}'").count > 0 || @user.private_messages.where("created_at > '#{@user.click_time}'").count > 0
@user_url = "#{@old_domain}/users/#{@user.login}"
@user_url = "/users/#{@user.login}"
@career = Career.where(status: true).order("created_at asc").pluck(:id, :name)
ec_user = EcSchoolUser.where(:user_id => current_user.id).first
@auth = ec_user ? "#{@old_domain}/ecs/department?school_id=#{ec_user.school_id}" : nil

@ -14,7 +14,7 @@ module GraduationTasksHelper
# 交叉评阅
def cross_comment task, user_id
if task.cross_comment
if task.cross_comment && task.status >= 3
[{id: 1, name: "只看我的交叉评阅", count: task.graduation_work_comment_assignations.myself(user_id).count}]
else
[]

@ -61,6 +61,8 @@ class Course < ApplicationRecord
scope :hidden, ->(is_hidden = true) { where(is_hidden: is_hidden) }
scope :ended, ->(is_end = true) { where(is_end: is_end) }
scope :processing, -> { where(is_end: false) }
scope :not_deleted, -> { where(is_delete: 0) }
scope :deleted, ->(is_delete = 1) { where(is_delete: is_delete) }
scope :by_user, ->(user) { joins(:course_members).where('course_members.user_id = ?', user.id).order(updated_at: :desc) }
scope :by_keywords, lambda { |keywords|

@ -56,11 +56,10 @@ class Message < ApplicationRecord
from_board = Board.find(messages.first.board_id)
root_ids = messages.map(&:id)
children = Message.where(parent_id: root_ids).select(:id).to_a
children_ids = children.map(&:id)
second_children = Message.where(parent_id: children_ids).select(:id).to_a
children_ids = Message.where(parent_id: root_ids).pluck(:id)
second_children_ids = Message.where(parent_id: children_ids).pluck(:id)
ids = root_ids.concat(children_ids).concat(second_children.map(&:id)).uniq
ids = root_ids.concat(children_ids).concat(second_children_ids).uniq
ActiveRecord::Base.transaction do
Message.where(id: ids, board_id: from_board.id).update_all(board_id: to_board.id)

@ -11,7 +11,7 @@ class Users::CourseService
end
def call
courses = category_scope_courses.deleted(false)
courses = category_scope_courses.not_deleted
courses = status_filter(courses)
@ -38,9 +38,9 @@ class Users::CourseService
case params[:status]
when 'processing' then
relations.ended(false)
relations.processing
when 'end' then
relations.ended(true)
relations.ended
else
relations
end

@ -0,0 +1,111 @@
class ExercisePublishTask
include ExercisesHelper
include GitHelper
def publish
Rails.logger.info("log--------------------------------exercise_publish start")
puts "--------------------------------exercise_publish start"
exercises = Exercise.where("publish_time is not null and exercise_status = 1 and publish_time <=?",Time.now)
exercises.each do |exercise|
exercise.update_column('exercise_status', 2)
course = exercise.course
tid_str = ""
course.teachers.find_each do |member|
tid_str += "," if tid_str != ""
tid_str += "(#{member.user_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
if exercise.unified_setting
course.student.find_each do |student|
tid_str += "," if tid_str != ""
tid_str += "(#{student.student_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
end
if tid_str != ""
tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str
ActiveRecord::Base.connection.execute tid_sql
end
if exercise.exercise_users.count == 0
str = ""
course.students.find_each do |student|
str += "," if str != ""
str += "(#{student.user_id}, #{exercise.id}, 0, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
if str != ""
sql = "insert into exercise_users (user_id, exercise_id, commit_status, created_at, updated_at) values" + str
ActiveRecord::Base.connection.execute sql
end
end
if exercise.course_acts.size == 0
exercise.course_acts << CourseActivity.new(:user_id => exercise.user_id,:course_id => exercise.course_id)
end
end
# 分组设置发布时间的测验
exercise_group_settings = ExerciseGroupSetting.where("publish_time < ? and publish_time > ?", Time.now + 1800, Time.now - 1800)
exercise_group_settings.each do |exercise_group|
exercise = exercise_group.exercise
if exercise.present?
course = exercise.course
exercise.update_attributes(:exercise_status => 2) if exercise.exercise_status == 1
tid_str = ""
members = course.students.where(:course_group_id => exercise_group.course_group_id)
members.find_each do |member|
tid_str += "," if tid_str != ""
tid_str += "(#{member.user_id},#{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
if tid_str != ""
tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str
ActiveRecord::Base.connection.execute tid_sql
end
end
end
Rails.logger.info("log--------------------------------exercise_publish end")
puts "--------------------------------exercise_publish end"
end
def end
exercises = Exercise.where("end_time <=? and exercise_status = 2",Time.now)
exercises.each do |exercise|
course = exercise.course
exercise.update_column('exercise_status', 3)
exercise.exercise_users.each do |exercise_user|
if exercise_user.commit_status == 0 && !exercise_user.start_at.nil?
exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true)
s_score = calculate_student_score(exercise, exercise_user.user)[:total_score]
exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (exercise_user.subjective_score && exercise_user.subjective_score > 0 ? exercise_user.subjective_score : 0)))
if exercise_user.user.exercise_answers.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty?
exercise_user.update_attributes(:subjective_score => 0)
end
end
end
end
all_exercises = Exercise.where("end_time > ? and exercise_status = 2",Time.now)
exercise_ids = all_exercises.blank? ? "(-1)" : "(" + all_exercises.map(&:id).join(",") + ")"
ExerciseGroupSetting.where("end_time <= '#{Time.now}' and exercise_id in #{exercise_ids}").each do |exercise_setting|
exercise = exercise_setting.exercise
users = exercise.course.students.where(:course_group_id => exercise_setting.course_group_id)
exercise_users = exercise.exercise_users.where(:user_id => users.map(&:user_id))
exercise_users.each do |exercise_user|
if exercise_user.commit_status == 0 && !exercise_user.start_at.nil?
exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true)
s_score = calculate_student_score(exercise, exercise_user.user)[:total_score]
exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (exercise_user.subjective_score && exercise_user.subjective_score > 0 ? exercise_user.subjective_score : 0)))
if exercise_user.user.exercise_answers.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty?
exercise_user.update_attributes(:subjective_score => 0)
end
end
end
end
Rails.logger.info("log--------------------------------exercise_end completed")
end
end

@ -10,7 +10,7 @@ json.courses do
json.tasks_count get_tasks_count course
json.visits course.visits
json.is_public course.is_public
json.is_accessible course.is_public? ? true : (course.tea_id == @user.id || @user.course_identity(course) < 5)
json.is_accessible course.is_public == 1 || @user.course_identity(course) < Course::NORMAL
json.is_end course.is_end
json.first_category_url module_url(course.course_modules.first, course)
end

@ -15,6 +15,7 @@ json.exercise_types do
json.exercise_id @exercise.id
json.subjective @subjective_type #是否包含主观题1为包括0为不包括
json.exercise_end_time ((@ex_user_end_time.nil? || @ex_user_end_time < Time.now) ? "--" : how_much_time(@ex_user_end_time))
json.groups_count @c_group_counts
end
if @exercise_current_user_status == 0 #当为老师的时候
@ -29,7 +30,6 @@ if @exercise_current_user_status == 0 #当为老师的时候
else
json.course_groups []
end
end
if @current_user_ex_answers.present?

@ -7,13 +7,8 @@ json.edit_url edit_course_graduation_topic_path(@course, graduation_topic_id: @g
json.users_count @users_count
json.course_identity @current_user.course_identity(@course)
json.user_selected_topic @graduation_topic.user_status(current_user.id)
json.group_list do
json.array! @group_list do |group|
json.course_group_id group.id
json.group_name group.name
end
end
json.group_list @course.teacher_group(current_user.id)
json.user_selected @user_selected
# 列表数据
json.users_list do

@ -16,6 +16,7 @@ json.poll_types do
json.user_permission @poll_current_user_status #当前用户存在且为课堂教师/管理员/超级管理员时为0 其他否则为1
json.poll_id @poll.id
json.poll_end_time @poll.end_time
json.groups_count @poll_group_counts
end
if @poll_current_user_status == 0

@ -303,6 +303,7 @@ Rails.application.routes.draw do
post 'search_course_list'
get 'board_list'
get 'mine'
get 'search_slim'
end
resources :polls, only:[:index,:new,:create] do

@ -2,8 +2,8 @@ class InitUseIndex < ActiveRecord::Migration[5.2]
def change
remove_index :users, name: :index_users_on_login if index_exists?(:users, :login, name: :index_users_on_login)
remove_index :users, name: :index_users_on_mail if index_exists?(:users, :mail, name: :index_users_on_mail)
add_index :users, :login, unique: true
add_index :users, :mail, unique: true
add_index :users, :phone, unique: true
# add_index :users, :login, unique: true
# add_index :users, :mail, unique: true
# add_index :users, :phone, unique: true
end
end

@ -1,5 +1,5 @@
class AddTestSetAverageForChallenges < ActiveRecord::Migration[5.2]
def change
add_column :challenges, :test_set_average, :boolean, :default => false
add_column :challenges, :test_set_average, :boolean, :default => true
end
end

@ -1,5 +0,0 @@
class ChangeTestSetAvergerForChallenges < ActiveRecord::Migration[5.2]
def change
change_column :challenges, :test_set_average, :boolean, :default => true
end
end

@ -0,0 +1,5 @@
class ChangeDefaultToMaxNum < ActiveRecord::Migration[5.2]
def change
change_column :graduation_tasks, :max_num, :integer, default: 5
end
end

@ -0,0 +1,26 @@
namespace :course_board do
desc 'transfer children message to root message board'
task children_message_transfer: :environment do
Board.find_each do |board|
logger("Current transfer board id: #{board.id} ~")
root_subquery = board.messages.where(parent_id: nil).reorder(nil).pluck(:id)
if root_subquery.present?
children_subquery = Message.where(parent_id: root_subquery).reorder(nil).pluck(:id)
if children_subquery.present?
second_children_subquery = Message.where(parent_id: children_subquery).reorder(nil).pluck(:id)
ids = children_subquery.concat(second_children_subquery).uniq
Message.where(id: ids).update_all(board_id: board.id)
end
end
Board.reset_counters(board.id, :messages)
logger("transfer success ~")
end
end
def logger(msg)
puts msg
end
end

@ -1,211 +1,268 @@
#coding=utf-8
namespace :exercise_publish do
desc "publish exercise and end exercise"
def get_mulscore(question, user)
ecs = ExerciseAnswer.where("user_id =? and exercise_question_id =?", user.id, question.id)
arr = []
ecs.each do |ec|
arr << ec.exercise_choice.choice_position
end
#arr = arr.sort
str = arr.sort.join("")
return str
end
def tran_base64_decode64 str
if str.blank?
str
else
s_size = str.size % 4
if s_size != 0
str += "=" * (4 - s_size)
end
Base64.decode64(str.tr("-_", "+/")).force_encoding("utf-8")
end
end
# def calculate_student_score(exercise, user)
# score = 0
# score1 = 0
# score2 = 0
# score3 = 0
# score4 = 0
# exercise_qustions = exercise.exercise_questions
# exercise_qustions.each do |question|
# if question.question_type != 5
# answer = question.exercise_answers.where("#{ExerciseAnswer.table_name}.user_id = #{user.id}")
# if question.question_type == 3
# standard_answer =[]
# question.exercise_standard_answers.each do |answer|
# standard_answer << answer.answer_text.strip.downcase
# #coding=utf-8
#
# namespace :exercise_publish do
# desc "publish exercise and end exercise"
# def get_mulscore(question, user)
# ecs = ExerciseAnswer.where("user_id =? and exercise_question_id =?", user.id, question.id)
# arr = []
# ecs.each do |ec|
# arr << ec.exercise_choice.choice_position
# end
# #arr = arr.sort
# str = arr.sort.join("")
# return str
# end
#
# def tran_base64_decode64 str
# if str.blank?
# str
# else
# standard_answer = question.exercise_standard_answers.first
# s_size = str.size % 4
# if s_size != 0
# str += "=" * (4 - s_size)
# end
# Base64.decode64(str.tr("-_", "+/")).force_encoding("utf-8")
# end
# end
#
# unless answer.empty?
# # 问答题有多个答案
# if question.question_type == 3 && !standard_answer.empty?
# if standard_answer.include?(answer.first.answer_text.strip.downcase)
# score1 = score1+ question.question_score unless question.question_score.nil?
# #计算试卷的总分和试卷的答题状态
# def calculate_student_score(exercise,user)
# score1 = 0.0 #选择题/判断题
# score2 = 0.0 #填空题
# score5 = 0.0 #实训题
# ques_stand = [] #问题是否正确
# exercise_questions = exercise.exercise_questions
# exercise_questions.each do |q|
# if q.question_type != 5
# answers_content = q.exercise_answers.search_answer_users("user_id",user.id) #学生的答案
# else
# answers_content = q.exercise_shixun_answers.search_shixun_answers("user_id",user.id) #学生的答案
# end
# elsif question.question_type == 1 && !standard_answer.nil?
# if answer.first.exercise_choice.choice_position == standard_answer.exercise_choice_id
# score2 = score2 + question.question_score unless question.question_score.nil?
# if q.question_type <= 2 #为选择题或判断题时
# answer_choice_array = []
# answers_content.each do |a|
# answer_choice_array.push(a.exercise_choice.choice_position) #学生答案的位置
# end
# elsif question.question_type == 2 && !standard_answer.nil?
# arr = get_mulscore(question, user)
# if arr.to_i == standard_answer.exercise_choice_id
# score3 = score3 + question.question_score unless question.question_score.nil?
# user_answer_content = answer_choice_array.sort
# standard_answer = q.exercise_standard_answers.pluck(:exercise_choice_id).sort #该问题的标准答案,可能有多个
# if user_answer_content == standard_answer #答案一致,多选或单选才给分,答案不对不给分
# if standard_answer.count > 0
# multi_each_score = (q.question_score / standard_answer.count) #当多选答案正确时每个answer的分数均摊。
# else
# multi_each_score = 0.0
# end
# answers_content.update_all(:score => multi_each_score)
# score1 = score1 + q.question_score
# end
# elsif q.question_type == 3 #填空题
# null_standard_answer = q.exercise_standard_answers
# standard_answer_array = null_standard_answer.select(:exercise_choice_id,:answer_text)
# standard_answer_ids = standard_answer_array.pluck(:exercise_choice_id).reject(&:blank?).uniq #标准答案的exercise_choice_id数组
# standard_answer_count = standard_answer_ids.count
# if standard_answer_count > 0 #存在标准答案时才有分数
# each_standard_score = (q.question_score.to_f / standard_answer_count).round(1) #每一空的得分
# else
# each_standard_score = 0.0
# end
# if q.is_ordered
# answers_content.each do |u|
# i_standard_answer = standard_answer_array.where(exercise_choice_id:u.exercise_choice_id).pluck(:answer_text).reject(&:blank?).map!(&:downcase) #该选项的全部标准答案
# if i_standard_answer.include?(u.answer_text.downcase) #该空的标准答案包含用户的答案才有分数
# u.update_attribute("score",each_standard_score)
# score2 = score2 + each_standard_score
# end
# end
# else
# question.exercise_shixun_challenges.each do |exercise_cha|
# game = Game.where(:user_id => user.id, :challenge_id => exercise_cha.challenge_id).first
# st_answer_text = standard_answer_array.pluck(:answer_text).reject(&:blank?).map!(&:downcase)
# answers_content.each do |u|
# u_answer_text = u.answer_text.downcase
# if st_answer_text.include?(u_answer_text) #只要标准答案包含用户的答案,就有分数。同时,下一次循环时,就会删除该标准答案。防止用户的相同答案获分
# u.update_attribute("score",each_standard_score)
# score2 = score2 + each_standard_score
# st_answer_text.delete(u_answer_text)
# end
# end
# end
# elsif q.question_type == 5 #实训题时,主观题这里不评分
# q.exercise_shixun_challenges.each do |exercise_cha|
# game = Game.user_games(user.id,exercise_cha.challenge_id).first #当前用户的关卡
# if game.present?
# exercise_cha_score = 0
# answer_status = 0
# cha_path = exercise_cha.challenge.path.present? ? exercise_cha.challenge.path.split("") : []
# challeng_path = cha_path.reject(&:blank?)[0].try(:strip)
# cha_path = exercise_cha.challenge.path.present? ? exercise_cha.challenge.path.split("").reject(&:blank?) : []
# if game.status == 2 && game.final_score >= 0
# exercise_cha_score = exercise_cha.question_score
# exercise_cha_score = game.real_score exercise_cha.question_score #每一关卡的得分
# answer_status = 1
# end
# if exercise_cha.exercise_shixun_answers.where(:user_id => user.id).empty?
# if GameCode.where(:game_id => game.try(:id), :path => challeng_path).first.present?
# game_code = GameCode.where(:game_id => game.try(:id), :path => challeng_path).first
# if exercise_cha.exercise_shixun_answers.search_shixun_answers("user_id",user.id).blank? #把关卡的答案存入试卷的实训里
# game_challenge = game.game_codes.search_challenge_path(cha_path).first
# if game_challenge.present?
# game_code = game_challenge
# code = game_code.try(:new_code)
# else
# begin
# g = Gitlab.client
# Rails.logger.info "commit_exercise_path---- #{challeng_path}"
# if game.present?
# code = g.files(game.myshixun.gpid, challeng_path, "master").try(:content)
# content = GitService.file_content(repo_path: exercise_cha.shixun.repo_path, path: cha_path)["content"]
# decode_content = nil
# if content.present?
# content = Base64.decode64(content)
# cd = CharDet.detect(content)
# decode_content =
# if cd["encoding"] == 'GB18030' && cd['confidence'] > 0.8
# content.encode('UTF-8', 'GBK', {:invalid => :replace, :undef => :replace, :replace => ' '})
# else
# code = g.files(question.shixun.gpid, challeng_path, "master").try(:content)
# content.force_encoding('UTF-8')
# end
# code = tran_base64_decode64(code)
# end
# decode_content
# rescue Exception => e
# @error_messages = e.message
# Rails.logger.info "commit_exercise---- #{@error_messages}"
# end
# end
# ExerciseShixunAnswer.create(:exercise_question_id => question.id, :exercise_shixun_challenge_id => exercise_cha.id, :user_id => user.id,
# :score => exercise_cha_score, :answer_text => code, :status => answer_status)
#
# end
# score4 += exercise_cha_score
# end
# end
# end
# end
# score = score1 + score2 + score3 + score4
# end
task :publish => :environment do
Rails.logger.info("log--------------------------------exercise_publish start")
puts "--------------------------------exercise_publish start"
exercises = Exercise.where("publish_time is not null and exercise_status = 1 and publish_time <=?",Time.now)
exercises.each do |exercise|
exercise.update_column('exercise_status', 2)
course = exercise.course
tid_str = ""
course.teachers.find_each do |member|
tid_str += "," if tid_str != ""
tid_str += "(#{member.user_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
if exercise.unified_setting
course.student.find_each do |student|
tid_str += "," if tid_str != ""
tid_str += "(#{student.student_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
end
if tid_str != ""
tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str
ActiveRecord::Base.connection.execute tid_sql
end
if exercise.exercise_users.count == 0
str = ""
course.student.find_each do |student|
str += "," if str != ""
str += "(#{student.user_id}, #{exercise.id}, 0, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
if str != ""
sql = "insert into exercise_users (user_id, exercise_id, commit_status, created_at, updated_at) values" + str
ActiveRecord::Base.connection.execute sql
end
end
if exercise.course_acts.size == 0
exercise.course_acts << CourseActivity.new(:user_id => exercise.user_id,:course_id => exercise.course_id)
end
end
# 分组设置发布时间的测验
exercise_group_settings = ExerciseGroupSetting.where("publish_time < ? and publish_time > ?", Time.now + 1800, Time.now - 1800)
exercise_group_settings.each do |exercise_group|
exercise = exercise_group.exercise
if exercise.present?
course = exercise.course
exercise.update_attributes(:exercise_status => 2) if exercise.exercise_status == 1
tid_str = ""
members = course.students.where(:course_group_id => exercise_group.course_group_id)
members.find_each do |member|
tid_str += "," if tid_str != ""
tid_str += "(#{member.user_id},#{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
if tid_str != ""
tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str
ActiveRecord::Base.connection.execute tid_sql
end
end
end
Rails.logger.info("log--------------------------------exercise_publish end")
puts "--------------------------------exercise_publish end"
end
task :end => :environment do
exercises = Exercise.where("end_time <=? and exercise_status = 2",Time.now)
exercises.each do |exercise|
course = exercise.course
exercise.update_column('exercise_status', 3)
exercise.exercise_users.each do |exercise_user|
if exercise_user.commit_status == 0 && !exercise_user.start_at.nil?
exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true)
s_score = calculate_student_score(exercise, exercise_user.user)[:total_score]
exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (exercise_user.subjective_score && exercise_user.subjective_score > 0 ? exercise_user.subjective_score : 0)))
if exercise_user.user.exercise_answer.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty?
exercise_user.update_attributes(:subjective_score => 0)
end
end
end
end
all_exercises = Exercise.where("end_time > ? and exercise_status = 2",Time.now)
exercise_ids = all_exercises.blank? ? "(-1)" : "(" + all_exercises.map(&:id).join(",") + ")"
ExerciseGroupSetting.where("end_time <= '#{Time.now}' and exercise_id in #{exercise_ids}").each do |exercise_setting|
exercise = exercise_setting.exercise
users = exercise.course.students.where(:course_group_id => exercise_setting.course_group_id)
exercise_users = exercise.exercise_users.where(:user_id => users.map(&:user_id))
exercise_users.each do |exercise_user|
if exercise_user.commit_status == 0 && !exercise_user.start_at.nil?
exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true)
s_score = calculate_student_score(exercise, exercise_user.user)[:total_score]
exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (exercise_user.subjective_score && exercise_user.subjective_score > 0 ? exercise_user.subjective_score : 0)))
if exercise_user.user.exercise_answer.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty?
exercise_user.update_attributes(:subjective_score => 0)
end
end
end
end
end
end
# uid_logger_error(e.message)
# raise Educoder::TipException.new("文档内容获取异常")
# end
# code = git_fle_content(exercise_cha.shixun.repo_path,cha_path)
# end
# sx_option = {
# :exercise_question_id => q.id,
# :exercise_shixun_challenge_id => exercise_cha.id,
# :user_id => user.id,
# :score => exercise_cha_score,
# :answer_text => code,
# :status => answer_status
# }
# ex_shixun_answer = ExerciseShixunAnswer.new(sx_option)
# ex_shixun_answer.save!
# end
# score5 += exercise_cha_score
# end
# end
# end
# user_scores = answers_content.score_reviewed.pluck(:score).sum
# if user_scores > 0
# stand_answer = 1
# else
# stand_answer = 0
# end
# ques_option = {
# "q_id":q.id, #该问题的id
# "q_type":q.question_type,
# "q_position":q.question_number, #该问题的位置
# "stand_status":stand_answer, #该问题是否正确,1为正确0为错误
# "user_score":user_scores #每个问题的总得分
# }
# ques_stand.push(ques_option)
# end
# total_score = score1 + score2 + score5
# {
# "total_score":total_score,
# "stand_status":ques_stand
# }
# end
#
# task :publish => :environment do
# Rails.logger.info("log--------------------------------exercise_publish start")
# puts "--------------------------------exercise_publish start"
# exercises = Exercise.where("publish_time is not null and exercise_status = 1 and publish_time <=?",Time.now)
# exercises.each do |exercise|
# exercise.update_column('exercise_status', 2)
# course = exercise.course
# tid_str = ""
# course.teachers.find_each do |member|
# tid_str += "," if tid_str != ""
# tid_str += "(#{member.user_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
# end
# if exercise.unified_setting
# course.student.find_each do |student|
# tid_str += "," if tid_str != ""
# tid_str += "(#{student.student_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
# end
# end
# if tid_str != ""
# tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str
# ActiveRecord::Base.connection.execute tid_sql
# end
#
# if exercise.exercise_users.count == 0
# str = ""
# course.student.find_each do |student|
# str += "," if str != ""
# str += "(#{student.user_id}, #{exercise.id}, 0, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
# end
#
# if str != ""
# sql = "insert into exercise_users (user_id, exercise_id, commit_status, created_at, updated_at) values" + str
# ActiveRecord::Base.connection.execute sql
# end
# end
#
# if exercise.course_acts.size == 0
# exercise.course_acts << CourseActivity.new(:user_id => exercise.user_id,:course_id => exercise.course_id)
# end
# end
#
# # 分组设置发布时间的测验
# exercise_group_settings = ExerciseGroupSetting.where("publish_time < ? and publish_time > ?", Time.now + 1800, Time.now - 1800)
# exercise_group_settings.each do |exercise_group|
# exercise = exercise_group.exercise
# if exercise.present?
# course = exercise.course
# exercise.update_attributes(:exercise_status => 2) if exercise.exercise_status == 1
# tid_str = ""
# members = course.students.where(:course_group_id => exercise_group.course_group_id)
# members.find_each do |member|
# tid_str += "," if tid_str != ""
# tid_str += "(#{member.user_id},#{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
# end
# if tid_str != ""
# tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str
# ActiveRecord::Base.connection.execute tid_sql
# end
# end
# end
# Rails.logger.info("log--------------------------------exercise_publish end")
# puts "--------------------------------exercise_publish end"
# end
#
# task :end => :environment do
# # include ExercisesHelper
# # include ApplicationController
#
# exercises = Exercise.where("end_time <=? and exercise_status = 2",Time.now)
# exercises.each do |exercise|
# course = exercise.course
# exercise.update_column('exercise_status', 3)
#
# exercise.exercise_users.each do |exercise_user|
# if exercise_user.commit_status == 0 && !exercise_user.start_at.nil?
# exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true)
#
# s_score = calculate_student_score(exercise, exercise_user.user)[:total_score]
# exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (exercise_user.subjective_score && exercise_user.subjective_score > 0 ? exercise_user.subjective_score : 0)))
# if exercise_user.user.exercise_answers.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty?
# exercise_user.update_attributes(:subjective_score => 0)
# end
# end
# end
# end
#
# all_exercises = Exercise.where("end_time > ? and exercise_status = 2",Time.now)
# exercise_ids = all_exercises.blank? ? "(-1)" : "(" + all_exercises.map(&:id).join(",") + ")"
# ExerciseGroupSetting.where("end_time <= '#{Time.now}' and exercise_id in #{exercise_ids}").each do |exercise_setting|
# exercise = exercise_setting.exercise
#
# users = exercise.course.students.where(:course_group_id => exercise_setting.course_group_id)
# exercise_users = exercise.exercise_users.where(:user_id => users.map(&:user_id))
#
# exercise_users.each do |exercise_user|
# if exercise_user.commit_status == 0 && !exercise_user.start_at.nil?
# exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true)
#
# s_score = calculate_student_score(exercise, exercise_user.user)[:total_score]
# exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (exercise_user.subjective_score && exercise_user.subjective_score > 0 ? exercise_user.subjective_score : 0)))
# if exercise_user.user.exercise_answers.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty?
# exercise_user.update_attributes(:subjective_score => 0)
# end
# end
# end
# end
# Rails.logger.info("log--------------------------------exercise_end completed")
# end
# end

Loading…
Cancel
Save