File:  [LON-CAPA] / capa / capa51 / pProj / capaCgiUtils.c
Revision 1.7: download - view: text, annotated - select for diffs
Fri Dec 3 18:39:38 1999 UTC (24 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Fixed TScore so it calculates the homework/quiz scores correctly
- Fixed typo in quizzer.tcl
- added config options tscore_width and tscore_height
- scorer should Quit properly in all cases now

    1: /* ===================================================================== */
    2: /*   copyrighted by Isaac Tsai, 1998, 1999, 2000    */
    3: /* ===================================================================== */
    4: #include <stdio.h>
    5: #include <ctype.h>
    6: #ifndef NO_STDLIB_H
    7: #include <stdlib.h>
    8: #else
    9: char *getenv();
   10: #endif
   11: #include <stdio.h>
   12: 
   13: #include "capaToken.h"
   14: #include "capaParser.h"
   15: #include "capaCommon.h"
   16: #include "ranlib.h"
   17: 
   18: #ifdef _MAIN_PROGRAM_
   19: #undef _MAIN_PROGRAM_
   20: #endif
   21: 
   22: #include "capaCGI.h"
   23: 
   24: void getword
   25: CAPA_ARG((char *word, char *line, char stop))
   26: {
   27:     int x = 0,y;
   28: 
   29:     for(x=0;((line[x]) && (line[x] != stop));x++)
   30:         word[x] = line[x];
   31: 
   32:     word[x] = '\0';
   33:     if(line[x]) ++x;
   34:     y=0;
   35: 
   36:     while((line[y++] = line[x++]));
   37: }
   38: 
   39: char *makeword
   40: CAPA_ARG((char *line, char stop))
   41: {
   42:     int x = 0,y;
   43:     char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
   44: 
   45:     for(x=0;((line[x]) && (line[x] != stop));x++)
   46:         word[x] = line[x];
   47: 
   48:     word[x] = '\0';
   49:     if(line[x]) ++x;
   50:     y=0;
   51: 
   52:     while((line[y++] = line[x++]));
   53:     return word;
   54: }
   55: 
   56: char *fmakeword
   57: CAPA_ARG((FILE *f,char  stop,int * cl))
   58: {
   59:     int wsize;
   60:     char *word;
   61:     int ll;
   62: 
   63:     wsize = 102400;
   64:     ll=0;
   65:     word = (char *) malloc(sizeof(char) * (wsize + 1));
   66: 
   67:     while(1) {
   68:         word[ll] = (char)fgetc(f);
   69:         if(ll==wsize) {
   70:             word[ll+1] = '\0';
   71:             wsize+=102400;
   72:             word = (char *)realloc(word,sizeof(char)*(wsize+1));
   73:         }
   74:         --(*cl);
   75:         if((word[ll] == stop) || (feof(f)) || (!(*cl))) {
   76:             if(word[ll] != stop) ll++;
   77:             word[ll] = '\0';
   78:             return word;
   79:         }
   80:         ++ll;
   81:     }
   82: }
   83: 
   84: char x2c
   85: CAPA_ARG((char *what))
   86: {
   87:     register char digit;
   88: 
   89:     digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
   90:     digit *= 16;
   91:     digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
   92:     return(digit);
   93: }
   94: 
   95: void unescape_url
   96: CAPA_ARG((char *url))
   97: {
   98:     register int x,y;
   99: 
  100:     for(x=0,y=0;url[y];++x,++y) {
  101:         if((url[x] = url[y]) == '%') {
  102:             url[x] = x2c(&url[y+1]);
  103:             y+=2;
  104:         }
  105:     }
  106:     url[x] = '\0';
  107: }
  108: 
  109: void plustospace
  110: CAPA_ARG((char *str))
  111: {
  112:     register int x;
  113: 
  114:     for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
  115: }
  116: 
  117: int rind
  118: CAPA_ARG((char *s,char c))
  119: {
  120:     register int x;
  121:     for(x=strlen(s) - 1;x != -1; x--)
  122:         if(s[x] == c) return x;
  123:     return -1;
  124: }
  125: 
  126: int getline
  127: CAPA_ARG((char *s,int n,FILE *f)) 
  128: {
  129:     register int i=0;
  130: 
  131:     while(1) {
  132:         s[i] = (char)fgetc(f);
  133: 
  134:         if(s[i] == CR)
  135:             s[i] = fgetc(f);
  136: 
  137:         if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) {
  138:             s[i] = '\0';
  139:             return (feof(f) ? 1 : 0);
  140:         }
  141:         ++i;
  142:     }
  143: }
  144: 
  145: void send_fd
  146: CAPA_ARG((FILE *f, FILE *fd))
  147: {
  148:     char c;
  149: 
  150:     while (1) {
  151:         c = fgetc(f);
  152:         if(feof(f)) return;
  153:         fputc(c,fd);
  154:     }
  155: }
  156: 
  157: int ind
  158: CAPA_ARG((char *s,char c))
  159: {
  160:     register int x;
  161: 
  162:     for(x=0;s[x];x++)
  163:         if(s[x] == c) return x;
  164: 
  165:     return -1;
  166: }
  167: 
  168: void escape_shell_cmd
  169: CAPA_ARG((char *cmd))
  170: {
  171:     register int x,y,l;
  172: 
  173:     l=strlen(cmd);
  174:     for(x=0;cmd[x];x++) {
  175:         if(ind("&;`'\"|*?~<>^()[]{}$\\",cmd[x]) != -1){
  176:             for(y=l+1;y>x;y--)
  177:                 cmd[y] = cmd[y-1];
  178:             l++; /* length has been increased */
  179:             cmd[x] = '\\';
  180:             x++; /* skip the character */
  181:         }
  182:     }
  183: }
  184: 
  185: /* ==========================================  Updated according to Frank Wolfs 
  186:    description on July 7 1997 */
  187: char *c_getpath
  188: CAPA_ARG((FILE *f)) 
  189: {
  190:   register int c;
  191:   register int idx;
  192:   char     tmp_string[MAX_BUFFER_SIZE];
  193:   char     *new_string;
  194: 
  195:   idx = 0;
  196:   tmp_string[0]='\0';
  197:   c_ignorewhite(f);
  198:   do {  
  199:     c = getc(f);
  200:     tmp_string[idx] = c;
  201:     idx++;
  202:   } while (isalnum(c) || c == '{' || c == '}' || c == '-' || c == '\\' ||
  203: 	   c == '^'   || c == '_' || c == '/' || c == '.' || c == ':' ||
  204: 	   c == '+'   || c == '*' || c == '#' || c == '!' || c == '=' || 
  205: 	   c == ';'   || c == '$' || c == '(' || c == ')' || c == '[' ||
  206: 	   c == ']'   || c == '?' || c == '>' || c == '<' || c == ',');
  207:   ungetc(c,f); idx--;
  208:   tmp_string[idx] = 0;
  209:   new_string = (char *)malloc( (idx+1)*sizeof(char) );
  210:   strncpy(new_string,tmp_string, (idx+1) );
  211:   return (new_string);
  212: }
  213: 
  214: /* ------------------------------------------------------------------------- */
  215: 
  216: void web_printheader(FILE *out)
  217: {
  218:   FILE *header;
  219:   char *buf[MAX_BUFFER_SIZE];
  220:   int amt=0;
  221: 
  222:   if ((capa_access("HTMLheader",F_OK|R_OK)!=-1) &&
  223:       (NULL!=(header=fopen("HTMLheader","r")))) {
  224:     while(0 < (amt=fread(buf,1,MAX_BUFFER_SIZE,header))) {
  225:       fwrite(buf,1,amt,out);
  226:     }
  227:     fclose(header);
  228:   } else {
  229:     fprintf(out,"<HTML><HEAD>\n");
  230:     fprintf(out,"<BODY BGCOLOR=\"#FFFFFF\" LINK=\"#0000EE\" VLINK=\"#EE1100\">\n");
  231:   }
  232: 
  233: #ifdef  CAPA_WEB
  234:   fprintf(out,"<!-- capasbin, CAPA Version %s, %s -->\n",
  235: 	  CAPA_VER,COMPILE_DATE);
  236: #else
  237:   fprintf(out,"<!-- capahtml, CAPA Version %s, %s -->\n",
  238: 	  CAPA_VER,COMPILE_DATE);
  239: #endif
  240: }
  241: 
  242: void web_printfooter(FILE *out)
  243: {
  244:   FILE *footer;
  245:   char *buf[MAX_BUFFER_SIZE];
  246:   int amt=0;
  247: 
  248:   if ((capa_access("HTMLfooter",F_OK|R_OK)!=-1) &&
  249:       (NULL!=(footer=fopen("HTMLfooter","r")))) {
  250:     while(0 < (amt=fread(buf,1,MAX_BUFFER_SIZE,footer))) {
  251:       fwrite(buf,1,amt,out);
  252:     }
  253:     fclose(footer);
  254:   } else {
  255:     fprintf(out,"</BODY></HTML>\n");
  256:   }
  257: }
  258: 
  259: int  web_getclassdir
  260: CAPA_ARG((char **cpath_p, char **cown_p, char *class))
  261: {
  262:     FILE     *fp;
  263:     char      filename[FILE_NAME_LENGTH];
  264:     char     *cname_p;
  265:     int       done;
  266:     char      c;
  267:     
  268:     sprintf(filename,"class.conf");
  269:     if ((fp=fopen(filename,"r"))==NULL) {
  270:       sprintf(filename,"../class.conf");
  271:       if ((fp=fopen(filename,"r"))==NULL) {
  272:         fprintf(stdout,"<!-- Error: can't open %s --> \n",filename); fflush(stdout);
  273:         return (2);
  274:       }
  275:     }
  276:     do {
  277:       c_ignorewhite(fp);
  278:       c = getc(fp); ungetc(c,fp);
  279:       if( c != EOF ) {
  280:         cname_p = c_getword(fp);
  281:        *cpath_p = c_getpath(fp);
  282:        *cown_p  = c_getword(fp);
  283:         throwaway_line(fp);
  284:         if( ! strcasecmp(cname_p, class) ) {
  285:           done = 1;
  286:         } else {
  287:           free(cname_p); free(*cpath_p); free(*cown_p);
  288:           done = 0;
  289:         }
  290:       } else {
  291:         done = 1;
  292:       }
  293:     } while ( ! done );
  294:     fclose(fp);
  295:     free(cname_p);
  296:     return (1);
  297: }
  298: 
  299: int web_log(log_str)char *log_str;
  300: {
  301:    FILE  *fp;       
  302:    char   filename[FILE_NAME_LENGTH];
  303: 
  304:    sprintf(filename,"web_access.log");
  305:    if ((fp=fopen(filename,"a"))==NULL) {
  306:      return -1; 
  307:    }
  308:    flockstream(fp);
  309:    fprintf(fp,"%s",log_str);fflush(fp);
  310:    funlockstream(fp);
  311:    fclose(fp);
  312:    return 0;
  313: }
  314: 
  315: int  w_log_timing(student_number,set,section,log_string)
  316: char  student_number[MAX_STUDENT_NUMBER+1];     
  317: int   set;  
  318: int   section;                                    
  319: char *log_string;                                
  320: {                                                  
  321:    char   filename[FILE_NAME_LENGTH],            
  322:          *ct;                                    
  323:    FILE  *fp;                                     
  324:    time_t t;                                 
  325: 
  326:    sprintf(filename,"records/webtiming%d.log",set);
  327:    if ((fp=fopen(filename,"a"))==NULL) {
  328:       return (-1);
  329:    }
  330:    /* CREATE LOG ENTRY */
  331:    time(&t);
  332:    ct=ctime(&t);
  333:    ct[ strlen(ct)-1 ]=0; /* Trash newline */
  334:    fprintf(fp,"%s %s %s\n",student_number,ct,log_string); fflush(fp);
  335:    fclose(fp);
  336:    return (0);
  337: }
  338: 
  339: int  w_log_attempt(student_number,set,log_string)
  340: char  student_number[MAX_STUDENT_NUMBER+1];     
  341: int   set;  
  342: char *log_string;                                
  343: {                                                  
  344:    char   filename[FILE_NAME_LENGTH],            
  345:          *ct;                                    
  346:    FILE  *fp;                                     
  347:    time_t t;                                 
  348: 
  349:    sprintf(filename,"records/weblog%d.db",set);
  350:    if ((fp=fopen(filename,"a"))==NULL) {
  351:       return (-1);
  352:    }
  353: 
  354:    /* CREATE LOG ENTRY */
  355:    time(&t);
  356:    ct=ctime(&t);
  357:    ct[ strlen(ct)-1 ]=0; /* Trash newline */
  358:    fprintf(fp,"%s %s %s\n",student_number,ct,log_string); fflush(fp);
  359:    fclose(fp);
  360:    return (0);
  361: }
  362: 
  363: int  w_log_submissions(student_number,set,log_string)
  364: char  student_number[MAX_STUDENT_NUMBER+1];     
  365: int   set;  
  366: char *log_string;                                
  367: {                                                  
  368:    char   filename[FILE_NAME_LENGTH],timeStr[FILE_NAME_LENGTH],buf2[MAX_BUFFER_SIZE];
  369:    FILE  *fp;                                     
  370:    time_t t;            
  371:    struct tm     *tmtime;
  372:    int do_log_submissions=1,result;
  373:    char buf[MAX_BUFFER_SIZE];
  374: 
  375:    result=read_capa_config("do_log_submissions",buf);
  376:    if (result != 0 && result != -1) 
  377:      if (strcasecmp(buf2,"no")==0) 
  378:        do_log_submissions=0;
  379:    if (!do_log_submissions) return 0;
  380: 
  381:    sprintf(filename,"records/websubmissions%d.db",set);
  382:    if ((fp=fopen(filename,"a"))==NULL) {
  383:      return (-1);
  384:    }
  385: 
  386:    /* CREATE LOG ENTRY */
  387:    time(&t);
  388:    tmtime=localtime(&t);
  389:    strftime(timeStr,FILE_NAME_LENGTH,"%d/%m %X",tmtime);
  390:    /*ct[ strlen(ct)-1 ]=0;*/ /* Trash newline */
  391:    protect_log_string(log_string);
  392:    fprintf(fp,"%s\t%s\t%s\n",student_number,timeStr,log_string); fflush(fp);
  393:    fclose(fp);
  394:    return (0);
  395: }
  396: 
  397: 
  398: void w_get_responses(int x,int q_idx,char* submissions_str)
  399: {
  400:   int leng, sub_idx;
  401:   char buf[MAX_BUFFER_SIZE];
  402:   if( !strncmp(g_entries[x].name,"INPUT",5) ) {
  403:     if( index(g_entries[x].name, ',' ) == NULL ) {  /* only one answer */
  404:       sscanf(g_entries[x].name,"INPUT%d",&q_idx);
  405:       if( q_idx > 0 && q_idx < MAX_PROBLEM_CNT ) {
  406: 	if ( ! is_all_ws(g_entries[x].val) ) {
  407: 	  g_stu_ans_pp[q_idx] = (StudentAnswer_t *)capa_malloc(sizeof(StudentAnswer_t),1);
  408: 	  (g_stu_ans_pp[q_idx])->a_idx  = 1;
  409: 	  (g_stu_ans_pp[q_idx])->a_str  = strsave(g_entries[x].val);
  410: 	  (g_stu_ans_pp[q_idx])->a_next = NULL;
  411: 	  trim_response_ws((g_stu_ans_pp[q_idx])->a_str);
  412: 	}
  413: 	leng = strlen( g_entries[x].val );
  414: 	if ( leng > 0 ) {
  415: 	  sprintf(buf,"%d\t%s\t",q_idx,g_entries[x].val);
  416: 	  strcat(submissions_str,buf);
  417: 	}
  418:       }
  419:     } else { /* this answer belongs to /AND answers */
  420:       sscanf(g_entries[x].name,"INPUT%d,%d",&q_idx,&sub_idx);
  421:       if( q_idx > 0 && q_idx < MAX_PROBLEM_CNT ) {
  422: 	if ( ! is_all_ws(g_entries[x].val) ) { 
  423: 	  StudentAnswer_t *sa_p;
  424: 	  sa_p = (StudentAnswer_t *)capa_malloc(sizeof(StudentAnswer_t),1);
  425: 	  sa_p->a_idx  = sub_idx;
  426: 	  sa_p->a_str  = strsave(g_entries[x].val);
  427: 	  sa_p->a_next = NULL;
  428: 	  if( g_stu_ans_pp[q_idx] == NULL ) {
  429: 	    g_stu_ans_pp[q_idx] = sa_p;
  430: 	  } else { 
  431: 	    StudentAnswer_t *sb_p;
  432: 	    for(sb_p=g_stu_ans_pp[q_idx]; sb_p->a_next; sb_p=sb_p->a_next);
  433: 	    sb_p->a_next = sa_p;
  434: 	  }
  435: 	}
  436: 	leng = strlen( g_entries[x].val );
  437: 	if ( leng > 0 ) {
  438: 	  sprintf(buf,"%d\t%s\t",q_idx,g_entries[x].val);
  439: 	  strcat(submissions_str,buf);
  440: 	}
  441:       }
  442:     }
  443:   }
  444:   if( !strncmp(g_entries[x].name,"LAST",4) ) {  
  445:     StudentAnswer_t *sa_p;  
  446:     if( index(g_entries[x].name, ',' ) == NULL ) {  /* only one answer */
  447:       sscanf(g_entries[x].name,"LAST%d",&q_idx);
  448:       if( q_idx > 0 && q_idx < MAX_PROBLEM_CNT ) {
  449: 	sa_p = (StudentAnswer_t *)capa_malloc(sizeof(StudentAnswer_t),1);
  450: 	sa_p->a_idx  = 1;
  451: 	sa_p->a_str  = strsave(g_entries[x].val);
  452: 	sa_p->a_next = NULL;
  453: 	g_last_ans_pp[q_idx] = sa_p;
  454:       }
  455:     } else {
  456:       sscanf(g_entries[x].name,"LAST%d,%d",&q_idx,&sub_idx);
  457:       if( q_idx > 0 && q_idx < MAX_PROBLEM_CNT ) {
  458: 	sa_p = (StudentAnswer_t *)capa_malloc(sizeof(StudentAnswer_t),1);
  459: 	sa_p->a_idx  = sub_idx;
  460: 	sa_p->a_str  = strsave(g_entries[x].val);
  461: 	sa_p->a_next = NULL;
  462: 	if( g_last_ans_pp[q_idx] == NULL) {
  463: 	  g_last_ans_pp[q_idx] = sa_p;
  464: 	} else {
  465: 	    StudentAnswer_t *sb_p;
  466: 	    for(sb_p=g_last_ans_pp[q_idx]; sb_p->a_next; sb_p=sb_p->a_next);
  467: 	    sb_p->a_next = sa_p;
  468: 	}
  469:       }
  470:     }
  471:   }
  472: }
  473: 
  474: /* ========================================================================= */
  475: /* Check in:      Class name     (CLASS)
  476:                   Student Number (SNUM)
  477:                   CAPA ID        (CAPAID)
  478:                   M=1
  479:    Try set:       Class name     (CLASS)
  480:                   Student Number (SNUM)
  481:                   CAPA ID        (CAPAID)
  482: 		  First question to print (STARTNUM)
  483:                   M=2
  484:    View previous: Class name     (CLASS)
  485:                   Student Number (SNUM)
  486:                   CAPA ID        (CAPAID)
  487: 		  First question to print (STARTNUM)
  488:                   View set       (VSET)
  489:                   M=3
  490:    Set summary:   
  491:                   
  492:  --------------------------------------------------------------------------- */
  493:              
  494: int  w_get_input() 
  495: {
  496:   register  int   x,m=0;
  497:   int             cl, q_idx, result = 1;
  498:   T_header        header; 
  499:   char            log_str[MAX_BUFFER_SIZE],buf[MAX_BUFFER_SIZE];
  500:   char            submissions_str[MAX_BUFFER_SIZE];
  501:   time_t          curtime;
  502:   char           *time_str,*UNKNOWN="UNKNOWN";
  503:   int             error=0;
  504:   char           *envPtr=NULL,*envPtr2=NULL;
  505: #ifdef CGI_DBUG
  506:   fprintf(g_cgi,"Entered get_input()\n"); fflush(g_cgi);
  507: #endif /* CGI_DBUG */
  508: 
  509:   envPtr=getenv("REQUEST_METHOD");
  510:   if(!envPtr) { 
  511:     fprintf(stdout,"Enviroment variable REQUEST_METHOD not set.\n");
  512:     fprintf(stdout,"CAPA is improperly installed or run, please let your \
  513: instructor know about this error.\n");
  514:     fprintf(stdout,"No tries have been deducted for the last submission.\n");
  515:     error |= 1;
  516:     return error;
  517:   }
  518:   if(strcmp(envPtr,"POST")) {
  519:         fprintf(stdout,"This script should be referenced with a METHOD of POST.\n");
  520: 	fprintf(stdout,"CAPA is improperly installed or run, please let your \
  521: instructor know about this error.\n");
  522: 	fprintf(stdout,"No tries have been deducted for the last submission.\n");
  523:         fflush(stdout);
  524: #ifdef CGI_DBUG
  525:   fprintf(g_cgi,"Request_method is not POST\n"); fflush(g_cgi);
  526: #endif /* CGI_DBUG */        
  527:         error |= 2; return (error);
  528:   }
  529:   
  530:   envPtr=getenv("CONTENT_TYPE");
  531:   if(!envPtr) { 
  532:     fprintf(stdout,"Enviroment variable CONTENT_TYPE not set.\n");
  533:     fprintf(stdout,"CAPA is improperly installed or run, please let your \
  534: instructor know about this error.\n");
  535:     fprintf(stdout,"No tries have been deducted for the last submission.\n");
  536:     fflush(stdout);
  537:     error |= 4;
  538:     return error;
  539:   }
  540:   if(strncmp(envPtr,"application/x-www-form-urlencoded",33)) {
  541:         fprintf(stdout,"This script can only be used to decode form results. \n");
  542: 	fprintf(stdout,"CAPA is improperly installed or run, please let your \
  543: instructor know about this error.\n");
  544: 	fprintf(stdout,"No tries have been deducted for the last submission.\n");
  545:         fflush(stdout);
  546: #ifdef CGI_DBUG
  547: 	fprintf(g_cgi,"CONTENT_TYPE is not application/x-www-form-urlencoded\n"); fflush(g_cgi);
  548: #endif /* CGI_DBUG */              
  549:         error |= 8; return (error);
  550:   }
  551: 
  552:   envPtr=getenv("CONTENT_LENGTH");
  553:   if(!envPtr) { 
  554:     fprintf(stdout,"Enviroment variable CONTENT_LENGTH not set.\n");
  555:     fprintf(stdout,"CAPA is improperly installed or run, please let your \
  556: instructor know about this error.\n");
  557:     fprintf(stdout,"No tries have been deducted for the last submission.\n");
  558:     error |= 16;
  559:     return error;
  560:   }
  561:   cl = atoi(envPtr);
  562: #ifdef CGI_DBUG
  563:   fprintf(g_cgi,"CONTENT_LENGTH is %d\n",cl); fflush(g_cgi);
  564: #endif /* CGI_DBUG */
  565:   for(x=0;cl && (!feof(stdin));x++) {
  566:     m=x;
  567:     g_entries[x].val = fmakeword(stdin,'&',&cl);
  568:     plustospace(g_entries[x].val);
  569:     unescape_url(g_entries[x].val);
  570:     g_entries[x].name = makeword(g_entries[x].val,'=');
  571:   }
  572:   /* ---------------------------------------------------- */
  573:   g_entered_pin = 0; g_run_mode =0;
  574:   submissions_str[0]='\0';
  575:   for(x=0; x <= m; x++) {
  576:       if( !strcmp(g_entries[x].name,"CLASS") ) {
  577:         strncpy(g_class_name,g_entries[x].val,MAX_CLASS_CHAR);
  578:       }
  579:       if( !strcmp(g_entries[x].name,"M") ) {  /* run mode */
  580:         sscanf(g_entries[x].val,"%d",&g_run_mode);
  581:       }
  582:       if( !strcmp(g_entries[x].name,"STARTNUM")) {
  583: 	sscanf(g_entries[x].val,"%d",&g_start_question);
  584:       }
  585:       if( !strcmp(g_entries[x].name,"SNUM") ) {
  586:         strncpy(g_student_number,g_entries[x].val,MAX_STUDENT_NUMBER+4);
  587:       }
  588:       if( !strcmp(g_entries[x].name,"CAPAID") ) {
  589:         sscanf(g_entries[x].val,"%d",&g_entered_pin);
  590:       }
  591:       if( !strcmp(g_entries[x].name,"SET") ) {
  592:         sscanf(g_entries[x].val,"%d",&g_set);
  593:       }
  594:       if( !strcmp(g_entries[x].name,"VSET") ) {
  595: 	if (g_entries[x].val[0] == '\0') {
  596: 	  g_vset=0;
  597: 	} else {
  598: 	  sscanf(g_entries[x].val,"%d",&g_vset);
  599: 	}
  600:       }
  601:       if( !strcmp(g_entries[x].name,"KND") ) {
  602:         sscanf(g_entries[x].val,"%d",&g_skind);
  603:       }
  604:       w_get_responses(x,q_idx,submissions_str);
  605:       free(g_entries[x].val);
  606:       free(g_entries[x].name);
  607:   }
  608:   
  609: #ifdef CGI_DBUG
  610:   fprintf(g_cgi,"DONE: Parse input\n"); fflush(g_cgi);
  611: #endif /* CGI_DBUG */
  612:   /* --------------------------------------------------------- */
  613:   /* if( strcasecmp(g_prog_name,"capacheckin") == 0 ) { */
  614:   if( g_run_mode == M_CHECKIN ) { /* capa_checkin */
  615:     time(&curtime); time_str = ctime(&curtime);
  616:     time_str[ strlen(time_str)-1 ] = 0;
  617:     envPtr=getenv("REMOTE_HOST");
  618:     if(!envPtr) { 
  619:       fprintf(stdout,"<!-- Enviroment variable REMOTE_HOST not set.-->\n");
  620:       envPtr=getenv("REMOTE_ADDR");
  621:       if(!envPtr) { 
  622: 	fprintf(stdout,"<!-- Enviroment variable REMOTE_ADDR not set.-->\n");
  623: 	envPtr=UNKNOWN;
  624: 	error |= 32;
  625:       }
  626:     }
  627: #ifdef CGI_DBUG
  628:   fprintf(g_cgi,"DONE: REMOTE_HOST\n"); fflush(g_cgi);
  629: #endif /* CGI_DBUG */
  630: 
  631:     envPtr2=getenv("HTTP_USER_AGENT");
  632:     if(!envPtr2) { 
  633:       fprintf(stdout,"<!-- Enviroment variable HTTP_USER_AGENT not set. -->\n");
  634:       envPtr2=UNKNOWN;
  635:       error |= 64;
  636:     }
  637:     sprintf(log_str,"%s\t%s\t%s\t%s\t%s\n",g_class_name,g_student_number,time_str,envPtr,envPtr2);
  638:     if (web_log(log_str) == -1 ) {
  639:       fprintf(stdout,"Unable to log access to the system. Please notify instructor\n.");
  640:       fprintf(stdout,"No tries have been deducted for the last submission.\n");
  641:       error |= 128;
  642:       return error;
  643:     }
  644:     
  645:   }
  646: #if defined(NeXT) 
  647:    getwd(g_cwd);
  648: #else
  649:    getcwd(g_cwd,255);
  650: #endif
  651:   web_getclassdir(&g_cpath, &g_cowner, g_class_name);
  652:   sprintf(g_class_fullpath,"%s/%s",g_cpath,g_class_name);
  653: #ifdef CGI_DBUG
  654:   fprintf(g_cgi,"DONE: getclassdir() [%s]\n",g_class_fullpath); fflush(g_cgi);
  655: #endif /* CGI_DBUG */
  656:   if( !capa_access(g_class_fullpath, F_OK) == 0 ) {
  657:     fprintf(stdout,"ACCESS: could not access the class directory [%s]!\n",g_class_fullpath);
  658:     fprintf(stdout,"Please exit the web browser and try accessing the system again\n");
  659:     fprintf(stdout,"No tries have been deducted for the last submission.\n");
  660:     fflush(stdout);
  661:     sprintf(log_str,"Failed to access class dir, g_class_fullpath: %s\tg_cpath: %s\tg_class_name: %s\tg_cowner: %s\tg_cwd: %s\t",
  662: 	    g_class_fullpath,g_cpath,g_class_name,g_cowner,g_cwd);
  663:     if (web_log(log_str) == -1 ) {
  664:       fprintf(stdout,"Unable to log access to the system. Please notify instructor\n.");
  665:       fflush(stdout);
  666:       error |= 256;
  667:       return error;
  668:     } 
  669: #ifdef CGI_DBUG
  670:     fprintf(g_cgi,"NO ACCESS: cannot access() [%s]\n",g_class_fullpath); fflush(g_cgi);
  671: #endif /* CGI_DBUG */      
  672:     error |= 512;
  673:     return (error);
  674:   }
  675:   /* ---------------------------------------------------- */
  676:   /*      change working directory to the class           */
  677:   /* ---------------------------------------------------- */
  678:   chdir(g_class_fullpath); /* before performing any capa*() calls */
  679: #ifdef CGI_DBUG
  680:   fprintf(g_cgi,"DONE cd to [%s]\n",g_class_fullpath); fflush(g_cgi);
  681: #endif /* CGI_DBUG */
  682: 
  683:   /* Now in proper directory, can log submissions */
  684:   if ( g_run_mode == M_CHECKANS) {
  685:     if (w_log_submissions(g_student_number,g_set,submissions_str) == -1 ) {
  686:       fprintf(stdout,"Unable to log submissions. Please notify instructor\n.");
  687:       fprintf(stdout,"No tries have been deducted for the last submission.\n");
  688:       error |= 1024;
  689:       return error; 
  690:     }
  691:   }
  692: 
  693:   result=read_capa_config("capaweb_cgibin_path",buf);
  694:   if (result != 0 && result != -1) {
  695:     g_cgibin_path=capa_malloc(strlen(buf)+1,1);
  696:     strcpy(g_cgibin_path,buf);
  697:   } else {
  698:     g_cgibin_path=capa_malloc(strlen("capa-bin")+1,1);
  699:     strcpy(g_cgibin_path,"capa-bin");
  700:   }
  701: 
  702:   if( g_entered_pin != 0 ) {
  703:       g_login_set = capa_PIN(g_student_number,999,g_entered_pin);
  704: #ifdef CGI_DBUG
  705:   fprintf(g_cgi,"Succeed in login to set %d with pin %d\n",g_login_set,g_entered_pin); fflush(g_cgi);
  706: #endif /* CGI_DBUG */   
  707:     /* ---------------------------------------------------- 
  708:       printf("Your entered capa id %d, login to set %d\n", g_entered_pin, g_login_set);
  709:       printf("The real capa id for %s is %d\n",g_student_number,capa_PIN(g_student_number,1,0));
  710:     ---------------------------------------------------- */
  711:     
  712:   } else {
  713:     fprintf(stdout,"CAPA ID entered was zero, this is not valid.\n");
  714:     fprintf(stdout,"No tries have been deducted for the last submission.\n");
  715:     fflush(stdout);
  716:     error |= 2048; return (error);
  717:   }
  718: 
  719:   if (!g_login_set) { 
  720:         fprintf(stdout,"The student ID (%s) or CAPA ID (%d) that you entered \
  721: is not listed for the class (%s) that you have selected. Please check that \
  722: you have selected the correct class on the CAPA logon page and that the \
  723: student ID and CAPA ID are correct.\n", 
  724: 		g_student_number, g_entered_pin, g_class_name);
  725:         fflush(stdout);
  726: #ifdef CGI_DBUG
  727: 	fprintf(g_cgi,"CAPA ID or student number is not valid.\n"); 
  728: 	fflush(g_cgi);
  729: #endif /* CGI_DBUG */   
  730:         error |= 4096;  return (error);
  731:   } else {
  732:       if ( g_login_set > 99 )  { 
  733:         fprintf(stdout,"The student ID (%s) or CAPA ID (%d) that you entered \
  734: is not listed for the class (%s) that you have selected. Please check that \
  735: you have selected the correct class on the CAPA logon page and that the \
  736: student ID and CAPA ID are correct.\n", 
  737: 		g_student_number, g_entered_pin, g_class_name);
  738:            fflush(stdout);
  739: #ifdef CGI_DBUG
  740: 	   fprintf(g_cgi,"CAPA ID is not valid.\n"); fflush(g_cgi);
  741: #endif /* CGI_DBUG */            
  742:            error |= 8192; return (error);
  743:       }
  744:       if(g_login_set < g_vset ) {
  745:            fprintf(stdout,"Your CAPA ID (for set %d) does not allow you to view set %d.\n",g_login_set, g_vset);
  746:            fflush(stdout);
  747: #ifdef CGI_DBUG
  748: 	   fprintf(g_cgi,"Login set %d is less than view set %d.\n",g_login_set,g_vset); fflush(g_cgi);
  749: #endif /* CGI_DBUG */ 
  750:            error |= 16384; return (error);
  751:       }
  752:       chdir(g_class_fullpath);  /* again, to make sure */
  753:       
  754:       if ( capa_get_student(g_student_number,&g_student_data) == 0 ) {
  755:            fprintf(stdout,"Entered student id is not in the class list.\n");
  756: 	   fprintf(stdout,"Please check that have selected the correct class \
  757: and have entered you student id correctly.\n");
  758:            fflush(stdout);
  759: #ifdef CGI_DBUG
  760: 	   fprintf(g_cgi,"get_student(): Student id not in the classl file.\n"); fflush(g_cgi);
  761: #endif /* CGI_DBUG */               
  762:             error |= 32768; return (error);
  763:       } else {
  764: 	 time(&curtime);
  765: 	 if (capa_get_header(&header, g_login_set))  {
  766: 	   fprintf(stdout,"This problem set is not ready yet.\n"); 
  767: 	   fflush(stdout);
  768: 	  
  769: #ifdef CGI_DBUG
  770: 	   fprintf(g_cgi,"get_header(): Problem set not ready.\n"); fflush(g_cgi);
  771: #endif /* CGI_DBUG */            
  772: 	   error |= 65536; return (error);
  773:  	 }
  774: 	 capa_mfree(header.weight);
  775: 	 capa_mfree(header.partial_credit);
  776:          /* ===> if (compare_datetime(curtime,header.open_date) < 0 ) { */
  777:          if( capa_check_date(CHECK_OPEN_DATE,g_student_number,
  778: 			     g_student_data.s_sec,g_login_set) < 0 ) {
  779:             fprintf(stdout,"This set(%d) is not yet open. Please try back later.\n",g_login_set); 
  780: 	    fflush(stdout);
  781: #ifdef CGI_DBUG
  782: 	    fprintf(g_cgi,"check_date(): Problem set not open.\n"); 
  783: 	    fflush(g_cgi);
  784: #endif /* CGI_DBUG */    
  785:             error |= 131072; return (error);
  786:          }
  787:       }
  788:   }
  789:   return (error);
  790: }
  791: 
  792: /* ============================================================================= */
  793: void        append_qtext(new_str) char *new_str;
  794: {
  795:   int ii,len;
  796:   if (new_str==NULL) return;
  797:   len=strlen(new_str);
  798: #ifdef CGI_DBUG
  799:   fprintf(g_cgi,"before: len %d; g_qchar_cnt %d; g_qsize %d\n",
  800: 	  len,g_qchar_cnt,g_qsize);
  801:   fflush(g_cgi);
  802: #endif /* CGI_DBUG */    
  803:   if (g_qchar_cnt+len>g_qsize-1) {
  804:     char *temp_text;
  805:     g_qsize=(g_qchar_cnt+len)*2;
  806:     temp_text=capa_malloc(g_qsize,sizeof(char));
  807:     strncpy(temp_text,g_question_txt,g_qchar_cnt);
  808:     capa_mfree(g_question_txt);
  809:     g_question_txt=temp_text;
  810:   }
  811:   for(ii=0;ii<len;ii++) {
  812:     g_question_txt[g_qchar_cnt+ii]=new_str[ii];
  813:   }
  814:   g_qchar_cnt += len;
  815:   g_question_txt[g_qchar_cnt+1]='\0';
  816: #ifdef CGI_DBUG
  817:   fprintf(g_cgi,"after: len %d; g_qchar_cnt %d; g_qsize %d\n",len,g_qchar_cnt,g_qsize);
  818:   fflush(g_cgi);
  819: #endif /* CGI_DBUG */    
  820: }
  821: void        append_stext(new_str) char *new_str;
  822: {
  823:   int ii,len;
  824:   if (new_str==NULL) return;
  825:   len=strlen(new_str);
  826: #ifdef CGI_DBUG
  827:   fprintf(g_cgi,"appending status{%s}\nlen %d; g_schar_cnt %d; g_ssize %d\n",
  828: 	  new_str,len,g_schar_cnt,g_ssize);
  829:   fflush(g_cgi);
  830: #endif /* CGI_DBUG */    
  831:   if (g_schar_cnt+len>g_ssize-1) {
  832:     char *temp_text;
  833:     g_ssize=(g_schar_cnt+len)*2;
  834:     temp_text=capa_malloc(g_ssize,sizeof(char));
  835:     strncpy(temp_text,g_status_txt,g_schar_cnt);
  836:     capa_mfree(g_status_txt);
  837:     g_status_txt=temp_text;
  838:   }
  839:   for(ii=0;ii<len;ii++) {
  840:     g_status_txt[g_schar_cnt+ii]=new_str[ii];
  841:   }
  842:   g_schar_cnt += len;
  843:   g_status_txt[g_schar_cnt+1]='\0';
  844: #ifdef CGI_DBUG
  845:   fprintf(g_cgi,"len %d; g_schar_cnt %d; g_ssize %d\n",len,g_schar_cnt,g_ssize);
  846:   fflush(g_cgi);
  847: #endif /* CGI_DBUG */    
  848: }
  849: /* ============================================================================= */
  850: /* ------------------------------------------------------------
  851:   printf("ENV<br>\n");
  852:   printf("SERVER_PROTOCOL:%s<br>",getenv("SERVER_PROTOCOL"));
  853:   printf("PATH_INFO:%s<br>",getenv("PATH_INFO"));
  854:   printf("PATH_TRANSLATED:%s<br>\n",getenv("PATH_TRANSLATED"));
  855:   printf("SCRIPT_NAME:%s<br>\n",getenv("SCRIPT_NAME"));
  856:   printf("QUERY_STRING:%s<br>\n",getenv("QUERY_STRING"));
  857:   printf("REMOTE_HOST:%s<br>\n",getenv("REMOTE_HOST"));
  858:   printf("REMOTE_USER:%s<br>\n",getenv("REMOTE_USER"));
  859:   printf("REMOTE_IDENT:%s<br>\n",getenv("REMOTE_IDENT"));
  860:   printf("USER_AGENT:%s<br>\n",getenv("USER_AGENT"));
  861:   printf("HTTP_USER_AGENT:%s<br>\n",getenv("HTTP_USER_AGENT"));
  862:   ------------------------------------------------------------
  863: */
  864: 
  865: /* ------------------------------------------------------ */
  866: /*   A class directory must have   */
  867: /*     records/                    */
  868: /*                                 */
  869: /*  returns: 0  structure is correct, but no set.db files */
  870: /*          -1  structure is not correct                  */
  871: /*          >=1 the last set.db                           */
  872: 
  873: int
  874: check_class_get_maxset(dir_path) char  *dir_path;
  875: {
  876:   char   f_name[1024];
  877:   int    set;
  878:   
  879:   if( capa_access(dir_path, F_OK) == 0 ) { /* class dir exists */
  880:     sprintf(f_name,"%s/records",dir_path);
  881:     if( capa_access(f_name, F_OK) == 0 ) { /* class/records dir exists */
  882:       for(set = 1; ; set++ ) {
  883:         sprintf(f_name,"%s/records/set%d.db",dir_path,set);
  884:         if(capa_access(f_name, F_OK) == -1 )  break;
  885:       }
  886:       set--;
  887:     } else {
  888:       set = -1;
  889:     }
  890:   } else {
  891:     set = -1;
  892:   } 
  893:   return (set);
  894: }
  895: 
  896: /* ------------------------------------------------------------------------- */
  897: /* Get Exam and Quiz Path                                                    */
  898: /*   return  0, 1, 2, 3                                                      */
  899: /* 0: Neither exist                                                          */
  900: /* 1: Exam.path exists                                                       */
  901: /* 2: Quiz.path exists                                                       */
  902: /* 3: Both Exam.path and Quiz.path exists                                    */
  903: /* ------------------------------------------------------------------------- */
  904: int
  905: check_exam_quiz_path()
  906: {
  907:   char  buf[MAX_BUFFER_SIZE];
  908:   int   result = 0, configResult=0;
  909:   
  910:   configResult=read_capa_config("exam_path",buf);
  911:   if (configResult != 0 && configResult != -1) {
  912:     g_exam_set = check_class_get_maxset(buf);
  913:     if(g_exam_set > 0 )  {
  914:       result = 1;
  915:       sprintf(g_exam_path,buf);
  916:     }
  917:   }
  918:   configResult=read_capa_config("quiz_path",buf);
  919:   if (configResult != 0 && configResult != -1) {
  920:     g_quiz_set = check_class_get_maxset(buf);
  921:     if(g_quiz_set > 0 )  {
  922:       result = (result | 2);
  923:       sprintf(g_quiz_path,buf);
  924:     }
  925:   }
  926:   return (result);
  927: }
  928: 
  929: 
  930: int
  931: check_termscore_option()
  932: {
  933:   char  buf[MAX_BUFFER_SIZE];
  934:   int   result = 0, configResult=0;
  935:   
  936:   configResult=read_capa_config("term_score_applet",buf);
  937:   if (configResult != 0 && configResult != -1) {
  938:     fprintf(stdout,"<!-- term_score_applet is in capa.config file -->\n");
  939:     if (strcasecmp(buf,"yes")==0) {
  940:       fprintf(stdout,"<!-- term_score_applet is YES -->\n");
  941:       result=1;
  942:     }
  943:   }
  944:   return (result);
  945: }
  946: 
  947: 
  948: 
  949: 
  950: /* ============================================================================= */
  951: void
  952: print_mainmenu(class,sn,pin)char *class; char *sn;int pin;
  953: {
  954:   char  buf[MAX_BUFFER_SIZE];
  955:   int   outcome,configResult,term_summary_button=1;
  956:   char *serverName;
  957: 
  958:   serverName=getenv("SERVER_NAME");
  959:   if (!serverName) {
  960:     fprintf(stdout,"Enviroment variable SERVER_NAME not set.\n");
  961:     fprintf(stdout,"Unable to complete actions.\n");
  962:     return;
  963:   }
  964:   
  965:   fprintf(stdout,"<TITLE>%s main menu</TITLE>\n",g_class_name);
  966:   fprintf(stdout,"<h3>%s</h3><br>\n",g_student_name);
  967:   fprintf(stdout,"<menu>\n");
  968:   fprintf(stdout,"<li><form method=\"get\" action=\"http://%s/CAPA/help.html\">",serverName);
  969:   fprintf(stdout,"<input type=\"submit\" value=\"Help\" ></form>\n");
  970: 
  971:   fprintf(stdout,"<li><form method=\"post\" ");
  972:   /* fprintf(stdout," target=\"right_frame\" "); */
  973:   sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
  974:   fprintf(stdout,"%s\n", buf);
  975:   fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",class);
  976:   fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",sn);
  977:   fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",pin);
  978:   fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_TRYSET);
  979:   fprintf(stdout,"<input type=\"hidden\" name=\"STARTNUM\" value=\"%d\">\n",1);
  980:   fprintf(stdout,"<input type=\"submit\" value=\"Try current set\" ></form>\n");
  981:   
  982:   fprintf(stdout,"<li><form method=\"post\" ");
  983:   /* fprintf(stdout," target=\"right_frame\" "); */
  984:   sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
  985:   fprintf(stdout,"%s\n", buf);
  986:   fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",class);
  987:   fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",sn);
  988:   fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",pin);
  989:   fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_VIEWPREV);
  990:   fprintf(stdout,"<input type=\"hidden\" name=\"STARTNUM\" value=\"%d\">\n",1);
  991:   fprintf(stdout,"<input type=\"submit\" value=\"View previous set:\" >");
  992:   fprintf(stdout,"<b> set:</b><input name=\"VSET\" value=\"\" size=4></form>\n");
  993:   
  994:   /*Term Summary*/
  995:   configResult=read_capa_config("term_summary_button",buf);
  996:   if (configResult != 0 && configResult != -1 ) {
  997:     if (strcasecmp(buf,"no")==0) {
  998:       term_summary_button=0;
  999:     }
 1000:   }
 1001:   if (term_summary_button) {
 1002:     fprintf(stdout,"<li><form method=\"post\" ");
 1003:     /* fprintf(stdout," target=\"right_frame\" "); */
 1004:     sprintf(buf,"action=\"http://%s/%s/capahtml\">",serverName,g_cgibin_path);
 1005:     fprintf(stdout,"%s\n", buf);
 1006:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",class);
 1007:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",sn);
 1008:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",pin);
 1009:     fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_VIEWSUMM);
 1010:     fprintf(stdout,"<input type=\"submit\" value=\"Display term summary\" ></form>\n");
 1011:   }
 1012: 
 1013:   outcome = check_exam_quiz_path();
 1014:   if( outcome & 1 ) {  /* exam summary */
 1015:     fprintf(stdout,"<li><form method=\"post\" ");
 1016:     /* fprintf(stdout," target=\"right_frame\" "); */
 1017:     sprintf(buf,"action=\"http://%s/%s/capahtml\">",serverName,g_cgibin_path);
 1018:     fprintf(stdout,"%s\n", buf);
 1019:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",class);
 1020:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",sn);
 1021:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",pin);
 1022:     fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_EXAMSUMM);
 1023:     fprintf(stdout,"<input type=\"submit\" value=\"Display Exam summary\" ></form>\n");
 1024:   }
 1025:   if( outcome & 2 ) { /* Quiz summary */
 1026:     fprintf(stdout,"<li><form method=\"post\" ");
 1027:     /* fprintf(stdout," target=\"right_frame\" "); */
 1028:     sprintf(buf,"action=\"http://%s/%s/capahtml\">",serverName,g_cgibin_path);
 1029:     fprintf(stdout,"%s\n", buf);
 1030:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",class);
 1031:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",sn);
 1032:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",pin);
 1033:     fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\" >\n",M_QUIZSUMM );
 1034:     fprintf(stdout,"<input type=\"submit\" value=\"Display Quiz summary\" ></form>\n");
 1035:   }
 1036:   outcome = check_termscore_option();
 1037:   fprintf(stdout,"<!-- Outcome of check_termscore_option()=%d -->\n",outcome);
 1038:   /*Termscore Button*/
 1039:   if( outcome ) {
 1040:     fprintf(stdout,"<li><form method=\"post\" ");
 1041:    sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
 1042:     fprintf(stdout,"%s\n", buf);
 1043:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",class);
 1044:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",sn);
 1045:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",pin);
 1046:     fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\" >\n",M_TERMSCORE );
 1047:     fprintf(stdout,"<input type=\"submit\" value=\"Extrapolate Term score\" ></form>\n");
 1048:   }
 1049:   /*Exit Button*/
 1050:   fprintf(stdout,"<TD><form method=\"get\" action=\"http://%s/CAPA/class.html\">",serverName);
 1051:   fprintf(stdout,"<input type=\"button\" value=\"Exit\" onclick=\"window.close()\"></form></TD>");
 1052: 
 1053:   fprintf(stdout,"</menu>\n"); fflush(stdout);
 1054:   
 1055: }
 1056: 
 1057: /* ====================================================================================== */
 1058: void
 1059: print_page_header(mode,num_quest) int mode;int num_quest;
 1060: {
 1061:   char   buf[MAX_BUFFER_SIZE], discussdir[MAX_BUFFER_SIZE];
 1062:   char *serverName;
 1063:   int    configResult,term_summary_button=1;
 1064: 
 1065:   serverName=getenv("SERVER_NAME");
 1066:   if (!serverName) {
 1067:     fprintf(stdout,"Enviroment variable SERVER_NAME not set.\n");
 1068:     fprintf(stdout,"Unable to complete actions.\n");
 1069:     return;
 1070:   }
 1071:   
 1072:   /* now done in the .qz file
 1073:   fprintf(stdout,"<TITLE>%s set%d</TITLE>",g_class_name,g_login_set);
 1074:   if( mode == VIEW_PREVIOUS_MODE ) {
 1075:     fprintf(stdout,"<H2>%s set%d</H2>\n",g_class_name,g_vset);
 1076:   } else {
 1077:     fprintf(stdout,"<H2>%s set%d</H2>\n",g_class_name,g_login_set);
 1078:   }
 1079:   fprintf(stdout,"<H3>%s</H3>\n",g_student_data.s_nm);
 1080:   */
 1081:   
 1082:   fprintf(stdout,"<A NAME=\"TOP\"></A>");
 1083:   fprintf(stdout,"<TABLE cellpadding=0 cellspacing=0 border=0>\n<TR><TD>");
 1084: 
 1085: 
 1086:   /*Term summary button*/
 1087:   configResult=read_capa_config("term_summary_button",buf);
 1088:   if (configResult != 0 && configResult != -1 ) {
 1089:     if (strcasecmp(buf,"no")==0) {
 1090:       term_summary_button=0;
 1091:     }
 1092:   }
 1093: #ifdef CGI_DBUG
 1094:   fprintf(g_cgi,"buf: %s\ntermsum: %d\n",buf,term_summary_button); fflush(g_cgi);
 1095: #endif /* CGI_DBUG */ 
 1096: 
 1097:   if (term_summary_button) {
 1098:     fprintf(stdout,"<form method=\"post\" ");
 1099:     sprintf(buf,"action=\"http://%s/%s/capahtml\">",serverName,g_cgibin_path);
 1100:     fprintf(stdout,"%s\n", buf);
 1101:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
 1102:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
 1103:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
 1104:     fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_VIEWSUMM);
 1105:     fprintf(stdout,"<input type=\"submit\" value=\"Term summary\" ></form></TD>\n");
 1106:   }
 1107: 
 1108:   /*Exit Button*/
 1109:   fprintf(stdout,"<TD><form method=\"get\" action=\"http://%s/CAPA/class.html\">",serverName);
 1110:   fprintf(stdout,"<input type=\"button\" value=\"Exit\" onclick=\"window.close()\"></form></TD>");
 1111:   /*help button*/
 1112:   if (mode != VIEW_PREVIOUS_MODE) {
 1113:     fprintf(stdout,"<TD><form method=\"get\" action=\"http://%s/CAPA/help.html\">",serverName);
 1114:     fprintf(stdout,"<input type=\"submit\" value=\"Help\"></form></TD>");
 1115:   }
 1116: 
 1117:   /*Reload button*/
 1118:   fprintf(stdout,"<TD><form method=\"post\" ");
 1119:   sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
 1120:   fprintf(stdout,"%s\n", buf);
 1121:   fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
 1122:   fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
 1123:   fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
 1124:   fprintf(stdout,"<input type=\"hidden\" name=\"STARTNUM\" value=\"%d\">\n",g_start_question);
 1125:   if (mode == VIEW_PREVIOUS_MODE ) {
 1126:     fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_VIEWPREV);
 1127:     fprintf(stdout,"<input type=\"hidden\" name=\"VSET\" value=\"%d\" size=4>\n",g_vset);
 1128:   } else {
 1129:     fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_TRYSET);
 1130:   }
 1131:   fprintf(stdout,"<input type=\"submit\" value=\"Reload\" >\n</form></TD>");
 1132: #ifdef NOT_DEFINED
 1133:   /* Next Button */
 1134:   if ( (!(g_num_questions_per_page==ALL_QUESTIONS)) && 
 1135:        ((g_num_questions_per_page+g_start_question)<num_quest)) {
 1136:     fprintf(stdout,"<TD><form method=\"post\" ");
 1137:     sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
 1138:     fprintf(stdout,"%s\n", buf);
 1139:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
 1140:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
 1141:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
 1142:     fprintf(stdout,"<input type=\"hidden\" name=\"STARTNUM\" value=\"%d\">\n",g_start_question+g_num_questions_per_page);
 1143:     if (mode == VIEW_PREVIOUS_MODE ) {
 1144:       fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_VIEWPREV);
 1145:       fprintf(stdout,"<input type=\"hidden\" name=\"VSET\" value=\"%d\" size=4>\n",g_vset);
 1146:     } else {
 1147:       fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_TRYSET);
 1148:     }
 1149:     fprintf(stdout,"<input type=\"submit\" value=\"Next Page\" >\n</form></TD>");
 1150:   }
 1151: 
 1152:   /* Previous Button */
 1153:   if ( (!(g_num_questions_per_page==ALL_QUESTIONS)) && (g_start_question > 1)) {
 1154:     fprintf(stdout,"<TD><form method=\"post\" ");
 1155:     sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
 1156:     fprintf(stdout,"%s\n", buf);
 1157:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
 1158:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
 1159:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
 1160:     fprintf(stdout,"<input type=\"hidden\" name=\"STARTNUM\" value=\"%d\">\n",g_start_question-g_num_questions_per_page);
 1161:     if (mode == VIEW_PREVIOUS_MODE ) {
 1162:       fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_VIEWPREV);
 1163:       fprintf(stdout,"<input type=\"hidden\" name=\"VSET\" value=\"%d\" size=4>\n",g_vset);
 1164:     } else {
 1165:       fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_TRYSET);
 1166:     }
 1167:     fprintf(stdout,"<input type=\"submit\" value=\"Previous Page\" >\n</form></TD>");
 1168:   }
 1169: #endif
 1170:   /* Goto Button */
 1171:   if (!(g_num_questions_per_page==ALL_QUESTIONS)) {
 1172:     int idx,numquest;
 1173:     T_header header;
 1174:     fprintf(stdout,"<TD><form method=\"post\" ");
 1175:     sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
 1176:     fprintf(stdout,"%s\n", buf);
 1177:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
 1178:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
 1179:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
 1180:     fprintf(stdout,"<input type=\"submit\" value=\"Goto\" >");
 1181:     fprintf(stdout,"<b>Problem:</b><input name=\"STARTNUM\" value=\"\" size=4>\n");
 1182:     if (mode == VIEW_PREVIOUS_MODE ) {
 1183:       fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_VIEWPREV);
 1184:       fprintf(stdout,"<input type=\"hidden\" name=\"VSET\" value=\"%d\" size=4>\n",g_vset);
 1185:     } else {
 1186:       fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_TRYSET);
 1187:     }
 1188: 
 1189:     if (!capa_get_header(&header, g_login_set)) {
 1190:       numquest=atoi(header.num_questions);
 1191:       capa_mfree(header.weight);
 1192:       capa_mfree(header.partial_credit);
 1193:       for(idx=0;idx<numquest;idx++) {
 1194: 	preserve_last_answer(idx,1);
 1195:       }
 1196:     }
 1197:     fprintf(stdout,"</form></TD>");
 1198:   }
 1199: 
 1200:   /*Discuss Button*/
 1201: 
 1202:   sprintf(discussdir,"%s/discussion/%d",g_class_fullpath,g_login_set);
 1203:   if ( access(discussdir,F_OK) == 0 ) {
 1204:     fprintf(stdout,"<TD><form method=\"post\" ");
 1205:     sprintf(buf,"action=\"http://%s/%s/%s/capadiscuss\">",serverName,g_cgibin_path,g_cowner);
 1206:     fprintf(stdout,"%s\n", buf);
 1207:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
 1208:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
 1209:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
 1210:     fprintf(stdout,"<input type=\"hidden\" name=\"SETID\" value=\"%d\">\n",g_login_set);
 1211:     fprintf(stdout,"<input type=\"submit\" value=\"Discuss\" >\n</form></TD>");
 1212:   }
 1213: 
 1214:   fprintf(stdout,"\n</TR></TABLE>\n");
 1215:   fflush(stdout);    
 1216: }
 1217: 
 1218: void create_status_line(int mode,int question_cnt, T_entry* entry)
 1219: {
 1220:   char          buf[MAX_BUFFER_SIZE];
 1221:   int           idx,configResult,status_line_length;
 1222: 
 1223:   configResult=read_capa_config("web_status_line_length",buf);
 1224:   if (configResult != 0 && configResult != -1 ) {
 1225:     if (sscanf(buf,"%d",&status_line_length)==0) {
 1226:       status_line_length=question_cnt;
 1227:     }
 1228:   } else {
 1229:     status_line_length=question_cnt;
 1230:   }
 1231: 
 1232:   append_stext("<TABLE cellpadding=0 cellspacing=0 border=0><TR>");
 1233:   append_stext("<TD><b><u>Go to problem</u>  </b></TD><TD></TD>");
 1234:   for(idx=0; idx < status_line_length;idx++ ) {
 1235:     sprintf(buf,"<TD ALIGN=center VALIGN=bottom>[%d]</TD>",idx+1);
 1236:     append_stext(buf);
 1237:   }
 1238:   for(idx = 0; idx < question_cnt; idx++ ) {
 1239:     if ( !(idx%status_line_length) ) {
 1240:       sprintf(buf,"</TR><TR><TD ALIGN=left>%d-%d</TD><TD ALIGN=right>Status: </TD>",
 1241: 		   idx+1,idx+status_line_length);
 1242:       append_stext(buf);
 1243:     }
 1244:     if ((idx >= g_start_question-1) && 
 1245: 	 (((g_num_questions_per_page == ALL_QUESTIONS)) || 
 1246: 	   (idx < (g_start_question+g_num_questions_per_page-1)))) {
 1247:       sprintf(buf,"<TD ALIGN=center VALIGN=bottom><A href=\"#P%d\">",idx+1);
 1248:     } else {
 1249:       sprintf(buf,"<TD ALIGN=center VALIGN=bottom>");
 1250:     }
 1251:     append_stext(buf);
 1252:     if ( (mode == CHECK_ANSWER_MODE) && g_log_string[idx] == '-') {
 1253:       if (g_inhibit_response && (entry->answers[idx]!='-')) {
 1254: 	sprintf(buf,"A");
 1255:       } else {
 1256: 	sprintf(buf,"%c",entry->answers[idx]); 
 1257:       }
 1258:     } else {
 1259:       if (g_inhibit_response && (entry->answers[idx]!='-')) {
 1260: 	sprintf(buf,"<b>A</b>");
 1261:       } else {
 1262: 	if ( mode == CHECK_ANSWER_MODE ) {
 1263: 	  sprintf(buf,"<b>%c</b>",g_log_string[idx]);
 1264: 	} else {
 1265: 	  sprintf(buf,"<b>%c</b>",entry->answers[idx]);
 1266: 	}
 1267:       }
 1268:     }
 1269:     append_stext(buf);
 1270:     if ((idx >= g_start_question-1) && 
 1271: 	 (((g_num_questions_per_page == ALL_QUESTIONS)) || 
 1272: 	   (idx < (g_start_question+g_num_questions_per_page-1)))) {
 1273:       sprintf(buf,"</A></TD>");
 1274:     } else {
 1275:       sprintf(buf,"</TD>");
 1276:     }
 1277:     append_stext(buf);
 1278:   }
 1279:   append_stext("</TR></TABLE>\n");
 1280: }
 1281: 
 1282: /* -------------------------------- called by try set, view previous, check answer */
 1283: void
 1284: print_quizz(class_dir,c_owner,class,sn,pin,set,mode)
 1285: char *class_dir; char *c_owner;char *class;char *sn;int pin;int set;int mode;
 1286: {
 1287:   extern int    Parsemode_f;
 1288:   extern char  *StartText_p;
 1289:   extern char  *EndText_p;
 1290:   Problem_t    *first_prob, *prob_idx;
 1291:   int           result, question_idx, question_cnt, idx, view_assignment_after_due=1;
 1292:   int           q_leng, display_ans=1, configResult;
 1293:   int           view_assignments_after_due=1;
 1294:   char          buf[MAX_BUFFER_SIZE];
 1295:   char          class_fullpath[FILE_NAME_LENGTH];
 1296:   T_entry       entry;
 1297:   T_header      header;
 1298:   long          offset;
 1299:   double        a;
 1300:   char          cmp_ans[MAX_BUFFER_SIZE],date_str[DATE_BUFFER];
 1301:   time_t        curtime;
 1302:   char         *serverName;
 1303: 
 1304:   serverName=getenv("SERVER_NAME");
 1305:   if (!serverName) {
 1306:     fprintf(stdout,"Enviroment variable SERVER_NAME not set.\n");
 1307:     fprintf(stdout,"Unable to complete actions.\n");
 1308:     return;
 1309:   }
 1310: 
 1311:   sprintf(class_fullpath,"%s/%s",class_dir,class);
 1312:   
 1313:   /*
 1314:   chdir(class_fullpath);
 1315:   */
 1316: #ifdef CGI_DBUG
 1317:   fprintf(g_cgi,"enter print_quizz() %s, mode:%d\n",class_fullpath,mode); fflush(g_cgi);
 1318: #endif /* CGI_DBUG */ 
 1319: 
 1320:   /* get configuration options */
 1321:   configResult=read_capa_config("num_questions_per_page",buf);
 1322:   if (configResult != 0 && configResult != -1 ) {
 1323:     if (sscanf(buf,"%d",&g_num_questions_per_page)==0) {
 1324:       g_num_questions_per_page=ALL_QUESTIONS;
 1325:     }
 1326:   } else {
 1327:     g_num_questions_per_page=ALL_QUESTIONS;
 1328:   }
 1329: 
 1330:   view_assignments_after_due=capa_check_option(OPTION_VIEW_PROBLEMS_AFTER_DUE,
 1331: 					       set,g_student_data.s_sec);
 1332:   if (view_assignments_after_due < 0 ) view_assignments_after_due=1;
 1333:   g_inhibit_response=capa_check_option(OPTION_INHIBIT_RESPONSE,set,g_student_data.s_sec);
 1334:   if (g_inhibit_response < 0 ) g_inhibit_response=0;
 1335: 
 1336: #ifdef CGI_DBUG
 1337:   fprintf(g_cgi,"Set %d, Section%d, ViewAssign? %d, Inhibit Resp? %d\n",set,
 1338: 	  g_student_data.s_sec,view_assignments_after_due,
 1339: 	  g_inhibit_response); 
 1340:   fflush(g_cgi);
 1341: #endif /* CGI_DBUG */ 
 1342: 
 1343:   time(&curtime);
 1344:   offset=capa_get_entry(&entry,sn,set);          /* <-------- capa*() call  ---- */
 1345:   if( mode == VIEW_PREVIOUS_MODE ) {
 1346:     if( view_assignment_after_due ) {
 1347:       if( capa_check_date(CHECK_OPEN_DATE,g_student_number,
 1348: 			  g_student_data.s_sec,set) < 0 ) {
 1349: 	append_qtext("This set is not yet open.\n");
 1350: 	return ;
 1351:       }
 1352:     } else {
 1353:       if( (capa_check_date(CHECK_ANS_DATE,g_student_number,
 1354: 			   g_student_data.s_sec,set) < 0) &&
 1355: 	  (capa_check_date(CHECK_DUE_DATE,g_student_number,
 1356: 			   g_student_data.s_sec,set) > 0) ) {
 1357: 	append_qtext("This set is not yet available to be viewed.\n");
 1358: 	return ;
 1359:       }
 1360:     }
 1361:     if( capa_check_date(CHECK_ANS_DATE,g_student_number,
 1362: 			g_student_data.s_sec,set) < 0 ) {
 1363:       display_ans = 0;
 1364:     }
 1365:   } 
 1366:   g_passdue = 0;
 1367:   if( mode == CHECK_ANSWER_MODE || 
 1368:       ( (!view_assignment_after_due) && mode == TRY_SET_MODE)) {
 1369:     if( capa_check_date(CHECK_DUE_DATE,g_student_number,
 1370: 			g_student_data.s_sec,set) > 0 ) {
 1371:        capa_get_date(CHECK_DUE_DATE,g_student_number,
 1372: 		     g_student_data.s_sec,set,date_str);
 1373:        sprintf(buf,"SORRY, the due date was: %s\n",date_str);
 1374:        append_qtext(buf);
 1375:        g_passdue = 1;
 1376:     }
 1377:   }
 1378: 
 1379:   if ((mode==CHECK_ANSWER_MODE) || (mode== TRY_SET_MODE))
 1380:     capa_set_login_time(g_student_number,set);
 1381: 
 1382:   capa_get_header(&header,set);
 1383: 
 1384:   sscanf(header.num_questions,"%d",&question_cnt);
 1385:   print_page_header(mode,question_cnt);
 1386: 
 1387: #ifdef CGI_DBUG
 1388:   fprintf(g_cgi,"DONE page header\n"); fflush(g_cgi);
 1389: #endif /* CGI_DBUG */
 1390: 
 1391:   if(offset < 0 ) offset = - offset;
 1392:   
 1393:   Parsemode_f = HTML_MODE; /* WEB_MODE */
 1394:   result = capa_parse(set, &first_prob, sn, &question_cnt,NULL);  /* <-- capa*() call */
 1395: 
 1396: #ifdef CGI_DBUG
 1397:   fprintf(g_cgi,"DONE capa_parse() [%d], pass due=%d\n",result,g_passdue); fflush(g_cgi);
 1398: #endif /* CGI_DBUG */
 1399: 
 1400:   if (StartText_p) printf(StartText_p);
 1401:  
 1402: #ifdef CGI_DBUG
 1403:   fprintf(g_cgi,"DONE Start Text\n"); fflush(g_cgi);
 1404: #endif /* CGI_DBUG */
 1405: 
 1406:   if ( result != 0 ) {
 1407:      if( !g_passdue ) {
 1408:        append_qtext("<FORM method=\"post\" ");
 1409:        sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,
 1410: 	       g_cgibin_path,c_owner);
 1411:        append_qtext(buf);
 1412:        sprintf(buf,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",class); append_qtext(buf);
 1413:        sprintf(buf,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",sn);          append_qtext(buf);
 1414:        sprintf(buf,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",pin);       append_qtext(buf);
 1415:        sprintf(buf,"<input type=\"hidden\" name=\"SET\" value=\"%d\">\n",set);          append_qtext(buf);
 1416:        sprintf(buf,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_CHECKANS); append_qtext(buf);
 1417:        sprintf(buf,"<input type=\"hidden\" name=\"STARTNUM\" value=\"%d\">\n",g_start_question); append_qtext(buf);
 1418:        append_qtext("\n<OL>\n");
 1419:      }
 1420: 
 1421:      for(idx=0;idx<question_cnt;idx++) {         /* prepare log string and new database entry */
 1422:        g_new_answerdb[idx] = entry.answers[idx];
 1423:        g_log_string[idx]   = '-';
 1424:        sscanf(entry.tries + 3*idx,"%d,",&(g_tried[idx]) );
 1425:      }
 1426:      g_new_answerdb[question_cnt]=0;  g_log_string[question_cnt]=0;
 1427:      prob_idx = first_prob;
 1428:      for( question_idx = 0; question_idx < question_cnt; 
 1429: 	  question_idx++,prob_idx = prob_idx->next ) {
 1430: #ifdef CGI_DBUG
 1431:   fprintf(g_cgi,"quetion_idx: %d, g_start_question:%d, g_num_que: %d\n",
 1432: 	  question_idx,g_start_question,g_num_questions_per_page); fflush(g_cgi);
 1433: #endif /* CGI_DBUG */        
 1434:          if ((question_idx < g_start_question-1) 
 1435: 	     ||
 1436: 	     (((!(g_num_questions_per_page == ALL_QUESTIONS)) 
 1437: 	       && 
 1438: 	       (question_idx >= (g_start_question+g_num_questions_per_page-1))))) {
 1439: 	   preserve_last_answer(question_idx,0);
 1440: 	   continue;
 1441: 	 }
 1442:          if( !g_passdue ) {
 1443:            sprintf(buf,"<A NAME=\"P%d\"></A>",question_idx+1);  append_qtext(buf);
 1444: 	   /*	   if (!((question_idx == (g_start_question-1)) || (question_idx == 0))) {
 1445: 	     append_qtext("<A href=\"#TOP\">Top</A>");
 1446: 	     sprintf(buf,"&nbsp;&nbsp;<A href=\"#P%d\">Next</A>",question_idx+2);  append_qtext(buf);
 1447: 	     }*/
 1448: 	   if (prob_idx->question != NULL) {
 1449: 	     q_leng = strlen(prob_idx->question);
 1450: 	     if ( !prob_idx->show_br ) {
 1451: 	       append_qtext(prob_idx->question);
 1452: 	     } else {
 1453: 	       for(idx=0;idx<q_leng;idx++) {
 1454: 		 if ( g_qchar_cnt+2 >= g_qsize ) {
 1455: 		   char *temp_text;
 1456: 		   g_qsize=g_qchar_cnt*2;
 1457: 		   temp_text=capa_malloc(g_qsize,sizeof(char));
 1458: 		   strncpy(temp_text,g_question_txt,g_qsize);
 1459: 		   capa_mfree(g_question_txt);
 1460: 		   g_question_txt=temp_text;		 
 1461: 		 }
 1462: 		 g_question_txt[g_qchar_cnt]=prob_idx->question[idx];
 1463: 		 g_qchar_cnt++;
 1464: 		 if(prob_idx->question[idx] == '\n' ) {
 1465: 		   append_qtext("<br>\n");
 1466: 		 }
 1467: 	       }
 1468: 	     }
 1469: 	   }
 1470:          }
 1471:          if(mode == VIEW_PREVIOUS_MODE) { /* VIEW_PREVIOUS_MODE */
 1472:            if( display_ans ) {
 1473:              if( prob_idx->ans_type == ANSWER_IS_FLOAT ) {
 1474:                  a = (double)atof(prob_idx->answer);
 1475:                  sprintf(cmp_ans,prob_idx->ans_fmt, a);
 1476:              } else {
 1477:                  strcpy(cmp_ans,prob_idx->answer);
 1478:              }
 1479:              if( prob_idx->ans_unit ) {
 1480:                  sprintf(buf,"<p><tt><b>Answer:</b> %s %s</tt><br>\n",cmp_ans, prob_idx->unit_str); 
 1481:              } else {
 1482:                  sprintf(buf,"<p><tt><b>Answer:</b> %s</tt><br>\n",cmp_ans); 
 1483:              }
 1484:              append_qtext(buf);
 1485: 	     if ( prob_idx->explain) {
 1486: 	       sprintf(buf,"<p><b>Explanation: </b>\n<p>%s<br>\n",prob_idx->explain);
 1487: 	       append_qtext(buf);
 1488: 	     }
 1489:            }
 1490:          } else { /* could be TRY_SET_MODE, CHECK_ANSWER_MODE */
 1491:            
 1492:            if( g_passdue ) {
 1493:              get_response(header.partial_credit[question_idx],entry.answers[question_idx],question_idx,prob_idx);
 1494:            }else{
 1495: 	     if (g_inhibit_response) {
 1496: 	       print_inhibited_response(header.partial_credit[question_idx],entry.answers[question_idx],question_idx,prob_idx);
 1497: 	     } else {
 1498: 	       print_response(header.partial_credit[question_idx],entry.answers[question_idx],question_idx,prob_idx); 
 1499: 	     }
 1500:              append_qtext("<br>\n");
 1501:              if( (g_tried[question_idx] >= prob_idx->show_hint) || 
 1502: 		 (entry.answers[question_idx]=='Y') ||
 1503: 		 (entry.answers[question_idx]=='y')) {
 1504:                if( prob_idx->hint ) {
 1505:                  sprintf(buf,"<p><B>Hint: </B>%s\n<br>\n", prob_idx->hint);
 1506:                  append_qtext(buf);
 1507:                }
 1508:              }
 1509:            }
 1510:          }
 1511:      }   /* ------------------------------------- end displaying each problem */
 1512:      append_qtext("\n</OL>\n");
 1513:      if( EndText_p )   append_qtext(EndText_p);
 1514:      free_problems(first_prob);
 1515: 
 1516: #ifdef CGI_DBUG
 1517:   fprintf(g_cgi,"End display each problem\n"); fflush(g_cgi);
 1518: #endif /* CGI_DBUG */
 1519: 
 1520:      if( mode == CHECK_ANSWER_MODE ) {  /* update the record database */
 1521:        if( !g_passdue ) {
 1522:          for(idx=0;idx<question_cnt;idx++){
 1523:            if( g_new_answerdb[idx] != entry.answers[idx]) { 
 1524:              entry.answers[idx] = g_new_answerdb[idx];
 1525:            }
 1526:            if(g_tried[idx] < 10 ) {
 1527:              entry.tries[3*idx]   = ' ';
 1528:              entry.tries[3*idx+1] = g_tried[idx] + '0';
 1529:              if(idx < question_cnt-1)  entry.tries[3*idx+2] = ',';
 1530:            } else {
 1531:              entry.tries[3*idx]   = (int)(g_tried[idx]/10) + '0';
 1532:              entry.tries[3*idx+1] = (g_tried[idx] % 10) + '0';
 1533:              if(idx < question_cnt-1)  entry.tries[3*idx+2] = ',';
 1534:            }
 1535:          }
 1536:          capa_set_entry(&entry,sn,set,offset);                     /* <-------- capa*() call */
 1537: 
 1538: #ifdef CGI_DBUG
 1539: 	 fprintf(g_cgi,"DONE set db entry\n"); fflush(g_cgi);
 1540: #endif /* CGI_DBUG */
 1541: 
 1542: 	 create_status_line(mode,question_cnt,&entry);
 1543:        } 
 1544:        if (w_log_attempt(g_student_number,set,g_log_string) == -1 ) {
 1545: 	 fprintf(stdout,"<BOLD>Unable to log attempt. Please notify instructor.</BOLD>\n");
 1546:        }
 1547:      }
 1548: #ifdef CGI_DBUG
 1549:      fprintf(g_cgi,"DONE check answer mode\n"); fflush(g_cgi);
 1550: #endif /* CGI_DBUG */
 1551:      
 1552:      if( (mode == TRY_SET_MODE && !g_passdue)  || 
 1553: 	 mode == VIEW_PREVIOUS_MODE) {
 1554:        create_status_line(mode,question_cnt,&entry);
 1555:      }
 1556:      
 1557:      if( !g_passdue ) {
 1558:        sprintf(buf,"</ul></form>\n"); append_qtext(buf);
 1559:      }
 1560:   }
 1561: }
 1562: 
 1563: void
 1564: get_response(char pcr,char u_db,int q_idx,Problem_t *p)
 1565: {
 1566:   if( pcr == '0' || p->ans_type==ANSWER_IS_SUBJECTIVE ) { /* not hand-graded question */
 1567:      switch(u_db) {  /* what's from the user record */
 1568:        case 'Y': break;
 1569:        case 'y': break;
 1570:        case '-':
 1571:                if(g_stu_ans_pp[q_idx+1] != NULL ) log_user_ans(q_idx,p);
 1572:                break;
 1573:        case 'E':
 1574:        case 'e': break;
 1575:        case 'n': break;
 1576:        case 'N':
 1577:        case '0': case '1': case '2': case '3': case '4': 
 1578:        case '5': case '6': case '7': case '8': case '9':
 1579:                if(g_stu_ans_pp[q_idx+1] != NULL ) log_user_ans(q_idx,p);
 1580:                break;
 1581:        default:
 1582: 	      break;
 1583:      }
 1584:    }
 1585: }
 1586: 
 1587: void display_last_answer(int q_idx)
 1588: {
 1589:   char       buf[MAX_BUFFER_SIZE];  
 1590:   StudentAnswer_t  *sa_p;
 1591: #ifdef CGI_DBUG
 1592:   fprintf(g_cgi,"Enter display_last_answer() [%d]\n",q_idx); fflush(g_cgi);
 1593: #endif /* CGI_DBUG */
 1594:   if(g_stu_ans_pp[q_idx+1] != NULL) {
 1595:     sa_p=g_stu_ans_pp[q_idx+1];
 1596:   } else {
 1597:     if (g_last_ans_pp[q_idx+1] != NULL) {
 1598:       sa_p=g_last_ans_pp[q_idx+1];
 1599:     } else {
 1600:       return;
 1601:     }
 1602:   }
 1603:   if (sa_p->a_next == NULL) {
 1604:     sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d\" value=\"%s\">\n",
 1605: 	    q_idx+1,sa_p->a_str);
 1606:     append_qtext(buf);
 1607:     sprintf(buf," <b>Last Answer:</b> %s\n",sa_p->a_str);
 1608:     append_qtext(buf);
 1609:   } else {
 1610:     while(sa_p) {
 1611:       sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d,%02d\" value=\"%s\">\n",
 1612: 	      q_idx+1,sa_p->a_idx,sa_p->a_str);
 1613:       append_qtext(buf);
 1614:       sprintf(buf," <b>Last Answer %d:</b> %s\n",sa_p->a_idx,sa_p->a_str);
 1615:       append_qtext(buf);
 1616:       sa_p=sa_p->a_next;
 1617:     }
 1618:   }
 1619: }
 1620: 
 1621: void preserve_last_answer(int q_idx,int print)
 1622: {
 1623:   char       buf[MAX_BUFFER_SIZE];  
 1624:   StudentAnswer_t  *sa_p;
 1625: #ifdef CGI_DBUG
 1626:   fprintf(g_cgi,"Enter preserve_last_answer() [%d]\n",q_idx); fflush(g_cgi);
 1627: #endif /* CGI_DBUG */
 1628:   if(g_stu_ans_pp[q_idx+1] != NULL) {
 1629:     sa_p=g_stu_ans_pp[q_idx+1];
 1630:   } else {
 1631:     if (g_last_ans_pp[q_idx+1] != NULL) {
 1632:       sa_p=g_last_ans_pp[q_idx+1];
 1633:     } else {
 1634:       return;
 1635:     }
 1636:   }
 1637:   if (sa_p->a_next == NULL) {
 1638:     sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d\" value=\"%s\">\n",
 1639: 	    q_idx+1,sa_p->a_str);
 1640:     if (print) printf(buf); else append_qtext(buf);
 1641:   } else {
 1642:     while(sa_p) {
 1643:       sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d,%02d\" value=\"%s\">\n",
 1644: 	      q_idx+1,sa_p->a_idx,sa_p->a_str);
 1645:       if (print) printf(buf); else append_qtext(buf);
 1646:       sa_p=sa_p->a_next;
 1647:     }
 1648:   }
 1649: }
 1650: 
 1651: void display_last_subjective(int q_idx)
 1652: {
 1653:   char      *buf;
 1654:   char      *answer;
 1655: #ifdef CGI_DBUG
 1656:   fprintf(g_cgi,"Enter display_last_subjective() [%d]\n",q_idx); fflush(g_cgi);
 1657: #endif /* CGI_DBUG */
 1658:   answer=capa_get_subjective(g_login_set,q_idx+1,g_student_number);
 1659:   if (answer==NULL) return;
 1660: #ifdef CGI_DBUG
 1661:   fprintf(g_cgi,"Found answer %s\n",answer); fflush(g_cgi);
 1662: #endif /* CGI_DBUG */
 1663:   buf=capa_malloc(MAX_BUFFER_SIZE+strlen(answer),1);
 1664:   /* don't need to stick in a last since we always get it from the files */
 1665:   /*  sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d\" value=\"%s\">\n",q_idx+1,answer);*/
 1666:   append_qtext(buf);
 1667:   append_qtext("<b>Current Submission:</b><br>\n");
 1668:   append_qtext("<TABLE BORDER=1 CELLSPACING=0>\n<TR><TD>\n");
 1669:   append_qtext("<PRE>");
 1670:   append_qtext(answer);
 1671:   append_qtext("</PRE>");
 1672:   append_qtext("</TD></TR></TABLE>\n");
 1673:   capa_mfree(buf);
 1674:   capa_mfree(answer);
 1675: }
 1676: 
 1677: void create_answer_area(Problem_t *p,int q_idx) 
 1678: {
 1679:   int ii=0;
 1680:   char       buf[MAX_BUFFER_SIZE];  
 1681:   AnswerInfo_t *ai;
 1682: #ifdef CGI_DBUG
 1683:   fprintf(g_cgi,"Enter create_answer_area() [%d]\n",q_idx); fflush(g_cgi);
 1684: #endif /* CGI_DBUG */
 1685: 
 1686:   if ( p->ans_type==ANSWER_IS_SUBJECTIVE ) {
 1687:     display_last_subjective(q_idx);
 1688:   }
 1689:   if ( p->show_ans_box ) { 
 1690:     if ( p->ans_op == ANS_AND ) {
 1691:       if (p->ans_type == ANSWER_IS_FORMULA) {
 1692: 	/* first answer is stored in p, the rest are linked off of p->ans_list */
 1693: 	sprintf(buf,"<p><B>Answer %d of %d:</B><input size=80 name=\"INPUT%02d,%02d\" value=\"\">\n",ii+1,p->ans_cnt,q_idx+1,ii+1);
 1694:       } else {
 1695: 	sprintf(buf,"<p><B>Answer %d of %d:</B><input name=\"INPUT%02d,%02d\" value=\"\">\n",ii+1,p->ans_cnt,q_idx+1,ii+1);
 1696:       }
 1697:       append_qtext(buf);
 1698:       for(ii=1, ai=p->ans_list;ii<p->ans_cnt;ai=ai->ans_next,ii++) {
 1699: 	if (ai->ans_type == ANSWER_IS_FORMULA) {
 1700: 	  sprintf(buf,"<p><B>Answer %d of %d:</B><input size=80 name=\"INPUT%02d,%02d\" value=\"\">\n",ii+1,p->ans_cnt,q_idx+1,ii+1);
 1701: 	} else {
 1702: 	  sprintf(buf,"<p><B>Answer %d of %d:</B><input name=\"INPUT%02d,%02d\" value=\"\">\n",ii+1,p->ans_cnt,q_idx+1,ii+1);
 1703: 	}
 1704: 	append_qtext(buf);
 1705:       }
 1706:     } else { /* single answer, or /OR answers, or subjective answer */
 1707:       if (p->ans_type == ANSWER_IS_SUBJECTIVE ) {
 1708: 	sprintf(buf,"<p><B>Answer:</B><br><TEXTAREA name=\"INPUT%02d\" rows=\"15\" cols=\"80\"></TEXTAREA>\n",q_idx+1);
 1709:       } else {
 1710: 	if (p->ans_type == ANSWER_IS_FORMULA) {
 1711: 	  sprintf(buf,"<p><B>Answer:</B><input size=80 name=\"INPUT%02d\" value=\"\">\n",q_idx+1);
 1712: 	} else {
 1713: 	  sprintf(buf,"<p><B>Answer:</B><input name=\"INPUT%02d\" value=\"\">\n",q_idx+1);
 1714: 	}
 1715:       }
 1716:       append_qtext(buf);
 1717:     }
 1718:   }
 1719:   append_qtext("<input type=\"submit\" value=\"Submit All Answers\" >\n");
 1720:   if ( p->ans_type!=ANSWER_IS_SUBJECTIVE ) {
 1721:     display_last_answer(q_idx);
 1722:   }
 1723: }
 1724: 
 1725: void
 1726: print_response(char pcr,char u_db,int q_idx,Problem_t *p)
 1727: {
 1728: int   a_tpe;
 1729: char *c_ans,*response,*answered="Answered",*nycorrect="Not yet correct";
 1730: int   t_tpe;
 1731: double tol;
 1732: int    sig_l;
 1733: int    sig_u;
 1734: char  *a_fmt, *c_answer_str;
 1735: int    tries;
 1736: 
 1737:     char       buf[MAX_BUFFER_SIZE];
 1738: 
 1739:  a_tpe = p->ans_type;
 1740:  c_ans = p->answer;
 1741:  t_tpe = p->tol_type;
 1742:  tol   = p->tolerance;
 1743:  sig_l = p->sig_lbound;
 1744:  sig_u = p->sig_ubound;
 1745:  a_fmt = p->ans_fmt;
 1746:  tries = p->tries;
 1747:  response=nycorrect;
 1748: 
 1749: #ifdef CGI_DBUG
 1750:   fprintf(g_cgi,"Enter print_response() [%c]\n",u_db); fflush(g_cgi);
 1751: #endif /* CGI_DBUG */
 1752:   if( pcr == '0' || a_tpe==ANSWER_IS_SUBJECTIVE ) { /* not hand-graded question */
 1753:      switch(u_db) {  /* this is from the user record */
 1754:        case 'Y':
 1755:                c_answer_str = answers_string(ANSWER_STRING_MODE, p);
 1756:                sprintf(buf,"<p><tt>Correct, computer gets: %s</tt>\n", c_answer_str);
 1757:                append_qtext(buf);
 1758:                capa_mfree((char *)c_answer_str);
 1759:                break;
 1760:        case 'y':
 1761:                append_qtext("<p><tt>Hand-graded Correct</tt>\n");
 1762:                break;
 1763:        case '-':
 1764:                if(g_stu_ans_pp[q_idx+1] == NULL ) {
 1765: 		 create_answer_area(p,q_idx);
 1766: 	       } else {
 1767:                  check_user_ans(q_idx,p);
 1768:                }
 1769:                break;
 1770:        case 'E':
 1771:        case 'e': append_qtext("<p><tt>Excused</tt>\n");               break;
 1772:        case 'n': append_qtext("<p><tt>Hand-graded Incorrect</tt>\n"); break;
 1773:        case '0': case '1': case '2': case '3': case '4': 
 1774:        case '5': case '6': case '7': case '8': case '9':
 1775: 	 response=answered;
 1776:        case 'N':
 1777:                if(g_stu_ans_pp[q_idx+1] == NULL ) {
 1778: 		 if( g_tried[q_idx] < tries) {
 1779: 		   create_answer_area(p,q_idx);
 1780: 		   if( tries - g_tried[q_idx] == 1) {
 1781: 		     sprintf(buf,"<br><tt>%s, ONE try left!!</tt>\n",response);
 1782: 		   }else{
 1783: 		     sprintf(buf,"<br><tt>%s, tries %d/%d</tt>\n",response,
 1784: 			     g_tried[q_idx],tries);
 1785: 		   }  
 1786: 		   append_qtext(buf);
 1787: 		 }else{
 1788: 		   if (p->ans_type==ANSWER_IS_SUBJECTIVE)
 1789: 		     display_last_answer(q_idx);
 1790: 		   else
 1791: 		     display_last_subjective(q_idx);
 1792: 		   append_qtext("<br><tt>No more tries.</tt>\n");
 1793: 		 } 
 1794:                } else { /* answering this question */
 1795: 		 if( g_tried[q_idx] < tries) {
 1796: 		   check_user_ans(q_idx,p);
 1797: 		 } else {
 1798: 		   if (p->ans_type==ANSWER_IS_SUBJECTIVE)
 1799: 		     display_last_answer(q_idx);
 1800: 		   else
 1801: 		     display_last_subjective(q_idx);
 1802: 		   append_qtext("<br><tt>No more tries.</tt>\n");
 1803: 		 }
 1804: 	       }
 1805:                break;
 1806:      }
 1807:    } else {
 1808:      append_qtext("<p><tt>Question to be Graded Manually.</tt>\n");
 1809:      
 1810:    }
 1811: 
 1812: }
 1813: 
 1814: void
 1815: print_inhibited_response(char pcr,char u_db,int q_idx,Problem_t *p)
 1816: {
 1817: int   a_tpe;
 1818: char *c_ans;
 1819: int   t_tpe;
 1820: double tol;
 1821: int    sig_l;
 1822: int    sig_u;
 1823: char  *a_fmt;
 1824: int    tries;
 1825: char       buf[MAX_BUFFER_SIZE];
 1826: 
 1827:  a_tpe = p->ans_type;
 1828:  c_ans = p->answer;
 1829:  t_tpe = p->tol_type;
 1830:  tol   = p->tolerance;
 1831:  sig_l = p->sig_lbound;
 1832:  sig_u = p->sig_ubound;
 1833:  a_fmt = p->ans_fmt;
 1834:  tries = p->tries;
 1835: 
 1836: #ifdef CGI_DBUG
 1837:   fprintf(g_cgi,"Enter print_inhibited_response() [%c]\n",u_db); fflush(g_cgi);
 1838: #endif /* CGI_DBUG */
 1839: 
 1840:   if( pcr == '0' || a_tpe==ANSWER_IS_SUBJECTIVE ) { /* not hand-graded question */
 1841:      switch(u_db) {  /* this is from the user record */
 1842:        case '-': 
 1843:                if(g_stu_ans_pp[q_idx+1] == NULL ) {
 1844: 		 create_answer_area(p,q_idx);
 1845:                } else {
 1846:                  check_inhibited_user_ans(q_idx,p);
 1847:                }
 1848:                break;
 1849:        case 'Y': case 'y':
 1850:        case 'E': case 'e': 
 1851:        case 'n': case 'N': 
 1852:        case '0': case '1': case '2': case '3': case '4': 
 1853:        case '5': case '6': case '7': case '8': case '9':
 1854:                if(g_stu_ans_pp[q_idx+1] == NULL ) {
 1855: 		 if( g_tried[q_idx] < tries) {
 1856: 		   create_answer_area(p,q_idx);	
 1857: 		   if( tries - g_tried[q_idx] == 1) {
 1858: 		     append_qtext("<br><tt>Answered, ONE try left!!</tt>\n");
 1859: 		   }else{
 1860: 		     sprintf(buf,"<br><tt>Answered, tries %d/%d</tt>\n",g_tried[q_idx],tries);
 1861: 		     append_qtext(buf);
 1862: 		   }  
 1863: 		 }else{
 1864: 		   if (p->ans_type==ANSWER_IS_SUBJECTIVE)
 1865: 		     display_last_answer(q_idx);
 1866: 		   else
 1867: 		     display_last_subjective(q_idx);
 1868: 		   append_qtext("<br><tt>Answered,No more tries.</tt>\n");
 1869: 		 } 
 1870: 	       } else { /* answering this question */
 1871: 		 if( g_tried[q_idx] < tries) {
 1872: 		   check_inhibited_user_ans(q_idx,p);
 1873: 		 } else {
 1874: 		   if (p->ans_type==ANSWER_IS_SUBJECTIVE)
 1875: 		     display_last_answer(q_idx);
 1876: 		   else
 1877: 		     display_last_subjective(q_idx);
 1878: 		   append_qtext("<br><tt>Answered, No more tries.</tt>\n");
 1879: 		 }
 1880:                }
 1881:                break;
 1882:      }
 1883:    } else {
 1884:      append_qtext("<p><tt>Question to be Graded Manually.</tt>\n"); 
 1885:    }
 1886: }
 1887: 
 1888: /* returns a -1 if there were not enough answers, otherwise the number of responses
 1889:    for the question is returned
 1890:    !!!!!AS A SIDEEFFECT IT ALSO CROPS ANSWERS TO ANSWER_STRING_LENG!!!!!!!
 1891: */
 1892: int gather_answers(char ***ans,int q_idx,Problem_t *p)
 1893: {
 1894:   int cnt;
 1895:   if(p->ans_op==ANS_AND) {
 1896:     int i; StudentAnswer_t *sa_p;
 1897:     *ans=(char**)capa_malloc(p->ans_cnt,1);
 1898:     sa_p= g_stu_ans_pp[q_idx+1];
 1899:     for(i=0;((i<p->ans_cnt)&&(sa_p));i++){
 1900:       ans[0][i]=sa_p->a_str;
 1901:       if ((strlen(ans[0][i])+1) > ANSWER_STRING_LENG) ans[0][i][ANSWER_STRING_LENG]='\0';
 1902:       sa_p=sa_p->a_next;
 1903:     }
 1904:     cnt=p->ans_cnt;
 1905:     if (i<p->ans_cnt) return -1;
 1906:   } else {
 1907:     *ans=(char**)capa_malloc(p->ans_cnt,1);
 1908:     ans[0][0]=g_stu_ans_pp[q_idx+1]->a_str;
 1909:     if ((strlen(ans[0][0])+1) > ANSWER_STRING_LENG) ans[0][0][ANSWER_STRING_LENG]='\0';
 1910:     cnt=1;
 1911:   }
 1912:   return cnt;
 1913: }
 1914: 
 1915: void
 1916: log_user_ans(int q_idx,Problem_t *p)
 1917: {
 1918:   char **ans;
 1919:   int cnt;
 1920:   if (p->ans_type==ANSWER_IS_SUBJECTIVE) {
 1921:     capa_set_subjective(g_login_set,q_idx+1,g_student_number,
 1922: 			g_stu_ans_pp[q_idx+1]->a_str);
 1923:   } else {
 1924:     if (-1 != (cnt=gather_answers(&ans,q_idx,p))) {
 1925:       switch( capa_check_answers(p,ans,cnt) ) {
 1926:         case  EXACT_ANS:  g_log_string[q_idx]='Y'; break;
 1927:         case  APPROX_ANS: g_log_string[q_idx]='Y'; break;
 1928:         case  SIG_FAIL:   g_log_string[q_idx]='S'; break;
 1929:         case  UNIT_FAIL:  g_log_string[q_idx]='U'; break;
 1930:         case  UNIT_NOTNEEDED:  g_log_string[q_idx]='U'; break;
 1931:         case  NO_UNIT:    g_log_string[q_idx]='u'; break;
 1932:         case  BAD_FORMULA:  g_log_string[q_idx]='F'; break;
 1933:         case  INCORRECT:  g_log_string[q_idx]='N'; break;
 1934:       }
 1935:     }
 1936:   }
 1937: }
 1938: 
 1939: void submit_subjective(int q_idx,Problem_t *p)
 1940: {
 1941:   char buf[MAX_BUFFER_SIZE];
 1942:   if (capa_set_subjective(g_login_set,q_idx+1,g_student_number,
 1943: 			  g_stu_ans_pp[q_idx+1]->a_str)<0){
 1944:     sprintf(buf,"<p><tt>Falied to record last submission.</tt><br>\n");
 1945:     g_tried[q_idx]--;
 1946:   } else {
 1947:     sprintf(buf,"<p><tt>Current submission recorded.</tt><br>\n");
 1948:     g_new_answerdb[q_idx] = '0';
 1949:     g_log_string[q_idx]='A';
 1950:   }
 1951:   append_qtext(buf);
 1952:   if (g_tried[q_idx]<p->tries) {
 1953:     create_answer_area(p,q_idx);
 1954:     if( p->tries - g_tried[q_idx] == 1) {
 1955:       append_qtext("<br><tt>ONE try left</tt>\n");
 1956:     }else{
 1957:       sprintf(buf,"<br><tt>tries %d/%d</tt>\n",g_tried[q_idx],p->tries);
 1958:       append_qtext(buf);
 1959:     }  
 1960:   }else{
 1961:     display_last_answer(q_idx);
 1962:     append_qtext("<br><tt>No more tries.</tt>\n");
 1963:   } 
 1964: }
 1965: 
 1966: void
 1967: check_user_ans(int q_idx,Problem_t *p)
 1968: {
 1969: int a_tpe,cnt;
 1970: char *c_ans,**ans;
 1971: int   t_tpe;
 1972: double tol;
 1973: int    sig_l;
 1974: int    sig_u;
 1975: char  *a_fmt;
 1976: int    tries;
 1977:   char       buf[MAX_BUFFER_SIZE];
 1978:   
 1979:   a_tpe = p->ans_type;
 1980:   t_tpe = p->tol_type;
 1981:   tol   = p->tolerance;
 1982:   sig_l = p->sig_lbound;
 1983:   sig_u = p->sig_ubound;
 1984:   a_fmt = p->ans_fmt;
 1985:   tries = p->tries;
 1986: 
 1987: #ifdef CGI_DBUG
 1988:   fprintf(g_cgi,"Enter check_user_ans() anstype=%d\n",a_tpe); fflush(g_cgi);
 1989: #endif /* CGI_DBUG */
 1990: 
 1991:   g_tried[q_idx]++;
 1992: 
 1993: #ifdef CGI_DBUG
 1994:   fprintf(g_cgi,"Call capa_check_answer() with [%s]\n",g_stu_ans_pp[q_idx+1]->a_str); fflush(g_cgi);
 1995: #endif /* CGI_DBUG */
 1996:   if (a_tpe==ANSWER_IS_SUBJECTIVE) {
 1997:     submit_subjective(q_idx,p);
 1998:     return;
 1999:   }
 2000:   
 2001:   cnt=gather_answers(&ans,q_idx,p);
 2002:   if (cnt == -1) {
 2003:     g_tried[q_idx]--;
 2004:     create_answer_area(p,q_idx);
 2005:     if( (tries - g_tried[q_idx]) == 1 ) {
 2006:       append_qtext("<br><tt>Not all answers submitted, ONE try left!!</tt>\n");
 2007:     }else{
 2008:       sprintf(buf,"<br><tt>Not all answers submitted, tries %d/%d.</tt>\n",
 2009: 	      g_tried[q_idx],tries);
 2010:       append_qtext(buf);
 2011:     }
 2012:     return;
 2013:   }
 2014: 
 2015:   switch( capa_check_answers(p,ans,cnt) ) {
 2016:     case  EXACT_ANS:    
 2017:     case  APPROX_ANS: 
 2018:                    c_ans=answers_string(ANSWER_STRING_MODE, p);
 2019:                    sprintf(buf,"<p><tt>Yes, Computer gets: %s</tt>\n", c_ans);
 2020:                    append_qtext(buf);
 2021:                    g_new_answerdb[q_idx] = 'Y';
 2022:                    g_log_string[q_idx]='Y';
 2023: 		   capa_mfree(c_ans);
 2024: 		   break;
 2025:     case  SIG_FAIL:
 2026:                    create_answer_area(p,q_idx);
 2027: 		   g_tried[q_idx]--;  /* don't count as a try */
 2028: 		   sprintf(buf,"<br><tt>Please adjust significant figures, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
 2029: 		   append_qtext(buf);
 2030: 		   g_new_answerdb[q_idx] = 'N';
 2031:                    g_log_string[q_idx]='S';
 2032:                    break;
 2033:     case  UNIT_FAIL:
 2034:                    create_answer_area(p,q_idx);
 2035:                    g_tried[q_idx]--;  /* don't count as a try */
 2036: 		   sprintf(buf,"<br><tt>Units incorrect, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
 2037: 		   append_qtext(buf);
 2038: 		   g_new_answerdb[q_idx] = 'N';
 2039:                    g_log_string[q_idx]='U';
 2040:                    break;
 2041:     case  UNIT_NOTNEEDED:
 2042:                    create_answer_area(p,q_idx);
 2043:                    g_tried[q_idx]--;  /* don't count as a try */
 2044:                    if(tries > 0) {
 2045:                      sprintf(buf,"<br><tt>Only a number required, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
 2046:                      append_qtext(buf);
 2047:                    }
 2048:                    g_new_answerdb[q_idx] = 'N';
 2049:                    g_log_string[q_idx]='U';
 2050:                    break;
 2051:     case  NO_UNIT: 
 2052:                    create_answer_area(p,q_idx);
 2053:                    g_tried[q_idx]--;  /* don't count as a try */
 2054: 		   sprintf(buf,"<br><tt>Units required, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
 2055: 		   append_qtext(buf);
 2056: 		   g_new_answerdb[q_idx] = 'N';
 2057:                    g_log_string[q_idx]='u';
 2058:                    break;
 2059:     case  BAD_FORMULA: 
 2060:                    create_answer_area(p,q_idx);
 2061:                    g_tried[q_idx]--;  /* don't count as a try */
 2062: 		   sprintf(buf,"<br><tt>Unable to understand formula, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
 2063: 		   append_qtext(buf);
 2064: 		   g_new_answerdb[q_idx] = 'N';
 2065:                    g_log_string[q_idx]='F';
 2066:                    break;
 2067:     case  INCORRECT:
 2068:                    if( g_tried[q_idx] < tries ) {
 2069: 		     create_answer_area(p,q_idx);
 2070: 		     if( (tries - g_tried[q_idx]) == 1 ) {
 2071: 		       append_qtext("<br><tt>Incorrect, ONE try left!!</tt>\n");
 2072: 		     }else{
 2073: 		       sprintf(buf,"<br><tt>Incorrect, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
 2074: 		       append_qtext(buf);
 2075: 		     }
 2076: 		   } else {
 2077: 		     display_last_answer(q_idx);
 2078: 		     append_qtext("<br><tt>Incorrect, no more tries.</tt>\n");
 2079: 		   }
 2080:                    g_new_answerdb[q_idx] = 'N';
 2081:                    g_log_string[q_idx]='N';
 2082:                    break;
 2083:   }
 2084: }
 2085: 
 2086: void
 2087: check_inhibited_user_ans(int q_idx,Problem_t *p)
 2088: {
 2089: int a_tpe,cnt;
 2090: char *c_ans,**ans;
 2091: int   t_tpe;
 2092: double tol;
 2093: int    sig_l;
 2094: int    sig_u;
 2095: char  *a_fmt;
 2096: int    tries;
 2097:   char       buf[MAX_BUFFER_SIZE];
 2098:   
 2099:   a_tpe = p->ans_type;
 2100:   c_ans = p->answer;
 2101:   t_tpe = p->tol_type;
 2102:   tol   = p->tolerance;
 2103:   sig_l = p->sig_lbound;
 2104:   sig_u = p->sig_ubound;
 2105:   a_fmt = p->ans_fmt;
 2106:   tries = p->tries;
 2107:   
 2108: #ifdef CGI_DBUG
 2109:   fprintf(g_cgi,"Enter check_inhibited_user_ans() anstype=%d\n",a_tpe); fflush(g_cgi);
 2110: #endif /* CGI_DBUG */
 2111: 
 2112:   g_tried[q_idx]++;
 2113: 
 2114: #ifdef CGI_DBUG
 2115:   fprintf(g_cgi,"Call capa_check_answer() with [%s]\n",g_stu_ans_pp[q_idx+1]->a_str); 
 2116:   fflush(g_cgi);
 2117: #endif /* CGI_DBUG */
 2118:   if (a_tpe==ANSWER_IS_SUBJECTIVE) {
 2119:     submit_subjective(q_idx,p);
 2120:     return;
 2121:   }
 2122: 
 2123:   cnt=gather_answers(&ans,q_idx,p);
 2124:   if (cnt == -1) {
 2125:     g_tried[q_idx]--;
 2126:     create_answer_area(p,q_idx);
 2127:     if( (tries - g_tried[q_idx]) == 1 ) {
 2128:       append_qtext("<br><tt>Not all answers submitted, ONE try left!!</tt>\n");
 2129:     }else{
 2130:       sprintf(buf,"<br><tt>Not all answers submitted, tries %d/%d.</tt>\n",
 2131: 	      g_tried[q_idx],tries);
 2132:       append_qtext(buf);
 2133:     }
 2134:     return;
 2135:   }
 2136: 
 2137:   switch( capa_check_answers(p,ans,cnt) ) {
 2138:     case  EXACT_ANS:
 2139:     case  APPROX_ANS: 
 2140:                    g_new_answerdb[q_idx] = 'Y';
 2141:                    g_log_string[q_idx]='Y';
 2142:                    break;
 2143:     case  SIG_FAIL:
 2144:                    g_new_answerdb[q_idx] = 'N';
 2145:                    g_log_string[q_idx]='S';
 2146:                    break;
 2147:     case  UNIT_FAIL:
 2148:                    g_new_answerdb[q_idx] = 'N';
 2149:                    g_log_string[q_idx]='U';
 2150:                    break;
 2151:     case  UNIT_NOTNEEDED:
 2152:                    g_new_answerdb[q_idx] = 'N';
 2153:                    g_log_string[q_idx]='U';
 2154:                    break;
 2155:     case  NO_UNIT:
 2156:                    g_new_answerdb[q_idx] = 'N';
 2157:                    g_log_string[q_idx]='u';
 2158:                    break;
 2159:     case  BAD_FORMULA:
 2160:                    g_new_answerdb[q_idx] = 'N';
 2161:                    g_log_string[q_idx]='F';
 2162:                    break;
 2163:     case  INCORRECT:
 2164:                    g_new_answerdb[q_idx] = 'N';
 2165:                    g_log_string[q_idx]='N';
 2166:                    break;
 2167:   }
 2168: 
 2169:   if( g_tried[q_idx] < tries ) {
 2170:     create_answer_area(p,q_idx);
 2171:     if( (tries - g_tried[q_idx]) == 1 ) {
 2172:       append_qtext("<br><tt>Answered, ONE try left!!</tt>\n");
 2173:     }else{
 2174:       sprintf(buf,"<br><tt>Answered, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
 2175:       append_qtext(buf);
 2176:     }
 2177:   } else {
 2178:     display_last_answer(q_idx);
 2179:     append_qtext("<br><tt>Answered, no more tries.</tt>\n");
 2180:   }
 2181: }
 2182: 
 2183: void                                     /* RETURNS: (nothing)          */
 2184: print_summary(class_dir,class,student_number,pin,set)
 2185: char *class_dir;char *class;char *student_number;int pin;int set;
 2186: {                                       /* LOCAL VARIABLES:            */
 2187:   int      set_idx,                     /*    Set counter              */
 2188:            i,                           /*    Question counter         */
 2189:            set_score,                   /*    Score on a set           */
 2190:            term_score=0,                /*    Total points received    */
 2191:            term_valid=0,                /*    Total points possible    */
 2192:            result,
 2193:            tot_num_sets=0;
 2194:   T_entry  entry;                       /*    Database entry for a set */
 2195:   char     buf[MAX_BUFFER_SIZE]; /* Output line buffer  */
 2196:   char     buf2[MAX_BUFFER_SIZE]; /* Output line buffer  */
 2197:   T_header header;                      /*    Problem set header       */
 2198:   int      question_cnt,valid_wgt, rate,configResult,
 2199:     status_line_length=DEFAULT_STATUS_LINE_LENGTH,row;
 2200:   char     class_fullpath[ONE_K],*serverName;
 2201: 
 2202:   serverName=getenv("SERVER_NAME");
 2203:   if (!serverName) {
 2204:     fprintf(stdout,"Enviroment variable SERVER_NAME not set.\n");
 2205:     fprintf(stdout,"Unable to complete actions.\n");
 2206:     return;
 2207:   }
 2208:   printf("<!--print_summary-->");
 2209:   sprintf(class_fullpath,"%s/%s",class_dir,class);
 2210:   chdir(class_fullpath);
 2211:   configResult=read_capa_config("web_status_line_length",buf);
 2212:   if (configResult != 0 && configResult != -1 ) {
 2213:     if (sscanf(buf,"%d",&status_line_length)==0) {
 2214:       status_line_length=DEFAULT_STATUS_LINE_LENGTH;
 2215:     }
 2216:   } else {
 2217:     status_line_length=DEFAULT_STATUS_LINE_LENGTH;
 2218:   }
 2219:   
 2220:   printf("<TABLE>\n<TR><TD></TD>\n");
 2221:   for(i=0;i<status_line_length;i++) {
 2222:     printf("<TD align=center valign=bottom>%d</TD>\n",i+1);
 2223:   }
 2224:   printf("</TR>");
 2225: 
 2226:   for (set_idx=1; set_idx<=set; set_idx++) {
 2227:     g_inhibit_response=capa_check_option(OPTION_INHIBIT_RESPONSE,set_idx,
 2228: 					 g_student_data.s_sec);
 2229:     if (g_inhibit_response > 0) {
 2230:       printf("<!-- Set %d is inhibited -->\n",set_idx);
 2231:       continue;
 2232:     }
 2233:     if ( capa_check_date(CHECK_OPEN_DATE,g_student_number,
 2234: 			     g_student_data.s_sec,set_idx) < 0 ){
 2235:       printf("<!-- Set %d is not open -->\n",set_idx);
 2236:       continue;
 2237:     }
 2238: 
 2239:     if (capa_get_header(&header,set_idx))  return;
 2240:     tot_num_sets++;
 2241:     capa_get_entry(&entry,student_number,set_idx);
 2242:     sscanf(header.num_questions,"%d", &(question_cnt) );
 2243:     valid_wgt = 0; set_score = 0;
 2244:     header.weight[question_cnt] = '\0';
 2245:     header.partial_credit[question_cnt] = '\0';
 2246:     for (i=0; i<question_cnt; i++) {
 2247:       valid_wgt +=  (header.weight[i] - '0');
 2248:       if((entry.answers[i]=='Y') || (entry.answers[i]=='y'))  
 2249: 	set_score += (header.weight[i]-'0');
 2250:       if((entry.answers[i]=='E') || (entry.answers[i]=='e'))  
 2251: 	valid_wgt -= (header.weight[i] - '0');
 2252:       if((entry.answers[i]>='0') && (entry.answers[i]<='9'))  
 2253: 	set_score += (entry.answers[i] - '0');
 2254:     }
 2255:     term_valid += valid_wgt;
 2256:     term_score += set_score;
 2257: 
 2258:     if( valid_wgt != 0 ) {
 2259:       rate = 100*set_score / valid_wgt;
 2260:       printf("<TR><TD nowrap align=center valign=bottom>set <B>%d</B>, %d/%d(%d %%)  </TD>",set_idx,set_score,valid_wgt,rate);
 2261:     } else {
 2262:       printf("<TR><TD nowrap align=center valign=bottom>set <B>%d</B>,   0/0(0 %%)   </TD>",set_idx);
 2263:     }
 2264:     for(row=0;row<=(question_cnt/status_line_length);row++) {
 2265:       for(i=(row*status_line_length);
 2266: 	  ((i<question_cnt)&&(i<((row+1)*status_line_length))); i++) {
 2267: 	if (i != 0 && (!(i%status_line_length))) { printf("</TR><TD></TD>"); }
 2268: 	printf("<TD align=center valign=bottom><tt>%c</tt></TD>\n",entry.answers[i]);
 2269:       }
 2270:       printf("</TR>\n<TR><TD></TD>");
 2271:       for(i=(row*status_line_length);
 2272: 	  ((i<question_cnt)&&(i<((row+1)*status_line_length))); i++) {
 2273: 	if (i != 0 && (!(i%status_line_length))) { printf("</TR><TD></TD>"); }
 2274: 	printf("<TD align=center valign=bottom><tt>%c</tt></TD>\n",header.weight[i]);
 2275:       }
 2276:     }
 2277:     printf("</TR>");
 2278:     capa_mfree(header.weight);
 2279:     capa_mfree(header.partial_credit);
 2280:   }
 2281:   printf("\n</TABLE>\n<hr>\n");
 2282:   /* SHOW TOTALS */
 2283:   /* if capalogin_show_summary_score is set to none don't show it */
 2284:   if (term_valid > 0) {
 2285:     sprintf(buf,"%d sets, total = %3d/%3d (%d%%)\n", tot_num_sets, term_score, term_valid, 100*term_score/term_valid);
 2286:   } else {
 2287:     sprintf(buf,"%d sets, total = %3d/%3d\n", tot_num_sets, term_score, term_valid);
 2288:   }
 2289:   result=read_capa_config("capalogin_show_summary_score",buf2);
 2290:   if (result != 0 && result != -1) {
 2291:     if (strcasecmp(buf2,"none")==0) {
 2292:     } else {
 2293:       printf("%s",buf);
 2294:     }
 2295:   } else {
 2296:     printf("%s",buf);
 2297:   }
 2298: 
 2299:   printf("<TABLE cellpadding=0 cellspacing=0 border=0>\n<TR><TD>");
 2300:   printf("<form method=\"post\" ");
 2301:   sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
 2302:   printf("%s\n", buf);
 2303:   printf("<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
 2304:   printf("<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
 2305:   printf("<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
 2306:   printf("<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_CHECKIN);
 2307:   printf("<input type=\"submit\" value=\"Main menu\" ></form></TD>\n");
 2308:   printf("<TD><form method=\"get\" action=\"http://%s/CAPA/class.html\">",serverName); 
 2309:   printf("<input type=\"button\" value=\"Exit\" onclick=\"window.close()\"></form></TD>");
 2310:   printf("\n</TABLE>\n");
 2311: }
 2312: 
 2313: 
 2314: void
 2315: process_mode(int mode) {
 2316: 
 2317: #ifdef CGI_DBUG
 2318:   fprintf(g_cgi,"entered process_mode[%d]\n",mode); fflush(g_cgi);
 2319: #endif /* CGI_DBUG */
 2320:   g_qchar_cnt=g_schar_cnt=0;
 2321:   g_qsize=TEXT_BUF_SIZE*sizeof(char);
 2322:   g_ssize=STATUS_BUF_SIZE*sizeof(char);
 2323:   g_question_txt=capa_malloc(TEXT_BUF_SIZE,sizeof(char));
 2324:   g_status_txt  =capa_malloc(STATUS_BUF_SIZE,sizeof(char));
 2325: #ifdef CGI_DBUG
 2326:   fprintf(g_cgi,"alloced everything\n"); fflush(g_cgi);
 2327: #endif /* CGI_DBUG */
 2328:   if( mode == VIEW_PREVIOUS_MODE ) {
 2329:     print_quizz(g_cpath,g_cowner,g_class_name,g_student_number, g_entered_pin, g_vset,mode); 
 2330:   } else if( mode == TRY_SET_MODE ) {
 2331:     print_quizz(g_cpath,g_cowner,g_class_name,g_student_number, g_entered_pin, g_login_set,mode);
 2332:   } else {
 2333:     print_quizz(g_cpath,g_cowner,g_class_name,g_student_number,g_entered_pin,g_login_set,CHECK_ANSWER_MODE);
 2334:   }
 2335:   g_status_txt[g_schar_cnt]=0;
 2336:   g_question_txt[g_qchar_cnt]=0;
 2337:   if( g_schar_cnt != 0 ) {
 2338:          fprintf(stdout,"%s",g_status_txt);
 2339: #ifdef CGI_DBUG
 2340:   fprintf(g_cgi,"print status [%s]\n",g_status_txt); fflush(g_cgi);
 2341: #endif /* CGI_DBUG */
 2342:   }
 2343:   if( g_qchar_cnt != 0) {
 2344:          fprintf(stdout,"%s",g_question_txt);
 2345: #ifdef CGI_DBUG
 2346:   fprintf(g_cgi,"print question [%s]\n",g_question_txt); fflush(g_cgi);
 2347: #endif /* CGI_DBUG */
 2348:   }
 2349:   if( g_schar_cnt != 0 ) {
 2350:          fprintf(stdout,"%s",g_status_txt);
 2351:   }
 2352:   fflush(stdout);
 2353:   capa_mfree(g_status_txt);
 2354:   capa_mfree(g_question_txt);
 2355: 
 2356: }
 2357: 
 2358: /*  mode could be exam summary, show or not show percentage */
 2359: /*                quiz summary, show or not show percentage */
 2360: void
 2361: process_summary(int mode)
 2362: {
 2363:   int   outcome;
 2364:   int   i, len;
 2365:   char *c_name;
 2366:   char  c_path[512];
 2367:   
 2368:   outcome = check_exam_quiz_path();
 2369:   if( (mode == M_EXAMSUMM) && (outcome & 1) ) {  /* exam summary */
 2370:     c_name = rindex(g_exam_path,'/');
 2371:     c_name++;
 2372:     i = strlen(c_name);
 2373:     len = strlen(g_exam_path) - i - 1;
 2374:     for(i=0;i<len;i++) {
 2375:       c_path[i]=g_exam_path[i];
 2376:     }
 2377:     c_path[len]=0;
 2378:     print_summary(c_path,c_name,g_student_number, g_entered_pin, g_exam_set);
 2379:   }
 2380:   if( (mode == M_QUIZSUMM) && (outcome & 2) ) {  /* quiz summary */
 2381:     c_name = rindex(g_quiz_path,'/');
 2382:     c_name++;
 2383:     i = strlen(c_name);
 2384:     len = strlen(g_quiz_path) - i - 1;
 2385:     for(i=0;i<len;i++) {
 2386:       c_path[i]=g_quiz_path[i];
 2387:     }
 2388:     c_path[len]=0;
 2389:     print_summary(c_path,c_name,g_student_number, g_entered_pin, g_quiz_set);
 2390:   }
 2391:   
 2392: }
 2393: 
 2394: /* ---------------- JAVA TScore.class page print out ----------------- */
 2395: void                                     /* RETURNS: (nothing)          */
 2396: print_termscore_page(class_dir,class,student_number,pin,set,out)
 2397: char *class_dir;char *class;char *student_number;int pin;int set; /* student login set */
 2398: FILE *out;
 2399: {                                       /* LOCAL VARIABLES:            */
 2400:   int      set_idx,                     /*    Set counter              */
 2401:            i,                           /*    Question counter         */
 2402:            set_score,                   /*    Score on a set           */
 2403:            term_score=0,                /*    Total points received    */
 2404:            term_valid=0;                /*    Total points possible    */
 2405:   T_entry  entry;                       /*    Database entry for a set */
 2406:   char     buf[MAX_BUFFER_SIZE]; /* Output line buffer  */
 2407:   T_header header;                      /*    Problem set header       */
 2408:   int      question_cnt,valid_wgt,configResult;
 2409:   char     class_fullpath[ONE_K],*serverName;
 2410:   int      hw_c, hw_r, qz_c, qz_r, fs, homework_count, quiz_count;
 2411:   float    hw_w, qz_w, ex_w, fe_w, pc_w;
 2412:   int      idx, entry_count, tmp_len;
 2413:   float    *S, *F;
 2414:   int      *X; /* array controlling whether to extrapolate scores */
 2415:   char     *capa_server;
 2416:   int      max_set[4], width=600,height=750; /* width and height of applet*/
 2417:   char     **c_path_pp;
 2418: 
 2419:   /*Unused Vars  
 2420:     char     buf2[MAX_BUFFER_SIZE]; 
 2421:     char *qz_p, *ex_p, *epc_p; 
 2422:     int      ex_c, epc_c, result;
 2423:     int  rate, status_line_length=DEFAULT_STATUS_LINE_LENGTH,row;
 2424:   */
 2425: 
 2426:   serverName=getenv("SERVER_NAME");
 2427:   if (!serverName) {
 2428:     fprintf(out,"Enviroment variable SERVER_NAME not set.\n");
 2429:     fprintf(out,"Unable to complete actions.\n");
 2430:     return;
 2431:   }
 2432: 
 2433:   sprintf(class_fullpath,"%s/%s",class_dir,class);
 2434:   chdir(class_fullpath);
 2435:   
 2436:   /*
 2437:      change the working director to the major homework directory and begin to
 2438:      read off the remaining path informations from this capa.config file 
 2439:      homework_path   = 
 2440:      quiz_path   = 
 2441:      exam_path   = 
 2442:      correction_path = 
 2443:      homework_weight   = 0.3
 2444:      quiz_weight       = 0.7
 2445:      exam_weight       = 0.3
 2446:      final_weight      = 0.35
 2447:      correction_weight = 0.3
 2448:      final_exam_set_number = 4
 2449:      homework_count    = 12
 2450:      quiz_count        = 24
 2451:      
 2452:   */
 2453:   
 2454:   configResult=read_capa_config("capa_server",buf);
 2455:   if (configResult != 0 && configResult != -1 ) {
 2456:     tmp_len = strlen(buf) + 1;
 2457:     capa_server =   (char *)capa_malloc( tmp_len, sizeof(char));
 2458:     sprintf(capa_server,"%s",buf);
 2459:   } else { /* if capa_server is not set then we won't do anything further */
 2460:     fprintf(out,"Parameter: capa_server in capa.config file are not properly set.\n");
 2461:     return ;
 2462:   }
 2463:   if( get_termscore_params(&hw_w,&qz_w,&ex_w,&fe_w,&pc_w,&homework_count,&quiz_count,&fs) == -1 ) {
 2464:     fprintf(out,"Parameters in capa.config file are not properly set.\n");
 2465:     fprintf(out," such as homework_weight, quiz_weight, exam_weight, final_weight, correction_weight.\n");
 2466:     
 2467:     return;
 2468:   }
 2469: 
 2470:   get_tscore_width_height(&width,&height);
 2471: 
 2472:   c_path_pp = (char **)capa_malloc( 4, sizeof(char *));
 2473:   tmp_len = strlen(class_fullpath) + 1;
 2474:   c_path_pp[0] = (char *)capa_malloc(tmp_len,sizeof(char));
 2475:   sprintf(c_path_pp[0],"%s",class_fullpath); /* c_path_pp[0] should always be there */
 2476:   
 2477:   entry_count = fs*2 + 1;
 2478:   S = (float *)capa_malloc( ((fs+1)*2), sizeof(float));
 2479:   F = (float *)capa_malloc( ((fs+1)*2), sizeof(float));
 2480:   X =   (int *)capa_malloc( ((fs+1)*2), sizeof(int));
 2481:   
 2482:   max_set[0] = set;  /* the login set number */
 2483:   hw_c = max_set[0];
 2484:   hw_r = homework_count - set;
 2485:   
 2486:   
 2487:   configResult=read_capa_config("quiz_path",buf);
 2488:   if (configResult != 0 && configResult != -1 ) {
 2489:     tmp_len = strlen(buf)+1;
 2490:     c_path_pp[1] = (char *)capa_malloc(tmp_len,sizeof(char));
 2491:     sprintf(c_path_pp[1],"%s",buf);
 2492:     max_set[1] = check_class_get_maxset(c_path_pp[1]);
 2493:     if( max_set[1] <= 0 ) {
 2494:       /* should we continue ? */
 2495:       max_set[1] = 1;
 2496:       X[1] = 1;
 2497:     }
 2498:     qz_c = max_set[1];
 2499:     qz_r = quiz_count - max_set[1];
 2500:   } else { /* if quiz_path is not in capa.config, then we will skip quizz */
 2501:     qz_c = 0;
 2502:     qz_r = 0;
 2503:   }
 2504:   
 2505:   
 2506:   configResult=read_capa_config("exam_path",buf);
 2507:   if (configResult != 0 && configResult != -1 ) {
 2508:     tmp_len = strlen(buf)+1;
 2509:     c_path_pp[2] = (char *)capa_malloc( (tmp_len),sizeof(char));
 2510:     sprintf(c_path_pp[2],"%s",buf);
 2511:     max_set[2] = check_class_get_maxset(c_path_pp[2]);
 2512:     printf("<!-- for %s max_set %d -->\n",c_path_pp[2],max_set[2]);
 2513:     if( max_set[2] <= 0 ) {
 2514: 	/* no sets */
 2515: 	max_set[2] = 0;
 2516:     }
 2517:     /* start extrapolation with sets that don't yet exist */
 2518:     for(idx=2+(max_set[2]*2);idx <= (fs*2); idx++) {
 2519:         X[idx] = 1;
 2520:     }
 2521:   } else { /* if exam_path is not in capa.config, then skip exams */
 2522:     fs = 0;
 2523:   }
 2524:   configResult=read_capa_config("correction_path",buf);
 2525:   if (configResult != 0 && configResult != -1 ) {
 2526:     tmp_len = strlen(buf)+1;
 2527:     c_path_pp[3] = (char *)capa_malloc(tmp_len,sizeof(char));
 2528:     sprintf(c_path_pp[3],"%s",buf);
 2529:     max_set[3] = check_class_get_maxset(c_path_pp[3]);
 2530:     if( max_set[3] <= 0 ) {
 2531:       /* should we continue ? */
 2532:       max_set[3] = 0;
 2533:       
 2534:     }
 2535:   } else { /* if correction_path is not in capa.config, then skip corrections */
 2536:     pc_w = 0.0;
 2537:   }
 2538:   
 2539:   
 2540:   
 2541:   for( idx = 0; idx < 4; idx++) {
 2542:      if( c_path_pp[idx] != NULL ) {
 2543:        chdir(c_path_pp[idx]);
 2544:        term_score=0;
 2545:        term_valid=0;
 2546:        for (set_idx=1; set_idx<=max_set[idx]; set_idx++) {
 2547:           if (capa_get_header(&header,set_idx))  return;
 2548:           capa_get_entry(&entry,student_number,set_idx);
 2549:           sscanf(header.num_questions,"%d", &(question_cnt) );
 2550:           valid_wgt = 0; set_score = 0;
 2551:           header.weight[question_cnt] = '\0';
 2552:           header.partial_credit[question_cnt] = '\0';
 2553:           for (i=0; i<question_cnt; i++) {
 2554:             valid_wgt +=  (header.weight[i] - '0');
 2555:             if((entry.answers[i]=='Y') || (entry.answers[i]=='y'))  
 2556: 	       set_score += (header.weight[i]-'0');
 2557:             if((entry.answers[i]=='E') || (entry.answers[i]=='e'))  
 2558: 	       valid_wgt -= (header.weight[i] - '0');
 2559:             if((entry.answers[i]>='0') && (entry.answers[i]<='9'))  
 2560: 	       set_score += (entry.answers[i] - '0');
 2561:           }
 2562:           term_valid += valid_wgt;
 2563:           term_score += set_score;
 2564:           capa_mfree(header.weight);
 2565:           capa_mfree(header.partial_credit);
 2566: 	  printf("<!-- %s %d %d -->\n",c_path_pp[idx],set_score,valid_wgt);
 2567:           if(idx==2) { /* exam sets */
 2568:             S[set_idx*2] = (float)set_score;
 2569:             F[set_idx*2] = (float)valid_wgt;
 2570:             if (valid_wgt == 0) {
 2571: 		X[set_idx*2] = 1;
 2572: 	    } else {
 2573: 		X[set_idx*2] = 0;
 2574: 	    }
 2575:           }
 2576:           if(idx==3) { /* correction sets */
 2577:             S[set_idx*2+1] = (float)set_score;
 2578:             F[set_idx*2+1] = (float)valid_wgt;
 2579: 	    if (valid_wgt == 0 ) {
 2580: 		X[set_idx*2+1] = 1;
 2581: 	    } else {
 2582: 		X[set_idx*2+1] = 0;
 2583: 	    }
 2584:           }
 2585:        }
 2586:        if( (idx == 0) || (idx==1) ) { /* homeworks and quizzes */
 2587:          S[idx] = (float)term_score;
 2588:          F[idx] = (float)term_valid;
 2589:          X[idx] = 1;
 2590:        }
 2591:      }
 2592:   }
 2593:   
 2594:   
 2595: 
 2596:   
 2597:   fprintf(out,"<CENTER>\n");
 2598:   fprintf(out,"<APPLET CODE=TScore.class CODEBASE=\"http://%s\" width=%d height=%d>\n",capa_server,width,height);
 2599:   fprintf(out,"<PARAM NAME=\"HW_W\"  VALUE=\"%f\">\n", hw_w);
 2600:   fprintf(out,"<PARAM NAME=\"QZ_W\"  VALUE=\"%f\">\n", qz_w);
 2601:   fprintf(out,"<PARAM NAME=\"EX_W\"  VALUE=\"%f\">\n", ex_w);
 2602:   fprintf(out,"<PARAM NAME=\"FE_W\"  VALUE=\"%f\">\n", fe_w);
 2603:   fprintf(out,"<PARAM NAME=\"PC_W\"  VALUE=\"%f\">\n", pc_w);
 2604:   fprintf(out,"<PARAM NAME=\"HW_C\"  VALUE=\"%d\">\n", hw_c);
 2605:   fprintf(out,"<PARAM NAME=\"HW_R\"  VALUE=\"%d\">\n", hw_r);
 2606:   fprintf(out,"<PARAM NAME=\"FS\"    VALUE=\"%d\">\n", fs);
 2607:   fprintf(out,"<PARAM NAME=\"QZ_C\"  VALUE=\"%d\">\n", qz_c);
 2608:   fprintf(out,"<PARAM NAME=\"QZ_R\"  VALUE=\"%d\">\n", qz_r);
 2609:   
 2610: 
 2611:   for(idx=0;idx<entry_count;idx++) {
 2612:     fprintf(out,"<PARAM NAME=\"S%d\"  VALUE=\"%f\">\n",idx,S[idx]);
 2613:     fprintf(out,"<PARAM NAME=\"F%d\"  VALUE=\"%f\">\n",idx,F[idx]);
 2614:     fprintf(out,"<PARAM NAME=\"X%d\"  VALUE=\"%d\">\n",idx,X[idx]);
 2615:   }
 2616:   
 2617:   fprintf(out,"</APPLET> </CENTER>\n");
 2618:   
 2619:   fprintf(out,"<TABLE cellpadding=0 cellspacing=0 border=0>\n<TR><TD>");
 2620:   fprintf(out,"<form method=\"post\" ");
 2621:   sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
 2622:   fprintf(out,"%s\n", buf);
 2623:   fprintf(out,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
 2624:   fprintf(out,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
 2625:   fprintf(out,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
 2626:   fprintf(out,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_CHECKIN);
 2627:   fprintf(out,"<input type=\"submit\" value=\"Main menu\" ></form></TD>\n");
 2628:   fprintf(out,"<TD><form method=\"get\" action=\"http://%s/CAPA/class.html\">",serverName); 
 2629:   fprintf(out,"<input type=\"button\" value=\"Exit\" onclick=\"window.close()\"></form></TD>");
 2630:   fprintf(out,"\n</TABLE>\n");
 2631:   
 2632:   capa_mfree((char *)S);
 2633:   capa_mfree((char *)F);
 2634:   capa_mfree((char *)X);
 2635:   for(idx=0;idx<4;idx++) {
 2636:     if( c_path_pp[idx] != NULL )  capa_mfree((char *)c_path_pp[idx]);
 2637:   }
 2638:   capa_mfree((char *)c_path_pp);
 2639:   capa_mfree((char *)capa_server);
 2640: }
 2641: 
 2642: int
 2643: get_tscore_width_height(width,height) 
 2644: int *width;int *height;
 2645: {
 2646:   char     buf[MAX_BUFFER_SIZE]; 
 2647:   int      configResult;
 2648:   
 2649:   configResult=read_capa_config("tscore_width",buf);
 2650:   if (configResult != 0 && configResult != -1 ) {
 2651:     sscanf(buf,"%d", width);
 2652:     if (*width <= 0 )  { *width = DEFAULT_WIDTH; }
 2653:   } else {
 2654:       printf("<!-- tscore_width not found. %d-->\n",configResult);
 2655:   }
 2656:   configResult=read_capa_config("tscore_height",buf);
 2657:   if (configResult != 0 && configResult != -1 ) {
 2658:     sscanf(buf,"%d", height);
 2659:     if (*height <= 0 )  { *height = DEFAULT_HEIGHT; }
 2660:   } else {
 2661:       printf("<!-- tscore_height not found. %d-->\n",configResult);
 2662:   }
 2663: }
 2664: 
 2665: int
 2666: get_termscore_params(hw,qw,ew,fw,pw,hc,qc,fs) 
 2667: float *hw;float *qw;float *ew;float *fw;float *pw;
 2668: int   *hc;int   *qc;int   *fs;
 2669: {
 2670:   char     buf[MAX_BUFFER_SIZE]; /* Output line buffer  */
 2671:   int      hw_c, qz_c, fe_s;
 2672:   float    hw_w, qz_w, ex_w, fe_w, pc_w;
 2673:   int      configResult;
 2674:   
 2675:   configResult=read_capa_config("homework_weight",buf);
 2676:   if (configResult != 0 && configResult != -1 ) {
 2677:     sscanf(buf,"%f", &hw_w);
 2678:     if(hw_w <= 0.0 )  {
 2679:       hw_w = DEFAULT_HW_W;
 2680:     }
 2681:   } else {
 2682:     return (-1);
 2683:   }
 2684:   configResult=read_capa_config("quiz_weight",buf);
 2685:   if (configResult != 0 && configResult != -1 ) {
 2686:     sscanf(buf,"%f", &qz_w);
 2687:     if(qz_w <= 0.0 )  {
 2688:       qz_w = DEFAULT_QZ_W;
 2689:     }
 2690:   } else {
 2691:     return (-1);
 2692:   }
 2693:   configResult=read_capa_config("exam_weight",buf);
 2694:   if (configResult != 0 && configResult != -1 ) {
 2695:     sscanf(buf,"%f", &ex_w);
 2696:     if(ex_w <= 0.0 )  {
 2697:       ex_w = DEFAULT_EX_W;
 2698:     }
 2699:   } else {
 2700:     return (-1);
 2701:   }
 2702:   configResult=read_capa_config("final_weight",buf);
 2703:   if (configResult != 0 && configResult != -1 ) {
 2704:     sscanf(buf,"%f", &fe_w);
 2705:     if(fe_w <= 0.0 )  {
 2706:       fe_w = DEFAULT_FE_W;
 2707:     }
 2708:   } else {
 2709:     return (-1);
 2710:   }
 2711:   configResult=read_capa_config("correction_weight",buf);
 2712:   if (configResult != 0 && configResult != -1 ) {
 2713:     sscanf(buf,"%f", &pc_w);
 2714:     if(pc_w <= 0.0 )  {
 2715:       pc_w = DEFAULT_PC_W;
 2716:     }
 2717:   } else {
 2718:     return (-1);
 2719:   }
 2720:   configResult=read_capa_config("final_exam_set_number",buf);
 2721:   if (configResult != 0 && configResult != -1 ) {
 2722:     sscanf(buf,"%d", &fe_s);
 2723:     if(fe_s <= 0 )  {
 2724:       fe_s = DEFAULT_FE_NUMBER;
 2725:     }
 2726:   } else {
 2727:     return (-1);
 2728:   }
 2729:   configResult=read_capa_config("homework_count",buf);
 2730:   if (configResult != 0 && configResult != -1 ) {
 2731:     sscanf(buf,"%d", &hw_c);
 2732:     if(hw_c <= 0 )  {
 2733:       hw_c = DEFAULT_HW_COUNT;
 2734:     }
 2735:   } else {
 2736:     return (-1);
 2737:   }
 2738:   configResult=read_capa_config("quiz_count",buf);
 2739:   if (configResult != 0 && configResult != -1 ) {
 2740:     sscanf(buf,"%d", &qz_c);
 2741:     if(qz_c <= 0 )  {
 2742:       qz_c = DEFAULT_QZ_COUNT;
 2743:     }
 2744:   } else {
 2745:     return (-1);
 2746:   }
 2747:   *hw = hw_w; *qw = qz_w; *ew = ex_w; *fw = fe_w; *pw = pc_w;
 2748:   *hc = hw_c; *qc = qz_c; *fs = fe_s;
 2749:   return (0);
 2750: 
 2751: }
 2752: 
 2753: /* =================================================================================================== */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>