#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_file_util.h"
#include "manhat-lib/shared_grade_book_util.h"
#include "manhat-lib/shared_grade_book_db.h"
#include "manhat-lib/shared_person_list.h"

void
read_parameters ( char *course, char *key, int *item_id, char *username)
{
     char temp[MAX_KEY +1];

     cs_get_required_parameter("crs", course, MAX_PATH);
     cs_get_required_parameter("id", key, MAX_KEY);
     cs_get_required_parameter("item_id", temp, MAX_KEY);
     *item_id = atoi(temp);
     cs_get_required_parameter("username", username, MAX_USERNAME);

}


void set_student_grade(const char *username) 
{
   char name[MAX_PATH];
   char temp[MAX_PATH];
   GRADE *ptr;

   cs_set_value("items.0.item_name", global_item_list->head->item_name);
   snprintf(temp, MAX_PATH, "%4.2f", global_item_list->head->points);
   cs_set_value("items.0.points", temp);
   if(global_item_list->head->grades)
   {
      ptr = global_item_list->head->grades->head;
      if(strcmp(global_item_list->head->is_letter_grade, "no") ==0)
      {
	  snprintf(name, MAX_PATH, "score.%s.score", username);
	  snprintf(temp, MAX_PATH, "%4.2f", ptr->score);
	  cs_set_value(name, temp);

      }
      else
      {
	  snprintf(name, MAX_PATH, "score.%s.letter", username);
	  cs_set_value(name, ptr->letter);
      }
      snprintf(name, MAX_PATH, "score.%s.comment", username);
      cs_set_value(name, ptr->comment);
      snprintf(name, MAX_PATH, "score.%s.excuse", username);
      cs_set_value(name, ptr->excuse);
      snprintf(name, MAX_PATH, "score.%s.pending", username);
      cs_set_value(name, ptr->pending);

   }
    
}

void set_one_stuent_info(P_NODE *head, const char *username) 
{
    P_NODE *ptr;
    
    for(ptr = head; ptr; ptr = ptr->next)
    {
	if(strcmp(ptr->data.username, username) ==0)
	{
	     cs_set_value("std.0.id", ptr->data.id);
	     cs_set_value("std.0.username", ptr->data.username);
	     cs_set_value("std.0.realname", ptr->data.realname);
	}
    }
}


void  do_action(CONFIG_STRUCT *conf, int item_id, const char *course, const char *key, const char *username)
{
    P_NODE *head;

    get_one_item(conf, item_id, GRADE_BOOK_STUDENT_DB);        /* shared_grade_book_db.c */
    if(!global_item_list || !global_item_list->head)
       cs_critical_error(ERR_GRADES_DB, "There is no item in db");
    if(strcmp(global_item_list->head->grade_entered, "no") == 0)
       cs_critical_error(ERR_GRADES_DB, "item is not graded");
    get_student_one_item_grade(conf, global_item_list->head->item_id, username, GRADE_BOOK_STUDENT_DB); /* shared_grade_book_db.c */
    if(!global_grade_list || !global_grade_list->head)
       cs_critical_error(ERR_GRADES_DB, "No grade for this student");

    if(strcmp(global_item_list->head->is_letter_grade, "yes") == 0)
        update_letter_log_list(conf,  global_item_list->head->item_id, GRADE_BOOK_STUDENT_DB, global_grade_list);  /* shared_grade_book_db.h */
     global_item_list->head->grades = global_grade_list;
    
     set_student_grade(username); 
     head = build_option_person_list(conf);
     set_one_stuent_info(head, username); 
     
     if(head)
	free_option_person_list(head);
     if(global_item_list)
        free_item_list(global_item_list);    /* shared_grade_book_util.c */
}




int
main ()
{
  char course[MAX_PATH +1];
  char key[MAX_KEY +1];
  char username[MAX_USERNAME +1];
  int item_id;
  SESSION user;
  CONFIG_STRUCT conf;
  
  cs_cgi_init();   /* shared_cs_util.h */
  read_parameters ( course, key, &item_id, username);
  read_configuration_file (course, &conf);	
  validate_key (key, &user, &conf);	

  if(user.group == STUDENT && strcmp(user.username, username) != 0)
	 cs_critical_error(ERR_REQUEST_DENIED, "");


  do_action(&conf, item_id, course, key, username);
  cs_set_int_value("show_comment", 1);
  cs_cgi_display ("grade_book_enter_grades", 1);
  cs_cgi_destroy();      

  return 0;			

}


