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

#include "global.h"

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



static void
read_parameters (char *course, char *key)
{
  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_get_required_parameter ("id", key, MAX_KEY);
}


static void
show_stats_form (const char *course, const char *key, CONFIG_STRUCT *conf)
{
 #define MAX_TMP_NAME 30
  char url[MAX_PATH +1];
  int i=0;
  char name[MAX_TMP_NAME];

  snprintf (url, MAX_PATH +1, "%s?crs=%s&amp;id=%s","admin_main_menu", course, key);
  cs_set_value("back_url", url);
  cs_set_course_info(conf);
  cs_set_current_time();

  snprintf(name, MAX_TMP_NAME, "item.%d.name", i); 
  cs_set_value(name, "recent_logins");

  snprintf(name, MAX_TMP_NAME, "item.%d.url", i); 
  snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s","stats_recent_logins", course, key);
  cs_set_value(name, url);
  i++; 


  snprintf(name, MAX_TMP_NAME, "item.%d.name", i); 
  cs_set_value(name, "event_calendar");


 /* event calendar link is live only when not in capture mode */
 if(conf->access != ACCESS_CAPTURE)
 {
  snprintf(name, MAX_TMP_NAME, "item.%d.url", i); 
  snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;list=0","stats_calendar_select", course, key);
  cs_set_value(name, url);
 }
  i++; 
     

  snprintf(name, MAX_TMP_NAME, "item.%d.name", i); 
  cs_set_value(name, "event_list");


/* event list link is live only when not in capture mode */
 if(conf->access != ACCESS_CAPTURE)
  {
     snprintf(name, MAX_TMP_NAME, "item.%d.url", i); 
     snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;list=1","stats_calendar_select", course, key);
     cs_set_value(name, url);
  }
  i++; 
  
  snprintf(name, MAX_TMP_NAME, "item.%d.name", i); 
  cs_set_value(name, "stats_disk");
  
  
  snprintf(name, MAX_TMP_NAME, "item.%d.url", i); 
  snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s", "stats_disk", course, key);
  cs_set_value(name, url);
  
  i++; 
 #undef MAX_TMP_NAME 


}




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; 


  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_stats_form (course, key, &conf);
  else
    cs_critical_error (ERR_REQUEST_DENIED, "");
  cs_cgi_display("stats_menu", 1);
  cs_cgi_destroy();
  return 0;			/* exit successfully */

}

