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

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

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