#include <stdio.h>
#include <time.h>
#include <string.h>

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

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

static void
read_parameters (char *course, char *key, char *sender, char *info_fname, long *offset)
{

  char tmp[20];


  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);
}




  


static void
set_verify_page(CONFIG_STRUCT *conf, SESSION *user,  char *crs,  char *key, 
                                          char *sender,  char *info_fname, long offset)
{
 SURVEY_RELEASE_RECORD release;       /* contains release time, etc of survey */
 int already_taken;                   /* has this user read this message? */
 char survey_dir[MAX_FILENAME + 1];   /* randomly generated name of dir where survey is stored */
 time_t current_time;
 char timestring[MAX_TIMESTRING + 1];
 NEWS_INFO infofile_info;
 CENTRAL_INBOX_RECORD data;
 char tmp[20]; 
 char url[MAX_PATH +1];

 cs_set_course_info(conf);    /* shared_cs_util.c */
 cs_set_current_time(conf);   /* shared_cs_util.c */

 current_time = time(NULL);

 /* shared_survey_take_verify.c */
 read_survey_release_data(conf, sender, info_fname, user, 
                          &already_taken, survey_dir, 
                           &release, &infofile_info);
 
 cs_set_int_value("is_faculty", (user->group == FACULTY)? 1 : 0);
 cs_set_int_value("student_view_results", release.student_view_results? 1 : 0);
 cs_set_int_value("student_view_again", release.student_view_again? 1 : 0);
 cs_set_int_value("student_answer_required", release.student_answer_required? 1 : 0);
 cs_set_int_value("survey_already_taken", already_taken? 1 : 0 );
 

 strftime(timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT, localtime(&release.results_time));
 cs_set_value("release_time", timestring);
 
 cs_set_int_value("result_available", current_time >=release.results_time? 1 : 0);

 set_discussion_names (conf->course_path, user->username, SURVEYS);  /* shared_news_util.c */
 read_msg_record(offset, &data);   /*shared_news_util.c */
       
 /* BASIC_STATUS is #defined in global.h */
  cs_set_int_value("is_read_only", (BASIC_STATUS(data.status) == READ_ONLY)? 1 : 0);
  

 cs_set_int_value("capture_mode", (conf->access == ACCESS_CAPTURE)? 1 : 0);
 
  /* provide values for a form to submit */
 cs_set_value("crs", crs);
 cs_set_value("id", key);
 cs_set_value("sender", sender);
 cs_set_value("inf", info_fname);
 snprintf(tmp, 20, "%ld", offset);    
 cs_set_value("loc", tmp);
 cs_set_value("action", "1");

 /* also provide a link to use instead of a form */
 snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;sender=%s&amp;inf=%s&amp;loc=%ld&amp;action=1",
                            "survey_page", crs, key, sender,info_fname, offset);
 cs_set_value("take_survey_url", url);
}






int
main (void)
{
  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 */
  SESSION user;
  CONFIG_STRUCT conf;		        /* the configuration read from config file */
  long offset;
  
  /** NOTE: this program does not check to see if the message has been
  *** hidden by the teacher - thought you should know.
  **/
  cs_cgi_init();  
  read_parameters (course, key, sender, info_fname, &offset);

  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);	        /* shared_util.c */

  set_verify_page(&conf, &user, course,key,sender,info_fname, offset);
  
  cs_cgi_display("survey_take_verify", 1);
  cs_cgi_destroy(); 
  return 0;
}
