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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # chemical equation style response
                      3: #
1.11    ! albertel    4: # $Id: chemresponse.pm,v 1.10 2003/07/01 21:04:48 albertel Exp $
1.1       albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: #
                     29: package Apache::chemresponse;
                     30: use strict;
                     31: use Apache::lonxml;
                     32: use Apache::lonnet;
                     33: 
                     34: BEGIN {
1.6       albertel   35:     &Apache::lonxml::register('Apache::chemresponse',('organicresponse','organicstructure','reactionresponse'));
1.1       albertel   36: }
                     37: 
                     38: sub seperate_jme_window {
1.6       albertel   39:     my ($smile_input,$jme_input,$molecule,$options)=@_;
1.2       albertel   40:     my $smilesection;
                     41:     if (defined($smile_input)) {
                     42: 	$smilesection=<<SMILESECTION;
                     43: 	opener.document.lonhomework.$smile_input.value = smiles;
                     44: SMILESECTION
                     45:     }
                     46:     my $jmesection;
                     47:     if (defined($jme_input)) {
                     48: 	$jmesection=<<JMESECTION;
                     49: 	jmeFile = document.applets.JME.jmeFile();
                     50: 	opener.document.lonhomework.$jme_input.value = jmeFile;
                     51: JMESECTION
                     52:     }
                     53: 
1.1       albertel   54:     my $body=<<CHEMPAGE;
                     55: <html>
                     56: <head>
                     57: <title>Molecule Editor</title>
                     58: <script language="JavaScript">
                     59: function submitSmiles() {
                     60:     smiles = document.applets.JME.smiles();
                     61:     if (smiles == "") {
                     62: 	alert("Nothing to submit");
                     63:     } else {
1.2       albertel   64:         $smilesection
                     65:         $jmesection
1.1       albertel   66: 	window.close();
                     67:     }
                     68: }
                     69: function openHelpWindow() {
1.2       albertel   70:     window.open("/adm/jme/jme_help.html","","scrollbars=yes,resizable=yes,width=500,height=600");
1.1       albertel   71: }
                     72: </script>
                     73: </head>
                     74: <body bgcolor="#ffffff">
                     75: <center>
                     76: <applet code="JME.class" name="JME" archive="/adm/jme/JME.jar" width="97%" height="78%">
                     77: You have to enable Java and JavaScript on your machine.
                     78: <param name="jme" value="$molecule" />
1.6       albertel   79: <param name="options" value="$options" />
1.1       albertel   80: </applet><br />
                     81: <font face="arial,helvetica,sans-serif" size=-1><a href="http://www.molinspiration.com/jme/index.html">JME Editor</a> courtesy of Peter Ertl, Novartis</font>
                     82: <form>
                     83: <input type="button" name="submit" value="Insert Answer" onClick = "submitSmiles();" />
                     84: <br />
                     85: <input type="button" value="  Close  " onClick = "window.close()" />
                     86: &nbsp;&nbsp;
                     87: <input type="button" value="  Help  " onClick = "openHelpWindow()" />
                     88: </form>
                     89: </center>
                     90: </body>
                     91: </html>
                     92: CHEMPAGE
                     93:     $body=&HTML::Entities::encode($body);
                     94:     $body=~s/\n/ /g;
                     95:     my $result=<<CHEMINPUT;
                     96: <input type="button" value="Draw Molecule" onClick="javascript:editor=window.open('','','width=500,height=500,scrollbars=no,resizable=yes');editor.document.open('text/html','replace');editor.document.writeln('$body')" />
                     97: CHEMINPUT
                     98:     return $result;
                     99: }
                    100: 
1.6       albertel  101: sub start_organicresponse {
1.1       albertel  102:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    103:     my $result;
                    104:     my $partid = $Apache::inputtags::part;
                    105:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    106:     if ($target eq 'meta') {
                    107:     } elsif ($target eq 'web') {
                    108: 	my $molecule;
                    109: 	if (defined($Apache::lonhomework::history{"resource.$partid.$id.molecule"})) {
                    110: 	    $molecule=$Apache::lonhomework::history{"resource.$partid.$id.molecule"};
                    111: 	} else {
                    112: 	    $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    113: 						 $safeeval);
                    114: 	}
1.6       albertel  115: 	my $multipart=&Apache::lonxml::get_param('multipart',$parstack,
                    116: 						 $safeeval);
                    117: 	if ($multipart eq 'yes') {
                    118: 	    $multipart = 'multipart';
                    119: 	} else {
                    120: 	    $multipart ='';
                    121:         }
                    122: 	$result=&seperate_jme_window("HWVAL_$id","MOLECULE_$id",$molecule,$multipart);
1.1       albertel  123: 	$result.= '<input type="hidden" name="MOLECULE_'.$id.'" value="" />';
1.2       albertel  124:     } elsif ($target eq 'edit') {
                    125: 	$result .=&Apache::edit::tag_start($target,$token);
1.7       albertel  126: 	$result .='<nobr>'.
                    127: 	    &Apache::edit::text_arg('Starting Molecule:','molecule',
                    128: 				    $token,40);
1.2       albertel  129: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    130: 						$safeeval);
                    131: 	$result .=&seperate_jme_window(undef,
                    132: 		      &Apache::edit::html_element_name('molecule'),
1.6       albertel  133: 		      $molecule,'multipart');
1.7       albertel  134: 	$result .='</nobr><br /><nobr>';
1.2       albertel  135: 	$result .=&Apache::edit::text_arg('Correct Answer:','answer',
                    136: 					  $token,40);
                    137: 	$result .=&Apache::edit::hidden_arg('jmeanswer',$token);
                    138: 	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
                    139: 						 $safeeval);
                    140: 	$result .=&seperate_jme_window(
                    141:                       &Apache::edit::html_element_name('answer'),
                    142:                       &Apache::edit::html_element_name('jmeanswer'),
1.6       albertel  143: 		      $jmeanswer,'multipart');
1.7       albertel  144: 	$result .='</nobr>'.
                    145: 	    &Apache::edit::select_arg('Multipart:','multipart',
                    146: 				      ['no','yes'],$token);
1.2       albertel  147: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    148:     } elsif ($target eq 'modified') {
                    149: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    150: 						     $safeeval,'molecule',
1.6       albertel  151: 						     'answer','jmeanswer',
                    152: 						     'multipart');
1.2       albertel  153: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.1       albertel  154:     }
                    155:     return $result;
                    156: }
                    157: 
1.6       albertel  158: sub end_organicresponse {
1.1       albertel  159:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    160:     my $result;
                    161:     if ($target eq 'grade' && defined($ENV{'form.submitted'})) {
                    162: 	&Apache::response::setup_params($$tagstack[-1]);
                    163: 	my $response = &Apache::response::getresponse();
                    164: 	if ( $response =~ /[^\s]/) {
                    165: 	    my $partid = $Apache::inputtags::part;
                    166: 	    my $id = $Apache::inputtags::response['-1'];
                    167: 	    my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    168: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    169: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    170: 	    &Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
                    171: 	    my $ad;
                    172: 	    if ($response eq $answer) {
                    173: 		$ad='EXACT_ANS';
                    174: 	    } else {
                    175: 		$ad='INCORRECT';
                    176: 	    }
                    177: 	    &Apache::response::handle_previous(\%previous,$ad);
                    178: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
                    179: 	    $Apache::lonhomework::results{"resource.$partid.$id.molecule"}=$ENV{"form.MOLECULE_$id"};
                    180: 	}
1.2       albertel  181:     } elsif ($target eq "edit") {
                    182: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.1       albertel  183:     }
                    184:     &Apache::response::end_response;
                    185:     return $result;
                    186: }
                    187: 
1.6       albertel  188: sub start_organicstructure {
1.3       albertel  189:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    190:     my $result;
                    191:     if ($target eq 'web') {
                    192: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
                    193: 	my $height=&Apache::lonxml::get_param('height',$parstack,$safeeval);
                    194: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
                    195: 	$result=<<CHEMOUTPUT;
                    196: <applet code="JME.class" archive="/adm/jme/JME.jar" width="$width" height="$height">
                    197: <param name="options" value="depict border" />
                    198: <param name="jme" value="$molecule" />
                    199: </applet>
                    200: CHEMOUTPUT
                    201:     } elsif ($target eq 'edit') {
                    202: 	$result .=&Apache::edit::tag_start($target,$token);
                    203: 	$result .=&Apache::edit::text_arg('Width:','width',$token,5);
                    204: 	$result .=&Apache::edit::text_arg('Height:','height',$token,5);
                    205: 	$result .=&Apache::edit::text_arg('Molecule:','molecule',$token,40);
                    206: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
                    207: 						$safeeval);
                    208: 	$result .=&seperate_jme_window(undef,
                    209: 		      &Apache::edit::html_element_name('molecule'),
1.6       albertel  210: 		      $molecule,'multipart');
1.3       albertel  211: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    212:     } elsif ($target eq 'modified') {
                    213: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    214: 						     $safeeval,'molecule',
                    215: 						     'width','height');
                    216: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    217:     }
                    218:     return $result;
1.1       albertel  219: }
                    220: 
1.6       albertel  221: sub end_organicstructure {
1.3       albertel  222:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    223:     my $result;
                    224:     if ($target eq "edit") {
                    225: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    226:     }
1.4       albertel  227:     return $result;
                    228: }
                    229: 
1.9       albertel  230: sub edit_reaction_button {
1.11    ! albertel  231:     my ($id,$field,$reaction)=@_;
1.10      albertel  232:     my $id_es=&Apache::lonnet::escape($id);
                    233:     my $field_es=&Apache::lonnet::escape($field);
1.11    ! albertel  234:     my $reaction_es=&Apache::lonnet::escape($reaction);
1.9       albertel  235:     my $result=<<EDITREACTION;
1.10      albertel  236: <script type="text/javascript">
                    237:     function create_reaction_window_${id}_${field} () {
                    238: 	editor=window.open('','','width=500,height=270,scrollbars=no,resizable=yes');
                    239: 	editor.document.open('text/html','replace');
1.11    ! albertel  240: 	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  241:     }
                    242: </script>
                    243: <input type='button' value='Edit Reaction' onClick="javascript:create_reaction_window_${id}_${field}();void(0);" />
1.9       albertel  244: EDITREACTION
                    245:     return $result;
                    246: }
                    247: 
1.4       albertel  248: sub start_reactionresponse {
                    249:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    250:     my $result;
                    251:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.10      albertel  252:     if ($target eq 'meta') {
                    253:     } elsif ($target eq 'web') {
1.11    ! albertel  254: 	my $partid = $Apache::inputtags::part;
        !           255: 	my $id = $Apache::inputtags::response['-1'];
        !           256: 	my $reaction=$Apache::lonhomework::history{"resource.$partid.$id.submission"};
        !           257: 	$result.=&edit_reaction_button($id,"HWVAL_$id",$reaction);
1.4       albertel  258:     } elsif ($target eq "edit") {
1.9       albertel  259: 	$result .=&Apache::edit::tag_start($target,$token);
                    260: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,
                    261: 						$safeeval);
1.10      albertel  262: 	$result .='<nobr>'.
                    263: 	    &Apache::edit::text_arg('Answer:','answer',$token,40);
                    264: 	$result .=&edit_reaction_button($id,&Apache::edit::html_element_name('answer'),$answer).'</nobr>';
                    265: 	
1.9       albertel  266: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.10      albertel  267:     }  elsif ($target eq 'modified') {
                    268: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    269: 						     $safeeval,'answer');
                    270: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.4       albertel  271:     }
                    272:     return $result;
                    273: }
                    274: 
                    275: sub end_reactionresponse {
                    276:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    277:     my $result;
1.10      albertel  278:     if ($target eq 'grade' && defined($ENV{'form.submitted'})) {
                    279: 	&Apache::response::setup_params($$tagstack[-1]);
                    280: 	my $response = &Apache::response::getresponse();
                    281: 	if ( $response =~ /[^\s]/) {
                    282: 	    my $partid = $Apache::inputtags::part;
                    283: 	    my $id = $Apache::inputtags::response['-1'];
                    284: 	    my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    285: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    286: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    287: 	    &Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
                    288: 	    my $ad;
                    289: 	    if ($response eq $answer) {
                    290: 		$ad='EXACT_ANS';
                    291: 	    } else {
                    292: 		$ad='INCORRECT';
                    293: 	    }
                    294: 	    &Apache::response::handle_previous(\%previous,$ad);
                    295: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
                    296: 	}
                    297:     }  elsif ($target eq "edit") {
1.4       albertel  298: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    299:     }
                    300:     &Apache::response::end_response;
1.3       albertel  301:     return $result;
1.1       albertel  302: }
                    303: 
                    304: 1;
                    305: __END__

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