--- loncom/homework/caparesponse/caparesponse.c 2001/10/29 21:15:45 1.6 +++ loncom/homework/caparesponse/caparesponse.c 2004/12/23 18:36:42 1.16 @@ -1,3 +1,30 @@ +/* The LearningOnline Network with CAPA + * CAPA wrapper code + * $Id: caparesponse.c,v 1.16 2004/12/23 18:36:42 albertel Exp $ + * + * Copyright Michigan State University Board of Trustees + * + * This file is part of the LearningOnline Network with CAPA (LON-CAPA). + * + * LON-CAPA is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * LON-CAPA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LON-CAPA; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * /home/httpd/html/adm/gpl.txt + * + * http://www.lon-capa.org/ + */ + #include #include @@ -41,7 +68,7 @@ int caparesponse_capa_check_answer(char int sig_lbound,int sig_ubound, char *ans_fmt, char *unit_str, int calc, char *id_list, char *pts_list, - char *rndseed) + char *rndseed, char** reterror) { long result,seed1,seed2; @@ -49,8 +76,8 @@ int caparesponse_capa_check_answer(char char *error=NULL,filename[FILE_NAME_LENGTH]; FILE *fp; - /*fprintf(stderr,"hi in caparesponse\n");*/ - /*need to initialize unit parser*/ + /* need to initialize unit parser*/ + *reterror=NULL; sprintf(filename,"/home/httpd/html/res/adm/includes/capa.units"); if ((fp=fopen(filename,"r"))==NULL) { /* printf("Error: can't open %s\n",filename);*/ @@ -77,8 +104,11 @@ int caparesponse_capa_check_answer(char p.tolerance = tolerance; p.sig_lbound = sig_lbound; p.sig_ubound = sig_ubound; - strncpy(p.ans_fmt,ans_fmt,ANSWER_STRING_LENG-1); - if (unit_str[0]!='\0') { + + if (ans_fmt != NULL ) { + strncpy(p.ans_fmt,ans_fmt,ANSWER_STRING_LENG-1); + } + if (unit_str != NULL && unit_str[0]!='\0') { strncpy(p.unit_str,unit_str,ANSWER_STRING_LENG-1); p.ans_unit = u_parse_unit(unit_str); } else { @@ -87,10 +117,67 @@ int caparesponse_capa_check_answer(char } p.calc = calc; - result=capa_check_answer(&p,response,&error); + *reterror=error; + // Caller is expected to free reterror + // if (error!=NULL) {free(error);} - if (error!=NULL) {free(error);} + return result; +} +int caparesponse_get_real_response (char* unit_str, char* answer, + double* scaled) { + //double caparesponse_get_real_response (char* unit_str, char* answer) { + int input_len,all_alphabet,idx,outcome,result; + double n_part,scale=1.0,given,target; + char input[ANSWER_STRING_LENG],filename[FILE_NAME_LENGTH], + tmp_unit_str[ANSWER_STRING_LENG]; + Unit_t *ans_unit; + long seed1,seed2; + FILE *fp; + sprintf(filename,"/home/httpd/html/res/adm/includes/capa.units"); + if ((fp=fopen(filename,"r"))==NULL) { + /* printf("Error: can't open %s\n",filename);*/ + return (-1); + } + u_getunit(fp); + fclose(fp); + + if (unit_str != NULL && unit_str[0]!='\0') { + ans_unit = u_parse_unit(unit_str); + } else { + ans_unit=NULL; + } + input_len = strlen(answer); + all_alphabet = 1; + for(idx=0;idx 1 ) { /* with both num and unit parts or only unit part */ + if( ans_unit != NULL ) { + result = check_correct_unit(tmp_unit_str,ans_unit,&scale); + } else { + /* what to do when no unit is specified but student entered a unit? */ + result = UNIT_NOTNEEDED; + } + } else { + if( ans_unit != NULL ) { + result = NO_UNIT; + } + } + if( (result != NO_UNIT) && (!check_for_unit_fail(result)) && ( result != UNIT_NOTNEEDED) ) { + given = n_part * scale; + *scaled=given; + /* convert the given answer into proper scale for units */ + } /* end if unit check */ + } else { /* user entered a +lphabet, but no number */ + result = WANTED_NUMERIC; + } return result; }