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

#include "global.h"

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



static void
read_parameters (char *course, char *key)
{
  char temp[30];

  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_set_value("crs", course);
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_set_value("id", key);
  cs_get_optional_parameter ("list", temp, 29);
  if(strlen(temp))
     cs_set_int_value("show_list", atoi(temp));
}


static void
show_form (const char *course, const char *key,  CONFIG_STRUCT *conf)
{
  char url[MAX_PATH +1];
  P_NODE *p = build_option_person_list (conf);	/* shared_person_list.c */

  cs_set_course_info(conf);
  cs_set_current_time();
  snprintf(url, MAX_PATH +1, "%s?id=%s&amp;crs=%s", "stats_menu", key, course);
  cs_set_value("back_url", url);
  
  set_option_userlist (p);	/* shared_person_list.c */
  free_option_person_list (p);	/* shared_person_list.c */
}



int
main ()
{
  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */
  SESSION user;
  CONFIG_STRUCT conf;		/* the configuration read from config file */

  cs_cgi_init();

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

  if ( (user.group == FACULTY) || (user.group == ADMIN))
    show_form (course, key, &conf);
  else
    cs_critical_error (ERR_REQUEST_DENIED, "");

  cs_cgi_display("stats_calendar_select", 1);
  cs_cgi_destroy();
  return 0;			/* exit successfully */

}

