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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # chemical equation style response
                      3: #
1.18    ! albertel    4: # $Id: chemresponse.pm,v 1.17 2003/10/16 21:52:27 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.14      albertel   54:     if ($molecule) { $molecule='<param name="jme" value="'.$molecule.'" />'; }
1.1       albertel   55:     my $body=<<CHEMPAGE;
                     56: <html>
                     57: <head>
                     58: <title>Molecule Editor</title>
                     59: <script language="JavaScript">
                     60: function submitSmiles() {
                     61:     smiles = document.applets.JME.smiles();
                     62:     if (smiles == "") {
                     63: 	alert("Nothing to submit");
                     64:     } else {
1.2       albertel   65:         $smilesection
                     66:         $jmesection
1.1       albertel   67: 	window.close();
                     68:     }
                     69: }
                     70: function openHelpWindow() {
1.2       albertel   71:     window.open("/adm/jme/jme_help.html","","scrollbars=yes,resizable=yes,width=500,height=600");
1.1       albertel   72: }
                     73: </script>
                     74: </head>
                     75: <body bgcolor="#ffffff">
                     76: <center>
                     77: <applet code="JME.class" name="JME" archive="/adm/jme/JME.jar" width="97%" height="78%">
                     78: You have to enable Java and JavaScript on your machine.
1.12      albertel   79: $molecule
1.6       albertel   80: <param name="options" value="$options" />
1.1       albertel   81: </applet><br />
                     82: <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>
                     83: <form>
                     84: <input type="button" name="submit" value="Insert Answer" onClick = "submitSmiles();" />
                     85: <br />
                     86: <input type="button" value="  Close  " onClick = "window.close()" />
                     87: &nbsp;&nbsp;
                     88: <input type="button" value="  Help  " onClick = "openHelpWindow()" />
                     89: </form>
                     90: </center>
                     91: </body>
                     92: </html>
                     93: CHEMPAGE
                     94:     $body=&HTML::Entities::encode($body);
                     95:     $body=~s/\n/ /g;
                     96:     my $result=<<CHEMINPUT;
                     97: <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')" />
                     98: CHEMINPUT
                     99:     return $result;
                    100: }
                    101: 
1.6       albertel  102: sub start_organicresponse {
1.1       albertel  103:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    104:     my $result;
                    105:     my $partid = $Apache::inputtags::part;
                    106:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    107:     if ($target eq 'meta') {
                    108:     } elsif ($target eq 'web') {
                    109: 	my $molecule;
                    110: 	if (defined($Apache::lonhomework::history{"resource.$partid.$id.molecule"})) {
                    111: 	    $molecule=$Apache::lonhomework::history{"resource.$partid.$id.molecule"};
                    112: 	} else {
                    113: 	    $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    114: 						 $safeeval);
                    115: 	}
1.15      albertel  116: 	my $options=&Apache::lonxml::get_param('options',$parstack,
                    117: 					       $safeeval);
                    118: 	$result=&seperate_jme_window("HWVAL_$id","MOLECULE_$id",$molecule,$options);
1.1       albertel  119: 	$result.= '<input type="hidden" name="MOLECULE_'.$id.'" value="" />';
1.2       albertel  120:     } elsif ($target eq 'edit') {
                    121: 	$result .=&Apache::edit::tag_start($target,$token);
1.12      albertel  122: 	my $options=&Apache::lonxml::get_param('options',$parstack,
                    123: 					       $safeeval);
                    124: 	if ($options !~ /multipart/) { $options.=',multipart'; }
1.7       albertel  125: 	$result .='<nobr>'.
                    126: 	    &Apache::edit::text_arg('Starting Molecule:','molecule',
                    127: 				    $token,40);
1.2       albertel  128: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    129: 						$safeeval);
                    130: 	$result .=&seperate_jme_window(undef,
                    131: 		      &Apache::edit::html_element_name('molecule'),
1.12      albertel  132: 		      $molecule,$options);
1.7       albertel  133: 	$result .='</nobr><br /><nobr>';
1.2       albertel  134: 	$result .=&Apache::edit::text_arg('Correct Answer:','answer',
                    135: 					  $token,40);
                    136: 	$result .=&Apache::edit::hidden_arg('jmeanswer',$token);
                    137: 	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
                    138: 						 $safeeval);
                    139: 	$result .=&seperate_jme_window(
                    140:                       &Apache::edit::html_element_name('answer'),
                    141:                       &Apache::edit::html_element_name('jmeanswer'),
1.12      albertel  142: 		      $jmeanswer,$options);
                    143: 	$result .='</nobr><br />';
                    144: 	$result .=&Apache::edit::checked_arg('Options:','options',
                    145: 				    [ ['autoez','Auto E,Z sterochemistry'],
1.18    ! albertel  146: 				      ['multipart','Multipart Structures'],
1.12      albertel  147: 				      ['nostereo','No stereochemistry'],
                    148: 				      ['reaction','Is a reaction'],
1.18    ! albertel  149: 				      ['number','Able to number atoms'] ],
1.12      albertel  150: 					     ,$token);
1.2       albertel  151: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    152:     } elsif ($target eq 'modified') {
                    153: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    154: 						     $safeeval,'molecule',
1.6       albertel  155: 						     'answer','jmeanswer',
1.12      albertel  156: 						     'options');
1.2       albertel  157: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.1       albertel  158:     }
                    159:     return $result;
                    160: }
                    161: 
1.6       albertel  162: sub end_organicresponse {
1.1       albertel  163:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    164:     my $result;
                    165:     if ($target eq 'grade' && defined($ENV{'form.submitted'})) {
                    166: 	&Apache::response::setup_params($$tagstack[-1]);
                    167: 	my $response = &Apache::response::getresponse();
                    168: 	if ( $response =~ /[^\s]/) {
                    169: 	    my $partid = $Apache::inputtags::part;
                    170: 	    my $id = $Apache::inputtags::response['-1'];
1.13      albertel  171: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.1       albertel  172: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    173: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    174: 	    my $ad;
1.13      albertel  175: 	    foreach my $answer (@answers) {
                    176: 		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
                    177: 		if ($response eq $answer) {
                    178: 		    $ad='EXACT_ANS';
                    179: 		    last;
                    180: 		} else {
                    181: 		    $ad='INCORRECT';
                    182: 		}
1.1       albertel  183: 	    }
                    184: 	    &Apache::response::handle_previous(\%previous,$ad);
                    185: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
                    186: 	    $Apache::lonhomework::results{"resource.$partid.$id.molecule"}=$ENV{"form.MOLECULE_$id"};
                    187: 	}
1.2       albertel  188:     } elsif ($target eq "edit") {
                    189: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.15      albertel  190:     } elsif ($target eq 'answer') {
                    191: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
                    192: 						     $safeeval);
                    193: 	$result.=&Apache::response::answer_header('organicresponse');
                    194: 	foreach my $answer (@answers) {
                    195: 	    $result.=&Apache::response::answer_part('organicresponse',$answer);
                    196: 	}
                    197: 	$result.=&Apache::response::answer_footer('organicresponse');
1.1       albertel  198:     }
                    199:     &Apache::response::end_response;
                    200:     return $result;
                    201: }
                    202: 
1.6       albertel  203: sub start_organicstructure {
1.3       albertel  204:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    205:     my $result;
                    206:     if ($target eq 'web') {
                    207: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
                    208: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
1.12      albertel  209: 	my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.16      albertel  210: 	my $id=time.'_'.int(rand(1000));
                    211: 	$result="<img src='/cgi-bin/convertjme.pl?$id' />";
                    212: 	&Apache::lonnet::appenv(
                    213:             'cgi.'.$id.'.JME'   => &Apache::lonnet::escape($molecule),
1.17      albertel  214: 	    'cgi.'.$id.'.PNG' => 1,
1.16      albertel  215: 	    'cgi.'.$id.'.WIDTH' => $width );
1.17      albertel  216:     } elsif ($target eq 'tex') {
                    217: 	my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,1);
                    218: 	if (!$texwidth) { $texwidth='90'; }
                    219: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
                    220: 	my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
                    221: 	my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
                    222: 	    '_'.time.'_'.$$.int(rand(1000)).'_organicstructure';
                    223: 	my $id=$filename;
                    224: 	&Apache::lonnet::appenv(
                    225: 		     'cgi.'.$id.'.JME'   => &Apache::lonnet::escape($molecule),
                    226: 		     'cgi.'.$id.'.PS' => 1,
                    227: 		     'cgi.'.$id.'.WIDTH' => $texwidth );
                    228: 	$id=&Apache::lonnet::escape($id);
                    229: 	&Apache::lonxml::register_ssi("/cgi-bin/convertjme.pl?$id");
                    230: 	$result = '\graphicspath{{/home/httpd/perl/tmp/}}\includegraphics[width='.$texwidth.' mm]{'.$filename.'.eps}';
1.3       albertel  231:     } elsif ($target eq 'edit') {
                    232: 	$result .=&Apache::edit::tag_start($target,$token);
                    233: 	$result .=&Apache::edit::text_arg('Width:','width',$token,5);
                    234: 	$result .=&Apache::edit::text_arg('Height:','height',$token,5);
1.17      albertel  235: 	$result .=&Apache::edit::text_arg('TeXwidth:','texwidth',$token,5);
1.12      albertel  236: 	$result .='<nobr>';
1.3       albertel  237: 	$result .=&Apache::edit::text_arg('Molecule:','molecule',$token,40);
                    238: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    239: 						$safeeval);
1.12      albertel  240: 	my $options=&Apache::lonxml::get_param('options',$parstack,
                    241: 					       $safeeval);
                    242: 	if ($options !~ /reaction/) {
                    243: 	    $options.= ',multipart,number';
                    244: 	}
                    245: 						   
1.3       albertel  246: 	$result .=&seperate_jme_window(undef,
1.12      albertel  247: 				 &Apache::edit::html_element_name('molecule'),
                    248: 				       $molecule,$options);
                    249: 	$result.="</nobr><br />";
                    250: 	$result .=&Apache::edit::checked_arg('Options:','options',
1.18    ! albertel  251: 					     [ ['reaction','Is a reaction'],
1.12      albertel  252: 					       ['border','Draw a border'] ],
                    253: 					     $token);
1.3       albertel  254: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    255:     } elsif ($target eq 'modified') {
                    256: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    257: 						     $safeeval,'molecule',
1.12      albertel  258: 						     'width','height',
1.17      albertel  259: 						     'texwidth','options');
1.3       albertel  260: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    261:     }
                    262:     return $result;
1.1       albertel  263: }
                    264: 
1.6       albertel  265: sub end_organicstructure {
1.3       albertel  266:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    267:     my $result;
                    268:     if ($target eq "edit") {
                    269: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    270:     }
1.4       albertel  271:     return $result;
                    272: }
                    273: 
1.9       albertel  274: sub edit_reaction_button {
1.11      albertel  275:     my ($id,$field,$reaction)=@_;
1.10      albertel  276:     my $id_es=&Apache::lonnet::escape($id);
                    277:     my $field_es=&Apache::lonnet::escape($field);
1.11      albertel  278:     my $reaction_es=&Apache::lonnet::escape($reaction);
1.9       albertel  279:     my $result=<<EDITREACTION;
1.10      albertel  280: <script type="text/javascript">
                    281:     function create_reaction_window_${id}_${field} () {
                    282: 	editor=window.open('','','width=500,height=270,scrollbars=no,resizable=yes');
                    283: 	editor.document.open('text/html','replace');
1.11      albertel  284: 	editor.document.writeln('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><html> <head><title>LON-CAPA Reaction Editor</title></head><frameset rows="30%,*" border="0">  <frame src="/res/adm/pages/reactionresponse/reaction_viewer.html" name="viewer" scrolling="no" />  <frame src="/res/adm/pages/reactionresponse/reaction_editor.html?reaction=$reaction_es&id=$id_es&field=$field_es" name="editor" scrolling="no" /> </frameset> </html>');
1.10      albertel  285:     }
                    286: </script>
                    287: <input type='button' value='Edit Reaction' onClick="javascript:create_reaction_window_${id}_${field}();void(0);" />
1.9       albertel  288: EDITREACTION
                    289:     return $result;
                    290: }
                    291: 
1.4       albertel  292: sub start_reactionresponse {
                    293:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    294:     my $result;
                    295:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.10      albertel  296:     if ($target eq 'meta') {
                    297:     } elsif ($target eq 'web') {
1.11      albertel  298: 	my $partid = $Apache::inputtags::part;
                    299: 	my $id = $Apache::inputtags::response['-1'];
                    300: 	my $reaction=$Apache::lonhomework::history{"resource.$partid.$id.submission"};
                    301: 	$result.=&edit_reaction_button($id,"HWVAL_$id",$reaction);
1.4       albertel  302:     } elsif ($target eq "edit") {
1.9       albertel  303: 	$result .=&Apache::edit::tag_start($target,$token);
                    304: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,
                    305: 						$safeeval);
1.10      albertel  306: 	$result .='<nobr>'.
                    307: 	    &Apache::edit::text_arg('Answer:','answer',$token,40);
                    308: 	$result .=&edit_reaction_button($id,&Apache::edit::html_element_name('answer'),$answer).'</nobr>';
                    309: 	
1.9       albertel  310: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.10      albertel  311:     }  elsif ($target eq 'modified') {
                    312: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    313: 						     $safeeval,'answer');
                    314: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.4       albertel  315:     }
                    316:     return $result;
                    317: }
                    318: 
                    319: sub end_reactionresponse {
                    320:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    321:     my $result;
1.10      albertel  322:     if ($target eq 'grade' && defined($ENV{'form.submitted'})) {
                    323: 	&Apache::response::setup_params($$tagstack[-1]);
                    324: 	my $response = &Apache::response::getresponse();
                    325: 	if ( $response =~ /[^\s]/) {
                    326: 	    my $partid = $Apache::inputtags::part;
                    327: 	    my $id = $Apache::inputtags::response['-1'];
1.15      albertel  328: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.10      albertel  329: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    330: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    331: 	    my $ad;
1.13      albertel  332: 	    foreach my $answer (@answers) {
                    333: 		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
                    334: 		if ($response eq $answer) {
                    335: 		    $ad='EXACT_ANS';
                    336: 		} else {
                    337: 		    $ad='INCORRECT';
                    338: 		}
1.10      albertel  339: 	    }
                    340: 	    &Apache::response::handle_previous(\%previous,$ad);
                    341: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
                    342: 	}
                    343:     }  elsif ($target eq "edit") {
1.4       albertel  344: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.15      albertel  345:     } elsif ($target eq 'answer') {
                    346: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
                    347: 						     $safeeval);
                    348: 	$result.=&Apache::response::answer_header('reactionresponse');
                    349: 	foreach my $answer (@answers) {
                    350: 	    $result.=&Apache::response::answer_part('reactionresponse',
                    351: 						    $answer);
                    352: 	}
                    353: 	$result.=&Apache::response::answer_footer('reactionresponse');
1.4       albertel  354:     }
                    355:     &Apache::response::end_response;
1.3       albertel  356:     return $result;
1.1       albertel  357: }
                    358: 
                    359: 1;
                    360: __END__

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