PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_survey/questions/survey_q_shortanswer.erl

https://code.google.com/p/zotonic/
Erlang | 86 lines | 57 code | 14 blank | 15 comment | 0 complexity | a7dd31fa0b6dbed2ee68d2085a8ec199 MD5 | raw file
 1%% @author Marc Worrell <marc@worrell.nl>
 2%% @copyright 2011 Marc Worrell
 3
 4%% Copyright 2011 Marc Worrell
 5%%
 6%% Licensed under the Apache License, Version 2.0 (the "License");
 7%% you may not use this file except in compliance with the License.
 8%% You may obtain a copy of the License at
 9%% 
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%% 
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17
18-module(survey_q_shortanswer).
19
20-include("zotonic.hrl").
21
22-export([
23    new/0,
24    question_props/1,
25    render/1,
26    answer/2,
27    prep_answer_header/1,
28    prep_answer/2
29]).
30
31-include("../survey.hrl").
32
33new() ->
34    Q = #survey_question{
35        type = shortanswer, 
36        name = z_ids:identifier(5),
37        text = "", 
38        question = <<"Please enter your name.">>,
39      is_required = true
40    },
41    render(Q).
42
43
44question_props(Q) ->
45    [
46        {explanation, ""},
47        {has_question, true},
48        {has_text, false},
49        {has_name, true},
50        {question_label, ""},
51        {text_label, ""}
52    ] ++
53    ?QUESTION_AS_PROPLIST(Q).
54
55render(Q) ->
56    Name = z_html:escape(Q#survey_question.name),
57    Q#survey_question{
58        text = "",
59        question = iolist_to_binary(Q#survey_question.question),
60        html = iolist_to_binary([
61            "<p class=\"question\">", z_html:escape(Q#survey_question.question), "</p>",
62            "<p class=\"shortanswer\">",
63            "<input class=\"survey-q\" type=\"text\" name=\"",Name,"\" value=\"\" /> "
64            "<p>"
65            ])
66    }.
67
68answer(Q, Answers) ->
69    Name = Q#survey_question.name,
70    case proplists:get_value(Name, Answers) of
71        undefined -> {error, missing};
72        Value -> case z_string:trim(Value) of
73                    [] -> {error, missing};
74                    V -> {ok, [{Name, {text, V}}]}
75                 end
76    end.
77
78prep_answer_header(Q) ->
79    z_convert:to_binary(Q#survey_question.name).
80
81prep_answer(_Q, []) ->
82    <<>>;
83prep_answer(_Q, [{_Name, {_Value, Text}}]) ->
84    z_convert:to_binary(Text).
85
86