added points and answer type to this module

This commit is contained in:
Harry Bomrah 2016-08-18 21:18:02 +08:00
parent 2c9c57287f
commit 4d57437144
14 changed files with 167 additions and 45 deletions

View File

@ -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]

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -88,7 +88,6 @@
</label>
</div>
</div>
<!-- redirect mode -->
<div class="control-group">
<label class="control-label muted" for="redirectMode"><%= t('survey.redirect_mode') %></label>

View File

@ -170,6 +170,10 @@
<i class="icon-trash"></i>
</a>
</div>
<div style="margin-left:10px; display:inline;">
<input type="number" name="${_points[1]}" id="${_points[0]}" class="span1" value="${_points[3]}" />
<label for="${_points[0]}" class="radio inline" style="padding-left: 5px;">Points</label>
</div>
</div>
{{/if}}

View File

@ -30,13 +30,14 @@
<%= f.select :result_type, {
t('survey.chart') => QuestionnaireSurvey::ResultChart,
t('survey.extern_link') => QuestionnaireSurvey::ResultExtern,
t('survey.upload_file') => QuestionnaireSurvey::ResultFile
t('survey.upload_file') => QuestionnaireSurvey::ResultFile,
t('survey.criteria') => QuestionnaireSurvey::ResultCriteria
}, {}, :class => "tinput-medium", :onchange=>"OnSelectionChange (this)" %>
</div>
</div>
<!-- Extern Link -->
<div class="control-group <%= (@survey.result_type == 1) ? '' : 'hide' %>" data-type="1">
<div class="control-group selectable <%= (@survey.result_type == 1) ? '' : 'hide' %>" data-type="1">
<label class="control-label muted" for="account"><%= t('survey.extern_link') %></label>
<div class="controls">
<%= f.text_field :extern_link, :style=>"width:80%;", :class=>'post-extern_link' %>
@ -44,7 +45,7 @@
</div>
<!-- Upload_file -->
<div class="control-group <%= (@survey.result_type == 2) ? '' : 'hide' %>" data-type="2">
<div class="control-group selectable <%= (@survey.result_type == 2) ? '' : 'hide' %>" data-type="2">
<label class="control-label muted" for="account"><%= t('survey.upload_file') %></label>
<div class="controls">
<%= f.file_field :upload_file %>
@ -58,11 +59,40 @@
<% end %>
</div>
</div>
<!-- points criteria -->
<div class="control-group selectable <%= (@survey.result_type == 3) ? '' : 'hide' %>" data-type="3">
<label class="control-label muted" for="account"><%= t('survey.criteria') %></label>
<div class="controls">
<button class="btn btn-small btn-primary" id="add-criteria">Add</button>
</div>
<div id="criterias" class="controls" style="width:850px;">
<% if @survey.result_criteria.blank? %>
<div class="criteria-span" style="margin-left:0px;display:block;">
<label style="padding-left:0px;" for="" class="radio inline"> From : <input name="questionnaire_survey[result_criteria][0][]" class="span1" max="<%= @survey.total_points %>" type="number"></label>
<label for="" class="radio inline"> To : <input name="questionnaire_survey[result_criteria][0][]" class="span1" max="<%= @survey.total_points %>" type="number"></label>
<label for="" class="radio inline">Save to add text</label>
</div>
<% else %>
<% @survey.result_criteria.each_with_index do |criteria, idx| %>
<div class="criteria-span" style="margin-left:0px;display:block;margin-bottom:50px;">
<label style="padding-left:0px;" for="" class="radio inline"> From : <input name="questionnaire_survey[result_criteria][<%= idx %>][]" class="span1" max="<%= @survey.total_points %>" type="number" value="<%= criteria[0] %>"></label>
<label for="" class="radio inline"> To : <input name="questionnaire_survey[result_criteria][<%= idx %>][]" class="span1" max="<%= @survey.total_points %>" type="number" value="<%= criteria[1] %>"></label>
<% if idx != 0 %>
<a href="" style="margin-left:5px;" class="btn btn-danger delete-critera"><i class="icon icon-trash"></i></a>
<% end %>
<label for="" class="radio inline">
<textarea class="ckeditor" name="questionnaire_survey[result_criteria][<%= idx %>][]"><%= criteria[2] %></textarea>
</label>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
<div class="form-actions">
<%= hidden_field_tag 'page', params[:page] if !params[:page].blank? %>
<%= hidden_field_tag 'et', "result" %>
<%= f.submit t('save'), :class=>'btn btn-primary' %>
<%= link_to t('cancel'), get_go_back, :class=>"btn" %>
</div>
@ -74,21 +104,29 @@
<script type="text/javascript">
function OnSelectionChange (select) {
var selectedOption = select.options[select.selectedIndex];
if (selectedOption.value == '1' )
{
$('.control-group[data-type=1]').removeClass('hide');
$('.control-group[data-type=2]').addClass('hide');
}
else if (selectedOption.value == '2' )
{
$('.control-group[data-type=2]').removeClass('hide');
$('.control-group[data-type=1]').addClass('hide');
}
else
{
$('.control-group[data-type=1]').addClass('hide');
$('.control-group[data-type=2]').addClass('hide');
}
$(".selectable").addClass("hide");
$(".selectable[data-type=" + selectedOption.value + "]").removeClass("hide");
}
var index = <%= @survey.result_criteria.count == 0 ? 1 : @survey.result_criteria.count + 1 %>,
html = '<div data-critera-number="criteria_{index}" class="criteria-span" style="width:800px;"><label style="padding-left:0px;" for="" class="radio inline"> From : <input name="questionnaire_survey[result_criteria][{index}][]" class="span1" max="<%= @survey.total_points %>" type="number"></label><label for="" class="radio inline"> To : <input name="questionnaire_survey[result_criteria][{index}][]" class="span1" max="<%= @survey.total_points %>" type="number"></label><a href="" style="margin-left:5px;" class="btn btn-primary delete-critera"><i class="icon icon-trash"></i></a><span class="radio inline">Save to add text</span></div>';
$("#add-criteria").on("click",function(){
var newhtml = html.replace(/{index}/g,index);
console.log(newhtml);
index++;
$("#criterias").append(newhtml);
return false;
})
$(document).on("click", ".delete-critera" ,function(){
$(this).parent().remove();
return false;
})
</script>

View File

@ -1,2 +1,15 @@
alert("<%= t('survey.answer_success')%>");
<% @survey.result_type.inspect %>
<% if @survey.result_type == 3 %>
var scoredPoints = <%= @answer_model.scored_points %>,
msg = "";
<% @survey.result_criteria.each do |criteria| %>
<% if (criteria[0].to_i..criteria[1].to_i).cover?(@answer_model.scored_points) %>
msg = "<%= criteria[2] %>";
<% end %>
<% end %>
alert("Your total score is " + scoredPoints + "; " + msg);
<% else %>
alert("<%= t('survey.answer_success')%>");
<% end %>
window.location.href = "<%= page_for_survey %>";

View File

@ -1,4 +1,18 @@
<script type="text/javascript">
alert("<%= t('survey.answer_success')%>");
window.location.href = "<%= page_for_survey %>";
</script>
<%
data = action_data
@survey = data['survey']
@answer_model = data['answer']
%>
<% if @survey.result_type == 3 %>
<h3>Your total score is <%= @answer_model.scored_points %> </h3>
<% @survey.result_criteria.each do |criteria| %>
<% if (criteria[0].to_i..criteria[1].to_i).cover?(@answer_model.scored_points) %>
<div> <%= criteria[2].html_safe %> </div>
<% end %>
<% end %>
<% else %>
<h3><%= t('survey.answer_success')%></h3>
<% end %>
<a href="<%= data['url'] %>">Go Back</a>

View File

@ -145,13 +145,6 @@
<%= javascript_include_tag "lib/jquery.form"%>
<script type="text/javascript">
$('form.survey').ajaxForm({
beforeSubmit: function(a,f,o) {
o.dataType = 'script';
},
success: function(data) {
}
});
<% if @survey.jump_mode %>
var answers = <%== @answers.to_json %>;

View File

@ -8,7 +8,7 @@ en:
time_range: Date Range
write: Write
result: Result
survey_type: Survey Type
description: Description
postdate: Start Date
deadline: End Date
@ -26,7 +26,7 @@ en:
result_type: Result Type (Frontend)
extern_link: External Link
upload_file: Uploaded File
criteria: Criteria
remove_upload_file: Remove Uploaded File
answer_success: Successfully answeredjump back now

View File

@ -10,7 +10,7 @@ zh_tw:
time_range: 問卷期間
write: 填問卷
result: 結果
survey_type: Survey Type
description: 問卷說明
postdate: 開始時間
deadline: 結束時間
@ -25,7 +25,7 @@ zh_tw:
taken_by: Taken By
taken_date: Taken On
view_answers: View Answers
criteria: Criteria
result_type: 前端結果表示方式
extern_link: 外部連結
upload_file: 結果文件