File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.21: download - view: text, annotated - select for diffs
Mon Aug 6 19:00:13 2001 UTC (22 years, 9 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
beautify/optimize

    1: # The LON-CAPA option response handler
    2: #
    3: # Option list style responses
    4: #
    5: # YEAR=2001
    6: # 1/19,2/7,2/19,2/21,2/22,3/1,5/4,5/21,5/31,6/1,6/2,6/5,6/11 Guy Albertelli
    7: # 6/12,6/16,6/26,6/27,7/18 Guy Albertelli
    8: # 8/6 Scott Harrison
    9: 
   10: package Apache::optionresponse;
   11: use strict;
   12: use Apache::response;
   13: 
   14: # ======================================================================= BEGIN
   15: sub BEGIN {
   16:   &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
   17: }
   18: 
   19: # ================================ Start option response (return scalar string)
   20: sub start_optionresponse {
   21:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
   22:     my $result = '';
   23:     #when in a radiobutton response use these
   24:     &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil',
   25: 							'conceptgroup'));
   26:     push (@Apache::lonxml::namespace,'optionresponse');
   27:     my $id = &Apache::response::start_response($parstack,$safeeval);
   28:     if ($target eq 'edit') {
   29: 	$result .= &Apache::edit::start_table($token).
   30: 	           "<tr><td>Multiple Option Response Question</td><td>Delete:".
   31: 		   &Apache::edit::deletelist($target,$token).
   32: 		   "</td><td>&nbsp;</td></tr><tr><td colspan=\"3\">\n";
   33: 	$result .= &Apache::edit::text_arg('Max Number Of Foils:','max',
   34: 					   $token,'4')."</td></tr>";
   35: 	$result .= "<tr><td colspan=\"3\">\n";
   36:     }
   37:     if ($target eq 'modified') {
   38: 	my $constructtag = &Apache::edit::get_new_args($token,$parstack,
   39: 						       $safeeval,'max');
   40: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   41:     }
   42:     return $result;
   43: }
   44: 
   45: # ================================== End option response (return scalar string)
   46: sub end_optionresponse {
   47:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
   48:     &Apache::response::end_response;
   49:     pop @Apache::lonxml::namespace;
   50:     my $result;
   51:     if ($target eq 'edit') { $result = &Apache::edit::end_table(); }
   52:     return $result;
   53: }
   54: 
   55: # ===================================== Start foil group (return scalar string)
   56: %Apache::response::foilgroup = {};
   57: sub start_foilgroup {
   58:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
   59:     my $result = '';
   60:     %Apache::response::foilgroup = {};
   61:     $Apache::optionresponse::conceptgroup = 0;
   62:     &Apache::response::setrandomnumber();
   63:     if ($target eq 'edit') {
   64: 	my $optionlist = "<option></option>\n";
   65: 	my $option;
   66: 	my @opt;
   67: 	eval '@opt ='.
   68: 	    &Apache::lonxml::get_param('options',$parstack,$safeeval);
   69: 	my $count = 1;
   70: 	foreach $option (@opt) {
   71: 	    $optionlist .= "<option value=\"$count\">$option</option>\n";
   72: 	    $count++;
   73: 	}
   74: 	my $insertlist = &Apache::edit::insertlist($target,$token);
   75: 	$result .= &Apache::edit::start_table($token);
   76: 	$result .= (<<ENDTABLE);
   77:      <tr><td>Select Options</td>
   78:         <td>
   79: 	  Add new Option: <input type="text" name="$Apache::lonxml::curdepth.options" />
   80:         </td>
   81:         <td>Delete an Option:
   82: 	  <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
   83:         </td>
   84:      </tr>
   85:      <tr><td colspan="3">$insertlist<br />
   86: ENDTABLE
   87:     }
   88:     if ($target eq 'modified') {
   89: 	my @options;
   90: 	my $optchanged=0;
   91: 	eval '@options ='.
   92: 	    &Apache::lonxml::get_param('options',$parstack,$safeeval);
   93: 	if ($ENV{"form.$Apache::lonxml::curdepth.deleteopt"}) {
   94: 	    my $delopt=$ENV{"form.$Apache::lonxml::curdepth.deleteopt"};
   95: 	    &Apache::lonxml::debug("Deleting :$delopt:");
   96: 	    splice(@options,$delopt-1,1);
   97: 	    $optchanged = 1;
   98: 	}
   99: 	if ($ENV{"form.$Apache::lonxml::curdepth.options"}) {
  100: 	    my $newopt = $ENV{"form.$Apache::lonxml::curdepth.options"};
  101: 	    if ($options[0]) {
  102: 		push(@options,$newopt);
  103: 	    } else {
  104: 		$options[0] = $newopt;
  105: 	    }
  106: 	    $optchanged = 1;
  107: 	}
  108: 	if ($optchanged) {
  109: 	    $result = "<foilgroup options=\"(";
  110: 	    foreach my $option (@options) {
  111: 		$option =~ s/\'/\\\'/;
  112: 		&Apache::lonxml::debug("adding option :$option:");
  113: 		$result .= "'".$option."',";
  114: 	    }
  115: 	    chop $result;
  116: 	    $result .= ')">';
  117: 	} # else nothing changed so just use the default mechanism
  118:     }
  119:     return $result;
  120: }
  121: 
  122: # ======================================= End foil group (return scalar string)
  123: sub end_foilgroup {
  124:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  125:     my $result;
  126:     if ($target eq 'grade' || $target eq 'web') {
  127: 	my $name;
  128: 	my ($count,$max) = &getfoilcounts($parstack,$safeeval);
  129: 	if ($count>$max) { $count = $max } 
  130: 	&Apache::lonxml::debug("Count is $count from $max");
  131: 	my @opt;
  132: 	eval '@opt ='.
  133: 	    &Apache::lonxml::get_param('options',$parstack,$safeeval);
  134: 	&Apache::lonxml::debug("Options are $#opt");
  135: 	if ($target eq 'web') {
  136: 	    $result=&displayfoils($count,@opt);
  137: 	} elsif ( $target eq 'grade') {
  138: 	    if ( defined $ENV{'form.submitted'}) {
  139: 		my @whichopt = &whichfoils($count);
  140: 		my $temp=1;my $name;
  141: 		my $allresponse;
  142: 		my $right=0;
  143: 		my $wrong=0;
  144: 		my $ignored=0;
  145: 		foreach $name (@whichopt) {
  146: 		    my $response = $ENV{'form.HWVAL_'.
  147: 					$Apache::inputtags::response['-1'].
  148: 					":$temp"};
  149: 		    $allresponse .= "$response:";
  150: 		    if ( $response =~ /[^\s]/) {
  151: 			&Apache::lonxml::debug("submitted a $response".
  152: 					       "<br />\n");
  153: 			my $value = $Apache::response::foilgroup{$name.
  154: 								     '.value'};
  155: 			if ($value eq $response) {$right++;} else {$wrong++;}
  156: 		    } else {
  157: 			$ignored++;
  158: 		    }
  159: 		    $temp++;
  160: 		}
  161: 		my $id = $Apache::inputtags::response['-1'];
  162: 		$Apache::lonhomework::results{"resource.".
  163:  	            $Apache::inputtags::part.".$id.submission"} = $allresponse;
  164: 		&Apache::lonxml::debug("Got $right right and $wrong wrong, ".
  165: 				       "and $ignored were ignored");
  166: 		if ($wrong==0 && $ignored==0) {
  167: 		    $Apache::lonhomework::results{"resource.".
  168: 				   $Apache::inputtags::part.".$id.awarddetail"}
  169:  		                                                 = 'EXACT_ANS';
  170: 		} else {
  171: 		    $Apache::lonhomework::results{"resource.".
  172: 				   $Apache::inputtags::part.".$id.awarddetail"}
  173: 		                                                 = 'INCORRECT';
  174: 		}
  175: 	    }
  176: 	}
  177:     }
  178:     if ($target eq 'edit') {
  179: 	$result .= &Apache::edit::end_table();
  180:     }
  181:     return $result;
  182: }
  183: 
  184: # ==================================== Get foil counts (return 2 element array)
  185: sub getfoilcounts {
  186:     my ($parstack,$safeeval) = @_;
  187:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  188:     # +1 since instructors will count from 1
  189:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  190:     return ($count,$max);
  191: }
  192: 
  193: # =============================================== Which foils (return an array)
  194: sub whichfoils {
  195:     my ($max)=@_;
  196:     my @names = @{ $Apache::response::foilgroup{'names'} };
  197:     my @whichopt =();
  198:     while ((($#whichopt+1) < $max) && ($#names > -1)) {
  199: 	&Apache::lonxml::debug("Have $#whichopt max is $max");
  200: 	my $aopt=int(rand($#names+1));
  201: 	&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking ".
  202: 			       $aopt);
  203: 	$aopt=splice(@names,$aopt,1);
  204: 	&Apache::lonxml::debug("Picked $aopt");
  205: 	push (@whichopt,$aopt);
  206:     }
  207:     return @whichopt;
  208: }
  209: 
  210: # ======================================== Display foils (return scalar string)
  211: sub displayfoils {
  212:     my ($max,@opt) = @_;
  213:     my @names = @{ $Apache::response::foilgroup{'names'} };
  214:     my @truelist;
  215:     my @falselist;
  216:     my $result;
  217:     my $name;
  218:     my @whichopt = &whichfoils($max);
  219:     my $optionlist = "<option></option>\n";
  220:     my $option;
  221:     foreach $option (@opt) {
  222: 	$optionlist .= "<option>$option</option>\n";
  223:     }
  224:     if ($Apache::lonhomework::history{"resource.".
  225: 				      $Apache::inputtags::part.
  226: 				      ".solved"} =~ /^correct/ ) {
  227: 	foreach $name (@whichopt) {
  228: 	    $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
  229: 		":".$Apache::response::foilgroup{$name.'.text'}."\n";
  230: 	}
  231:     } else {
  232: 	my $temp=1;
  233: 	foreach $name (@whichopt) {
  234: 	    $result .= "<br /><select ".
  235: 		"name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">".
  236: 		$optionlist.
  237: 		"</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
  238: 	    $temp++;
  239: 	}
  240:     }
  241:     return $result."<br />";
  242: }
  243: 
  244: # ================================== Start concept group (return scalar string)
  245: sub start_conceptgroup {
  246:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  247:     $Apache::optionresponse::conceptgroup = 1;
  248:     %Apache::response::conceptgroup = {};
  249:     my $result;
  250:     if ($target eq 'edit') {
  251: 	$result .= &Apache::edit::tag_start($target,$token,
  252: 					"Concept Grouped Foils");
  253: 	$result .= &Apache::edit::text_arg('Concept:','concept',$token,'50').
  254: 	           "</td></tr>";
  255: 	$result .= "<tr><td colspan=\"3\">\n";
  256:     }
  257:     if ($target eq 'modified') {
  258:         my $constructtag = &Apache::edit::get_new_args($token,$parstack,
  259: 						     $safeeval,'concept');
  260: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  261:     }
  262:     return $result;
  263: }
  264: 
  265: # ==================================== End concept group (return scalar string)
  266: sub end_conceptgroup {
  267:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  268:     $Apache::optionresponse::conceptgroup = 0;
  269:     my $result = '';
  270:     if ($target eq 'web' || $target eq 'grade') {
  271: 	#if not there aren't any foils to display and thus no question
  272: 	if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
  273: 	    my @names = @{ $Apache::response::conceptgroup{'names'} };
  274: 	    my $pick = int rand $#names+1;
  275: 	    my $name = $names[$pick];
  276: 	    push @{ $Apache::response::foilgroup{'names'} }, $name;
  277: 	    $Apache::response::foilgroup{"$name.value"} =
  278: 		$Apache::response::conceptgroup{"$name.value"};
  279: 	    $Apache::response::foilgroup{"$name.text"} =
  280: 		$Apache::response::conceptgroup{"$name.text"};
  281: 	    my $concept = 
  282: 		&Apache::lonxml::get_param('concept',$parstack,$safeeval);
  283: 	    $Apache::response::foilgroup{"$name.concept"} = $concept;
  284: 	    &Apache::lonxml::debug("Selecting $name in $concept");
  285: 	}
  286:     }
  287:     if ($target eq 'edit') {
  288: 	$result = &Apache::edit::end_table();
  289:     }
  290:     return $result;
  291: }
  292: 
  293: # ======================================================== Insert concept group
  294: sub insert_conceptgroup {
  295:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil().
  296: 	       "\n\t\t</conceptgroup>\n";
  297:     return $result;
  298: }
  299: 
  300: # =========================================== Start foil (return scalar string)
  301: sub start_foil {
  302:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  303:     my $result = '';
  304:     if ($target eq 'web') { &Apache::lonxml::startredirection; }
  305:     if ($target eq 'edit') {
  306: 	$result = &Apache::edit::tag_start($target,$token,"Foil");
  307: 	my $level = '-2';
  308: 	if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
  309: 	my @opt;
  310: 	eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,
  311: 						 $safeeval,$level);
  312: 	$result .= &Apache::edit::text_arg('Name:','name',$token);
  313: 	$result .= &Apache::edit::select_or_text_arg('Correct Option:',
  314: 						     'value',
  315: 						     ['unused',(@opt)],$token,
  316: 						     '15');
  317: 	$result .= '</td></tr><tr><td colspan="3">';
  318:     }
  319:     if ($target eq 'modified') {
  320: 	my $constructtag = &Apache::edit::get_new_args($token,$parstack,
  321: 						       $safeeval,'value',
  322: 						       'name');
  323: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  324:     }
  325:     return $result;
  326: }
  327: 
  328: # ============================================= End foil (return scalar string)
  329: sub end_foil {
  330:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  331:     my $text = '';
  332:     my $result = '';
  333:     if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
  334:     if ($target eq 'web' || $target eq 'grade') {
  335: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  336: 	if ($value ne 'unused') {
  337: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  338: 	    &Apache::lonxml::debug("Got a name of :$name:");
  339: 	    if (!$name) { $name = $Apache::lonxml::curdepth; }
  340: 	    &Apache::lonxml::debug("Using a name of :$name:");
  341: 	    if ( $Apache::optionresponse::conceptgroup ) {
  342: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
  343: 		$Apache::response::conceptgroup{"$name.value"} = $value;
  344: 		$Apache::response::conceptgroup{"$name.text"} = $text;	
  345: 	    } else {
  346: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
  347: 		$Apache::response::foilgroup{"$name.value"} = $value;
  348: 		$Apache::response::foilgroup{"$name.text"} = $text;
  349: 	    }
  350: 	}
  351:     }
  352:     if ($target eq 'edit') {
  353: 	$result .= &Apache::edit::tag_end($target,$token,'');
  354:     }
  355:     return $result;
  356: }
  357: 
  358: # ========================================== Insert foil (return scalar string)
  359: sub insert_foil {
  360:     return '
  361: <foil name="" value="unused">
  362: <startouttext />
  363: <endouttext />
  364: </foil>';
  365: }
  366: 
  367: 1;
  368: 
  369: __END__
  370:  

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