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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # chemical equation style response
                      3: #
1.95    ! raeburn     4: # $Id: chemresponse.pm,v 1.94 2014/08/20 18:02:08 raeburn 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.94      raeburn    71:     my $usejsme = 1;
1.95    ! raeburn    72:     if (($env{'request.course.id'}) && ($env{'request.state'} ne 'construct')) {
        !            73:         if (exists($env{'course.'.$env{'request.course.id'}.'.usejsme'})) {
        !            74:             if ($env{'course.'.$env{'request.course.id'}.'.usejsme'} eq '0') {
        !            75:                 $usejsme = 0;
        !            76:             }
        !            77:         } else {
        !            78:             my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
        !            79:             if ($domdefs{'usejsme'} eq '0') {
        !            80:                 $usejsme = 0;
        !            81:             }
        !            82:         }
        !            83:     } else {
        !            84:         my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
        !            85:         if ($domdefs{'usejsme'} eq '0') {
        !            86:             $usejsme = 0;
        !            87:         }
        !            88:     }
        !            89:     if ($usejsme) {
        !            90:         if ($env{'browser.type'} eq 'mozilla') {
        !            91:             if ($env{'browser.version'} < 4) {
        !            92:                 $usejsme = 0;
        !            93:             }
        !            94:         }
        !            95:     } else {
        !            96:         if ($env{'browser.mobile'}) {
        !            97:             $usejsme = 1;
        !            98:         }
        !            99:     }
1.94      raeburn   100:     my $linkstyle = 'display:none';
                    101:     my $creditstyle = 'display:inline';
                    102:     if ($env{'browser.type'} eq 'explorer') {
                    103:         if (($env{'browser.os'} eq 'win') && ($env{'browser.version'} < 9)) {
                    104:             $linkstyle = 'display:inline';
                    105:             $creditstyle = 'display:none';
                    106:         }
                    107:     }
1.2       albertel  108:     my $smilesection;
                    109:     if (defined($smile_input)) {
1.94      raeburn   110:         my $smiles;
                    111:         if ($usejsme) {
                    112:             $smiles = 'document.JME.smiles()'; 
                    113:         } else {
                    114:             $smiles = 'document.applets.JME.smiles()';
                    115:         }
1.2       albertel  116: 	$smilesection=<<SMILESECTION;
1.94      raeburn   117: 	opener.document.lonhomework.$smile_input.value = $smiles;
1.2       albertel  118: SMILESECTION
                    119:     }
                    120:     my $jmesection;
1.94      raeburn   121:     my $jmefile;
1.2       albertel  122:     if (defined($jme_input)) {
1.94      raeburn   123:         if ($usejsme) {
                    124:             $jmefile = 'document.JME.jmeFile()';
                    125:         } else {
                    126:             $jmefile = 'document.applets.JME.jmeFile()';
                    127:         }
1.2       albertel  128: 	$jmesection=<<JMESECTION;
1.94      raeburn   129: 	opener.document.lonhomework.$jme_input.value = $jmefile;
1.2       albertel  130: JMESECTION
                    131:     }
                    132: 
1.94      raeburn   133:     if ($molecule) {
                    134:         if ($usejsme) {
                    135:             $molecule = 'document.JME.readMolecule("'.$molecule.'")';
                    136:         } else {
                    137:             $molecule='<param name="jme" value="'.$molecule.'" />';
                    138:         }
                    139:     }
1.57      albertel  140:     my $insert_answer;
1.59      albertel  141:     if ($shown_text eq '') { 
1.57      albertel  142: 	$insert_answer=
1.94      raeburn   143: 	    '<input type="button" name="submit" value="'.&mt('Insert Answer').'" onclick="javascript:submitSmiles();" /><br />';
                    144:     }
                    145: 
                    146:    
                    147: 
                    148:     my ($jsme_js,$js,$buttonstyle,$viewportjs,$resizejs);
                    149:     if ($usejsme) {
                    150:         $buttonstyle = 'display:none';
                    151:         $resizejs =<<RESIZEJS;
                    152: <script type="text/javascript">
                    153: function callResize() {
                    154:     var timer;
                    155:     clearTimeout(timer);
                    156:     timer=setTimeout('resize_jsme()',100);
                    157: }
                    158: 
                    159: window.onresize = callResize;
                    160: 
                    161: function resize_jsme() {
                    162:     init_geometry();
                    163:     var vph = Geometry.getViewportHeight();
                    164:     var vpw = Geometry.getViewportWidth();
                    165:     var lowerdivheight = document.getElementById('JMEbuttons').offsetHeight;
                    166:     var formheight = document.getElementById('JMEform').offsetHeight;
                    167:     var freevspace = vph-(lowerdivheight+50);
                    168:     var freehspace = vpw-20;
                    169:     if (typeof jsmeApplet !== 'undefined') {
                    170:         jsmeApplet.setSize(freehspace,freevspace);
                    171:     }
                    172: }
                    173: </script>
                    174: RESIZEJS
                    175:         $resizejs = &Apache::loncommon::js_ready($resizejs);
                    176:         $jsme_js = '
                    177: <script type="text/javascript" language="javascript" src="/adm/jsme/jsme.nocache.js"></script>'."\n";
                    178:         $js =<<CHEMJS;
                    179: <script type="text/javascript">
                    180: function jsmeOnLoad() {
                    181:     document.getElementById('JMErefresh').style.display="none";
                    182:     document.getElementById('JMEcredits').style.display="inline";
                    183:     jsmeApplet = new JSApplet.JSME("jme", "420px", "330px");
                    184:     document.JME = jsmeApplet;
                    185:     $molecule;
                    186:     document.getElementById('JMEbuttons').style.display="block";
                    187:     callResize();
                    188: }
                    189: 
                    190: function submitSmiles() {
                    191:     jmeFile = document.JME.jmeFile();
                    192:     if (jmeFile == "") {
                    193:         alert("Nothing to submit");
                    194:     } else {
                    195:         $jmesection
                    196:         $smilesection
                    197:         window.close();
1.57      albertel  198:     }
1.94      raeburn   199: }
                    200: function openHelpWindow() {
                    201:     window.open("http://peter-ertl.com/jsme/2013_03/help.html","","scrollbars=yes,resizable=yes,width=500,height=600");
                    202: }
                    203: 
                    204: </script>
                    205: 
                    206: CHEMJS
1.57      albertel  207: 
1.94      raeburn   208:         $viewportjs = &Apache::loncommon::viewport_geometry_js();
                    209:         $viewportjs = '<script type="text/javascript">'."\n".
                    210:                       $viewportjs."\n".
                    211:                       '</script>';
                    212: 
                    213:     } else {
                    214:         $buttonstyle = 'display:block';
                    215:         $js =<<CHEMJS; 
1.47      albertel  216: <script type="text/javascript">
1.1       albertel  217: function submitSmiles() {
1.21      albertel  218:     jmeFile = document.applets.JME.jmeFile();
                    219:     if (jmeFile == "") {
1.1       albertel  220: 	alert("Nothing to submit");
                    221:     } else {
1.21      albertel  222:         $jmesection
1.2       albertel  223:         $smilesection
1.1       albertel  224: 	window.close();
                    225:     }
                    226: }
                    227: function openHelpWindow() {
1.2       albertel  228:     window.open("/adm/jme/jme_help.html","","scrollbars=yes,resizable=yes,width=500,height=600");
1.1       albertel  229: }
1.60      albertel  230: function substituent(r) {document.applets.JME.setSubstituent(r);}
1.1       albertel  231: </script>
1.65      albertel  232: CHEMJS
1.94      raeburn   233:     }
1.65      albertel  234: 
                    235:     my $start_page = 
1.94      raeburn   236:         &Apache::loncommon::start_page('Molecule Editor',$viewportjs,
1.65      albertel  237: 				       {'only_body' => 1,
1.69      albertel  238: 					'js_ready'  => 1,
1.65      albertel  239: 					'bgcolor'   => '#FFFFFF',});
1.69      albertel  240:     my $end_page =
                    241:  	&Apache::loncommon::end_page({'js_ready' => 1,});
1.88      www       242:     my $java_not_enabled=&Apache::lonhtmlcommon::java_not_enabled();
1.93      bisitz    243:     my %lt = &Apache::lonlocal::texthash(
                    244:         'seltext' => 'Select substituent...',
                    245:         'close'   => 'Close',
                    246:         'help'    => 'Help',
                    247:        );
1.94      raeburn   248:     my $body = "
                    249: $jsme_js
                    250: $js".'
1.1       albertel  251: <center>
1.94      raeburn   252: <form action="" id="JMEform">
                    253: ';
                    254:     if ($usejsme) {
                    255:         $body.= <<"ENDCHEM";
                    256: <div id="jme">
                    257: <div id="JMEcredits" style="$creditstyle">
                    258: <span style="font-size:small; font-family:arial,sans-serif;"><a href="http://peter-ertl.com/jsme/">JSME Molecular Editor</a> courtesy of Peter Ertl (Novartis) and Bruno Bienfait</span></div>
                    259: </div>
                    260: ENDCHEM
                    261:     } else {
                    262:         $body.=<<CHEMPAGE;
1.60      albertel  263:   <table width="440"><tr>
                    264:     <td></td>
                    265:     <td align="right">
                    266:       <select onchange="javascript:substituent(options[selectedIndex].text)">
1.93      bisitz    267:         <option>$lt{'seltext'}</option>
1.60      albertel  268:         <option>-C(=O)OH</option>
                    269:         <option>-C(=O)OMe</option>
                    270:         <option>-OC(=O)Me</option>
                    271:         <option>-CMe3</option>
                    272:         <option>-CF3</option>
                    273:         <option>-CCl3</option>
                    274:         <option>-NO2</option>
                    275:         <option>-SO2-NH2</option>
                    276:         <option>-NH-SO2-Me</option>
                    277:         <option>-NMe2</option>
                    278:         <option>-C#N</option>
                    279:         <option>-C#C-Me</option>
                    280:         <option>-C#CH</option>
                    281:       </select>
                    282:     </td></tr>
                    283:   </table>
                    284: <applet code="JME.class" name="JME" archive="/adm/jme/JME.jar" width="440" height="390" mayscript>
1.88      www       285: $java_not_enabled
1.12      albertel  286: $molecule
1.6       albertel  287: <param name="options" value="$options" />
1.94      raeburn   288: </applet>
                    289: <br />
1.49      albertel  290: <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.94      raeburn   291: CHEMPAGE
                    292:     }
                    293:     $body .= <<CHEMPAGE;
                    294: <div id="JMEbuttons" style="$buttonstyle">
1.57      albertel  295: $insert_answer
1.93      bisitz    296: <input type="button" value="$lt{'close'}" onclick="javascript:window.close()" />
1.1       albertel  297: &nbsp;&nbsp;
1.93      bisitz    298: <input type="button" value="$lt{'help'}" onclick="javascript:openHelpWindow()" />
1.94      raeburn   299: </div>
                    300: <div id="JMErefresh" style="$linkstyle">
                    301: <a href="javascript:location.reload();">Display Molecule Editor</a>
                    302: </div> 
1.1       albertel  303: </form>
                    304: </center>
                    305: CHEMPAGE
1.65      albertel  306: 
1.69      albertel  307:     $body=&Apache::loncommon::js_ready($body);
1.53      albertel  308:     my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
1.41      www       309:     my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
1.55      albertel  310:     my $display=&mt('Draw Molecule');
                    311:     if (defined($shown_text)) { $display=&mt($shown_text); }
1.79      riegler   312:     my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
1.94      raeburn   313:     my $function =
1.91      raeburn   314: 	'LONCAPA_draw_molecule_'.&get_uniq_name();
1.1       albertel  315:     my $result=<<CHEMINPUT;
1.69      albertel  316: <script type="text/javascript">
                    317:     function $function() {
                    318: 	editor=window.open($nothing,'jmeedit','width=500,height=500,menubar=no,scrollbars=no,resizable=yes');
1.94      raeburn   319:         if (editor) {
                    320: 	    editor.$docopen;
                    321: 	    editor.document.write('$start_page $body $resizejs $end_page');
                    322: 	    editor.document.close();
                    323: 	    editor.focus();
                    324:         }
1.69      albertel  325:     }
                    326: </script>
1.86      raeburn   327: CHEMINPUT
1.94      raeburn   328:     my $jscall = "javascript:$function();void(0);";
1.86      raeburn   329:     if ($shown_text eq '') {
1.94      raeburn   330:         $result .=<<PENCIL;
                    331: <a href="$jscall"><img class="stift" src="$iconpath/stift.gif" alt="$display" title="$display" /></a>
1.86      raeburn   332: PENCIL
                    333:     } else {
1.94      raeburn   334:         $result .= '<input type="button" value="'.&mt($shown_text).'" onclick="$jscall" />';
1.86      raeburn   335:     }
1.1       albertel  336:     return $result;
                    337: }
1.56      albertel  338: sub jme_img {
                    339:     my ($jme,$smile,$width,$options)=@_;
                    340:     my $id=&Apache::loncommon::get_cgi_id();
                    341:     my $result='<img alt="'.$smile.'" src="/cgi-bin/convertjme.pl?'.$id.'"';
                    342:     if ($options =~ /border/) { $result.= ' border="1"'; }
                    343:     $result.=' />';
1.78      raeburn   344:     &Apache::lonnet::appenv({'cgi.'.$id.'.JME'   =>
                    345: 			     &escape($jme),
                    346: 			     'cgi.'.$id.'.PNG'   => 1,
                    347: 			     'cgi.'.$id.'.WIDTH' => $width});
1.56      albertel  348:     return $result;
                    349: }
                    350: 
1.6       albertel  351: sub start_organicresponse {
1.1       albertel  352:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    353:     my $result;
                    354:     my $partid = $Apache::inputtags::part;
                    355:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    356:     if ($target eq 'meta') {
1.28      albertel  357: 	$result=&Apache::response::meta_package_write('organicresponse');
1.1       albertel  358:     } elsif ($target eq 'web') {
1.55      albertel  359: 	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
                    360: 						 $safeeval);
1.86      raeburn   361: 	if (&Apache::response::show_answer()) {
                    362:             my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
                    363:                                                      $safeeval);
                    364:             if ($jmeanswer ne '') {
                    365: 	        my $options=&Apache::lonxml::get_param('options',$parstack,
                    366: 	    					       $safeeval);
                    367: 	        my $width=&Apache::lonxml::get_param('width',$parstack,
                    368: 						     $safeeval);
                    369: 	        my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
                    370: 							     $safeeval);
                    371: 	        $result.=&jme_img($jmeanswer,$answers[0],$width,$options);
                    372:             }
1.1       albertel  373: 	} else {
1.44      albertel  374: 	    $result.= '<input type="hidden" name="MOLECULE_'.$id.'" value="" />';
1.1       albertel  375: 	}
1.2       albertel  376:     } elsif ($target eq 'edit') {
                    377: 	$result .=&Apache::edit::tag_start($target,$token);
1.12      albertel  378: 	my $options=&Apache::lonxml::get_param('options',$parstack,
                    379: 					       $safeeval);
                    380: 	if ($options !~ /multipart/) { $options.=',multipart'; }
1.83      bisitz    381: 	$result .='<span class="LC_nobreak">'.
1.7       albertel  382: 	    &Apache::edit::text_arg('Starting Molecule:','molecule',
                    383: 				    $token,40);
1.2       albertel  384: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    385: 						$safeeval);
1.30      www       386: 	$result .=&separate_jme_window(undef,
1.2       albertel  387: 		      &Apache::edit::html_element_name('molecule'),
1.12      albertel  388: 		      $molecule,$options);
1.83      bisitz    389: 	$result .='</span><br /><span class="LC_nobreak">';
1.2       albertel  390: 	$result .=&Apache::edit::text_arg('Correct Answer:','answer',
                    391: 					  $token,40);
1.85      raeburn   392: 	$result .='</span><br /><span class="LC_nobreak">';
1.86      raeburn   393: 	$result .=&Apache::edit::text_arg('JME string of the answer - automatically updated by "Insert Answer" in the JME pop-up (click pencil):',
1.67      albertel  394: 					  'jmeanswer',$token);
1.2       albertel  395: 	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
                    396: 						 $safeeval);
1.30      www       397: 	$result .=&separate_jme_window(
1.2       albertel  398:                       &Apache::edit::html_element_name('answer'),
                    399:                       &Apache::edit::html_element_name('jmeanswer'),
1.12      albertel  400: 		      $jmeanswer,$options);
1.83      bisitz    401: 	$result .='</span><br />';
1.12      albertel  402: 	$result .=&Apache::edit::checked_arg('Options:','options',
1.29      www       403: 				    [ ['autoez','Auto E,Z stereochemistry'],
1.18      albertel  404: 				      ['multipart','Multipart Structures'],
1.12      albertel  405: 				      ['nostereo','No stereochemistry'],
                    406: 				      ['reaction','Is a reaction'],
1.18      albertel  407: 				      ['number','Able to number atoms'] ],
1.12      albertel  408: 					     ,$token);
1.44      albertel  409: 	$result .=&Apache::edit::text_arg('Width of correct answer image:',
                    410: 					  'width',$token,10);
1.2       albertel  411: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    412:     } elsif ($target eq 'modified') {
                    413: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    414: 						     $safeeval,'molecule',
1.6       albertel  415: 						     'answer','jmeanswer',
1.44      albertel  416: 						     'options','width');
1.2       albertel  417: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.1       albertel  418:     }
1.63      albertel  419: 
1.1       albertel  420:     return $result;
                    421: }
                    422: 
1.6       albertel  423: sub end_organicresponse {
1.1       albertel  424:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    425:     my $result;
1.64      albertel  426: 
                    427:     my $partid = $Apache::inputtags::part;
                    428:     my $id = $Apache::inputtags::response['-1'];
                    429: 
                    430:     if ($target eq 'grade' 
                    431: 	&& &Apache::response::submitted()
                    432: 	&& $Apache::lonhomework::type eq 'exam') {
                    433: 
                    434: 	&Apache::response::scored_response($partid,$id);
                    435: 
                    436:     } elsif ($target eq 'grade' 
                    437: 	&& &Apache::response::submitted()
                    438: 	&& $Apache::lonhomework::type ne 'exam') {
                    439: 
1.31      albertel  440: 	&Apache::response::setup_params($$tagstack[-1],$safeeval);
1.1       albertel  441: 	my $response = &Apache::response::getresponse();
                    442: 	if ( $response =~ /[^\s]/) {
1.13      albertel  443: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.1       albertel  444: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    445: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    446: 	    my $ad;
1.13      albertel  447: 	    foreach my $answer (@answers) {
                    448: 		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
                    449: 		if ($response eq $answer) {
                    450: 		    $ad='EXACT_ANS';
                    451: 		    last;
                    452: 		} else {
                    453: 		    $ad='INCORRECT';
                    454: 		}
1.1       albertel  455: 	    }
1.87      raeburn   456:             if ($ad) {
                    457: 	        if ($Apache::lonhomework::type eq 'survey') {
                    458: 		    $ad='SUBMITTED';
                    459:                 } elsif ($Apache::lonhomework::type eq 'surveycred') {
                    460:                     $ad='SUBMITTED_CREDIT';
                    461:                 } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
                    462:                     $ad='ANONYMOUS';
                    463:                 } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
                    464:                     $ad='ANONYMOUS_CREDIT';
                    465:                 }
                    466:             }
1.1       albertel  467: 	    &Apache::response::handle_previous(\%previous,$ad);
                    468: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1.50      albertel  469: 	    $Apache::lonhomework::results{"resource.$partid.$id.molecule"}=$env{"form.MOLECULE_$id"};
1.1       albertel  470: 	}
1.2       albertel  471:     } elsif ($target eq "edit") {
                    472: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.15      albertel  473:     } elsif ($target eq 'answer') {
                    474: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
                    475: 						     $safeeval);
                    476: 	$result.=&Apache::response::answer_header('organicresponse');
                    477: 	foreach my $answer (@answers) {
                    478: 	    $result.=&Apache::response::answer_part('organicresponse',$answer);
                    479: 	}
                    480: 	$result.=&Apache::response::answer_footer('organicresponse');
1.1       albertel  481:     }
1.73      albertel  482:     if ($target eq 'web') {
                    483: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer_organic,
                    484: 						  ['molecule'])
                    485:     }
1.63      albertel  486: 
                    487:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                    488: 	$target eq 'tex' || $target eq 'analyze') {
1.90      raeburn   489:         my $repetition = &Apache::response::repetition();
                    490: 	&Apache::lonxml::increment_counter($repetition,"$partid.$id"); # part.response
1.74      foxr      491: 	if ($target eq 'analyze') {
1.76      raeburn   492:             $Apache::lonhomework::analyze{"$partid.$id.type"} = 'organicresponse';
1.84      raeburn   493:             push (@{ $Apache::lonhomework::analyze{"parts"} },"$partid.$id");
1.74      foxr      494: 	    &Apache::lonhomework::set_bubble_lines();
                    495: 	}
1.63      albertel  496:     }
1.86      raeburn   497:     if ($target eq 'web' ) {
                    498:         my ($showpencil,$shown_text);
                    499:         if ($Apache::inputtags::status['-1'] eq 'CAN_ANSWER') {
                    500:             $showpencil = 1;
                    501:         } elsif (&Apache::response::show_answer()) {
                    502:             my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,                                                         $safeeval);
                    503:             if ($jmeanswer eq '') {
                    504:                 $showpencil = 1;
                    505:                 $shown_text="Show Your Last Answer";
                    506:             }
                    507:         }
                    508:         if ($showpencil) {
1.79      riegler   509:             my $options=&Apache::lonxml::get_param('options',$parstack,
                    510:                                                    $safeeval);
1.86      raeburn   511: 
1.79      riegler   512: 	    my $molecule;
                    513: 	    if (defined($Apache::lonhomework::history{"resource.$partid.$id.molecule"})) {
                    514: 		$molecule=$Apache::lonhomework::history{"resource.$partid.$id.molecule"};
                    515: 	    } else {
                    516: 		$molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    517: 						     $safeeval);
                    518: 	    }
1.86      raeburn   519: 	    $result.=&separate_jme_window("HWVAL_$id","MOLECULE_$id",$molecule,
                    520:                                           $options,$shown_text);
                    521:         }
1.79      riegler   522:     }
1.63      albertel  523:     &Apache::response::end_response();
1.1       albertel  524:     return $result;
                    525: }
                    526: 
1.73      albertel  527: sub format_prior_answer_organic {
                    528:     my ($mode,$answer,$other_data) = @_;
1.93      bisitz    529:     my $result=&mt('Smile representation: [_1]','"<tt>'.$answer.'</tt>"');
1.73      albertel  530:     my $jme=$other_data->[0];
                    531:     $result.=&jme_img($jme,$answer,400);
                    532:     return $result;
                    533: }
                    534: 
1.6       albertel  535: sub start_organicstructure {
1.3       albertel  536:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    537:     my $result;
                    538:     if ($target eq 'web') {
                    539: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
                    540: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
1.12      albertel  541: 	my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.23      albertel  542: 	my $id=&Apache::loncommon::get_cgi_id();
1.19      albertel  543: 	$result="<img src='/cgi-bin/convertjme.pl?$id'";
                    544: 	if ($options =~ /border/) { $result.= ' border="1"'; }
                    545: 	$result.=' />';
1.16      albertel  546: 	&Apache::lonnet::appenv(
1.78      raeburn   547:             {'cgi.'.$id.'.JME'   => &escape($molecule),
                    548: 	     'cgi.'.$id.'.PNG' => 1,
                    549: 	     'cgi.'.$id.'.WIDTH' => $width});
1.17      albertel  550:     } elsif ($target eq 'tex') {
1.38      albertel  551: 	my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1);
1.58      foxr      552: 	my $webwidth=&Apache::lonxml::get_param('width', $parstack, $safeeval);
                    553: 	my $webheight=&Apache::lonxml::get_param('height', $parstack, $safeeval);
1.62      foxr      554: 	if (!$webheight) { $webheight = $webwidth; }
1.17      albertel  555: 	if (!$texwidth) { $texwidth='90'; }
1.58      foxr      556: 	$result = "%DYNAMICIMAGE:$webwidth:$webheight:$texwidth\n";
1.17      albertel  557: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
                    558: 	my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.50      albertel  559: 	my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
1.17      albertel  560: 	    '_'.time.'_'.$$.int(rand(1000)).'_organicstructure';
                    561: 	my $id=$filename;
                    562: 	&Apache::lonnet::appenv(
1.78      raeburn   563: 		     {'cgi.'.$id.'.JME'   => &escape($molecule),
                    564: 		      'cgi.'.$id.'.PS' => 1,
                    565: 		      'cgi.'.$id.'.WIDTH' => $texwidth});
1.66      www       566: 	$id=&escape($id);
1.17      albertel  567: 	&Apache::lonxml::register_ssi("/cgi-bin/convertjme.pl?$id");
1.20      albertel  568: 	if ($options =~ /border/) { $result.= '\fbox{'; }
1.89      foxr      569: 	$result .= '\graphicspath{{'.LONCAPA::tempdir().
                    570: 	    '}}\includegraphics[width='.$texwidth.' mm]{'.$filename.'.eps}';
1.20      albertel  571: 	if ($options =~ /border/) { $result.= '} '; }
1.3       albertel  572:     } elsif ($target eq 'edit') {
                    573: 	$result .=&Apache::edit::tag_start($target,$token);
1.22      albertel  574: 	$result .=&Apache::edit::text_arg('Width (pixels):','width',$token,5);
                    575: 	$result .=&Apache::edit::text_arg('TeXwidth (mm):','texwidth',$token,5);
1.83      bisitz    576: 	$result .='<span class="LC_nobreak">';
1.3       albertel  577: 	$result .=&Apache::edit::text_arg('Molecule:','molecule',$token,40);
                    578: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    579: 						$safeeval);
1.12      albertel  580: 	my $options=&Apache::lonxml::get_param('options',$parstack,
                    581: 					       $safeeval);
                    582: 	if ($options !~ /reaction/) {
                    583: 	    $options.= ',multipart,number';
                    584: 	}
                    585: 						   
1.30      www       586: 	$result .=&separate_jme_window(undef,
1.12      albertel  587: 				 &Apache::edit::html_element_name('molecule'),
                    588: 				       $molecule,$options);
1.83      bisitz    589: 	$result.="</span><br />";
1.12      albertel  590: 	$result .=&Apache::edit::checked_arg('Options:','options',
1.18      albertel  591: 					     [ ['reaction','Is a reaction'],
1.12      albertel  592: 					       ['border','Draw a border'] ],
                    593: 					     $token);
1.24      albertel  594: 	$result .=&Apache::edit::end_row();
1.3       albertel  595:     } elsif ($target eq 'modified') {
                    596: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    597: 						     $safeeval,'molecule',
1.22      albertel  598: 						     'width','texwidth',
                    599: 						     'options');
1.3       albertel  600: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    601:     }
                    602:     return $result;
1.1       albertel  603: }
                    604: 
1.6       albertel  605: sub end_organicstructure {
1.3       albertel  606:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    607:     my $result;
                    608:     if ($target eq "edit") {
                    609: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    610:     }
1.4       albertel  611:     return $result;
                    612: }
                    613: 
1.9       albertel  614: sub edit_reaction_button {
1.11      albertel  615:     my ($id,$field,$reaction)=@_;
1.66      www       616:     my $id_es=&escape($id);
                    617:     my $field_es=&escape($field);
                    618:     my $reaction_es=&escape($reaction);
1.41      www       619:     my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
1.80      riegler   620:     my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
                    621:     my $display=&mt('Edit Answer');
1.65      albertel  622:     my $start_page = 
                    623: 	&Apache::loncommon::start_page('LON-CAPA Reaction Editor',undef,
                    624: 				       {'frameset'    => 1,
                    625: 					'js_ready'    => 1,
                    626: 					'add_entries' => {
1.68      albertel  627: 					    'rows'   => "30%,*",
1.65      albertel  628: 					    'border' => "0",}},);
                    629:     my $end_page = 
                    630: 	&Apache::loncommon::end_page({'frameset' => 1,
                    631: 				      'js_ready' => 1});
1.9       albertel  632:     my $result=<<EDITREACTION;
1.10      albertel  633: <script type="text/javascript">
1.47      albertel  634: // <!--
1.10      albertel  635:     function create_reaction_window_${id}_${field} () {
                    636: 	editor=window.open('','','width=500,height=270,scrollbars=no,resizable=yes');
1.41      www       637: 	editor.$docopen;
1.65      albertel  638: 	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  639: 	editor.document.close();
1.10      albertel  640:     }
1.47      albertel  641: // -->
1.10      albertel  642: </script>
1.80      riegler   643: <a href="javascript:create_reaction_window_${id}_${field}();void(0);"><img class="stift" src='$iconpath/stift.gif' alt='$display' title='$display' /></a>
1.9       albertel  644: EDITREACTION
                    645:     return $result;
                    646: }
                    647: 
1.4       albertel  648: sub start_reactionresponse {
                    649:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    650:     my $result;
                    651:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.10      albertel  652:     if ($target eq 'meta') {
1.28      albertel  653: 	$result=&Apache::response::meta_package_write('reactionresponse');
1.10      albertel  654:     } elsif ($target eq 'web') {
1.11      albertel  655: 	my $partid = $Apache::inputtags::part;
                    656: 	my $id = $Apache::inputtags::response['-1'];
1.33      albertel  657: 	if (  &Apache::response::show_answer() ) {
                    658: 	    my $ans=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
1.51      albertel  659: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    660: 		&Apache::lonxml::default_homework_load($safeeval);
                    661: 	    }
                    662: 	    @Apache::scripttag::parser_env = @_;
1.71      albertel  663: 	    $Apache::inputtags::answertxt{$id}=[&Apache::run::run("return &chemparse(q\0$ans\0);",$safeeval)];
1.33      albertel  664: 	}
1.4       albertel  665:     } elsif ($target eq "edit") {
1.9       albertel  666: 	$result .=&Apache::edit::tag_start($target,$token);
                    667: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,
                    668: 						$safeeval);
1.82      raeburn   669: 	$result .='<span class="LC_nobreak">'.
1.10      albertel  670: 	    &Apache::edit::text_arg('Answer:','answer',$token,40);
1.82      raeburn   671: 	$result .=&edit_reaction_button($id,&Apache::edit::html_element_name('answer'),$answer).'</span>';
1.35      albertel  672: 	my $initial=&Apache::lonxml::get_param('initial',$parstack,$safeeval);
1.82      raeburn   673: 	$result.='<span class="LC_nobreak">'.
1.43      albertel  674: 	    &Apache::edit::text_arg('Initial Reaction:','initial',$token,40);
1.82      raeburn   675: 	$result .=&edit_reaction_button($id,&Apache::edit::html_element_name('initial'),$initial).'</span>';
1.9       albertel  676: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.10      albertel  677:     }  elsif ($target eq 'modified') {
                    678: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.35      albertel  679: 						     $safeeval,'answer',
                    680: 						     'initial');
1.10      albertel  681: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.4       albertel  682:     }
                    683:     return $result;
                    684: }
                    685: 
                    686: sub end_reactionresponse {
                    687:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    688:     my $result;
1.64      albertel  689: 
                    690:     my $partid = $Apache::inputtags::part;
                    691:     my $id = $Apache::inputtags::response['-1'];
                    692: 
                    693:     if ($target eq 'grade' 
                    694: 	&& &Apache::response::submitted()
                    695: 	&& $Apache::lonhomework::type eq 'exam') {
                    696: 
                    697: 	&Apache::response::scored_response($partid,$id);
                    698: 
                    699:     } elsif ($target eq 'grade' 
                    700: 	&& &Apache::response::submitted()
                    701: 	&& $Apache::lonhomework::type ne 'exam') {
                    702: 
1.31      albertel  703: 	&Apache::response::setup_params($$tagstack[-1],$safeeval);
1.10      albertel  704: 	my $response = &Apache::response::getresponse();
                    705: 	if ( $response =~ /[^\s]/) {
1.15      albertel  706: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.10      albertel  707: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    708: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    709: 	    my $ad;
1.13      albertel  710: 	    foreach my $answer (@answers) {
                    711: 		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
1.34      albertel  712: 		if (&chem_standard_order($response) eq 
                    713: 		    &chem_standard_order($answer)) {
1.13      albertel  714: 		    $ad='EXACT_ANS';
                    715: 		} else {
                    716: 		    $ad='INCORRECT';
                    717: 		}
1.10      albertel  718: 	    }
1.87      raeburn   719:             if ($ad) {
                    720:                 if ($Apache::lonhomework::type eq 'survey') {
                    721: 		    $ad='SUBMITTED';
                    722: 	        } elsif ($ad && $Apache::lonhomework::type eq 'surveycred') {
                    723:                     $ad='SUBMITTED_CREDIT';
                    724:                 } elsif ($ad && $Apache::lonhomework::type eq 'anonsurvey') {
                    725:                     $ad='ANONYMOUS';
                    726:                 } elsif ($ad && $Apache::lonhomework::type eq 'anonsurveycred') {
                    727:                     $ad='ANONYMOUS_CREDIT';
                    728:                 }
                    729:             }
1.10      albertel  730: 	    &Apache::response::handle_previous(\%previous,$ad);
                    731: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
                    732: 	}
                    733:     }  elsif ($target eq "edit") {
1.4       albertel  734: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.15      albertel  735:     } elsif ($target eq 'answer') {
                    736: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
                    737: 						     $safeeval);
                    738: 	$result.=&Apache::response::answer_header('reactionresponse');
                    739: 	foreach my $answer (@answers) {
                    740: 	    $result.=&Apache::response::answer_part('reactionresponse',
                    741: 						    $answer);
                    742: 	}
                    743: 	$result.=&Apache::response::answer_footer('reactionresponse');
1.4       albertel  744:     }
1.72      albertel  745:     if ($target eq 'web') {
                    746: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response_reaction);
                    747:     }
1.63      albertel  748: 
                    749:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                    750: 	$target eq 'tex' || $target eq 'analyze') {
1.90      raeburn   751: 	my $repetition = &Apache::response::repetition();
                    752:         &Apache::lonxml::increment_counter($repetition,"$partid.$id");
1.77      raeburn   753:         if ($target eq 'analyze') {
                    754:             $Apache::lonhomework::analyze{"$partid.$id.type"} = 'reactionresponse';
1.84      raeburn   755:             push (@{ $Apache::lonhomework::analyze{"parts"} },"$partid.$id");
1.77      raeburn   756:             &Apache::lonhomework::set_bubble_lines();
                    757:         }
1.63      albertel  758:     }
1.81      raeburn   759:     my $status=$Apache::inputtags::status['-1'];
                    760:     if  (($target eq 'web') && ($Apache::lonhomework::type ne 'exam') && ($status eq 'CAN_ANSWER')) {
                    761:         my $reaction=$Apache::lonhomework::history{"resource.$partid.$id.submission"};
                    762:         if ($reaction eq '') {  $reaction=&Apache::lonxml::get_param('initial',$parstack,$safeeval); }
                    763:         $result.=&edit_reaction_button($id,"HWVAL_$id",$reaction);
                    764:     }
1.63      albertel  765:     &Apache::response::end_response();
1.3       albertel  766:     return $result;
1.1       albertel  767: }
                    768: 
1.72      albertel  769: sub format_prior_response_reaction {
                    770:     my ($mode,$answer) =@_;
                    771:     return '<span class="LC_prior_reaction">'.
                    772: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
                    773: }
                    774: 
1.46      albertel  775: sub start_chem {
                    776:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    777:     my $result = '';
1.48      albertel  778:     my $inside = &Apache::lonxml::get_all_text_unbalanced("/chem",$parser);
1.46      albertel  779:     if ($target eq 'tex' || $target eq 'web') {
1.48      albertel  780: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
                    781: 	if (!$Apache::lonxml::default_homework_loaded) {
                    782: 	    &Apache::lonxml::default_homework_load($safeeval);
                    783: 	}
                    784: 	@Apache::scripttag::parser_env = @_;
                    785: 	$result=&Apache::run::run("return &chemparse(q\0$inside\0);",$safeeval);
1.46      albertel  786:     }    
                    787:     return $result;
                    788: }
                    789: 
                    790: sub end_chem {
                    791:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    792:     my $result = '';
                    793:     return $result;
                    794: }
                    795: 
1.91      raeburn   796: my $uniq=0;
                    797: sub get_uniq_name {
                    798:     $uniq++;
                    799:     return 'uniquename'.$uniq;
                    800: }
                    801: 
1.1       albertel  802: 1;
                    803: __END__

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