#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "global.h"
#include "survey.h"

#include "manhat-lib/shared_survey_info_list_parser.h"
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_key.h"
#include "manhat-lib/shared_survey_path_util.h"
#include "manhat-lib/shared_translate_url.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_server_string.h"
#include "manhat-lib/shared_survey_www_util.h"



static void
read_parameters( int *is_admin, char *key, char *crs, char *username)
{

    char tmp[10];

    cs_get_required_parameter("id", key, MAX_KEY);


    cs_get_optional_parameter("admin", tmp, 9); 

    *is_admin = !strlen(tmp)? 0: atoi(tmp)? 1: 0;

    cs_get_optional_parameter("crs", crs, MAX_PATH);
    cs_get_optional_parameter ("username", username, MAX_USERNAME);

}





static void
set_back_button_url(int is_admin, const char *key, SESSION *user, 
                              const char *course, const char *username, int standalone)
{
   char url[MAX_PATH + 1];
   char server_string[MAX_PATH + 1];

   if(is_admin)
   {
        if(!strlen(username))
	{
	   get_server_string(server_string, MAX_PATH +1); 
	   snprintf(url, MAX_PATH +1, "%s/%s/%s?id=%s", server_string,SBIN_ALIAS, "super_admin_page", key);
	}
	else
	   snprintf(url, MAX_PATH +1, "%s?id=%s&amp;admin=%d",  "survey_manage_teacher_survey", key, is_admin);
   }
   else
      snprintf(url, MAX_PATH +1, "%s?id=%s&amp;crs=%s&amp;grp=%d",  "news_topic", key, course, SURVEYS);
 
  cs_set_value("back_button_url", url);
}




static void
set_create_new_survey_url(int is_admin, const char *key, SESSION *user, const char *course, const char *username, int standalone)
{
   char url[MAX_PATH + 1];

   if(is_admin && !strlen(username) )
	snprintf(url, MAX_PATH +1, "%s?admin=%d&amp;id=%s", "survey_new_form", is_admin, key);
   else 
     if(!is_admin && !strlen(username) && !standalone) 
  	snprintf(url, MAX_PATH +1, "%s?admin=%d&amp;id=%s&amp;crs=%s", "survey_new_form", is_admin, key, course);
	
   cs_set_value("new_survey_url", url);
   
   
   if(is_admin)
   {
     snprintf(url, MAX_PATH +1, "%s?admin=%d&amp;id=%s", "survey_manage_teacher_survey", is_admin, key);
     cs_set_value("enter_teacher_surveys_url", url);
   }
}








static void
encode_url(char *string)
{
    char *ptr;
    for(ptr = string; *ptr; ptr++)
    {
         switch(*ptr)
	 {
	    case '?': *ptr=QMARK_SUB; break;
	    case '#': *ptr='['; break;
	    case '=': *ptr= EQUAL_SUB; break;
	    case '&': *ptr= AMP_SUB; break;
	    case ' ': *ptr= '+'; break;
            default: break;
	 }
    }

}






static void
set_private_data(SURVEY_INFO *info, const char *key, int is_admin, 
		const char *course, const char *username, int count)
{
    #define MAX_TMP_NAME 50

    typedef enum survey_status {UNKNOWN, UNDER_DEVELOPMENT, FINISHED, SHARED} SURVEY_STATUS;

    SURVEY_STATUS status = UNKNOWN;
    char url[MAX_PATH * 2+ 1];
    char name[MAX_TMP_NAME];


    if(!strcmp(info->state, SURVEY_STATE_DEV))
	 status = UNDER_DEVELOPMENT;
    else if(!strcmp(info->state, SURVEY_STATE_FIN))
	 status = FINISHED;
    else if( !strcmp(info->state, SURVEY_STATE_SHARED))
         status = SHARED;

    
    snprintf(name, MAX_TMP_NAME, "survey_private.%d.id", count);
    cs_set_value(name, info->id);
    
    snprintf(name, MAX_TMP_NAME, "survey_private.%d.title", count);
    cs_set_value(name, info->title);

    if(!is_admin )
    {
      if((status == FINISHED) || (status == SHARED))
      {
	 snprintf(name, MAX_TMP_NAME, "survey_private.%d.is_deliverable", count);
	 cs_set_value(name, "1");
	 
	 cs_set_int_value("has_selectable_items", 1);
      }
    } 
      
    
    /* preview urls */
    snprintf(name, MAX_TMP_NAME, "survey_private.%d.preview_url", count);

    if(is_admin && strlen(username))
	snprintf(url, MAX_PATH *2 +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;mode=%d&amp;domain=0&amp;username=%s",
             "survey_page", is_admin, key, info->id, MODE_PREVIEW_PRIVATE,  username);
    else
        snprintf(url, MAX_PATH*2 +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;mode=%d&amp;domain=0",
             "survey_page", is_admin, key, info->id, MODE_PREVIEW_PRIVATE);  
    cs_set_value(name, url);


    /* modify button, if this survey is under development */   
    if(status == UNDER_DEVELOPMENT) 
    {
       snprintf(name, MAX_TMP_NAME, "survey_private.%d.modify_url", count);

       if(is_admin && !strlen(username) )
	  snprintf(url, MAX_PATH *2+1,   "%s?admin=%d&amp;id=%s&amp;count=%s", "survey_new", is_admin, key, info->id);
       else if(!is_admin)
        {
          /** next used to pass username= parameter.  Removed it since it doesn't seem to be needed by 
          **  "survey_new"
          */
	  snprintf(url, MAX_PATH *2+1, "%s?admin=%d&amp;id=%s&amp;count=%s","survey_new", is_admin, key, info->id);
        }
        
        
      cs_set_value(name, url); 
    }


    if(status == UNDER_DEVELOPMENT)
    {
       snprintf(name, MAX_TMP_NAME, "survey_private.%d.finish_url", count);
       if(is_admin)
          snprintf(url, MAX_PATH *2+1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;mode=%d&amp;finish=5&amp;username=%s",
                  "survey_finish_save", is_admin, key, info->id, MODE_TAKE_WEB_DELIVERED, username);
       else
          snprintf(url, MAX_PATH *2 +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;mode=%d&amp;finish=5&amp;crs=%s",
                 "survey_finish_save", is_admin, key, info->id, MODE_TAKE_WEB_DELIVERED, course); 
      cs_set_value(name, url);
    }
    else if((status == FINISHED) && is_admin && !strlen(username))
    {
       snprintf(name, MAX_TMP_NAME, "survey_private.%d.deliver_url", count);
       snprintf(url, MAX_PATH *2 + 1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;mode=%d&amp;finish=3&amp;username=%s",
              "survey_finish_save", is_admin, key, info->id , MODE_TAKE_WEB_DELIVERED, username);
       cs_set_value(name, url);
    }
    
    
    
    if( (status == FINISHED) || (status == SHARED) )
    {
       snprintf(name, MAX_TMP_NAME, "survey_private.%d.copy_url", count);
       snprintf(url, MAX_PATH *2 +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;crs=%s&amp;username=%s",
              "survey_reuse" , is_admin, key, info->id, course, username);
       cs_set_value(name, url);
    }



    if(is_admin && strlen(username))
    {
       snprintf(name, MAX_TMP_NAME, "survey_private.%d.detail_url", count);
       snprintf(url, MAX_PATH *2 +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;username=%s","survey_info_detail", is_admin, key, info->id, username);  
       cs_set_value(name, url);
    }
    else
    {
       snprintf(name, MAX_TMP_NAME, "survey_private.%d.detail_url", count);
       snprintf(url, MAX_PATH *2 +1, "%s?admin=%d&amp;id=%s&amp;count=%s","survey_info_detail", is_admin, key, info->id); 
       cs_set_value(name, url);
    }




    if(is_admin && strlen(username))
    {
       snprintf(name, MAX_TMP_NAME, "survey_private.%d.delete_url", count);
       snprintf(url, MAX_PATH *2 +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;oper=1&amp;username=%s", 
                           "survey_info_detail", is_admin, key, info->id, username); 
       cs_set_value(name, url);
    }
    else
    {
       snprintf(name, MAX_TMP_NAME, "survey_private.%d.delete_url", count);
       snprintf(url, MAX_PATH *2 +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;oper=1",
           "survey_info_detail", is_admin, key, info->id);
       cs_set_value(name, url);
    }
    
    if((status == FINISHED) && is_admin)
    { 
      encode_url(info->title);
      if(!strlen(username))
      {
          snprintf(name, MAX_TMP_NAME, "survey_private.%d.public_url", count);
          snprintf(url, MAX_PATH *2 +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;public=1&amp;linkTitle=%s",
                        "survey_public_form", is_admin, key, info->id, info->title);
          cs_set_value(name, url);
          
          
          snprintf(name, MAX_TMP_NAME, "survey_private.%d.give_copy_to_url", count);
          snprintf(url, MAX_PATH *2+1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;linkTitle=%s",
                            "survey_public_form", is_admin, key, info->id, info->title);
          cs_set_value(name, url);


          snprintf(name, MAX_TMP_NAME, "survey_private.%d.deliver_via_www_url", count);
          snprintf(url, MAX_PATH *2 +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;public=3&amp;linkTitle=%s",
                        "survey_public_form", is_admin, key, info->id, info->title);
          cs_set_value(name, url);
          
          
          
      }
      else 
      {
        snprintf(name, MAX_TMP_NAME, "survey_private.%d.public_url", count);
        snprintf(url, MAX_PATH *2 +1, 
	      "%s?admin=%d&amp;id=%s&amp;count=%s&amp;username=%s&amp;to=yes&amp;linkTitle=%s",
              "survey_reuse", is_admin, key, info->id, username, info->title);
         cs_set_value(name, url);

     }
    }

   #undef MAX_TMP_NAME

}







static void
set_public_data(SURVEY_INFO *info, int is_admin, const char *key, const char *course, 
		const char *username, int standalone, int count)
{
    #define MAX_TMP_NAME 50

    char url[MAX_PATH +1];
    char name[MAX_TMP_NAME];
   
     
    snprintf(name, MAX_TMP_NAME, "survey_public.%d.id", count);
    cs_set_value(name, info->id);
    
    snprintf(name, MAX_TMP_NAME, "survey_public.%d.title", count);
    cs_set_value(name, info->title);

    if(!is_admin)
    {
       snprintf(name, MAX_TMP_NAME, "survey_public.%d.is_deliverable", count);
       cs_set_value(name, "1");
    }


    if(!standalone)
    {
       snprintf(name, MAX_TMP_NAME, "survey_public.%d.preview_url", count);
       if(is_admin && strlen(username))
          snprintf(url, MAX_PATH +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;mode=%d&amp;domain=3&amp;username=%s",
           "survey_page", is_admin, key, info->id, MODE_PREVIEW_PUBLIC,  username); 
       else
	  snprintf(url, MAX_PATH +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;mode=%d&amp;domain=1",
                 "survey_page", is_admin, key, info->id, MODE_PREVIEW_PUBLIC);   
       cs_set_value(name, url);
     }
    else
    {
       snprintf(name, MAX_TMP_NAME, "survey_public.%d.preview_url", count);
       snprintf(url, MAX_PATH +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;mode=%d&amp;crs=%s",
           "survey_page", is_admin, key, info->id, MODE_PREVIEW_PUBLIC, course);
       cs_set_value(name, url);
    }


   if(!standalone)
   { 
    encode_url(info->title);
    snprintf(name, MAX_TMP_NAME, "survey_public.%d.copy_url", count);
    snprintf(url, MAX_PATH +1, 
	   "%s?admin=%d&amp;id=%s&amp;count=%s&amp;domain=1&amp;crs=%s&amp;linkTitle=%s&amp;username=%s",
	   "survey_reuse" , is_admin, key, info->id, course, info->title, username);
    cs_set_value(name, url);
   }

   if(!standalone)
   {
     snprintf(name, MAX_TMP_NAME, "survey_public.%d.detail_url", count);
     if(is_admin && strlen(username) >0)
        snprintf(url, MAX_PATH +1,"%s?admin=%d&amp;id=%s&amp;count=%s&amp;domain=1&amp;win=no&amp;username=%s",
                                "survey_info_detail", is_admin, key,  info->id, username); 
     else
        snprintf(url, MAX_PATH +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;domain=1",
                                  "survey_info_detail", is_admin, key,  info->id); 
    cs_set_value(name, url);
   }
   else
   {
     snprintf(name, MAX_TMP_NAME, "survey_public.%d.detail_url", count);
     snprintf(url, MAX_PATH +1, 
	   "%s?admin=%d&amp;id=%s&amp;count=%s&amp;domain=1&amp;crs=%s","survey_info_detail", is_admin, key,  info->id, course);
     cs_set_value(name, url);
   }

  if(is_admin && !strlen(username))
    {
     snprintf(name, MAX_TMP_NAME, "survey_public.%d.delete_url", count);
      snprintf(url, MAX_PATH +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;oper=1&amp;domain=1",
             "survey_info_detail", is_admin, key, info->id); 
      cs_set_value(name, url);
    }
   #undef MAX_TMP_NAME
}




static void
set_delivered_data(SURVEY_INFO *info, int is_admin, const char *key, const char *course, int count)
{
    #define MAX_TMP_NAME 50

    char url[MAX_PATH +1];
    char name[MAX_TMP_NAME];
   
     
    snprintf(name, MAX_TMP_NAME, "survey_delivered.%d.id", count);
    cs_set_value(name, info->id);

    snprintf(name, MAX_TMP_NAME, "survey_delivered.%d.title", count);
    cs_set_value(name, info->title);

    if(!is_admin)
    {
       snprintf(name, MAX_TMP_NAME, "survey_delivered.%d.is_deliverable", count);
       cs_set_value(name, "1");
       
       snprintf(name, MAX_TMP_NAME, "survey_delivered.%d.domain", count);
       cs_set_value(name, info->survey_domain);
    }
          
    snprintf(name, MAX_TMP_NAME, "survey_delivered.%d.preview_url", count);
    snprintf(url, MAX_PATH +1,"%s?admin=%d&amp;id=%s&amp;count=%s&amp;mode=%d",
                                            "survey_page", is_admin, key, info->id, MODE_PREVIEW_PUBLIC);  
    cs_set_value(name, url);

    snprintf(name, MAX_TMP_NAME, "survey_delivered.%d.copy_url", count);
    snprintf(url, MAX_PATH +1, "%s?admin=%d&amp;id=%s&amp;count=%s","survey_reuse" , is_admin, key, info->id); 
    cs_set_value(name, url);

    snprintf(name, MAX_TMP_NAME, "survey_delivered.%d.graph_url", count);
    snprintf(url, MAX_PATH +1, "%s?admin=%d&amp;id=%s&amp;count=%s","survey_mymanhattan_bar_graph", is_admin, key, info->id);
    cs_set_value(name, url);

    snprintf(name, MAX_TMP_NAME, "survey_delivered.%d.table_url", count);
    snprintf(url, MAX_PATH +1, "%s?admin=%d&amp;id=%s&amp;count=%s","survey_mymanhattan_table", is_admin, key, info->id); 
    cs_set_value(name, url);
    
    snprintf(name, MAX_TMP_NAME, "survey_delivered.%d.csv_url", count);
    snprintf(url, MAX_PATH +1, "%s?id=%s&amp;admin=%d&amp;count=%s&amp;csv=yes&amp;ext=.csv",
                                      "survey_mymanhattan_table", key, is_admin,  info->id); 
    cs_set_value(name, url);
    
    snprintf(name, MAX_TMP_NAME, "survey_delivered.%d.detail_url", count);
    snprintf(url, MAX_PATH +1, "%s?admin=%d&amp;id=%s&amp;count=%s","survey_info_detail", is_admin, key, info->id); 
    cs_set_value(name, url);
    
    snprintf(name, MAX_TMP_NAME, "survey_delivered.%d.delete_url", count);
    snprintf(url, MAX_PATH +1,"%s?admin=%d&amp;id=%s&amp;count=%s&amp;oper=1","survey_info_detail", is_admin, key, info->id); 
    cs_set_value(name, url);
   #undef MAX_TMP_NAME
}
     



  
   
static void
set_www_data(SURVEY_INFO *info, int is_admin, const char *key, const char *course, int count)
{
    #define MAX_TMP_NAME 50

    char url[MAX_PATH +1];
    char name[MAX_TMP_NAME];
    char www_count[MAX_PATH +1];
    char temp[MAX_PATH +1];
    char *ptr ;
    int password;
   
     
    snprintf(name, MAX_TMP_NAME, "survey_www.%d.id", count);
    cs_set_value(name, info->id);

    snprintf(name, MAX_TMP_NAME, "survey_www.%d.title", count);
    cs_set_value(name, info->title);

    strncpy(temp, info->survey_domain, MAX_PATH + 1);
    ptr = strrchr(temp, '/');
    if(ptr)
    {
       *ptr = '\0';
       ptr++;
       strncpy(www_count, ptr, MAX_PATH + 1);
    }

    
    snprintf(name, MAX_TMP_NAME, "survey_www.%d.preview_url", count);
    snprintf(url, MAX_PATH +1,"%s?id=%s&amp;key=admin","survey_www_survey",  www_count);   
    cs_set_value(name, url);
    
    snprintf(name, MAX_TMP_NAME, "survey_www.%d.copy_url", count);
    snprintf(url, MAX_PATH + 1, "%s?admin=%d&amp;id=%s&amp;count=%s", "survey_reuse", is_admin, key, info->id); 
    cs_set_value(name, url);
    
    
    password = read_password_from_path(info->survey_domain);   /* shared_survey_www_util.c */
    
    snprintf(name, MAX_TMP_NAME, "survey_www.%d.graph_url", count);
    snprintf(url, MAX_PATH +1, "%s?id=%s&amp;admin=%d&amp;key=%s&amp;pass=%d",
                     "survey_www_bar_graph",  www_count, is_admin, key, password);
    cs_set_value(name, url);

    snprintf(name, MAX_TMP_NAME, "survey_www.%d.table_url", count);
    snprintf(url, MAX_PATH +1, "%s?id=%s&amp;admin=%d&amp;key=%s&amp;pass=%d",
                                        "survey_www_table", www_count, is_admin, key, password); 
    cs_set_value(name, url);

    snprintf(name, MAX_TMP_NAME, "survey_www.%d.csv_url", count);
    snprintf(url, MAX_PATH +1, "%s?id=%s&amp;admin=%d&amp;key=%s&amp;csv=yes&amp;pass=%d&amp;ext=.csv",
                            "survey_www_table", www_count, is_admin, key, password); 
    cs_set_value(name, url);


    
    snprintf(name, MAX_TMP_NAME, "survey_www.%d.detail_url", count);
    snprintf(url, MAX_PATH +1, "%s?admin=%d&amp;id=%s&amp;count=%s",
                                                   "survey_info_detail", is_admin, key, info->id);  
    cs_set_value(name, url);
    
    snprintf(name, MAX_TMP_NAME, "survey_www.%d.delete_url", count);
    snprintf(url, MAX_PATH +1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;oper=1&amp;www_count=%s",
                                                              "survey_info_detail", is_admin, key, info->id, www_count); 
    cs_set_value(name, url);

    snprintf(name, MAX_TMP_NAME, "survey_www.%d.access_url", count);
    encode_url(info->title);
    snprintf(url, MAX_PATH + 1, "%s?admin=%d&amp;id=%s&amp;count=%s&amp;title=%s",
                                        "survey_www_access_code", is_admin, key, www_count,info->title); 
    cs_set_value(name, url);
    
   #undef MAX_TMP_NAME
}





static void
set_private_surveys(int is_admin, const char *key, SURVEY_INFO_LIST *list, 
		                          const char *course, const char *username)
{
   int count = 0;
     
   list->current = list->head;
   while(list->current)
   {
       if( !strcmp(list->current->state, SURVEY_STATE_DEV) || 
           !strcmp(list->current->state, SURVEY_STATE_FIN) ||
           !strcmp(list->current->state, SURVEY_STATE_SHARED) 
                       )
       {
         set_private_data(list->current, key,  is_admin, course, username, count);
         count++;
       }
      list->current = list->current->next;
   }
   
   cs_set_int_value("private_survey_count", count);
}







static void
set_public_surveys(int crs, const char *key, SURVEY_INFO_LIST *list, 
		        const char *course, const char *username, int standalone)
{
    
       int count = 0;	

       list->current = list->head;
        
       while(list->current)
       {
	   if( !strcmp(list->current->state, SURVEY_STATE_PUB) ) 
	   {
             set_public_data(list->current, crs, key, course, username, standalone, count);
	     count++;
	   }
	  list->current = list->current->next;
       }
       
       
       cs_set_int_value("public_survey_count", count);

}







static void
set_delivered_surveys(int is_admin, const char *key, SURVEY_INFO_LIST *list, const char *course)
{
     int count =0;

     list->current = list->head;
     while(list->current)
       {
	   if(!strcmp(list->current->state, SURVEY_STATE_REL) ) 
	   {
             set_delivered_data(list->current, is_admin, key, course, count);
	     count++;
	   }
	  list->current = list->current->next;
       }
       
       
     cs_set_int_value("delivered_survey_count", count);
       
}






static void
set_www_accessible_surveys(int is_admin, const char *key, SURVEY_INFO_LIST *list, const char *course)
{
     int count = 0;
     
     list->current = list->head;
     while(list->current)
      {
	   if( !strcmp(list->current->state, SURVEY_STATE_WWW) )
	   { 
             set_www_data(list->current, is_admin, key, course, count);
	     count++;
	   }
	  list->current = list->current->next;
       }
       cs_set_int_value("www_survey_count", count);
}









/* the name of the window we'll open can't have punctuation
 ** marks or spaces.  We'll use the name of the course as the window
 ** name, with spaces and punctuation marks replaced by 'x'
 **/

static void set_window_name(const char *course_real,  int crs)
{
     char *ptr;
     char my_name[MAX_PATH + 1];

     if(!crs)
     {
        strncpy(my_name, course_real, MAX_PATH + 1);
        *(my_name + MAX_PATH) = '\0';
     
        for(ptr = my_name; *ptr; ptr++)
	    if(isspace(*ptr) || ispunct(*ptr))
	       *ptr = 'x';
        cs_set_value("window_name", my_name);
     }
     else
        cs_set_value("window_name", "goodwin");
}








static void
set_survey_data(int is_admin, const char *key,  SESSION *user, const char *course, 
		const char *username, int standalone)
{
    SURVEY_INFO_LIST *list =0;
    SURVEY_INFO_LIST *list_admin = 0;
    char file[MAX_PATH +1];
    char base[MAX_PATH +1];
    char admin_file[MAX_PATH +1];

    if(is_admin && strlen(username) )
       snprintf(base, MAX_PATH +1, "../%s/%s", SURVEY_DIR, username);
    else
       get_survey_base(user, base);  			/* shared_survey_path_util.c */

    snprintf(file, MAX_PATH + 1, "%s/%s",base, SURVEY_INFO_FNAME);

    if(!is_admin || (is_admin && strlen(username)))   
     {
      snprintf(admin_file, MAX_PATH +1, "../%s/_%s_/%s",
		   SURVEY_DIR, ADMIN_DIR, SURVEY_INFO_FNAME);
      list_admin = survey_info_parser(admin_file);   	/* shared_survey_info_list_parser.c */
     } 
    
    
    if(!standalone)
      list = survey_info_parser(file);   		/* shared_survey_info_list_parser.c */
     
     
    set_back_button_url(is_admin, key, user, course, username,  standalone);
    set_create_new_survey_url(is_admin, key, user, course, username,  standalone);
    set_window_name(course, is_admin);
    
    
    if(!standalone)
        set_private_surveys( is_admin, key, list, course, username);
      
    if(!is_admin || ( is_admin && strlen(username) ))
         set_public_surveys( is_admin, key, list_admin, course, username, standalone);
    else
         set_public_surveys ( is_admin, key, list, course, username, standalone);
      
      
    if(is_admin && !strlen(username))
      {
         set_delivered_surveys( is_admin, key, list, course);
         set_www_accessible_surveys(is_admin, key, list, course);
      }

    if(!is_admin && (is_admin && strlen(username)))
      free_survey_info_list(list_admin);  		/* shared_survey_info_list_parser.c */

    if(!standalone)
       free_survey_info_list(list);
}








int main()
{
   int is_admin;     		/*	if 1 then an admin is running the program, if 0 
                                 ** 	it's a teacher 
                                 */
                
   char key[MAX_KEY +1];
   char crs[MAX_PATH +1];
   char username[MAX_USERNAME +1];
   SESSION user;
   CONFIG_STRUCT conf;
   int standalone = 0;

   cs_cgi_init();   			/* shared_cs_util.c */
   read_parameters(&is_admin, key, crs, username);    


   if(is_admin)     
     {
      validate_super_key(key, &user);    /* shared_super_key.c */
      cs_set_value("is_admin", "1");
     } 
   else
      {
       read_configuration_file(crs, &conf);    /* shared_util.c */
       validate_key(key, &user, &conf);        /* shared_util.c */
       cs_set_value("is_admin", "0");
       standalone = conf.standalone;
      }
      
      
      
   if( (user.group != ADMIN) &&
       (user.group != FACULTY) )
         cs_critical_error(ERR_REQUEST_DENIED, "");



   if(is_admin && *username && (user.group != ADMIN))
         cs_critical_error(ERR_REQUEST_DENIED, "");
         
         
   if(*crs)
      cs_set_course_info(&conf);      

 
   cs_set_value("is_admin_viewing_teacher", *username? "1":"0");
 
      
   set_survey_data(is_admin, key, &user, crs, username, standalone);
   
   cs_cgi_display("survey_read_info", 1);  /* shared_cs_util.c */
   cs_cgi_destroy();                       /* shared_cs_util.c */

  return 0;
} 

