Annotation of loncom/homework/chemresponse.pm, revision 1.60

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # chemical equation style response
                      3: #
1.60    ! albertel    4: # $Id: chemresponse.pm,v 1.59 2005/06/27 14:27:25 albertel Exp $
1.1       albertel    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;
1.55      albertel   33: use Apache::lonlocal;
1.1       albertel   34: 
                     35: BEGIN {
1.46      albertel   36:     &Apache::lonxml::register('Apache::chemresponse',('organicresponse','organicstructure','reactionresponse','chem'));
1.1       albertel   37: }
                     38: 
1.34      albertel   39: sub chem_standard_order {
                     40:     my ($reaction) = @_;
                     41:     my ($re,$pr) = split(/->|<=>/,$reaction);
                     42:     my @reactants = split(/\s\+/,$re);
                     43:     my @products =  split(/\s\+/,$pr);
                     44:     foreach my $substance (@reactants,@products) {
                     45: 	$substance =~ s/(\^\d*)\s+/$1_/g;         # protect superscript space
                     46: 	$substance =~ s/\s*//g;                   # strip whitespace
                     47: 	$substance =~ s/_/ /g;                    # restore superscript space
                     48:     }
                     49:     @reactants = sort @reactants;
                     50:     @products = sort @products;
                     51:     my $standard = '';
                     52:     foreach my $substance (@reactants) {
                     53: 	$standard .= $substance;
                     54: 	$standard .= ' + ';
                     55:     }
                     56:     $standard =~ s/ \+ $//;              # get rid of trailing plus sign
                     57:     $standard .= ' -> ';
                     58:     foreach my $substance (@products) {
                     59: 	$standard .= $substance;
                     60: 	$standard .= ' + ';
                     61:     }
                     62:     $standard =~ s/ \+ $//;              # get rid of trailing plus sign
                     63:     return $standard;
                     64: }
                     65: 
1.30      www        66: sub separate_jme_window {
1.55      albertel   67:     my ($smile_input,$jme_input,$molecule,$options,$shown_text)=@_;
1.2       albertel   68:     my $smilesection;
                     69:     if (defined($smile_input)) {
                     70: 	$smilesection=<<SMILESECTION;
1.21      albertel   71:         smiles = document.applets.JME.smiles();
1.2       albertel   72: 	opener.document.lonhomework.$smile_input.value = smiles;
                     73: SMILESECTION
                     74:     }
                     75:     my $jmesection;
                     76:     if (defined($jme_input)) {
                     77: 	$jmesection=<<JMESECTION;
                     78: 	jmeFile = document.applets.JME.jmeFile();
                     79: 	opener.document.lonhomework.$jme_input.value = jmeFile;
                     80: JMESECTION
                     81:     }
                     82: 
1.14      albertel   83:     if ($molecule) { $molecule='<param name="jme" value="'.$molecule.'" />'; }
1.57      albertel   84:     my $insert_answer;
1.59      albertel   85:     if ($shown_text eq '') { 
1.57      albertel   86: 	$insert_answer=
                     87: 	    '<input type="button" name="submit" value="Insert Answer" onclick="javascript:submitSmiles();" />';
                     88:     }
                     89: 
1.1       albertel   90:     my $body=<<CHEMPAGE;
                     91: <html>
                     92: <head>
                     93: <title>Molecule Editor</title>
1.47      albertel   94: <script type="text/javascript">
1.1       albertel   95: function submitSmiles() {
1.21      albertel   96:     jmeFile = document.applets.JME.jmeFile();
                     97:     if (jmeFile == "") {
1.1       albertel   98: 	alert("Nothing to submit");
                     99:     } else {
1.21      albertel  100:         $jmesection
1.2       albertel  101:         $smilesection
1.1       albertel  102: 	window.close();
                    103:     }
                    104: }
                    105: function openHelpWindow() {
1.2       albertel  106:     window.open("/adm/jme/jme_help.html","","scrollbars=yes,resizable=yes,width=500,height=600");
1.1       albertel  107: }
1.60    ! albertel  108: function substituent(r) {document.applets.JME.setSubstituent(r);}
1.1       albertel  109: </script>
                    110: </head>
                    111: <body bgcolor="#ffffff">
                    112: <center>
1.60    ! albertel  113: <form>
        !           114:   <table width="440"><tr>
        !           115:     <td></td>
        !           116:     <td align="right">
        !           117:       <select onchange="javascript:substituent(options[selectedIndex].text)">
        !           118:         <option>Select substituent</option>
        !           119:         <option>-C(=O)OH</option>
        !           120:         <option>-C(=O)OMe</option>
        !           121:         <option>-OC(=O)Me</option>
        !           122:         <option>-CMe3</option>
        !           123:         <option>-CF3</option>
        !           124:         <option>-CCl3</option>
        !           125:         <option>-NO2</option>
        !           126:         <option>-SO2-NH2</option>
        !           127:         <option>-NH-SO2-Me</option>
        !           128:         <option>-NMe2</option>
        !           129:         <option>-C#N</option>
        !           130:         <option>-C#C-Me</option>
        !           131:         <option>-C#CH</option>
        !           132:       </select>
        !           133:     </td></tr>
        !           134:   </table>
        !           135: <applet code="JME.class" name="JME" archive="/adm/jme/JME.jar" width="440" height="390" mayscript>
1.1       albertel  136: You have to enable Java and JavaScript on your machine.
1.12      albertel  137: $molecule
1.6       albertel  138: <param name="options" value="$options" />
1.1       albertel  139: </applet><br />
1.49      albertel  140: <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>
1.57      albertel  141: $insert_answer
1.1       albertel  142: <br />
1.57      albertel  143: <input type="button" value="  Close  " onclick = "javascript:window.close()" />
1.1       albertel  144: &nbsp;&nbsp;
1.57      albertel  145: <input type="button" value="  Help  " onclick = "javascript:openHelpWindow()" />
1.1       albertel  146: </form>
                    147: </center>
                    148: </body>
                    149: </html>
                    150: CHEMPAGE
1.32      albertel  151:     $body=&HTML::Entities::encode($body,'<>&"');
1.1       albertel  152:     $body=~s/\n/ /g;
1.53      albertel  153:     my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
1.41      www       154:     my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
1.55      albertel  155:     my $display=&mt('Draw Molecule');
                    156:     if (defined($shown_text)) { $display=&mt($shown_text); }
1.1       albertel  157:     my $result=<<CHEMINPUT;
1.55      albertel  158: <input type="button" value="$display" onclick="javascript:editor=window.open($nothing,'jmeedit','width=500,height=500,menubar=yes,scrollbars=no,resizable=yes');editor.$docopen;editor.document.write('$body');editor.document.close();editor.focus()" />
1.1       albertel  159: CHEMINPUT
                    160:     return $result;
                    161: }
                    162: 
1.56      albertel  163: sub jme_img {
                    164:     my ($jme,$smile,$width,$options)=@_;
                    165:     my $id=&Apache::loncommon::get_cgi_id();
                    166:     my $result='<img alt="'.$smile.'" src="/cgi-bin/convertjme.pl?'.$id.'"';
                    167:     if ($options =~ /border/) { $result.= ' border="1"'; }
                    168:     $result.=' />';
                    169:     &Apache::lonnet::appenv('cgi.'.$id.'.JME'   =>
                    170: 			    &Apache::lonnet::escape($jme),
                    171: 			    'cgi.'.$id.'.PNG'   => 1,
                    172: 			    'cgi.'.$id.'.WIDTH' => $width);
                    173:     return $result;
                    174: }
                    175: 
1.6       albertel  176: sub start_organicresponse {
1.1       albertel  177:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    178:     my $result;
                    179:     my $partid = $Apache::inputtags::part;
                    180:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    181:     if ($target eq 'meta') {
1.28      albertel  182: 	$result=&Apache::response::meta_package_write('organicresponse');
1.1       albertel  183:     } elsif ($target eq 'web') {
1.55      albertel  184: 	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
                    185: 						 $safeeval);
                    186: 	if (  &Apache::response::show_answer() && $jmeanswer ne '') {
1.44      albertel  187: 	    my $options=&Apache::lonxml::get_param('options',$parstack,
                    188: 						   $safeeval);
                    189: 	    my $width=&Apache::lonxml::get_param('width',$parstack,
                    190: 						   $safeeval);
1.56      albertel  191: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
                    192: 							 $safeeval);
                    193: 	    $result.=&jme_img($jmeanswer,$answers[0],$width,$options);
1.1       albertel  194: 	} else {
1.44      albertel  195: 	    my $molecule;
                    196: 	    if (defined($Apache::lonhomework::history{"resource.$partid.$id.molecule"})) {
                    197: 		$molecule=$Apache::lonhomework::history{"resource.$partid.$id.molecule"};
                    198: 	    } else {
                    199: 		$molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    200: 						     $safeeval);
                    201: 	    }
                    202: 	    my $options=&Apache::lonxml::get_param('options',$parstack,
                    203: 						   $safeeval);
1.55      albertel  204: 	    my $shown_text;
                    205: 	    if (&Apache::response::show_answer()) {
                    206: 		$shown_text="Show Your Last Answer";
                    207: 	    }
                    208: 	    $result=&separate_jme_window("HWVAL_$id","MOLECULE_$id",$molecule,
                    209: 					 $options,$shown_text);
1.44      albertel  210: 	    $result.= '<input type="hidden" name="MOLECULE_'.$id.'" value="" />';
1.1       albertel  211: 	}
1.2       albertel  212:     } elsif ($target eq 'edit') {
                    213: 	$result .=&Apache::edit::tag_start($target,$token);
1.12      albertel  214: 	my $options=&Apache::lonxml::get_param('options',$parstack,
                    215: 					       $safeeval);
                    216: 	if ($options !~ /multipart/) { $options.=',multipart'; }
1.7       albertel  217: 	$result .='<nobr>'.
                    218: 	    &Apache::edit::text_arg('Starting Molecule:','molecule',
                    219: 				    $token,40);
1.2       albertel  220: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    221: 						$safeeval);
1.30      www       222: 	$result .=&separate_jme_window(undef,
1.2       albertel  223: 		      &Apache::edit::html_element_name('molecule'),
1.12      albertel  224: 		      $molecule,$options);
1.7       albertel  225: 	$result .='</nobr><br /><nobr>';
1.2       albertel  226: 	$result .=&Apache::edit::text_arg('Correct Answer:','answer',
                    227: 					  $token,40);
                    228: 	$result .=&Apache::edit::hidden_arg('jmeanswer',$token);
                    229: 	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
                    230: 						 $safeeval);
1.30      www       231: 	$result .=&separate_jme_window(
1.2       albertel  232:                       &Apache::edit::html_element_name('answer'),
                    233:                       &Apache::edit::html_element_name('jmeanswer'),
1.12      albertel  234: 		      $jmeanswer,$options);
                    235: 	$result .='</nobr><br />';
                    236: 	$result .=&Apache::edit::checked_arg('Options:','options',
1.29      www       237: 				    [ ['autoez','Auto E,Z stereochemistry'],
1.18      albertel  238: 				      ['multipart','Multipart Structures'],
1.12      albertel  239: 				      ['nostereo','No stereochemistry'],
                    240: 				      ['reaction','Is a reaction'],
1.18      albertel  241: 				      ['number','Able to number atoms'] ],
1.12      albertel  242: 					     ,$token);
1.44      albertel  243: 	$result .=&Apache::edit::text_arg('Width of correct answer image:',
                    244: 					  'width',$token,10);
1.2       albertel  245: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    246:     } elsif ($target eq 'modified') {
                    247: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    248: 						     $safeeval,'molecule',
1.6       albertel  249: 						     'answer','jmeanswer',
1.44      albertel  250: 						     'options','width');
1.2       albertel  251: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.1       albertel  252:     }
                    253:     return $result;
                    254: }
                    255: 
1.6       albertel  256: sub end_organicresponse {
1.1       albertel  257:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    258:     my $result;
1.45      albertel  259:     if ($target eq 'grade' && &Apache::response::submitted()) {
1.31      albertel  260: 	&Apache::response::setup_params($$tagstack[-1],$safeeval);
1.1       albertel  261: 	my $response = &Apache::response::getresponse();
                    262: 	if ( $response =~ /[^\s]/) {
                    263: 	    my $partid = $Apache::inputtags::part;
                    264: 	    my $id = $Apache::inputtags::response['-1'];
1.13      albertel  265: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.1       albertel  266: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    267: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    268: 	    my $ad;
1.13      albertel  269: 	    foreach my $answer (@answers) {
                    270: 		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
                    271: 		if ($response eq $answer) {
                    272: 		    $ad='EXACT_ANS';
                    273: 		    last;
                    274: 		} else {
                    275: 		    $ad='INCORRECT';
                    276: 		}
1.1       albertel  277: 	    }
1.42      albertel  278: 	    if ($ad && $Apache::lonhomework::type eq 'survey') {
                    279: 		$ad='SUBMITTED';
                    280: 	    }
1.1       albertel  281: 	    &Apache::response::handle_previous(\%previous,$ad);
                    282: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1.50      albertel  283: 	    $Apache::lonhomework::results{"resource.$partid.$id.molecule"}=$env{"form.MOLECULE_$id"};
1.1       albertel  284: 	}
1.2       albertel  285:     } elsif ($target eq "edit") {
                    286: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.15      albertel  287:     } elsif ($target eq 'answer') {
                    288: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
                    289: 						     $safeeval);
                    290: 	$result.=&Apache::response::answer_header('organicresponse');
                    291: 	foreach my $answer (@answers) {
                    292: 	    $result.=&Apache::response::answer_part('organicresponse',$answer);
                    293: 	}
                    294: 	$result.=&Apache::response::answer_footer('organicresponse');
1.1       albertel  295:     }
                    296:     &Apache::response::end_response;
                    297:     return $result;
                    298: }
                    299: 
1.6       albertel  300: sub start_organicstructure {
1.3       albertel  301:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    302:     my $result;
                    303:     if ($target eq 'web') {
                    304: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
                    305: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
1.12      albertel  306: 	my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.23      albertel  307: 	my $id=&Apache::loncommon::get_cgi_id();
1.19      albertel  308: 	$result="<img src='/cgi-bin/convertjme.pl?$id'";
                    309: 	if ($options =~ /border/) { $result.= ' border="1"'; }
                    310: 	$result.=' />';
1.16      albertel  311: 	&Apache::lonnet::appenv(
                    312:             'cgi.'.$id.'.JME'   => &Apache::lonnet::escape($molecule),
1.17      albertel  313: 	    'cgi.'.$id.'.PNG' => 1,
1.16      albertel  314: 	    'cgi.'.$id.'.WIDTH' => $width );
1.17      albertel  315:     } elsif ($target eq 'tex') {
1.38      albertel  316: 	my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1);
1.58      foxr      317: 	my $webwidth=&Apache::lonxml::get_param('width', $parstack, $safeeval);
                    318: 	my $webheight=&Apache::lonxml::get_param('height', $parstack, $safeeval);
                    319: 	if ($webheight) { $webheight = $webwidth; }
1.17      albertel  320: 	if (!$texwidth) { $texwidth='90'; }
1.58      foxr      321: 	$result = "%DYNAMICIMAGE:$webwidth:$webheight:$texwidth\n";
1.17      albertel  322: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
                    323: 	my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.50      albertel  324: 	my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
1.17      albertel  325: 	    '_'.time.'_'.$$.int(rand(1000)).'_organicstructure';
                    326: 	my $id=$filename;
                    327: 	&Apache::lonnet::appenv(
                    328: 		     'cgi.'.$id.'.JME'   => &Apache::lonnet::escape($molecule),
                    329: 		     'cgi.'.$id.'.PS' => 1,
                    330: 		     'cgi.'.$id.'.WIDTH' => $texwidth );
                    331: 	$id=&Apache::lonnet::escape($id);
                    332: 	&Apache::lonxml::register_ssi("/cgi-bin/convertjme.pl?$id");
1.20      albertel  333: 	if ($options =~ /border/) { $result.= '\fbox{'; }
                    334: 	$result .= '\graphicspath{{/home/httpd/perl/tmp/}}\includegraphics[width='.$texwidth.' mm]{'.$filename.'.eps}';
                    335: 	if ($options =~ /border/) { $result.= '} '; }
1.3       albertel  336:     } elsif ($target eq 'edit') {
                    337: 	$result .=&Apache::edit::tag_start($target,$token);
1.22      albertel  338: 	$result .=&Apache::edit::text_arg('Width (pixels):','width',$token,5);
                    339: 	$result .=&Apache::edit::text_arg('TeXwidth (mm):','texwidth',$token,5);
1.12      albertel  340: 	$result .='<nobr>';
1.3       albertel  341: 	$result .=&Apache::edit::text_arg('Molecule:','molecule',$token,40);
                    342: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    343: 						$safeeval);
1.12      albertel  344: 	my $options=&Apache::lonxml::get_param('options',$parstack,
                    345: 					       $safeeval);
                    346: 	if ($options !~ /reaction/) {
                    347: 	    $options.= ',multipart,number';
                    348: 	}
                    349: 						   
1.30      www       350: 	$result .=&separate_jme_window(undef,
1.12      albertel  351: 				 &Apache::edit::html_element_name('molecule'),
                    352: 				       $molecule,$options);
                    353: 	$result.="</nobr><br />";
                    354: 	$result .=&Apache::edit::checked_arg('Options:','options',
1.18      albertel  355: 					     [ ['reaction','Is a reaction'],
1.12      albertel  356: 					       ['border','Draw a border'] ],
                    357: 					     $token);
1.24      albertel  358: 	$result .=&Apache::edit::end_row();
1.3       albertel  359:     } elsif ($target eq 'modified') {
                    360: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    361: 						     $safeeval,'molecule',
1.22      albertel  362: 						     'width','texwidth',
                    363: 						     'options');
1.3       albertel  364: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    365:     }
                    366:     return $result;
1.1       albertel  367: }
                    368: 
1.6       albertel  369: sub end_organicstructure {
1.3       albertel  370:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    371:     my $result;
                    372:     if ($target eq "edit") {
                    373: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    374:     }
1.4       albertel  375:     return $result;
                    376: }
                    377: 
1.9       albertel  378: sub edit_reaction_button {
1.11      albertel  379:     my ($id,$field,$reaction)=@_;
1.10      albertel  380:     my $id_es=&Apache::lonnet::escape($id);
                    381:     my $field_es=&Apache::lonnet::escape($field);
1.11      albertel  382:     my $reaction_es=&Apache::lonnet::escape($reaction);
1.41      www       383:     my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
1.9       albertel  384:     my $result=<<EDITREACTION;
1.10      albertel  385: <script type="text/javascript">
1.47      albertel  386: // <!--
1.10      albertel  387:     function create_reaction_window_${id}_${field} () {
                    388: 	editor=window.open('','','width=500,height=270,scrollbars=no,resizable=yes');
1.41      www       389: 	editor.$docopen;
1.37      albertel  390: 	editor.document.writeln('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><html> <head><title>LON-CAPA Reaction Editor</title></head><frameset rows="30%,*" border="0">  <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" /> </frameset> </html>');
1.10      albertel  391:     }
1.47      albertel  392: // -->
1.10      albertel  393: </script>
1.47      albertel  394: <input type='button' value='Edit Answer' onclick="javascript:create_reaction_window_${id}_${field}();void(0);" />
1.9       albertel  395: EDITREACTION
                    396:     return $result;
                    397: }
                    398: 
1.4       albertel  399: sub start_reactionresponse {
                    400:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    401:     my $result;
                    402:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.10      albertel  403:     if ($target eq 'meta') {
1.28      albertel  404: 	$result=&Apache::response::meta_package_write('reactionresponse');
1.10      albertel  405:     } elsif ($target eq 'web') {
1.11      albertel  406: 	my $partid = $Apache::inputtags::part;
                    407: 	my $id = $Apache::inputtags::response['-1'];
                    408: 	my $reaction=$Apache::lonhomework::history{"resource.$partid.$id.submission"};
1.35      albertel  409: 	if ($reaction eq '') {  $reaction=&Apache::lonxml::get_param('initial',$parstack,$safeeval); }
1.33      albertel  410: 	my $status=$Apache::inputtags::status['-1'];
                    411: 	if ($status eq 'CAN_ANSWER') {
                    412: 	    $result.=&edit_reaction_button($id,"HWVAL_$id",$reaction);
                    413: 	}
                    414: 	if (  &Apache::response::show_answer() ) {
                    415: 	    my $ans=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
1.51      albertel  416: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    417: 		&Apache::lonxml::default_homework_load($safeeval);
                    418: 	    }
                    419: 	    @Apache::scripttag::parser_env = @_;
                    420: 	    $Apache::inputtags::answertxt{$id}=&Apache::run::run("return &chemparse(q\0$ans\0);",$safeeval);
1.33      albertel  421: 	}
1.4       albertel  422:     } elsif ($target eq "edit") {
1.9       albertel  423: 	$result .=&Apache::edit::tag_start($target,$token);
                    424: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,
                    425: 						$safeeval);
1.10      albertel  426: 	$result .='<nobr>'.
                    427: 	    &Apache::edit::text_arg('Answer:','answer',$token,40);
                    428: 	$result .=&edit_reaction_button($id,&Apache::edit::html_element_name('answer'),$answer).'</nobr>';
1.35      albertel  429: 	my $initial=&Apache::lonxml::get_param('initial',$parstack,$safeeval);
                    430: 	$result.='<nobr>'.
1.43      albertel  431: 	    &Apache::edit::text_arg('Initial Reaction:','initial',$token,40);
1.35      albertel  432: 	$result .=&edit_reaction_button($id,&Apache::edit::html_element_name('initial'),$initial).'</nobr>';
1.10      albertel  433: 	
1.9       albertel  434: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.10      albertel  435:     }  elsif ($target eq 'modified') {
                    436: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.35      albertel  437: 						     $safeeval,'answer',
                    438: 						     'initial');
1.10      albertel  439: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.4       albertel  440:     }
                    441:     return $result;
                    442: }
                    443: 
                    444: sub end_reactionresponse {
                    445:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    446:     my $result;
1.45      albertel  447:     if ($target eq 'grade' && &Apache::response::submitted()) {
1.31      albertel  448: 	&Apache::response::setup_params($$tagstack[-1],$safeeval);
1.10      albertel  449: 	my $response = &Apache::response::getresponse();
                    450: 	if ( $response =~ /[^\s]/) {
                    451: 	    my $partid = $Apache::inputtags::part;
                    452: 	    my $id = $Apache::inputtags::response['-1'];
1.15      albertel  453: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.10      albertel  454: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    455: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    456: 	    my $ad;
1.13      albertel  457: 	    foreach my $answer (@answers) {
                    458: 		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
1.34      albertel  459: 		if (&chem_standard_order($response) eq 
                    460: 		    &chem_standard_order($answer)) {
1.13      albertel  461: 		    $ad='EXACT_ANS';
                    462: 		} else {
                    463: 		    $ad='INCORRECT';
                    464: 		}
1.10      albertel  465: 	    }
1.42      albertel  466: 	    if ($ad && $Apache::lonhomework::type eq 'survey') {
                    467: 		$ad='SUBMITTED';
                    468: 	    }
1.10      albertel  469: 	    &Apache::response::handle_previous(\%previous,$ad);
                    470: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
                    471: 	}
                    472:     }  elsif ($target eq "edit") {
1.4       albertel  473: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.15      albertel  474:     } elsif ($target eq 'answer') {
                    475: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
                    476: 						     $safeeval);
                    477: 	$result.=&Apache::response::answer_header('reactionresponse');
                    478: 	foreach my $answer (@answers) {
                    479: 	    $result.=&Apache::response::answer_part('reactionresponse',
                    480: 						    $answer);
                    481: 	}
                    482: 	$result.=&Apache::response::answer_footer('reactionresponse');
1.4       albertel  483:     }
                    484:     &Apache::response::end_response;
1.3       albertel  485:     return $result;
1.1       albertel  486: }
                    487: 
1.46      albertel  488: sub start_chem {
                    489:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    490:     my $result = '';
1.48      albertel  491:     my $inside = &Apache::lonxml::get_all_text_unbalanced("/chem",$parser);
1.46      albertel  492:     if ($target eq 'tex' || $target eq 'web') {
1.48      albertel  493: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
                    494: 	if (!$Apache::lonxml::default_homework_loaded) {
                    495: 	    &Apache::lonxml::default_homework_load($safeeval);
                    496: 	}
                    497: 	@Apache::scripttag::parser_env = @_;
                    498: 	$result=&Apache::run::run("return &chemparse(q\0$inside\0);",$safeeval);
1.46      albertel  499:     }    
                    500:     return $result;
                    501: }
                    502: 
                    503: sub end_chem {
                    504:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    505:     my $result = '';
                    506:     return $result;
                    507: }
                    508: 
1.1       albertel  509: 1;
                    510: __END__

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