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