#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

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

#include "manhat-lib/shared_server_string.h"
#include "manhat-lib/shared_survey_bar_graph.h"
#include "manhat-lib/shared_survey_take_verify.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)
{
  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);
}



char *survey_id(const char *survey_dir)
{

   char *survey_id;

   survey_id = (char*)malloc(sizeof(char) *(MAX_FILENAME +1));

   strncpy(survey_id,survey_dir, MAX_FILENAME + 1);
   *(survey_id + MAX_FILENAME) = '\0';
   
   return (strstr(survey_id,"dir_") == survey_id)? survey_id + strlen("dir_") : survey_id;

}


   

static
void set_info(time_t now, CONFIG_STRUCT *conf, NEWS_INFO *infofile_info, 
                               SURVEY_RELEASE_RECORD *release, const char *survey_dir)
{
   char timestring[MAX_TIMESTRING +1];

   cs_set_course_info(conf);
   cs_set_current_time();


   cs_set_value("survey.id", survey_id(survey_dir)); 

   cs_set_value("survey.subject", infofile_info->subject); 
   cs_set_value("survey.sender_name", infofile_info->sender_realname); 




  /*** print time msg was posted **/
  strftime (timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,
	      localtime (&infofile_info->time_sent));
  cs_set_value("survey.send_time", timestring); 




 /** print result time */
  strftime (timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,
	      localtime (&(release->results_time)) );
  cs_set_value("survey.result_time", timestring); 
  
  /** can students view results? */
  if(release->student_view_results)
     cs_set_int_value("survey.student_view_result", 1); 
  else
     cs_set_int_value("survey.student_view_result", 0); 

   /** can students view survey again? */
  if(release->student_view_again)
     cs_set_int_value("survey.student_view_again", 1); 
  else
     cs_set_int_value("survey.student_view_again", 0); 

}                                 
   
   
   
void
show_graphs( SESSION *user, const char *course,
	    const char *key, const char *sender, 
	    const char *survey_dir, CONFIG_STRUCT *conf,
	    NEWS_INFO *infofile_info, SURVEY_RELEASE_RECORD *release,
	    time_t now)
{
    char survey_fname[MAX_PATH +1];
    char base_path[MAX_PATH +1];
    char result_fname[MAX_PATH +1];
    struct stat buf;
    int number_students;
    
    snprintf(base_path, MAX_PATH + 1, "%s%s/%s/%s/%s",
             conf->course_path,
             PEOPLE_DIR,
             sender,
             SURVEY_DIR,
             survey_dir);

    snprintf(survey_fname, MAX_PATH+1, "%s/%s", base_path, SURVEY_XML_FNAME);
    snprintf(result_fname, MAX_PATH+1, "%s/%s", base_path, SURVEY_RESULT_FNAME);
    if(stat(result_fname, &buf))
      cs_critical_error(ERR_SURVEY_RESULT_NOT_AVAILABLE , "");

    
    number_students = current_student_count(conf);    /* shared_person_list.c */
    set_info(now, conf, infofile_info,release, survey_dir); 
    if(!set_survey_graph_results(survey_fname,result_fname, number_students))    /* shared_survey_bar_graph.c */
       cs_critical_error(ERR_SURVEY_RESULT_NOT_AVAILABLE, "");
    cs_set_value("survey_type","survey_module");  

}




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;

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

    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) ) )
    {
       show_graphs( &user, course,key, sender, survey_dir, &conf, &infofile_info, &release, now);
       cs_cgi_display("survey_bar_graph", 1);
    }
    else
       cs_critical_error(ERR_REQUEST_DENIED, "");


    cs_cgi_destroy();
    return 0;
}

       
    
	
