Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added 'is_multiple' option to thurstone. Fixed matching question.

  • Loading branch information...
commit 379c036f0741fa3221494593ae9c3c184a96a537 1 parent 8eacd28
@mworrell mworrell authored
View
29 modules/mod_survey/filters/filter_survey_answer_split.erl
@@ -0,0 +1,29 @@
+%% @author Marc Worrell <marc@worrell.nl>
+%% @copyright 2012 Marc Worrell
+
+%% Copyright 2012 Marc Worrell
+%%
+%% Licensed under the Apache License, Version 2.0 (the "License");
+%% you may not use this file except in compliance with the License.
+%% You may obtain a copy of the License at
+%%
+%% http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing, software
+%% distributed under the License is distributed on an "AS IS" BASIS,
+%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+%% See the License for the specific language governing permissions and
+%% limitations under the License.
+
+-module(filter_survey_answer_split).
+
+-export([
+ survey_answer_split/3
+]).
+
+survey_answer_split(V, _Block, _Context) when is_binary(V) ->
+ binstr:split(V, <<$#>>);
+survey_answer_split(V, _Block, _Context) ->
+ V.
+
+
View
13 modules/mod_survey/mod_survey.erl
@@ -125,7 +125,7 @@ render_next_page(Id, 0, _Direction, Answers, _History, Context) ->
render_next_page(Id, PageNr, Direction, Answers, History, Context) ->
As = [ {z_convert:to_binary(K), z_convert:to_binary(V)} || {K,V} <- z_context:get_q_all_noz(Context) ],
Answers1 = lists:foldl(fun({Arg,_Val}, Acc) -> proplists:delete(Arg, Acc) end, Answers, As),
- Answers2 = Answers1 ++ As,
+ Answers2 = Answers1 ++ group_multiselect(As),
case m_rsc:p(Id, blocks, Context) of
Questions when is_list(Questions) ->
case go_page(PageNr, Questions, Answers2, Direction, Context) of
@@ -164,6 +164,17 @@ render_next_page(Id, PageNr, Direction, Answers, History, Context) ->
#render{template="_survey_empty.tpl", vars=[{id,Id}, {q, As}]}
end.
+ group_multiselect([]) ->
+ [];
+ group_multiselect(As) ->
+ group_multiselect(lists:sort(As), undefined, [], []).
+
+ group_multiselect([], K, [V], Acc) -> [{K,V}|Acc];
+ group_multiselect([], K, Vs, Acc) -> [{K,Vs}|Acc];
+ group_multiselect([{K,V}|KVs], undefined, [], Acc) -> group_multiselect(KVs, K, [V], Acc);
+ group_multiselect([{K,V}|KVs], K, Vs, Acc) -> group_multiselect(KVs, K, [V|Vs], Acc);
+ group_multiselect([{K,V}|KVs], K1, [V1], Acc) -> group_multiselect(KVs, K, [V], [{K1,V1}|Acc]);
+ group_multiselect([{K,V}|KVs], K1, V1s, Acc) -> group_multiselect(KVs, K, [V], [{K1,V1s}|Acc]).
%% @doc Count the number of pages in the survey
count_pages([]) ->
View
23 modules/mod_survey/questions/survey_q_matching.erl
@@ -1,7 +1,7 @@
%% @author Marc Worrell <marc@worrell.nl>
-%% @copyright 2011 Marc Worrell
+%% @copyright 2011-2012 Marc Worrell
-%% Copyright 2011 Marc Worrell
+%% Copyright 2011-2012 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
@@ -41,10 +41,10 @@ to_block(Q) ->
answer(Block, Answers, Context) ->
Name = proplists:get_value(name, Block),
Props = filter_survey_prepare_matching:survey_prepare_matching(Block, Context),
- Count = length(proplists:get_value(items, Props)),
Options = [ Val || {Val,_Text} <- proplists:get_value(options, Props) ],
- Names = [ iolist_to_binary([Name, $_, integer_to_list(N)]) || N <- lists:seq(1,Count) ],
- ensure_option(Names, Options, Answers, []).
+ Items = proplists:get_value(items, Props),
+ ItemNames = [ iolist_to_binary([Name, $_, KI]) || {KI,_} <- Items ],
+ ensure_option(ItemNames, Options, Answers, []).
ensure_option([], _Options, _Answers, Acc) ->
@@ -69,10 +69,9 @@ prep_chart(Block, Answers, Context) ->
Name = proplists:get_value(name, Block),
Props = filter_survey_prepare_matching:survey_prepare_matching(Block, Context),
Items = proplists:get_value(items, Props),
- Parts = proplists:get_value(parts, Props),
- ItemNames = [ iolist_to_binary([Name, $_, integer_to_list(N)]) || N <- lists:seq(1,length(Items)) ],
- Labels = proplists:get_value(options, Parts),
- LabelsB = [ z_convert:to_binary(Lab) || Lab <- Labels ],
+ ItemNames = [ iolist_to_binary([Name, $_, KI]) || {KI,_} <- Items ],
+ Labels = proplists:get_value(options, Props),
+ LabelsB = [ Lab || {Lab, _} <- Labels ],
[
{question, z_html:escape(proplists:get_value(prompt, Block), Context)},
{charts, [ prep_chart1(ItemText, proplists:get_value(ItemName, Answers, []), LabelsB)
@@ -82,7 +81,7 @@ prep_chart(Block, Answers, Context) ->
prep_chart1(_ItemText, undefined, _LabelsB) ->
undefined;
- prep_chart1(ItemText, Vals, LabelsB) ->
+ prep_chart1({_,ItemText}, Vals, LabelsB) ->
Values = [ proplists:get_value(C, Vals, 0) || C <- LabelsB ],
Sum = case lists:sum(Values) of 0 -> 1; N -> N end,
Perc = [ round(V*100/Sum) || V <- Values ],
@@ -97,13 +96,13 @@ prep_chart(Block, Answers, Context) ->
prep_answer_header(Block, _Context) ->
Name = proplists:get_value(name, Block),
Items = proplists:get_value(items, Block),
- [ iolist_to_binary([Name, $., integer_to_list(N)]) || N <- lists:seq(1,length(Items)) ].
+ [ iolist_to_binary([Name, $:, KI]) || {KI,_} <- Items ].
prep_answer(Block, Answers, _Context) ->
Name = proplists:get_value(name, Block),
Items = proplists:get_value(items, Block),
- ItemNames = [ iolist_to_binary([Name, $., integer_to_list(N)]) || N <- lists:seq(1,length(Items)) ],
+ ItemNames = [ iolist_to_binary([Name, $_, KI]) || {KI,_} <- Items ],
[ prep_answer1(Item, Answers) || Item <- ItemNames ].
prep_answer1(Item, Answers) ->
View
68 modules/mod_survey/questions/survey_q_thurstone.erl
@@ -32,20 +32,29 @@
answer(Block, Answers, Context) ->
Name = proplists:get_value(name, Block),
+ Props = filter_survey_prepare_thurstone:survey_prepare_thurstone(Block, Context),
+ Options = proplists:get_value(answers, Props),
case proplists:get_value(Name, Answers) of
undefined ->
{error, missing};
- N ->
- Props = filter_survey_prepare_thurstone:survey_prepare_thurstone(Block, Context),
- Options = proplists:get_value(answers, Props),
- {Val, _} = lists:nth(list_to_integer(N), Options),
- {ok, [{Name, Val}]}
+ Label when is_binary(Label) ->
+ case proplists:is_defined(Label, Options) of
+ true -> {ok, [{Name, Label}]};
+ false -> {error, missing}
+ end;
+ Value when is_list(Value) ->
+ Defined = lists:filter(fun(Lab) -> proplists:is_defined(Lab, Options) end, Value),
+ Flattened = string:join([ z_convert:to_list(V) || V <- Defined ], "#"),
+ {ok, [{Name, {text, list_to_binary(Flattened)}}]}
end.
prep_chart(_Q, [], _Context) ->
undefined;
-prep_chart(Block, [{_, Vals}], Context) ->
+prep_chart(Block, [{Name, {text, Vals0}}], Context) ->
+ prep_chart(Block, [{Name, Vals0}], Context);
+prep_chart(Block, [{_, Vals0}], Context) ->
+ Vals = explode(Vals0),
Props = filter_survey_prepare_thurstone:survey_prepare_thurstone(Block, Context),
Labels = [ Lab || {Lab,_} <- proplists:get_value(answers, Props) ],
Values = [ proplists:get_value(C, Vals, 0) || C <- Labels ],
@@ -57,24 +66,55 @@ prep_chart(Block, [{_, Vals}], Context) ->
{type, "pie"},
{data, [{L,P} || {L,P} <- lists:zip(Labels, Perc), P /= 0]}
].
-
+
+ explode(Vs) ->
+ lists:flatten([
+ case binstr:strchr(V, $#) of
+ 0 -> V;
+ _ -> binstr:split(V, <<$#>>)
+ end
+ || V <- Vs
+ ]).
prep_answer_header(Q, _Context) ->
- proplists:get_value(name, Q).
+ Name = proplists:get_value(name, Q),
+ case z_convert:to_bool(proplists:get_value(is_multiple, Q)) of
+ true -> [ <<Name/binary, $:, K/binary>> || {K,_} <- proplists:get_value(answers, Q) ];
+ false -> Name
+ end.
-prep_answer(_Q, [], _Context) ->
- <<>>;
-prep_answer(_Q, [{_Name, {Value, _Text}}], _Context) ->
- Value.
+prep_answer(Q, [], _Context) ->
+ prep(Q, []);
+prep_answer(Q, [{_Name, {undefined, Text}}], _Context) ->
+ prep(Q, binstr:split(Text, <<$#>>));
+prep_answer(Q, [{_Name, {Value, _Text}}], _Context) ->
+ prep(Q, [Value]).
-prep_block(B, _Context) ->
- B.
+ prep(Q, Vs) ->
+ case z_convert:to_bool(proplists:get_value(is_multiple, Q)) of
+ false ->
+ hd(Vs);
+ true ->
+ [
+ case lists:member(K, Vs) of
+ true -> K;
+ false -> <<>>
+ end
+ || {K, _} <- proplists:get_value(answers, Q)
+ ]
+ end.
+
+
+prep_block(Block, Context) ->
+ Props = filter_survey_prepare_thurstone:survey_prepare_thurstone(Block, Context),
+ Props ++ Block.
to_block(Q) ->
[
{type, survey_thurstone},
{is_required, Q#survey_question.is_required},
+ {is_multiple, false},
{name, z_convert:to_binary(Q#survey_question.name)},
{prompt, z_convert:to_binary(Q#survey_question.question)},
{answers, z_convert:to_binary(Q#survey_question.text)}
View
1  modules/mod_survey/scomps/scomp_survey_poll.erl
@@ -38,3 +38,4 @@ render(Args, _Vars, Context) ->
end,
Render = mod_survey:render_next_page(SurveyId, 1, exact, Answers1, [], Context),
{ok, z_template:render(Render#render{vars=[{element_id, ElementId}|Render#render.vars]}, Context)}.
+
View
2  modules/mod_survey/templates/blocks/_admin_edit_block_li_survey_thurstone.tpl
@@ -29,7 +29,7 @@
<fieldset class="form-vertical">
<div class="control-group">
<label class="checkbox">
- <input type="checkbox" id="block-{{name}}-is_multiple" name="block-{{name}}-is_multiple" value="1" {% if blk.is_multiple %}check="checked"{% endif %} />
+ <input type="checkbox" id="block-{{name}}-is_multiple" name="block-{{name}}-is_multiple" value="1" {% if blk.is_multiple %}checked="checked"{% endif %} />
{_ Multiple answers possible (use checkboxes instead of radio buttons) _}
</label>
View
2  modules/mod_survey/templates/blocks/_block_view_survey_matching.tpl
@@ -4,7 +4,7 @@
<div class="controls">
{% for val,item in props.items %}
{% with forloop.counter as index %}
- {% with [name, "_", index|make_list]|join as nm %}
+ {% with [blk.name, "_", val]|join|to_binary as nm %}
{% with answers[nm] as ans %}
<label for="{{ #match.index }}">{{ item|escape }}</label>
<select id="{{ #match.index }}" name="{{ nm }}">
View
2  modules/mod_survey/templates/blocks/_block_view_survey_thurstone.tpl
@@ -1,5 +1,5 @@
{% with blk|survey_prepare_thurstone as props %}
-{% with answers[blk.name] as ans %}
+{% with answers[blk.name]|survey_answer_split:blk as ans %}
<div class="control-group survey-thurstone">
<label>{{ blk.prompt }}</label>
<div class="controls">
Please sign in to comment.
Something went wrong with that request. Please try again.