1: # The LearningOnline Network with CAPA
2: # chemical equation style response
3: #
4: # $Id: chemresponse.pm,v 1.63 2006/03/09 00:48:47 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: use Apache::lonlocal;
34:
35: BEGIN {
36: &Apache::lonxml::register('Apache::chemresponse',('organicresponse','organicstructure','reactionresponse','chem'));
37: }
38:
39: sub chem_standard_order {
40: my ($reaction) = @_;
41: my ($re,$pr) = split(/->|<=>/,$reaction);
42: my @reactants = split(/\s\+/,$re);
43: my @products = split(/\s\+/,$pr);
44: foreach my $substance (@reactants,@products) {
45: $substance =~ s/(\^\d*)\s+/$1_/g; # protect superscript space
46: $substance =~ s/\s*//g; # strip whitespace
47: $substance =~ s/_/ /g; # restore superscript space
48: }
49: @reactants = sort @reactants;
50: @products = sort @products;
51: my $standard = '';
52: foreach my $substance (@reactants) {
53: $standard .= $substance;
54: $standard .= ' + ';
55: }
56: $standard =~ s/ \+ $//; # get rid of trailing plus sign
57: $standard .= ' -> ';
58: foreach my $substance (@products) {
59: $standard .= $substance;
60: $standard .= ' + ';
61: }
62: $standard =~ s/ \+ $//; # get rid of trailing plus sign
63: return $standard;
64: }
65:
66: sub separate_jme_window {
67: my ($smile_input,$jme_input,$molecule,$options,$shown_text)=@_;
68: my $smilesection;
69: if (defined($smile_input)) {
70: $smilesection=<<SMILESECTION;
71: smiles = document.applets.JME.smiles();
72: opener.document.lonhomework.$smile_input.value = smiles;
73: SMILESECTION
74: }
75: my $jmesection;
76: if (defined($jme_input)) {
77: $jmesection=<<JMESECTION;
78: jmeFile = document.applets.JME.jmeFile();
79: opener.document.lonhomework.$jme_input.value = jmeFile;
80: JMESECTION
81: }
82:
83: if ($molecule) { $molecule='<param name="jme" value="'.$molecule.'" />'; }
84: my $insert_answer;
85: if ($shown_text eq '') {
86: $insert_answer=
87: '<input type="button" name="submit" value="Insert Answer" onclick="javascript:submitSmiles();" />';
88: }
89:
90: my $body=<<CHEMPAGE;
91: <html>
92: <head>
93: <title>Molecule Editor</title>
94: <script type="text/javascript">
95: function submitSmiles() {
96: jmeFile = document.applets.JME.jmeFile();
97: if (jmeFile == "") {
98: alert("Nothing to submit");
99: } else {
100: $jmesection
101: $smilesection
102: window.close();
103: }
104: }
105: function openHelpWindow() {
106: window.open("/adm/jme/jme_help.html","","scrollbars=yes,resizable=yes,width=500,height=600");
107: }
108: function substituent(r) {document.applets.JME.setSubstituent(r);}
109: </script>
110: </head>
111: <body bgcolor="#ffffff">
112: <center>
113: <form>
114: <table width="440"><tr>
115: <td></td>
116: <td align="right">
117: <select onchange="javascript:substituent(options[selectedIndex].text)">
118: <option>Select substituent</option>
119: <option>-C(=O)OH</option>
120: <option>-C(=O)OMe</option>
121: <option>-OC(=O)Me</option>
122: <option>-CMe3</option>
123: <option>-CF3</option>
124: <option>-CCl3</option>
125: <option>-NO2</option>
126: <option>-SO2-NH2</option>
127: <option>-NH-SO2-Me</option>
128: <option>-NMe2</option>
129: <option>-C#N</option>
130: <option>-C#C-Me</option>
131: <option>-C#CH</option>
132: </select>
133: </td></tr>
134: </table>
135: <applet code="JME.class" name="JME" archive="/adm/jme/JME.jar" width="440" height="390" mayscript>
136: You have to enable Java and JavaScript on your machine.
137: $molecule
138: <param name="options" value="$options" />
139: </applet><br />
140: <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>
141: <br />
142: $insert_answer
143: <br />
144: <input type="button" value=" Close " onclick = "javascript:window.close()" />
145:
146: <input type="button" value=" Help " onclick = "javascript:openHelpWindow()" />
147: </form>
148: </center>
149: </body>
150: </html>
151: CHEMPAGE
152: $body=&HTML::Entities::encode($body,'<>&"');
153: $body=~s/\n/ /g;
154: my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
155: my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
156: my $display=&mt('Draw Molecule');
157: if (defined($shown_text)) { $display=&mt($shown_text); }
158: my $result=<<CHEMINPUT;
159: <input type="button" value="$display" onclick="javascript:editor=window.open($nothing,'jmeedit','width=500,height=500,menubar=no,scrollbars=no,resizable=yes');editor.$docopen;editor.document.write('$body');editor.document.close();editor.focus()" />
160: CHEMINPUT
161: return $result;
162: }
163:
164: sub jme_img {
165: my ($jme,$smile,$width,$options)=@_;
166: my $id=&Apache::loncommon::get_cgi_id();
167: my $result='<img alt="'.$smile.'" src="/cgi-bin/convertjme.pl?'.$id.'"';
168: if ($options =~ /border/) { $result.= ' border="1"'; }
169: $result.=' />';
170: &Apache::lonnet::appenv('cgi.'.$id.'.JME' =>
171: &Apache::lonnet::escape($jme),
172: 'cgi.'.$id.'.PNG' => 1,
173: 'cgi.'.$id.'.WIDTH' => $width);
174: return $result;
175: }
176:
177: sub start_organicresponse {
178: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
179: my $result;
180: my $partid = $Apache::inputtags::part;
181: my $id = &Apache::response::start_response($parstack,$safeeval);
182: if ($target eq 'meta') {
183: $result=&Apache::response::meta_package_write('organicresponse');
184: } elsif ($target eq 'web') {
185: my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
186: $safeeval);
187: if ( &Apache::response::show_answer() && $jmeanswer ne '') {
188: my $options=&Apache::lonxml::get_param('options',$parstack,
189: $safeeval);
190: my $width=&Apache::lonxml::get_param('width',$parstack,
191: $safeeval);
192: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
193: $safeeval);
194: $result.=&jme_img($jmeanswer,$answers[0],$width,$options);
195: } else {
196: my $molecule;
197: if (defined($Apache::lonhomework::history{"resource.$partid.$id.molecule"})) {
198: $molecule=$Apache::lonhomework::history{"resource.$partid.$id.molecule"};
199: } else {
200: $molecule=&Apache::lonxml::get_param('molecule',$parstack,
201: $safeeval);
202: }
203: my $options=&Apache::lonxml::get_param('options',$parstack,
204: $safeeval);
205: my $shown_text;
206: if (&Apache::response::show_answer()) {
207: $shown_text="Show Your Last Answer";
208: }
209: $result=&separate_jme_window("HWVAL_$id","MOLECULE_$id",$molecule,
210: $options,$shown_text);
211: $result.= '<input type="hidden" name="MOLECULE_'.$id.'" value="" />';
212: }
213: } elsif ($target eq 'edit') {
214: $result .=&Apache::edit::tag_start($target,$token);
215: my $options=&Apache::lonxml::get_param('options',$parstack,
216: $safeeval);
217: if ($options !~ /multipart/) { $options.=',multipart'; }
218: $result .='<nobr>'.
219: &Apache::edit::text_arg('Starting Molecule:','molecule',
220: $token,40);
221: my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
222: $safeeval);
223: $result .=&separate_jme_window(undef,
224: &Apache::edit::html_element_name('molecule'),
225: $molecule,$options);
226: $result .='</nobr><br /><nobr>';
227: $result .=&Apache::edit::text_arg('Correct Answer:','answer',
228: $token,40);
229: $result .=&Apache::edit::hidden_arg('jmeanswer',$token);
230: my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
231: $safeeval);
232: $result .=&separate_jme_window(
233: &Apache::edit::html_element_name('answer'),
234: &Apache::edit::html_element_name('jmeanswer'),
235: $jmeanswer,$options);
236: $result .='</nobr><br />';
237: $result .=&Apache::edit::checked_arg('Options:','options',
238: [ ['autoez','Auto E,Z stereochemistry'],
239: ['multipart','Multipart Structures'],
240: ['nostereo','No stereochemistry'],
241: ['reaction','Is a reaction'],
242: ['number','Able to number atoms'] ],
243: ,$token);
244: $result .=&Apache::edit::text_arg('Width of correct answer image:',
245: 'width',$token,10);
246: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
247: } elsif ($target eq 'modified') {
248: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
249: $safeeval,'molecule',
250: 'answer','jmeanswer',
251: 'options','width');
252: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
253: }
254:
255: return $result;
256: }
257:
258: sub end_organicresponse {
259: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
260: my $result;
261: if ($target eq 'grade' && &Apache::response::submitted()) {
262: &Apache::response::setup_params($$tagstack[-1],$safeeval);
263: my $response = &Apache::response::getresponse();
264: if ( $response =~ /[^\s]/) {
265: my $partid = $Apache::inputtags::part;
266: my $id = $Apache::inputtags::response['-1'];
267: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
268: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
269: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
270: my $ad;
271: foreach my $answer (@answers) {
272: &Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
273: if ($response eq $answer) {
274: $ad='EXACT_ANS';
275: last;
276: } else {
277: $ad='INCORRECT';
278: }
279: }
280: if ($ad && $Apache::lonhomework::type eq 'survey') {
281: $ad='SUBMITTED';
282: }
283: &Apache::response::handle_previous(\%previous,$ad);
284: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
285: $Apache::lonhomework::results{"resource.$partid.$id.molecule"}=$env{"form.MOLECULE_$id"};
286: }
287: } elsif ($target eq "edit") {
288: $result.= &Apache::edit::tag_end($target,$token,'');
289: } elsif ($target eq 'answer') {
290: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
291: $safeeval);
292: $result.=&Apache::response::answer_header('organicresponse');
293: foreach my $answer (@answers) {
294: $result.=&Apache::response::answer_part('organicresponse',$answer);
295: }
296: $result.=&Apache::response::answer_footer('organicresponse');
297: }
298:
299: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
300: $target eq 'tex' || $target eq 'analyze') {
301: &Apache::lonxml::increment_counter(&Apache::response::repetition());
302: }
303: &Apache::response::end_response();
304: return $result;
305: }
306:
307: sub start_organicstructure {
308: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
309: my $result;
310: if ($target eq 'web') {
311: my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
312: my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
313: my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
314: my $id=&Apache::loncommon::get_cgi_id();
315: $result="<img src='/cgi-bin/convertjme.pl?$id'";
316: if ($options =~ /border/) { $result.= ' border="1"'; }
317: $result.=' />';
318: &Apache::lonnet::appenv(
319: 'cgi.'.$id.'.JME' => &Apache::lonnet::escape($molecule),
320: 'cgi.'.$id.'.PNG' => 1,
321: 'cgi.'.$id.'.WIDTH' => $width );
322: } elsif ($target eq 'tex') {
323: my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1);
324: my $webwidth=&Apache::lonxml::get_param('width', $parstack, $safeeval);
325: my $webheight=&Apache::lonxml::get_param('height', $parstack, $safeeval);
326: if (!$webheight) { $webheight = $webwidth; }
327: if (!$texwidth) { $texwidth='90'; }
328: $result = "%DYNAMICIMAGE:$webwidth:$webheight:$texwidth\n";
329: my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
330: my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
331: my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
332: '_'.time.'_'.$$.int(rand(1000)).'_organicstructure';
333: my $id=$filename;
334: &Apache::lonnet::appenv(
335: 'cgi.'.$id.'.JME' => &Apache::lonnet::escape($molecule),
336: 'cgi.'.$id.'.PS' => 1,
337: 'cgi.'.$id.'.WIDTH' => $texwidth );
338: $id=&Apache::lonnet::escape($id);
339: &Apache::lonxml::register_ssi("/cgi-bin/convertjme.pl?$id");
340: if ($options =~ /border/) { $result.= '\fbox{'; }
341: $result .= '\graphicspath{{/home/httpd/perl/tmp/}}\includegraphics[width='.$texwidth.' mm]{'.$filename.'.eps}';
342: if ($options =~ /border/) { $result.= '} '; }
343: } elsif ($target eq 'edit') {
344: $result .=&Apache::edit::tag_start($target,$token);
345: $result .=&Apache::edit::text_arg('Width (pixels):','width',$token,5);
346: $result .=&Apache::edit::text_arg('TeXwidth (mm):','texwidth',$token,5);
347: $result .='<nobr>';
348: $result .=&Apache::edit::text_arg('Molecule:','molecule',$token,40);
349: my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
350: $safeeval);
351: my $options=&Apache::lonxml::get_param('options',$parstack,
352: $safeeval);
353: if ($options !~ /reaction/) {
354: $options.= ',multipart,number';
355: }
356:
357: $result .=&separate_jme_window(undef,
358: &Apache::edit::html_element_name('molecule'),
359: $molecule,$options);
360: $result.="</nobr><br />";
361: $result .=&Apache::edit::checked_arg('Options:','options',
362: [ ['reaction','Is a reaction'],
363: ['border','Draw a border'] ],
364: $token);
365: $result .=&Apache::edit::end_row();
366: } elsif ($target eq 'modified') {
367: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
368: $safeeval,'molecule',
369: 'width','texwidth',
370: 'options');
371: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
372: }
373: return $result;
374: }
375:
376: sub end_organicstructure {
377: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
378: my $result;
379: if ($target eq "edit") {
380: $result.= &Apache::edit::tag_end($target,$token,'');
381: }
382: return $result;
383: }
384:
385: sub edit_reaction_button {
386: my ($id,$field,$reaction)=@_;
387: my $id_es=&Apache::lonnet::escape($id);
388: my $field_es=&Apache::lonnet::escape($field);
389: my $reaction_es=&Apache::lonnet::escape($reaction);
390: my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
391: my $result=<<EDITREACTION;
392: <script type="text/javascript">
393: // <!--
394: function create_reaction_window_${id}_${field} () {
395: editor=window.open('','','width=500,height=270,scrollbars=no,resizable=yes');
396: editor.$docopen;
397: 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?inhibitmenu=yes" name="viewer" scrolling="no" /> <frame src="/res/adm/pages/reactionresponse/reaction_editor.html?inhibitmenu=yes&reaction=$reaction_es&id=$id_es&field=$field_es" name="editor" scrolling="no" /> </frameset> </html>');
398: }
399: // -->
400: </script>
401: <input type='button' value='Edit Answer' onclick="javascript:create_reaction_window_${id}_${field}();void(0);" />
402: EDITREACTION
403: return $result;
404: }
405:
406: sub start_reactionresponse {
407: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
408: my $result;
409: my $id = &Apache::response::start_response($parstack,$safeeval);
410: if ($target eq 'meta') {
411: $result=&Apache::response::meta_package_write('reactionresponse');
412: } elsif ($target eq 'web') {
413: my $partid = $Apache::inputtags::part;
414: my $id = $Apache::inputtags::response['-1'];
415: my $reaction=$Apache::lonhomework::history{"resource.$partid.$id.submission"};
416: if ($reaction eq '') { $reaction=&Apache::lonxml::get_param('initial',$parstack,$safeeval); }
417: my $status=$Apache::inputtags::status['-1'];
418: if ($status eq 'CAN_ANSWER') {
419: $result.=&edit_reaction_button($id,"HWVAL_$id",$reaction);
420: }
421: if ( &Apache::response::show_answer() ) {
422: my $ans=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
423: if (!$Apache::lonxml::default_homework_loaded) {
424: &Apache::lonxml::default_homework_load($safeeval);
425: }
426: @Apache::scripttag::parser_env = @_;
427: $Apache::inputtags::answertxt{$id}=&Apache::run::run("return &chemparse(q\0$ans\0);",$safeeval);
428: }
429: } elsif ($target eq "edit") {
430: $result .=&Apache::edit::tag_start($target,$token);
431: my $answer=&Apache::lonxml::get_param('answer',$parstack,
432: $safeeval);
433: $result .='<nobr>'.
434: &Apache::edit::text_arg('Answer:','answer',$token,40);
435: $result .=&edit_reaction_button($id,&Apache::edit::html_element_name('answer'),$answer).'</nobr>';
436: my $initial=&Apache::lonxml::get_param('initial',$parstack,$safeeval);
437: $result.='<nobr>'.
438: &Apache::edit::text_arg('Initial Reaction:','initial',$token,40);
439: $result .=&edit_reaction_button($id,&Apache::edit::html_element_name('initial'),$initial).'</nobr>';
440:
441: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
442: } elsif ($target eq 'modified') {
443: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
444: $safeeval,'answer',
445: 'initial');
446: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
447: }
448: return $result;
449: }
450:
451: sub end_reactionresponse {
452: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
453: my $result;
454: if ($target eq 'grade' && &Apache::response::submitted()) {
455: &Apache::response::setup_params($$tagstack[-1],$safeeval);
456: my $response = &Apache::response::getresponse();
457: if ( $response =~ /[^\s]/) {
458: my $partid = $Apache::inputtags::part;
459: my $id = $Apache::inputtags::response['-1'];
460: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
461: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
462: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
463: my $ad;
464: foreach my $answer (@answers) {
465: &Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
466: if (&chem_standard_order($response) eq
467: &chem_standard_order($answer)) {
468: $ad='EXACT_ANS';
469: } else {
470: $ad='INCORRECT';
471: }
472: }
473: if ($ad && $Apache::lonhomework::type eq 'survey') {
474: $ad='SUBMITTED';
475: }
476: &Apache::response::handle_previous(\%previous,$ad);
477: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
478: }
479: } elsif ($target eq "edit") {
480: $result.= &Apache::edit::tag_end($target,$token,'');
481: } elsif ($target eq 'answer') {
482: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
483: $safeeval);
484: $result.=&Apache::response::answer_header('reactionresponse');
485: foreach my $answer (@answers) {
486: $result.=&Apache::response::answer_part('reactionresponse',
487: $answer);
488: }
489: $result.=&Apache::response::answer_footer('reactionresponse');
490: }
491:
492: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
493: $target eq 'tex' || $target eq 'analyze') {
494: &Apache::lonxml::increment_counter(&Apache::response::repetition());
495: }
496: &Apache::response::end_response();
497: return $result;
498: }
499:
500: sub start_chem {
501: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
502: my $result = '';
503: my $inside = &Apache::lonxml::get_all_text_unbalanced("/chem",$parser);
504: if ($target eq 'tex' || $target eq 'web') {
505: $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
506: if (!$Apache::lonxml::default_homework_loaded) {
507: &Apache::lonxml::default_homework_load($safeeval);
508: }
509: @Apache::scripttag::parser_env = @_;
510: $result=&Apache::run::run("return &chemparse(q\0$inside\0);",$safeeval);
511: }
512: return $result;
513: }
514:
515: sub end_chem {
516: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
517: my $result = '';
518: return $result;
519: }
520:
521: 1;
522: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>