#include <stdio.h>


#include "global.h"
#include "custom.h"

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

static
void read_parameters (char *course, char *key, char *sender,  char *info_fname, long *offset)
{
  char tmp[25];

  cs_get_required_parameter ("crs", course, MAX_FILENAME);	/* shared_cgi_util.c */
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_get_required_parameter ("sender", sender, MAX_USERNAME);
  cs_get_required_parameter ("inf", info_fname, MAX_FILENAME);  
  cs_get_required_parameter  ("loc", tmp, 24);
  *offset = atol(tmp);
  
}


static void
print_error(int question,  char *string)
{
 static int first_error = 1;
 char name[MAX_PATH +1];

 if(first_error)
  {
   cs_set_int_value("has_error", 1);
   first_error = 0;
  }
  
 if(question) 
 {
     snprintf(name, MAX_PATH +1, "error.%d.num", question);
     cs_set_int_value(name, question);
     snprintf(name, MAX_PATH +1, "error.%d.type", question);
     cs_set_value(name, string);
 }
 else
     cs_set_value("parse_error", "parse_error");
 
             
}

 
static
void check_no_correct_answers(QUESTION_NODE *qptr, int question)
{

 if(!qptr->correct_answer_count)
    print_error(question, "multi_no_answer");  /* 0 means no correct answer */

}


static void
print_mc_warnings(QUESTION_NODE *qptr, int question)
{
 if(qptr->correct_answer_count == qptr->answer_count)
   print_error(question, "multi_all");  /* 1 means mc type question has all correct answers */ 
 else
  if(qptr->correct_answer_count > 1)
    print_error(question, "multi_more_than_one"); /* 2 means mc type question has more than one correct answer */
}


static
void print_short_warnings(QUESTION_NODE *qptr, int question)
{
 if(qptr->correct_answer_count != qptr->answer_count)
    print_error(question, "short_answers");  /* 3 means SHORT WITH INCORRECT ANSWERS */

}

static 
void check_question(QUESTION_NODE *qptr, int question)
{
 check_no_correct_answers(qptr,question);
 
 switch(qptr->question_type)
 {
  case MC: print_mc_warnings(qptr,question); break;
  case SHORT: print_short_warnings(qptr, question);    break;
  case MC_MULTI: break;
  default: print_error(question, "unknown_type"); break;
 }
}


static void
report_errors()
{
  QUESTION_NODE *qptr;
  int question;
 
  if(!quiz_data.head)
    print_error(0, "no_question_found");   /* NO QUESTIONS FOUND */
  else
   {
    for(qptr=quiz_data.head, question = 1; qptr; qptr=qptr->next, question++)
       check_question(qptr,question);
   }
}         




static
void report_results(const char *course, const char *key, int parse_error)
{
 char url[MAX_PATH +1];

 snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;grp=%d",
                            "news_topic", course, key, ASSIGNMENTS);
  cs_set_value("onload_url", url);
                 
 if(parse_error)
    print_error(0, "parse_error");     /* PARSE  ERROR */
 else        
    report_errors();     
      
}

                            
 





int main()
{
  char course[MAX_PATH + 1];	     /* from crs=? command line */
  char key[MAX_KEY + 1];	     /* from id=? command line */
  char sender[MAX_USERNAME + 1];     /* from user=? command line */
  char info_fname[MAX_FILENAME + 1]; /* from info=? cmd line */
  long offset;                       /* from loc=? cmd line */
  int parse_error;

  SESSION user;
  CONFIG_STRUCT conf;		/* the configuration read from config file */

  char source_path[MAX_PATH + 1];
  char dirname[MAX_FILENAME + 1];


  cs_cgi_init();
  read_parameters (course, key, sender, info_fname, &offset);
  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);	/* shared_util.c */

  cs_set_course_info(&conf);
  cs_set_current_time();
  if(user.group != FACULTY)
      cs_critical_error(ERR_REQUEST_DENIED, "");
 
  get_hotpot_exam_source_fname (source_path, dirname, conf.course_path, sender, info_fname);  /* shared_hotpot_exam_util.c */      
  
  parse_error = parse_hotpot_file(source_path);   /* shared_hotpot_parser.c */

  report_results(course, key, parse_error);

  free_quiz_data();     /* shared_hotpot_parser.c */    
  cs_cgi_display("hotpot_exam_check", 1);
  cs_cgi_destroy();
 return 0;
}


