File:  [LON-CAPA] / loncom / homework / chemresponse.pm
Revision 1.76: download - view: text, annotated - select for diffs
Fri Feb 1 22:05:45 2008 UTC (16 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
When the target is 'analyze', include the responsetype of the current item in the lonhomework::analyze hash, for use later on, e.g., for scantron grading.

    1: # The LearningOnline Network with CAPA
    2: # chemical equation style response
    3: #
    4: # $Id: chemresponse.pm,v 1.76 2008/02/01 22:05:45 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: package Apache::chemresponse;
   30: use strict;
   31: use Apache::lonxml;
   32: use Apache::lonnet;
   33: use Apache::lonlocal;
   34: use lib '/home/httpd/lib/perl/';
   35: use LONCAPA;
   36:  
   37: 
   38: BEGIN {
   39:     &Apache::lonxml::register('Apache::chemresponse',('organicresponse','organicstructure','reactionresponse','chem'));
   40: }
   41: 
   42: sub chem_standard_order {
   43:     my ($reaction) = @_;
   44:     my ($re,$pr) = split(/->|<=>/,$reaction);
   45:     my @reactants = split(/\s\+/,$re);
   46:     my @products =  split(/\s\+/,$pr);
   47:     foreach my $substance (@reactants,@products) {
   48: 	$substance =~ s/(\^\d*)\s+/$1_/g;         # protect superscript space
   49: 	$substance =~ s/\s*//g;                   # strip whitespace
   50: 	$substance =~ s/_/ /g;                    # restore superscript space
   51:     }
   52:     @reactants = sort @reactants;
   53:     @products = sort @products;
   54:     my $standard = '';
   55:     foreach my $substance (@reactants) {
   56: 	$standard .= $substance;
   57: 	$standard .= ' + ';
   58:     }
   59:     $standard =~ s/ \+ $//;              # get rid of trailing plus sign
   60:     $standard .= ' -> ';
   61:     foreach my $substance (@products) {
   62: 	$standard .= $substance;
   63: 	$standard .= ' + ';
   64:     }
   65:     $standard =~ s/ \+ $//;              # get rid of trailing plus sign
   66:     return $standard;
   67: }
   68: 
   69: sub separate_jme_window {
   70:     my ($smile_input,$jme_input,$molecule,$options,$shown_text)=@_;
   71:     my $smilesection;
   72:     if (defined($smile_input)) {
   73: 	$smilesection=<<SMILESECTION;
   74:         smiles = document.applets.JME.smiles();
   75: 	opener.document.lonhomework.$smile_input.value = smiles;
   76: SMILESECTION
   77:     }
   78:     my $jmesection;
   79:     if (defined($jme_input)) {
   80: 	$jmesection=<<JMESECTION;
   81: 	jmeFile = document.applets.JME.jmeFile();
   82: 	opener.document.lonhomework.$jme_input.value = jmeFile;
   83: JMESECTION
   84:     }
   85: 
   86:     if ($molecule) { $molecule='<param name="jme" value="'.$molecule.'" />'; }
   87:     my $insert_answer;
   88:     if ($shown_text eq '') { 
   89: 	$insert_answer=
   90: 	    '<input type="button" name="submit" value="Insert Answer" onclick="javascript:submitSmiles();" />';
   91:     }
   92: 
   93:     my $js = <<CHEMJS;
   94: <script type="text/javascript">
   95: function submitSmiles() {
   96:     jmeFile = document.applets.JME.jmeFile();
   97:     if (jmeFile == "") {
   98: 	alert("Nothing to submit");
   99:     } else {
  100:         $jmesection
  101:         $smilesection
  102: 	window.close();
  103:     }
  104: }
  105: function openHelpWindow() {
  106:     window.open("/adm/jme/jme_help.html","","scrollbars=yes,resizable=yes,width=500,height=600");
  107: }
  108: function substituent(r) {document.applets.JME.setSubstituent(r);}
  109: </script>
  110: CHEMJS
  111: 
  112:     my $start_page = 
  113:         &Apache::loncommon::start_page('Molecule Editor',undef,
  114: 				       {'only_body' => 1,
  115: 					'js_ready'  => 1,
  116: 					'bgcolor'   => '#FFFFFF',});
  117:     my $end_page =
  118:  	&Apache::loncommon::end_page({'js_ready' => 1,});
  119: 	
  120:     my $body=<<CHEMPAGE;
  121: $js
  122: <center>
  123: <form>
  124:   <table width="440"><tr>
  125:     <td></td>
  126:     <td align="right">
  127:       <select onchange="javascript:substituent(options[selectedIndex].text)">
  128:         <option>Select substituent</option>
  129:         <option>-C(=O)OH</option>
  130:         <option>-C(=O)OMe</option>
  131:         <option>-OC(=O)Me</option>
  132:         <option>-CMe3</option>
  133:         <option>-CF3</option>
  134:         <option>-CCl3</option>
  135:         <option>-NO2</option>
  136:         <option>-SO2-NH2</option>
  137:         <option>-NH-SO2-Me</option>
  138:         <option>-NMe2</option>
  139:         <option>-C#N</option>
  140:         <option>-C#C-Me</option>
  141:         <option>-C#CH</option>
  142:       </select>
  143:     </td></tr>
  144:   </table>
  145: <applet code="JME.class" name="JME" archive="/adm/jme/JME.jar" width="440" height="390" mayscript>
  146: You have to enable Java and JavaScript on your machine.
  147: $molecule
  148: <param name="options" value="$options" />
  149: </applet><br />
  150: <font face="arial,helvetica,sans-serif" size="-1"><a href="http://www.molinspiration.com/jme/index.html">JME Editor</a> courtesy of Peter Ertl, Novartis</font>
  151: <br />
  152: $insert_answer
  153: <br />
  154: <input type="button" value="  Close  " onclick = "javascript:window.close()" />
  155: &nbsp;&nbsp;
  156: <input type="button" value="  Help  " onclick = "javascript:openHelpWindow()" />
  157: </form>
  158: </center>
  159: CHEMPAGE
  160: 
  161:     $body=&Apache::loncommon::js_ready($body);
  162:     my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
  163:     my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
  164:     my $display=&mt('Draw Molecule');
  165:     if (defined($shown_text)) { $display=&mt($shown_text); }
  166:     my $function = 
  167: 	'LONCAPA_draw_molecule_'.&Apache::lonhtmlcommon::get_uniq_name();
  168:     my $result=<<CHEMINPUT;
  169: <script type="text/javascript">
  170:     function $function() {
  171: 	editor=window.open($nothing,'jmeedit','width=500,height=500,menubar=no,scrollbars=no,resizable=yes');
  172: 	editor.$docopen;
  173: 	editor.document.write('$start_page $body $end_page');
  174: 	editor.document.close();
  175: 	editor.focus();
  176:     }
  177: </script>
  178: <input type="button" value="$display" onclick="javascript:$function();void(0);" />
  179: CHEMINPUT
  180:     return $result;
  181: }
  182: 
  183: sub jme_img {
  184:     my ($jme,$smile,$width,$options)=@_;
  185:     my $id=&Apache::loncommon::get_cgi_id();
  186:     my $result='<img alt="'.$smile.'" src="/cgi-bin/convertjme.pl?'.$id.'"';
  187:     if ($options =~ /border/) { $result.= ' border="1"'; }
  188:     $result.=' />';
  189:     &Apache::lonnet::appenv('cgi.'.$id.'.JME'   =>
  190: 			    &escape($jme),
  191: 			    'cgi.'.$id.'.PNG'   => 1,
  192: 			    'cgi.'.$id.'.WIDTH' => $width);
  193:     return $result;
  194: }
  195: 
  196: sub start_organicresponse {
  197:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  198:     my $result;
  199:     my $partid = $Apache::inputtags::part;
  200:     my $id = &Apache::response::start_response($parstack,$safeeval);
  201:     if ($target eq 'meta') {
  202: 	$result=&Apache::response::meta_package_write('organicresponse');
  203:     } elsif ($target eq 'web') {
  204: 	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
  205: 						 $safeeval);
  206: 	if (  &Apache::response::show_answer() && $jmeanswer ne '') {
  207: 	    my $options=&Apache::lonxml::get_param('options',$parstack,
  208: 						   $safeeval);
  209: 	    my $width=&Apache::lonxml::get_param('width',$parstack,
  210: 						   $safeeval);
  211: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
  212: 							 $safeeval);
  213: 	    $result.=&jme_img($jmeanswer,$answers[0],$width,$options);
  214: 	} else {
  215: 	    my $molecule;
  216: 	    if (defined($Apache::lonhomework::history{"resource.$partid.$id.molecule"})) {
  217: 		$molecule=$Apache::lonhomework::history{"resource.$partid.$id.molecule"};
  218: 	    } else {
  219: 		$molecule=&Apache::lonxml::get_param('molecule',$parstack,
  220: 						     $safeeval);
  221: 	    }
  222: 	    my $options=&Apache::lonxml::get_param('options',$parstack,
  223: 						   $safeeval);
  224: 	    my $shown_text;
  225: 	    if (&Apache::response::show_answer()) {
  226: 		$shown_text="Show Your Last Answer";
  227: 	    }
  228: 	    $result=&separate_jme_window("HWVAL_$id","MOLECULE_$id",$molecule,
  229: 					 $options,$shown_text);
  230: 	    $result.= '<input type="hidden" name="MOLECULE_'.$id.'" value="" />';
  231: 	}
  232:     } elsif ($target eq 'edit') {
  233: 	$result .=&Apache::edit::tag_start($target,$token);
  234: 	my $options=&Apache::lonxml::get_param('options',$parstack,
  235: 					       $safeeval);
  236: 	if ($options !~ /multipart/) { $options.=',multipart'; }
  237: 	$result .='<nobr>'.
  238: 	    &Apache::edit::text_arg('Starting Molecule:','molecule',
  239: 				    $token,40);
  240: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
  241: 						$safeeval);
  242: 	$result .=&separate_jme_window(undef,
  243: 		      &Apache::edit::html_element_name('molecule'),
  244: 		      $molecule,$options);
  245: 	$result .='</nobr><br /><nobr>';
  246: 	$result .=&Apache::edit::text_arg('Correct Answer:','answer',
  247: 					  $token,40);
  248: 	$result .='</nobr><br /><nobr>';
  249: 	$result .=&Apache::edit::text_arg('JME string of the answer (automatically updated when using the Draw Molecule button):',
  250: 					  'jmeanswer',$token);
  251: 	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
  252: 						 $safeeval);
  253: 	$result .=&separate_jme_window(
  254:                       &Apache::edit::html_element_name('answer'),
  255:                       &Apache::edit::html_element_name('jmeanswer'),
  256: 		      $jmeanswer,$options);
  257: 	$result .='</nobr><br />';
  258: 	$result .=&Apache::edit::checked_arg('Options:','options',
  259: 				    [ ['autoez','Auto E,Z stereochemistry'],
  260: 				      ['multipart','Multipart Structures'],
  261: 				      ['nostereo','No stereochemistry'],
  262: 				      ['reaction','Is a reaction'],
  263: 				      ['number','Able to number atoms'] ],
  264: 					     ,$token);
  265: 	$result .=&Apache::edit::text_arg('Width of correct answer image:',
  266: 					  'width',$token,10);
  267: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  268:     } elsif ($target eq 'modified') {
  269: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  270: 						     $safeeval,'molecule',
  271: 						     'answer','jmeanswer',
  272: 						     'options','width');
  273: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  274:     }
  275: 
  276:     return $result;
  277: }
  278: 
  279: sub end_organicresponse {
  280:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  281:     my $result;
  282: 
  283:     my $partid = $Apache::inputtags::part;
  284:     my $id = $Apache::inputtags::response['-1'];
  285: 
  286:     if ($target eq 'grade' 
  287: 	&& &Apache::response::submitted()
  288: 	&& $Apache::lonhomework::type eq 'exam') {
  289: 
  290: 	&Apache::response::scored_response($partid,$id);
  291: 
  292:     } elsif ($target eq 'grade' 
  293: 	&& &Apache::response::submitted()
  294: 	&& $Apache::lonhomework::type ne 'exam') {
  295: 
  296: 	&Apache::response::setup_params($$tagstack[-1],$safeeval);
  297: 	my $response = &Apache::response::getresponse();
  298: 	if ( $response =~ /[^\s]/) {
  299: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
  300: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
  301: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
  302: 	    my $ad;
  303: 	    foreach my $answer (@answers) {
  304: 		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
  305: 		if ($response eq $answer) {
  306: 		    $ad='EXACT_ANS';
  307: 		    last;
  308: 		} else {
  309: 		    $ad='INCORRECT';
  310: 		}
  311: 	    }
  312: 	    if ($ad && $Apache::lonhomework::type eq 'survey') {
  313: 		$ad='SUBMITTED';
  314: 	    }
  315: 	    &Apache::response::handle_previous(\%previous,$ad);
  316: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
  317: 	    $Apache::lonhomework::results{"resource.$partid.$id.molecule"}=$env{"form.MOLECULE_$id"};
  318: 	}
  319:     } elsif ($target eq "edit") {
  320: 	$result.= &Apache::edit::tag_end($target,$token,'');
  321:     } elsif ($target eq 'answer') {
  322: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
  323: 						     $safeeval);
  324: 	$result.=&Apache::response::answer_header('organicresponse');
  325: 	foreach my $answer (@answers) {
  326: 	    $result.=&Apache::response::answer_part('organicresponse',$answer);
  327: 	}
  328: 	$result.=&Apache::response::answer_footer('organicresponse');
  329:     }
  330:     if ($target eq 'web') {
  331: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer_organic,
  332: 						  ['molecule'])
  333:     }
  334: 
  335:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
  336: 	$target eq 'tex' || $target eq 'analyze') {
  337: 	&Apache::lonxml::increment_counter(&Apache::response::repetition(), 
  338: 					   "$partid.$id"); # part.response
  339: 	if ($target eq 'analyze') {
  340:             $Apache::lonhomework::analyze{"$partid.$id.type"} = 'organicresponse';
  341: 	    &Apache::lonhomework::set_bubble_lines();
  342: 	}
  343:     }
  344:     &Apache::response::end_response();
  345:     return $result;
  346: }
  347: 
  348: sub format_prior_answer_organic {
  349:     my ($mode,$answer,$other_data) = @_;
  350:     my $result=&mt('Smile representation: "[_1]"','<tt>'.$answer.'</tt>');
  351:     my $jme=$other_data->[0];
  352:     $result.=&jme_img($jme,$answer,400);
  353:     return $result;
  354: }
  355: 
  356: sub start_organicstructure {
  357:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  358:     my $result;
  359:     if ($target eq 'web') {
  360: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
  361: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
  362: 	my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
  363: 	my $id=&Apache::loncommon::get_cgi_id();
  364: 	$result="<img src='/cgi-bin/convertjme.pl?$id'";
  365: 	if ($options =~ /border/) { $result.= ' border="1"'; }
  366: 	$result.=' />';
  367: 	&Apache::lonnet::appenv(
  368:             'cgi.'.$id.'.JME'   => &escape($molecule),
  369: 	    'cgi.'.$id.'.PNG' => 1,
  370: 	    'cgi.'.$id.'.WIDTH' => $width );
  371:     } elsif ($target eq 'tex') {
  372: 	my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1);
  373: 	my $webwidth=&Apache::lonxml::get_param('width', $parstack, $safeeval);
  374: 	my $webheight=&Apache::lonxml::get_param('height', $parstack, $safeeval);
  375: 	if (!$webheight) { $webheight = $webwidth; }
  376: 	if (!$texwidth) { $texwidth='90'; }
  377: 	$result = "%DYNAMICIMAGE:$webwidth:$webheight:$texwidth\n";
  378: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
  379: 	my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
  380: 	my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
  381: 	    '_'.time.'_'.$$.int(rand(1000)).'_organicstructure';
  382: 	my $id=$filename;
  383: 	&Apache::lonnet::appenv(
  384: 		     'cgi.'.$id.'.JME'   => &escape($molecule),
  385: 		     'cgi.'.$id.'.PS' => 1,
  386: 		     'cgi.'.$id.'.WIDTH' => $texwidth );
  387: 	$id=&escape($id);
  388: 	&Apache::lonxml::register_ssi("/cgi-bin/convertjme.pl?$id");
  389: 	if ($options =~ /border/) { $result.= '\fbox{'; }
  390: 	$result .= '\graphicspath{{/home/httpd/perl/tmp/}}\includegraphics[width='.$texwidth.' mm]{'.$filename.'.eps}';
  391: 	if ($options =~ /border/) { $result.= '} '; }
  392:     } elsif ($target eq 'edit') {
  393: 	$result .=&Apache::edit::tag_start($target,$token);
  394: 	$result .=&Apache::edit::text_arg('Width (pixels):','width',$token,5);
  395: 	$result .=&Apache::edit::text_arg('TeXwidth (mm):','texwidth',$token,5);
  396: 	$result .='<nobr>';
  397: 	$result .=&Apache::edit::text_arg('Molecule:','molecule',$token,40);
  398: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
  399: 						$safeeval);
  400: 	my $options=&Apache::lonxml::get_param('options',$parstack,
  401: 					       $safeeval);
  402: 	if ($options !~ /reaction/) {
  403: 	    $options.= ',multipart,number';
  404: 	}
  405: 						   
  406: 	$result .=&separate_jme_window(undef,
  407: 				 &Apache::edit::html_element_name('molecule'),
  408: 				       $molecule,$options);
  409: 	$result.="</nobr><br />";
  410: 	$result .=&Apache::edit::checked_arg('Options:','options',
  411: 					     [ ['reaction','Is a reaction'],
  412: 					       ['border','Draw a border'] ],
  413: 					     $token);
  414: 	$result .=&Apache::edit::end_row();
  415:     } elsif ($target eq 'modified') {
  416: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  417: 						     $safeeval,'molecule',
  418: 						     'width','texwidth',
  419: 						     'options');
  420: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  421:     }
  422:     return $result;
  423: }
  424: 
  425: sub end_organicstructure {
  426:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  427:     my $result;
  428:     if ($target eq "edit") {
  429: 	$result.= &Apache::edit::tag_end($target,$token,'');
  430:     }
  431:     return $result;
  432: }
  433: 
  434: sub edit_reaction_button {
  435:     my ($id,$field,$reaction)=@_;
  436:     my $id_es=&escape($id);
  437:     my $field_es=&escape($field);
  438:     my $reaction_es=&escape($reaction);
  439:     my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
  440:     my $start_page = 
  441: 	&Apache::loncommon::start_page('LON-CAPA Reaction Editor',undef,
  442: 				       {'frameset'    => 1,
  443: 					'js_ready'    => 1,
  444: 					'add_entries' => {
  445: 					    'rows'   => "30%,*",
  446: 					    'border' => "0",}},);
  447:     my $end_page = 
  448: 	&Apache::loncommon::end_page({'frameset' => 1,
  449: 				      'js_ready' => 1});
  450:     my $result=<<EDITREACTION;
  451: <script type="text/javascript">
  452: // <!--
  453:     function create_reaction_window_${id}_${field} () {
  454: 	editor=window.open('','','width=500,height=270,scrollbars=no,resizable=yes');
  455: 	editor.$docopen;
  456: 	editor.document.writeln('$start_page <frame src="/res/adm/pages/reactionresponse/reaction_viewer.html?inhibitmenu=yes" name="viewer" scrolling="no" />  <frame src="/res/adm/pages/reactionresponse/reaction_editor.html?inhibitmenu=yes&reaction=$reaction_es&id=$id_es&field=$field_es" name="editor" scrolling="no" /> $end_page');
  457: 	editor.document.close();
  458:     }
  459: // -->
  460: </script>
  461: <input type='button' value='Edit Answer' onclick="javascript:create_reaction_window_${id}_${field}();void(0);" />
  462: EDITREACTION
  463:     return $result;
  464: }
  465: 
  466: sub start_reactionresponse {
  467:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  468:     my $result;
  469:     my $id = &Apache::response::start_response($parstack,$safeeval);
  470:     if ($target eq 'meta') {
  471: 	$result=&Apache::response::meta_package_write('reactionresponse');
  472:     } elsif ($target eq 'web') {
  473: 	my $partid = $Apache::inputtags::part;
  474: 	my $id = $Apache::inputtags::response['-1'];
  475: 	my $reaction=$Apache::lonhomework::history{"resource.$partid.$id.submission"};
  476: 	if ($reaction eq '') {  $reaction=&Apache::lonxml::get_param('initial',$parstack,$safeeval); }
  477: 	my $status=$Apache::inputtags::status['-1'];
  478: 	if ($status eq 'CAN_ANSWER') {
  479: 	    $result.=&edit_reaction_button($id,"HWVAL_$id",$reaction);
  480: 	}
  481: 	if (  &Apache::response::show_answer() ) {
  482: 	    my $ans=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  483: 	    if (!$Apache::lonxml::default_homework_loaded) {
  484: 		&Apache::lonxml::default_homework_load($safeeval);
  485: 	    }
  486: 	    @Apache::scripttag::parser_env = @_;
  487: 	    $Apache::inputtags::answertxt{$id}=[&Apache::run::run("return &chemparse(q\0$ans\0);",$safeeval)];
  488: 	}
  489:     } elsif ($target eq "edit") {
  490: 	$result .=&Apache::edit::tag_start($target,$token);
  491: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,
  492: 						$safeeval);
  493: 	$result .='<nobr>'.
  494: 	    &Apache::edit::text_arg('Answer:','answer',$token,40);
  495: 	$result .=&edit_reaction_button($id,&Apache::edit::html_element_name('answer'),$answer).'</nobr>';
  496: 	my $initial=&Apache::lonxml::get_param('initial',$parstack,$safeeval);
  497: 	$result.='<nobr>'.
  498: 	    &Apache::edit::text_arg('Initial Reaction:','initial',$token,40);
  499: 	$result .=&edit_reaction_button($id,&Apache::edit::html_element_name('initial'),$initial).'</nobr>';
  500: 	
  501: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  502:     }  elsif ($target eq 'modified') {
  503: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  504: 						     $safeeval,'answer',
  505: 						     'initial');
  506: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  507:     }
  508:     return $result;
  509: }
  510: 
  511: sub end_reactionresponse {
  512:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  513:     my $result;
  514: 
  515:     my $partid = $Apache::inputtags::part;
  516:     my $id = $Apache::inputtags::response['-1'];
  517: 
  518:     if ($target eq 'grade' 
  519: 	&& &Apache::response::submitted()
  520: 	&& $Apache::lonhomework::type eq 'exam') {
  521: 
  522: 	&Apache::response::scored_response($partid,$id);
  523: 
  524:     } elsif ($target eq 'grade' 
  525: 	&& &Apache::response::submitted()
  526: 	&& $Apache::lonhomework::type ne 'exam') {
  527: 
  528: 	&Apache::response::setup_params($$tagstack[-1],$safeeval);
  529: 	my $response = &Apache::response::getresponse();
  530: 	if ( $response =~ /[^\s]/) {
  531: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
  532: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
  533: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
  534: 	    my $ad;
  535: 	    foreach my $answer (@answers) {
  536: 		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
  537: 		if (&chem_standard_order($response) eq 
  538: 		    &chem_standard_order($answer)) {
  539: 		    $ad='EXACT_ANS';
  540: 		} else {
  541: 		    $ad='INCORRECT';
  542: 		}
  543: 	    }
  544: 	    if ($ad && $Apache::lonhomework::type eq 'survey') {
  545: 		$ad='SUBMITTED';
  546: 	    }
  547: 	    &Apache::response::handle_previous(\%previous,$ad);
  548: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
  549: 	}
  550:     }  elsif ($target eq "edit") {
  551: 	$result.= &Apache::edit::tag_end($target,$token,'');
  552:     } elsif ($target eq 'answer') {
  553: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
  554: 						     $safeeval);
  555: 	$result.=&Apache::response::answer_header('reactionresponse');
  556: 	foreach my $answer (@answers) {
  557: 	    $result.=&Apache::response::answer_part('reactionresponse',
  558: 						    $answer);
  559: 	}
  560: 	$result.=&Apache::response::answer_footer('reactionresponse');
  561:     }
  562:     if ($target eq 'web') {
  563: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response_reaction);
  564:     }
  565: 
  566:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
  567: 	$target eq 'tex' || $target eq 'analyze') {
  568: 	&Apache::lonxml::increment_counter(&Apache::response::repetition(), "$partid.$id");
  569:     }
  570:     &Apache::response::end_response();
  571:     return $result;
  572: }
  573: 
  574: sub format_prior_response_reaction {
  575:     my ($mode,$answer) =@_;
  576:     return '<span class="LC_prior_reaction">'.
  577: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
  578: }
  579: 
  580: sub start_chem {
  581:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  582:     my $result = '';
  583:     my $inside = &Apache::lonxml::get_all_text_unbalanced("/chem",$parser);
  584:     if ($target eq 'tex' || $target eq 'web') {
  585: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
  586: 	if (!$Apache::lonxml::default_homework_loaded) {
  587: 	    &Apache::lonxml::default_homework_load($safeeval);
  588: 	}
  589: 	@Apache::scripttag::parser_env = @_;
  590: 	$result=&Apache::run::run("return &chemparse(q\0$inside\0);",$safeeval);
  591:     }    
  592:     return $result;
  593: }
  594: 
  595: sub end_chem {
  596:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  597:     my $result = '';
  598:     return $result;
  599: }
  600: 
  601: 1;
  602: __END__

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