Annotation of capa/capa51/pProj/capaCgiUtils.c, revision 1.3

1.1       albertel    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: 	  leng = strlen(g_entries[x].val) + 1;
                    408: 	  g_stu_ans_pp[q_idx] = (StudentAnswer_t *)capa_malloc(sizeof(StudentAnswer_t),1);
                    409: 	  (g_stu_ans_pp[q_idx])->a_idx  = 1;
                    410: 	  (g_stu_ans_pp[q_idx])->a_str  = strsave(g_entries[x].val);
                    411: 	  if (leng > ANSWER_STRING_LENG) 
                    412: 	    (g_stu_ans_pp[q_idx])->a_str[ANSWER_STRING_LENG] = '\0';
                    413: 	  (g_stu_ans_pp[q_idx])->a_next = NULL;
                    414: 	  trim_response_ws((g_stu_ans_pp[q_idx])->a_str);
                    415: 	}
                    416: 	leng = strlen( g_entries[x].val );
                    417: 	if ( leng > 0 ) {
                    418: 	  sprintf(buf,"%d\t%s\t",q_idx,g_entries[x].val);
                    419: 	  strcat(submissions_str,buf);
                    420: 	}
                    421:       }
                    422:     } else { /* this answer belongs to /AND answers */
                    423:       sscanf(g_entries[x].name,"INPUT%d,%d",&q_idx,&sub_idx);
                    424:       if( q_idx > 0 && q_idx < MAX_PROBLEM_CNT ) {
                    425: 	if ( ! is_all_ws(g_entries[x].val) ) { 
                    426: 	  StudentAnswer_t *sa_p;
                    427: 	  leng = strlen(g_entries[x].val) + 1;
                    428: 	  sa_p = (StudentAnswer_t *)capa_malloc(sizeof(StudentAnswer_t),1);
                    429: 	  sa_p->a_idx  = sub_idx;
                    430: 	  sa_p->a_str  = strsave(g_entries[x].val);
                    431: 	  if (leng > ANSWER_STRING_LENG) sa_p->a_str[ANSWER_STRING_LENG] = '\0';
                    432: 	  sa_p->a_next = NULL;
                    433: 	  if( g_stu_ans_pp[q_idx] == NULL ) {
                    434: 	    g_stu_ans_pp[q_idx] = sa_p;
                    435: 	  } else { 
                    436: 	    StudentAnswer_t *sb_p;
                    437: 	    for(sb_p=g_stu_ans_pp[q_idx]; sb_p->a_next; sb_p=sb_p->a_next);
                    438: 	    sb_p->a_next = sa_p;
                    439: 	  }
                    440: 	}
                    441: 	leng = strlen( g_entries[x].val );
                    442: 	if ( leng > 0 ) {
                    443: 	  sprintf(buf,"%d\t%s\t",q_idx,g_entries[x].val);
                    444: 	  strcat(submissions_str,buf);
                    445: 	}
                    446:       }
                    447:     }
                    448:   }
                    449:   if( !strncmp(g_entries[x].name,"LAST",4) ) {  
                    450:     StudentAnswer_t *sa_p;  
                    451:     if( index(g_entries[x].name, ',' ) == NULL ) {  /* only one answer */
                    452:       sscanf(g_entries[x].name,"LAST%d",&q_idx);
                    453:       if( q_idx > 0 && q_idx < MAX_PROBLEM_CNT ) {
                    454: 	leng = strlen(g_entries[x].val) + 1;
                    455: 	sa_p = (StudentAnswer_t *)capa_malloc(sizeof(StudentAnswer_t),1);
                    456: 	sa_p->a_idx  = 1;
                    457: 	sa_p->a_str  = strsave(g_entries[x].val);
                    458: 	if (leng > ANSWER_STRING_LENG) sa_p->a_str[ANSWER_STRING_LENG] = '\0';
                    459: 	sa_p->a_next = NULL;
                    460: 	g_last_ans_pp[q_idx] = sa_p;
                    461:       }
                    462:     } else {
                    463:       sscanf(g_entries[x].name,"LAST%d,%d",&q_idx,&sub_idx);
                    464:       if( q_idx > 0 && q_idx < MAX_PROBLEM_CNT ) {
                    465: 	leng = strlen(g_entries[x].val) + 1;
                    466: 	sa_p = (StudentAnswer_t *)capa_malloc(sizeof(StudentAnswer_t),1);
                    467: 	sa_p->a_idx  = sub_idx;
                    468: 	sa_p->a_str  = strsave(g_entries[x].val);
                    469: 	if (leng > ANSWER_STRING_LENG) sa_p->a_str[ANSWER_STRING_LENG] = '\0';
                    470: 	sa_p->a_next = NULL;
                    471: 	if( g_last_ans_pp[q_idx] == NULL) {
                    472: 	  g_last_ans_pp[q_idx] = sa_p;
                    473: 	} else {
                    474: 	    StudentAnswer_t *sb_p;
                    475: 	    for(sb_p=g_last_ans_pp[q_idx]; sb_p->a_next; sb_p=sb_p->a_next);
                    476: 	    sb_p->a_next = sa_p;
                    477: 	}
                    478:       }
                    479:     }
                    480:   }
                    481: }
                    482: 
                    483: /* ========================================================================= */
                    484: /* Check in:      Class name     (CLASS)
                    485:                   Student Number (SNUM)
                    486:                   CAPA ID        (CAPAID)
                    487:                   M=1
                    488:    Try set:       Class name     (CLASS)
                    489:                   Student Number (SNUM)
                    490:                   CAPA ID        (CAPAID)
                    491: 		  First question to print (STARTNUM)
                    492:                   M=2
                    493:    View previous: Class name     (CLASS)
                    494:                   Student Number (SNUM)
                    495:                   CAPA ID        (CAPAID)
                    496: 		  First question to print (STARTNUM)
                    497:                   View set       (VSET)
                    498:                   M=3
                    499:    Set summary:   
                    500:                   
                    501:  --------------------------------------------------------------------------- */
                    502:              
                    503: int  w_get_input() 
                    504: {
                    505:   register  int   x,m=0;
                    506:   int             cl, q_idx, result = 1;
                    507:   T_header        header; 
                    508:   char            log_str[MAX_BUFFER_SIZE],buf[MAX_BUFFER_SIZE];
                    509:   char            submissions_str[MAX_BUFFER_SIZE];
                    510:   time_t          curtime;
                    511:   char           *time_str,*UNKNOWN="UNKNOWN";
                    512:   int             error=0;
                    513:   char           *envPtr=NULL,*envPtr2=NULL;
                    514: #ifdef CGI_DBUG
                    515:   fprintf(g_cgi,"Entered get_input()\n"); fflush(g_cgi);
                    516: #endif /* CGI_DBUG */
                    517: 
                    518:   envPtr=getenv("REQUEST_METHOD");
                    519:   if(!envPtr) { 
                    520:     fprintf(stdout,"Enviroment variable REQUEST_METHOD not set.\n");
                    521:     fprintf(stdout,"CAPA is improperly installed or run, please let your \
                    522: instructor know about this error.\n");
                    523:     fprintf(stdout,"No tries have been deducted for the last submission.\n");
                    524:     error |= 1;
                    525:     return error;
                    526:   }
                    527:   if(strcmp(envPtr,"POST")) {
                    528:         fprintf(stdout,"This script should be referenced with a METHOD of POST.\n");
                    529: 	fprintf(stdout,"CAPA is improperly installed or run, please let your \
                    530: instructor know about this error.\n");
                    531: 	fprintf(stdout,"No tries have been deducted for the last submission.\n");
                    532:         fflush(stdout);
                    533: #ifdef CGI_DBUG
                    534:   fprintf(g_cgi,"Request_method is not POST\n"); fflush(g_cgi);
                    535: #endif /* CGI_DBUG */        
                    536:         error |= 2; return (error);
                    537:   }
                    538:   
                    539:   envPtr=getenv("CONTENT_TYPE");
                    540:   if(!envPtr) { 
                    541:     fprintf(stdout,"Enviroment variable CONTENT_TYPE not set.\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:     error |= 4;
                    547:     return error;
                    548:   }
                    549:   if(strncmp(envPtr,"application/x-www-form-urlencoded",33)) {
                    550:         fprintf(stdout,"This script can only be used to decode form results. \n");
                    551: 	fprintf(stdout,"CAPA is improperly installed or run, please let your \
                    552: instructor know about this error.\n");
                    553: 	fprintf(stdout,"No tries have been deducted for the last submission.\n");
                    554:         fflush(stdout);
                    555: #ifdef CGI_DBUG
                    556: 	fprintf(g_cgi,"CONTENT_TYPE is not application/x-www-form-urlencoded\n"); fflush(g_cgi);
                    557: #endif /* CGI_DBUG */              
                    558:         error |= 8; return (error);
                    559:   }
                    560: 
                    561:   envPtr=getenv("CONTENT_LENGTH");
                    562:   if(!envPtr) { 
                    563:     fprintf(stdout,"Enviroment variable CONTENT_LENGTH not set.\n");
                    564:     fprintf(stdout,"CAPA is improperly installed or run, please let your \
                    565: instructor know about this error.\n");
                    566:     fprintf(stdout,"No tries have been deducted for the last submission.\n");
                    567:     error |= 16;
                    568:     return error;
                    569:   }
                    570:   cl = atoi(envPtr);
                    571: #ifdef CGI_DBUG
                    572:   fprintf(g_cgi,"CONTENT_LENGTH is %d\n",cl); fflush(g_cgi);
                    573: #endif /* CGI_DBUG */
                    574:   for(x=0;cl && (!feof(stdin));x++) {
                    575:     m=x;
                    576:     g_entries[x].val = fmakeword(stdin,'&',&cl);
                    577:     plustospace(g_entries[x].val);
                    578:     unescape_url(g_entries[x].val);
                    579:     g_entries[x].name = makeword(g_entries[x].val,'=');
                    580:   }
                    581:   /* ---------------------------------------------------- */
                    582:   g_entered_pin = 0; g_run_mode =0;
                    583:   submissions_str[0]='\0';
                    584:   for(x=0; x <= m; x++) {
                    585:       if( !strcmp(g_entries[x].name,"CLASS") ) {
                    586:         strncpy(g_class_name,g_entries[x].val,MAX_CLASS_CHAR);
                    587:       }
                    588:       if( !strcmp(g_entries[x].name,"M") ) {  /* run mode */
                    589:         sscanf(g_entries[x].val,"%d",&g_run_mode);
                    590:       }
                    591:       if( !strcmp(g_entries[x].name,"STARTNUM")) {
                    592: 	sscanf(g_entries[x].val,"%d",&g_start_question);
                    593:       }
                    594:       if( !strcmp(g_entries[x].name,"SNUM") ) {
                    595:         strncpy(g_student_number,g_entries[x].val,MAX_STUDENT_NUMBER+4);
                    596:       }
                    597:       if( !strcmp(g_entries[x].name,"CAPAID") ) {
                    598:         sscanf(g_entries[x].val,"%d",&g_entered_pin);
                    599:       }
                    600:       if( !strcmp(g_entries[x].name,"SET") ) {
                    601:         sscanf(g_entries[x].val,"%d",&g_set);
                    602:       }
                    603:       if( !strcmp(g_entries[x].name,"VSET") ) {
                    604: 	if (g_entries[x].val[0] == '\0') {
                    605: 	  g_vset=0;
                    606: 	} else {
                    607: 	  sscanf(g_entries[x].val,"%d",&g_vset);
                    608: 	}
                    609:       }
                    610:       if( !strcmp(g_entries[x].name,"KND") ) {
                    611:         sscanf(g_entries[x].val,"%d",&g_skind);
                    612:       }
                    613:       w_get_responses(x,q_idx,submissions_str);
                    614:       free(g_entries[x].val);
                    615:       free(g_entries[x].name);
                    616:   }
                    617:   
                    618: #ifdef CGI_DBUG
                    619:   fprintf(g_cgi,"DONE: Parse input\n"); fflush(g_cgi);
                    620: #endif /* CGI_DBUG */
                    621:   /* --------------------------------------------------------- */
                    622:   /* if( strcasecmp(g_prog_name,"capacheckin") == 0 ) { */
                    623:   if( g_run_mode == M_CHECKIN ) { /* capa_checkin */
                    624:     time(&curtime); time_str = ctime(&curtime);
                    625:     time_str[ strlen(time_str)-1 ] = 0;
                    626:     envPtr=getenv("REMOTE_HOST");
                    627:     if(!envPtr) { 
                    628:       fprintf(stdout,"<!-- Enviroment variable REMOTE_HOST not set.-->\n");
                    629:       envPtr=getenv("REMOTE_ADDR");
                    630:       if(!envPtr) { 
                    631: 	fprintf(stdout,"<!-- Enviroment variable REMOTE_ADDR not set.-->\n");
                    632: 	envPtr=UNKNOWN;
                    633: 	error |= 32;
                    634:       }
                    635:     }
                    636: #ifdef CGI_DBUG
                    637:   fprintf(g_cgi,"DONE: REMOTE_HOST\n"); fflush(g_cgi);
                    638: #endif /* CGI_DBUG */
                    639: 
                    640:     envPtr2=getenv("HTTP_USER_AGENT");
                    641:     if(!envPtr2) { 
                    642:       fprintf(stdout,"<!-- Enviroment variable HTTP_USER_AGENT not set. -->\n");
                    643:       envPtr2=UNKNOWN;
                    644:       error |= 64;
                    645:     }
                    646:     sprintf(log_str,"%s\t%s\t%s\t%s\t%s\n",g_class_name,g_student_number,time_str,envPtr,envPtr2);
                    647:     if (web_log(log_str) == -1 ) {
                    648:       fprintf(stdout,"Unable to log access to the system. Please notify instructor\n.");
                    649:       fprintf(stdout,"No tries have been deducted for the last submission.\n");
                    650:       error |= 128;
                    651:       return error;
                    652:     }
                    653:     
                    654:   }
                    655: #if defined(NeXT) 
                    656:    getwd(g_cwd);
                    657: #else
                    658:    getcwd(g_cwd,255);
                    659: #endif
                    660:   web_getclassdir(&g_cpath, &g_cowner, g_class_name);
                    661:   sprintf(g_class_fullpath,"%s/%s",g_cpath,g_class_name);
                    662: #ifdef CGI_DBUG
                    663:   fprintf(g_cgi,"DONE: getclassdir() [%s]\n",g_class_fullpath); fflush(g_cgi);
                    664: #endif /* CGI_DBUG */
                    665:   if( !capa_access(g_class_fullpath, F_OK) == 0 ) {
                    666:     fprintf(stdout,"ACCESS: could not access the class directory [%s]!\n",g_class_fullpath);
                    667:     fprintf(stdout,"Please exit the web browser and try accessing the system again\n");
                    668:     fprintf(stdout,"No tries have been deducted for the last submission.\n");
                    669:     fflush(stdout);
                    670:     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",
                    671: 	    g_class_fullpath,g_cpath,g_class_name,g_cowner,g_cwd);
                    672:     if (web_log(log_str) == -1 ) {
                    673:       fprintf(stdout,"Unable to log access to the system. Please notify instructor\n.");
                    674:       fflush(stdout);
                    675:       error |= 256;
                    676:       return error;
                    677:     } 
                    678: #ifdef CGI_DBUG
                    679:     fprintf(g_cgi,"NO ACCESS: cannot access() [%s]\n",g_class_fullpath); fflush(g_cgi);
                    680: #endif /* CGI_DBUG */      
                    681:     error |= 512;
                    682:     return (error);
                    683:   }
                    684:   /* ---------------------------------------------------- */
                    685:   /*      change working directory to the class           */
                    686:   /* ---------------------------------------------------- */
                    687:   chdir(g_class_fullpath); /* before performing any capa*() calls */
                    688: #ifdef CGI_DBUG
                    689:   fprintf(g_cgi,"DONE cd to [%s]\n",g_class_fullpath); fflush(g_cgi);
                    690: #endif /* CGI_DBUG */
                    691: 
                    692:   /* Now in proper directory, can log submissions */
                    693:   if ( g_run_mode == M_CHECKANS) {
                    694:     if (w_log_submissions(g_student_number,g_set,submissions_str) == -1 ) {
                    695:       fprintf(stdout,"Unable to log submissions. Please notify instructor\n.");
                    696:       fprintf(stdout,"No tries have been deducted for the last submission.\n");
                    697:       error |= 1024;
                    698:       return error; 
                    699:     }
                    700:   }
                    701: 
                    702:   result=read_capa_config("capaweb_cgibin_path",buf);
                    703:   if (result != 0 && result != -1) {
                    704:     g_cgibin_path=capa_malloc(strlen(buf)+1,1);
                    705:     strcpy(g_cgibin_path,buf);
                    706:   } else {
                    707:     g_cgibin_path=capa_malloc(strlen("capa-bin")+1,1);
                    708:     strcpy(g_cgibin_path,"capa-bin");
                    709:   }
                    710: 
                    711:   if( g_entered_pin != 0 ) {
                    712:       g_login_set = capa_PIN(g_student_number,999,g_entered_pin);
                    713: #ifdef CGI_DBUG
                    714:   fprintf(g_cgi,"Succeed in login to set %d with pin %d\n",g_login_set,g_entered_pin); fflush(g_cgi);
                    715: #endif /* CGI_DBUG */   
                    716:     /* ---------------------------------------------------- 
                    717:       printf("Your entered capa id %d, login to set %d\n", g_entered_pin, g_login_set);
                    718:       printf("The real capa id for %s is %d\n",g_student_number,capa_PIN(g_student_number,1,0));
                    719:     ---------------------------------------------------- */
                    720:     
                    721:   } else {
                    722:     fprintf(stdout,"CAPA ID entered was zero, this is not valid.\n");
                    723:     fprintf(stdout,"No tries have been deducted for the last submission.\n");
                    724:     fflush(stdout);
                    725:     error |= 2048; return (error);
                    726:   }
                    727: 
                    728:   if (!g_login_set) { 
                    729:         fprintf(stdout,"The student ID (%s) or CAPA ID (%d) that you entered \
                    730: is not listed for the class (%s) that you have selected. Please check that \
                    731: you have selected the correct class on the CAPA logon page and that the \
                    732: student ID and CAPA ID are correct.\n", 
                    733: 		g_student_number, g_entered_pin, g_class_name);
                    734:         fflush(stdout);
                    735: #ifdef CGI_DBUG
                    736: 	fprintf(g_cgi,"CAPA ID or student number is not valid.\n"); 
                    737: 	fflush(g_cgi);
                    738: #endif /* CGI_DBUG */   
                    739:         error |= 4096;  return (error);
                    740:   } else {
                    741:       if ( g_login_set > 99 )  { 
                    742:         fprintf(stdout,"The student ID (%s) or CAPA ID (%d) that you entered \
                    743: is not listed for the class (%s) that you have selected. Please check that \
                    744: you have selected the correct class on the CAPA logon page and that the \
                    745: student ID and CAPA ID are correct.\n", 
                    746: 		g_student_number, g_entered_pin, g_class_name);
                    747:            fflush(stdout);
                    748: #ifdef CGI_DBUG
                    749: 	   fprintf(g_cgi,"CAPA ID is not valid.\n"); fflush(g_cgi);
                    750: #endif /* CGI_DBUG */            
                    751:            error |= 8192; return (error);
                    752:       }
                    753:       if(g_login_set < g_vset ) {
                    754:            fprintf(stdout,"Your CAPA ID (for set %d) does not allow you to view set %d.\n",g_login_set, g_vset);
                    755:            fflush(stdout);
                    756: #ifdef CGI_DBUG
                    757: 	   fprintf(g_cgi,"Login set %d is less than view set %d.\n",g_login_set,g_vset); fflush(g_cgi);
                    758: #endif /* CGI_DBUG */ 
                    759:            error |= 16384; return (error);
                    760:       }
                    761:       chdir(g_class_fullpath);  /* again, to make sure */
                    762:       
                    763:       if ( capa_get_student(g_student_number,&g_student_data) == 0 ) {
                    764:            fprintf(stdout,"Entered student id is not in the class list.\n");
                    765: 	   fprintf(stdout,"Please check that have selected the correct class \
                    766: and have entered you student id correctly.\n");
                    767:            fflush(stdout);
                    768: #ifdef CGI_DBUG
                    769: 	   fprintf(g_cgi,"get_student(): Student id not in the classl file.\n"); fflush(g_cgi);
                    770: #endif /* CGI_DBUG */               
                    771:             error |= 32768; return (error);
                    772:       } else {
                    773: 	 time(&curtime);
                    774: 	 if (capa_get_header(&header, g_login_set))  {
                    775: 	   fprintf(stdout,"This problem set is not ready yet.\n"); 
                    776: 	   fflush(stdout);
                    777: 	  
                    778: #ifdef CGI_DBUG
                    779: 	   fprintf(g_cgi,"get_header(): Problem set not ready.\n"); fflush(g_cgi);
                    780: #endif /* CGI_DBUG */            
                    781: 	   error |= 65536; return (error);
                    782:  	 }
                    783: 	 capa_mfree(header.weight);
                    784: 	 capa_mfree(header.partial_credit);
                    785:          /* ===> if (compare_datetime(curtime,header.open_date) < 0 ) { */
                    786:          if( capa_check_date(CHECK_OPEN_DATE,g_student_number,
                    787: 			     g_student_data.s_sec,g_login_set) < 0 ) {
                    788:             fprintf(stdout,"This set(%d) is not yet open. Please try back later.\n",g_login_set); 
                    789: 	    fflush(stdout);
                    790: #ifdef CGI_DBUG
                    791: 	    fprintf(g_cgi,"check_date(): Problem set not open.\n"); 
                    792: 	    fflush(g_cgi);
                    793: #endif /* CGI_DBUG */    
                    794:             error |= 131072; return (error);
                    795:          }
                    796:       }
                    797:   }
                    798:   return (error);
                    799: }
                    800: 
                    801: /* ============================================================================= */
                    802: void        append_qtext(new_str) char *new_str;
                    803: {
                    804:   int ii,len;
                    805:   if (new_str==NULL) return;
                    806:   len=strlen(new_str);
                    807: #ifdef CGI_DBUG
                    808:   fprintf(g_cgi,"before: len %d; g_qchar_cnt %d; g_qsize %d\n",
                    809: 	  len,g_qchar_cnt,g_qsize);
                    810:   fflush(g_cgi);
                    811: #endif /* CGI_DBUG */    
                    812:   if (g_qchar_cnt+len>g_qsize-1) {
                    813:     char *temp_text;
                    814:     g_qsize=(g_qchar_cnt+len)*2;
                    815:     temp_text=capa_malloc(g_qsize,sizeof(char));
                    816:     strncpy(temp_text,g_question_txt,g_qchar_cnt);
                    817:     capa_mfree(g_question_txt);
                    818:     g_question_txt=temp_text;
                    819:   }
                    820:   for(ii=0;ii<len;ii++) {
                    821:     g_question_txt[g_qchar_cnt+ii]=new_str[ii];
                    822:   }
                    823:   g_qchar_cnt += len;
                    824:   g_question_txt[g_qchar_cnt+1]='\0';
                    825: #ifdef CGI_DBUG
                    826:   fprintf(g_cgi,"after: len %d; g_qchar_cnt %d; g_qsize %d\n",len,g_qchar_cnt,g_qsize);
                    827:   fflush(g_cgi);
                    828: #endif /* CGI_DBUG */    
                    829: }
                    830: void        append_stext(new_str) char *new_str;
                    831: {
                    832:   int ii,len;
                    833:   if (new_str==NULL) return;
                    834:   len=strlen(new_str);
                    835: #ifdef CGI_DBUG
                    836:   fprintf(g_cgi,"appending status{%s}\nlen %d; g_schar_cnt %d; g_ssize %d\n",
                    837: 	  new_str,len,g_schar_cnt,g_ssize);
                    838:   fflush(g_cgi);
                    839: #endif /* CGI_DBUG */    
                    840:   if (g_schar_cnt+len>g_ssize-1) {
                    841:     char *temp_text;
                    842:     g_ssize=(g_schar_cnt+len)*2;
                    843:     temp_text=capa_malloc(g_ssize,sizeof(char));
                    844:     strncpy(temp_text,g_status_txt,g_schar_cnt);
                    845:     capa_mfree(g_status_txt);
                    846:     g_status_txt=temp_text;
                    847:   }
                    848:   for(ii=0;ii<len;ii++) {
                    849:     g_status_txt[g_schar_cnt+ii]=new_str[ii];
                    850:   }
                    851:   g_schar_cnt += len;
                    852:   g_status_txt[g_schar_cnt+1]='\0';
                    853: #ifdef CGI_DBUG
                    854:   fprintf(g_cgi,"len %d; g_schar_cnt %d; g_ssize %d\n",len,g_schar_cnt,g_ssize);
                    855:   fflush(g_cgi);
                    856: #endif /* CGI_DBUG */    
                    857: }
                    858: /* ============================================================================= */
                    859: /* ------------------------------------------------------------
                    860:   printf("ENV<br>\n");
                    861:   printf("SERVER_PROTOCOL:%s<br>",getenv("SERVER_PROTOCOL"));
                    862:   printf("PATH_INFO:%s<br>",getenv("PATH_INFO"));
                    863:   printf("PATH_TRANSLATED:%s<br>\n",getenv("PATH_TRANSLATED"));
                    864:   printf("SCRIPT_NAME:%s<br>\n",getenv("SCRIPT_NAME"));
                    865:   printf("QUERY_STRING:%s<br>\n",getenv("QUERY_STRING"));
                    866:   printf("REMOTE_HOST:%s<br>\n",getenv("REMOTE_HOST"));
                    867:   printf("REMOTE_USER:%s<br>\n",getenv("REMOTE_USER"));
                    868:   printf("REMOTE_IDENT:%s<br>\n",getenv("REMOTE_IDENT"));
                    869:   printf("USER_AGENT:%s<br>\n",getenv("USER_AGENT"));
                    870:   printf("HTTP_USER_AGENT:%s<br>\n",getenv("HTTP_USER_AGENT"));
                    871:   ------------------------------------------------------------
                    872: */
                    873: 
                    874: /* ------------------------------------------------------ */
                    875: /*   A class directory must have   */
                    876: /*     records/                    */
                    877: /*                                 */
                    878: /*  returns: 0  structure is correct, but no set.db files */
                    879: /*          -1  structure is not correct                  */
                    880: /*          >=1 the last set.db                           */
                    881: 
                    882: int
                    883: check_class_get_maxset(dir_path) char  *dir_path;
                    884: {
                    885:   char   f_name[1024];
                    886:   int    set;
                    887:   
                    888:   if( capa_access(dir_path, F_OK) == 0 ) { /* class dir exists */
                    889:     sprintf(f_name,"%s/records",dir_path);
                    890:     if( capa_access(f_name, F_OK) == 0 ) { /* class/records dir exists */
                    891:       for(set = 1; ; set++ ) {
                    892:         sprintf(f_name,"%s/records/set%d.db",dir_path,set);
                    893:         if(capa_access(f_name, F_OK) == -1 )  break;
                    894:       }
                    895:       set--;
                    896:     } else {
                    897:       set = -1;
                    898:     }
                    899:   } else {
                    900:     set = -1;
                    901:   } 
                    902:   return (set);
                    903: }
                    904: 
                    905: /* ------------------------------------------------------------------------- */
                    906: /* Get Exam and Quiz Path                                                    */
                    907: /*   return  0, 1, 2, 3                                                      */
                    908: /* 0: Neither exist                                                          */
                    909: /* 1: Exam.path exists                                                       */
                    910: /* 2: Quiz.path exists                                                       */
                    911: /* 3: Both Exam.path and Quiz.path exists                                    */
                    912: /* ------------------------------------------------------------------------- */
                    913: int
                    914: check_exam_quiz_path()
                    915: {
                    916:   char  buf[MAX_BUFFER_SIZE];
                    917:   int   result = 0, configResult=0;
                    918:   
                    919:   configResult=read_capa_config("exam_path",buf);
                    920:   if (configResult != 0 && configResult != -1) {
                    921:     g_exam_set = check_class_get_maxset(buf);
                    922:     if(g_exam_set > 0 )  {
                    923:       result = 1;
                    924:       sprintf(g_exam_path,buf);
                    925:     }
                    926:   }
                    927:   configResult=read_capa_config("quiz_path",buf);
                    928:   if (configResult != 0 && configResult != -1) {
                    929:     g_quiz_set = check_class_get_maxset(buf);
                    930:     if(g_quiz_set > 0 )  {
                    931:       result = (result | 2);
                    932:       sprintf(g_quiz_path,buf);
                    933:     }
                    934:   }
                    935:   return (result);
                    936: }
                    937: 
                    938: 
                    939: int
                    940: check_termscore_option()
                    941: {
                    942:   char  buf[MAX_BUFFER_SIZE];
                    943:   int   result = 0, configResult=0;
                    944:   
                    945:   configResult=read_capa_config("term_score_applet",buf);
                    946:   if (configResult != 0 && configResult != -1) {
                    947:     fprintf(stdout,"<!-- term_score_applet is in capa.config file -->\n");
                    948:     if (strcasecmp(buf,"yes")==0) {
                    949:       fprintf(stdout,"<!-- term_score_applet is YES -->\n");
                    950:       result=1;
                    951:     }
                    952:   }
                    953:   return (result);
                    954: }
                    955: 
                    956: 
                    957: 
                    958: 
                    959: /* ============================================================================= */
                    960: void
                    961: print_mainmenu(class,sn,pin)char *class; char *sn;int pin;
                    962: {
                    963:   char  buf[MAX_BUFFER_SIZE];
                    964:   int   outcome,configResult,term_summary_button=1;
                    965:   char *serverName;
                    966: 
                    967:   serverName=getenv("SERVER_NAME");
                    968:   if (!serverName) {
                    969:     fprintf(stdout,"Enviroment variable SERVER_NAME not set.\n");
                    970:     fprintf(stdout,"Unable to complete actions.\n");
                    971:     return;
                    972:   }
                    973:   
                    974:   fprintf(stdout,"<TITLE>%s main menu</TITLE>\n",g_class_name);
                    975:   fprintf(stdout,"<h3>%s</h3><br>\n",g_student_name);
                    976:   fprintf(stdout,"<menu>\n");
                    977:   fprintf(stdout,"<li><form method=\"get\" action=\"http://%s/CAPA/help.html\">",serverName);
                    978:   fprintf(stdout,"<input type=\"submit\" value=\"Help\" ></form>\n");
                    979: 
                    980:   fprintf(stdout,"<li><form method=\"post\" ");
                    981:   /* fprintf(stdout," target=\"right_frame\" "); */
                    982:   sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
                    983:   fprintf(stdout,"%s\n", buf);
                    984:   fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",class);
                    985:   fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",sn);
                    986:   fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",pin);
                    987:   fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_TRYSET);
                    988:   fprintf(stdout,"<input type=\"hidden\" name=\"STARTNUM\" value=\"%d\">\n",1);
                    989:   fprintf(stdout,"<input type=\"submit\" value=\"Try current set\" ></form>\n");
                    990:   
                    991:   fprintf(stdout,"<li><form method=\"post\" ");
                    992:   /* fprintf(stdout," target=\"right_frame\" "); */
                    993:   sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
                    994:   fprintf(stdout,"%s\n", buf);
                    995:   fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",class);
                    996:   fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",sn);
                    997:   fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",pin);
                    998:   fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_VIEWPREV);
                    999:   fprintf(stdout,"<input type=\"hidden\" name=\"STARTNUM\" value=\"%d\">\n",1);
                   1000:   fprintf(stdout,"<input type=\"submit\" value=\"View previous set:\" >");
                   1001:   fprintf(stdout,"<b> set:</b><input name=\"VSET\" value=\"\" size=4></form>\n");
                   1002:   
                   1003:   /*Term Summary*/
                   1004:   configResult=read_capa_config("term_summary_button",buf);
                   1005:   if (configResult != 0 && configResult != -1 ) {
                   1006:     if (strcasecmp(buf,"no")==0) {
                   1007:       term_summary_button=0;
                   1008:     }
                   1009:   }
                   1010:   if (term_summary_button) {
                   1011:     fprintf(stdout,"<li><form method=\"post\" ");
                   1012:     /* fprintf(stdout," target=\"right_frame\" "); */
                   1013:     sprintf(buf,"action=\"http://%s/%s/capahtml\">",serverName,g_cgibin_path);
                   1014:     fprintf(stdout,"%s\n", buf);
                   1015:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",class);
                   1016:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",sn);
                   1017:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",pin);
                   1018:     fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_VIEWSUMM);
                   1019:     fprintf(stdout,"<input type=\"submit\" value=\"Display term summary\" ></form>\n");
                   1020:   }
                   1021: 
                   1022:   outcome = check_exam_quiz_path();
                   1023:   if( outcome & 1 ) {  /* exam summary */
                   1024:     fprintf(stdout,"<li><form method=\"post\" ");
                   1025:     /* fprintf(stdout," target=\"right_frame\" "); */
                   1026:     sprintf(buf,"action=\"http://%s/%s/capahtml\">",serverName,g_cgibin_path);
                   1027:     fprintf(stdout,"%s\n", buf);
                   1028:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",class);
                   1029:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",sn);
                   1030:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",pin);
                   1031:     fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_EXAMSUMM);
                   1032:     fprintf(stdout,"<input type=\"submit\" value=\"Display Exam summary\" ></form>\n");
                   1033:   }
                   1034:   if( outcome & 2 ) { /* Quiz summary */
                   1035:     fprintf(stdout,"<li><form method=\"post\" ");
                   1036:     /* fprintf(stdout," target=\"right_frame\" "); */
                   1037:     sprintf(buf,"action=\"http://%s/%s/capahtml\">",serverName,g_cgibin_path);
                   1038:     fprintf(stdout,"%s\n", buf);
                   1039:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",class);
                   1040:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",sn);
                   1041:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",pin);
                   1042:     fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\" >\n",M_QUIZSUMM );
                   1043:     fprintf(stdout,"<input type=\"submit\" value=\"Display Quiz summary\" ></form>\n");
                   1044:   }
                   1045:   outcome = check_termscore_option();
                   1046:   fprintf(stdout,"<!-- Outcome of check_termscore_option()=%d --!>\n",outcome);
                   1047:   /*Termscore Button*/
                   1048:   if( outcome ) {
                   1049:     fprintf(stdout,"<li><form method=\"post\" ");
                   1050:     sprintf(buf,"action=\"http://%s/%s/capahtml\">",serverName,g_cgibin_path);
                   1051:     fprintf(stdout,"%s\n", buf);
                   1052:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",class);
                   1053:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",sn);
                   1054:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",pin);
                   1055:     fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\" >\n",M_TERMSCORE );
                   1056:     fprintf(stdout,"<input type=\"submit\" value=\"Extrapolate Term score\" ></form>\n");
                   1057:   }
                   1058:   /*Exit Button*/
                   1059:   fprintf(stdout,"<TD><form method=\"get\" action=\"http://%s/CAPA/class.html\">",serverName);
                   1060:   fprintf(stdout,"<input type=\"button\" value=\"Exit\" onclick=\"window.close()\"></form></TD>");
                   1061: 
                   1062:   fprintf(stdout,"</menu>\n"); fflush(stdout);
                   1063:   
                   1064: }
                   1065: 
                   1066: /* ====================================================================================== */
                   1067: void
                   1068: print_page_header(mode,num_quest) int mode;int num_quest;
                   1069: {
1.2       albertel 1070:   char   buf[MAX_BUFFER_SIZE], discussdir[MAX_BUFFER_SIZE];
1.1       albertel 1071:   char *serverName;
                   1072:   int    configResult,term_summary_button=1;
                   1073: 
                   1074:   serverName=getenv("SERVER_NAME");
                   1075:   if (!serverName) {
                   1076:     fprintf(stdout,"Enviroment variable SERVER_NAME not set.\n");
                   1077:     fprintf(stdout,"Unable to complete actions.\n");
                   1078:     return;
                   1079:   }
                   1080:   
                   1081:   /* now done in the .qz file
                   1082:   fprintf(stdout,"<TITLE>%s set%d</TITLE>",g_class_name,g_login_set);
                   1083:   if( mode == VIEW_PREVIOUS_MODE ) {
                   1084:     fprintf(stdout,"<H2>%s set%d</H2>\n",g_class_name,g_vset);
                   1085:   } else {
                   1086:     fprintf(stdout,"<H2>%s set%d</H2>\n",g_class_name,g_login_set);
                   1087:   }
                   1088:   fprintf(stdout,"<H3>%s</H3>\n",g_student_data.s_nm);
                   1089:   */
                   1090:   
                   1091:   fprintf(stdout,"<A NAME=\"TOP\"></A>");
                   1092:   fprintf(stdout,"<TABLE cellpadding=0 cellspacing=0 border=0>\n<TR><TD>");
                   1093: 
                   1094: 
                   1095:   /*Term summary button*/
                   1096:   configResult=read_capa_config("term_summary_button",buf);
                   1097:   if (configResult != 0 && configResult != -1 ) {
                   1098:     if (strcasecmp(buf,"no")==0) {
                   1099:       term_summary_button=0;
                   1100:     }
                   1101:   }
                   1102: #ifdef CGI_DBUG
                   1103:   fprintf(g_cgi,"buf: %s\ntermsum: %d\n",buf,term_summary_button); fflush(g_cgi);
                   1104: #endif /* CGI_DBUG */ 
                   1105: 
                   1106:   if (term_summary_button) {
                   1107:     fprintf(stdout,"<form method=\"post\" ");
                   1108:     sprintf(buf,"action=\"http://%s/%s/capahtml\">",serverName,g_cgibin_path);
                   1109:     fprintf(stdout,"%s\n", buf);
                   1110:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
                   1111:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
                   1112:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
                   1113:     fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_VIEWSUMM);
                   1114:     fprintf(stdout,"<input type=\"submit\" value=\"Term summary\" ></form></TD>\n");
                   1115:   }
                   1116: 
                   1117:   /*Exit Button*/
                   1118:   fprintf(stdout,"<TD><form method=\"get\" action=\"http://%s/CAPA/class.html\">",serverName);
                   1119:   fprintf(stdout,"<input type=\"button\" value=\"Exit\" onclick=\"window.close()\"></form></TD>");
                   1120:   /*help button*/
                   1121:   if (mode != VIEW_PREVIOUS_MODE) {
                   1122:     fprintf(stdout,"<TD><form method=\"get\" action=\"http://%s/CAPA/help.html\">",serverName);
                   1123:     fprintf(stdout,"<input type=\"submit\" value=\"Help\"></form></TD>");
                   1124:   }
                   1125: 
                   1126:   /*Reload button*/
                   1127:   fprintf(stdout,"<TD><form method=\"post\" ");
                   1128:   sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
                   1129:   fprintf(stdout,"%s\n", buf);
                   1130:   fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
                   1131:   fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
                   1132:   fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
                   1133:   fprintf(stdout,"<input type=\"hidden\" name=\"STARTNUM\" value=\"%d\">\n",g_start_question);
                   1134:   if (mode == VIEW_PREVIOUS_MODE ) {
                   1135:     fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_VIEWPREV);
                   1136:     fprintf(stdout,"<input type=\"hidden\" name=\"VSET\" value=\"%d\" size=4>\n",g_vset);
                   1137:   } else {
                   1138:     fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_TRYSET);
                   1139:   }
                   1140:   fprintf(stdout,"<input type=\"submit\" value=\"Reload\" >\n</form></TD>");
                   1141: #ifdef NOT_DEFINED
                   1142:   /* Next Button */
                   1143:   if ( (!(g_num_questions_per_page==ALL_QUESTIONS)) && 
                   1144:        ((g_num_questions_per_page+g_start_question)<num_quest)) {
                   1145:     fprintf(stdout,"<TD><form method=\"post\" ");
                   1146:     sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
                   1147:     fprintf(stdout,"%s\n", buf);
                   1148:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
                   1149:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
                   1150:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
                   1151:     fprintf(stdout,"<input type=\"hidden\" name=\"STARTNUM\" value=\"%d\">\n",g_start_question+g_num_questions_per_page);
                   1152:     if (mode == VIEW_PREVIOUS_MODE ) {
                   1153:       fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_VIEWPREV);
                   1154:       fprintf(stdout,"<input type=\"hidden\" name=\"VSET\" value=\"%d\" size=4>\n",g_vset);
                   1155:     } else {
                   1156:       fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_TRYSET);
                   1157:     }
                   1158:     fprintf(stdout,"<input type=\"submit\" value=\"Next Page\" >\n</form></TD>");
                   1159:   }
                   1160: 
                   1161:   /* Previous Button */
                   1162:   if ( (!(g_num_questions_per_page==ALL_QUESTIONS)) && (g_start_question > 1)) {
                   1163:     fprintf(stdout,"<TD><form method=\"post\" ");
                   1164:     sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
                   1165:     fprintf(stdout,"%s\n", buf);
                   1166:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
                   1167:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
                   1168:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
                   1169:     fprintf(stdout,"<input type=\"hidden\" name=\"STARTNUM\" value=\"%d\">\n",g_start_question-g_num_questions_per_page);
                   1170:     if (mode == VIEW_PREVIOUS_MODE ) {
                   1171:       fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_VIEWPREV);
                   1172:       fprintf(stdout,"<input type=\"hidden\" name=\"VSET\" value=\"%d\" size=4>\n",g_vset);
                   1173:     } else {
                   1174:       fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_TRYSET);
                   1175:     }
                   1176:     fprintf(stdout,"<input type=\"submit\" value=\"Previous Page\" >\n</form></TD>");
                   1177:   }
                   1178: #endif
                   1179:   /* Goto Button */
                   1180:   if (!(g_num_questions_per_page==ALL_QUESTIONS)) {
                   1181:     int idx,numquest;
                   1182:     T_header header;
                   1183:     fprintf(stdout,"<TD><form method=\"post\" ");
                   1184:     sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
                   1185:     fprintf(stdout,"%s\n", buf);
                   1186:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
                   1187:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
                   1188:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
                   1189:     fprintf(stdout,"<input type=\"submit\" value=\"Goto\" >");
                   1190:     fprintf(stdout,"<b>Problem:</b><input name=\"STARTNUM\" value=\"\" size=4>\n");
                   1191:     if (mode == VIEW_PREVIOUS_MODE ) {
                   1192:       fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_VIEWPREV);
                   1193:       fprintf(stdout,"<input type=\"hidden\" name=\"VSET\" value=\"%d\" size=4>\n",g_vset);
                   1194:     } else {
                   1195:       fprintf(stdout,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_TRYSET);
                   1196:     }
                   1197: 
                   1198:     if (!capa_get_header(&header, g_login_set)) {
                   1199:       numquest=atoi(header.num_questions);
                   1200:       capa_mfree(header.weight);
                   1201:       capa_mfree(header.partial_credit);
                   1202:       for(idx=0;idx<numquest;idx++) {
                   1203: 	preserve_last_answer(idx,1);
                   1204:       }
                   1205:     }
                   1206:     fprintf(stdout,"</form></TD>");
1.2       albertel 1207:   }
                   1208: 
                   1209:   /*Discuss Button*/
                   1210: 
                   1211:   sprintf(discussdir,"%s/discussion/%d",g_class_fullpath,g_login_set);
                   1212:   if ( access(discussdir,F_OK) == 0 ) {
                   1213:     fprintf(stdout,"<TD><form method=\"post\" ");
                   1214:     sprintf(buf,"action=\"http://%s/%s/%s/capadiscuss\">",serverName,g_cgibin_path,g_cowner);
                   1215:     fprintf(stdout,"%s\n", buf);
                   1216:     fprintf(stdout,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
                   1217:     fprintf(stdout,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
                   1218:     fprintf(stdout,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
                   1219:     fprintf(stdout,"<input type=\"hidden\" name=\"SETID\" value=\"%d\">\n",g_login_set);
                   1220:     fprintf(stdout,"<input type=\"submit\" value=\"Discuss\" >\n</form></TD>");
1.1       albertel 1221:   }
                   1222: 
                   1223:   fprintf(stdout,"\n</TR></TABLE>\n");
                   1224:   fflush(stdout);    
                   1225: }
                   1226: 
                   1227: void create_status_line(int mode,int question_cnt, T_entry* entry)
                   1228: {
                   1229:   char          buf[MAX_BUFFER_SIZE];
                   1230:   int           idx,configResult,status_line_length;
                   1231: 
                   1232:   configResult=read_capa_config("web_status_line_length",buf);
                   1233:   if (configResult != 0 && configResult != -1 ) {
                   1234:     if (sscanf(buf,"%d",&status_line_length)==0) {
                   1235:       status_line_length=question_cnt;
                   1236:     }
                   1237:   } else {
                   1238:     status_line_length=question_cnt;
                   1239:   }
                   1240: 
                   1241:   append_stext("<TABLE cellpadding=0 cellspacing=0 border=0><TR>");
                   1242:   append_stext("<TD><b><u>Go to problem</u>  </b></TD><TD></TD>");
                   1243:   for(idx=0; idx < status_line_length;idx++ ) {
                   1244:     sprintf(buf,"<TD ALIGN=center VALIGN=bottom>[%d]</TD>",idx+1);
                   1245:     append_stext(buf);
                   1246:   }
                   1247:   for(idx = 0; idx < question_cnt; idx++ ) {
                   1248:     if ( !(idx%status_line_length) ) {
                   1249:       sprintf(buf,"</TR><TR><TD ALIGN=left>%d-%d</TD><TD ALIGN=right>Status: </TD>",
                   1250: 		   idx+1,idx+status_line_length);
                   1251:       append_stext(buf);
                   1252:     }
                   1253:     if ((idx >= g_start_question-1) && 
                   1254: 	 (((g_num_questions_per_page == ALL_QUESTIONS)) || 
                   1255: 	   (idx < (g_start_question+g_num_questions_per_page-1)))) {
                   1256:       sprintf(buf,"<TD ALIGN=center VALIGN=bottom><A href=\"#P%d\">",idx+1);
                   1257:     } else {
                   1258:       sprintf(buf,"<TD ALIGN=center VALIGN=bottom>");
                   1259:     }
                   1260:     append_stext(buf);
                   1261:     if ( (mode == CHECK_ANSWER_MODE) && g_log_string[idx] == '-') {
                   1262:       if (g_inhibit_response && (entry->answers[idx]!='-')) {
                   1263: 	sprintf(buf,"A");
                   1264:       } else {
                   1265: 	sprintf(buf,"%c",entry->answers[idx]); 
                   1266:       }
                   1267:     } else {
                   1268:       if (g_inhibit_response && (entry->answers[idx]!='-')) {
                   1269: 	sprintf(buf,"<b>A</b>");
                   1270:       } else {
                   1271: 	if ( mode == CHECK_ANSWER_MODE ) {
                   1272: 	  sprintf(buf,"<b>%c</b>",g_log_string[idx]);
                   1273: 	} else {
                   1274: 	  sprintf(buf,"<b>%c</b>",entry->answers[idx]);
                   1275: 	}
                   1276:       }
                   1277:     }
                   1278:     append_stext(buf);
                   1279:     if ((idx >= g_start_question-1) && 
                   1280: 	 (((g_num_questions_per_page == ALL_QUESTIONS)) || 
                   1281: 	   (idx < (g_start_question+g_num_questions_per_page-1)))) {
                   1282:       sprintf(buf,"</A></TD>");
                   1283:     } else {
                   1284:       sprintf(buf,"</TD>");
                   1285:     }
                   1286:     append_stext(buf);
                   1287:   }
                   1288:   append_stext("</TR></TABLE>\n");
                   1289: }
                   1290: 
                   1291: /* -------------------------------- called by try set, view previous, check answer */
                   1292: void
                   1293: print_quizz(class_dir,c_owner,class,sn,pin,set,mode)
                   1294: char *class_dir; char *c_owner;char *class;char *sn;int pin;int set;int mode;
                   1295: {
                   1296:   extern int    Parsemode_f;
                   1297:   extern char  *StartText_p;
                   1298:   extern char  *EndText_p;
                   1299:   Problem_t    *first_prob, *prob_idx;
                   1300:   int           result, question_idx, question_cnt, idx, view_assignment_after_due=1;
                   1301:   int           q_leng, display_ans=1, configResult;
                   1302:   int           view_assignments_after_due=1;
                   1303:   char          buf[MAX_BUFFER_SIZE];
                   1304:   char          class_fullpath[FILE_NAME_LENGTH];
                   1305:   T_entry       entry;
                   1306:   T_header      header;
                   1307:   long          offset;
                   1308:   double        a;
                   1309:   char          cmp_ans[MAX_BUFFER_SIZE],date_str[DATE_BUFFER];
                   1310:   time_t        curtime;
                   1311:   char         *serverName;
                   1312: 
                   1313:   serverName=getenv("SERVER_NAME");
                   1314:   if (!serverName) {
                   1315:     fprintf(stdout,"Enviroment variable SERVER_NAME not set.\n");
                   1316:     fprintf(stdout,"Unable to complete actions.\n");
                   1317:     return;
                   1318:   }
                   1319: 
                   1320:   sprintf(class_fullpath,"%s/%s",class_dir,class);
                   1321:   
                   1322:   /*
                   1323:   chdir(class_fullpath);
                   1324:   */
                   1325: #ifdef CGI_DBUG
                   1326:   fprintf(g_cgi,"enter print_quizz() %s, mode:%d\n",class_fullpath,mode); fflush(g_cgi);
                   1327: #endif /* CGI_DBUG */ 
                   1328: 
                   1329:   /* get configuration options */
                   1330:   configResult=read_capa_config("num_questions_per_page",buf);
                   1331:   if (configResult != 0 && configResult != -1 ) {
                   1332:     if (sscanf(buf,"%d",&g_num_questions_per_page)==0) {
                   1333:       g_num_questions_per_page=ALL_QUESTIONS;
                   1334:     }
                   1335:   } else {
                   1336:     g_num_questions_per_page=ALL_QUESTIONS;
                   1337:   }
                   1338: 
                   1339:   view_assignments_after_due=capa_check_option(OPTION_VIEW_PROBLEMS_AFTER_DUE,
                   1340: 					       set,g_student_data.s_sec);
                   1341:   if (view_assignments_after_due < 0 ) view_assignments_after_due=1;
                   1342:   g_inhibit_response=capa_check_option(OPTION_INHIBIT_RESPONSE,set,g_student_data.s_sec);
                   1343:   if (g_inhibit_response < 0 ) g_inhibit_response=0;
                   1344: 
                   1345: #ifdef CGI_DBUG
                   1346:   fprintf(g_cgi,"Set %d, Section%d, ViewAssign? %d, Inhibit Resp? %d\n",set,
                   1347: 	  g_student_data.s_sec,view_assignments_after_due,
                   1348: 	  g_inhibit_response); 
                   1349:   fflush(g_cgi);
                   1350: #endif /* CGI_DBUG */ 
                   1351: 
                   1352:   time(&curtime);
                   1353:   offset=capa_get_entry(&entry,sn,set);          /* <-------- capa*() call  ---- */
                   1354:   if( mode == VIEW_PREVIOUS_MODE ) {
                   1355:     if( view_assignment_after_due ) {
                   1356:       if( capa_check_date(CHECK_OPEN_DATE,g_student_number,
                   1357: 			  g_student_data.s_sec,set) < 0 ) {
                   1358: 	append_qtext("This set is not yet open.\n");
                   1359: 	return ;
                   1360:       }
                   1361:     } else {
                   1362:       if( (capa_check_date(CHECK_ANS_DATE,g_student_number,
                   1363: 			   g_student_data.s_sec,set) < 0) &&
                   1364: 	  (capa_check_date(CHECK_DUE_DATE,g_student_number,
                   1365: 			   g_student_data.s_sec,set) > 0) ) {
                   1366: 	append_qtext("This set is not yet available to be viewed.\n");
                   1367: 	return ;
                   1368:       }
                   1369:     }
                   1370:     if( capa_check_date(CHECK_ANS_DATE,g_student_number,
                   1371: 			g_student_data.s_sec,set) < 0 ) {
                   1372:       display_ans = 0;
                   1373:     }
                   1374:   } 
                   1375:   g_passdue = 0;
                   1376:   if( mode == CHECK_ANSWER_MODE || 
                   1377:       ( (!view_assignment_after_due) && mode == TRY_SET_MODE)) {
                   1378:     if( capa_check_date(CHECK_DUE_DATE,g_student_number,
                   1379: 			g_student_data.s_sec,set) > 0 ) {
                   1380:        capa_get_date(CHECK_DUE_DATE,g_student_number,
                   1381: 		     g_student_data.s_sec,set,date_str);
                   1382:        sprintf(buf,"SORRY, the due date was: %s\n",date_str);
                   1383:        append_qtext(buf);
                   1384:        g_passdue = 1;
                   1385:     }
                   1386:   }
                   1387: 
                   1388:   if ((mode==CHECK_ANSWER_MODE) || (mode== TRY_SET_MODE))
                   1389:     capa_set_login_time(g_student_number,set);
                   1390: 
                   1391:   capa_get_header(&header,set);
                   1392: 
                   1393:   sscanf(header.num_questions,"%d",&question_cnt);
                   1394:   print_page_header(mode,question_cnt);
                   1395: 
                   1396: #ifdef CGI_DBUG
                   1397:   fprintf(g_cgi,"DONE page header\n"); fflush(g_cgi);
                   1398: #endif /* CGI_DBUG */
                   1399: 
                   1400:   if(offset < 0 ) offset = - offset;
                   1401:   
                   1402:   Parsemode_f = HTML_MODE; /* WEB_MODE */
                   1403:   result = capa_parse(set, &first_prob, sn, &question_cnt,NULL);  /* <-- capa*() call */
                   1404: 
                   1405: #ifdef CGI_DBUG
                   1406:   fprintf(g_cgi,"DONE capa_parse() [%d], pass due=%d\n",result,g_passdue); fflush(g_cgi);
                   1407: #endif /* CGI_DBUG */
                   1408: 
                   1409:   if (StartText_p) printf(StartText_p);
                   1410:  
                   1411: #ifdef CGI_DBUG
                   1412:   fprintf(g_cgi,"DONE Start Text\n"); fflush(g_cgi);
                   1413: #endif /* CGI_DBUG */
                   1414: 
                   1415:   if ( result != 0 ) {
                   1416:      if( !g_passdue ) {
                   1417:        append_qtext("<FORM method=\"post\" ");
                   1418:        sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,
                   1419: 	       g_cgibin_path,c_owner);
                   1420:        append_qtext(buf);
                   1421:        sprintf(buf,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",class); append_qtext(buf);
                   1422:        sprintf(buf,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",sn);          append_qtext(buf);
                   1423:        sprintf(buf,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",pin);       append_qtext(buf);
                   1424:        sprintf(buf,"<input type=\"hidden\" name=\"SET\" value=\"%d\">\n",set);          append_qtext(buf);
                   1425:        sprintf(buf,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_CHECKANS); append_qtext(buf);
                   1426:        sprintf(buf,"<input type=\"hidden\" name=\"STARTNUM\" value=\"%d\">\n",g_start_question); append_qtext(buf);
                   1427:        append_qtext("\n<OL>\n");
                   1428:      }
                   1429: 
                   1430:      for(idx=0;idx<question_cnt;idx++) {         /* prepare log string and new database entry */
                   1431:        g_new_answerdb[idx] = entry.answers[idx];
                   1432:        g_log_string[idx]   = '-';
                   1433:        sscanf(entry.tries + 3*idx,"%d,",&(g_tried[idx]) );
                   1434:      }
                   1435:      g_new_answerdb[question_cnt]=0;  g_log_string[question_cnt]=0;
                   1436:      prob_idx = first_prob;
                   1437:      for( question_idx = 0; question_idx < question_cnt; 
                   1438: 	  question_idx++,prob_idx = prob_idx->next ) {
                   1439: #ifdef CGI_DBUG
                   1440:   fprintf(g_cgi,"quetion_idx: %d, g_start_question:%d, g_num_que: %d\n",
                   1441: 	  question_idx,g_start_question,g_num_questions_per_page); fflush(g_cgi);
                   1442: #endif /* CGI_DBUG */        
                   1443:          if ((question_idx < g_start_question-1) 
                   1444: 	     ||
                   1445: 	     (((!(g_num_questions_per_page == ALL_QUESTIONS)) 
                   1446: 	       && 
                   1447: 	       (question_idx >= (g_start_question+g_num_questions_per_page-1))))) {
                   1448: 	   preserve_last_answer(question_idx,0);
                   1449: 	   continue;
                   1450: 	 }
                   1451:          if( !g_passdue ) {
                   1452:            sprintf(buf,"<A NAME=\"P%d\"></A>",question_idx+1);  append_qtext(buf);
                   1453: 	   /*	   if (!((question_idx == (g_start_question-1)) || (question_idx == 0))) {
                   1454: 	     append_qtext("<A href=\"#TOP\">Top</A>");
                   1455: 	     sprintf(buf,"&nbsp;&nbsp;<A href=\"#P%d\">Next</A>",question_idx+2);  append_qtext(buf);
                   1456: 	     }*/
                   1457:            q_leng = strlen(prob_idx->question);
                   1458: 	   if ( !prob_idx->show_br ) {
                   1459: 	     append_qtext(prob_idx->question);
                   1460: 	   } else {
                   1461: 	     for(idx=0;idx<q_leng;idx++) {
                   1462: 	       if ( g_qchar_cnt+2 >= g_qsize ) {
                   1463: 		 char *temp_text;
                   1464: 		 g_qsize=g_qchar_cnt*2;
                   1465: 		 temp_text=capa_malloc(g_qsize,sizeof(char));
                   1466: 		 strncpy(temp_text,g_question_txt,g_qsize);
                   1467: 		 capa_mfree(g_question_txt);
                   1468: 		 g_question_txt=temp_text;		 
                   1469: 	       }
                   1470: 	       g_question_txt[g_qchar_cnt]=prob_idx->question[idx];
                   1471: 	       g_qchar_cnt++;
                   1472: 	       if(prob_idx->question[idx] == '\n' ) {
                   1473: 		 append_qtext("<br>\n");
                   1474: 	       }
                   1475: 	     }
                   1476: 	   }
                   1477:          }
                   1478:          if(mode == VIEW_PREVIOUS_MODE) { /* VIEW_PREVIOUS_MODE */
                   1479:            if( display_ans ) {
                   1480:              if( prob_idx->ans_type == ANSWER_IS_FLOAT ) {
                   1481:                  a = (double)atof(prob_idx->answer);
                   1482:                  sprintf(cmp_ans,prob_idx->ans_fmt, a);
                   1483:              } else {
                   1484:                  strcpy(cmp_ans,prob_idx->answer);
                   1485:              }
                   1486:              if( prob_idx->ans_unit ) {
                   1487:                  sprintf(buf,"<p><tt><b>Answer:</b> %s %s</tt><br>\n",cmp_ans, prob_idx->unit_str); 
                   1488:              } else {
                   1489:                  sprintf(buf,"<p><tt><b>Answer:</b> %s</tt><br>\n",cmp_ans); 
                   1490:              }
                   1491:              append_qtext(buf);
                   1492: 	     if ( prob_idx->explain) {
                   1493: 	       sprintf(buf,"<p><b>Explanation: </b>\n<p>%s<br>\n",prob_idx->explain);
                   1494: 	       append_qtext(buf);
                   1495: 	     }
                   1496:            }
                   1497:          } else { /* could be TRY_SET_MODE, CHECK_ANSWER_MODE */
                   1498:            
                   1499:            if( g_passdue ) {
                   1500:              get_response(header.partial_credit[question_idx],entry.answers[question_idx],question_idx,prob_idx);
                   1501:            }else{
                   1502: 	     if (g_inhibit_response) {
                   1503: 	       print_inhibited_response(header.partial_credit[question_idx],entry.answers[question_idx],question_idx,prob_idx);
                   1504: 	     } else {
                   1505: 	       print_response(header.partial_credit[question_idx],entry.answers[question_idx],question_idx,prob_idx); 
                   1506: 	     }
                   1507:              append_qtext("<br>\n");
                   1508:              if( (g_tried[question_idx] >= prob_idx->show_hint) || 
                   1509: 		 (entry.answers[question_idx]=='Y') ||
                   1510: 		 (entry.answers[question_idx]=='y')) {
                   1511:                if( prob_idx->hint ) {
                   1512:                  sprintf(buf,"<p><B>Hint: </B>%s\n<br>\n", prob_idx->hint);
                   1513:                  append_qtext(buf);
                   1514:                }
                   1515:              }
                   1516:            }
                   1517:          }
                   1518:      }   /* ------------------------------------- end displaying each problem */
                   1519:      append_qtext("\n</OL>\n");
                   1520:      if( EndText_p )   append_qtext(EndText_p);
                   1521:      free_problems(first_prob);
                   1522: 
                   1523: #ifdef CGI_DBUG
                   1524:   fprintf(g_cgi,"End display each problem\n"); fflush(g_cgi);
                   1525: #endif /* CGI_DBUG */
                   1526: 
                   1527:      if( mode == CHECK_ANSWER_MODE ) {  /* update the record database */
                   1528:        if( !g_passdue ) {
                   1529:          for(idx=0;idx<question_cnt;idx++){
                   1530:            if( g_new_answerdb[idx] != entry.answers[idx]) { 
                   1531:              entry.answers[idx] = g_new_answerdb[idx];
                   1532:            }
                   1533:            if(g_tried[idx] < 10 ) {
                   1534:              entry.tries[3*idx]   = ' ';
                   1535:              entry.tries[3*idx+1] = g_tried[idx] + '0';
                   1536:              if(idx < question_cnt-1)  entry.tries[3*idx+2] = ',';
                   1537:            } else {
                   1538:              entry.tries[3*idx]   = (int)(g_tried[idx]/10) + '0';
                   1539:              entry.tries[3*idx+1] = (g_tried[idx] % 10) + '0';
                   1540:              if(idx < question_cnt-1)  entry.tries[3*idx+2] = ',';
                   1541:            }
                   1542:          }
                   1543:          capa_set_entry(&entry,sn,set,offset);                     /* <-------- capa*() call */
                   1544: 
                   1545: #ifdef CGI_DBUG
                   1546: 	 fprintf(g_cgi,"DONE set db entry\n"); fflush(g_cgi);
                   1547: #endif /* CGI_DBUG */
                   1548: 
                   1549: 	 create_status_line(mode,question_cnt,&entry);
                   1550:        } 
                   1551:        if (w_log_attempt(g_student_number,set,g_log_string) == -1 ) {
                   1552: 	 fprintf(stdout,"<BOLD>Unable to log attempt. Please notify instructor.</BOLD>\n");
                   1553:        }
                   1554:      }
                   1555: #ifdef CGI_DBUG
                   1556:      fprintf(g_cgi,"DONE check answer mode\n"); fflush(g_cgi);
                   1557: #endif /* CGI_DBUG */
                   1558:      
                   1559:      if( (mode == TRY_SET_MODE && !g_passdue)  || 
                   1560: 	 mode == VIEW_PREVIOUS_MODE) {
                   1561:        create_status_line(mode,question_cnt,&entry);
                   1562:      }
                   1563:      
                   1564:      if( !g_passdue ) {
                   1565:        sprintf(buf,"</ul></form>\n"); append_qtext(buf);
                   1566:      }
                   1567:   }
                   1568: }
                   1569: 
                   1570: void
                   1571: get_response(char pcr,char u_db,int q_idx,Problem_t *p)
                   1572: {
                   1573:   if( pcr == '0' || p->ans_type==ANSWER_IS_SUBJECTIVE ) { /* not hand-graded question */
                   1574:      switch(u_db) {  /* what's from the user record */
                   1575:        case 'Y': break;
                   1576:        case 'y': break;
                   1577:        case '-':
                   1578:                if(g_stu_ans_pp[q_idx+1] != NULL ) log_user_ans(q_idx,p);
                   1579:                break;
                   1580:        case 'E':
                   1581:        case 'e': break;
                   1582:        case 'n': break;
                   1583:        case 'N':
                   1584:        case '0': case '1': case '2': case '3': case '4': 
                   1585:        case '5': case '6': case '7': case '8': case '9':
                   1586:                if(g_stu_ans_pp[q_idx+1] != NULL ) log_user_ans(q_idx,p);
                   1587:                break;
                   1588:        default:
                   1589: 	      break;
                   1590:      }
                   1591:    }
                   1592: }
                   1593: 
                   1594: void display_last_answer(int q_idx)
                   1595: {
                   1596:   char       buf[MAX_BUFFER_SIZE];  
                   1597:   StudentAnswer_t  *sa_p;
                   1598: #ifdef CGI_DBUG
                   1599:   fprintf(g_cgi,"Enter display_last_answer() [%d]\n",q_idx); fflush(g_cgi);
                   1600: #endif /* CGI_DBUG */
                   1601:   if(g_stu_ans_pp[q_idx+1] != NULL) {
                   1602:     sa_p=g_stu_ans_pp[q_idx+1];
                   1603:   } else {
                   1604:     if (g_last_ans_pp[q_idx+1] != NULL) {
                   1605:       sa_p=g_last_ans_pp[q_idx+1];
                   1606:     } else {
                   1607:       return;
                   1608:     }
                   1609:   }
                   1610:   if (sa_p->a_next == NULL) {
                   1611:     sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d\" value=\"%s\">\n",
                   1612: 	    q_idx+1,sa_p->a_str);
                   1613:     append_qtext(buf);
                   1614:     sprintf(buf," <b>Last Answer:</b> %s\n",sa_p->a_str);
                   1615:     append_qtext(buf);
                   1616:   } else {
                   1617:     while(sa_p) {
                   1618:       sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d,%02d\" value=\"%s\">\n",
                   1619: 	      q_idx+1,sa_p->a_idx,sa_p->a_str);
                   1620:       append_qtext(buf);
                   1621:       sprintf(buf," <b>Last Answer %d:</b> %s\n",sa_p->a_idx,sa_p->a_str);
                   1622:       append_qtext(buf);
                   1623:       sa_p=sa_p->a_next;
                   1624:     }
                   1625:   }
                   1626: }
                   1627: 
                   1628: void preserve_last_answer(int q_idx,int print)
                   1629: {
                   1630:   char       buf[MAX_BUFFER_SIZE];  
                   1631:   StudentAnswer_t  *sa_p;
                   1632: #ifdef CGI_DBUG
                   1633:   fprintf(g_cgi,"Enter preserve_last_answer() [%d]\n",q_idx); fflush(g_cgi);
                   1634: #endif /* CGI_DBUG */
                   1635:   if(g_stu_ans_pp[q_idx+1] != NULL) {
                   1636:     sa_p=g_stu_ans_pp[q_idx+1];
                   1637:   } else {
                   1638:     if (g_last_ans_pp[q_idx+1] != NULL) {
                   1639:       sa_p=g_last_ans_pp[q_idx+1];
                   1640:     } else {
                   1641:       return;
                   1642:     }
                   1643:   }
                   1644:   if (sa_p->a_next == NULL) {
                   1645:     sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d\" value=\"%s\">\n",
                   1646: 	    q_idx+1,sa_p->a_str);
                   1647:     if (print) printf(buf); else append_qtext(buf);
                   1648:   } else {
                   1649:     while(sa_p) {
                   1650:       sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d,%02d\" value=\"%s\">\n",
                   1651: 	      q_idx+1,sa_p->a_idx,sa_p->a_str);
                   1652:       if (print) printf(buf); else append_qtext(buf);
                   1653:       sa_p=sa_p->a_next;
                   1654:     }
                   1655:   }
                   1656: }
                   1657: 
                   1658: void display_last_subjective(int q_idx)
                   1659: {
                   1660:   char      *buf;
                   1661:   char      *answer;
                   1662: #ifdef CGI_DBUG
                   1663:   fprintf(g_cgi,"Enter display_last_subjective() [%d]\n",q_idx); fflush(g_cgi);
                   1664: #endif /* CGI_DBUG */
                   1665:   answer=capa_get_subjective(g_login_set,q_idx+1,g_student_number);
                   1666:   if (answer==NULL) return;
                   1667: #ifdef CGI_DBUG
                   1668:   fprintf(g_cgi,"Found answer %s\n",answer); fflush(g_cgi);
                   1669: #endif /* CGI_DBUG */
                   1670:   buf=capa_malloc(MAX_BUFFER_SIZE+strlen(answer),1);
                   1671:   /* don't need to stick in a last since we always get it from the files */
                   1672:   /*  sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d\" value=\"%s\">\n",q_idx+1,answer);*/
                   1673:   append_qtext(buf);
                   1674:   append_qtext("<b>Current Submission:</b><br>\n");
                   1675:   append_qtext("<TABLE BORDER=1 CELLSPACING=0>\n<TR><TD>\n");
                   1676:   append_qtext("<PRE>");
                   1677:   append_qtext(answer);
                   1678:   append_qtext("</PRE>");
                   1679:   append_qtext("</TD></TR></TABLE>\n");
                   1680:   capa_mfree(buf);
                   1681:   capa_mfree(answer);
                   1682: }
                   1683: 
                   1684: void create_answer_area(Problem_t *p,int q_idx) 
                   1685: {
                   1686:   int ii;
                   1687:   char       buf[MAX_BUFFER_SIZE];  
                   1688: 
                   1689: #ifdef CGI_DBUG
                   1690:   fprintf(g_cgi,"Enter create_answer_area() [%d]\n",q_idx); fflush(g_cgi);
                   1691: #endif /* CGI_DBUG */
                   1692: 
                   1693:   if ( p->ans_type==ANSWER_IS_SUBJECTIVE ) {
                   1694:     display_last_subjective(q_idx);
                   1695:   }
                   1696:   if ( p->show_ans_box ) { 
                   1697:     if ( p->ans_op == ANS_AND ) {
                   1698:       for(ii=0;ii<p->ans_cnt;ii++) {
                   1699: 	if (p->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: int gather_answers(char ***ans,int q_idx,Problem_t *p)
                   1891: {
                   1892:   int cnt;
                   1893:   if(p->ans_op==ANS_AND) {
                   1894:     int i; StudentAnswer_t *sa_p;
                   1895:     *ans=(char**)capa_malloc(p->ans_cnt,1);
                   1896:     sa_p= g_stu_ans_pp[q_idx+1];
                   1897:     for(i=0;((i<p->ans_cnt)&&(sa_p));i++){
                   1898:       ans[0][i]=sa_p->a_str;
                   1899:       sa_p=sa_p->a_next;
                   1900:     }
                   1901:     cnt=p->ans_cnt;
                   1902:     if (i<p->ans_cnt) return -1;
                   1903:   } else {
                   1904:     *ans=(char**)capa_malloc(p->ans_cnt,1);
                   1905:     ans[0][0]=g_stu_ans_pp[q_idx+1]->a_str;
                   1906:     cnt=1;
                   1907:   }
                   1908:   return cnt;
                   1909: }
                   1910: 
                   1911: void
                   1912: log_user_ans(int q_idx,Problem_t *p)
                   1913: {
                   1914:   char **ans;
                   1915:   int cnt;
                   1916:   if (p->ans_type==ANSWER_IS_SUBJECTIVE) {
                   1917:     capa_set_subjective(g_login_set,q_idx+1,g_student_number,
                   1918: 			g_stu_ans_pp[q_idx+1]->a_str);
                   1919:   } else {
                   1920:     if (-1 != (cnt=gather_answers(&ans,q_idx,p))) {
                   1921:       switch( capa_check_answers(p,ans,cnt) ) {
                   1922:         case  EXACT_ANS:  g_log_string[q_idx]='Y'; break;
                   1923:         case  APPROX_ANS: g_log_string[q_idx]='Y'; break;
                   1924:         case  SIG_FAIL:   g_log_string[q_idx]='S'; break;
                   1925:         case  UNIT_FAIL:  g_log_string[q_idx]='U'; break;
                   1926:         case  UNIT_NOTNEEDED:  g_log_string[q_idx]='U'; break;
                   1927:         case  NO_UNIT:    g_log_string[q_idx]='u'; break;
                   1928:         case  BAD_FORMULA:  g_log_string[q_idx]='F'; break;
                   1929:         case  INCORRECT:  g_log_string[q_idx]='N'; break;
                   1930:       }
                   1931:     }
                   1932:   }
                   1933: }
                   1934: 
                   1935: void submit_subjective(int q_idx,Problem_t *p)
                   1936: {
                   1937:   char buf[MAX_BUFFER_SIZE];
                   1938:   if (capa_set_subjective(g_login_set,q_idx+1,g_student_number,
                   1939: 			  g_stu_ans_pp[q_idx+1]->a_str)<0){
                   1940:     sprintf(buf,"<p><tt>Falied to record last submission.</tt><br>\n");
                   1941:     g_tried[q_idx]--;
                   1942:   } else {
                   1943:     sprintf(buf,"<p><tt>Current submission recorded.</tt><br>\n");
                   1944:     g_new_answerdb[q_idx] = '0';
                   1945:     g_log_string[q_idx]='A';
                   1946:   }
                   1947:   append_qtext(buf);
                   1948:   if (g_tried[q_idx]<p->tries) {
                   1949:     create_answer_area(p,q_idx);
                   1950:     if( p->tries - g_tried[q_idx] == 1) {
                   1951:       append_qtext("<br><tt>ONE try left</tt>\n");
                   1952:     }else{
                   1953:       sprintf(buf,"<br><tt>tries %d/%d</tt>\n",g_tried[q_idx],p->tries);
                   1954:       append_qtext(buf);
                   1955:     }  
                   1956:   }else{
                   1957:     display_last_answer(q_idx);
                   1958:     append_qtext("<br><tt>No more tries.</tt>\n");
                   1959:   } 
                   1960: }
                   1961: 
                   1962: void
                   1963: check_user_ans(int q_idx,Problem_t *p)
                   1964: {
                   1965: int a_tpe,cnt;
                   1966: char *c_ans,**ans;
                   1967: int   t_tpe;
                   1968: double tol;
                   1969: int    sig_l;
                   1970: int    sig_u;
                   1971: char  *a_fmt;
                   1972: int    tries;
                   1973:   char       buf[MAX_BUFFER_SIZE];
                   1974:   
                   1975:   a_tpe = p->ans_type;
                   1976:   t_tpe = p->tol_type;
                   1977:   tol   = p->tolerance;
                   1978:   sig_l = p->sig_lbound;
                   1979:   sig_u = p->sig_ubound;
                   1980:   a_fmt = p->ans_fmt;
                   1981:   tries = p->tries;
                   1982: 
                   1983: #ifdef CGI_DBUG
                   1984:   fprintf(g_cgi,"Enter check_user_ans() anstype=%d\n",a_tpe); fflush(g_cgi);
                   1985: #endif /* CGI_DBUG */
                   1986: 
                   1987:   g_tried[q_idx]++;
                   1988: 
                   1989: #ifdef CGI_DBUG
                   1990:   fprintf(g_cgi,"Call capa_check_answer() with [%s]\n",g_stu_ans_pp[q_idx+1]->a_str); fflush(g_cgi);
                   1991: #endif /* CGI_DBUG */
                   1992:   if (a_tpe==ANSWER_IS_SUBJECTIVE) {
                   1993:     submit_subjective(q_idx,p);
                   1994:     return;
                   1995:   }
                   1996: 
                   1997:   cnt=gather_answers(&ans,q_idx,p);
                   1998:   if (cnt == -1) {
                   1999:     g_tried[q_idx]--;
                   2000:     create_answer_area(p,q_idx);
                   2001:     if( (tries - g_tried[q_idx]) == 1 ) {
                   2002:       append_qtext("<br><tt>Not all answers submitted, ONE try left!!</tt>\n");
                   2003:     }else{
                   2004:       sprintf(buf,"<br><tt>Not all answers submitted, tries %d/%d.</tt>\n",
                   2005: 	      g_tried[q_idx],tries);
                   2006:       append_qtext(buf);
                   2007:     }
                   2008:     return;
                   2009:   }
                   2010: 
                   2011:   switch( capa_check_answers(p,ans,cnt) ) {
                   2012:     case  EXACT_ANS:    
                   2013:     case  APPROX_ANS: 
                   2014:                    c_ans=answers_string(ANSWER_STRING_MODE, p);
                   2015:                    sprintf(buf,"<p><tt>Yes, Computer gets: %s</tt>\n", c_ans);
                   2016:                    append_qtext(buf);
                   2017:                    g_new_answerdb[q_idx] = 'Y';
                   2018:                    g_log_string[q_idx]='Y';
                   2019: 		   capa_mfree(c_ans);
                   2020: 		   break;
                   2021:     case  SIG_FAIL:
                   2022:                    create_answer_area(p,q_idx);
                   2023: 		   g_tried[q_idx]--;  /* don't count as a try */
                   2024: 		   sprintf(buf,"<br><tt>Please adjust significant figures, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
                   2025: 		   append_qtext(buf);
                   2026: 		   g_new_answerdb[q_idx] = 'N';
                   2027:                    g_log_string[q_idx]='S';
                   2028:                    break;
                   2029:     case  UNIT_FAIL:
                   2030:                    create_answer_area(p,q_idx);
                   2031:                    g_tried[q_idx]--;  /* don't count as a try */
                   2032: 		   sprintf(buf,"<br><tt>Units incorrect, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
                   2033: 		   append_qtext(buf);
                   2034: 		   g_new_answerdb[q_idx] = 'N';
                   2035:                    g_log_string[q_idx]='U';
                   2036:                    break;
                   2037:     case  UNIT_NOTNEEDED:
                   2038:                    create_answer_area(p,q_idx);
                   2039:                    g_tried[q_idx]--;  /* don't count as a try */
                   2040:                    if(tries > 0) {
                   2041:                      sprintf(buf,"<br><tt>Only a number required, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
                   2042:                      append_qtext(buf);
                   2043:                    }
                   2044:                    g_new_answerdb[q_idx] = 'N';
                   2045:                    g_log_string[q_idx]='U';
                   2046:                    break;
                   2047:     case  NO_UNIT: 
                   2048:                    create_answer_area(p,q_idx);
                   2049:                    g_tried[q_idx]--;  /* don't count as a try */
                   2050: 		   sprintf(buf,"<br><tt>Units required, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
                   2051: 		   append_qtext(buf);
                   2052: 		   g_new_answerdb[q_idx] = 'N';
                   2053:                    g_log_string[q_idx]='u';
                   2054:                    break;
                   2055:     case  BAD_FORMULA: 
                   2056:                    create_answer_area(p,q_idx);
                   2057:                    g_tried[q_idx]--;  /* don't count as a try */
                   2058: 		   sprintf(buf,"<br><tt>Unable to understand formula, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
                   2059: 		   append_qtext(buf);
                   2060: 		   g_new_answerdb[q_idx] = 'N';
                   2061:                    g_log_string[q_idx]='F';
                   2062:                    break;
                   2063:     case  INCORRECT:
                   2064:                    if( g_tried[q_idx] < tries ) {
                   2065: 		     create_answer_area(p,q_idx);
                   2066: 		     if( (tries - g_tried[q_idx]) == 1 ) {
                   2067: 		       append_qtext("<br><tt>Incorrect, ONE try left!!</tt>\n");
                   2068: 		     }else{
                   2069: 		       sprintf(buf,"<br><tt>Incorrect, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
                   2070: 		       append_qtext(buf);
                   2071: 		     }
                   2072: 		   } else {
                   2073: 		     display_last_answer(q_idx);
                   2074: 		     append_qtext("<br><tt>Incorrect, no more tries.</tt>\n");
                   2075: 		   }
                   2076:                    g_new_answerdb[q_idx] = 'N';
                   2077:                    g_log_string[q_idx]='N';
                   2078:                    break;
                   2079:   }
                   2080: }
                   2081: 
                   2082: void
                   2083: check_inhibited_user_ans(int q_idx,Problem_t *p)
                   2084: {
                   2085: int a_tpe,cnt;
                   2086: char *c_ans,**ans;
                   2087: int   t_tpe;
                   2088: double tol;
                   2089: int    sig_l;
                   2090: int    sig_u;
                   2091: char  *a_fmt;
                   2092: int    tries;
                   2093:   char       buf[MAX_BUFFER_SIZE];
                   2094:   
                   2095:   a_tpe = p->ans_type;
                   2096:   c_ans = p->answer;
                   2097:   t_tpe = p->tol_type;
                   2098:   tol   = p->tolerance;
                   2099:   sig_l = p->sig_lbound;
                   2100:   sig_u = p->sig_ubound;
                   2101:   a_fmt = p->ans_fmt;
                   2102:   tries = p->tries;
                   2103:   
                   2104: #ifdef CGI_DBUG
                   2105:   fprintf(g_cgi,"Enter check_inhibited_user_ans() anstype=%d\n",a_tpe); fflush(g_cgi);
                   2106: #endif /* CGI_DBUG */
                   2107: 
                   2108:   g_tried[q_idx]++;
                   2109: 
                   2110: #ifdef CGI_DBUG
                   2111:   fprintf(g_cgi,"Call capa_check_answer() with [%s]\n",g_stu_ans_pp[q_idx+1]->a_str); 
                   2112:   fflush(g_cgi);
                   2113: #endif /* CGI_DBUG */
                   2114:   if (a_tpe==ANSWER_IS_SUBJECTIVE) {
                   2115:     submit_subjective(q_idx,p);
                   2116:     return;
                   2117:   }
                   2118: 
                   2119:   cnt=gather_answers(&ans,q_idx,p);
                   2120:   if (cnt == -1) {
                   2121:     g_tried[q_idx]--;
                   2122:     create_answer_area(p,q_idx);
                   2123:     if( (tries - g_tried[q_idx]) == 1 ) {
                   2124:       append_qtext("<br><tt>Not all answers submitted, ONE try left!!</tt>\n");
                   2125:     }else{
                   2126:       sprintf(buf,"<br><tt>Not all answers submitted, tries %d/%d.</tt>\n",
                   2127: 	      g_tried[q_idx],tries);
                   2128:       append_qtext(buf);
                   2129:     }
                   2130:     return;
                   2131:   }
                   2132: 
                   2133:   switch( capa_check_answers(p,ans,cnt) ) {
                   2134:     case  EXACT_ANS:
                   2135:     case  APPROX_ANS: 
                   2136:                    g_new_answerdb[q_idx] = 'Y';
                   2137:                    g_log_string[q_idx]='Y';
                   2138:                    break;
                   2139:     case  SIG_FAIL:
                   2140:                    g_new_answerdb[q_idx] = 'N';
                   2141:                    g_log_string[q_idx]='S';
                   2142:                    break;
                   2143:     case  UNIT_FAIL:
                   2144:                    g_new_answerdb[q_idx] = 'N';
                   2145:                    g_log_string[q_idx]='U';
                   2146:                    break;
                   2147:     case  UNIT_NOTNEEDED:
                   2148:                    g_new_answerdb[q_idx] = 'N';
                   2149:                    g_log_string[q_idx]='U';
                   2150:                    break;
                   2151:     case  NO_UNIT:
                   2152:                    g_new_answerdb[q_idx] = 'N';
                   2153:                    g_log_string[q_idx]='u';
                   2154:                    break;
                   2155:     case  BAD_FORMULA:
                   2156:                    g_new_answerdb[q_idx] = 'N';
                   2157:                    g_log_string[q_idx]='F';
                   2158:                    break;
                   2159:     case  INCORRECT:
                   2160:                    g_new_answerdb[q_idx] = 'N';
                   2161:                    g_log_string[q_idx]='N';
                   2162:                    break;
                   2163:   }
                   2164: 
                   2165:   if( g_tried[q_idx] < tries ) {
                   2166:     create_answer_area(p,q_idx);
                   2167:     if( (tries - g_tried[q_idx]) == 1 ) {
                   2168:       append_qtext("<br><tt>Answered, ONE try left!!</tt>\n");
                   2169:     }else{
                   2170:       sprintf(buf,"<br><tt>Answered, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
                   2171:       append_qtext(buf);
                   2172:     }
                   2173:   } else {
                   2174:     display_last_answer(q_idx);
                   2175:     append_qtext("<br><tt>Answered, no more tries.</tt>\n");
                   2176:   }
                   2177: }
                   2178: 
                   2179: void                                     /* RETURNS: (nothing)          */
                   2180: print_summary(class_dir,class,student_number,pin,set)
                   2181: char *class_dir;char *class;char *student_number;int pin;int set;
                   2182: {                                       /* LOCAL VARIABLES:            */
                   2183:   int      set_idx,                     /*    Set counter              */
                   2184:            i,                           /*    Question counter         */
                   2185:            set_score,                   /*    Score on a set           */
                   2186:            term_score=0,                /*    Total points received    */
                   2187:            term_valid=0,                /*    Total points possible    */
                   2188:            result;
                   2189:   T_entry  entry;                       /*    Database entry for a set */
                   2190:   char     buf[MAX_BUFFER_SIZE]; /* Output line buffer  */
                   2191:   char     buf2[MAX_BUFFER_SIZE]; /* Output line buffer  */
                   2192:   T_header header;                      /*    Problem set header       */
                   2193:   int      question_cnt,valid_wgt, rate,configResult,
                   2194:     status_line_length=DEFAULT_STATUS_LINE_LENGTH,row;
                   2195:   char     class_fullpath[ONE_K],*serverName;
                   2196: 
                   2197:   serverName=getenv("SERVER_NAME");
                   2198:   if (!serverName) {
                   2199:     fprintf(stdout,"Enviroment variable SERVER_NAME not set.\n");
                   2200:     fprintf(stdout,"Unable to complete actions.\n");
                   2201:     return;
                   2202:   }
                   2203: 
                   2204:   sprintf(class_fullpath,"%s/%s",class_dir,class);
                   2205:   chdir(class_fullpath);
                   2206:   configResult=read_capa_config("web_status_line_length",buf);
                   2207:   if (configResult != 0 && configResult != -1 ) {
                   2208:     if (sscanf(buf,"%d",&status_line_length)==0) {
                   2209:       status_line_length=DEFAULT_STATUS_LINE_LENGTH;
                   2210:     }
                   2211:   } else {
                   2212:     status_line_length=DEFAULT_STATUS_LINE_LENGTH;
                   2213:   }
                   2214:   
                   2215:   printf("<TABLE>\n<TR><TD></TD>\n");
                   2216:   for(i=0;i<status_line_length;i++) {
                   2217:     printf("<TD align=center valign=bottom>%d</TD>\n",i+1);
                   2218:   }
                   2219:   printf("</TR>");
                   2220: 
                   2221:   for (set_idx=1; set_idx<=set; set_idx++) {
                   2222:     g_inhibit_response=capa_check_option(OPTION_INHIBIT_RESPONSE,set_idx,
                   2223: 					 g_student_data.s_sec);
                   2224:     if (g_inhibit_response > 0) continue;
                   2225: 
                   2226:     if (capa_get_header(&header,set_idx))  return;
                   2227:     capa_get_entry(&entry,student_number,set_idx);
                   2228:     sscanf(header.num_questions,"%d", &(question_cnt) );
                   2229:     valid_wgt = 0; set_score = 0;
                   2230:     header.weight[question_cnt] = '\0';
                   2231:     header.partial_credit[question_cnt] = '\0';
                   2232:     for (i=0; i<question_cnt; i++) {
                   2233:       valid_wgt +=  (header.weight[i] - '0');
                   2234:       if((entry.answers[i]=='Y') || (entry.answers[i]=='y'))  
                   2235: 	set_score += (header.weight[i]-'0');
                   2236:       if((entry.answers[i]=='E') || (entry.answers[i]=='e'))  
                   2237: 	valid_wgt -= (header.weight[i] - '0');
                   2238:       if((entry.answers[i]>='0') && (entry.answers[i]<='9'))  
                   2239: 	set_score += (entry.answers[i] - '0');
                   2240:     }
                   2241:     term_valid += valid_wgt;
                   2242:     term_score += set_score;
                   2243: 
                   2244:     if( valid_wgt != 0 ) {
                   2245:       rate = 100*set_score / valid_wgt;
                   2246:       printf("<TR><TD nowrap align=center valign=bottom>set <B>%d</B>, %d/%d(%d %%)  </TD>",set_idx,set_score,valid_wgt,rate);
                   2247:     } else {
                   2248:       printf("<TR><TD nowrap align=center valign=bottom>set <B>%d</B>,   0/0(0 %%)   </TD>",set_idx);
                   2249:     }
                   2250:     for(row=0;row<=(question_cnt/status_line_length);row++) {
                   2251:       for(i=(row*status_line_length);
                   2252: 	  ((i<question_cnt)&&(i<((row+1)*status_line_length))); i++) {
                   2253: 	if (i != 0 && (!(i%status_line_length))) { printf("</TR><TD></TD>"); }
                   2254: 	printf("<TD align=center valign=bottom><tt>%c</tt></TD>\n",entry.answers[i]);
                   2255:       }
                   2256:       printf("</TR>\n<TR><TD></TD>");
                   2257:       for(i=(row*status_line_length);
                   2258: 	  ((i<question_cnt)&&(i<((row+1)*status_line_length))); i++) {
                   2259: 	if (i != 0 && (!(i%status_line_length))) { printf("</TR><TD></TD>"); }
                   2260: 	printf("<TD align=center valign=bottom><tt>%c</tt></TD>\n",header.weight[i]);
                   2261:       }
                   2262:     }
                   2263:     printf("</TR>");
                   2264:     capa_mfree(header.weight);
                   2265:     capa_mfree(header.partial_credit);
                   2266:   }
                   2267:   printf("\n</TABLE>\n<hr>\n");
                   2268:   /* SHOW TOTALS */
                   2269:   /* if capalogin_show_summary_score is set to none don't show it */
                   2270:   sprintf(buf,"%d sets, total = %3d/%3d (%d%%)\n", set, term_score, term_valid, 100*term_score/term_valid);
                   2271:   result=read_capa_config("capalogin_show_summary_score",buf2);
                   2272:   if (result != 0 && result != -1) {
                   2273:     if (strcasecmp(buf2,"none")==0) {
                   2274:     } else {
                   2275:       printf("%s",buf);
                   2276:     }
                   2277:   } else {
                   2278:     printf("%s",buf);
                   2279:   }
                   2280: 
                   2281:   printf("<TABLE cellpadding=0 cellspacing=0 border=0>\n<TR><TD>");
                   2282:   printf("<form method=\"post\" ");
                   2283:   sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
                   2284:   printf("%s\n", buf);
                   2285:   printf("<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
                   2286:   printf("<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
                   2287:   printf("<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
                   2288:   printf("<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_CHECKIN);
                   2289:   printf("<input type=\"submit\" value=\"Main menu\" ></form></TD>\n");
                   2290:   printf("<TD><form method=\"get\" action=\"http://%s/CAPA/class.html\">",serverName); 
                   2291:   printf("<input type=\"button\" value=\"Exit\" onclick=\"window.close()\"></form></TD>");
                   2292:   printf("\n</TABLE>\n");
                   2293: }
                   2294: 
                   2295: 
                   2296: void
                   2297: process_mode(int mode) {
                   2298: 
                   2299: #ifdef CGI_DBUG
                   2300:   fprintf(g_cgi,"entered process_mode[%d]\n",mode); fflush(g_cgi);
                   2301: #endif /* CGI_DBUG */
                   2302:   g_qchar_cnt=g_schar_cnt=0;
                   2303:   g_qsize=TEXT_BUF_SIZE*sizeof(char);
                   2304:   g_ssize=STATUS_BUF_SIZE*sizeof(char);
                   2305:   g_question_txt=capa_malloc(TEXT_BUF_SIZE,sizeof(char));
                   2306:   g_status_txt  =capa_malloc(STATUS_BUF_SIZE,sizeof(char));
                   2307: #ifdef CGI_DBUG
                   2308:   fprintf(g_cgi,"alloced everything\n"); fflush(g_cgi);
                   2309: #endif /* CGI_DBUG */
                   2310:   if( mode == VIEW_PREVIOUS_MODE ) {
                   2311:     print_quizz(g_cpath,g_cowner,g_class_name,g_student_number, g_entered_pin, g_vset,mode); 
                   2312:   } else if( mode == TRY_SET_MODE ) {
                   2313:     print_quizz(g_cpath,g_cowner,g_class_name,g_student_number, g_entered_pin, g_login_set,mode);
                   2314:   } else {
                   2315:     print_quizz(g_cpath,g_cowner,g_class_name,g_student_number,g_entered_pin,g_login_set,CHECK_ANSWER_MODE);
                   2316:   }
                   2317:   g_status_txt[g_schar_cnt]=0;
                   2318:   g_question_txt[g_qchar_cnt]=0;
                   2319:   if( g_schar_cnt != 0 ) {
                   2320:          fprintf(stdout,"%s",g_status_txt);
                   2321: #ifdef CGI_DBUG
                   2322:   fprintf(g_cgi,"print status [%s]\n",g_status_txt); fflush(g_cgi);
                   2323: #endif /* CGI_DBUG */
                   2324:   }
                   2325:   if( g_qchar_cnt != 0) {
                   2326:          fprintf(stdout,"%s",g_question_txt);
                   2327: #ifdef CGI_DBUG
                   2328:   fprintf(g_cgi,"print question [%s]\n",g_question_txt); fflush(g_cgi);
                   2329: #endif /* CGI_DBUG */
                   2330:   }
                   2331:   if( g_schar_cnt != 0 ) {
                   2332:          fprintf(stdout,"%s",g_status_txt);
                   2333:   }
                   2334:   fflush(stdout);
                   2335:   capa_mfree(g_status_txt);
                   2336:   capa_mfree(g_question_txt);
                   2337: 
                   2338: }
                   2339: 
                   2340: /*  mode could be exam summary, show or not show percentage */
                   2341: /*                quiz summary, show or not show percentage */
                   2342: void
                   2343: process_summary(int mode)
                   2344: {
                   2345:   int   outcome;
                   2346:   int   i, len;
                   2347:   char *c_name;
                   2348:   char  c_path[512];
                   2349:   
                   2350:   outcome = check_exam_quiz_path();
                   2351:   if( (mode == M_EXAMSUMM) && (outcome & 1) ) {  /* exam summary */
                   2352:     c_name = rindex(g_exam_path,'/');
                   2353:     c_name++;
                   2354:     i = strlen(c_name);
                   2355:     len = strlen(g_exam_path) - i - 1;
                   2356:     for(i=0;i<len;i++) {
                   2357:       c_path[i]=g_exam_path[i];
                   2358:     }
                   2359:     c_path[len]=0;
                   2360:     print_summary(c_path,c_name,g_student_number, g_entered_pin, g_exam_set);
                   2361:   }
                   2362:   if( (mode == M_QUIZSUMM) && (outcome & 2) ) {  /* quiz summary */
                   2363:     c_name = rindex(g_quiz_path,'/');
                   2364:     c_name++;
                   2365:     i = strlen(c_name);
                   2366:     len = strlen(g_quiz_path) - i - 1;
                   2367:     for(i=0;i<len;i++) {
                   2368:       c_path[i]=g_quiz_path[i];
                   2369:     }
                   2370:     c_path[len]=0;
                   2371:     print_summary(c_path,c_name,g_student_number, g_entered_pin, g_quiz_set);
                   2372:   }
                   2373:   
                   2374: }
                   2375: 
                   2376: /* ---------------- JAVA TScore.class page print out ----------------- */
                   2377: void                                     /* RETURNS: (nothing)          */
                   2378: print_termscore_page(class_dir,class,student_number,pin,set,out)
                   2379: char *class_dir;char *class;char *student_number;int pin;int set; /* student login set */
                   2380: FILE *out;
                   2381: {                                       /* LOCAL VARIABLES:            */
                   2382:   int      set_idx,                     /*    Set counter              */
                   2383:            i,                           /*    Question counter         */
                   2384:            set_score,                   /*    Score on a set           */
                   2385:            term_score=0,                /*    Total points received    */
1.3     ! albertel 2386:            term_valid=0;                /*    Total points possible    */
1.1       albertel 2387:   T_entry  entry;                       /*    Database entry for a set */
                   2388:   char     buf[MAX_BUFFER_SIZE]; /* Output line buffer  */
                   2389:   T_header header;                      /*    Problem set header       */
1.3     ! albertel 2390:   int      question_cnt,valid_wgt,configResult;
1.1       albertel 2391:   char     class_fullpath[ONE_K],*serverName;
                   2392:   int      hw_c, hw_r, qz_c, qz_r, fs, homework_count, quiz_count;
                   2393:   float    hw_w, qz_w, ex_w, fe_w, pc_w;
                   2394:   int      idx, entry_count, tmp_len;
                   2395:   float    *S, *F;
                   2396:   int      *X;
1.3     ! albertel 2397:   char     *capa_server;
1.1       albertel 2398:   int      max_set[4];
                   2399:   char     **c_path_pp;
1.3     ! albertel 2400: 
        !          2401:   /*Unused Vars  
        !          2402:     char     buf2[MAX_BUFFER_SIZE]; 
        !          2403:     char *qz_p, *ex_p, *epc_p; 
        !          2404:     int      ex_c, epc_c, result;
        !          2405:     int  rate, status_line_length=DEFAULT_STATUS_LINE_LENGTH,row;
        !          2406:   */
        !          2407: 
1.1       albertel 2408:   serverName=getenv("SERVER_NAME");
                   2409:   if (!serverName) {
                   2410:     fprintf(out,"Enviroment variable SERVER_NAME not set.\n");
                   2411:     fprintf(out,"Unable to complete actions.\n");
                   2412:     return;
                   2413:   }
                   2414: 
                   2415:   sprintf(class_fullpath,"%s/%s",class_dir,class);
                   2416:   chdir(class_fullpath);
                   2417:   
                   2418:   /*
                   2419:      change the working director to the major homework directory and begin to
                   2420:      read off the remaining path informations from this capa.config file 
                   2421:      homework_path   = 
                   2422:      quiz_path   = 
                   2423:      exam_path   = 
                   2424:      correction_path = 
                   2425:      homework_weight   = 0.3
                   2426:      quiz_weight       = 0.7
                   2427:      exam_weight       = 0.3
                   2428:      final_weight      = 0.35
                   2429:      correction_weight = 0.3
                   2430:      final_exam_set_number = 4
                   2431:      homework_count    = 12
                   2432:      quiz_count        = 24
                   2433:      
                   2434:   */
                   2435:   
                   2436:   configResult=read_capa_config("capa_server",buf);
                   2437:   if (configResult != 0 && configResult != -1 ) {
                   2438:     tmp_len = strlen(buf) + 1;
                   2439:     capa_server =   (char *)capa_malloc( tmp_len, sizeof(char));
                   2440:     sprintf(capa_server,"%s",buf);
                   2441:   } else { /* if capa_server is not set then we won't do anything further */
                   2442:     fprintf(out,"Parameter: capa_server in capa.config file are not properly set.\n");
                   2443:     return ;
                   2444:   }
                   2445:   if( get_termscore_params(&hw_w,&qz_w,&ex_w,&fe_w,&pc_w,&homework_count,&quiz_count,&fs) == -1 ) {
                   2446:     fprintf(out,"Parameters in capa.config file are not properly set.\n");
                   2447:     fprintf(out," such as homework_weight, quiz_weight, exam_weight, final_weight, correction_weight.\n");
                   2448:     
                   2449:     return;
                   2450:   }
                   2451:   c_path_pp = (char **)capa_malloc( 4, sizeof(char *));
                   2452:   tmp_len = strlen(class_fullpath) + 1;
                   2453:   c_path_pp[0] = (char *)capa_malloc(tmp_len,sizeof(char));
                   2454:   sprintf(c_path_pp[0],"%s",class_fullpath); /* c_path_pp[0] should always be there */
                   2455:   
                   2456:   entry_count = fs*2 + 1;
                   2457:   S = (float *)capa_malloc( ((fs+1)*2), sizeof(float));
                   2458:   F = (float *)capa_malloc( ((fs+1)*2), sizeof(float));
                   2459:   X =   (int *)capa_malloc( ((fs+1)*2), sizeof(int));
                   2460:   
                   2461:   max_set[0] = set;  /* the login set number */
                   2462:   hw_c = max_set[0];
                   2463:   hw_r = homework_count - set;
                   2464:   
                   2465:   
                   2466:   configResult=read_capa_config("quiz_path",buf);
                   2467:   if (configResult != 0 && configResult != -1 ) {
                   2468:     tmp_len = strlen(buf)+1;
                   2469:     c_path_pp[1] = (char *)capa_malloc(tmp_len,sizeof(char));
                   2470:     sprintf(c_path_pp[1],"%s",buf);
                   2471:     max_set[1] = check_class_get_maxset(c_path_pp[1]);
                   2472:     if( max_set[1] <= 0 ) {
                   2473:       /* should we continue ? */
                   2474:       max_set[1] = 1;
                   2475:       X[1] = 1;
                   2476:     }
                   2477:     qz_c = max_set[1];
                   2478:     qz_r = quiz_count - max_set[1];
                   2479:   } else { /* if quiz_path is not in capa.config, then we will skip quizz */
                   2480:     qz_c = 0;
                   2481:     qz_r = 0;
                   2482:   }
                   2483:   
                   2484:   
                   2485:   configResult=read_capa_config("exam_path",buf);
                   2486:   if (configResult != 0 && configResult != -1 ) {
                   2487:     tmp_len = strlen(buf)+1;
                   2488:     c_path_pp[2] = (char *)capa_malloc( (tmp_len),sizeof(char));
                   2489:     sprintf(c_path_pp[2],"%s",buf);
                   2490:     max_set[2] = check_class_get_maxset(c_path_pp[2]);
                   2491:     if( max_set[2] <= 0 ) {
                   2492:       /* should we continue ? */
                   2493:       max_set[2] = 0;
                   2494:       for(idx=2;idx <= (fs*2); idx++) {
                   2495:         X[idx] = 1;
                   2496:       }
                   2497:     }
                   2498:   } else { /* if exam_path is not in capa.config, then skip exams */
                   2499:     fs = 0;
                   2500:   }
                   2501:   configResult=read_capa_config("correction_path",buf);
                   2502:   if (configResult != 0 && configResult != -1 ) {
                   2503:     tmp_len = strlen(buf)+1;
                   2504:     c_path_pp[3] = (char *)capa_malloc(tmp_len,sizeof(char));
                   2505:     sprintf(c_path_pp[3],"%s",buf);
                   2506:     max_set[3] = check_class_get_maxset(c_path_pp[3]);
                   2507:     if( max_set[3] <= 0 ) {
                   2508:       /* should we continue ? */
                   2509:       max_set[3] = 0;
                   2510:       
                   2511:     }
                   2512:   } else { /* if correction_path is not in capa.config, then skip corrections */
                   2513:     pc_w = 0.0;
                   2514:   }
                   2515:   
                   2516:   
                   2517:   
                   2518:   for( idx = 0; idx < 4; idx++) {
                   2519:      if( c_path_pp[idx] != NULL ) {
                   2520:        chdir(c_path_pp[idx]);
                   2521:        term_score=0;
                   2522:        term_valid=0;
                   2523:        for (set_idx=1; set_idx<=max_set[idx]; set_idx++) {
                   2524:           if (capa_get_header(&header,set_idx))  return;
                   2525:           capa_get_entry(&entry,student_number,set_idx);
                   2526:           sscanf(header.num_questions,"%d", &(question_cnt) );
                   2527:           valid_wgt = 0; set_score = 0;
                   2528:           header.weight[question_cnt] = '\0';
                   2529:           header.partial_credit[question_cnt] = '\0';
                   2530:           for (i=0; i<question_cnt; i++) {
                   2531:             valid_wgt +=  (header.weight[i] - '0');
                   2532:             if((entry.answers[i]=='Y') || (entry.answers[i]=='y'))  
                   2533: 	       set_score += (header.weight[i]-'0');
                   2534:             if((entry.answers[i]=='E') || (entry.answers[i]=='e'))  
                   2535: 	       valid_wgt -= (header.weight[i] - '0');
                   2536:             if((entry.answers[i]>='0') && (entry.answers[i]<='9'))  
                   2537: 	       set_score += (entry.answers[i] - '0');
                   2538:           }
                   2539:           term_valid += valid_wgt;
                   2540:           term_score += set_score;
                   2541:           capa_mfree(header.weight);
                   2542:           capa_mfree(header.partial_credit);
                   2543:           if(idx==2) { /* exam sets */
                   2544:             S[set_idx*2] = (float)set_score;
                   2545:             F[set_idx*2] = (float)valid_wgt;
                   2546:             X[set_idx*2] = 0;
                   2547:           }
                   2548:           if(idx==3) { /* correction sets */
                   2549:             S[set_idx*2+1] = (float)set_score;
                   2550:             F[set_idx*2+1] = (float)valid_wgt;
                   2551:             X[set_idx*2+1] = 0;
                   2552:           }
                   2553:        }
                   2554:        if( (idx == 0) || (idx==1) ) { /* homeworks and quizzes */
                   2555:          S[idx] = (float)term_score;
                   2556:          F[idx] = (float)term_valid;
                   2557:          X[idx] = 1;
                   2558:        }
                   2559:      }
                   2560:   }
                   2561:   
                   2562:   
                   2563: 
                   2564:   
                   2565:   fprintf(out,"<CENTER>\n");
                   2566:   fprintf(out,"<APPLET CODE=TScore.class CODEBASE=\"http://%s\" width=600 height=750>\n",capa_server);
                   2567:   fprintf(out,"<PARAM NAME=\"HW_W\"  VALUE=\"%f\">\n", hw_w);
                   2568:   fprintf(out,"<PARAM NAME=\"QZ_W\"  VALUE=\"%f\">\n", qz_w);
                   2569:   fprintf(out,"<PARAM NAME=\"EX_W\"  VALUE=\"%f\">\n", ex_w);
                   2570:   fprintf(out,"<PARAM NAME=\"FE_W\"  VALUE=\"%f\">\n", fe_w);
                   2571:   fprintf(out,"<PARAM NAME=\"PC_W\"  VALUE=\"%f\">\n", pc_w);
                   2572:   fprintf(out,"<PARAM NAME=\"HW_C\"  VALUE=\"%d\">\n", hw_c);
                   2573:   fprintf(out,"<PARAM NAME=\"HW_R\"  VALUE=\"%d\">\n", hw_r);
                   2574:   fprintf(out,"<PARAM NAME=\"FS\"    VALUE=\"%d\">\n", fs);
                   2575:   fprintf(out,"<PARAM NAME=\"QZ_C\"  VALUE=\"%d\">\n", qz_c);
                   2576:   fprintf(out,"<PARAM NAME=\"QZ_R\"  VALUE=\"%d\">\n", qz_r);
                   2577:   
                   2578: 
                   2579:   for(idx=0;idx<entry_count;idx++) {
                   2580:     fprintf(out,"<PARAM NAME=\"S%d\"  VALUE=\"%f\">\n",idx,S[idx]);
                   2581:     fprintf(out,"<PARAM NAME=\"F%d\"  VALUE=\"%f\">\n",idx,F[idx]);
                   2582:     fprintf(out,"<PARAM NAME=\"X%d\"  VALUE=\"%d\">\n",idx,X[idx]);
                   2583:   }
                   2584:   
                   2585:   fprintf(out,"</APPLET> </CENTER>\n");
                   2586:   
                   2587:   fprintf(out,"<TABLE cellpadding=0 cellspacing=0 border=0>\n<TR><TD>");
                   2588:   fprintf(out,"<form method=\"post\" ");
                   2589:   sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
                   2590:   fprintf(out,"%s\n", buf);
                   2591:   fprintf(out,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
                   2592:   fprintf(out,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
                   2593:   fprintf(out,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
                   2594:   fprintf(out,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_CHECKIN);
                   2595:   fprintf(out,"<input type=\"submit\" value=\"Main menu\" ></form></TD>\n");
                   2596:   fprintf(out,"<TD><form method=\"get\" action=\"http://%s/CAPA/class.html\">",serverName); 
                   2597:   fprintf(out,"<input type=\"button\" value=\"Exit\" onclick=\"window.close()\"></form></TD>");
                   2598:   fprintf(out,"\n</TABLE>\n");
                   2599:   
                   2600:   capa_mfree((char *)S);
                   2601:   capa_mfree((char *)F);
                   2602:   capa_mfree((char *)X);
                   2603:   for(idx=0;idx<4;idx++) {
                   2604:     if( c_path_pp[idx] != NULL )  capa_mfree((char *)c_path_pp[idx]);
                   2605:   }
                   2606:   capa_mfree((char *)c_path_pp);
                   2607:   capa_mfree((char *)capa_server);
                   2608: }
                   2609: 
                   2610: 
                   2611: 
                   2612: int
                   2613: get_termscore_params(hw,qw,ew,fw,pw,hc,qc,fs) 
                   2614: float *hw;float *qw;float *ew;float *fw;float *pw;
                   2615: int   *hc;int   *qc;int   *fs;
                   2616: {
                   2617:   char     buf[MAX_BUFFER_SIZE]; /* Output line buffer  */
                   2618:   int      hw_c, qz_c, fe_s;
                   2619:   float    hw_w, qz_w, ex_w, fe_w, pc_w;
                   2620:   int      configResult;
                   2621:   
                   2622:   configResult=read_capa_config("homework_weight",buf);
                   2623:   if (configResult != 0 && configResult != -1 ) {
                   2624:     sscanf(buf,"%f", &hw_w);
                   2625:     if(hw_w <= 0.0 )  {
                   2626:       hw_w = DEFAULT_HW_W;
                   2627:     }
                   2628:   } else {
                   2629:     return (-1);
                   2630:   }
                   2631:   configResult=read_capa_config("quiz_weight",buf);
                   2632:   if (configResult != 0 && configResult != -1 ) {
                   2633:     sscanf(buf,"%f", &qz_w);
                   2634:     if(qz_w <= 0.0 )  {
                   2635:       qz_w = DEFAULT_QZ_W;
                   2636:     }
                   2637:   } else {
                   2638:     return (-1);
                   2639:   }
                   2640:   configResult=read_capa_config("exam_weight",buf);
                   2641:   if (configResult != 0 && configResult != -1 ) {
                   2642:     sscanf(buf,"%f", &ex_w);
                   2643:     if(ex_w <= 0.0 )  {
                   2644:       ex_w = DEFAULT_EX_W;
                   2645:     }
                   2646:   } else {
                   2647:     return (-1);
                   2648:   }
                   2649:   configResult=read_capa_config("final_weight",buf);
                   2650:   if (configResult != 0 && configResult != -1 ) {
                   2651:     sscanf(buf,"%f", &fe_w);
                   2652:     if(fe_w <= 0.0 )  {
                   2653:       fe_w = DEFAULT_FE_W;
                   2654:     }
                   2655:   } else {
                   2656:     return (-1);
                   2657:   }
                   2658:   configResult=read_capa_config("correction_weight",buf);
                   2659:   if (configResult != 0 && configResult != -1 ) {
                   2660:     sscanf(buf,"%f", &pc_w);
                   2661:     if(pc_w <= 0.0 )  {
                   2662:       pc_w = DEFAULT_PC_W;
                   2663:     }
                   2664:   } else {
                   2665:     return (-1);
                   2666:   }
                   2667:   configResult=read_capa_config("final_exam_set_number",buf);
                   2668:   if (configResult != 0 && configResult != -1 ) {
                   2669:     sscanf(buf,"%d", &fe_s);
                   2670:     if(fe_s <= 0 )  {
                   2671:       fe_s = DEFAULT_FE_NUMBER;
                   2672:     }
                   2673:   } else {
                   2674:     return (-1);
                   2675:   }
                   2676:   configResult=read_capa_config("homework_count",buf);
                   2677:   if (configResult != 0 && configResult != -1 ) {
                   2678:     sscanf(buf,"%d", &hw_c);
                   2679:     if(hw_c <= 0 )  {
                   2680:       hw_c = DEFAULT_HW_COUNT;
                   2681:     }
                   2682:   } else {
                   2683:     return (-1);
                   2684:   }
                   2685:   configResult=read_capa_config("quiz_count",buf);
                   2686:   if (configResult != 0 && configResult != -1 ) {
                   2687:     sscanf(buf,"%d", &qz_c);
                   2688:     if(qz_c <= 0 )  {
                   2689:       qz_c = DEFAULT_QZ_COUNT;
                   2690:     }
                   2691:   } else {
                   2692:     return (-1);
                   2693:   }
                   2694:   *hw = hw_w; *qw = qz_w; *ew = ex_w; *fw = fe_w; *pw = pc_w;
                   2695:   *hc = hw_c; *qc = qz_c; *fs = fe_s;
                   2696:   return (0);
                   2697: 
                   2698: }
                   2699: 
                   2700: /* =================================================================================================== */

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