socialforge/app/controllers/exercise_controller.rb

263 lines
8.9 KiB
Ruby
Raw Normal View History

2015-11-13 16:40:39 +08:00
class ExerciseController < ApplicationController
layout "base_courses"
2015-11-18 10:46:56 +08:00
before_filter :find_exercise_and_course, :only => [:create_exercise_question, :edit, :update, :show]
2015-11-17 19:47:48 +08:00
before_filter :find_course, :only => [:index,:new,:create,:student_exercise_list]
2015-11-18 10:46:56 +08:00
helper :Exercise
2015-11-17 19:29:25 +08:00
2015-11-13 16:40:39 +08:00
def index
2015-11-17 20:23:39 +08:00
remove_invalid_exercise(@course)
@is_teacher = User.current.allowed_to?(:as_teacher,@course)
if @is_teacher
exercises = @course.exercises
else
2015-11-17 19:47:48 +08:00
exercises = @course.exercises.where(:exercise_status => 2)
end
@exercises = paginateHelper exercises,20 #分页
respond_to do |format|
format.html
end
2015-11-13 16:40:39 +08:00
end
2015-11-13 16:40:39 +08:00
def show
2015-11-17 11:25:17 +08:00
@exercise = Exercise.find params[:id]
if @exercise.exercise_status != 2 && (!User.current.allowed_to?(:as_teacher,@course) || User.current.admin?)
render_403
return
end
2015-11-17 18:20:47 +08:00
# 已提交问卷的用户不能再访问该界面
2015-11-17 15:20:59 +08:00
if has_commit_exercise?(@exercise.id, User.current.id) && (!User.current.admin?)
2015-11-17 20:23:14 +08:00
redirect_to exercise_index_url(:course_id=> @course.id)
2015-11-17 11:25:17 +08:00
else
2015-11-17 15:20:59 +08:00
@can_edit_poll = (!has_commit_exercise?(@exercise.id,User.current.id)) || User.current.admin?
@percent = get_percent(@exercise,User.current)
2015-11-17 20:23:14 +08:00
exercise_questions = @exercise.exercise_questions
@exercise_questions = paginateHelper exercise_questions,5 #分页
2015-11-17 11:25:17 +08:00
respond_to do |format|
format.html {render :layout => 'base_courses'}
end
end
2015-11-13 16:40:39 +08:00
end
def new
2015-11-17 19:29:25 +08:00
option = {
:exercise_name => "",
:course_id => @course.id,
:exercise_status => 1,
:user_id => User.current.id,
2015-11-17 20:23:14 +08:00
:time => "",
:end_time => "",
:publish_time => "",
2015-11-18 10:46:56 +08:00
:exercise_description => "",
:show_result => ""
2015-11-17 19:29:25 +08:00
}
@exercise = Exercise.create option
if @exercise
redirect_to edit_exercise_url @exercise.id
2015-11-17 11:25:17 +08:00
end
2015-11-13 16:40:39 +08:00
end
def create
2015-11-17 18:22:31 +08:00
if params[:exercise]
exercise = Exercise.find(params[:exercise_id]) if params[:exercise_id]
exercise ||= Exercise.new
exercise.exercise_name = params[:exercise][:exercise_name]
exercise.exercise_description = params[:exercise][:exercise_description]
exercise.end_time = params[:exercise][:end_time]
exercise.publish_time = params[:exercise][:publish_time]
exercise.user_id = User.current.id
exercise.time = params[:exercise][:time]
exercise.course_id = params[:course_id]
exercise.exercise_status = 1
if exercise.save
@exercise = exercise
respond_to do |format|
format.js
end
end
end
2015-11-13 16:40:39 +08:00
end
def edit
2015-11-17 15:18:07 +08:00
respond_to do |format|
format.html{render :layout => 'base_courses'}
end
2015-11-13 16:40:39 +08:00
end
def update
2015-11-17 19:29:25 +08:00
@exercise.exercise_name = params[:exercise][:exercise_name]
@exercise.exercise_description = params[:exercise][:exercise_description]
@exercise.time = params[:exercise][:time]
@exercise.end_time = params[:exercise][:end_time]
2015-11-17 20:23:14 +08:00
@exercise.publish_time = params[:exercise][:publish_time]
2015-11-18 10:46:56 +08:00
@exercise.publish_time = params[:exercise][:show_result]
2015-11-17 11:25:17 +08:00
if @exercise.save
respond_to do |format|
format.js
end
else
render_404
end
2015-11-13 16:40:39 +08:00
end
def destroy
2015-11-17 18:20:47 +08:00
2015-11-13 16:40:39 +08:00
end
2015-11-17 18:20:47 +08:00
# 统计结果
def statistics_result
2015-11-17 15:20:59 +08:00
@exercise = Exercise.find(params[:id])
2015-11-17 20:23:14 +08:00
exercise_questions = @exercise.exercise_questions
2015-11-17 15:20:59 +08:00
@exercise_questions = paginateHelper exercise_questions, 5
respond_to do |format|
format.html{render :layout => 'base_courses'}
end
end
2015-11-17 18:20:47 +08:00
# 添加题目
# question_type 1单选 2多选 3填空题
2015-11-17 15:20:59 +08:00
def create_exercise_question
2015-11-17 19:49:33 +08:00
question_title = params[:question_title].nil? || params[:question_title].empty? ? l(:label_enter_single_title) : params[:question_title]
2015-11-17 15:20:59 +08:00
option = {
:question_title => question_title,
:question_type => params[:question_type] || 1,
2015-11-18 10:46:56 +08:00
:question_number => @exercise.exercise_questions.count + 1,
:question_score => params[:question_score]
2015-11-17 15:20:59 +08:00
}
2015-11-17 18:20:47 +08:00
@exercise_questions = @exercise.exercise_questions.new option
2015-11-17 15:20:59 +08:00
if params[:question_answer]
for i in 1..params[:question_answer].count
answer = (params[:question_answer].values[i-1].nil? || params[:question_answer].values[i-1].empty?) ? l(:label_new_answer) : params[:question_answer].values[i-1]
question_option = {
2015-11-18 10:46:56 +08:00
:choice_position => i,
:choice_text => answer
2015-11-17 15:20:59 +08:00
}
2015-11-18 10:46:56 +08:00
@exercise_questions.exercise_choices.new question_option
2015-11-17 15:20:59 +08:00
end
end
# 如果是插入的话那么从插入的这个id以后的question_num都将要+1
if params[:quest_id]
@is_insert = true
2015-11-17 18:20:47 +08:00
@exercise.exercise_questions.where("question_number > #{params[:quest_num].to_i}").update_all(" question_number = question_number + 1")
# @exercise_question_num = params[:quest_num].to_i
@exercise_questions.question_number = params[:quest_num].to_i + 1
2015-11-17 15:20:59 +08:00
end
2015-11-17 20:23:14 +08:00
if @exercise_questions.save
2015-11-17 21:13:17 +08:00
standart_answer = ExerciseStandardAnswer.new
standart_answer.exercise_question_id = @exercise_questions.id
2015-11-18 10:46:56 +08:00
@exercise_questions.question_type == 3 ? standart_answer.answer_text = params[:exercise_choice] : standart_answer.exercise_choice_id = params[:exercise_choice]
2015-11-17 21:13:17 +08:00
standart_answer.save
2015-11-17 15:20:59 +08:00
respond_to do |format|
format.js
end
end
end
2015-11-17 18:20:47 +08:00
# 修改题目
# params[:exercise_question] The id of exercise_question
# params[:question_answer] eg:A、B、C选项
def update_exercise_question
@exercise_question = ExerciseQuestion.find params[:exercise_question]
@exercise_questions.question_title = params[:exercise_questions_title].nil? || params[:exercise_questions_title].empty? ? l(:label_enter_single_title) : params[:exercise_questions_title]
################处理选项
if params[:question_answer]
@exercise_question.exercise_answers.each do |answer|
answer.destroy unless params[:question_answer].keys.include? answer.id.to_s
end
# 界面需要判断选择题至少有一个选项
for i in 1..params[:question_answer].count
question = @exercise_question.exercise_answers.find_by_id params[:question_answer].keys[i-1]
answer = (params[:question_answer].values[i-1].nil? || params[:question_answer].values[i-1].empty?) ? l(:label_new_answer) : params[:question_answer].values[i-1]
if question
question.exercise_choices_id = i
question.answer_text = answer
question.save
else
question_option = {
:exercise_choices_id => i,
:answer_text => answer
}
@exercise_question.exercise_answers.new question_option
end
end
end
@exercise_question.save
respond_to do |format|
format.js
end
end
# 删除题目
def delete_exercise_question
@exercise_question = ExerciseQuestion.find params[:exercise_question]
@exercise = @exercise_question.exercise
exercise_questions = @exercise.exercise_questions.where("question_number > #{@exercise_question.question_number}")
2015-11-17 20:23:14 +08:00
exercise_questions.each do |question|
2015-11-17 18:20:47 +08:00
question.question_number -= 1
question.save
end
2015-11-17 18:34:01 +08:00
if @exercise_question && @exercise_question.destroy
2015-11-17 18:20:47 +08:00
respond_to do |format|
format.js
end
end
end
2015-11-17 15:20:59 +08:00
#发布问卷
def publish_excercise
@exercise.exercise_status = 2
@exercise.publish_time = Time.now
if @exercise.save
if params[:is_remote]
2015-11-17 20:23:14 +08:00
redirect_to exercise_index_url(:course_id => @course.id)
2015-11-17 15:20:59 +08:00
else
respond_to do |format|
format.js
end
end
end
end
2015-11-17 09:33:40 +08:00
def student_exercise_list
@is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin?
@exercise = Exercise.find params[:id]
@all_exercises = @course.exercises.order("created_at desc")
@exercise_count = @exercise.exercise_users.where('score is not NULL').count
if @is_teacher || (!@exercise.exercise_users.where(:user_id => User.current.id).empty? && Time.parse(@exercise.end_time.to_s).strftime("%Y-%m-%d-%H-%M-%S") <= Time.now.strftime("%Y-%m-%d-%H-%M-%S"))
@exercise_users_list = @exercise.exercise_users.where('score is not NULL')
2015-11-17 15:18:07 +08:00
@show_all = true;
2015-11-17 09:33:40 +08:00
elsif !@exercise.exercise_users.where(:user_id => User.current.id).empty? && Time.parse(@exercise.end_time.to_s).strftime("%Y-%m-%d-%H-%M-%S") > Time.now.strftime("%Y-%m-%d-%H-%M-%S")
@exercise_users_list = @exercise.exercise_users.where("user_id = ? and score is not NULL",User.current.id)
else
@exercise_users_list = []
end
respond_to do |format|
format.html
end
end
2015-11-13 16:40:39 +08:00
private
2015-11-17 20:23:39 +08:00
def remove_invalid_exercise(course)
exercises = course.exercises.where("exercise_name=?","")
unless exercises.empty?
exercises.each do |exercise|
exercise.destroy
end
end
2015-11-17 19:47:48 +08:00
end
2015-11-17 19:29:25 +08:00
def find_exercise_and_course
@exercise = Exercise.find params[:id]
@course = Course.find @exercise.course_id
rescue Exception => e
render_404
end
2015-11-13 16:40:39 +08:00
def find_course
@course = Course.find params[:course_id]
rescue Exception => e
render_404
end
end