|
|
class ExerciseController < ApplicationController
|
|
|
layout "base_courses"
|
|
|
|
|
|
before_filter :find_course, :only => [:index,:new,:create,:student_exercise_list]
|
|
|
def index
|
|
|
@is_teacher = User.current.allowed_to?(:as_teacher,@course)
|
|
|
if @is_teacher
|
|
|
exercises = @course.exercises
|
|
|
else
|
|
|
exercises = @course.exercises.where(:exercise_status => 1)
|
|
|
end
|
|
|
@exercises = paginateHelper exercises,20 #分页
|
|
|
respond_to do |format|
|
|
|
format.html
|
|
|
end
|
|
|
end
|
|
|
|
|
|
def show
|
|
|
@exercise = Exercise.find params[:id]
|
|
|
if @exercise.exercise_status != 2 && (!User.current.allowed_to?(:as_teacher,@course) || User.current.admin?)
|
|
|
render_403
|
|
|
return
|
|
|
end
|
|
|
#已提交问卷的用户不能再访问该界面
|
|
|
if has_commit_exercise?(@exercise.id, User.current.id) && (!User.current.admin?)
|
|
|
redirect_to poll_index_url(:course_id=> @course.id)
|
|
|
else
|
|
|
@can_edit_poll = (!has_commit_exercise?(@exercise.id,User.current.id)) || User.current.admin?
|
|
|
@percent = get_percent(@exercise,User.current)
|
|
|
poll_questions = @poll.poll_questions
|
|
|
@poll_questions = paginateHelper poll_questions,5 #分页
|
|
|
respond_to do |format|
|
|
|
format.html {render :layout => 'base_courses'}
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
|
|
|
def new
|
|
|
@exercise = Exercise.new
|
|
|
respond_to do |format|
|
|
|
format.html{render :layout => 'base_courses'}
|
|
|
end
|
|
|
end
|
|
|
|
|
|
def create
|
|
|
|
|
|
end
|
|
|
|
|
|
def edit
|
|
|
respond_to do |format|
|
|
|
format.html{render :layout => 'base_courses'}
|
|
|
end
|
|
|
end
|
|
|
|
|
|
def update
|
|
|
@exercise.exercise_name = params[:exercise_name]
|
|
|
@exercise.exercise_description = params[:exercise_name]
|
|
|
@exercise.start_at = params[:start_at]
|
|
|
@exercise.end_at = params[:end_at]
|
|
|
if @exercise.save
|
|
|
respond_to do |format|
|
|
|
format.js
|
|
|
end
|
|
|
else
|
|
|
render_404
|
|
|
end
|
|
|
end
|
|
|
|
|
|
def destroy
|
|
|
if @exercise && @exercise.destroy
|
|
|
if @is_teacher
|
|
|
polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id}")
|
|
|
else
|
|
|
polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id} and polls_status = 2")
|
|
|
end
|
|
|
@polls = paginateHelper polls,20 #分页
|
|
|
respond_to do |format|
|
|
|
format.js
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
|
|
|
#统计结果
|
|
|
def statistics_result
|
|
|
@exercise = Exercise.find(params[:id])
|
|
|
exercise_questions = @exercise.poll_questions
|
|
|
@exercise_questions = paginateHelper exercise_questions, 5
|
|
|
respond_to do |format|
|
|
|
format.html{render :layout => 'base_courses'}
|
|
|
end
|
|
|
end
|
|
|
|
|
|
#添加题目
|
|
|
#question_type 1:单选 2:多选 3:填空题
|
|
|
def create_exercise_question
|
|
|
question_title = params[:exercise_questions_title].nil? || params[:poll_questions_title].empty? ? l(:label_enter_single_title) : params[:poll_questions_title]
|
|
|
option = {
|
|
|
:is_necessary => (params[:is_necessary]=="true" ? 1 : 0),
|
|
|
:question_title => question_title,
|
|
|
:question_type => params[:question_type] || 1,
|
|
|
:question_number => @poll.poll_questions.count + 1
|
|
|
}
|
|
|
@poll_questions = @poll.poll_questions.new option
|
|
|
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 = {
|
|
|
:answer_position => i,
|
|
|
:answer_text => answer
|
|
|
}
|
|
|
@poll_questions.poll_answers.new question_option
|
|
|
end
|
|
|
end
|
|
|
# 如果是插入的话,那么从插入的这个id以后的question_num都将要+1
|
|
|
if params[:quest_id]
|
|
|
@is_insert = true
|
|
|
@poll.poll_questions.where("question_number > #{params[:quest_num].to_i}").update_all(" question_number = question_number + 1")
|
|
|
@poll_question_num = params[:quest_num].to_i
|
|
|
@poll_questions.question_number = params[:quest_num].to_i + 1
|
|
|
end
|
|
|
if @poll_questions.save
|
|
|
respond_to do |format|
|
|
|
format.js
|
|
|
end
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
#发布问卷
|
|
|
def publish_excercise
|
|
|
@exercise.exercise_status = 2
|
|
|
@exercise.publish_time = Time.now
|
|
|
if @exercise.save
|
|
|
if params[:is_remote]
|
|
|
redirect_to poll_index_url(:course_id => @course.id)
|
|
|
else
|
|
|
respond_to do |format|
|
|
|
format.js
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
|
|
|
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')
|
|
|
@show_all = true;
|
|
|
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
|
|
|
|
|
|
private
|
|
|
def find_course
|
|
|
@course = Course.find params[:course_id]
|
|
|
rescue Exception => e
|
|
|
render_404
|
|
|
end
|
|
|
end |