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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # chemical equation style response
                      3: #
1.68    ! albertel    4: # $Id: chemresponse.pm,v 1.67 2006/06/27 14:15:26 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.66      www        34: use lib '/home/httpd/lib/perl/';
                     35: use LONCAPA;
                     36:  
1.1       albertel   37: 
                     38: BEGIN {
1.46      albertel   39:     &Apache::lonxml::register('Apache::chemresponse',('organicresponse','organicstructure','reactionresponse','chem'));
1.1       albertel   40: }
                     41: 
1.34      albertel   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: 
1.30      www        69: sub separate_jme_window {
1.55      albertel   70:     my ($smile_input,$jme_input,$molecule,$options,$shown_text)=@_;
1.2       albertel   71:     my $smilesection;
                     72:     if (defined($smile_input)) {
                     73: 	$smilesection=<<SMILESECTION;
1.21      albertel   74:         smiles = document.applets.JME.smiles();
1.2       albertel   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: 
1.14      albertel   86:     if ($molecule) { $molecule='<param name="jme" value="'.$molecule.'" />'; }
1.57      albertel   87:     my $insert_answer;
1.59      albertel   88:     if ($shown_text eq '') { 
1.57      albertel   89: 	$insert_answer=
                     90: 	    '<input type="button" name="submit" value="Insert Answer" onclick="javascript:submitSmiles();" />';
                     91:     }
                     92: 
1.65      albertel   93:     my $js = <<CHEMJS;
1.47      albertel   94: <script type="text/javascript">
1.1       albertel   95: function submitSmiles() {
1.21      albertel   96:     jmeFile = document.applets.JME.jmeFile();
                     97:     if (jmeFile == "") {
1.1       albertel   98: 	alert("Nothing to submit");
                     99:     } else {
1.21      albertel  100:         $jmesection
1.2       albertel  101:         $smilesection
1.1       albertel  102: 	window.close();
                    103:     }
                    104: }
                    105: function openHelpWindow() {
1.2       albertel  106:     window.open("/adm/jme/jme_help.html","","scrollbars=yes,resizable=yes,width=500,height=600");
1.1       albertel  107: }
1.60      albertel  108: function substituent(r) {document.applets.JME.setSubstituent(r);}
1.1       albertel  109: </script>
1.65      albertel  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
1.1       albertel  121: <center>
1.60      albertel  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>
1.1       albertel  145: You have to enable Java and JavaScript on your machine.
1.12      albertel  146: $molecule
1.6       albertel  147: <param name="options" value="$options" />
1.1       albertel  148: </applet><br />
1.49      albertel  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>
1.61      albertel  150: <br />
1.57      albertel  151: $insert_answer
1.1       albertel  152: <br />
1.57      albertel  153: <input type="button" value="  Close  " onclick = "javascript:window.close()" />
1.1       albertel  154: &nbsp;&nbsp;
1.57      albertel  155: <input type="button" value="  Help  " onclick = "javascript:openHelpWindow()" />
1.1       albertel  156: </form>
                    157: </center>
1.65      albertel  158: $end_page
1.1       albertel  159: CHEMPAGE
1.65      albertel  160: 
1.32      albertel  161:     $body=&HTML::Entities::encode($body,'<>&"');
1.1       albertel  162:     $body=~s/\n/ /g;
1.53      albertel  163:     my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
1.41      www       164:     my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
1.55      albertel  165:     my $display=&mt('Draw Molecule');
                    166:     if (defined($shown_text)) { $display=&mt($shown_text); }
1.1       albertel  167:     my $result=<<CHEMINPUT;
1.61      albertel  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()" />
1.1       albertel  169: CHEMINPUT
                    170:     return $result;
                    171: }
                    172: 
1.56      albertel  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'   =>
1.66      www       180: 			    &escape($jme),
1.56      albertel  181: 			    'cgi.'.$id.'.PNG'   => 1,
                    182: 			    'cgi.'.$id.'.WIDTH' => $width);
                    183:     return $result;
                    184: }
                    185: 
1.6       albertel  186: sub start_organicresponse {
1.1       albertel  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') {
1.28      albertel  192: 	$result=&Apache::response::meta_package_write('organicresponse');
1.1       albertel  193:     } elsif ($target eq 'web') {
1.55      albertel  194: 	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
                    195: 						 $safeeval);
                    196: 	if (  &Apache::response::show_answer() && $jmeanswer ne '') {
1.44      albertel  197: 	    my $options=&Apache::lonxml::get_param('options',$parstack,
                    198: 						   $safeeval);
                    199: 	    my $width=&Apache::lonxml::get_param('width',$parstack,
                    200: 						   $safeeval);
1.56      albertel  201: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
                    202: 							 $safeeval);
                    203: 	    $result.=&jme_img($jmeanswer,$answers[0],$width,$options);
1.1       albertel  204: 	} else {
1.44      albertel  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);
1.55      albertel  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);
1.44      albertel  220: 	    $result.= '<input type="hidden" name="MOLECULE_'.$id.'" value="" />';
1.1       albertel  221: 	}
1.2       albertel  222:     } elsif ($target eq 'edit') {
                    223: 	$result .=&Apache::edit::tag_start($target,$token);
1.12      albertel  224: 	my $options=&Apache::lonxml::get_param('options',$parstack,
                    225: 					       $safeeval);
                    226: 	if ($options !~ /multipart/) { $options.=',multipart'; }
1.7       albertel  227: 	$result .='<nobr>'.
                    228: 	    &Apache::edit::text_arg('Starting Molecule:','molecule',
                    229: 				    $token,40);
1.2       albertel  230: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    231: 						$safeeval);
1.30      www       232: 	$result .=&separate_jme_window(undef,
1.2       albertel  233: 		      &Apache::edit::html_element_name('molecule'),
1.12      albertel  234: 		      $molecule,$options);
1.7       albertel  235: 	$result .='</nobr><br /><nobr>';
1.2       albertel  236: 	$result .=&Apache::edit::text_arg('Correct Answer:','answer',
                    237: 					  $token,40);
1.67      albertel  238: 	$result .=&Apache::edit::text_arg('JME string of the answer (automatically updated when using the Draw Molecule button):',
                    239: 					  'jmeanswer',$token);
1.2       albertel  240: 	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
                    241: 						 $safeeval);
1.30      www       242: 	$result .=&separate_jme_window(
1.2       albertel  243:                       &Apache::edit::html_element_name('answer'),
                    244:                       &Apache::edit::html_element_name('jmeanswer'),
1.12      albertel  245: 		      $jmeanswer,$options);
                    246: 	$result .='</nobr><br />';
                    247: 	$result .=&Apache::edit::checked_arg('Options:','options',
1.29      www       248: 				    [ ['autoez','Auto E,Z stereochemistry'],
1.18      albertel  249: 				      ['multipart','Multipart Structures'],
1.12      albertel  250: 				      ['nostereo','No stereochemistry'],
                    251: 				      ['reaction','Is a reaction'],
1.18      albertel  252: 				      ['number','Able to number atoms'] ],
1.12      albertel  253: 					     ,$token);
1.44      albertel  254: 	$result .=&Apache::edit::text_arg('Width of correct answer image:',
                    255: 					  'width',$token,10);
1.2       albertel  256: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    257:     } elsif ($target eq 'modified') {
                    258: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    259: 						     $safeeval,'molecule',
1.6       albertel  260: 						     'answer','jmeanswer',
1.44      albertel  261: 						     'options','width');
1.2       albertel  262: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.1       albertel  263:     }
1.63      albertel  264: 
1.1       albertel  265:     return $result;
                    266: }
                    267: 
1.6       albertel  268: sub end_organicresponse {
1.1       albertel  269:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    270:     my $result;
1.64      albertel  271: 
                    272:     my $partid = $Apache::inputtags::part;
                    273:     my $id = $Apache::inputtags::response['-1'];
                    274: 
                    275:     if ($target eq 'grade' 
                    276: 	&& &Apache::response::submitted()
                    277: 	&& $Apache::lonhomework::type eq 'exam') {
                    278: 
                    279: 	&Apache::response::scored_response($partid,$id);
                    280: 
                    281:     } elsif ($target eq 'grade' 
                    282: 	&& &Apache::response::submitted()
                    283: 	&& $Apache::lonhomework::type ne 'exam') {
                    284: 
1.31      albertel  285: 	&Apache::response::setup_params($$tagstack[-1],$safeeval);
1.1       albertel  286: 	my $response = &Apache::response::getresponse();
                    287: 	if ( $response =~ /[^\s]/) {
1.13      albertel  288: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.1       albertel  289: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    290: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    291: 	    my $ad;
1.13      albertel  292: 	    foreach my $answer (@answers) {
                    293: 		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
                    294: 		if ($response eq $answer) {
                    295: 		    $ad='EXACT_ANS';
                    296: 		    last;
                    297: 		} else {
                    298: 		    $ad='INCORRECT';
                    299: 		}
1.1       albertel  300: 	    }
1.42      albertel  301: 	    if ($ad && $Apache::lonhomework::type eq 'survey') {
                    302: 		$ad='SUBMITTED';
                    303: 	    }
1.1       albertel  304: 	    &Apache::response::handle_previous(\%previous,$ad);
                    305: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1.50      albertel  306: 	    $Apache::lonhomework::results{"resource.$partid.$id.molecule"}=$env{"form.MOLECULE_$id"};
1.1       albertel  307: 	}
1.2       albertel  308:     } elsif ($target eq "edit") {
                    309: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.15      albertel  310:     } elsif ($target eq 'answer') {
                    311: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
                    312: 						     $safeeval);
                    313: 	$result.=&Apache::response::answer_header('organicresponse');
                    314: 	foreach my $answer (@answers) {
                    315: 	    $result.=&Apache::response::answer_part('organicresponse',$answer);
                    316: 	}
                    317: 	$result.=&Apache::response::answer_footer('organicresponse');
1.1       albertel  318:     }
1.63      albertel  319: 
                    320:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                    321: 	$target eq 'tex' || $target eq 'analyze') {
                    322: 	&Apache::lonxml::increment_counter(&Apache::response::repetition());
                    323:     }
                    324:     &Apache::response::end_response();
1.1       albertel  325:     return $result;
                    326: }
                    327: 
1.6       albertel  328: sub start_organicstructure {
1.3       albertel  329:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    330:     my $result;
                    331:     if ($target eq 'web') {
                    332: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
                    333: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
1.12      albertel  334: 	my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.23      albertel  335: 	my $id=&Apache::loncommon::get_cgi_id();
1.19      albertel  336: 	$result="<img src='/cgi-bin/convertjme.pl?$id'";
                    337: 	if ($options =~ /border/) { $result.= ' border="1"'; }
                    338: 	$result.=' />';
1.16      albertel  339: 	&Apache::lonnet::appenv(
1.66      www       340:             'cgi.'.$id.'.JME'   => &escape($molecule),
1.17      albertel  341: 	    'cgi.'.$id.'.PNG' => 1,
1.16      albertel  342: 	    'cgi.'.$id.'.WIDTH' => $width );
1.17      albertel  343:     } elsif ($target eq 'tex') {
1.38      albertel  344: 	my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1);
1.58      foxr      345: 	my $webwidth=&Apache::lonxml::get_param('width', $parstack, $safeeval);
                    346: 	my $webheight=&Apache::lonxml::get_param('height', $parstack, $safeeval);
1.62      foxr      347: 	if (!$webheight) { $webheight = $webwidth; }
1.17      albertel  348: 	if (!$texwidth) { $texwidth='90'; }
1.58      foxr      349: 	$result = "%DYNAMICIMAGE:$webwidth:$webheight:$texwidth\n";
1.17      albertel  350: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
                    351: 	my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.50      albertel  352: 	my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
1.17      albertel  353: 	    '_'.time.'_'.$$.int(rand(1000)).'_organicstructure';
                    354: 	my $id=$filename;
                    355: 	&Apache::lonnet::appenv(
1.66      www       356: 		     'cgi.'.$id.'.JME'   => &escape($molecule),
1.17      albertel  357: 		     'cgi.'.$id.'.PS' => 1,
                    358: 		     'cgi.'.$id.'.WIDTH' => $texwidth );
1.66      www       359: 	$id=&escape($id);
1.17      albertel  360: 	&Apache::lonxml::register_ssi("/cgi-bin/convertjme.pl?$id");
1.20      albertel  361: 	if ($options =~ /border/) { $result.= '\fbox{'; }
                    362: 	$result .= '\graphicspath{{/home/httpd/perl/tmp/}}\includegraphics[width='.$texwidth.' mm]{'.$filename.'.eps}';
                    363: 	if ($options =~ /border/) { $result.= '} '; }
1.3       albertel  364:     } elsif ($target eq 'edit') {
                    365: 	$result .=&Apache::edit::tag_start($target,$token);
1.22      albertel  366: 	$result .=&Apache::edit::text_arg('Width (pixels):','width',$token,5);
                    367: 	$result .=&Apache::edit::text_arg('TeXwidth (mm):','texwidth',$token,5);
1.12      albertel  368: 	$result .='<nobr>';
1.3       albertel  369: 	$result .=&Apache::edit::text_arg('Molecule:','molecule',$token,40);
                    370: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    371: 						$safeeval);
1.12      albertel  372: 	my $options=&Apache::lonxml::get_param('options',$parstack,
                    373: 					       $safeeval);
                    374: 	if ($options !~ /reaction/) {
                    375: 	    $options.= ',multipart,number';
                    376: 	}
                    377: 						   
1.30      www       378: 	$result .=&separate_jme_window(undef,
1.12      albertel  379: 				 &Apache::edit::html_element_name('molecule'),
                    380: 				       $molecule,$options);
                    381: 	$result.="</nobr><br />";
                    382: 	$result .=&Apache::edit::checked_arg('Options:','options',
1.18      albertel  383: 					     [ ['reaction','Is a reaction'],
1.12      albertel  384: 					       ['border','Draw a border'] ],
                    385: 					     $token);
1.24      albertel  386: 	$result .=&Apache::edit::end_row();
1.3       albertel  387:     } elsif ($target eq 'modified') {
                    388: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    389: 						     $safeeval,'molecule',
1.22      albertel  390: 						     'width','texwidth',
                    391: 						     'options');
1.3       albertel  392: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    393:     }
                    394:     return $result;
1.1       albertel  395: }
                    396: 
1.6       albertel  397: sub end_organicstructure {
1.3       albertel  398:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    399:     my $result;
                    400:     if ($target eq "edit") {
                    401: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    402:     }
1.4       albertel  403:     return $result;
                    404: }
                    405: 
1.9       albertel  406: sub edit_reaction_button {
1.11      albertel  407:     my ($id,$field,$reaction)=@_;
1.66      www       408:     my $id_es=&escape($id);
                    409:     my $field_es=&escape($field);
                    410:     my $reaction_es=&escape($reaction);
1.41      www       411:     my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
1.65      albertel  412:     my $start_page = 
                    413: 	&Apache::loncommon::start_page('LON-CAPA Reaction Editor',undef,
                    414: 				       {'frameset'    => 1,
                    415: 					'js_ready'    => 1,
                    416: 					'add_entries' => {
1.68    ! albertel  417: 					    'rows'   => "30%,*",
1.65      albertel  418: 					    'border' => "0",}},);
                    419:     my $end_page = 
                    420: 	&Apache::loncommon::end_page({'frameset' => 1,
                    421: 				      'js_ready' => 1});
1.9       albertel  422:     my $result=<<EDITREACTION;
1.10      albertel  423: <script type="text/javascript">
1.47      albertel  424: // <!--
1.10      albertel  425:     function create_reaction_window_${id}_${field} () {
                    426: 	editor=window.open('','','width=500,height=270,scrollbars=no,resizable=yes');
1.41      www       427: 	editor.$docopen;
1.65      albertel  428: 	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');
1.68    ! albertel  429: 	editor.document.close();
1.10      albertel  430:     }
1.47      albertel  431: // -->
1.10      albertel  432: </script>
1.47      albertel  433: <input type='button' value='Edit Answer' onclick="javascript:create_reaction_window_${id}_${field}();void(0);" />
1.9       albertel  434: EDITREACTION
                    435:     return $result;
                    436: }
                    437: 
1.4       albertel  438: sub start_reactionresponse {
                    439:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    440:     my $result;
                    441:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.10      albertel  442:     if ($target eq 'meta') {
1.28      albertel  443: 	$result=&Apache::response::meta_package_write('reactionresponse');
1.10      albertel  444:     } elsif ($target eq 'web') {
1.11      albertel  445: 	my $partid = $Apache::inputtags::part;
                    446: 	my $id = $Apache::inputtags::response['-1'];
                    447: 	my $reaction=$Apache::lonhomework::history{"resource.$partid.$id.submission"};
1.35      albertel  448: 	if ($reaction eq '') {  $reaction=&Apache::lonxml::get_param('initial',$parstack,$safeeval); }
1.33      albertel  449: 	my $status=$Apache::inputtags::status['-1'];
                    450: 	if ($status eq 'CAN_ANSWER') {
                    451: 	    $result.=&edit_reaction_button($id,"HWVAL_$id",$reaction);
                    452: 	}
                    453: 	if (  &Apache::response::show_answer() ) {
                    454: 	    my $ans=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
1.51      albertel  455: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    456: 		&Apache::lonxml::default_homework_load($safeeval);
                    457: 	    }
                    458: 	    @Apache::scripttag::parser_env = @_;
                    459: 	    $Apache::inputtags::answertxt{$id}=&Apache::run::run("return &chemparse(q\0$ans\0);",$safeeval);
1.33      albertel  460: 	}
1.4       albertel  461:     } elsif ($target eq "edit") {
1.9       albertel  462: 	$result .=&Apache::edit::tag_start($target,$token);
                    463: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,
                    464: 						$safeeval);
1.10      albertel  465: 	$result .='<nobr>'.
                    466: 	    &Apache::edit::text_arg('Answer:','answer',$token,40);
                    467: 	$result .=&edit_reaction_button($id,&Apache::edit::html_element_name('answer'),$answer).'</nobr>';
1.35      albertel  468: 	my $initial=&Apache::lonxml::get_param('initial',$parstack,$safeeval);
                    469: 	$result.='<nobr>'.
1.43      albertel  470: 	    &Apache::edit::text_arg('Initial Reaction:','initial',$token,40);
1.35      albertel  471: 	$result .=&edit_reaction_button($id,&Apache::edit::html_element_name('initial'),$initial).'</nobr>';
1.10      albertel  472: 	
1.9       albertel  473: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.10      albertel  474:     }  elsif ($target eq 'modified') {
                    475: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.35      albertel  476: 						     $safeeval,'answer',
                    477: 						     'initial');
1.10      albertel  478: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.4       albertel  479:     }
                    480:     return $result;
                    481: }
                    482: 
                    483: sub end_reactionresponse {
                    484:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    485:     my $result;
1.64      albertel  486: 
                    487:     my $partid = $Apache::inputtags::part;
                    488:     my $id = $Apache::inputtags::response['-1'];
                    489: 
                    490:     if ($target eq 'grade' 
                    491: 	&& &Apache::response::submitted()
                    492: 	&& $Apache::lonhomework::type eq 'exam') {
                    493: 
                    494: 	&Apache::response::scored_response($partid,$id);
                    495: 
                    496:     } elsif ($target eq 'grade' 
                    497: 	&& &Apache::response::submitted()
                    498: 	&& $Apache::lonhomework::type ne 'exam') {
                    499: 
1.31      albertel  500: 	&Apache::response::setup_params($$tagstack[-1],$safeeval);
1.10      albertel  501: 	my $response = &Apache::response::getresponse();
                    502: 	if ( $response =~ /[^\s]/) {
1.15      albertel  503: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.10      albertel  504: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    505: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    506: 	    my $ad;
1.13      albertel  507: 	    foreach my $answer (@answers) {
                    508: 		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
1.34      albertel  509: 		if (&chem_standard_order($response) eq 
                    510: 		    &chem_standard_order($answer)) {
1.13      albertel  511: 		    $ad='EXACT_ANS';
                    512: 		} else {
                    513: 		    $ad='INCORRECT';
                    514: 		}
1.10      albertel  515: 	    }
1.42      albertel  516: 	    if ($ad && $Apache::lonhomework::type eq 'survey') {
                    517: 		$ad='SUBMITTED';
                    518: 	    }
1.10      albertel  519: 	    &Apache::response::handle_previous(\%previous,$ad);
                    520: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
                    521: 	}
                    522:     }  elsif ($target eq "edit") {
1.4       albertel  523: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.15      albertel  524:     } elsif ($target eq 'answer') {
                    525: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
                    526: 						     $safeeval);
                    527: 	$result.=&Apache::response::answer_header('reactionresponse');
                    528: 	foreach my $answer (@answers) {
                    529: 	    $result.=&Apache::response::answer_part('reactionresponse',
                    530: 						    $answer);
                    531: 	}
                    532: 	$result.=&Apache::response::answer_footer('reactionresponse');
1.4       albertel  533:     }
1.63      albertel  534: 
                    535:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                    536: 	$target eq 'tex' || $target eq 'analyze') {
                    537: 	&Apache::lonxml::increment_counter(&Apache::response::repetition());
                    538:     }
                    539:     &Apache::response::end_response();
1.3       albertel  540:     return $result;
1.1       albertel  541: }
                    542: 
1.46      albertel  543: sub start_chem {
                    544:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    545:     my $result = '';
1.48      albertel  546:     my $inside = &Apache::lonxml::get_all_text_unbalanced("/chem",$parser);
1.46      albertel  547:     if ($target eq 'tex' || $target eq 'web') {
1.48      albertel  548: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
                    549: 	if (!$Apache::lonxml::default_homework_loaded) {
                    550: 	    &Apache::lonxml::default_homework_load($safeeval);
                    551: 	}
                    552: 	@Apache::scripttag::parser_env = @_;
                    553: 	$result=&Apache::run::run("return &chemparse(q\0$inside\0);",$safeeval);
1.46      albertel  554:     }    
                    555:     return $result;
                    556: }
                    557: 
                    558: sub end_chem {
                    559:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    560:     my $result = '';
                    561:     return $result;
                    562: }
                    563: 
1.1       albertel  564: 1;
                    565: __END__

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