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

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

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