#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include "global.h"
#include "survey.h"
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_survey_util.h"
#include "manhat-lib/shared_super_key.h"
#include "manhat-lib/shared_survey_path_util.h"
#include "manhat-lib/shared_central_user.h"
#include "manhat-lib/shared_cs_util.h"

typedef struct teacher_list
{
     char username[MAX_USERNAME +1];
     char realname[MAX_NAME + 1];
     struct teacher_list *next;
}TEACHER_LIST;



void
read_parameters(int *is_admin, char *key )
{
    char tmp[10];

     cs_get_required_parameter("id", key, MAX_KEY);
     cs_get_required_parameter("admin", tmp, 9);
     *is_admin = !(*tmp)? 0 : atoi(tmp)? 1: 0;

}




TEACHER_LIST *
build_teacher_list()
{
    DIR *dir_p;
    char survey_dir[MAX_PATH +1];
    struct dirent *entry;
    TEACHER_LIST *head =0, *current=0, *ptr;
    
    snprintf(survey_dir, MAX_PATH +1, "../%s", SURVEY_DIR);
    dir_p = opendir(survey_dir);
    if(!dir_p)
	    cs_critical_error (ERR_OPENDIR_FAILED, "");
    while(NULL != (entry = readdir(dir_p)))
    {
       if( strcmp(entry->d_name, "_admin_") && 
	    strcmp(entry->d_name, "_data_") && 
	    strcmp(entry->d_name, "_www_") && 
	    strcmp(entry->d_name, ".") && 
	    strcmp(entry->d_name, ".." )  )
       {
	  ptr = (TEACHER_LIST *) malloc (sizeof(TEACHER_LIST));
	  if(!ptr)
	    cs_critical_error (ERR_MALLOC_FAILED, "");
	     
	  strncpy(ptr->username, entry->d_name, MAX_USERNAME +1);
          get_central_user_realname (ptr->username, ptr->realname); /* shared_central_user.c */
	  ptr->next =0;

	  if(!head)
	      head = ptr;
	  else
	      current->next = ptr;
	  current = ptr;
       }
    }
    return head;
}
void
free_teacher_list(TEACHER_LIST *head)
{
    TEACHER_LIST *ptr;
    while(head)
    {
       ptr = head->next;
       free(head);
       head = ptr;
    }
}



void
set_manage_surveys(const char *key, int is_admin)
{
   #define MAX_TMP_NAME 50
     TEACHER_LIST *head, *ptr;
     char name[MAX_TMP_NAME];
     char url[MAX_PATH +1];
     int i =0;

     head = build_teacher_list();
     if(!head)
      cs_critical_error(ERR_NO_SURVEYS, "");
     for(ptr = head; ptr; ptr = ptr->next)
     {
	snprintf(name, MAX_TMP_NAME, "user.%d.id", i);
	cs_set_value(name, ptr->username);
	snprintf(name, MAX_TMP_NAME, "user.%d.name", i);
	cs_set_value(name, ptr->realname);
	snprintf(name, MAX_TMP_NAME, "user.%d.url", i);
	snprintf(url, MAX_PATH +1, "survey_read_info?admin=%d&amp;id=%s&amp;username=%s", is_admin, key, ptr->username);
	cs_set_value(name, url);
	i++;
     }
     free_teacher_list(head);
    #undef MAX_TMP_NAME
}




void
set_page_data(int is_admin, const char *key)
{
   cs_set_current_time();
   set_manage_surveys(key,  is_admin);
}


int main()
{
   int is_admin;     /*if 1 then and admin is running the program, if 0 
                     ** it's a teacher 
                      */
   SESSION user;          
   char key[MAX_KEY +1];
   
   cs_cgi_init();
   read_parameters(&is_admin, key);
   if(is_admin)
      validate_super_key(key, &user);
   else
      cs_critical_error(ERR_REQUEST_DENIED, "");
         
   set_page_data(is_admin, key);
   cs_cgi_display("survey_manage_teacher_survey", 1);
   cs_cgi_destroy();
   return 0;
} 

