/branches/3.2/src/admin_list_students_in_courses.c

# · C · 256 lines · 202 code · 54 blank · 0 comment · 21 complexity · 2e9c502a9c89142ae76949a52d1798e2 MD5 · raw file

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include "global.h"
  5. #include "manhat-lib/shared_util.h"
  6. #include "manhat-lib/shared_cs_util.h"
  7. #include "manhat-lib/shared_super_util.h"
  8. #include "manhat-lib/shared_access.h"
  9. #include "manhat-lib/shared_search_profile.h"
  10. #include "manhat-lib/shared_person_list.h"
  11. #include "manhat-lib/shared_teamstring.h"
  12. typedef struct course_person_list
  13. {
  14. char dir[MAX_PATH +1];
  15. CONFIG_STRUCT conf;
  16. P_NODE *p_head;
  17. struct course_person_list *next;
  18. }COURSE_PERSON_LIST;
  19. COURSE_PERSON_LIST *
  20. get_new_node( char *crs)
  21. {
  22. COURSE_PERSON_LIST *ptr;
  23. char *str_ptr;
  24. str_ptr = strchr(crs, ':');
  25. *str_ptr = '\0';
  26. ptr = (COURSE_PERSON_LIST*)malloc(sizeof(COURSE_PERSON_LIST));
  27. if(!ptr)
  28. cs_critical_error(ERR_MALLOC_FAILED, "");
  29. strncpy(ptr->dir, crs, MAX_PATH +1);
  30. *str_ptr = ':';
  31. ptr->p_head =0;
  32. ptr->next =0;
  33. return ptr;
  34. }
  35. COURSE_PERSON_LIST *
  36. get_course_list()
  37. {
  38. COURSE_PERSON_LIST *head =0, *current=0, *ptr;
  39. char *str;
  40. HDF *obj;
  41. str = hdf_get_value(global_cgi->hdf, "Query.course_list.0", 0);
  42. if(str)
  43. {
  44. obj = hdf_get_child(global_cgi->hdf, "Query.course_list");
  45. while(obj)
  46. {
  47. str = obj->value;
  48. if(str && strlen(str))
  49. {
  50. ptr = get_new_node(str);
  51. if(!head)
  52. head = ptr;
  53. else
  54. current->next = ptr;
  55. current = ptr;
  56. }
  57. obj = obj->next;
  58. }
  59. }
  60. else
  61. {
  62. str = hdf_get_value(global_cgi->hdf, "Query.course_list", 0);
  63. if(!str)
  64. cs_critical_error(ERR_PARAM_MISSING, "course_list");
  65. head = get_new_node(str);
  66. }
  67. return head;
  68. }
  69. COURSE_PERSON_LIST *
  70. read_parameters (char *key, char *crs, char *group)
  71. {
  72. COURSE_PERSON_LIST *head;
  73. cs_get_required_parameter ("id", key, MAX_KEY);
  74. cs_get_required_parameter ("crs", crs, MAX_PATH );
  75. cs_get_required_parameter ("group", group, MAX_ID);
  76. head = get_course_list();
  77. return head;
  78. }
  79. COURSE_PERSON_LIST *
  80. update_course_person_list(COURSE_PERSON_LIST *head)
  81. {
  82. COURSE_PERSON_LIST *ptr;
  83. for(ptr = head; ptr; ptr = ptr->next)
  84. {
  85. read_configuration_file (ptr->dir, &(ptr->conf)); /* shared_util.c */
  86. ptr->p_head = build_option_person_list(&(ptr->conf));
  87. }
  88. return head;
  89. }
  90. void
  91. free_course_person_list(COURSE_PERSON_LIST *head)
  92. {
  93. COURSE_PERSON_LIST *ptr;
  94. while(head)
  95. {
  96. ptr = head->next;
  97. free_option_person_list(head->p_head);
  98. free(head);
  99. head = ptr;
  100. }
  101. }
  102. void
  103. set_head( char *key, char *crs, char *group, CONFIG_STRUCT *conf)
  104. {
  105. cs_set_course_info(conf);
  106. cs_set_current_time();
  107. cs_set_value("add_type",!strcmp(group,"student")? "student":"faculty");
  108. cs_set_value("id", key);
  109. cs_set_value( "crs", crs);
  110. cs_set_value( "group", group);
  111. }
  112. void
  113. set_one_course(COURSE_PERSON_LIST *one_course, P_NODE *current_list, const char *key, const char *crs, const char *group, int i, int *count)
  114. {
  115. #define MAX_TMP_NAME 30
  116. P_NODE *ptr;
  117. int find =0, j =0;
  118. char name[MAX_TMP_NAME];
  119. char url[MAX_PATH +1];
  120. snprintf(name, MAX_TMP_NAME, "course.%d.course_no", i);
  121. cs_set_value(name, one_course->conf.course_no);
  122. snprintf(name, MAX_TMP_NAME, "course.%d.title", i);
  123. cs_set_value(name, one_course->conf.title);
  124. snprintf(name, MAX_TMP_NAME, "course.%d.dir", i);
  125. cs_set_value(name, one_course->dir);
  126. for(ptr = one_course->p_head; ptr; ptr = ptr->next)
  127. {
  128. find = find_user_in_course(current_list, ptr->data.username); /*shared_search_profile.h */
  129. snprintf(name, MAX_TMP_NAME, "course.%d.user.%d.in_class", i, j);
  130. cs_set_int_value(name, find? 1 : 0);
  131. if(find)
  132. {
  133. snprintf(name, MAX_TMP_NAME, "course.%d.user.%d.url", i, j);
  134. snprintf(url, MAX_PATH +1, "%s?id=%s&amp;crs=%s&amp;open=1&amp;username=%s", "admin_list_passwd", key, crs,ptr->data.username);
  135. cs_set_value(name, url);
  136. }
  137. snprintf(name, MAX_TMP_NAME, "course.%d.user.%d.username", i, j);
  138. cs_set_value(name, ptr->data.username);
  139. snprintf(name, MAX_TMP_NAME, "course.%d.user.%d.realname", i, j);
  140. cs_set_value(name, ptr->data.realname);
  141. snprintf(name, MAX_TMP_NAME, "course.%d.user.%d.id", i, j);
  142. cs_set_value(name, ptr->data.id);
  143. if(!find)
  144. {
  145. snprintf(name, MAX_TMP_NAME, "course.%d.user.%d.team", i, j);
  146. if(strcmp(group, "student") == 0)
  147. cs_set_value(name, "A");
  148. else
  149. cs_set_value(name, "Z");
  150. }
  151. j++;
  152. }
  153. }
  154. void set_all_person_in_courses(COURSE_PERSON_LIST *head, CONFIG_STRUCT *conf,
  155. const char *key, const char *crs, const char *group)
  156. {
  157. COURSE_PERSON_LIST *ptr;
  158. int i =0, count =0;
  159. P_NODE *crs_person = build_option_person_list(conf); /* shared_person_list.c */
  160. for(ptr = head; ptr; ptr = ptr->next)
  161. {
  162. set_one_course(ptr, crs_person, key, crs, group, i, &count);
  163. i++;
  164. }
  165. free_option_person_list(crs_person); /* shared_person_list.c */
  166. cs_set_int_value("count", count);
  167. }
  168. void
  169. do_action( char *key, char *crs, char *group,
  170. CONFIG_STRUCT *conf, COURSE_PERSON_LIST *head)
  171. {
  172. set_head(key, crs, group, conf);
  173. set_all_person_in_courses(head, conf, key, crs, group);
  174. cs_set_team(); /* shared_teamstring.c */
  175. }
  176. int
  177. main()
  178. {
  179. char key[MAX_KEY +1];
  180. char crs[MAX_PATH + 1]; /* from command line - which program to pass form selections to? */
  181. SESSION user;
  182. char group[MAX_ID +1];
  183. CONFIG_STRUCT conf;
  184. COURSE_PERSON_LIST *head;
  185. cs_cgi_init();
  186. head = read_parameters (key, crs, group);
  187. read_configuration_file (crs, &conf); /* shared_util.c */
  188. validate_key(key, &user, &conf); /* shared_util.c */
  189. if(!has_write_permission(&user, &conf)) /* shared_access.c */
  190. access_denied_error(); /* shared_access.c */
  191. if ( (user.group == FACULTY) || (user.group == ADMIN))
  192. {
  193. head = update_course_person_list(head);
  194. do_action(key, crs, group, &conf, head);
  195. }
  196. else
  197. access_denied_error(); /* shared_access.c */
  198. free_course_person_list(head);
  199. cs_cgi_display("admin_list_students_in_courses", 1);
  200. cs_cgi_destroy();
  201. return 0;
  202. }