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


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

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"


static void
read_parameters (char *course, char *key, char *sender, char *info_fname, long *offset, int *action)
{
  char tmp[MAX_PATH + 1];

  cs_get_required_parameter ("crs", course, MAX_FILENAME);	/* shared_cgi_util.c */
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_get_required_parameter ("sender", sender, MAX_USERNAME);
  cs_get_required_parameter ("inf", info_fname, MAX_FILENAME);  

  cs_get_required_parameter ("loc", tmp, 19);
  *offset = atol(tmp);

  cs_get_required_parameter ("action", tmp, 19);
  *action = atoi(tmp);



}


static 
void set_frameset_url(const char *course, const char *key, const char *sender, const char *info_fname, long offset)
{
  char url[MAX_PATH +1];

 /* top frame has 'Return to classroom button */
  snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;sender=%s&amp;inf=%s&amp;loc=%ld&amp;action=2",   
                "survey_display_frameset", course, key, sender, info_fname, offset);
   cs_set_value("top_url", url);

/* bottom frame contains the survey */                
  snprintf (url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;sender=%s&amp;inf=%s",
     				"survey_display", course, key, sender, info_fname);
   cs_set_value("bottom_url", url);
}



static
void set_return_frame(const char *course, const char *key, long offset)
{
 CONFIG_STRUCT conf;
 char url[MAX_PATH +1];
 read_configuration_file (course, &conf);	/* shared_util.c */ 
 
  cs_set_int_value("offset", offset);
  cs_set_int_value("access", conf.access);
   

  if(offset == - 1)   /* button links to news_gather */
    {
     if(conf.access != ACCESS_CAPTURE)   /* use a standard form */
	  cs_set_value("action", "news_gather");
     else
     {
        snprintf(url, MAX_PATH +1, "%s?crs=%s&id=%s&grp=%d","news_gather", course, key, SURVEYS);
        cs_set_value("url", url);
     }
    }
  else   /*button links to news_read */                        
   {
     if(conf.access != ACCESS_CAPTURE)   /* use a standard form */
	  cs_set_value("action","news_read");
     else
     {
        snprintf(url, MAX_PATH +1, "%s?crs=%s&id=%s&&loc=%ld&grp=%d",  "news_read", course, key, offset,SURVEYS);
        cs_set_value("url", url);
     }
   }
}         




int
main()
{
  char course[MAX_PATH + 1];	        /* from crs=? command line */
  char key[MAX_KEY + 1];	        /* from id=? command line */
  char sender[MAX_USERNAME + 1];	/* from sender=? command line - which teacher posted this exam? */
  char info_fname[MAX_FILENAME + 1];    /* from inf=? cmd line */
  int action;                           /* from action=? cmd line */
  long offset;                          /* from loc = ? cmd line */
  CONFIG_STRUCT conf;
  SESSION user;

  cs_cgi_init();  
  read_parameters (course, key, sender, info_fname, &offset, &action);
  read_configuration_file (course, &conf);      /* shared_util.c */
  validate_key (key, &user, &conf);     /* shared_util.c */
  
  cs_set_course_info(&conf);
  if(action == 1)
     set_frameset_url(course,key,sender,info_fname, offset);
  else
     set_return_frame(course,key, offset);
  cs_cgi_display("survey_display_frameset", 1);
  cs_cgi_destroy(); 
  return 0;
  
}



