/*** survey_display.c
**** Displays a survey delivered within a Manhattan classroom
***/


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

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

#include "manhat-lib/shared_survey_xml_parser.h"
#include "manhat-lib/shared_util.h"
#include   "manhat-lib/shared_survey_take_verify.h"
#include "manhat-lib/shared_survey_page.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);  
}


static
void set_page_data(const char *course, const char *key, SESSION *user, 
                      const char *sender, const char *info_fname, 
                      const char *survey_dir,  int allow_submit, CONFIG_STRUCT *conf)
{
  char survey_path[MAX_PATH + 1];
  SURVEY_LIST  *list;

   

    snprintf(survey_path, MAX_PATH + 1, "%s%s/%s/%s/%s/%s",
                     conf->course_path,
                     PEOPLE_DIR,
                     sender,
                     SURVEY_DIR,
                     survey_dir,
                     SURVEY_XML_FNAME);
                     
                     
    list = survey_data_parser(survey_path);  /* shared_survey_xml_parser.c */
    set_survey_content(list); /* shared_survey_page.c */
    free_survey_data_list(list);
    cs_set_int_value("Query.mode", MODE_TAKE_CLASSROOM_DELIVERED);  /* survey.h */
    cs_set_int_value("allow_submit", allow_submit);


}


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 */
  
  int already_taken;            /* did user already take this survey? */
  SURVEY_RELEASE_RECORD release;
  char survey_dir[MAX_FILENAME + 1];    /* randomly generated name of dir where survey is stored */
  int allow_submit;                  /* should this user be allowed to submit the survey? */
  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);


    /* stop the bad guys */
     if ( (user.group != FACULTY) && (already_taken && !release.student_view_again))
            cs_critical_error(ERR_REQUEST_DENIED, "");   

     /* teachers can never submit, students can submit only if they haven't already taken
     ** the survey
     **/
     allow_submit = (user.group != FACULTY) && !already_taken;
   
     if(user.group == FACULTY)
	    cs_set_int_value("is_faculty", 1);

     set_page_data( course, key, &user, sender, info_fname, survey_dir, allow_submit, &conf);
     cs_cgi_display("survey_page", 1);
     cs_cgi_destroy();

    return 0;
}

       
    
	
