#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#include "ClearSilver.h"
#include "sqlite3.h"
#include "global.h"
#include "custom.h"		/* for date FORMAT strings */
#include "grade.h"		/* for date FORMAT strings */
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_grade_book_menu.h"
#include "manhat-lib/shared_grade_book_db.h"
#include "manhat-lib/shared_grade_book_util.h"
#include "manhat-lib/shared_time.h"

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

}


void do_action(CONFIG_STRUCT *conf, const char *course, const char *key)
{
   
    cs_set_int_value("max_item_len", MAX_ITEM_LEN);
    
    get_items(conf, GRADE_BOOK_DB, 1);         /* shared_grade_book_db.h */
    get_categories(conf, GRADE_BOOK_DB);    /* shared_grade_book_db.h */

    if(global_cat_list && global_cat_list->head)
    {

           set_cat_list_values();   /* shared_grade_book_util.h */
           free_category_list(global_cat_list);  /* shared_grade_book_util.h */

         if(global_item_list && global_item_list->head)
         {
              set_item_list_values(1, 0);   /* shared_grade_book_util.h */
              free_item_list(global_item_list);  /* shared_grade_book_util.h */
    
         }
        else
            cs_set_int_value("no_items", 1);
        cs_load_lang("grade_book_default_letter_grades");
        cs_cgi_display ("grade_book_items", 1);
    }
    else
    {
       cs_set_int_value("no_category", 1);
       cs_set_int_value("no_items", 1);
       cs_cgi_display ("grade_book_items", 1);
    }
}



int
main ()
{
  char course[MAX_PATH +1];
  char key[MAX_KEY +1];
  SESSION user;
  CONFIG_STRUCT conf;
  
  cs_cgi_init();   /* shared_cs_util.h */
  read_parameters ( course, key);
  read_configuration_file (course, &conf);	
  validate_key (key, &user, &conf);	
  if(user.group != FACULTY)
     cs_critical_error(ERR_REQUEST_DENIED, "grade_book_items");

  cs_set_course_info(&conf);  
  set_grade_book_menu("grade_book_items", key, course); /* shared_grade_book_menu.h */
  cs_load_lang("grade_book_menu_util");
  do_action(&conf, course, key);
  cs_cgi_destroy();      

  return 0;			

}




