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

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

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