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

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


#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_survey_table.h"
#include "manhat-lib/shared_survey_take_verify.h"
#include "manhat-lib/shared_survey_xml_parser.h"
#include "manhat-lib/shared_person_list.h"
#include "manhat-lib/shared_cs_util.h"



static void
read_parameters (char *course, char *key, char *sender, char *info_fname, int *csv)
{
  char tmp[20];

  tmp[0] ='\0';

  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_optional_parameter ("csv", tmp, 19);
   if(strcmp(tmp, "yes") ==0)
     *csv = 1;
   else
     *csv = 0;

}





void
set_urls( const char *course, const char *key, 
      const char *sender,  const char *inf_fname)
{
   char url[MAX_PATH +1];
   
   snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;inf=%s&amp;sender=%s&amp;csv=yes&amp;ext=.csv",
	  "survey_module_table", course, key, inf_fname, sender);
   cs_set_value("csv_url", url);
   cs_set_value("survey_type", "survey_module");
}

     
   



void
do_action( const char *course, const char *key, 
      const char *sender, const char *survey_dir, 
      const char *inf_fname, CONFIG_STRUCT *conf, int csv)
{
    char fname[MAX_PATH +1];
    RECORD_LIST *list;
    struct stat buf;
    int total = 0;
    int number_students;
    SURVEY_LIST *survey_list;


    snprintf(fname, MAX_PATH + 1, "%s%s/%s/%s/%s/%s",
             conf->course_path,
             PEOPLE_DIR,
             sender,
             SURVEY_DIR,
             survey_dir,
	     SURVEY_RESULT_FNAME);

    if(stat(fname, &buf))
       cs_critical_error(ERR_SURVEY_RESULT_NOT_AVAILABLE, "");
       
    list = record_html_parser(fname);



    if(list->head)
    {
      if(csv)
      {
           snprintf(fname, MAX_PATH + 1, "%s%s/%s/%s/%s/%s",
             conf->course_path,
             PEOPLE_DIR,
             sender,
             SURVEY_DIR,
             survey_dir,
	     SURVEY_XML_FNAME);
          survey_list = survey_data_parser(fname);

	 send_survey_results_csv(list, 0, course, survey_list);   /* shared_survey_table.c */
         free_survey_data_list(survey_list);
      }
      else
      {
        set_urls( course, key, sender, inf_fname);
        list->current = list->head;
        set_survey_table_heading(list->current, 0);
        while(list->current)
        {
          set_survey_table_one_record(list->current, 0, total);  /* shared_survey_table.c */
          list->current = list->current->next;
	  total++;
	
        }
        number_students = current_student_count(conf);    /* shared_person_list.c */
        set_survey_table_basic_data(list, total, number_students);   /* shared_survey_table.c */
      }
    }
    else
      cs_critical_error(ERR_SURVEY_RESULT_NOT_AVAILABLE, "");
    free_record_html_list(list);

}







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 */
  SESSION user;
  CONFIG_STRUCT conf;           	/* the configuration read from config file */
  time_t now;
  int already_taken;            	/* did user already take this survey? */
  SURVEY_RELEASE_RECORD release;
  char survey_dir[MAX_FILENAME +1];
  NEWS_INFO infofile_info;
  int csv;

   cs_cgi_init();
   read_parameters (course, key, sender, info_fname, &csv);

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

   /* shared_survey_take_verify.c */
   read_survey_release_data(&conf, sender, info_fname,  &user,
              &already_taken, survey_dir, &release, &infofile_info);

   now = time(NULL);
   if(  (release.results_time <= now) &&
           (release.student_view_results || (user.group == FACULTY) ) )
     {
       do_action( course,key, sender, survey_dir, info_fname, &conf, csv);
       cs_set_course_info(&conf);
       cs_set_current_time();
       if(!csv)
           cs_cgi_display("survey_table_data", 1);
     }
   else
       cs_critical_error(ERR_REQUEST_DENIED, "");


     cs_cgi_destroy();
     return 0;
}

