Annotation of loncom/homework/chemresponse.pm, revision 1.82
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # chemical equation style response
3: #
1.82 ! raeburn 4: # $Id: chemresponse.pm,v 1.81 2008/09/21 20:09:06 raeburn 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;
1.55 albertel 33: use Apache::lonlocal;
1.66 www 34: use lib '/home/httpd/lib/perl/';
35: use LONCAPA;
36:
1.1 albertel 37:
38: BEGIN {
1.46 albertel 39: &Apache::lonxml::register('Apache::chemresponse',('organicresponse','organicstructure','reactionresponse','chem'));
1.1 albertel 40: }
41:
1.34 albertel 42: sub chem_standard_order {
43: my ($reaction) = @_;
44: my ($re,$pr) = split(/->|<=>/,$reaction);
45: my @reactants = split(/\s\+/,$re);
46: my @products = split(/\s\+/,$pr);
47: foreach my $substance (@reactants,@products) {
48: $substance =~ s/(\^\d*)\s+/$1_/g; # protect superscript space
49: $substance =~ s/\s*//g; # strip whitespace
50: $substance =~ s/_/ /g; # restore superscript space
51: }
52: @reactants = sort @reactants;
53: @products = sort @products;
54: my $standard = '';
55: foreach my $substance (@reactants) {
56: $standard .= $substance;
57: $standard .= ' + ';
58: }
59: $standard =~ s/ \+ $//; # get rid of trailing plus sign
60: $standard .= ' -> ';
61: foreach my $substance (@products) {
62: $standard .= $substance;
63: $standard .= ' + ';
64: }
65: $standard =~ s/ \+ $//; # get rid of trailing plus sign
66: return $standard;
67: }
68:
1.30 www 69: sub separate_jme_window {
1.55 albertel 70: my ($smile_input,$jme_input,$molecule,$options,$shown_text)=@_;
1.2 albertel 71: my $smilesection;
72: if (defined($smile_input)) {
73: $smilesection=<<SMILESECTION;
1.21 albertel 74: smiles = document.applets.JME.smiles();
1.2 albertel 75: opener.document.lonhomework.$smile_input.value = smiles;
76: SMILESECTION
77: }
78: my $jmesection;
79: if (defined($jme_input)) {
80: $jmesection=<<JMESECTION;
81: jmeFile = document.applets.JME.jmeFile();
82: opener.document.lonhomework.$jme_input.value = jmeFile;
83: JMESECTION
84: }
85:
1.14 albertel 86: if ($molecule) { $molecule='<param name="jme" value="'.$molecule.'" />'; }
1.57 albertel 87: my $insert_answer;
1.59 albertel 88: if ($shown_text eq '') {
1.57 albertel 89: $insert_answer=
90: '<input type="button" name="submit" value="Insert Answer" onclick="javascript:submitSmiles();" />';
91: }
92:
1.65 albertel 93: my $js = <<CHEMJS;
1.47 albertel 94: <script type="text/javascript">
1.1 albertel 95: function submitSmiles() {
1.21 albertel 96: jmeFile = document.applets.JME.jmeFile();
97: if (jmeFile == "") {
1.1 albertel 98: alert("Nothing to submit");
99: } else {
1.21 albertel 100: $jmesection
1.2 albertel 101: $smilesection
1.1 albertel 102: window.close();
103: }
104: }
105: function openHelpWindow() {
1.2 albertel 106: window.open("/adm/jme/jme_help.html","","scrollbars=yes,resizable=yes,width=500,height=600");
1.1 albertel 107: }
1.60 albertel 108: function substituent(r) {document.applets.JME.setSubstituent(r);}
1.1 albertel 109: </script>
1.65 albertel 110: CHEMJS
111:
112: my $start_page =
1.69 albertel 113: &Apache::loncommon::start_page('Molecule Editor',undef,
1.65 albertel 114: {'only_body' => 1,
1.69 albertel 115: 'js_ready' => 1,
1.65 albertel 116: 'bgcolor' => '#FFFFFF',});
1.69 albertel 117: my $end_page =
118: &Apache::loncommon::end_page({'js_ready' => 1,});
119:
1.65 albertel 120: my $body=<<CHEMPAGE;
1.69 albertel 121: $js
1.1 albertel 122: <center>
1.60 albertel 123: <form>
124: <table width="440"><tr>
125: <td></td>
126: <td align="right">
127: <select onchange="javascript:substituent(options[selectedIndex].text)">
128: <option>Select substituent</option>
129: <option>-C(=O)OH</option>
130: <option>-C(=O)OMe</option>
131: <option>-OC(=O)Me</option>
132: <option>-CMe3</option>
133: <option>-CF3</option>
134: <option>-CCl3</option>
135: <option>-NO2</option>
136: <option>-SO2-NH2</option>
137: <option>-NH-SO2-Me</option>
138: <option>-NMe2</option>
139: <option>-C#N</option>
140: <option>-C#C-Me</option>
141: <option>-C#CH</option>
142: </select>
143: </td></tr>
144: </table>
145: <applet code="JME.class" name="JME" archive="/adm/jme/JME.jar" width="440" height="390" mayscript>
1.1 albertel 146: You have to enable Java and JavaScript on your machine.
1.12 albertel 147: $molecule
1.6 albertel 148: <param name="options" value="$options" />
1.1 albertel 149: </applet><br />
1.49 albertel 150: <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>
1.61 albertel 151: <br />
1.57 albertel 152: $insert_answer
1.1 albertel 153: <br />
1.57 albertel 154: <input type="button" value=" Close " onclick = "javascript:window.close()" />
1.1 albertel 155:
1.57 albertel 156: <input type="button" value=" Help " onclick = "javascript:openHelpWindow()" />
1.1 albertel 157: </form>
158: </center>
159: CHEMPAGE
1.65 albertel 160:
1.69 albertel 161: $body=&Apache::loncommon::js_ready($body);
1.53 albertel 162: my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
1.41 www 163: my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
1.55 albertel 164: my $display=&mt('Draw Molecule');
165: if (defined($shown_text)) { $display=&mt($shown_text); }
1.79 riegler 166: my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
1.69 albertel 167: my $function =
168: 'LONCAPA_draw_molecule_'.&Apache::lonhtmlcommon::get_uniq_name();
1.1 albertel 169: my $result=<<CHEMINPUT;
1.69 albertel 170: <script type="text/javascript">
171: function $function() {
172: editor=window.open($nothing,'jmeedit','width=500,height=500,menubar=no,scrollbars=no,resizable=yes');
173: editor.$docopen;
174: editor.document.write('$start_page $body $end_page');
175: editor.document.close();
176: editor.focus();
177: }
178: </script>
1.79 riegler 179: <a href="javascript:$function();void(0);"><img class="stift" src='$iconpath/stift.gif' alt='$display' title='$display' /></a>
1.1 albertel 180: CHEMINPUT
181: return $result;
182: }
1.56 albertel 183: sub jme_img {
184: my ($jme,$smile,$width,$options)=@_;
185: my $id=&Apache::loncommon::get_cgi_id();
186: my $result='<img alt="'.$smile.'" src="/cgi-bin/convertjme.pl?'.$id.'"';
187: if ($options =~ /border/) { $result.= ' border="1"'; }
188: $result.=' />';
1.78 raeburn 189: &Apache::lonnet::appenv({'cgi.'.$id.'.JME' =>
190: &escape($jme),
191: 'cgi.'.$id.'.PNG' => 1,
192: 'cgi.'.$id.'.WIDTH' => $width});
1.56 albertel 193: return $result;
194: }
195:
1.6 albertel 196: sub start_organicresponse {
1.1 albertel 197: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
198: my $result;
199: my $partid = $Apache::inputtags::part;
200: my $id = &Apache::response::start_response($parstack,$safeeval);
201: if ($target eq 'meta') {
1.28 albertel 202: $result=&Apache::response::meta_package_write('organicresponse');
1.1 albertel 203: } elsif ($target eq 'web') {
1.55 albertel 204: my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
205: $safeeval);
206: if ( &Apache::response::show_answer() && $jmeanswer ne '') {
1.44 albertel 207: my $options=&Apache::lonxml::get_param('options',$parstack,
208: $safeeval);
209: my $width=&Apache::lonxml::get_param('width',$parstack,
210: $safeeval);
1.56 albertel 211: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
212: $safeeval);
213: $result.=&jme_img($jmeanswer,$answers[0],$width,$options);
1.1 albertel 214: } else {
1.44 albertel 215: my $molecule;
216: if (defined($Apache::lonhomework::history{"resource.$partid.$id.molecule"})) {
217: $molecule=$Apache::lonhomework::history{"resource.$partid.$id.molecule"};
218: } else {
219: $molecule=&Apache::lonxml::get_param('molecule',$parstack,
220: $safeeval);
221: }
222: my $options=&Apache::lonxml::get_param('options',$parstack,
223: $safeeval);
1.55 albertel 224: my $shown_text;
225: if (&Apache::response::show_answer()) {
226: $shown_text="Show Your Last Answer";
227: }
1.79 riegler 228: #stift $result=&separate_jme_window("HWVAL_$id","MOLECULE_$id",$molecule,
229: # $options,$shown_text);
1.44 albertel 230: $result.= '<input type="hidden" name="MOLECULE_'.$id.'" value="" />';
1.1 albertel 231: }
1.2 albertel 232: } elsif ($target eq 'edit') {
233: $result .=&Apache::edit::tag_start($target,$token);
1.12 albertel 234: my $options=&Apache::lonxml::get_param('options',$parstack,
235: $safeeval);
236: if ($options !~ /multipart/) { $options.=',multipart'; }
1.7 albertel 237: $result .='<nobr>'.
238: &Apache::edit::text_arg('Starting Molecule:','molecule',
239: $token,40);
1.2 albertel 240: my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
241: $safeeval);
1.30 www 242: $result .=&separate_jme_window(undef,
1.2 albertel 243: &Apache::edit::html_element_name('molecule'),
1.12 albertel 244: $molecule,$options);
1.7 albertel 245: $result .='</nobr><br /><nobr>';
1.2 albertel 246: $result .=&Apache::edit::text_arg('Correct Answer:','answer',
247: $token,40);
1.70 albertel 248: $result .='</nobr><br /><nobr>';
1.67 albertel 249: $result .=&Apache::edit::text_arg('JME string of the answer (automatically updated when using the Draw Molecule button):',
250: 'jmeanswer',$token);
1.2 albertel 251: my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
252: $safeeval);
1.30 www 253: $result .=&separate_jme_window(
1.2 albertel 254: &Apache::edit::html_element_name('answer'),
255: &Apache::edit::html_element_name('jmeanswer'),
1.12 albertel 256: $jmeanswer,$options);
257: $result .='</nobr><br />';
258: $result .=&Apache::edit::checked_arg('Options:','options',
1.29 www 259: [ ['autoez','Auto E,Z stereochemistry'],
1.18 albertel 260: ['multipart','Multipart Structures'],
1.12 albertel 261: ['nostereo','No stereochemistry'],
262: ['reaction','Is a reaction'],
1.18 albertel 263: ['number','Able to number atoms'] ],
1.12 albertel 264: ,$token);
1.44 albertel 265: $result .=&Apache::edit::text_arg('Width of correct answer image:',
266: 'width',$token,10);
1.2 albertel 267: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
268: } elsif ($target eq 'modified') {
269: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
270: $safeeval,'molecule',
1.6 albertel 271: 'answer','jmeanswer',
1.44 albertel 272: 'options','width');
1.2 albertel 273: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.1 albertel 274: }
1.63 albertel 275:
1.1 albertel 276: return $result;
277: }
278:
1.6 albertel 279: sub end_organicresponse {
1.1 albertel 280: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
281: my $result;
1.64 albertel 282:
283: my $partid = $Apache::inputtags::part;
284: my $id = $Apache::inputtags::response['-1'];
285:
286: if ($target eq 'grade'
287: && &Apache::response::submitted()
288: && $Apache::lonhomework::type eq 'exam') {
289:
290: &Apache::response::scored_response($partid,$id);
291:
292: } elsif ($target eq 'grade'
293: && &Apache::response::submitted()
294: && $Apache::lonhomework::type ne 'exam') {
295:
1.31 albertel 296: &Apache::response::setup_params($$tagstack[-1],$safeeval);
1.1 albertel 297: my $response = &Apache::response::getresponse();
298: if ( $response =~ /[^\s]/) {
1.13 albertel 299: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.1 albertel 300: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
301: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
302: my $ad;
1.13 albertel 303: foreach my $answer (@answers) {
304: &Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
305: if ($response eq $answer) {
306: $ad='EXACT_ANS';
307: last;
308: } else {
309: $ad='INCORRECT';
310: }
1.1 albertel 311: }
1.42 albertel 312: if ($ad && $Apache::lonhomework::type eq 'survey') {
313: $ad='SUBMITTED';
314: }
1.1 albertel 315: &Apache::response::handle_previous(\%previous,$ad);
316: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1.50 albertel 317: $Apache::lonhomework::results{"resource.$partid.$id.molecule"}=$env{"form.MOLECULE_$id"};
1.1 albertel 318: }
1.2 albertel 319: } elsif ($target eq "edit") {
320: $result.= &Apache::edit::tag_end($target,$token,'');
1.15 albertel 321: } elsif ($target eq 'answer') {
322: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
323: $safeeval);
324: $result.=&Apache::response::answer_header('organicresponse');
325: foreach my $answer (@answers) {
326: $result.=&Apache::response::answer_part('organicresponse',$answer);
327: }
328: $result.=&Apache::response::answer_footer('organicresponse');
1.1 albertel 329: }
1.73 albertel 330: if ($target eq 'web') {
331: &Apache::response::setup_prior_tries_hash(\&format_prior_answer_organic,
332: ['molecule'])
333: }
1.63 albertel 334:
335: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
336: $target eq 'tex' || $target eq 'analyze') {
1.75 foxr 337: &Apache::lonxml::increment_counter(&Apache::response::repetition(),
338: "$partid.$id"); # part.response
1.74 foxr 339: if ($target eq 'analyze') {
1.76 raeburn 340: $Apache::lonhomework::analyze{"$partid.$id.type"} = 'organicresponse';
1.74 foxr 341: &Apache::lonhomework::set_bubble_lines();
342: }
1.63 albertel 343: }
1.79 riegler 344: if (($target eq 'web' ) && ($Apache::lonhomework::type ne 'exam') && ($Apache::inputtags::status['-1'] eq 'CAN_ANSWER')){
345: my $options=&Apache::lonxml::get_param('options',$parstack,
346: $safeeval);
347: my $shown_text;
348: if (&Apache::response::show_answer()) {
349: $shown_text="Show Your Last Answer";
350: }
351: my $molecule;
352: if (defined($Apache::lonhomework::history{"resource.$partid.$id.molecule"})) {
353: $molecule=$Apache::lonhomework::history{"resource.$partid.$id.molecule"};
354: } else {
355: $molecule=&Apache::lonxml::get_param('molecule',$parstack,
356: $safeeval);
357: }
358: $result.=&separate_jme_window("HWVAL_$id","MOLECULE_$id",$molecule,
359: $options,$shown_text);
360: }
1.63 albertel 361: &Apache::response::end_response();
1.1 albertel 362: return $result;
363: }
364:
1.73 albertel 365: sub format_prior_answer_organic {
366: my ($mode,$answer,$other_data) = @_;
367: my $result=&mt('Smile representation: "[_1]"','<tt>'.$answer.'</tt>');
368: my $jme=$other_data->[0];
369: $result.=&jme_img($jme,$answer,400);
370: return $result;
371: }
372:
1.6 albertel 373: sub start_organicstructure {
1.3 albertel 374: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
375: my $result;
376: if ($target eq 'web') {
377: my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
378: my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
1.12 albertel 379: my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.23 albertel 380: my $id=&Apache::loncommon::get_cgi_id();
1.19 albertel 381: $result="<img src='/cgi-bin/convertjme.pl?$id'";
382: if ($options =~ /border/) { $result.= ' border="1"'; }
383: $result.=' />';
1.16 albertel 384: &Apache::lonnet::appenv(
1.78 raeburn 385: {'cgi.'.$id.'.JME' => &escape($molecule),
386: 'cgi.'.$id.'.PNG' => 1,
387: 'cgi.'.$id.'.WIDTH' => $width});
1.17 albertel 388: } elsif ($target eq 'tex') {
1.38 albertel 389: my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1);
1.58 foxr 390: my $webwidth=&Apache::lonxml::get_param('width', $parstack, $safeeval);
391: my $webheight=&Apache::lonxml::get_param('height', $parstack, $safeeval);
1.62 foxr 392: if (!$webheight) { $webheight = $webwidth; }
1.17 albertel 393: if (!$texwidth) { $texwidth='90'; }
1.58 foxr 394: $result = "%DYNAMICIMAGE:$webwidth:$webheight:$texwidth\n";
1.17 albertel 395: my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
396: my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.50 albertel 397: my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
1.17 albertel 398: '_'.time.'_'.$$.int(rand(1000)).'_organicstructure';
399: my $id=$filename;
400: &Apache::lonnet::appenv(
1.78 raeburn 401: {'cgi.'.$id.'.JME' => &escape($molecule),
402: 'cgi.'.$id.'.PS' => 1,
403: 'cgi.'.$id.'.WIDTH' => $texwidth});
1.66 www 404: $id=&escape($id);
1.17 albertel 405: &Apache::lonxml::register_ssi("/cgi-bin/convertjme.pl?$id");
1.20 albertel 406: if ($options =~ /border/) { $result.= '\fbox{'; }
407: $result .= '\graphicspath{{/home/httpd/perl/tmp/}}\includegraphics[width='.$texwidth.' mm]{'.$filename.'.eps}';
408: if ($options =~ /border/) { $result.= '} '; }
1.3 albertel 409: } elsif ($target eq 'edit') {
410: $result .=&Apache::edit::tag_start($target,$token);
1.22 albertel 411: $result .=&Apache::edit::text_arg('Width (pixels):','width',$token,5);
412: $result .=&Apache::edit::text_arg('TeXwidth (mm):','texwidth',$token,5);
1.12 albertel 413: $result .='<nobr>';
1.3 albertel 414: $result .=&Apache::edit::text_arg('Molecule:','molecule',$token,40);
415: my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
416: $safeeval);
1.12 albertel 417: my $options=&Apache::lonxml::get_param('options',$parstack,
418: $safeeval);
419: if ($options !~ /reaction/) {
420: $options.= ',multipart,number';
421: }
422:
1.30 www 423: $result .=&separate_jme_window(undef,
1.12 albertel 424: &Apache::edit::html_element_name('molecule'),
425: $molecule,$options);
426: $result.="</nobr><br />";
427: $result .=&Apache::edit::checked_arg('Options:','options',
1.18 albertel 428: [ ['reaction','Is a reaction'],
1.12 albertel 429: ['border','Draw a border'] ],
430: $token);
1.24 albertel 431: $result .=&Apache::edit::end_row();
1.3 albertel 432: } elsif ($target eq 'modified') {
433: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
434: $safeeval,'molecule',
1.22 albertel 435: 'width','texwidth',
436: 'options');
1.3 albertel 437: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
438: }
439: return $result;
1.1 albertel 440: }
441:
1.6 albertel 442: sub end_organicstructure {
1.3 albertel 443: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
444: my $result;
445: if ($target eq "edit") {
446: $result.= &Apache::edit::tag_end($target,$token,'');
447: }
1.4 albertel 448: return $result;
449: }
450:
1.9 albertel 451: sub edit_reaction_button {
1.11 albertel 452: my ($id,$field,$reaction)=@_;
1.66 www 453: my $id_es=&escape($id);
454: my $field_es=&escape($field);
455: my $reaction_es=&escape($reaction);
1.41 www 456: my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
1.80 riegler 457: my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
458: my $display=&mt('Edit Answer');
1.65 albertel 459: my $start_page =
460: &Apache::loncommon::start_page('LON-CAPA Reaction Editor',undef,
461: {'frameset' => 1,
462: 'js_ready' => 1,
463: 'add_entries' => {
1.68 albertel 464: 'rows' => "30%,*",
1.65 albertel 465: 'border' => "0",}},);
466: my $end_page =
467: &Apache::loncommon::end_page({'frameset' => 1,
468: 'js_ready' => 1});
1.9 albertel 469: my $result=<<EDITREACTION;
1.10 albertel 470: <script type="text/javascript">
1.47 albertel 471: // <!--
1.10 albertel 472: function create_reaction_window_${id}_${field} () {
473: editor=window.open('','','width=500,height=270,scrollbars=no,resizable=yes');
1.41 www 474: editor.$docopen;
1.65 albertel 475: editor.document.writeln('$start_page <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" /> $end_page');
1.68 albertel 476: editor.document.close();
1.10 albertel 477: }
1.47 albertel 478: // -->
1.10 albertel 479: </script>
1.80 riegler 480: <a href="javascript:create_reaction_window_${id}_${field}();void(0);"><img class="stift" src='$iconpath/stift.gif' alt='$display' title='$display' /></a>
1.9 albertel 481: EDITREACTION
482: return $result;
483: }
484:
1.4 albertel 485: sub start_reactionresponse {
486: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
487: my $result;
488: my $id = &Apache::response::start_response($parstack,$safeeval);
1.10 albertel 489: if ($target eq 'meta') {
1.28 albertel 490: $result=&Apache::response::meta_package_write('reactionresponse');
1.10 albertel 491: } elsif ($target eq 'web') {
1.11 albertel 492: my $partid = $Apache::inputtags::part;
493: my $id = $Apache::inputtags::response['-1'];
1.33 albertel 494: if ( &Apache::response::show_answer() ) {
495: my $ans=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
1.51 albertel 496: if (!$Apache::lonxml::default_homework_loaded) {
497: &Apache::lonxml::default_homework_load($safeeval);
498: }
499: @Apache::scripttag::parser_env = @_;
1.71 albertel 500: $Apache::inputtags::answertxt{$id}=[&Apache::run::run("return &chemparse(q\0$ans\0);",$safeeval)];
1.33 albertel 501: }
1.4 albertel 502: } elsif ($target eq "edit") {
1.9 albertel 503: $result .=&Apache::edit::tag_start($target,$token);
504: my $answer=&Apache::lonxml::get_param('answer',$parstack,
505: $safeeval);
1.82 ! raeburn 506: $result .='<span class="LC_nobreak">'.
1.10 albertel 507: &Apache::edit::text_arg('Answer:','answer',$token,40);
1.82 ! raeburn 508: $result .=&edit_reaction_button($id,&Apache::edit::html_element_name('answer'),$answer).'</span>';
1.35 albertel 509: my $initial=&Apache::lonxml::get_param('initial',$parstack,$safeeval);
1.82 ! raeburn 510: $result.='<span class="LC_nobreak">'.
1.43 albertel 511: &Apache::edit::text_arg('Initial Reaction:','initial',$token,40);
1.82 ! raeburn 512: $result .=&edit_reaction_button($id,&Apache::edit::html_element_name('initial'),$initial).'</span>';
1.9 albertel 513: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.10 albertel 514: } elsif ($target eq 'modified') {
515: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.35 albertel 516: $safeeval,'answer',
517: 'initial');
1.10 albertel 518: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.4 albertel 519: }
520: return $result;
521: }
522:
523: sub end_reactionresponse {
524: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
525: my $result;
1.64 albertel 526:
527: my $partid = $Apache::inputtags::part;
528: my $id = $Apache::inputtags::response['-1'];
529:
530: if ($target eq 'grade'
531: && &Apache::response::submitted()
532: && $Apache::lonhomework::type eq 'exam') {
533:
534: &Apache::response::scored_response($partid,$id);
535:
536: } elsif ($target eq 'grade'
537: && &Apache::response::submitted()
538: && $Apache::lonhomework::type ne 'exam') {
539:
1.31 albertel 540: &Apache::response::setup_params($$tagstack[-1],$safeeval);
1.10 albertel 541: my $response = &Apache::response::getresponse();
542: if ( $response =~ /[^\s]/) {
1.15 albertel 543: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.10 albertel 544: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
545: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
546: my $ad;
1.13 albertel 547: foreach my $answer (@answers) {
548: &Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
1.34 albertel 549: if (&chem_standard_order($response) eq
550: &chem_standard_order($answer)) {
1.13 albertel 551: $ad='EXACT_ANS';
552: } else {
553: $ad='INCORRECT';
554: }
1.10 albertel 555: }
1.42 albertel 556: if ($ad && $Apache::lonhomework::type eq 'survey') {
557: $ad='SUBMITTED';
558: }
1.10 albertel 559: &Apache::response::handle_previous(\%previous,$ad);
560: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
561: }
562: } elsif ($target eq "edit") {
1.4 albertel 563: $result.= &Apache::edit::tag_end($target,$token,'');
1.15 albertel 564: } elsif ($target eq 'answer') {
565: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
566: $safeeval);
567: $result.=&Apache::response::answer_header('reactionresponse');
568: foreach my $answer (@answers) {
569: $result.=&Apache::response::answer_part('reactionresponse',
570: $answer);
571: }
572: $result.=&Apache::response::answer_footer('reactionresponse');
1.4 albertel 573: }
1.72 albertel 574: if ($target eq 'web') {
575: &Apache::response::setup_prior_tries_hash(\&format_prior_response_reaction);
576: }
1.63 albertel 577:
578: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
579: $target eq 'tex' || $target eq 'analyze') {
1.75 foxr 580: &Apache::lonxml::increment_counter(&Apache::response::repetition(), "$partid.$id");
1.77 raeburn 581: if ($target eq 'analyze') {
582: $Apache::lonhomework::analyze{"$partid.$id.type"} = 'reactionresponse';
583: &Apache::lonhomework::set_bubble_lines();
584: }
1.63 albertel 585: }
1.81 raeburn 586: my $status=$Apache::inputtags::status['-1'];
587: if (($target eq 'web') && ($Apache::lonhomework::type ne 'exam') && ($status eq 'CAN_ANSWER')) {
588: my $reaction=$Apache::lonhomework::history{"resource.$partid.$id.submission"};
589: if ($reaction eq '') { $reaction=&Apache::lonxml::get_param('initial',$parstack,$safeeval); }
590: $result.=&edit_reaction_button($id,"HWVAL_$id",$reaction);
591: }
1.63 albertel 592: &Apache::response::end_response();
1.3 albertel 593: return $result;
1.1 albertel 594: }
595:
1.72 albertel 596: sub format_prior_response_reaction {
597: my ($mode,$answer) =@_;
598: return '<span class="LC_prior_reaction">'.
599: &HTML::Entities::encode($answer,'"<>&').'</span>';
600: }
601:
1.46 albertel 602: sub start_chem {
603: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
604: my $result = '';
1.48 albertel 605: my $inside = &Apache::lonxml::get_all_text_unbalanced("/chem",$parser);
1.46 albertel 606: if ($target eq 'tex' || $target eq 'web') {
1.48 albertel 607: $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
608: if (!$Apache::lonxml::default_homework_loaded) {
609: &Apache::lonxml::default_homework_load($safeeval);
610: }
611: @Apache::scripttag::parser_env = @_;
612: $result=&Apache::run::run("return &chemparse(q\0$inside\0);",$safeeval);
1.46 albertel 613: }
614: return $result;
615: }
616:
617: sub end_chem {
618: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
619: my $result = '';
620: return $result;
621: }
622:
1.1 albertel 623: 1;
624: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>