anchor.diff:  survey_stats(SurveyId, Context) ->
       Rows = z_db:q("
                   select question, name, value, count(*) 
  -                from survey_answer 
  -                where survey_id = $1 and value is not null
  -                group by question, name, value 
  -                order by question, name, value", [SurveyId], Context),
  +                  from survey_answer 
  +                  where survey_id = $1 and value is not null
  +                  group by question, name, value 
  +                  order by question, name, value", [SurveyId], Context),
       group_questions(Rows, []).
  -    
  -    group_questions([], Acc) ->
  -        lists:reverse(Acc);
  -    group_questions([{Question,_,_,_}|_] = Answers, Acc) ->
  -        {Qs,Answers1} = lists:splitwith(fun({Q,_,_,_}) -> Q == Question end, Answers),
  -        NVs = case Qs of
  -            [{_, Name, Value, Count}|QsTail] -> group_values(Name, [{Value, Count}], QsTail, []);
  -            [] -> []
  -        end,
  -        group_questions(Answers1, [{Question,NVs}|Acc]).
  -        
  -    group_values(Name, Values, [], Acc) ->
  -        [{Name, Values}|Acc];
  -    group_values(Name, Values, [{_,Name,V,C}|Rs], Acc) ->
  -        group_values(Name, [{V,C}|Values], Rs, Acc);
  -    group_values(Name, Values, [{_,N,V,C}|Rs], Acc) ->
  -        group_values(N, [{V,C}], Rs, [{Name,Values}|Acc]).
  -
  -
  +
  +%% @private
  +group_questions([], Acc) ->
  +    lists:reverse(Acc);
  +group_questions([{Question,_,_,_}|_] = Answers, Acc) ->
  +    {Qs,Answers1} = lists:splitwith(fun({Q,_,_,_}) -> Q == Question end, Answers),
  +    NVs = case Qs of
  +              [{_, Name, Value, Count}|QsTail] -> group_values(Name, [{Value, Count}], QsTail, []);
  +              [] -> []
  +          end,
  +    group_questions(Answers1, [{Question,NVs}|Acc]).
  +
  +%% @private
  +group_values(Name, Values, [], Acc) ->
  +    [{Name, Values}|Acc];
  +group_values(Name, Values, [{_,Name,V,C}|Rs], Acc) ->
  +    group_values(Name, [{V,C}|Values], Rs, Acc);
  +group_values(Name, Values, [{_,N,V,C}|Rs], Acc) ->
  +    group_values(N, [{V,C}], Rs, [{Name,Values}|Acc]).
  +
  +
  +%% @doc Get survey results, sorted by the given sort column.
   survey_results_sorted(SurveyId, SortColumn, Context) ->
       [Headers|Data] = survey_results(SurveyId, Context),
       case string:str(Headers, [list_to_binary(SortColumn)]) of
  
positive.code:     z_db:q("DELETE FROM survey_answer WHERE " ++ Clause ++ " and survey_id = $2", Args ++ [SurveyId], Context).
  
  
  survey_captions(Id, Context) ->
      case m_rsc:p(Id, blocks, Context) of
          Blocks when is_list(Blocks) ->
  
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").
  