File:  [LON-CAPA] / loncom / homework / chemresponse.pm
Revision 1.93: download - view: text, annotated - select for diffs
Thu Feb 13 18:13:22 2014 UTC (10 years, 2 months ago) by bisitz
Branches: MAIN
CVS tags: version_2_11_0_RC3, version_2_11_0, HEAD
Internationalization: Added missing &mt() calls

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

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