class ExerciseController < ApplicationController layout "base_courses" before_filter :find_exercise_and_course, :only => [:create_exercise_question, :edit,:update] before_filter :find_course, :only => [:index,:new,:create,:student_exercise_list] def index remove_invalid_exercise(@course) @is_teacher = User.current.allowed_to?(:as_teacher,@course) if @is_teacher exercises = @course.exercises else exercises = @course.exercises.where(:exercise_status => 2) 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 exercise_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) exercise_questions = @exercise.exercise_questions @exercise_questions = paginateHelper exercise_questions,5 #分页 respond_to do |format| format.html {render :layout => 'base_courses'} end end end def new option = { :exercise_name => "", :course_id => @course.id, :exercise_status => 1, :user_id => User.current.id, :time => "", :end_time => "", :publish_time => "", :exercise_description => "" } @exercise = Exercise.create option if @exercise redirect_to edit_exercise_url @exercise.id end end def create 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 end def edit respond_to do |format| format.html{render :layout => 'base_courses'} end end def update @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] @exercise.publish_time = params[:exercise][:publish_time] if @exercise.save respond_to do |format| format.js end else render_404 end end def destroy end # 统计结果 def statistics_result @exercise = Exercise.find(params[:id]) exercise_questions = @exercise.exercise_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[:question_title].nil? || params[:question_title].empty? ? l(:label_enter_single_title) : params[:question_title] option = { :question_title => question_title, :question_type => params[:question_type] || 1, :question_number => @exercise.exercise_questions.count + 1 } @exercise_questions = @exercise.exercise_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 } @exercise_questions.exercise_answers.new question_option end end # 如果是插入的话,那么从插入的这个id以后的question_num都将要+1 if params[:quest_id] @is_insert = true @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 end if @exercise_questions.save respond_to do |format| format.js end end end # 修改题目 # 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}") exercise_questions.each do |question| question.question_number -= 1 question.save end if @exercise_question && @exercise_question.destroy 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 exercise_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 remove_invalid_exercise(course) exercises = course.exercises.where("exercise_name=?","") unless exercises.empty? exercises.each do |exercise| exercise.destroy end end end def find_exercise_and_course @exercise = Exercise.find params[:id] @course = Course.find @exercise.course_id rescue Exception => e render_404 end def find_course @course = Course.find params[:course_id] rescue Exception => e render_404 end end