#include "manhat-lib/shared_xml_course_create_util.h" #include "manhat-lib/shared_course_request_xml.h" #include "manhat-lib/shared_super_menu.h" #include "custom.h" void dump_xml_list(XML_COURSE_INFO_LIST *list); void dump_user_list(USER_GROUP_INFO_LIST *user_list); void read_parameters( char *roster_path, char *key) { char *address = 0; char temp[MAX_PATH +1]; cs_get_optional_parameter ("id", key, MAX_KEY); if(strlen(key) == 0) { address = hdf_get_value(global_cgi->hdf, "CGI.RemoteAddress", 0); if(!address) cs_critical_error(ERR_REQUEST_DENIED, " create new courses"); if(strcmp(address, MANHATTAN_SERVER_IP)) cs_critical_error(ERR_REQUEST_DENIED, " create new courses"); cs_get_required_parameter ("roster_path", roster_path, MAX_PATH); } else { cs_get_required_parameter ("roster_path", temp, MAX_PATH); snprintf(roster_path, MAX_PATH +1, "%s/%s", AUTO_UPDATE_ROSTER_PATH, temp); } } P_NODE * get_one_user_node(P_NODE *users) { P_NODE *new_node; new_node = (P_NODE*)malloc(sizeof(P_NODE)); if(!new_node) cs_critical_error(ERR_MALLOC_FAILED, "get_one_user_node"); strncpy(new_node->data.realname, users->data.realname, MAX_NAME +1); strncpy(new_node->data.lastname, users->data.lastname, MAX_NAME +1); strncpy(new_node->data.id, users->data.id, MAX_ID +1); if(strlen(users->data.username)) strncpy(new_node->data.username, users->data.username, MAX_USERNAME +1); else new_node->data.username[0] = '\0'; new_node->data.group = users->data.group; new_node->next = 0; return new_node; } XML_COURSE_INFO* get_course_info_node(XML_COURSE_INFO *one_course, const char *manhat_id, COURSE_TYPE type) { XML_COURSE_INFO *ptr; P_NODE *users, *user_ptr; ptr = (XML_COURSE_INFO*)malloc(sizeof(XML_COURSE_INFO)); if(!ptr) cs_critical_error(ERR_MALLOC_FAILED, "get_course_info_node"); strncpy(ptr->subdir, one_course->subdir, MAX_COURSE_SUBDIR +1); strncpy(ptr->dir_id, one_course->dir_id, MAX_COURSE_ID +1); strncpy(ptr->real_dir_id, manhat_id, MAX_COURSE_ID +1); strncpy(ptr->semester, one_course->semester, MAX_SEMESTER +1); strncpy(ptr->course_code, one_course->course_code, MAX_COURSE_NO +1); strncpy(ptr->title, one_course->title, MAX_COURSE_TITLE +1); if(strlen(one_course->instructor_title)) strncpy(ptr->instructor_title, one_course->instructor_title, MAX_INSTRUCTOR +1); else ptr->instructor_title[0] = '\0'; ptr->course_type = type; ptr->next = 0; ptr->user_head = 0; ptr->user_current = 0; for(users = one_course->user_head; users; users = users->next) { user_ptr = get_one_user_node(users); if(!ptr->user_head) ptr->user_head = user_ptr; else ptr->user_current->next = user_ptr; ptr->user_current = user_ptr; } return ptr; } void get_division(const char *roster_path, char *division) { char *ptr; ptr = strrchr(roster_path, '/'); if(!ptr) cs_critical_error(ERR_PARAM_FORMAT, "get_division"); *ptr = '\0'; ptr++; strncpy(division, ptr, MAX_PATH +1); } int check_request_file_exists(const char *roster_path, char *file) { char division[MAX_PATH +1]; get_division(roster_path, division); get_request_list_filename(division, file); /* shared_course_request_xml.h */ if(file_exists(file)) { return 1; } return 0; } XML_COURSE_INFO * find_course_in_xml_info_list(XML_COURSE_INFO_LIST *xml_info_list, REQUEST_COURSE *one) { for(xml_info_list->current = xml_info_list->head; xml_info_list->current; xml_info_list->current = xml_info_list->current->next) { if( !strcmp(xml_info_list->current->subdir,one->subdir) && !strcmp(xml_info_list->current->dir_id, one->sis_id)) return xml_info_list->current; } return 0; } XML_COURSE_INFO_LIST *build_new_course_list (XML_COURSE_INFO_LIST *xml_info_list, REQUEST_COURSE_LIST *request_list, SUBDIR_COURSE_LIST *sub_list) { XML_COURSE_INFO_LIST *new_list =0; XML_COURSE_INFO *ptr; XML_COURSE_INFO *found = 0; COURSE_TYPE type; /* dummy */ char manhat_course_id[MAX_COURSE_ID +1]; /* dummy */ new_list = (XML_COURSE_INFO_LIST *)malloc(sizeof(XML_COURSE_INFO_LIST)); new_list->head = 0; new_list->current = 0; for(request_list->current = request_list->head; request_list->current; request_list->current = request_list->current->next) { found = find_course_in_xml_info_list(xml_info_list, request_list->current); if(found) { if(!find_manhat_course_id(sub_list,found->subdir, found->dir_id, manhat_course_id, &type)) /* shared_course_info.c */ { ptr = get_course_info_node(found, found->dir_id, CENTRAL); if(!new_list->head) new_list->head = ptr; else new_list->current->next = ptr; new_list->current = ptr; } } } return new_list; } long get_size_request_file(const char *file) { struct stat buf; if(stat(file, &buf)) return -1; return buf.st_size; } void delete_request_file(const char *file, long pre_size) { long cur_size = 0; cur_size = get_size_request_file(file); if(pre_size != -1) { if(pre_size == cur_size) unlink(file); } } int main() { XML_COURSE_INFO_LIST *xml_info_list; /* shared_roster_xml_parser.h */ XML_COURSE_INFO_LIST *new_course_list =0; /* shared_roster_xml_parser.h */ CREATED_COURSE_NODE *course_head = 0; USER_GROUP_INFO_LIST *user_list = 0; INDEX_USER_LIST *index_list = 0; SUB_DIR_ID_NODE *unique_dir_list; SUBDIR_COURSE_LIST *sub_list; REQUEST_COURSE_LIST *request_list; long pre_size = 0; char roster_path[MAX_PATH + 1]; /* full (relative) path to xml file */ char file[MAX_PATH + 1]; char msg[MAX_PATH + 1]; char key[MAX_KEY +1]; SESSION user; cs_cgi_init(); key[0] = '\0'; /* build a linked list of directory names assigned to each course by the user */ read_parameters( roster_path, key); if(strlen(key)) { validate_super_key(key, &user); set_admin_page(&user, key, 0, "super_new_course_form"); } /* parse the xml file into a linked list */ xml_info_list = parse_xml_file(roster_path); /* parse xml file */ check_internal_course_names(xml_info_list); /* shared_xml_course_create_util.c */ unique_dir_list = build_unique_subdir_list(xml_info_list); /* shared_course_info.c */ sub_list = build_sub_list_info(unique_dir_list); /* shared_course_info.c */ free_sub_list_info(unique_dir_list); /* shared_course_info.c */ if(!check_request_file_exists(roster_path, file)) { snprintf(msg, MAX_PATH +1, "%s %s", "No such file:", file); update_roster_log(ROSTER_CREATE_COURSE, "", "", "", -1, -1, msg); cs_critical_error(ERR_GENERAL_ERROR, "No Request XML file."); } request_list = xml_request_course_parser(file); /* shared_course_request_xml.c */ if(!request_list->head) { snprintf(msg, MAX_PATH +1, "%s %s", "No such file:", file); update_roster_log(ROSTER_CREATE_COURSE, "", "", "", -1, -1, msg); cs_critical_error(ERR_GENERAL_ERROR, "No Request XML file"); } pre_size = get_size_request_file(file); new_course_list = build_new_course_list(xml_info_list, request_list, sub_list); free_request_list( request_list); /* shared_course_request_xml.c */ free_xml_data_list(xml_info_list); if(!new_course_list) { snprintf(msg, MAX_PATH +1, "%s %s", "No new courses were found in ", file); update_roster_log(ROSTER_CREATE_COURSE, roster_path, "", "", -1, -1, msg); /* shared_wnec_roster_log.h */ delete_request_file(file, pre_size); cs_critical_error(ERR_GENERAL_ERROR, "NO New courses were found from request list."); } if(!new_course_list->head) { free(new_course_list); snprintf(msg, MAX_PATH +1, "%s %s", "No new courses were found in ", file); update_roster_log(ROSTER_CREATE_COURSE, "", "", "", -1, -1, msg); /* shared_wnec_roster_log.h */ delete_request_file(file, pre_size); cs_critical_error(ERR_GENERAL_ERROR, "NO New courses were found from request list."); } /* only one admin can create a course at a time */ get_new_course_lock(); /* shared_new_course_lock.c */ /* start list with normal and template courses ** and only teacher info */ course_head = build_normal_template_course_list(new_course_list); /* shared_xml_course_create_util.c */ if(course_head) { update_roster_log(ROSTER_CREATE_COURSE, roster_path, "", "", -1, -1, "Start create courses"); /*shared_wnec_roster_log.h */ index_list = (INDEX_USER_LIST *)malloc(sizeof(INDEX_USER_LIST)); index_list->current =0; index_list->head =0; index_list = build_index_user_list(index_list); /* shared_user_index.c */ index_list = derive_teacher_username_password(course_head,index_list); /*shared_xml_course_create_util.c */ course_head =create_course_with_teachers(course_head); /* shared_xml_course_create_util.c */ user_list = build_user_list(new_course_list); /* list contains all students, with their courses */ if(user_list) { index_list=derive_student_username_password(user_list,index_list); /* shared_xml_course_create_util.c */ course_head = add_students_to_courses(course_head, user_list); /* shared_xml_course_create_util.c */ write_students_to_password_file(course_head); /* shared_xml_course_create_util.c */ reindex_users("../users"); /* shared_user_index.c */ write_add_drop_list(course_head); /* shared_xml_course_create_util.c */ set_course_data_record_log(course_head, "ctr", ROSTER_CREATE_COURSE); /* shared_xml_course_create_util.c */ } update_roster_log(ROSTER_CREATE_COURSE, roster_path, "", "", -1, -1, "End create courses"); /* shared_wnec_roster_log.h */ } delete_request_file(file, pre_size); free_xml_data_list(new_course_list); if(index_list) free_index_user_list(index_list); /* shared_user_index.c */ if(course_head) free_course_f_list(course_head); /* shared_xml_course_create_util.c */ if(user_list) free_user_group_info_list(user_list); /* shared_xml_course_create_util.c */ free_subdir_course_list(sub_list); free_new_course_lock(); /* shared_new_course_lock.c */ cs_cgi_display("super_xml_course_create", 1); cs_cgi_destroy(); return 0; }