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

# · C · 298 lines · 238 code · 59 blank · 1 comment · 58 complexity · 9f63112f1417068b00f6a47ea9c5ac58 MD5 · raw file

  1. #include "shared_grade_book_bar_graph_util.h"
  2. INTERVAL_COUNT *get_interval_list(int points)
  3. {
  4. INTERVAL_COUNT *head =0, *current=0, *ptr;
  5. double interval_value =0;
  6. int i, mod, max_interval;
  7. if(points < MAX_INTERVALS)
  8. {
  9. max_interval = points;
  10. mod =0;
  11. }
  12. else
  13. {
  14. mod = points % MAX_INTERVALS;
  15. max_interval = MAX_INTERVALS;
  16. }
  17. interval_value = points / max_interval;
  18. cs_set_int_value("max_intervals", max_interval);
  19. for(i =0; i<max_interval +1; i++)
  20. {
  21. ptr = (INTERVAL_COUNT *)malloc(sizeof(INTERVAL_COUNT));
  22. if(!ptr)
  23. cs_critical_error(ERR_MALLOC_FAILED, "");
  24. ptr->next =0;
  25. if(i ==0)
  26. ptr->start = - points;
  27. else if(i ==1)
  28. ptr->start = (i -1) * interval_value;
  29. else
  30. ptr->start = (i -1)* interval_value + (mod ? 0.1 : 1);
  31. if(i ==0)
  32. ptr->end = -0.1;
  33. else
  34. ptr->end = i * interval_value;
  35. ptr->count =0;
  36. if(!head)
  37. head = ptr;
  38. else
  39. current->next = ptr;
  40. current = ptr;
  41. }
  42. return head;
  43. }
  44. void
  45. free_interval_list(INTERVAL_COUNT *head)
  46. {
  47. INTERVAL_COUNT *ptr;
  48. if(head)
  49. {
  50. while(head)
  51. {
  52. ptr = head->next;
  53. free(head);
  54. head = ptr;
  55. }
  56. }
  57. }
  58. void update_interval_count(INTERVAL_COUNT *head)
  59. {
  60. INTERVAL_COUNT *int_ptr;
  61. GRADE *grades;
  62. float temp1 =0;
  63. if(!global_item_list || !global_item_list->head)
  64. cs_critical_error(ERR_GRADES_DB, "no grades");
  65. if(!global_item_list->head->grades)
  66. cs_critical_error(ERR_GRADES_DB, "no grades");
  67. for(int_ptr = head; int_ptr; int_ptr = int_ptr->next)
  68. {
  69. for(grades = global_item_list->head->grades->head; grades; grades = grades->next)
  70. {
  71. if(strcmp(grades->pending, "no") ==0 && strcmp(grades->excuse, "no") ==0 && strcmp(grades->username, "-1"))
  72. {
  73. if(grades->score >= int_ptr->start && grades->score <= int_ptr->end)
  74. (int_ptr->count)++;
  75. else if(grades->score >= int_ptr->start)
  76. {
  77. temp1 = grades->score - (int)int_ptr->end;
  78. if(temp1 >0 && temp1 < 1)
  79. (int_ptr->count)++;
  80. }
  81. }
  82. }
  83. }
  84. }
  85. void update_letter_grade_count()
  86. {
  87. LETTER_GRADE *ptr;
  88. GRADE *grades;
  89. if(!global_item_list || !global_item_list->head)
  90. cs_critical_error(ERR_GRADES_DB, "no grades");
  91. if(!global_item_list->head->grades)
  92. cs_critical_error(ERR_GRADES_DB, "no grades");
  93. for(ptr = global_letter_grades->head; ptr; ptr = ptr->next)
  94. {
  95. for(grades = global_item_list->head->grades->head; grades; grades = grades->next)
  96. {
  97. if(strcmp(grades->pending, "no") ==0 && strcmp(grades->excuse, "no") ==0 && strcmp(grades->username, "-1"))
  98. {
  99. if(strcmp(ptr->letter, grades->letter) ==0)
  100. (ptr->count)++;
  101. }
  102. }
  103. }
  104. }
  105. int get_number_student(GRADE *grades)
  106. {
  107. GRADE *ptr;
  108. int count =0;
  109. for(ptr = grades; ptr; ptr = ptr->next)
  110. {
  111. if(strcmp(ptr->pending, "no") ==0 && strcmp(ptr->excuse, "no") ==0 && strcmp(grades->username, "-1"))
  112. count++;
  113. }
  114. return count;
  115. }
  116. float
  117. compute_percent(int total, int count)
  118. {
  119. //if(total >= count)
  120. if(total != 0 && count != 0)
  121. return (float) count / (float) total * 100;
  122. return 0;
  123. }
  124. void set_bar_graph_value(INTERVAL_COUNT *head, int number_std)
  125. {
  126. #define MAX_TMP 50
  127. INTERVAL_COUNT *ptr;
  128. char name[MAX_TMP];
  129. char temp[MAX_TMP];
  130. int i =0;
  131. float percent;
  132. for(ptr = head; ptr; ptr = ptr->next)
  133. {
  134. percent = compute_percent(number_std, ptr->count);
  135. snprintf(name, MAX_TMP, "percent.%d", i);
  136. snprintf(temp, MAX_TMP, "%4.*f", test_precision(percent), percent);
  137. strtrm(temp);
  138. cs_set_value(name, temp);
  139. snprintf(name, MAX_TMP, "xscale.%d", i);
  140. snprintf(temp, MAX_TMP, "%4.*f-%4.*f", test_precision(ptr->start), ptr->start, test_precision(ptr->end),ptr->end);
  141. cs_set_value(name, temp);
  142. snprintf(name, MAX_TMP, "count.%d", i);
  143. snprintf(temp, MAX_TMP, "%d", ptr->count);
  144. cs_set_value(name, temp);
  145. i++;
  146. }
  147. cs_set_int_value("number_scales", i);
  148. #undef MAX_TMP
  149. }
  150. void set_letter_bar_graph_value(int number_std)
  151. {
  152. #define MAX_TMP 50
  153. LETTER_GRADE *ptr;
  154. char name[MAX_TMP];
  155. char temp[MAX_TMP];
  156. int i =0;
  157. float percent;
  158. for(ptr = global_letter_grades->head; ptr; ptr = ptr->next)
  159. {
  160. snprintf(name, MAX_TMP, "percent.%d", i);
  161. percent = compute_percent(number_std, ptr->count);
  162. snprintf(temp, MAX_TMP, "%4.*f", test_precision(percent), percent);
  163. strtrm(temp);
  164. cs_set_value(name, temp);
  165. snprintf(name, MAX_TMP, "xscale.%d", i);
  166. snprintf(temp, MAX_TMP, "%s", ptr->letter);
  167. cs_set_value(name, temp);
  168. snprintf(name, MAX_TMP, "count.%d", i);
  169. snprintf(temp, MAX_TMP, "%d", ptr->count);
  170. cs_set_value(name, temp);
  171. i++;
  172. }
  173. cs_set_int_value("number_scales", i);
  174. #undef MAX_TMP
  175. }
  176. void update_scale_grade_count(TOTAL_GRADE *head)
  177. {
  178. SCALE *ptr;
  179. TOTAL_GRADE *st_ptr;
  180. for(ptr = global_scale_list->head; ptr; ptr = ptr->next)
  181. {
  182. for(st_ptr = head; st_ptr; st_ptr = st_ptr->next)
  183. {
  184. if(strcmp(st_ptr->grade, ptr->grade) == 0)
  185. (ptr->count)++;
  186. }
  187. }
  188. }
  189. int get_student_number(TOTAL_GRADE *head)
  190. {
  191. int count =0;
  192. TOTAL_GRADE *st_ptr;
  193. for(st_ptr = head; st_ptr; st_ptr = st_ptr->next)
  194. count++;
  195. return count;
  196. }
  197. void set_scale_letter_grades( int number_std)
  198. {
  199. #define MAX_TMP 50
  200. SCALE *ptr;
  201. char name[MAX_TMP];
  202. char temp[MAX_TMP];
  203. int i =0;
  204. for(ptr = global_scale_list->head; ptr; ptr = ptr->next)
  205. {
  206. snprintf(name, MAX_TMP, "percent.%d", i);
  207. snprintf(temp, MAX_TMP, "%4.2f", compute_percent(number_std, ptr->count));
  208. cs_set_value(name, temp);
  209. snprintf(name, MAX_TMP, "xscale.%d", i);
  210. snprintf(temp, MAX_TMP, "%s", ptr->grade);
  211. cs_set_value(name, temp);
  212. snprintf(name, MAX_TMP, "count.%d", i);
  213. snprintf(temp, MAX_TMP, "%d", ptr->count);
  214. cs_set_value(name, temp);
  215. i++;
  216. }
  217. cs_set_int_value("number_scales", i);
  218. #undef MAX_TMP
  219. }
  220. void update_total_count(INTERVAL_COUNT *head, TOTAL_GRADE *std_grade)
  221. {
  222. INTERVAL_COUNT *ptr;
  223. TOTAL_GRADE *st_ptr;
  224. float temp1 =0;
  225. for(ptr = head; ptr; ptr = ptr->next)
  226. {
  227. for(st_ptr = std_grade; st_ptr; st_ptr=st_ptr->next)
  228. {
  229. if(st_ptr->total >= ptr->start && st_ptr->total <= ptr->end)
  230. (ptr->count)++;
  231. else if(st_ptr->total >= ptr->start)
  232. {
  233. temp1 = st_ptr->total - (int)ptr->end;
  234. if(temp1 >0 && temp1 < 1)
  235. (ptr->count)++;
  236. }
  237. }
  238. }
  239. }