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

#include "ClearSilver.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_person_list.h"
#include "manhat-lib/shared_grades_util.h"
#include "manhat-lib/shared_cs_util.h"


GRADES_INFO_NODE *
build_list (CONFIG_STRUCT *conf)
{
    P_NODE *p_head, *pptr;
    GRADES_INFO_NODE *to_head = 0;
    GRADES_INFO_NODE *ptr, *current_ptr = 0;
    p_head = build_option_person_list (conf);
    for(pptr = p_head; pptr; pptr = pptr->next)
    {
       if(pptr->data.group != FACULTY && *(pptr->data.realname) != '*')
       {
          ptr = ( GRADES_INFO_NODE *)malloc(sizeof(GRADES_INFO_NODE));
           strncpy (ptr->info.username, pptr->data.username, MAX_USERNAME + 1);
	  strncpy (ptr->info.realname, pptr->data.realname, MAX_NAME + 1);
	  ptr->info.time_read = 0;
	  ptr->next = 0;
          if(!to_head)
            to_head = ptr;
          else
            current_ptr->next = ptr;
           current_ptr = ptr;

       }
    }
    free_option_person_list (p_head);
    return to_head;
}


static void
free_list (GRADES_INFO_NODE *to_head)
{
  GRADES_INFO_NODE *ptr;

  while (to_head)
    {
      ptr = to_head;
      to_head = to_head->next;
      free (ptr);
    }

}



GRADES_INFO_NODE *
get_grades_info (CONFIG_STRUCT *conf, SESSION *user)
{
  FILE *fp;
  char inf_fname[MAX_PATH + 1];
  GRADES_INFO_STRUCT buf;
  GRADES_INFO_NODE *ptr;
  GRADES_INFO_NODE *to_head = build_list (conf);

  snprintf (inf_fname, MAX_PATH + 1, "%speople/%s/grades/%s",
	    conf->course_path, user->username, GRADES_INF_FNAME);


  fp = fopen (inf_fname, "r");
      
  if (fp)
  {
      get_shared_lock(fileno(fp), 1); 
      while (fread (&buf, sizeof (GRADES_INFO_STRUCT), 1, fp) == 1)
	{
	  for (ptr = to_head; ptr; ptr = ptr->next)
	    if (!strcmp (ptr->info.username, buf.username))
	      {
		ptr->info.time_read = buf.time_read;
		break;
	      }
	}
      release_lock(fileno(fp));
      fclose (fp);
    }
  return to_head;
}


void
set_grades_info(CONFIG_STRUCT *conf, SESSION *user)
{
  #define MAX_TEMP_LEN 35
   char name[MAX_TEMP_LEN +1];
   int i =0;
   GRADES_INFO_NODE *head;
   GRADES_INFO_NODE *ptr;
   char timestring[MAX_TIMESTRING +1] ;
   
   head = get_grades_info (conf, user);
   for(ptr = head; ptr; ptr = ptr->next)
   {
      snprintf(name, MAX_TEMP_LEN +1, "std.%d.name", i);
      cs_set_value( name, ptr->info.realname);
      snprintf(name, MAX_TEMP_LEN +1, "std.%d.time", i);
      if(ptr->info.time_read)
      {
          strftime (timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,
                     localtime (&ptr->info.time_read));
         cs_set_value( name, timestring);
      }
      i++;
   }
   free_list (head);
   
  #undef MAX_TEMP_LEN
}


void
set_info( const char *course, const char *key, CONFIG_STRUCT *conf,SESSION *user) 
{

    cs_set_course_info( conf);    /* sahred_cs_grade.c */
    cs_set_current_time();    /* sahred_cs_grade.c */
    if(conf->grades == 1 || conf->grades == 3)
      cs_set_back_url(course, key, "grades_fac_menu");  /* shared_cs_grade.c */

    set_grades_info(conf, user);
}


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


int
main ()
{
  char course[MAX_PATH +1];
  char key[MAX_KEY +1];
  SESSION user;
  CONFIG_STRUCT conf;

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

  if(user.group != FACULTY)
       cs_critical_error(ERR_REQUEST_DENIED, "");   /** shared_cs_util.c */

  update_grades_inf(&conf, user.username);    /*shared_grades_util.c */
  set_info(  course, key, &conf, &user);
  cs_cgi_display ("grades_info", 1); /*shared_cs_util.c */
  cs_cgi_destroy();          /* shared_cs_util.c */ 
                    

  return 0;			/* exit successfully */

}







