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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # chemical equation style response
                      3: #
1.6     ! albertel    4: # $Id: chemresponse.pm,v 1.5 2003/05/05 21:14:10 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;
                     33: 
                     34: BEGIN {
1.6     ! albertel   35:     &Apache::lonxml::register('Apache::chemresponse',('organicresponse','organicstructure','reactionresponse'));
1.1       albertel   36: }
                     37: 
                     38: sub seperate_jme_window {
1.6     ! albertel   39:     my ($smile_input,$jme_input,$molecule,$options)=@_;
1.2       albertel   40:     my $smilesection;
                     41:     if (defined($smile_input)) {
                     42: 	$smilesection=<<SMILESECTION;
                     43: 	opener.document.lonhomework.$smile_input.value = smiles;
                     44: SMILESECTION
                     45:     }
                     46:     my $jmesection;
                     47:     if (defined($jme_input)) {
                     48: 	$jmesection=<<JMESECTION;
                     49: 	jmeFile = document.applets.JME.jmeFile();
                     50: 	opener.document.lonhomework.$jme_input.value = jmeFile;
                     51: JMESECTION
                     52:     }
                     53: 
1.1       albertel   54:     my $body=<<CHEMPAGE;
                     55: <html>
                     56: <head>
                     57: <title>Molecule Editor</title>
                     58: <script language="JavaScript">
                     59: function submitSmiles() {
                     60:     smiles = document.applets.JME.smiles();
                     61:     if (smiles == "") {
                     62: 	alert("Nothing to submit");
                     63:     } else {
1.2       albertel   64:         $smilesection
                     65:         $jmesection
1.1       albertel   66: 	window.close();
                     67:     }
                     68: }
                     69: function openHelpWindow() {
1.2       albertel   70:     window.open("/adm/jme/jme_help.html","","scrollbars=yes,resizable=yes,width=500,height=600");
1.1       albertel   71: }
                     72: </script>
                     73: </head>
                     74: <body bgcolor="#ffffff">
                     75: <center>
                     76: <applet code="JME.class" name="JME" archive="/adm/jme/JME.jar" width="97%" height="78%">
                     77: You have to enable Java and JavaScript on your machine.
                     78: <param name="jme" value="$molecule" />
1.6     ! albertel   79: <param name="options" value="$options" />
1.1       albertel   80: </applet><br />
                     81: <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>
                     82: <form>
                     83: <input type="button" name="submit" value="Insert Answer" onClick = "submitSmiles();" />
                     84: <br />
                     85: <input type="button" value="  Close  " onClick = "window.close()" />
                     86: &nbsp;&nbsp;
                     87: <input type="button" value="  Help  " onClick = "openHelpWindow()" />
                     88: </form>
                     89: </center>
                     90: </body>
                     91: </html>
                     92: CHEMPAGE
                     93:     $body=&HTML::Entities::encode($body);
                     94:     $body=~s/\n/ /g;
                     95:     my $result=<<CHEMINPUT;
                     96: <input type="button" value="Draw Molecule" onClick="javascript:editor=window.open('','','width=500,height=500,scrollbars=no,resizable=yes');editor.document.open('text/html','replace');editor.document.writeln('$body')" />
                     97: CHEMINPUT
                     98:     return $result;
                     99: }
                    100: 
1.6     ! albertel  101: sub start_organicresponse {
1.1       albertel  102:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    103:     my $result;
                    104:     my $partid = $Apache::inputtags::part;
                    105:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    106:     if ($target eq 'meta') {
                    107:     } elsif ($target eq 'web') {
                    108: 	my $molecule;
                    109: 	if (defined($Apache::lonhomework::history{"resource.$partid.$id.molecule"})) {
                    110: 	    $molecule=$Apache::lonhomework::history{"resource.$partid.$id.molecule"};
                    111: 	} else {
                    112: 	    $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    113: 						 $safeeval);
                    114: 	}
1.6     ! albertel  115: 	my $multipart=&Apache::lonxml::get_param('multipart',$parstack,
        !           116: 						 $safeeval);
        !           117: 	if ($multipart eq 'yes') {
        !           118: 	    $multipart = 'multipart';
        !           119: 	} else {
        !           120: 	    $multipart ='';
        !           121:         }
        !           122: 	$result=&seperate_jme_window("HWVAL_$id","MOLECULE_$id",$molecule,$multipart);
1.1       albertel  123: 	$result.= '<input type="hidden" name="MOLECULE_'.$id.'" value="" />';
1.2       albertel  124:     } elsif ($target eq 'edit') {
                    125: 	$result .=&Apache::edit::tag_start($target,$token);
                    126: 	$result .=&Apache::edit::text_arg('Starting Molecule:','molecule',
                    127: 					  $token,40);
1.6     ! albertel  128: 	$result .=&Apache::edit::select_arg('Multipart:','multipart',
        !           129: 					    ['yes','no'],$token);
1.2       albertel  130: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    131: 						$safeeval);
                    132: 	$result .=&seperate_jme_window(undef,
                    133: 		      &Apache::edit::html_element_name('molecule'),
1.6     ! albertel  134: 		      $molecule,'multipart');
1.2       albertel  135: 	$result .='<br />';
                    136: 	$result .=&Apache::edit::text_arg('Correct Answer:','answer',
                    137: 					  $token,40);
                    138: 	$result .=&Apache::edit::hidden_arg('jmeanswer',$token);
                    139: 	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
                    140: 						 $safeeval);
                    141: 	$result .=&seperate_jme_window(
                    142:                       &Apache::edit::html_element_name('answer'),
                    143:                       &Apache::edit::html_element_name('jmeanswer'),
1.6     ! albertel  144: 		      $jmeanswer,'multipart');
1.2       albertel  145: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    146:     } elsif ($target eq 'modified') {
                    147: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    148: 						     $safeeval,'molecule',
1.6     ! albertel  149: 						     'answer','jmeanswer',
        !           150: 						     'multipart');
1.2       albertel  151: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.1       albertel  152:     }
                    153:     return $result;
                    154: }
                    155: 
1.6     ! albertel  156: sub end_organicresponse {
1.1       albertel  157:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    158:     my $result;
                    159:     if ($target eq 'grade' && defined($ENV{'form.submitted'})) {
                    160: 	&Apache::response::setup_params($$tagstack[-1]);
                    161: 	my $response = &Apache::response::getresponse();
                    162: 	if ( $response =~ /[^\s]/) {
                    163: 	    my $partid = $Apache::inputtags::part;
                    164: 	    my $id = $Apache::inputtags::response['-1'];
                    165: 	    my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    166: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    167: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    168: 	    &Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
                    169: 	    my $ad;
                    170: 	    if ($response eq $answer) {
                    171: 		$ad='EXACT_ANS';
                    172: 	    } else {
                    173: 		$ad='INCORRECT';
                    174: 	    }
                    175: 	    &Apache::response::handle_previous(\%previous,$ad);
                    176: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
                    177: 	    $Apache::lonhomework::results{"resource.$partid.$id.molecule"}=$ENV{"form.MOLECULE_$id"};
                    178: 	}
1.2       albertel  179:     } elsif ($target eq "edit") {
                    180: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.1       albertel  181:     }
                    182:     &Apache::response::end_response;
                    183:     return $result;
                    184: }
                    185: 
1.6     ! albertel  186: sub start_organicstructure {
1.3       albertel  187:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    188:     my $result;
                    189:     if ($target eq 'web') {
                    190: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
                    191: 	my $height=&Apache::lonxml::get_param('height',$parstack,$safeeval);
                    192: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
                    193: 	$result=<<CHEMOUTPUT;
                    194: <applet code="JME.class" archive="/adm/jme/JME.jar" width="$width" height="$height">
                    195: <param name="options" value="depict border" />
                    196: <param name="jme" value="$molecule" />
                    197: </applet>
                    198: CHEMOUTPUT
                    199:     } elsif ($target eq 'edit') {
                    200: 	$result .=&Apache::edit::tag_start($target,$token);
                    201: 	$result .=&Apache::edit::text_arg('Width:','width',$token,5);
                    202: 	$result .=&Apache::edit::text_arg('Height:','height',$token,5);
                    203: 	$result .=&Apache::edit::text_arg('Molecule:','molecule',$token,40);
                    204: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    205: 						$safeeval);
                    206: 	$result .=&seperate_jme_window(undef,
                    207: 		      &Apache::edit::html_element_name('molecule'),
1.6     ! albertel  208: 		      $molecule,'multipart');
1.3       albertel  209: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    210:     } elsif ($target eq 'modified') {
                    211: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    212: 						     $safeeval,'molecule',
                    213: 						     'width','height');
                    214: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    215:     }
                    216:     return $result;
1.1       albertel  217: }
                    218: 
1.6     ! albertel  219: sub end_organicstructure {
1.3       albertel  220:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    221:     my $result;
                    222:     if ($target eq "edit") {
                    223: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    224:     }
1.4       albertel  225:     return $result;
                    226: }
                    227: 
                    228: sub reaction_javascript {
1.5       albertel  229:     my $rightarrow;
                    230:     if ($ENV{'browser.unicode'}) {
                    231: 	$rightarrow=" &#8594; "
                    232:     } else {
                    233: 	$rightarrow=" <font face=symbol>&reg;</font> ";
                    234:     }
1.4       albertel  235:     my $result=<<REACTIONJAVASCRIPT;
1.5       albertel  236:     <script language="JavaScript">
1.4       albertel  237: 
                    238: var level;
                    239: var reactants;
                    240: var products;
                    241: 
                    242: 
                    243: function parse_reaction(string) {
                    244:   var reaction_array = string.split('->');
                    245:   var i;
                    246:   reactants = new Array(0);
                    247:   products = new Array(0);
                    248: 
                    249:   if (reaction_array.length > 0)
                    250:     reactants = reaction_array[0].split(' +');
                    251:   if (reaction_array.length > 1)
                    252:     products = reaction_array[1].split(' +');
                    253: }
                    254: 
                    255: function to_capa(string) {
                    256:   var reaction = "";
                    257:   var i;
                    258: 
                    259:   parse_reaction(string);
                    260: 
                    261:   for (i = 0; i < reactants.length; i++)
                    262:     reactants[i] = capa_component(reactants[i]);
                    263:   for (i = 0; i < products.length; i++)
                    264:     products[i] = capa_component(products[i]);
                    265: 
                    266:   reactants.sort();
                    267:   products.sort();
                    268: 
                    269:   for (i = 0; i < reactants.length-1; i++) {
                    270:     reaction += reactants[i];
                    271:     reaction += " + ";
                    272:   }
                    273:   if (i < reactants.length)
                    274:     reaction += reactants[i];
                    275:   if (products.length > 0) {
                    276:     reaction += " -> ";
                    277:     for (i = 0; i < products.length-1; i++) {
                    278:       reaction += products[i];
                    279:       reaction += " + ";
                    280:     }
                    281:     if (i < products.length)
                    282:       reaction += products[i];
                    283:   }
                    284: 
                    285:   return reaction;
                    286: }
                    287: 
                    288: function capa_component(string) {
                    289:   var reactant = "";
                    290:   var i = 0;
                    291:   level = 0;
                    292: 
                    293:   for (;string.substring(i,i+1) == ' ';i++)
                    294:     ;
                    295:   for (;isDigit(string.substring(i,i+1));i++)
                    296:     reactant += string.substring(i,i+1);
                    297:   for (;i < string.length;i++)
                    298:     reactant +=  capa_char(string.substring(i,i+1));
                    299: 
                    300:   return reactant;
                    301: }
                    302: 
                    303: function capa_char(chr) {
                    304:   if (level == 0) { // baseline
                    305:     if (chr == '^')
                    306:       level = 1;
                    307:     if (chr == ' ')
                    308:       return "";
                    309:     return chr;
                    310:   }
                    311:   if (level == 1) { // superscript
                    312:     if (isDigit(chr))
                    313:       return chr;
                    314:     level = 0;
                    315:     return chr;
                    316:   }
                    317: }
                    318: 
                    319: function to_html(string) {
                    320:   var reaction = "";
                    321:   var i;
                    322: 
                    323:   parse_reaction(string);
                    324:   for (i = 0; i < reactants.length-1; i++) {
                    325:     reaction += html_component(reactants[i]);
                    326:     reaction += " + ";
                    327:   }
                    328:   if (i < reactants.length)
                    329:     reaction += html_component(reactants[i]);
                    330: 
                    331:   if (products.length > 0) {
1.5       albertel  332:     reaction += " $rightarrow ";
1.4       albertel  333:     for (i = 0; i < products.length-1; i++) {
                    334:       reaction += html_component(products[i]);
                    335:       reaction += " + ";
                    336:     }
                    337:     if (i < products.length)
                    338:       reaction += html_component(products[i]);
                    339:   }
                    340: 
                    341:   return reaction;
                    342: }
                    343: 
                    344: function html_component(string) {
                    345:   var reactant = "";
                    346:   var i = 0;
                    347:   level = 0;
                    348: 
                    349:   for (;string.substring(i,i+1) == ' ';i++)
                    350:     ;
                    351:   for (;isDigit(string.substring(i,i+1));i++)
                    352:     reactant += string.substring(i,i+1);
                    353:   for (;i < string.length;i++)
                    354:     reactant +=  html_char(string.substring(i,i+1));
                    355: 
                    356:   return reactant;
                    357: }
                    358: 
                    359: function html_char(chr) {
                    360:   if (level == 0) { // baseline
                    361:     if (isDigit(chr))
                    362:       return chr.sub();
                    363:     if (chr == '^') {
                    364:       level = 1;
                    365:       return "";
                    366:     }
                    367:     if (chr == '+') // baseline or superscript
                    368:       return "?";
                    369:     if (chr == ' ')
                    370:       return "";
                    371:     return chr;
                    372:   }
                    373:   if (level == 1) { // superscript
                    374:     if (isDigit(chr))
                    375:       return chr.sup();
                    376:     if (chr == '+' || chr == '-') {
                    377:       level = 0;
                    378:       return chr.sup();
                    379:     }
                    380:     if (chr == ' ') {
                    381:       level = 0;
                    382:       return "";
                    383:     }
                    384:     level = 0;
                    385:     return chr;
                    386:   }
                    387: }
                    388: 
                    389: function isDigit(string) {
                    390:   if (string >= '0' && string <='9')
                    391:     return 1;
                    392:   else
                    393:     return 0;
                    394: }
                    395: 
                    396: function openHelpWindow() {
                    397:   window.open("reaction_help.html","","scrollbars=yes,resizable=yes,width=550,height=600")
                    398: }
                    399: 
                    400: function submitReaction() {
                    401:   reaction = to_capa(document.form.text.value);
                    402:   if (reaction == "") {
                    403:     alert("Nothing to submit");
                    404:   }
                    405:   else {
                    406:     name = "INPUT" + ((problem < 10) ? "0" : "") + problem;
                    407:     i = 0;
                    408:     while (parent.opener.document.CAPA.elements[i].name != name)
                    409:       i++;
                    410:     parent.opener.document.CAPA.elements[i].value = reaction;
                    411:     parent.opener.document.CAPA.submit();
                    412:   }
                    413: }
1.5       albertel  414: </script>
1.4       albertel  415: REACTIONJAVASCRIPT
                    416:     return $result;
                    417: }
                    418: 
                    419: sub start_reactionresponse {
                    420:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    421:     my $result;
                    422:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    423:     if ($target eq 'web') {
1.6     ! albertel  424: 	$result.=<<EDITREACTION;
        !           425: <input type='button' value='Edit Reaction' onClick="javascript:editor=window.open('/adm/jme/reaction_window.html','','width=500,height=270,scrollbars=no,resizable=yes'); document.cookie='problem=$id';"/>'
        !           426: EDITREACTION
        !           427: 
        !           428: #	$result.=&reaction_javascript();
1.4       albertel  429: #	$result.='<iframe name="REACTION_'.$id.'" width="200" height="100" src="/adm/jme/reaction_viewer.html"></iframe>';
1.6     ! albertel  430: #	$result.='<input type="button" value="Check" onClick = "javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200,scrollbars=1\');newWindow.document.open(\'text/html\',\'replace\');newWindow.document.writeln(\'<center><br />\'+to_html(document.lonhomework.HWVAL_'.$id.'.value)+\'</center><input type=&quot;button&quot; value=&quot;  Close  &quot; onClick = &quot;parent.window.close()&quot; />\');newWindow.document.close()" />'
1.4       albertel  431:     } elsif ($target eq "edit") {
                    432:     }
                    433:     return $result;
                    434: }
                    435: 
                    436: sub end_reactionresponse {
                    437:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    438:     my $result;
                    439:     if ($target eq "edit") {
                    440: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    441:     }
                    442:     &Apache::response::end_response;
1.3       albertel  443:     return $result;
1.1       albertel  444: }
                    445: 
                    446: 1;
                    447: __END__

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