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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # chemical equation style response
                      3: #
1.13    ! albertel    4: # $Id: chemresponse.pm,v 1.12 2003/09/08 22:08:37 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.12      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.6       albertel  116: 	my $multipart=&Apache::lonxml::get_param('multipart',$parstack,
                    117: 						 $safeeval);
                    118: 	if ($multipart eq 'yes') {
                    119: 	    $multipart = 'multipart';
                    120: 	} else {
                    121: 	    $multipart ='';
                    122:         }
                    123: 	$result=&seperate_jme_window("HWVAL_$id","MOLECULE_$id",$molecule,$multipart);
1.1       albertel  124: 	$result.= '<input type="hidden" name="MOLECULE_'.$id.'" value="" />';
1.2       albertel  125:     } elsif ($target eq 'edit') {
                    126: 	$result .=&Apache::edit::tag_start($target,$token);
1.12      albertel  127: 	my $options=&Apache::lonxml::get_param('options',$parstack,
                    128: 					       $safeeval);
                    129: 	if ($options !~ /multipart/) { $options.=',multipart'; }
1.7       albertel  130: 	$result .='<nobr>'.
                    131: 	    &Apache::edit::text_arg('Starting Molecule:','molecule',
                    132: 				    $token,40);
1.2       albertel  133: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    134: 						$safeeval);
                    135: 	$result .=&seperate_jme_window(undef,
                    136: 		      &Apache::edit::html_element_name('molecule'),
1.12      albertel  137: 		      $molecule,$options);
1.7       albertel  138: 	$result .='</nobr><br /><nobr>';
1.2       albertel  139: 	$result .=&Apache::edit::text_arg('Correct Answer:','answer',
                    140: 					  $token,40);
                    141: 	$result .=&Apache::edit::hidden_arg('jmeanswer',$token);
                    142: 	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
                    143: 						 $safeeval);
                    144: 	$result .=&seperate_jme_window(
                    145:                       &Apache::edit::html_element_name('answer'),
                    146:                       &Apache::edit::html_element_name('jmeanswer'),
1.12      albertel  147: 		      $jmeanswer,$options);
                    148: 	$result .='</nobr><br />';
                    149: 	$result .=&Apache::edit::checked_arg('Options:','options',
                    150: 				    [ ['autoez','Auto E,Z sterochemistry'],
                    151: 				      ['multipart','MultiPart Structures'],
                    152: 				      ['hydrogens','Show Hydrogens'],
                    153: 				      ['nostereo','No stereochemistry'],
                    154: 				      ['reaction','Is a reaction'],
                    155: 				      ['number','Able to number atoms'],
                    156: 				      ['border','Draw a border'] ],
                    157: 					     ,$token);
1.2       albertel  158: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    159:     } elsif ($target eq 'modified') {
                    160: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    161: 						     $safeeval,'molecule',
1.6       albertel  162: 						     'answer','jmeanswer',
1.12      albertel  163: 						     'options');
1.2       albertel  164: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.1       albertel  165:     }
                    166:     return $result;
                    167: }
                    168: 
1.6       albertel  169: sub end_organicresponse {
1.1       albertel  170:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    171:     my $result;
                    172:     if ($target eq 'grade' && defined($ENV{'form.submitted'})) {
                    173: 	&Apache::response::setup_params($$tagstack[-1]);
                    174: 	my $response = &Apache::response::getresponse();
                    175: 	if ( $response =~ /[^\s]/) {
                    176: 	    my $partid = $Apache::inputtags::part;
                    177: 	    my $id = $Apache::inputtags::response['-1'];
1.13    ! albertel  178: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.1       albertel  179: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    180: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    181: 	    my $ad;
1.13    ! albertel  182: 	    foreach my $answer (@answers) {
        !           183: 		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
        !           184: 		if ($response eq $answer) {
        !           185: 		    $ad='EXACT_ANS';
        !           186: 		    last;
        !           187: 		} else {
        !           188: 		    $ad='INCORRECT';
        !           189: 		}
1.1       albertel  190: 	    }
                    191: 	    &Apache::response::handle_previous(\%previous,$ad);
                    192: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
                    193: 	    $Apache::lonhomework::results{"resource.$partid.$id.molecule"}=$ENV{"form.MOLECULE_$id"};
                    194: 	}
1.2       albertel  195:     } elsif ($target eq "edit") {
                    196: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.1       albertel  197:     }
                    198:     &Apache::response::end_response;
                    199:     return $result;
                    200: }
                    201: 
1.6       albertel  202: sub start_organicstructure {
1.3       albertel  203:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    204:     my $result;
                    205:     if ($target eq 'web') {
                    206: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
                    207: 	my $height=&Apache::lonxml::get_param('height',$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.3       albertel  210: 	$result=<<CHEMOUTPUT;
                    211: <applet code="JME.class" archive="/adm/jme/JME.jar" width="$width" height="$height">
1.12      albertel  212: <param name="options" value="depict,$options" />
1.3       albertel  213: <param name="jme" value="$molecule" />
                    214: </applet>
                    215: CHEMOUTPUT
                    216:     } elsif ($target eq 'edit') {
                    217: 	$result .=&Apache::edit::tag_start($target,$token);
                    218: 	$result .=&Apache::edit::text_arg('Width:','width',$token,5);
                    219: 	$result .=&Apache::edit::text_arg('Height:','height',$token,5);
1.12      albertel  220: 	$result .='<nobr>';
1.3       albertel  221: 	$result .=&Apache::edit::text_arg('Molecule:','molecule',$token,40);
                    222: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    223: 						$safeeval);
1.12      albertel  224: 	my $options=&Apache::lonxml::get_param('options',$parstack,
                    225: 					       $safeeval);
                    226: 	if ($options !~ /reaction/) {
                    227: 	    $options.= ',multipart,number';
                    228: 	}
                    229: 						   
1.3       albertel  230: 	$result .=&seperate_jme_window(undef,
1.12      albertel  231: 				 &Apache::edit::html_element_name('molecule'),
                    232: 				       $molecule,$options);
                    233: 	$result.="</nobr><br />";
                    234: 	$result .=&Apache::edit::checked_arg('Options:','options',
                    235: 					     [ ['hydrogens','Show Hydrogens'],
                    236: 					       ['reaction','Is a reaction'],
                    237: 					       ['border','Draw a border'] ],
                    238: 					     $token);
1.3       albertel  239: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    240:     } elsif ($target eq 'modified') {
                    241: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    242: 						     $safeeval,'molecule',
1.12      albertel  243: 						     'width','height',
                    244: 						     'options');
1.3       albertel  245: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    246:     }
                    247:     return $result;
1.1       albertel  248: }
                    249: 
1.6       albertel  250: sub end_organicstructure {
1.3       albertel  251:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    252:     my $result;
                    253:     if ($target eq "edit") {
                    254: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    255:     }
1.4       albertel  256:     return $result;
                    257: }
                    258: 
1.9       albertel  259: sub edit_reaction_button {
1.11      albertel  260:     my ($id,$field,$reaction)=@_;
1.10      albertel  261:     my $id_es=&Apache::lonnet::escape($id);
                    262:     my $field_es=&Apache::lonnet::escape($field);
1.11      albertel  263:     my $reaction_es=&Apache::lonnet::escape($reaction);
1.9       albertel  264:     my $result=<<EDITREACTION;
1.10      albertel  265: <script type="text/javascript">
                    266:     function create_reaction_window_${id}_${field} () {
                    267: 	editor=window.open('','','width=500,height=270,scrollbars=no,resizable=yes');
                    268: 	editor.document.open('text/html','replace');
1.11      albertel  269: 	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  270:     }
                    271: </script>
                    272: <input type='button' value='Edit Reaction' onClick="javascript:create_reaction_window_${id}_${field}();void(0);" />
1.9       albertel  273: EDITREACTION
                    274:     return $result;
                    275: }
                    276: 
1.4       albertel  277: sub start_reactionresponse {
                    278:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    279:     my $result;
                    280:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.10      albertel  281:     if ($target eq 'meta') {
                    282:     } elsif ($target eq 'web') {
1.11      albertel  283: 	my $partid = $Apache::inputtags::part;
                    284: 	my $id = $Apache::inputtags::response['-1'];
                    285: 	my $reaction=$Apache::lonhomework::history{"resource.$partid.$id.submission"};
                    286: 	$result.=&edit_reaction_button($id,"HWVAL_$id",$reaction);
1.4       albertel  287:     } elsif ($target eq "edit") {
1.9       albertel  288: 	$result .=&Apache::edit::tag_start($target,$token);
                    289: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,
                    290: 						$safeeval);
1.10      albertel  291: 	$result .='<nobr>'.
                    292: 	    &Apache::edit::text_arg('Answer:','answer',$token,40);
                    293: 	$result .=&edit_reaction_button($id,&Apache::edit::html_element_name('answer'),$answer).'</nobr>';
                    294: 	
1.9       albertel  295: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.10      albertel  296:     }  elsif ($target eq 'modified') {
                    297: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    298: 						     $safeeval,'answer');
                    299: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.4       albertel  300:     }
                    301:     return $result;
                    302: }
                    303: 
                    304: sub end_reactionresponse {
                    305:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    306:     my $result;
1.10      albertel  307:     if ($target eq 'grade' && defined($ENV{'form.submitted'})) {
                    308: 	&Apache::response::setup_params($$tagstack[-1]);
                    309: 	my $response = &Apache::response::getresponse();
                    310: 	if ( $response =~ /[^\s]/) {
                    311: 	    my $partid = $Apache::inputtags::part;
                    312: 	    my $id = $Apache::inputtags::response['-1'];
1.13    ! albertel  313: 	    my (@answers)=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
1.10      albertel  314: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    315: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    316: 	    my $ad;
1.13    ! albertel  317: 	    foreach my $answer (@answers) {
        !           318: 		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
        !           319: 		if ($response eq $answer) {
        !           320: 		    $ad='EXACT_ANS';
        !           321: 		} else {
        !           322: 		    $ad='INCORRECT';
        !           323: 		}
1.10      albertel  324: 	    }
                    325: 	    &Apache::response::handle_previous(\%previous,$ad);
                    326: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
                    327: 	}
                    328:     }  elsif ($target eq "edit") {
1.4       albertel  329: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    330:     }
                    331:     &Apache::response::end_response;
1.3       albertel  332:     return $result;
1.1       albertel  333: }
                    334: 
                    335: 1;
                    336: __END__

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