File:  [LON-CAPA] / loncom / homework / chemresponse.pm
Revision 1.65: download - view: text, annotated - select for diffs
Thu Apr 13 18:57:51 2006 UTC (18 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- start_page

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

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