#include <stdio.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_cs_util.h"

void
set_student_info(CONFIG_STRUCT *conf)
{
  #define MAX_TEMP_LEN 35
   P_NODE *ptr, *head;
   char name[MAX_TEMP_LEN +1];
   int i =0;
   
   head = build_option_person_list(conf);   /* shared_person_list.c */
   for (ptr = head; ptr; ptr = ptr->next)
   {
        if( (ptr->data.group == STUDENT) && (*(ptr->data.realname) != '*') )
	{
	   snprintf(name, MAX_TEMP_LEN +1, "std.%d.username", i);
	   cs_set_value( name, ptr->data.username);
	   snprintf(name, MAX_TEMP_LEN +1, "std.%d.id", i);
	   cs_set_value( name, ptr->data.id);
	   snprintf(name, MAX_TEMP_LEN +1, "std.%d.realname", i);
	   cs_set_value( name, ptr->data.realname);
	  i++;
	}
   }
#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)
    {
       if(user->group == FACULTY)
          cs_set_back_url( course, key, "grades_fac_menu");  /* shared_cs_grade.c */
       cs_set_value( "grade_action", "grades_view");
    }
    else if(conf->grades == 3)
    {
       if(user->group == FACULTY)
          cs_set_back_url( course, key, "grades_fac_menu");  /* shared_cs_grade.c */
       cs_set_value( "grade_action", "gradequick_view");
    }
    else
       cs_set_value( "grade_action", "grade_book_std");
     set_student_info(conf);
}

	
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);	

  set_info( course, key, &conf,&user);
  cs_cgi_display ("grades_view_form", 1); /*shared_cs_util.c */
 
  cs_cgi_destroy();                   

  return 0;			/* exit successfully */

}






