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

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

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