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

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

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