diff --git a/app/assets/javascripts/survey.js b/app/assets/javascripts/survey.js index 8ddc7b2..e266e46 100644 --- a/app/assets/javascripts/survey.js +++ b/app/assets/javascripts/survey.js @@ -17,6 +17,8 @@ function setData(l, length, optionsI, className) { name = "questionnaire_survey[survey_questions_attributes][", optionID = l+"_survey_question_options_attributes_"+optionsI+"_name_translations_", optionName = l+"][survey_question_options_attributes]["+optionsI+"][name_translations]", + pointsID = l+"_survey_question_options_attributes_"+optionsI+"_points", + pointsName = l+"][survey_question_options_attributes]["+optionsI+"][points]", radiogroupsID = l+"_survey_question_radiogroups_attributes_"+optionsI+"_name_translations_", radiogroupsName = l+"][survey_question_radiogroups_attributes]["+optionsI+"][name_translations]"; data = { @@ -31,9 +33,11 @@ function setData(l, length, optionsI, className) { _type: [id+l+"_type", name+l+"][type]"], _is_required: [id+l+"_is_required", name+l+"][is_required]",false], _option: [id+optionID, name+optionName, "attributes_"+l+"_options_"+optionsI+"_",{"en":"","zh_tw":""}], + _points: [id+pointsID, name+pointsName, "attributes_"+l+"_options_"+optionsI+"_"], _radiogroups: [id+radiogroupsID, name+radiogroupsName, "attributes_"+l+"_radiogroups_"+optionsI+"_",{"en":"","zh_tw":""}], _custom_option: [id+l+"_custom_option", name+l+"][custom_option]",false] }; + return data; }; @@ -45,6 +49,8 @@ function setEditData(l, length, d, optionsI, className) { name = "questionnaire_survey[survey_questions_attributes][", optionID = l+"_survey_question_options_attributes_"+optionsI+"_name_translations_", optionName = l+"][survey_question_options_attributes]["+optionsI+"][name_translations]", + pointsID = l+"_survey_question_options_attributes_"+optionsI+"_points", + pointsName = l+"][survey_question_options_attributes]["+optionsI+"][points]", radiogroupsID = l+"_survey_question_radiogroups_attributes_"+optionsI+"_name_translations_", radiogroupsName = l+"][survey_question_radiogroups_attributes]["+optionsI+"][name_translations]"; data = { @@ -60,6 +66,7 @@ function setEditData(l, length, d, optionsI, className) { _type: [id+l+"_type", name+l+"][type]",d.type], _is_required: [id+l+"_is_required", name+l+"][is_required]",d.is_required], _option: [id+optionID, name+optionName, "attributes_"+l+"_options_"+optionsI+"_"], + _points: [id+pointsID, name+pointsName, "attributes_"+l+"_options_"+optionsI+"_", d.points], _radiogroups: [id+radiogroupsID, name+radiogroupsName, "attributes_"+l+"_radiogroups_"+optionsI+"_"], _custom_option: [id+l+"_custom_option", name+l+"][custom_option]",d.custom_option] }; @@ -67,6 +74,7 @@ function setEditData(l, length, d, optionsI, className) { }; function setEditQuestionAttributes(l, length, d, optionsI, className) { + console.log(d); if(!optionsI) { optionsI = 0; } @@ -76,6 +84,8 @@ function setEditQuestionAttributes(l, length, d, optionsI, className) { optionName = l+"][survey_question_options_attributes]["+optionsI+"][name_translations]", radiogroupsID = l+"_survey_question_radiogroups_attributes_"+optionsI+"_name_translations_", radiogroupsName = l+"][survey_question_radiogroups_attributes]["+optionsI+"][name_translations]", + pointsID = l+"_survey_question_options_attributes_"+optionsI+"_points", + pointsName = l+"][survey_question_options_attributes]["+optionsI+"][points]", option_id = l+"][survey_question_options_attributes]["+optionsI+"][id]", destroy = l+"][survey_question_options_attributes]["+optionsI+"][_destroy]", radiogroups_id = l+"][survey_question_radiogroups_attributes]["+optionsI+"][id]"; @@ -86,6 +96,7 @@ function setEditQuestionAttributes(l, length, d, optionsI, className) { _className: className, _destroy: [name+destroy], _option: [id+optionID, name+optionName, "attributes_"+l+"_options_"+optionsI+"_", d.name_translations], + _points: [id+pointsID, name+pointsName, "attributes_"+l+"_options_"+optionsI+"_", d.points], _radiogroups: [id+radiogroupsID, name+radiogroupsName, "attributes_"+l+"_radiogroups_"+optionsI+"_",d.name_translations], _options_id: [name+option_id,d.qid], _radiogroups_id: [name+radiogroups_id,d.qid] diff --git a/app/controllers/admin/surveys_controller.rb b/app/controllers/admin/surveys_controller.rb index 22a0999..5d6ca58 100644 --- a/app/controllers/admin/surveys_controller.rb +++ b/app/controllers/admin/surveys_controller.rb @@ -47,7 +47,14 @@ class Admin::SurveysController < OrbitAdminController def create @survey = QuestionnaireSurvey.new(survey_params) - + total = 0 + @survey.survey_questions.each do |ques| + ques.survey_question_options.each do |opt| + p = (opt.points == nil ? 0 : opt.points) rescue 0 + total = total + p + end + end + @survey.total_points = total @survey.create_user_id = current_user.id @survey.update_user_id = current_user.id @@ -92,10 +99,21 @@ class Admin::SurveysController < OrbitAdminController uid = params[:id].split('-').last @survey = QuestionnaireSurvey.find_by(:uid=>uid) @survey.update_user_id = current_user.id - + total = 0 + @survey.survey_questions.each do |ques| + ques.survey_question_options.each do |opt| + p = (opt.points == nil ? 0 : opt.points) rescue 0 + total = total + p + end + end + @survey.total_points = total respond_to do |format| if @survey.update_attributes(survey_params) - format.html { redirect_to(admin_surveys_url(:page => params[:page])) } + if params[:et] == "result" + format.html { redirect_to(set_answers_admin_survey_path(@survey.id)) } + else + format.html { redirect_to(admin_surveys_url(:page => params[:page])) } + end format.xml { head :ok } else format.html { render :action => "edit" } @@ -219,7 +237,12 @@ class Admin::SurveysController < OrbitAdminController end def survey_params - params.require(:questionnaire_survey).permit! + p = params.require(:questionnaire_survey).permit! + if !p[:result_criteria].blank? + x = p[:result_criteria].collect{|y| y.last} + p[:result_criteria] = x + end + p end end \ No newline at end of file diff --git a/app/controllers/surveys_controller.rb b/app/controllers/surveys_controller.rb index 3e87ef5..2ee2c43 100644 --- a/app/controllers/surveys_controller.rb +++ b/app/controllers/surveys_controller.rb @@ -101,6 +101,7 @@ class SurveysController < ApplicationController uid = params[:id].split('-').last @survey = QuestionnaireSurvey.find_by(:uid=>uid) + total = 0 answer = params[:answer] @answer_model = @survey.survey_answers.new @answer_model.user = current_user.id if !current_user.nil? @@ -115,7 +116,10 @@ class SurveysController < ApplicationController @answer_model[qid] = answer[qid + '_custom_option'] else if answer[qid] - @answer_model[qid] = question.survey_question_options.find(answer[qid]).name + opt = question.survey_question_options.find(answer[qid]) + @answer_model[qid] = opt.name + p = (opt.points == nil ? 0 : opt.points) rescue 0 + total = total + p end end when SurveyQuestion::Check @@ -126,19 +130,23 @@ class SurveysController < ApplicationController if question.custom_option && oid == 'custom_option' @answer_model[qid].push answer[qid + '_custom_option'] else - @answer_model[qid].push question.survey_question_options.find(oid).name + opt = question.survey_question_options.find(oid) + @answer_model[qid].push opt.name + p = (opt.points == nil ? 0 : opt.points) rescue 0 + total = total + p end end end end when SurveyQuestion::Radiogroup @answer_model[qid] = {} - options = Hash[question.survey_question_options.collect{|o| [ o.id.to_s, o.name ] }] + options = Hash[question.survey_question_options.collect{|o| [ o.id.to_s, (o.points.nil? ? 0 : o.points) ] }] radiogroups = Hash[question.survey_question_radiogroups.collect{|rg| [ rg.id.to_s, rg.name] }] if answer[qid] answer[qid].each do |oid, value| unless value.blank? @answer_model[qid][oid] = radiogroups[value] + total = total + options[oid] else if question.is_required @answer_model.errors.add question.title, t('survey_question.required_error') @@ -152,14 +160,28 @@ class SurveysController < ApplicationController end end if @answer_model.errors.empty? + @answer_model.scored_points = total @answer_model.save! - render :answer_success + redirect_to OrbitHelper.url_to_show(@survey.to_param) + "?method=answer_success&ans=#{@answer_model.id.to_s}" else @survey_answer_error = @answer_model.errors.full_messages.join(',') render :answer_error end end + def answer_success + + params = OrbitHelper.params + survey = QuestionnaireSurvey.find_by(uid: params[:uid]) + answer = SurveyAnswer.find(params[:ans]) + page = Page.where(:page_id => params[:page_id]).first + { + 'survey' => survey, + 'answer' => answer, + 'url' => "/#{I18n.locale.to_s}" + page.url + } + end + def result params = OrbitHelper.params diff --git a/app/models/questionnaire_survey.rb b/app/models/questionnaire_survey.rb index d906ba2..959450e 100644 --- a/app/models/questionnaire_survey.rb +++ b/app/models/questionnaire_survey.rb @@ -8,6 +8,7 @@ class QuestionnaireSurvey ResultChart = 0 ResultExtern = 1 ResultFile = 2 + ResultCriteria = 3 scope :can_display, ->{where(is_hidden: false)} @@ -22,10 +23,12 @@ class QuestionnaireSurvey field :is_hidden, :type => Boolean, :default => false field :needs_login, :type => Boolean, :default => false + field :total_points, type: Integer, :default => 0 field :result_type, :type => Integer, :default => 0 field :extern_link mount_uploader :upload_file, AssetUploader + field :result_criteria, type: Array, :default => [] field :jump_mode, :type => Boolean, :default => false diff --git a/app/models/survey_answer.rb b/app/models/survey_answer.rb index e032ce0..f44dd96 100644 --- a/app/models/survey_answer.rb +++ b/app/models/survey_answer.rb @@ -3,6 +3,7 @@ class SurveyAnswer include Mongoid::Timestamps field :user, type: BSON::ObjectId + field :scored_points, type: Integer, :default => 0 belongs_to :questionnaire_survey diff --git a/app/models/survey_question_option.rb b/app/models/survey_question_option.rb index c6cc2ba..4682fc2 100644 --- a/app/models/survey_question_option.rb +++ b/app/models/survey_question_option.rb @@ -3,6 +3,7 @@ class SurveyQuestionOption field :name, :localize => true field :jump_to, :default => 0 + field :points, :type => Integer, :default => 0 embedded_in :survey_question end \ No newline at end of file diff --git a/app/views/admin/surveys/_form.html.erb b/app/views/admin/surveys/_form.html.erb index b214ace..df25b56 100644 --- a/app/views/admin/surveys/_form.html.erb +++ b/app/views/admin/surveys/_form.html.erb @@ -88,7 +88,6 @@ -