File:  [LON-CAPA] / loncom / homework / chemresponse.pm
Revision 1.66: download - view: text, annotated - select for diffs
Tue May 30 12:45:36 2006 UTC (17 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
&Apache::lonnet::unescape -> &unescape
&Apache::lonnet::escape -> &escape

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

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