File:  [LON-CAPA] / capa / capa51 / pProj / capaCgiUtils.c
Revision 1.4: download - view: text, annotated - select for diffs
Mon Nov 8 22:30:02 1999 UTC (24 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- changed default to not allow viewing between answd and due
- added debug to scorer for when it won't quit
- fixed problems with blank question bodies in capasbin
- fixed displaying of answer boxes to work with mixed /AND questions
- fixed summary screens in both capalogin and capaweb to work, so that
  non opened sets are not summaried
- fixed equation answers, so they can take array arguments for the
  point specifications

    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: {
 1070:   char   buf[MAX_BUFFER_SIZE], discussdir[MAX_BUFFER_SIZE];
 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>");
 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>");
 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: 	   if (prob_idx->question != NULL) {
 1458: 	     q_leng = strlen(prob_idx->question);
 1459: 	     if ( !prob_idx->show_br ) {
 1460: 	       append_qtext(prob_idx->question);
 1461: 	     } else {
 1462: 	       for(idx=0;idx<q_leng;idx++) {
 1463: 		 if ( g_qchar_cnt+2 >= g_qsize ) {
 1464: 		   char *temp_text;
 1465: 		   g_qsize=g_qchar_cnt*2;
 1466: 		   temp_text=capa_malloc(g_qsize,sizeof(char));
 1467: 		   strncpy(temp_text,g_question_txt,g_qsize);
 1468: 		   capa_mfree(g_question_txt);
 1469: 		   g_question_txt=temp_text;		 
 1470: 		 }
 1471: 		 g_question_txt[g_qchar_cnt]=prob_idx->question[idx];
 1472: 		 g_qchar_cnt++;
 1473: 		 if(prob_idx->question[idx] == '\n' ) {
 1474: 		   append_qtext("<br>\n");
 1475: 		 }
 1476: 	       }
 1477: 	     }
 1478: 	   }
 1479:          }
 1480:          if(mode == VIEW_PREVIOUS_MODE) { /* VIEW_PREVIOUS_MODE */
 1481:            if( display_ans ) {
 1482:              if( prob_idx->ans_type == ANSWER_IS_FLOAT ) {
 1483:                  a = (double)atof(prob_idx->answer);
 1484:                  sprintf(cmp_ans,prob_idx->ans_fmt, a);
 1485:              } else {
 1486:                  strcpy(cmp_ans,prob_idx->answer);
 1487:              }
 1488:              if( prob_idx->ans_unit ) {
 1489:                  sprintf(buf,"<p><tt><b>Answer:</b> %s %s</tt><br>\n",cmp_ans, prob_idx->unit_str); 
 1490:              } else {
 1491:                  sprintf(buf,"<p><tt><b>Answer:</b> %s</tt><br>\n",cmp_ans); 
 1492:              }
 1493:              append_qtext(buf);
 1494: 	     if ( prob_idx->explain) {
 1495: 	       sprintf(buf,"<p><b>Explanation: </b>\n<p>%s<br>\n",prob_idx->explain);
 1496: 	       append_qtext(buf);
 1497: 	     }
 1498:            }
 1499:          } else { /* could be TRY_SET_MODE, CHECK_ANSWER_MODE */
 1500:            
 1501:            if( g_passdue ) {
 1502:              get_response(header.partial_credit[question_idx],entry.answers[question_idx],question_idx,prob_idx);
 1503:            }else{
 1504: 	     if (g_inhibit_response) {
 1505: 	       print_inhibited_response(header.partial_credit[question_idx],entry.answers[question_idx],question_idx,prob_idx);
 1506: 	     } else {
 1507: 	       print_response(header.partial_credit[question_idx],entry.answers[question_idx],question_idx,prob_idx); 
 1508: 	     }
 1509:              append_qtext("<br>\n");
 1510:              if( (g_tried[question_idx] >= prob_idx->show_hint) || 
 1511: 		 (entry.answers[question_idx]=='Y') ||
 1512: 		 (entry.answers[question_idx]=='y')) {
 1513:                if( prob_idx->hint ) {
 1514:                  sprintf(buf,"<p><B>Hint: </B>%s\n<br>\n", prob_idx->hint);
 1515:                  append_qtext(buf);
 1516:                }
 1517:              }
 1518:            }
 1519:          }
 1520:      }   /* ------------------------------------- end displaying each problem */
 1521:      append_qtext("\n</OL>\n");
 1522:      if( EndText_p )   append_qtext(EndText_p);
 1523:      free_problems(first_prob);
 1524: 
 1525: #ifdef CGI_DBUG
 1526:   fprintf(g_cgi,"End display each problem\n"); fflush(g_cgi);
 1527: #endif /* CGI_DBUG */
 1528: 
 1529:      if( mode == CHECK_ANSWER_MODE ) {  /* update the record database */
 1530:        if( !g_passdue ) {
 1531:          for(idx=0;idx<question_cnt;idx++){
 1532:            if( g_new_answerdb[idx] != entry.answers[idx]) { 
 1533:              entry.answers[idx] = g_new_answerdb[idx];
 1534:            }
 1535:            if(g_tried[idx] < 10 ) {
 1536:              entry.tries[3*idx]   = ' ';
 1537:              entry.tries[3*idx+1] = g_tried[idx] + '0';
 1538:              if(idx < question_cnt-1)  entry.tries[3*idx+2] = ',';
 1539:            } else {
 1540:              entry.tries[3*idx]   = (int)(g_tried[idx]/10) + '0';
 1541:              entry.tries[3*idx+1] = (g_tried[idx] % 10) + '0';
 1542:              if(idx < question_cnt-1)  entry.tries[3*idx+2] = ',';
 1543:            }
 1544:          }
 1545:          capa_set_entry(&entry,sn,set,offset);                     /* <-------- capa*() call */
 1546: 
 1547: #ifdef CGI_DBUG
 1548: 	 fprintf(g_cgi,"DONE set db entry\n"); fflush(g_cgi);
 1549: #endif /* CGI_DBUG */
 1550: 
 1551: 	 create_status_line(mode,question_cnt,&entry);
 1552:        } 
 1553:        if (w_log_attempt(g_student_number,set,g_log_string) == -1 ) {
 1554: 	 fprintf(stdout,"<BOLD>Unable to log attempt. Please notify instructor.</BOLD>\n");
 1555:        }
 1556:      }
 1557: #ifdef CGI_DBUG
 1558:      fprintf(g_cgi,"DONE check answer mode\n"); fflush(g_cgi);
 1559: #endif /* CGI_DBUG */
 1560:      
 1561:      if( (mode == TRY_SET_MODE && !g_passdue)  || 
 1562: 	 mode == VIEW_PREVIOUS_MODE) {
 1563:        create_status_line(mode,question_cnt,&entry);
 1564:      }
 1565:      
 1566:      if( !g_passdue ) {
 1567:        sprintf(buf,"</ul></form>\n"); append_qtext(buf);
 1568:      }
 1569:   }
 1570: }
 1571: 
 1572: void
 1573: get_response(char pcr,char u_db,int q_idx,Problem_t *p)
 1574: {
 1575:   if( pcr == '0' || p->ans_type==ANSWER_IS_SUBJECTIVE ) { /* not hand-graded question */
 1576:      switch(u_db) {  /* what's from the user record */
 1577:        case 'Y': break;
 1578:        case 'y': break;
 1579:        case '-':
 1580:                if(g_stu_ans_pp[q_idx+1] != NULL ) log_user_ans(q_idx,p);
 1581:                break;
 1582:        case 'E':
 1583:        case 'e': break;
 1584:        case 'n': break;
 1585:        case 'N':
 1586:        case '0': case '1': case '2': case '3': case '4': 
 1587:        case '5': case '6': case '7': case '8': case '9':
 1588:                if(g_stu_ans_pp[q_idx+1] != NULL ) log_user_ans(q_idx,p);
 1589:                break;
 1590:        default:
 1591: 	      break;
 1592:      }
 1593:    }
 1594: }
 1595: 
 1596: void display_last_answer(int q_idx)
 1597: {
 1598:   char       buf[MAX_BUFFER_SIZE];  
 1599:   StudentAnswer_t  *sa_p;
 1600: #ifdef CGI_DBUG
 1601:   fprintf(g_cgi,"Enter display_last_answer() [%d]\n",q_idx); fflush(g_cgi);
 1602: #endif /* CGI_DBUG */
 1603:   if(g_stu_ans_pp[q_idx+1] != NULL) {
 1604:     sa_p=g_stu_ans_pp[q_idx+1];
 1605:   } else {
 1606:     if (g_last_ans_pp[q_idx+1] != NULL) {
 1607:       sa_p=g_last_ans_pp[q_idx+1];
 1608:     } else {
 1609:       return;
 1610:     }
 1611:   }
 1612:   if (sa_p->a_next == NULL) {
 1613:     sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d\" value=\"%s\">\n",
 1614: 	    q_idx+1,sa_p->a_str);
 1615:     append_qtext(buf);
 1616:     sprintf(buf," <b>Last Answer:</b> %s\n",sa_p->a_str);
 1617:     append_qtext(buf);
 1618:   } else {
 1619:     while(sa_p) {
 1620:       sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d,%02d\" value=\"%s\">\n",
 1621: 	      q_idx+1,sa_p->a_idx,sa_p->a_str);
 1622:       append_qtext(buf);
 1623:       sprintf(buf," <b>Last Answer %d:</b> %s\n",sa_p->a_idx,sa_p->a_str);
 1624:       append_qtext(buf);
 1625:       sa_p=sa_p->a_next;
 1626:     }
 1627:   }
 1628: }
 1629: 
 1630: void preserve_last_answer(int q_idx,int print)
 1631: {
 1632:   char       buf[MAX_BUFFER_SIZE];  
 1633:   StudentAnswer_t  *sa_p;
 1634: #ifdef CGI_DBUG
 1635:   fprintf(g_cgi,"Enter preserve_last_answer() [%d]\n",q_idx); fflush(g_cgi);
 1636: #endif /* CGI_DBUG */
 1637:   if(g_stu_ans_pp[q_idx+1] != NULL) {
 1638:     sa_p=g_stu_ans_pp[q_idx+1];
 1639:   } else {
 1640:     if (g_last_ans_pp[q_idx+1] != NULL) {
 1641:       sa_p=g_last_ans_pp[q_idx+1];
 1642:     } else {
 1643:       return;
 1644:     }
 1645:   }
 1646:   if (sa_p->a_next == NULL) {
 1647:     sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d\" value=\"%s\">\n",
 1648: 	    q_idx+1,sa_p->a_str);
 1649:     if (print) printf(buf); else append_qtext(buf);
 1650:   } else {
 1651:     while(sa_p) {
 1652:       sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d,%02d\" value=\"%s\">\n",
 1653: 	      q_idx+1,sa_p->a_idx,sa_p->a_str);
 1654:       if (print) printf(buf); else append_qtext(buf);
 1655:       sa_p=sa_p->a_next;
 1656:     }
 1657:   }
 1658: }
 1659: 
 1660: void display_last_subjective(int q_idx)
 1661: {
 1662:   char      *buf;
 1663:   char      *answer;
 1664: #ifdef CGI_DBUG
 1665:   fprintf(g_cgi,"Enter display_last_subjective() [%d]\n",q_idx); fflush(g_cgi);
 1666: #endif /* CGI_DBUG */
 1667:   answer=capa_get_subjective(g_login_set,q_idx+1,g_student_number);
 1668:   if (answer==NULL) return;
 1669: #ifdef CGI_DBUG
 1670:   fprintf(g_cgi,"Found answer %s\n",answer); fflush(g_cgi);
 1671: #endif /* CGI_DBUG */
 1672:   buf=capa_malloc(MAX_BUFFER_SIZE+strlen(answer),1);
 1673:   /* don't need to stick in a last since we always get it from the files */
 1674:   /*  sprintf(buf,"<input type=\"hidden\" name=\"LAST%02d\" value=\"%s\">\n",q_idx+1,answer);*/
 1675:   append_qtext(buf);
 1676:   append_qtext("<b>Current Submission:</b><br>\n");
 1677:   append_qtext("<TABLE BORDER=1 CELLSPACING=0>\n<TR><TD>\n");
 1678:   append_qtext("<PRE>");
 1679:   append_qtext(answer);
 1680:   append_qtext("</PRE>");
 1681:   append_qtext("</TD></TR></TABLE>\n");
 1682:   capa_mfree(buf);
 1683:   capa_mfree(answer);
 1684: }
 1685: 
 1686: void create_answer_area(Problem_t *p,int q_idx) 
 1687: {
 1688:   int ii=0;
 1689:   char       buf[MAX_BUFFER_SIZE];  
 1690:   AnswerInfo_t *ai;
 1691: #ifdef CGI_DBUG
 1692:   fprintf(g_cgi,"Enter create_answer_area() [%d]\n",q_idx); fflush(g_cgi);
 1693: #endif /* CGI_DBUG */
 1694: 
 1695:   if ( p->ans_type==ANSWER_IS_SUBJECTIVE ) {
 1696:     display_last_subjective(q_idx);
 1697:   }
 1698:   if ( p->show_ans_box ) { 
 1699:     if ( p->ans_op == ANS_AND ) {
 1700:       if (p->ans_type == ANSWER_IS_FORMULA) {
 1701: 	/* first answer is stored in p, the rest are linked off of p->ans_list */
 1702: 	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);
 1703:       } else {
 1704: 	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);
 1705:       }
 1706:       append_qtext(buf);
 1707:       for(ii=1, ai=p->ans_list;ii<p->ans_cnt;ai=ai->ans_next,ii++) {
 1708: 	if (ai->ans_type == ANSWER_IS_FORMULA) {
 1709: 	  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);
 1710: 	} else {
 1711: 	  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);
 1712: 	}
 1713: 	append_qtext(buf);
 1714:       }
 1715:     } else { /* single answer, or /OR answers, or subjective answer */
 1716:       if (p->ans_type == ANSWER_IS_SUBJECTIVE ) {
 1717: 	sprintf(buf,"<p><B>Answer:</B><br><TEXTAREA name=\"INPUT%02d\" rows=\"15\" cols=\"80\"></TEXTAREA>\n",q_idx+1);
 1718:       } else {
 1719: 	if (p->ans_type == ANSWER_IS_FORMULA) {
 1720: 	  sprintf(buf,"<p><B>Answer:</B><input size=80 name=\"INPUT%02d\" value=\"\">\n",q_idx+1);
 1721: 	} else {
 1722: 	  sprintf(buf,"<p><B>Answer:</B><input name=\"INPUT%02d\" value=\"\">\n",q_idx+1);
 1723: 	}
 1724:       }
 1725:       append_qtext(buf);
 1726:     }
 1727:   }
 1728:   append_qtext("<input type=\"submit\" value=\"Submit All Answers\" >\n");
 1729:   if ( p->ans_type!=ANSWER_IS_SUBJECTIVE ) {
 1730:     display_last_answer(q_idx);
 1731:   }
 1732: }
 1733: 
 1734: void
 1735: print_response(char pcr,char u_db,int q_idx,Problem_t *p)
 1736: {
 1737: int   a_tpe;
 1738: char *c_ans,*response,*answered="Answered",*nycorrect="Not yet correct";
 1739: int   t_tpe;
 1740: double tol;
 1741: int    sig_l;
 1742: int    sig_u;
 1743: char  *a_fmt, *c_answer_str;
 1744: int    tries;
 1745: 
 1746:     char       buf[MAX_BUFFER_SIZE];
 1747: 
 1748:  a_tpe = p->ans_type;
 1749:  c_ans = p->answer;
 1750:  t_tpe = p->tol_type;
 1751:  tol   = p->tolerance;
 1752:  sig_l = p->sig_lbound;
 1753:  sig_u = p->sig_ubound;
 1754:  a_fmt = p->ans_fmt;
 1755:  tries = p->tries;
 1756:  response=nycorrect;
 1757: 
 1758: #ifdef CGI_DBUG
 1759:   fprintf(g_cgi,"Enter print_response() [%c]\n",u_db); fflush(g_cgi);
 1760: #endif /* CGI_DBUG */
 1761:   if( pcr == '0' || a_tpe==ANSWER_IS_SUBJECTIVE ) { /* not hand-graded question */
 1762:      switch(u_db) {  /* this is from the user record */
 1763:        case 'Y':
 1764:                c_answer_str = answers_string(ANSWER_STRING_MODE, p);
 1765:                sprintf(buf,"<p><tt>Correct, computer gets: %s</tt>\n", c_answer_str);
 1766:                append_qtext(buf);
 1767:                capa_mfree((char *)c_answer_str);
 1768:                break;
 1769:        case 'y':
 1770:                append_qtext("<p><tt>Hand-graded Correct</tt>\n");
 1771:                break;
 1772:        case '-':
 1773:                if(g_stu_ans_pp[q_idx+1] == NULL ) {
 1774: 		 create_answer_area(p,q_idx);
 1775: 	       } else {
 1776:                  check_user_ans(q_idx,p);
 1777:                }
 1778:                break;
 1779:        case 'E':
 1780:        case 'e': append_qtext("<p><tt>Excused</tt>\n");               break;
 1781:        case 'n': append_qtext("<p><tt>Hand-graded Incorrect</tt>\n"); break;
 1782:        case '0': case '1': case '2': case '3': case '4': 
 1783:        case '5': case '6': case '7': case '8': case '9':
 1784: 	 response=answered;
 1785:        case 'N':
 1786:                if(g_stu_ans_pp[q_idx+1] == NULL ) {
 1787: 		 if( g_tried[q_idx] < tries) {
 1788: 		   create_answer_area(p,q_idx);
 1789: 		   if( tries - g_tried[q_idx] == 1) {
 1790: 		     sprintf(buf,"<br><tt>%s, ONE try left!!</tt>\n",response);
 1791: 		   }else{
 1792: 		     sprintf(buf,"<br><tt>%s, tries %d/%d</tt>\n",response,
 1793: 			     g_tried[q_idx],tries);
 1794: 		   }  
 1795: 		   append_qtext(buf);
 1796: 		 }else{
 1797: 		   if (p->ans_type==ANSWER_IS_SUBJECTIVE)
 1798: 		     display_last_answer(q_idx);
 1799: 		   else
 1800: 		     display_last_subjective(q_idx);
 1801: 		   append_qtext("<br><tt>No more tries.</tt>\n");
 1802: 		 } 
 1803:                } else { /* answering this question */
 1804: 		 if( g_tried[q_idx] < tries) {
 1805: 		   check_user_ans(q_idx,p);
 1806: 		 } else {
 1807: 		   if (p->ans_type==ANSWER_IS_SUBJECTIVE)
 1808: 		     display_last_answer(q_idx);
 1809: 		   else
 1810: 		     display_last_subjective(q_idx);
 1811: 		   append_qtext("<br><tt>No more tries.</tt>\n");
 1812: 		 }
 1813: 	       }
 1814:                break;
 1815:      }
 1816:    } else {
 1817:      append_qtext("<p><tt>Question to be Graded Manually.</tt>\n");
 1818:      
 1819:    }
 1820: 
 1821: }
 1822: 
 1823: void
 1824: print_inhibited_response(char pcr,char u_db,int q_idx,Problem_t *p)
 1825: {
 1826: int   a_tpe;
 1827: char *c_ans;
 1828: int   t_tpe;
 1829: double tol;
 1830: int    sig_l;
 1831: int    sig_u;
 1832: char  *a_fmt;
 1833: int    tries;
 1834: char       buf[MAX_BUFFER_SIZE];
 1835: 
 1836:  a_tpe = p->ans_type;
 1837:  c_ans = p->answer;
 1838:  t_tpe = p->tol_type;
 1839:  tol   = p->tolerance;
 1840:  sig_l = p->sig_lbound;
 1841:  sig_u = p->sig_ubound;
 1842:  a_fmt = p->ans_fmt;
 1843:  tries = p->tries;
 1844: 
 1845: #ifdef CGI_DBUG
 1846:   fprintf(g_cgi,"Enter print_inhibited_response() [%c]\n",u_db); fflush(g_cgi);
 1847: #endif /* CGI_DBUG */
 1848: 
 1849:   if( pcr == '0' || a_tpe==ANSWER_IS_SUBJECTIVE ) { /* not hand-graded question */
 1850:      switch(u_db) {  /* this is from the user record */
 1851:        case '-': 
 1852:                if(g_stu_ans_pp[q_idx+1] == NULL ) {
 1853: 		 create_answer_area(p,q_idx);
 1854:                } else {
 1855:                  check_inhibited_user_ans(q_idx,p);
 1856:                }
 1857:                break;
 1858:        case 'Y': case 'y':
 1859:        case 'E': case 'e': 
 1860:        case 'n': case 'N': 
 1861:        case '0': case '1': case '2': case '3': case '4': 
 1862:        case '5': case '6': case '7': case '8': case '9':
 1863:                if(g_stu_ans_pp[q_idx+1] == NULL ) {
 1864: 		 if( g_tried[q_idx] < tries) {
 1865: 		   create_answer_area(p,q_idx);	
 1866: 		   if( tries - g_tried[q_idx] == 1) {
 1867: 		     append_qtext("<br><tt>Answered, ONE try left!!</tt>\n");
 1868: 		   }else{
 1869: 		     sprintf(buf,"<br><tt>Answered, tries %d/%d</tt>\n",g_tried[q_idx],tries);
 1870: 		     append_qtext(buf);
 1871: 		   }  
 1872: 		 }else{
 1873: 		   if (p->ans_type==ANSWER_IS_SUBJECTIVE)
 1874: 		     display_last_answer(q_idx);
 1875: 		   else
 1876: 		     display_last_subjective(q_idx);
 1877: 		   append_qtext("<br><tt>Answered,No more tries.</tt>\n");
 1878: 		 } 
 1879: 	       } else { /* answering this question */
 1880: 		 if( g_tried[q_idx] < tries) {
 1881: 		   check_inhibited_user_ans(q_idx,p);
 1882: 		 } else {
 1883: 		   if (p->ans_type==ANSWER_IS_SUBJECTIVE)
 1884: 		     display_last_answer(q_idx);
 1885: 		   else
 1886: 		     display_last_subjective(q_idx);
 1887: 		   append_qtext("<br><tt>Answered, No more tries.</tt>\n");
 1888: 		 }
 1889:                }
 1890:                break;
 1891:      }
 1892:    } else {
 1893:      append_qtext("<p><tt>Question to be Graded Manually.</tt>\n"); 
 1894:    }
 1895: }
 1896: 
 1897: /* returns a -1 if there were not enough answers, otherwise the number of responses
 1898:    for the question is returned*/
 1899: int gather_answers(char ***ans,int q_idx,Problem_t *p)
 1900: {
 1901:   int cnt;
 1902:   if(p->ans_op==ANS_AND) {
 1903:     int i; StudentAnswer_t *sa_p;
 1904:     *ans=(char**)capa_malloc(p->ans_cnt,1);
 1905:     sa_p= g_stu_ans_pp[q_idx+1];
 1906:     for(i=0;((i<p->ans_cnt)&&(sa_p));i++){
 1907:       ans[0][i]=sa_p->a_str;
 1908:       sa_p=sa_p->a_next;
 1909:     }
 1910:     cnt=p->ans_cnt;
 1911:     if (i<p->ans_cnt) return -1;
 1912:   } else {
 1913:     *ans=(char**)capa_malloc(p->ans_cnt,1);
 1914:     ans[0][0]=g_stu_ans_pp[q_idx+1]->a_str;
 1915:     cnt=1;
 1916:   }
 1917:   return cnt;
 1918: }
 1919: 
 1920: void
 1921: log_user_ans(int q_idx,Problem_t *p)
 1922: {
 1923:   char **ans;
 1924:   int cnt;
 1925:   if (p->ans_type==ANSWER_IS_SUBJECTIVE) {
 1926:     capa_set_subjective(g_login_set,q_idx+1,g_student_number,
 1927: 			g_stu_ans_pp[q_idx+1]->a_str);
 1928:   } else {
 1929:     if (-1 != (cnt=gather_answers(&ans,q_idx,p))) {
 1930:       switch( capa_check_answers(p,ans,cnt) ) {
 1931:         case  EXACT_ANS:  g_log_string[q_idx]='Y'; break;
 1932:         case  APPROX_ANS: g_log_string[q_idx]='Y'; break;
 1933:         case  SIG_FAIL:   g_log_string[q_idx]='S'; break;
 1934:         case  UNIT_FAIL:  g_log_string[q_idx]='U'; break;
 1935:         case  UNIT_NOTNEEDED:  g_log_string[q_idx]='U'; break;
 1936:         case  NO_UNIT:    g_log_string[q_idx]='u'; break;
 1937:         case  BAD_FORMULA:  g_log_string[q_idx]='F'; break;
 1938:         case  INCORRECT:  g_log_string[q_idx]='N'; break;
 1939:       }
 1940:     }
 1941:   }
 1942: }
 1943: 
 1944: void submit_subjective(int q_idx,Problem_t *p)
 1945: {
 1946:   char buf[MAX_BUFFER_SIZE];
 1947:   if (capa_set_subjective(g_login_set,q_idx+1,g_student_number,
 1948: 			  g_stu_ans_pp[q_idx+1]->a_str)<0){
 1949:     sprintf(buf,"<p><tt>Falied to record last submission.</tt><br>\n");
 1950:     g_tried[q_idx]--;
 1951:   } else {
 1952:     sprintf(buf,"<p><tt>Current submission recorded.</tt><br>\n");
 1953:     g_new_answerdb[q_idx] = '0';
 1954:     g_log_string[q_idx]='A';
 1955:   }
 1956:   append_qtext(buf);
 1957:   if (g_tried[q_idx]<p->tries) {
 1958:     create_answer_area(p,q_idx);
 1959:     if( p->tries - g_tried[q_idx] == 1) {
 1960:       append_qtext("<br><tt>ONE try left</tt>\n");
 1961:     }else{
 1962:       sprintf(buf,"<br><tt>tries %d/%d</tt>\n",g_tried[q_idx],p->tries);
 1963:       append_qtext(buf);
 1964:     }  
 1965:   }else{
 1966:     display_last_answer(q_idx);
 1967:     append_qtext("<br><tt>No more tries.</tt>\n");
 1968:   } 
 1969: }
 1970: 
 1971: void
 1972: check_user_ans(int q_idx,Problem_t *p)
 1973: {
 1974: int a_tpe,cnt;
 1975: char *c_ans,**ans;
 1976: int   t_tpe;
 1977: double tol;
 1978: int    sig_l;
 1979: int    sig_u;
 1980: char  *a_fmt;
 1981: int    tries;
 1982:   char       buf[MAX_BUFFER_SIZE];
 1983:   
 1984:   a_tpe = p->ans_type;
 1985:   t_tpe = p->tol_type;
 1986:   tol   = p->tolerance;
 1987:   sig_l = p->sig_lbound;
 1988:   sig_u = p->sig_ubound;
 1989:   a_fmt = p->ans_fmt;
 1990:   tries = p->tries;
 1991: 
 1992: #ifdef CGI_DBUG
 1993:   fprintf(g_cgi,"Enter check_user_ans() anstype=%d\n",a_tpe); fflush(g_cgi);
 1994: #endif /* CGI_DBUG */
 1995: 
 1996:   g_tried[q_idx]++;
 1997: 
 1998: #ifdef CGI_DBUG
 1999:   fprintf(g_cgi,"Call capa_check_answer() with [%s]\n",g_stu_ans_pp[q_idx+1]->a_str); fflush(g_cgi);
 2000: #endif /* CGI_DBUG */
 2001:   if (a_tpe==ANSWER_IS_SUBJECTIVE) {
 2002:     submit_subjective(q_idx,p);
 2003:     return;
 2004:   }
 2005: 
 2006:   cnt=gather_answers(&ans,q_idx,p);
 2007:   if (cnt == -1) {
 2008:     g_tried[q_idx]--;
 2009:     create_answer_area(p,q_idx);
 2010:     if( (tries - g_tried[q_idx]) == 1 ) {
 2011:       append_qtext("<br><tt>Not all answers submitted, ONE try left!!</tt>\n");
 2012:     }else{
 2013:       sprintf(buf,"<br><tt>Not all answers submitted, tries %d/%d.</tt>\n",
 2014: 	      g_tried[q_idx],tries);
 2015:       append_qtext(buf);
 2016:     }
 2017:     return;
 2018:   }
 2019: 
 2020:   switch( capa_check_answers(p,ans,cnt) ) {
 2021:     case  EXACT_ANS:    
 2022:     case  APPROX_ANS: 
 2023:                    c_ans=answers_string(ANSWER_STRING_MODE, p);
 2024:                    sprintf(buf,"<p><tt>Yes, Computer gets: %s</tt>\n", c_ans);
 2025:                    append_qtext(buf);
 2026:                    g_new_answerdb[q_idx] = 'Y';
 2027:                    g_log_string[q_idx]='Y';
 2028: 		   capa_mfree(c_ans);
 2029: 		   break;
 2030:     case  SIG_FAIL:
 2031:                    create_answer_area(p,q_idx);
 2032: 		   g_tried[q_idx]--;  /* don't count as a try */
 2033: 		   sprintf(buf,"<br><tt>Please adjust significant figures, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
 2034: 		   append_qtext(buf);
 2035: 		   g_new_answerdb[q_idx] = 'N';
 2036:                    g_log_string[q_idx]='S';
 2037:                    break;
 2038:     case  UNIT_FAIL:
 2039:                    create_answer_area(p,q_idx);
 2040:                    g_tried[q_idx]--;  /* don't count as a try */
 2041: 		   sprintf(buf,"<br><tt>Units incorrect, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
 2042: 		   append_qtext(buf);
 2043: 		   g_new_answerdb[q_idx] = 'N';
 2044:                    g_log_string[q_idx]='U';
 2045:                    break;
 2046:     case  UNIT_NOTNEEDED:
 2047:                    create_answer_area(p,q_idx);
 2048:                    g_tried[q_idx]--;  /* don't count as a try */
 2049:                    if(tries > 0) {
 2050:                      sprintf(buf,"<br><tt>Only a number required, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
 2051:                      append_qtext(buf);
 2052:                    }
 2053:                    g_new_answerdb[q_idx] = 'N';
 2054:                    g_log_string[q_idx]='U';
 2055:                    break;
 2056:     case  NO_UNIT: 
 2057:                    create_answer_area(p,q_idx);
 2058:                    g_tried[q_idx]--;  /* don't count as a try */
 2059: 		   sprintf(buf,"<br><tt>Units required, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
 2060: 		   append_qtext(buf);
 2061: 		   g_new_answerdb[q_idx] = 'N';
 2062:                    g_log_string[q_idx]='u';
 2063:                    break;
 2064:     case  BAD_FORMULA: 
 2065:                    create_answer_area(p,q_idx);
 2066:                    g_tried[q_idx]--;  /* don't count as a try */
 2067: 		   sprintf(buf,"<br><tt>Unable to understand formula, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
 2068: 		   append_qtext(buf);
 2069: 		   g_new_answerdb[q_idx] = 'N';
 2070:                    g_log_string[q_idx]='F';
 2071:                    break;
 2072:     case  INCORRECT:
 2073:                    if( g_tried[q_idx] < tries ) {
 2074: 		     create_answer_area(p,q_idx);
 2075: 		     if( (tries - g_tried[q_idx]) == 1 ) {
 2076: 		       append_qtext("<br><tt>Incorrect, ONE try left!!</tt>\n");
 2077: 		     }else{
 2078: 		       sprintf(buf,"<br><tt>Incorrect, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
 2079: 		       append_qtext(buf);
 2080: 		     }
 2081: 		   } else {
 2082: 		     display_last_answer(q_idx);
 2083: 		     append_qtext("<br><tt>Incorrect, no more tries.</tt>\n");
 2084: 		   }
 2085:                    g_new_answerdb[q_idx] = 'N';
 2086:                    g_log_string[q_idx]='N';
 2087:                    break;
 2088:   }
 2089: }
 2090: 
 2091: void
 2092: check_inhibited_user_ans(int q_idx,Problem_t *p)
 2093: {
 2094: int a_tpe,cnt;
 2095: char *c_ans,**ans;
 2096: int   t_tpe;
 2097: double tol;
 2098: int    sig_l;
 2099: int    sig_u;
 2100: char  *a_fmt;
 2101: int    tries;
 2102:   char       buf[MAX_BUFFER_SIZE];
 2103:   
 2104:   a_tpe = p->ans_type;
 2105:   c_ans = p->answer;
 2106:   t_tpe = p->tol_type;
 2107:   tol   = p->tolerance;
 2108:   sig_l = p->sig_lbound;
 2109:   sig_u = p->sig_ubound;
 2110:   a_fmt = p->ans_fmt;
 2111:   tries = p->tries;
 2112:   
 2113: #ifdef CGI_DBUG
 2114:   fprintf(g_cgi,"Enter check_inhibited_user_ans() anstype=%d\n",a_tpe); fflush(g_cgi);
 2115: #endif /* CGI_DBUG */
 2116: 
 2117:   g_tried[q_idx]++;
 2118: 
 2119: #ifdef CGI_DBUG
 2120:   fprintf(g_cgi,"Call capa_check_answer() with [%s]\n",g_stu_ans_pp[q_idx+1]->a_str); 
 2121:   fflush(g_cgi);
 2122: #endif /* CGI_DBUG */
 2123:   if (a_tpe==ANSWER_IS_SUBJECTIVE) {
 2124:     submit_subjective(q_idx,p);
 2125:     return;
 2126:   }
 2127: 
 2128:   cnt=gather_answers(&ans,q_idx,p);
 2129:   if (cnt == -1) {
 2130:     g_tried[q_idx]--;
 2131:     create_answer_area(p,q_idx);
 2132:     if( (tries - g_tried[q_idx]) == 1 ) {
 2133:       append_qtext("<br><tt>Not all answers submitted, ONE try left!!</tt>\n");
 2134:     }else{
 2135:       sprintf(buf,"<br><tt>Not all answers submitted, tries %d/%d.</tt>\n",
 2136: 	      g_tried[q_idx],tries);
 2137:       append_qtext(buf);
 2138:     }
 2139:     return;
 2140:   }
 2141: 
 2142:   switch( capa_check_answers(p,ans,cnt) ) {
 2143:     case  EXACT_ANS:
 2144:     case  APPROX_ANS: 
 2145:                    g_new_answerdb[q_idx] = 'Y';
 2146:                    g_log_string[q_idx]='Y';
 2147:                    break;
 2148:     case  SIG_FAIL:
 2149:                    g_new_answerdb[q_idx] = 'N';
 2150:                    g_log_string[q_idx]='S';
 2151:                    break;
 2152:     case  UNIT_FAIL:
 2153:                    g_new_answerdb[q_idx] = 'N';
 2154:                    g_log_string[q_idx]='U';
 2155:                    break;
 2156:     case  UNIT_NOTNEEDED:
 2157:                    g_new_answerdb[q_idx] = 'N';
 2158:                    g_log_string[q_idx]='U';
 2159:                    break;
 2160:     case  NO_UNIT:
 2161:                    g_new_answerdb[q_idx] = 'N';
 2162:                    g_log_string[q_idx]='u';
 2163:                    break;
 2164:     case  BAD_FORMULA:
 2165:                    g_new_answerdb[q_idx] = 'N';
 2166:                    g_log_string[q_idx]='F';
 2167:                    break;
 2168:     case  INCORRECT:
 2169:                    g_new_answerdb[q_idx] = 'N';
 2170:                    g_log_string[q_idx]='N';
 2171:                    break;
 2172:   }
 2173: 
 2174:   if( g_tried[q_idx] < tries ) {
 2175:     create_answer_area(p,q_idx);
 2176:     if( (tries - g_tried[q_idx]) == 1 ) {
 2177:       append_qtext("<br><tt>Answered, ONE try left!!</tt>\n");
 2178:     }else{
 2179:       sprintf(buf,"<br><tt>Answered, tries %d/%d.</tt>\n",g_tried[q_idx],tries);
 2180:       append_qtext(buf);
 2181:     }
 2182:   } else {
 2183:     display_last_answer(q_idx);
 2184:     append_qtext("<br><tt>Answered, no more tries.</tt>\n");
 2185:   }
 2186: }
 2187: 
 2188: void                                     /* RETURNS: (nothing)          */
 2189: print_summary(class_dir,class,student_number,pin,set)
 2190: char *class_dir;char *class;char *student_number;int pin;int set;
 2191: {                                       /* LOCAL VARIABLES:            */
 2192:   int      set_idx,                     /*    Set counter              */
 2193:            i,                           /*    Question counter         */
 2194:            set_score,                   /*    Score on a set           */
 2195:            term_score=0,                /*    Total points received    */
 2196:            term_valid=0,                /*    Total points possible    */
 2197:            result,
 2198:            tot_num_sets=0;
 2199:   T_entry  entry;                       /*    Database entry for a set */
 2200:   char     buf[MAX_BUFFER_SIZE]; /* Output line buffer  */
 2201:   char     buf2[MAX_BUFFER_SIZE]; /* Output line buffer  */
 2202:   T_header header;                      /*    Problem set header       */
 2203:   int      question_cnt,valid_wgt, rate,configResult,
 2204:     status_line_length=DEFAULT_STATUS_LINE_LENGTH,row;
 2205:   char     class_fullpath[ONE_K],*serverName;
 2206: 
 2207:   serverName=getenv("SERVER_NAME");
 2208:   if (!serverName) {
 2209:     fprintf(stdout,"Enviroment variable SERVER_NAME not set.\n");
 2210:     fprintf(stdout,"Unable to complete actions.\n");
 2211:     return;
 2212:   }
 2213:   printf("<!--print_summary-->");
 2214:   sprintf(class_fullpath,"%s/%s",class_dir,class);
 2215:   chdir(class_fullpath);
 2216:   configResult=read_capa_config("web_status_line_length",buf);
 2217:   if (configResult != 0 && configResult != -1 ) {
 2218:     if (sscanf(buf,"%d",&status_line_length)==0) {
 2219:       status_line_length=DEFAULT_STATUS_LINE_LENGTH;
 2220:     }
 2221:   } else {
 2222:     status_line_length=DEFAULT_STATUS_LINE_LENGTH;
 2223:   }
 2224:   
 2225:   printf("<TABLE>\n<TR><TD></TD>\n");
 2226:   for(i=0;i<status_line_length;i++) {
 2227:     printf("<TD align=center valign=bottom>%d</TD>\n",i+1);
 2228:   }
 2229:   printf("</TR>");
 2230: 
 2231:   for (set_idx=1; set_idx<=set; set_idx++) {
 2232:     g_inhibit_response=capa_check_option(OPTION_INHIBIT_RESPONSE,set_idx,
 2233: 					 g_student_data.s_sec);
 2234:     if (g_inhibit_response > 0) {
 2235:       printf("<!-- Set %d is inhibited -->\n",set_idx);
 2236:       continue;
 2237:     }
 2238:     if ( capa_check_date(CHECK_OPEN_DATE,g_student_number,
 2239: 			     g_student_data.s_sec,set_idx) < 0 ){
 2240:       printf("<!-- Set %d is not open -->\n",set_idx);
 2241:       continue;
 2242:     }
 2243: 
 2244:     if (capa_get_header(&header,set_idx))  return;
 2245:     tot_num_sets++;
 2246:     capa_get_entry(&entry,student_number,set_idx);
 2247:     sscanf(header.num_questions,"%d", &(question_cnt) );
 2248:     valid_wgt = 0; set_score = 0;
 2249:     header.weight[question_cnt] = '\0';
 2250:     header.partial_credit[question_cnt] = '\0';
 2251:     for (i=0; i<question_cnt; i++) {
 2252:       valid_wgt +=  (header.weight[i] - '0');
 2253:       if((entry.answers[i]=='Y') || (entry.answers[i]=='y'))  
 2254: 	set_score += (header.weight[i]-'0');
 2255:       if((entry.answers[i]=='E') || (entry.answers[i]=='e'))  
 2256: 	valid_wgt -= (header.weight[i] - '0');
 2257:       if((entry.answers[i]>='0') && (entry.answers[i]<='9'))  
 2258: 	set_score += (entry.answers[i] - '0');
 2259:     }
 2260:     term_valid += valid_wgt;
 2261:     term_score += set_score;
 2262: 
 2263:     if( valid_wgt != 0 ) {
 2264:       rate = 100*set_score / valid_wgt;
 2265:       printf("<TR><TD nowrap align=center valign=bottom>set <B>%d</B>, %d/%d(%d %%)  </TD>",set_idx,set_score,valid_wgt,rate);
 2266:     } else {
 2267:       printf("<TR><TD nowrap align=center valign=bottom>set <B>%d</B>,   0/0(0 %%)   </TD>",set_idx);
 2268:     }
 2269:     for(row=0;row<=(question_cnt/status_line_length);row++) {
 2270:       for(i=(row*status_line_length);
 2271: 	  ((i<question_cnt)&&(i<((row+1)*status_line_length))); i++) {
 2272: 	if (i != 0 && (!(i%status_line_length))) { printf("</TR><TD></TD>"); }
 2273: 	printf("<TD align=center valign=bottom><tt>%c</tt></TD>\n",entry.answers[i]);
 2274:       }
 2275:       printf("</TR>\n<TR><TD></TD>");
 2276:       for(i=(row*status_line_length);
 2277: 	  ((i<question_cnt)&&(i<((row+1)*status_line_length))); i++) {
 2278: 	if (i != 0 && (!(i%status_line_length))) { printf("</TR><TD></TD>"); }
 2279: 	printf("<TD align=center valign=bottom><tt>%c</tt></TD>\n",header.weight[i]);
 2280:       }
 2281:     }
 2282:     printf("</TR>");
 2283:     capa_mfree(header.weight);
 2284:     capa_mfree(header.partial_credit);
 2285:   }
 2286:   printf("\n</TABLE>\n<hr>\n");
 2287:   /* SHOW TOTALS */
 2288:   /* if capalogin_show_summary_score is set to none don't show it */
 2289:   if (term_valid > 0) {
 2290:     sprintf(buf,"%d sets, total = %3d/%3d (%d%%)\n", tot_num_sets, term_score, term_valid, 100*term_score/term_valid);
 2291:   } else {
 2292:     sprintf(buf,"%d sets, total = %3d/%3d\n", tot_num_sets, term_score, term_valid);
 2293:   }
 2294:   result=read_capa_config("capalogin_show_summary_score",buf2);
 2295:   if (result != 0 && result != -1) {
 2296:     if (strcasecmp(buf2,"none")==0) {
 2297:     } else {
 2298:       printf("%s",buf);
 2299:     }
 2300:   } else {
 2301:     printf("%s",buf);
 2302:   }
 2303: 
 2304:   printf("<TABLE cellpadding=0 cellspacing=0 border=0>\n<TR><TD>");
 2305:   printf("<form method=\"post\" ");
 2306:   sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
 2307:   printf("%s\n", buf);
 2308:   printf("<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
 2309:   printf("<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
 2310:   printf("<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
 2311:   printf("<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_CHECKIN);
 2312:   printf("<input type=\"submit\" value=\"Main menu\" ></form></TD>\n");
 2313:   printf("<TD><form method=\"get\" action=\"http://%s/CAPA/class.html\">",serverName); 
 2314:   printf("<input type=\"button\" value=\"Exit\" onclick=\"window.close()\"></form></TD>");
 2315:   printf("\n</TABLE>\n");
 2316: }
 2317: 
 2318: 
 2319: void
 2320: process_mode(int mode) {
 2321: 
 2322: #ifdef CGI_DBUG
 2323:   fprintf(g_cgi,"entered process_mode[%d]\n",mode); fflush(g_cgi);
 2324: #endif /* CGI_DBUG */
 2325:   g_qchar_cnt=g_schar_cnt=0;
 2326:   g_qsize=TEXT_BUF_SIZE*sizeof(char);
 2327:   g_ssize=STATUS_BUF_SIZE*sizeof(char);
 2328:   g_question_txt=capa_malloc(TEXT_BUF_SIZE,sizeof(char));
 2329:   g_status_txt  =capa_malloc(STATUS_BUF_SIZE,sizeof(char));
 2330: #ifdef CGI_DBUG
 2331:   fprintf(g_cgi,"alloced everything\n"); fflush(g_cgi);
 2332: #endif /* CGI_DBUG */
 2333:   if( mode == VIEW_PREVIOUS_MODE ) {
 2334:     print_quizz(g_cpath,g_cowner,g_class_name,g_student_number, g_entered_pin, g_vset,mode); 
 2335:   } else if( mode == TRY_SET_MODE ) {
 2336:     print_quizz(g_cpath,g_cowner,g_class_name,g_student_number, g_entered_pin, g_login_set,mode);
 2337:   } else {
 2338:     print_quizz(g_cpath,g_cowner,g_class_name,g_student_number,g_entered_pin,g_login_set,CHECK_ANSWER_MODE);
 2339:   }
 2340:   g_status_txt[g_schar_cnt]=0;
 2341:   g_question_txt[g_qchar_cnt]=0;
 2342:   if( g_schar_cnt != 0 ) {
 2343:          fprintf(stdout,"%s",g_status_txt);
 2344: #ifdef CGI_DBUG
 2345:   fprintf(g_cgi,"print status [%s]\n",g_status_txt); fflush(g_cgi);
 2346: #endif /* CGI_DBUG */
 2347:   }
 2348:   if( g_qchar_cnt != 0) {
 2349:          fprintf(stdout,"%s",g_question_txt);
 2350: #ifdef CGI_DBUG
 2351:   fprintf(g_cgi,"print question [%s]\n",g_question_txt); fflush(g_cgi);
 2352: #endif /* CGI_DBUG */
 2353:   }
 2354:   if( g_schar_cnt != 0 ) {
 2355:          fprintf(stdout,"%s",g_status_txt);
 2356:   }
 2357:   fflush(stdout);
 2358:   capa_mfree(g_status_txt);
 2359:   capa_mfree(g_question_txt);
 2360: 
 2361: }
 2362: 
 2363: /*  mode could be exam summary, show or not show percentage */
 2364: /*                quiz summary, show or not show percentage */
 2365: void
 2366: process_summary(int mode)
 2367: {
 2368:   int   outcome;
 2369:   int   i, len;
 2370:   char *c_name;
 2371:   char  c_path[512];
 2372:   
 2373:   outcome = check_exam_quiz_path();
 2374:   if( (mode == M_EXAMSUMM) && (outcome & 1) ) {  /* exam summary */
 2375:     c_name = rindex(g_exam_path,'/');
 2376:     c_name++;
 2377:     i = strlen(c_name);
 2378:     len = strlen(g_exam_path) - i - 1;
 2379:     for(i=0;i<len;i++) {
 2380:       c_path[i]=g_exam_path[i];
 2381:     }
 2382:     c_path[len]=0;
 2383:     print_summary(c_path,c_name,g_student_number, g_entered_pin, g_exam_set);
 2384:   }
 2385:   if( (mode == M_QUIZSUMM) && (outcome & 2) ) {  /* quiz summary */
 2386:     c_name = rindex(g_quiz_path,'/');
 2387:     c_name++;
 2388:     i = strlen(c_name);
 2389:     len = strlen(g_quiz_path) - i - 1;
 2390:     for(i=0;i<len;i++) {
 2391:       c_path[i]=g_quiz_path[i];
 2392:     }
 2393:     c_path[len]=0;
 2394:     print_summary(c_path,c_name,g_student_number, g_entered_pin, g_quiz_set);
 2395:   }
 2396:   
 2397: }
 2398: 
 2399: /* ---------------- JAVA TScore.class page print out ----------------- */
 2400: void                                     /* RETURNS: (nothing)          */
 2401: print_termscore_page(class_dir,class,student_number,pin,set,out)
 2402: char *class_dir;char *class;char *student_number;int pin;int set; /* student login set */
 2403: FILE *out;
 2404: {                                       /* LOCAL VARIABLES:            */
 2405:   int      set_idx,                     /*    Set counter              */
 2406:            i,                           /*    Question counter         */
 2407:            set_score,                   /*    Score on a set           */
 2408:            term_score=0,                /*    Total points received    */
 2409:            term_valid=0;                /*    Total points possible    */
 2410:   T_entry  entry;                       /*    Database entry for a set */
 2411:   char     buf[MAX_BUFFER_SIZE]; /* Output line buffer  */
 2412:   T_header header;                      /*    Problem set header       */
 2413:   int      question_cnt,valid_wgt,configResult;
 2414:   char     class_fullpath[ONE_K],*serverName;
 2415:   int      hw_c, hw_r, qz_c, qz_r, fs, homework_count, quiz_count;
 2416:   float    hw_w, qz_w, ex_w, fe_w, pc_w;
 2417:   int      idx, entry_count, tmp_len;
 2418:   float    *S, *F;
 2419:   int      *X;
 2420:   char     *capa_server;
 2421:   int      max_set[4];
 2422:   char     **c_path_pp;
 2423: 
 2424:   /*Unused Vars  
 2425:     char     buf2[MAX_BUFFER_SIZE]; 
 2426:     char *qz_p, *ex_p, *epc_p; 
 2427:     int      ex_c, epc_c, result;
 2428:     int  rate, status_line_length=DEFAULT_STATUS_LINE_LENGTH,row;
 2429:   */
 2430: 
 2431:   serverName=getenv("SERVER_NAME");
 2432:   if (!serverName) {
 2433:     fprintf(out,"Enviroment variable SERVER_NAME not set.\n");
 2434:     fprintf(out,"Unable to complete actions.\n");
 2435:     return;
 2436:   }
 2437: 
 2438:   sprintf(class_fullpath,"%s/%s",class_dir,class);
 2439:   chdir(class_fullpath);
 2440:   
 2441:   /*
 2442:      change the working director to the major homework directory and begin to
 2443:      read off the remaining path informations from this capa.config file 
 2444:      homework_path   = 
 2445:      quiz_path   = 
 2446:      exam_path   = 
 2447:      correction_path = 
 2448:      homework_weight   = 0.3
 2449:      quiz_weight       = 0.7
 2450:      exam_weight       = 0.3
 2451:      final_weight      = 0.35
 2452:      correction_weight = 0.3
 2453:      final_exam_set_number = 4
 2454:      homework_count    = 12
 2455:      quiz_count        = 24
 2456:      
 2457:   */
 2458:   
 2459:   configResult=read_capa_config("capa_server",buf);
 2460:   if (configResult != 0 && configResult != -1 ) {
 2461:     tmp_len = strlen(buf) + 1;
 2462:     capa_server =   (char *)capa_malloc( tmp_len, sizeof(char));
 2463:     sprintf(capa_server,"%s",buf);
 2464:   } else { /* if capa_server is not set then we won't do anything further */
 2465:     fprintf(out,"Parameter: capa_server in capa.config file are not properly set.\n");
 2466:     return ;
 2467:   }
 2468:   if( get_termscore_params(&hw_w,&qz_w,&ex_w,&fe_w,&pc_w,&homework_count,&quiz_count,&fs) == -1 ) {
 2469:     fprintf(out,"Parameters in capa.config file are not properly set.\n");
 2470:     fprintf(out," such as homework_weight, quiz_weight, exam_weight, final_weight, correction_weight.\n");
 2471:     
 2472:     return;
 2473:   }
 2474:   c_path_pp = (char **)capa_malloc( 4, sizeof(char *));
 2475:   tmp_len = strlen(class_fullpath) + 1;
 2476:   c_path_pp[0] = (char *)capa_malloc(tmp_len,sizeof(char));
 2477:   sprintf(c_path_pp[0],"%s",class_fullpath); /* c_path_pp[0] should always be there */
 2478:   
 2479:   entry_count = fs*2 + 1;
 2480:   S = (float *)capa_malloc( ((fs+1)*2), sizeof(float));
 2481:   F = (float *)capa_malloc( ((fs+1)*2), sizeof(float));
 2482:   X =   (int *)capa_malloc( ((fs+1)*2), sizeof(int));
 2483:   
 2484:   max_set[0] = set;  /* the login set number */
 2485:   hw_c = max_set[0];
 2486:   hw_r = homework_count - set;
 2487:   
 2488:   
 2489:   configResult=read_capa_config("quiz_path",buf);
 2490:   if (configResult != 0 && configResult != -1 ) {
 2491:     tmp_len = strlen(buf)+1;
 2492:     c_path_pp[1] = (char *)capa_malloc(tmp_len,sizeof(char));
 2493:     sprintf(c_path_pp[1],"%s",buf);
 2494:     max_set[1] = check_class_get_maxset(c_path_pp[1]);
 2495:     if( max_set[1] <= 0 ) {
 2496:       /* should we continue ? */
 2497:       max_set[1] = 1;
 2498:       X[1] = 1;
 2499:     }
 2500:     qz_c = max_set[1];
 2501:     qz_r = quiz_count - max_set[1];
 2502:   } else { /* if quiz_path is not in capa.config, then we will skip quizz */
 2503:     qz_c = 0;
 2504:     qz_r = 0;
 2505:   }
 2506:   
 2507:   
 2508:   configResult=read_capa_config("exam_path",buf);
 2509:   if (configResult != 0 && configResult != -1 ) {
 2510:     tmp_len = strlen(buf)+1;
 2511:     c_path_pp[2] = (char *)capa_malloc( (tmp_len),sizeof(char));
 2512:     sprintf(c_path_pp[2],"%s",buf);
 2513:     max_set[2] = check_class_get_maxset(c_path_pp[2]);
 2514:     if( max_set[2] <= 0 ) {
 2515:       /* should we continue ? */
 2516:       max_set[2] = 0;
 2517:       for(idx=2;idx <= (fs*2); idx++) {
 2518:         X[idx] = 1;
 2519:       }
 2520:     }
 2521:   } else { /* if exam_path is not in capa.config, then skip exams */
 2522:     fs = 0;
 2523:   }
 2524:   configResult=read_capa_config("correction_path",buf);
 2525:   if (configResult != 0 && configResult != -1 ) {
 2526:     tmp_len = strlen(buf)+1;
 2527:     c_path_pp[3] = (char *)capa_malloc(tmp_len,sizeof(char));
 2528:     sprintf(c_path_pp[3],"%s",buf);
 2529:     max_set[3] = check_class_get_maxset(c_path_pp[3]);
 2530:     if( max_set[3] <= 0 ) {
 2531:       /* should we continue ? */
 2532:       max_set[3] = 0;
 2533:       
 2534:     }
 2535:   } else { /* if correction_path is not in capa.config, then skip corrections */
 2536:     pc_w = 0.0;
 2537:   }
 2538:   
 2539:   
 2540:   
 2541:   for( idx = 0; idx < 4; idx++) {
 2542:      if( c_path_pp[idx] != NULL ) {
 2543:        chdir(c_path_pp[idx]);
 2544:        term_score=0;
 2545:        term_valid=0;
 2546:        for (set_idx=1; set_idx<=max_set[idx]; set_idx++) {
 2547:           if (capa_get_header(&header,set_idx))  return;
 2548:           capa_get_entry(&entry,student_number,set_idx);
 2549:           sscanf(header.num_questions,"%d", &(question_cnt) );
 2550:           valid_wgt = 0; set_score = 0;
 2551:           header.weight[question_cnt] = '\0';
 2552:           header.partial_credit[question_cnt] = '\0';
 2553:           for (i=0; i<question_cnt; i++) {
 2554:             valid_wgt +=  (header.weight[i] - '0');
 2555:             if((entry.answers[i]=='Y') || (entry.answers[i]=='y'))  
 2556: 	       set_score += (header.weight[i]-'0');
 2557:             if((entry.answers[i]=='E') || (entry.answers[i]=='e'))  
 2558: 	       valid_wgt -= (header.weight[i] - '0');
 2559:             if((entry.answers[i]>='0') && (entry.answers[i]<='9'))  
 2560: 	       set_score += (entry.answers[i] - '0');
 2561:           }
 2562:           term_valid += valid_wgt;
 2563:           term_score += set_score;
 2564:           capa_mfree(header.weight);
 2565:           capa_mfree(header.partial_credit);
 2566:           if(idx==2) { /* exam sets */
 2567:             S[set_idx*2] = (float)set_score;
 2568:             F[set_idx*2] = (float)valid_wgt;
 2569:             X[set_idx*2] = 0;
 2570:           }
 2571:           if(idx==3) { /* correction sets */
 2572:             S[set_idx*2+1] = (float)set_score;
 2573:             F[set_idx*2+1] = (float)valid_wgt;
 2574:             X[set_idx*2+1] = 0;
 2575:           }
 2576:        }
 2577:        if( (idx == 0) || (idx==1) ) { /* homeworks and quizzes */
 2578:          S[idx] = (float)term_score;
 2579:          F[idx] = (float)term_valid;
 2580:          X[idx] = 1;
 2581:        }
 2582:      }
 2583:   }
 2584:   
 2585:   
 2586: 
 2587:   
 2588:   fprintf(out,"<CENTER>\n");
 2589:   fprintf(out,"<APPLET CODE=TScore.class CODEBASE=\"http://%s\" width=600 height=750>\n",capa_server);
 2590:   fprintf(out,"<PARAM NAME=\"HW_W\"  VALUE=\"%f\">\n", hw_w);
 2591:   fprintf(out,"<PARAM NAME=\"QZ_W\"  VALUE=\"%f\">\n", qz_w);
 2592:   fprintf(out,"<PARAM NAME=\"EX_W\"  VALUE=\"%f\">\n", ex_w);
 2593:   fprintf(out,"<PARAM NAME=\"FE_W\"  VALUE=\"%f\">\n", fe_w);
 2594:   fprintf(out,"<PARAM NAME=\"PC_W\"  VALUE=\"%f\">\n", pc_w);
 2595:   fprintf(out,"<PARAM NAME=\"HW_C\"  VALUE=\"%d\">\n", hw_c);
 2596:   fprintf(out,"<PARAM NAME=\"HW_R\"  VALUE=\"%d\">\n", hw_r);
 2597:   fprintf(out,"<PARAM NAME=\"FS\"    VALUE=\"%d\">\n", fs);
 2598:   fprintf(out,"<PARAM NAME=\"QZ_C\"  VALUE=\"%d\">\n", qz_c);
 2599:   fprintf(out,"<PARAM NAME=\"QZ_R\"  VALUE=\"%d\">\n", qz_r);
 2600:   
 2601: 
 2602:   for(idx=0;idx<entry_count;idx++) {
 2603:     fprintf(out,"<PARAM NAME=\"S%d\"  VALUE=\"%f\">\n",idx,S[idx]);
 2604:     fprintf(out,"<PARAM NAME=\"F%d\"  VALUE=\"%f\">\n",idx,F[idx]);
 2605:     fprintf(out,"<PARAM NAME=\"X%d\"  VALUE=\"%d\">\n",idx,X[idx]);
 2606:   }
 2607:   
 2608:   fprintf(out,"</APPLET> </CENTER>\n");
 2609:   
 2610:   fprintf(out,"<TABLE cellpadding=0 cellspacing=0 border=0>\n<TR><TD>");
 2611:   fprintf(out,"<form method=\"post\" ");
 2612:   sprintf(buf,"action=\"http://%s/%s/%s/capasbin\">",serverName,g_cgibin_path,g_cowner);
 2613:   fprintf(out,"%s\n", buf);
 2614:   fprintf(out,"<input type=\"hidden\" name=\"CLASS\" value=\"%s\">\n",g_class_name);
 2615:   fprintf(out,"<input type=\"hidden\" name=\"SNUM\" value=\"%s\">\n",g_student_number);
 2616:   fprintf(out,"<input type=\"hidden\" name=\"CAPAID\" value=\"%d\">\n",g_entered_pin);
 2617:   fprintf(out,"<input type=\"hidden\" name=\"M\" value=\"%d\">\n",M_CHECKIN);
 2618:   fprintf(out,"<input type=\"submit\" value=\"Main menu\" ></form></TD>\n");
 2619:   fprintf(out,"<TD><form method=\"get\" action=\"http://%s/CAPA/class.html\">",serverName); 
 2620:   fprintf(out,"<input type=\"button\" value=\"Exit\" onclick=\"window.close()\"></form></TD>");
 2621:   fprintf(out,"\n</TABLE>\n");
 2622:   
 2623:   capa_mfree((char *)S);
 2624:   capa_mfree((char *)F);
 2625:   capa_mfree((char *)X);
 2626:   for(idx=0;idx<4;idx++) {
 2627:     if( c_path_pp[idx] != NULL )  capa_mfree((char *)c_path_pp[idx]);
 2628:   }
 2629:   capa_mfree((char *)c_path_pp);
 2630:   capa_mfree((char *)capa_server);
 2631: }
 2632: 
 2633: 
 2634: 
 2635: int
 2636: get_termscore_params(hw,qw,ew,fw,pw,hc,qc,fs) 
 2637: float *hw;float *qw;float *ew;float *fw;float *pw;
 2638: int   *hc;int   *qc;int   *fs;
 2639: {
 2640:   char     buf[MAX_BUFFER_SIZE]; /* Output line buffer  */
 2641:   int      hw_c, qz_c, fe_s;
 2642:   float    hw_w, qz_w, ex_w, fe_w, pc_w;
 2643:   int      configResult;
 2644:   
 2645:   configResult=read_capa_config("homework_weight",buf);
 2646:   if (configResult != 0 && configResult != -1 ) {
 2647:     sscanf(buf,"%f", &hw_w);
 2648:     if(hw_w <= 0.0 )  {
 2649:       hw_w = DEFAULT_HW_W;
 2650:     }
 2651:   } else {
 2652:     return (-1);
 2653:   }
 2654:   configResult=read_capa_config("quiz_weight",buf);
 2655:   if (configResult != 0 && configResult != -1 ) {
 2656:     sscanf(buf,"%f", &qz_w);
 2657:     if(qz_w <= 0.0 )  {
 2658:       qz_w = DEFAULT_QZ_W;
 2659:     }
 2660:   } else {
 2661:     return (-1);
 2662:   }
 2663:   configResult=read_capa_config("exam_weight",buf);
 2664:   if (configResult != 0 && configResult != -1 ) {
 2665:     sscanf(buf,"%f", &ex_w);
 2666:     if(ex_w <= 0.0 )  {
 2667:       ex_w = DEFAULT_EX_W;
 2668:     }
 2669:   } else {
 2670:     return (-1);
 2671:   }
 2672:   configResult=read_capa_config("final_weight",buf);
 2673:   if (configResult != 0 && configResult != -1 ) {
 2674:     sscanf(buf,"%f", &fe_w);
 2675:     if(fe_w <= 0.0 )  {
 2676:       fe_w = DEFAULT_FE_W;
 2677:     }
 2678:   } else {
 2679:     return (-1);
 2680:   }
 2681:   configResult=read_capa_config("correction_weight",buf);
 2682:   if (configResult != 0 && configResult != -1 ) {
 2683:     sscanf(buf,"%f", &pc_w);
 2684:     if(pc_w <= 0.0 )  {
 2685:       pc_w = DEFAULT_PC_W;
 2686:     }
 2687:   } else {
 2688:     return (-1);
 2689:   }
 2690:   configResult=read_capa_config("final_exam_set_number",buf);
 2691:   if (configResult != 0 && configResult != -1 ) {
 2692:     sscanf(buf,"%d", &fe_s);
 2693:     if(fe_s <= 0 )  {
 2694:       fe_s = DEFAULT_FE_NUMBER;
 2695:     }
 2696:   } else {
 2697:     return (-1);
 2698:   }
 2699:   configResult=read_capa_config("homework_count",buf);
 2700:   if (configResult != 0 && configResult != -1 ) {
 2701:     sscanf(buf,"%d", &hw_c);
 2702:     if(hw_c <= 0 )  {
 2703:       hw_c = DEFAULT_HW_COUNT;
 2704:     }
 2705:   } else {
 2706:     return (-1);
 2707:   }
 2708:   configResult=read_capa_config("quiz_count",buf);
 2709:   if (configResult != 0 && configResult != -1 ) {
 2710:     sscanf(buf,"%d", &qz_c);
 2711:     if(qz_c <= 0 )  {
 2712:       qz_c = DEFAULT_QZ_COUNT;
 2713:     }
 2714:   } else {
 2715:     return (-1);
 2716:   }
 2717:   *hw = hw_w; *qw = qz_w; *ew = ex_w; *fw = fe_w; *pw = pc_w;
 2718:   *hc = hw_c; *qc = qz_c; *fs = fe_s;
 2719:   return (0);
 2720: 
 2721: }
 2722: 
 2723: /* =================================================================================================== */

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