anchor.diff:              []
       end.
       
  -    group_users([]) ->
  -        [];
  -    group_users([R|Rs]) ->
  -        {User, Q, Created} = unpack_user_row(R),
  -        group_users(User, Created, Rs, [Q], []).
  -        
  -    group_users(User, Created, [], UserAcc, Acc) ->
  -        [{User, Created, UserAcc} | Acc];
  -    group_users(User, Created, [R|Rs], UserAcc, Acc) ->
  -        {U, Q, Crtd} = unpack_user_row(R),
  -        case User of
  -            U -> group_users(User, Created, Rs, [Q|UserAcc], Acc);
  -            _ -> group_users(U, Crtd, Rs, [Q], [{User,Created,UserAcc}|Acc])
  -        end.
  -
  -    question_prepare(B, Context) ->
  -        case mod_survey:module_name(proplists:get_value(type, B)) of
  -            undefined -> B;
  -            M -> M:prep_block(B, Context)
  -        end.
  -
  -    unpack_user_row({UserId, Persistent, Question, Name, Value, Text, Created}) ->
  -        {
  -            {user, UserId, Persistent},
  -            {Question, {Name, {Value, Text}}},
  -            Created
  -        }.
  -
  -
  -    user_answer_row({user, User, Persistent}, Created, Answers, Questions, Context) ->
  -        [
  -            User,
  -            Persistent,
  -            case Created of 
  -                undefined -> <<>>;
  -                _ -> erlydtl_dateformat:format(Created, "Y-m-d H:i", Context)
  -            end
  -            | answer_row(Answers, Questions, Context)
  -        ].
  -        
  -    answer_row(Answers, Questions, Context) ->
  -        %% Convert answers to proper format if old format is discovered
  -        Answers1 = [case A of
  -                        {X, {X, _}} -> A;
  -                        {_, {X, Y}} -> {X, {X, Y}}
  -                    end || A <- Answers],
  -        lists:flatten([
  -            answer_row_question(proplists:get_all_values(QId, Answers1), 
  -                                Question,
  -                                Context)
  -            || {QId, Question} <- Questions
  -        ]).
  -    
  -    answer_row_question(_Answer, undefined, _Context) ->
  -        [];
  -    answer_row_question(Answer, Q, Context) ->
  -        Type = proplists:get_value(type, Q),
  -        case mod_survey:module_name(Type) of
  -            undefined -> [];
  -            M -> M:prep_answer(Q, Answer, Context)
  -        end.
  -
  -
  -    answer_header(Type, Block, Context) ->
  -        case mod_survey:module_name(Type) of
  -            undefined -> [];
  -            M -> M:prep_answer_header(Block, Context)
  -        end.
  -
  +%% @doc private
  +group_users([]) ->
  +    [];
  +group_users([R|Rs]) ->
  +    {User, Q, Created} = unpack_user_row(R),
  +    group_users(User, Created, Rs, [Q], []).
  +
  +%% @doc private
  +group_users(User, Created, [], UserAcc, Acc) ->
  +    [{User, Created, UserAcc} | Acc];
  +group_users(User, Created, [R|Rs], UserAcc, Acc) ->
  +    {U, Q, Crtd} = unpack_user_row(R),
  +    case User of
  +        U -> group_users(User, Created, Rs, [Q|UserAcc], Acc);
  +        _ -> group_users(U, Crtd, Rs, [Q], [{User,Created,UserAcc}|Acc])
  +    end.
  +
  +%% @doc private
  +question_prepare(B, Context) ->
  +    case mod_survey:module_name(proplists:get_value(type, B)) of
  +        undefined -> B;
  +        M -> M:prep_block(B, Context)
  +    end.
  +
  +%% @doc private
  +unpack_user_row({UserId, Persistent, Question, Name, Value, Text, Created}) ->
  +    {
  +      {user, UserId, Persistent},
  +      {Question, {Name, {Value, Text}}},
  +      Created
  +    }.
  +
  +%% @doc private
  +user_answer_row({user, User, Persistent}, Created, Answers, Questions, Context) ->
  +    [
  +     User,
  +     Persistent,
  +     case Created of 
  +         undefined -> <<>>;
  +         _ -> erlydtl_dateformat:format(Created, "Y-m-d H:i", Context)
  +     end
  +     | answer_row(Answers, Questions, Context)
  +    ].
  +
  +%% @doc private
  +answer_row(Answers, Questions, Context) ->
  +    %% Convert answers to proper format if old format is discovered
  +    Answers1 = [case A of
  +                    {X, {X, _}} -> A;
  +                    {_, {X, Y}} -> {X, {X, Y}}
  +                end || A <- Answers],
  +    lists:flatten([
  +                   answer_row_question(proplists:get_all_values(QId, Answers1), 
  +                                       Question,
  +                                       Context)
  +                   || {QId, Question} <- Questions
  +                  ]).
  +
  +%% @doc private
  +answer_row_question(_Answer, undefined, _Context) ->
  +    [];
  +answer_row_question(Answer, Q, Context) ->
  +    Type = proplists:get_value(type, Q),
  +    case mod_survey:module_name(Type) of
  +        undefined -> [];
  +        M -> M:prep_answer(Q, Answer, Context)
  +    end.
  +
  +%% @doc private
  +answer_header(Type, Block, Context) ->
  +    case mod_survey:module_name(Type) of
  +        undefined -> [];
  +        M -> M:prep_answer_header(Block, Context)
  +    end.
  +
  +
  +%% @doc Retrieve a single survey result for a user or persistent id.
   single_result(SurveyId, UserId, PersistentId, Context) ->
       {Clause, Args} = case z_utils:is_empty(UserId) of
                            true -> {"persistent = $1", [PersistentId]};
  
positive.code:     end,
      insert_questions(SurveyId, UserId, PersistentId, Answers, Created, Context).
  
      insert_questions(_SurveyId, _UserId, _PersistentId, [], _Created, _Context) ->
          ok;
      insert_questions(SurveyId, UserId, PersistentId, [{QuestionId, Answers}|Rest], Created, Context) ->
          insert_answers(SurveyId, UserId, PersistentId, QuestionId, Answers, Created, Context),
          insert_questions(SurveyId, UserId, PersistentId, Rest, Created, Context).
          
      insert_answers(_SurveyId, _UserId, _PersistentId, _QuestionId, [], _Created, _Context) ->
          ok;
      insert_answers(SurveyId, UserId, PersistentId, QuestionId, [{Name, Answer}|As], Created, Context) ->
          Args = case Answer of
              {text, Text} -> [SurveyId, UserId, PersistentId, QuestionId, Name, undefined, Text, Created];
              Value -> [SurveyId, UserId, PersistentId, QuestionId, Name, z_convert:to_list(Value), undefined, Created]
          end,
          z_db:q("insert into survey_answer (survey_id, user_id, persistent, question, name, value, text, created) 
                  values ($1, $2, $3, $4, $5, $6, $7, $8)", 
                 Args,
                 Context),
          insert_answers(SurveyId, UserId, PersistentId, QuestionId, As, Created, Context).
  
  
  prepare_results(SurveyId, Context) ->
      case m_rsc:p(SurveyId, blocks, Context) of
          [] ->
  
negative.code: -behaviour(gen_model).
  
  %% interface functions
  -export(
     [
      m_find_value/3,
      m_to_list/2,
      m_value/2,
  
      is_allowed_results_download/2,
      insert_survey_submission/3,
      insert_survey_submission/5,
      survey_stats/2,
      survey_results/2,
      survey_results_sorted/3,
      single_result/4,
      delete_result/4
     ]).
  
  -include_lib("zotonic.hrl").
  -include("../survey.hrl").
  