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