File:  [LON-CAPA] / loncom / homework / chemresponse.pm
Revision 1.17: download - view: text, annotated - select for diffs
Thu Oct 16 21:52:27 2003 UTC (20 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#2292 mostly done, now prints (and generates a PS file of ths sturcture) all of the JME -> pns,PNG code was done by Guy Ashkenazi, I am just gluing it in.

    1: # The LearningOnline Network with CAPA
    2: # chemical equation style response
    3: #
    4: # $Id: chemresponse.pm,v 1.17 2003/10/16 21:52:27 albertel Exp $
    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 {
   35:     &Apache::lonxml::register('Apache::chemresponse',('organicresponse','organicstructure','reactionresponse'));
   36: }
   37: 
   38: sub seperate_jme_window {
   39:     my ($smile_input,$jme_input,$molecule,$options)=@_;
   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: 
   54:     if ($molecule) { $molecule='<param name="jme" value="'.$molecule.'" />'; }
   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 {
   65:         $smilesection
   66:         $jmesection
   67: 	window.close();
   68:     }
   69: }
   70: function openHelpWindow() {
   71:     window.open("/adm/jme/jme_help.html","","scrollbars=yes,resizable=yes,width=500,height=600");
   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.
   79: $molecule
   80: <param name="options" value="$options" />
   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: 
  102: sub start_organicresponse {
  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: 	}
  116: 	my $options=&Apache::lonxml::get_param('options',$parstack,
  117: 					       $safeeval);
  118: 	$result=&seperate_jme_window("HWVAL_$id","MOLECULE_$id",$molecule,$options);
  119: 	$result.= '<input type="hidden" name="MOLECULE_'.$id.'" value="" />';
  120:     } elsif ($target eq 'edit') {
  121: 	$result .=&Apache::edit::tag_start($target,$token);
  122: 	my $options=&Apache::lonxml::get_param('options',$parstack,
  123: 					       $safeeval);
  124: 	if ($options !~ /multipart/) { $options.=',multipart'; }
  125: 	$result .='<nobr>'.
  126: 	    &Apache::edit::text_arg('Starting Molecule:','molecule',
  127: 				    $token,40);
  128: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
  129: 						$safeeval);
  130: 	$result .=&seperate_jme_window(undef,
  131: 		      &Apache::edit::html_element_name('molecule'),
  132: 		      $molecule,$options);
  133: 	$result .='</nobr><br /><nobr>';
  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'),
  142: 		      $jmeanswer,$options);
  143: 	$result .='</nobr><br />';
  144: 	$result .=&Apache::edit::checked_arg('Options:','options',
  145: 				    [ ['autoez','Auto E,Z sterochemistry'],
  146: 				      ['multipart','MultiPart Structures'],
  147: 				      ['hydrogens','Show Hydrogens'],
  148: 				      ['nostereo','No stereochemistry'],
  149: 				      ['reaction','Is a reaction'],
  150: 				      ['number','Able to number atoms'],
  151: 				      ['border','Draw a border'] ],
  152: 					     ,$token);
  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',
  157: 						     'answer','jmeanswer',
  158: 						     'options');
  159: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  160:     }
  161:     return $result;
  162: }
  163: 
  164: sub end_organicresponse {
  165:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  166:     my $result;
  167:     if ($target eq 'grade' && defined($ENV{'form.submitted'})) {
  168: 	&Apache::response::setup_params($$tagstack[-1]);
  169: 	my $response = &Apache::response::getresponse();
  170: 	if ( $response =~ /[^\s]/) {
  171: 	    my $partid = $Apache::inputtags::part;
  172: 	    my $id = $Apache::inputtags::response['-1'];
  173: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
  174: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
  175: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
  176: 	    my $ad;
  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: 		}
  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: 	}
  190:     } elsif ($target eq "edit") {
  191: 	$result.= &Apache::edit::tag_end($target,$token,'');
  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');
  200:     }
  201:     &Apache::response::end_response;
  202:     return $result;
  203: }
  204: 
  205: sub start_organicstructure {
  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);
  211: 	my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
  212: 	my $id=time.'_'.int(rand(1000));
  213: 	$result="<img src='/cgi-bin/convertjme.pl?$id' />";
  214: 	&Apache::lonnet::appenv(
  215:             'cgi.'.$id.'.JME'   => &Apache::lonnet::escape($molecule),
  216: 	    'cgi.'.$id.'.PNG' => 1,
  217: 	    'cgi.'.$id.'.WIDTH' => $width );
  218:     } elsif ($target eq 'tex') {
  219: 	my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,1);
  220: 	if (!$texwidth) { $texwidth='90'; }
  221: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
  222: 	my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
  223: 	my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
  224: 	    '_'.time.'_'.$$.int(rand(1000)).'_organicstructure';
  225: 	my $id=$filename;
  226: 	&Apache::lonnet::appenv(
  227: 		     'cgi.'.$id.'.JME'   => &Apache::lonnet::escape($molecule),
  228: 		     'cgi.'.$id.'.PS' => 1,
  229: 		     'cgi.'.$id.'.WIDTH' => $texwidth );
  230: 	$id=&Apache::lonnet::escape($id);
  231: 	&Apache::lonxml::register_ssi("/cgi-bin/convertjme.pl?$id");
  232: 	$result = '\graphicspath{{/home/httpd/perl/tmp/}}\includegraphics[width='.$texwidth.' mm]{'.$filename.'.eps}';
  233:     } elsif ($target eq 'edit') {
  234: 	$result .=&Apache::edit::tag_start($target,$token);
  235: 	$result .=&Apache::edit::text_arg('Width:','width',$token,5);
  236: 	$result .=&Apache::edit::text_arg('Height:','height',$token,5);
  237: 	$result .=&Apache::edit::text_arg('TeXwidth:','texwidth',$token,5);
  238: 	$result .='<nobr>';
  239: 	$result .=&Apache::edit::text_arg('Molecule:','molecule',$token,40);
  240: 	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
  241: 						$safeeval);
  242: 	my $options=&Apache::lonxml::get_param('options',$parstack,
  243: 					       $safeeval);
  244: 	if ($options !~ /reaction/) {
  245: 	    $options.= ',multipart,number';
  246: 	}
  247: 						   
  248: 	$result .=&seperate_jme_window(undef,
  249: 				 &Apache::edit::html_element_name('molecule'),
  250: 				       $molecule,$options);
  251: 	$result.="</nobr><br />";
  252: 	$result .=&Apache::edit::checked_arg('Options:','options',
  253: 					     [ ['hydrogens','Show Hydrogens'],
  254: 					       ['reaction','Is a reaction'],
  255: 					       ['border','Draw a border'] ],
  256: 					     $token);
  257: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  258:     } elsif ($target eq 'modified') {
  259: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  260: 						     $safeeval,'molecule',
  261: 						     'width','height',
  262: 						     'texwidth','options');
  263: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  264:     }
  265:     return $result;
  266: }
  267: 
  268: sub end_organicstructure {
  269:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  270:     my $result;
  271:     if ($target eq "edit") {
  272: 	$result.= &Apache::edit::tag_end($target,$token,'');
  273:     }
  274:     return $result;
  275: }
  276: 
  277: sub edit_reaction_button {
  278:     my ($id,$field,$reaction)=@_;
  279:     my $id_es=&Apache::lonnet::escape($id);
  280:     my $field_es=&Apache::lonnet::escape($field);
  281:     my $reaction_es=&Apache::lonnet::escape($reaction);
  282:     my $result=<<EDITREACTION;
  283: <script type="text/javascript">
  284:     function create_reaction_window_${id}_${field} () {
  285: 	editor=window.open('','','width=500,height=270,scrollbars=no,resizable=yes');
  286: 	editor.document.open('text/html','replace');
  287: 	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>');
  288:     }
  289: </script>
  290: <input type='button' value='Edit Reaction' onClick="javascript:create_reaction_window_${id}_${field}();void(0);" />
  291: EDITREACTION
  292:     return $result;
  293: }
  294: 
  295: sub start_reactionresponse {
  296:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  297:     my $result;
  298:     my $id = &Apache::response::start_response($parstack,$safeeval);
  299:     if ($target eq 'meta') {
  300:     } elsif ($target eq 'web') {
  301: 	my $partid = $Apache::inputtags::part;
  302: 	my $id = $Apache::inputtags::response['-1'];
  303: 	my $reaction=$Apache::lonhomework::history{"resource.$partid.$id.submission"};
  304: 	$result.=&edit_reaction_button($id,"HWVAL_$id",$reaction);
  305:     } elsif ($target eq "edit") {
  306: 	$result .=&Apache::edit::tag_start($target,$token);
  307: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,
  308: 						$safeeval);
  309: 	$result .='<nobr>'.
  310: 	    &Apache::edit::text_arg('Answer:','answer',$token,40);
  311: 	$result .=&edit_reaction_button($id,&Apache::edit::html_element_name('answer'),$answer).'</nobr>';
  312: 	
  313: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  314:     }  elsif ($target eq 'modified') {
  315: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  316: 						     $safeeval,'answer');
  317: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  318:     }
  319:     return $result;
  320: }
  321: 
  322: sub end_reactionresponse {
  323:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  324:     my $result;
  325:     if ($target eq 'grade' && defined($ENV{'form.submitted'})) {
  326: 	&Apache::response::setup_params($$tagstack[-1]);
  327: 	my $response = &Apache::response::getresponse();
  328: 	if ( $response =~ /[^\s]/) {
  329: 	    my $partid = $Apache::inputtags::part;
  330: 	    my $id = $Apache::inputtags::response['-1'];
  331: 	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
  332: 	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
  333: 	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
  334: 	    my $ad;
  335: 	    foreach my $answer (@answers) {
  336: 		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
  337: 		if ($response eq $answer) {
  338: 		    $ad='EXACT_ANS';
  339: 		} else {
  340: 		    $ad='INCORRECT';
  341: 		}
  342: 	    }
  343: 	    &Apache::response::handle_previous(\%previous,$ad);
  344: 	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
  345: 	}
  346:     }  elsif ($target eq "edit") {
  347: 	$result.= &Apache::edit::tag_end($target,$token,'');
  348:     } elsif ($target eq 'answer') {
  349: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
  350: 						     $safeeval);
  351: 	$result.=&Apache::response::answer_header('reactionresponse');
  352: 	foreach my $answer (@answers) {
  353: 	    $result.=&Apache::response::answer_part('reactionresponse',
  354: 						    $answer);
  355: 	}
  356: 	$result.=&Apache::response::answer_footer('reactionresponse');
  357:     }
  358:     &Apache::response::end_response;
  359:     return $result;
  360: }
  361: 
  362: 1;
  363: __END__

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