/trunk/src/manhat-lib/shared_survey_table.c

# · C · 289 lines · 224 code · 56 blank · 9 comment · 56 complexity · ba31c9876197b43d9968e7376f060e73 MD5 · raw file

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "../global.h"
  5. #include "shared_util.h"
  6. #include "shared_survey_table.h"
  7. #include "shared_cs_util.h"
  8. static int get_number_choices(ELE_CHOICE *head);
  9. void
  10. set_survey_table_basic_data(RECORD_LIST *list, int total, int number_takers)
  11. {
  12. char temp_value[MAX_PATH +1];
  13. float rate;
  14. cs_set_int_value("survey.num_responses", total);
  15. if(number_takers == -1)
  16. {
  17. cs_set_int_value("survey.num_expected_takers", list->expect_takers);
  18. rate = (float)total / (float)list->expect_takers;
  19. }
  20. else if(number_takers >0)
  21. {
  22. cs_set_int_value("survey.num_expected_takers", number_takers);
  23. rate = (float)total / (float)number_takers;
  24. }
  25. snprintf(temp_value, MAX_PATH +1, "%.1f%%", rate * 100);
  26. cs_set_value("survey.rate", temp_value);
  27. }
  28. void
  29. set_survey_table_heading(RECORD *one_record, int number_takers)
  30. {
  31. RESULT *ptr;
  32. int i =0;
  33. ptr = one_record->head;
  34. if(number_takers != -2)
  35. cs_set_int_value("display_course_list", 1);
  36. while(ptr)
  37. {
  38. ptr = ptr->next;
  39. i++;
  40. }
  41. cs_set_int_value("number_question", i);
  42. }
  43. void
  44. set_survey_table_one_record(RECORD *one_record, int number_takers, int num)
  45. {
  46. #define MAX_TMP_NAME 50
  47. char name[MAX_TMP_NAME];
  48. int i =0;
  49. RESULT *ptr;
  50. ptr = one_record->head;
  51. if(number_takers != -2)
  52. {
  53. snprintf(name, MAX_TMP_NAME, "record.%d.course_list", num);
  54. cs_set_value( name, one_record->course_list);
  55. }
  56. while(ptr)
  57. {
  58. snprintf(name, MAX_TMP_NAME, "record.%d.value.%d", num, i);
  59. cs_set_value(name, ptr->value);
  60. ptr=ptr->next;
  61. i++;
  62. }
  63. #undef MAX_TMP_NAME
  64. }
  65. int get_value_count(RECORD *one_record)
  66. {
  67. int i =1;
  68. RESULT *ptr;
  69. for( ptr = one_record->head; ptr; ptr = ptr->next)
  70. i++;
  71. return i;
  72. }
  73. char *replacequots(const char *value)
  74. {
  75. char *buf;
  76. int i, j, len;
  77. buf = (char *)malloc(sizeof(char)*(strlen(value)*2 +1));
  78. len = strlen(value);
  79. for(i =0; i < len; i++)
  80. {
  81. if(value[i] == '\"')
  82. {
  83. buf[j] = '\"';
  84. buf[j+1] = '\"';
  85. j+=2;
  86. }
  87. else
  88. {
  89. buf[j] = value[i];
  90. j++;
  91. }
  92. }
  93. buf[j] = '\0';
  94. return buf;
  95. }
  96. int is_choice_select_question(char *name, SURVEY_LIST *list, int *choice)
  97. {
  98. SURVEY_DATA *ptr;
  99. int id = atoi(name);
  100. for(ptr = list->head; ptr; ptr = ptr->next)
  101. {
  102. if( ptr->id == id && strcmp(ptr->tag_name, "choice") == 0 && strcmp(ptr->steps_bgcolor, "yes") ==0)
  103. {
  104. *choice = get_number_choices(ptr->head);
  105. return 1;
  106. }
  107. }
  108. return 0;
  109. }
  110. static int get_this_value(char *value, int j)
  111. {
  112. char *ptr =0;
  113. char str[10];
  114. snprintf(str, 10, "%d", j);
  115. ptr = strstr(value, str);
  116. if(ptr)
  117. return j;
  118. else
  119. return -1;
  120. }
  121. void
  122. send_one_record(RECORD *one_record, int number_takers, SURVEY_LIST *list)
  123. {
  124. RESULT *ptr;
  125. int count, j;
  126. int number_choice =0;
  127. count = get_value_count(one_record);
  128. if(number_takers != -2)
  129. printf("\"%s\",", one_record->course_list);
  130. ptr = one_record->head;
  131. while(ptr)
  132. {
  133. count--;
  134. if(is_choice_select_question(ptr->name, list, &number_choice))
  135. {
  136. for(j = 1; j<= number_choice; j++)
  137. {
  138. if(count == 1 && j == number_choice)
  139. printf("\"%d\"\n", get_this_value(ptr->value, j));
  140. else
  141. printf("\"%d\",", get_this_value(ptr->value, j));
  142. }
  143. }
  144. else
  145. {
  146. if(count ==1)
  147. printf("\"%s\"\n", ptr->value);
  148. else
  149. printf("\"%s\",", ptr->value);
  150. }
  151. ptr=ptr->next;
  152. }
  153. }
  154. static int get_number_survey_questions(SURVEY_LIST *list)
  155. {
  156. SURVEY_DATA *ptr;
  157. int count =0;
  158. for(ptr = list->head; ptr; ptr = ptr->next)
  159. {
  160. if(strcmp(ptr->tag_name, "title") != 0 && strcmp(ptr->tag_name, "custom") != 0)
  161. count++;
  162. }
  163. return count;
  164. }
  165. static int get_number_choices(ELE_CHOICE *head)
  166. {
  167. ELE_CHOICE *ptr;
  168. int count = 0;
  169. for(ptr = head; ptr; ptr = ptr->next)
  170. count++;
  171. return count;
  172. }
  173. void send_csv_heading(SURVEY_LIST *survey_list, int number_takers)
  174. {
  175. SURVEY_DATA *ptr;
  176. int i = 1, j = 1, number_choice = 0;
  177. int number_questions = get_number_survey_questions(survey_list);
  178. if(number_takers != -2)
  179. printf("\" \",");
  180. for(ptr = survey_list->head; ptr; ptr = ptr->next)
  181. {
  182. if(strcmp(ptr->tag_name, "title") != 0 && strcmp(ptr->tag_name, "custom") != 0)
  183. {
  184. if(strcmp(ptr->tag_name, "choice") ==0 && strcmp(ptr->steps_bgcolor, "yes") == 0)
  185. {
  186. number_choice = get_number_choices(ptr->head);
  187. for(j = 1; j<= number_choice; j++)
  188. {
  189. if(j == number_choice && i == number_questions)
  190. printf("\"%d.%d\"\n", i, j);
  191. else
  192. printf("\"%d.%d\",", i, j);
  193. }
  194. }
  195. else
  196. {
  197. if(i == number_questions)
  198. printf("\"%d\"\n", i);
  199. else
  200. printf("\"%d\",", i);
  201. }
  202. i++;
  203. }
  204. }
  205. }
  206. void
  207. send_survey_results_csv(RECORD_LIST *list, int number_takers, const char *course, SURVEY_LIST *survey_list)
  208. {
  209. char modified_course[MAX_PATH + 1];
  210. char *cptr;
  211. /* course contains a slash / separating the directories
  212. ** first, we convert the slash to a dot .
  213. */
  214. course_slash_to_dot(course, modified_course); /* shared_util.c */
  215. /* MS IE does a weird thing with an attachment that has two dots.
  216. ** It appears to put a [1] before the first dot. For example, if
  217. ** told that the attachment is called "c102.ac10101.txt", IE will
  218. ** want to save the file as "c102[1].ac10101.txt". To circumvent this,
  219. ** we'll replace the first dot with an underscore
  220. */
  221. cptr = strchr(modified_course, '.');
  222. if(cptr)
  223. *cptr = '_';
  224. printf("Content-type:application/octet-stream\n");
  225. printf ("Content-disposition:attachment; filename=\"%s_survey.csv\"\n\n", modified_course);
  226. send_csv_heading(survey_list, number_takers);
  227. list->current = list->head;
  228. while(list->current)
  229. {
  230. send_one_record(list->current, number_takers, survey_list);
  231. list->current = list->current->next;
  232. }
  233. }