Annotation of loncom/homework/chemresponse.pm, revision 1.92
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # chemical equation style response
3: #
1.92 ! bisitz 4: # $Id: chemresponse.pm,v 1.91 2013/01/15 18:09:43 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,});
1.88 www 119: my $java_not_enabled=&Apache::lonhtmlcommon::java_not_enabled();
1.65 albertel 120: my $body=<<CHEMPAGE;
1.69 albertel 121: $js
1.1 albertel 122: <center>
1.92 ! bisitz 123: <form action="">
1.60 albertel 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.88 www 146: $java_not_enabled
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 =
1.91 raeburn 168: 'LONCAPA_draw_molecule_'.&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.86 raeburn 179: CHEMINPUT
180: if ($shown_text eq '') {
181: $result .=<<PENCIL;
1.79 riegler 182: <a href="javascript:$function();void(0);"><img class="stift" src='$iconpath/stift.gif' alt='$display' title='$display' /></a>
1.86 raeburn 183: PENCIL
184: } else {
185: $result .= '<input type="button" value="'.&mt($shown_text).'" onclick="javascript:'.$function.'();void(0);" />';
186: }
1.1 albertel 187: return $result;
188: }
1.56 albertel 189: sub jme_img {
190: my ($jme,$smile,$width,$options)=@_;
191: my $id=&Apache::loncommon::get_cgi_id();
192: my $result='<img alt="'.$smile.'" src="/cgi-bin/convertjme.pl?'.$id.'"';
193: if ($options =~ /border/) { $result.= ' border="1"'; }
194: $result.=' />';
1.78 raeburn 195: &Apache::lonnet::appenv({'cgi.'.$id.'.JME' =>
196: &escape($jme),
197: 'cgi.'.$id.'.PNG' => 1,
198: 'cgi.'.$id.'.WIDTH' => $width});
1.56 albertel 199: return $result;
200: }
201:
1.6 albertel 202: sub start_organicresponse {
1.1 albertel 203: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
204: my $result;
205: my $partid = $Apache::inputtags::part;
206: my $id = &Apache::response::start_response($parstack,$safeeval);
207: if ($target eq 'meta') {
1.28 albertel 208: $result=&Apache::response::meta_package_write('organicresponse');
1.1 albertel 209: } elsif ($target eq 'web') {
1.55 albertel 210: my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
211: $safeeval);
1.86 raeburn 212: if (&Apache::response::show_answer()) {
213: my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
214: $safeeval);
215: if ($jmeanswer ne '') {
216: my $options=&Apache::lonxml::get_param('options',$parstack,
217: $safeeval);
218: my $width=&Apache::lonxml::get_param('width',$parstack,
219: $safeeval);
220: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
221: $safeeval);
222: $result.=&jme_img($jmeanswer,$answers[0],$width,$options);
223: }
1.1 albertel 224: } else {
1.44 albertel 225: $result.= '<input type="hidden" name="MOLECULE_'.$id.'" value="" />';
1.1 albertel 226: }
1.2 albertel 227: } elsif ($target eq 'edit') {
228: $result .=&Apache::edit::tag_start($target,$token);
1.12 albertel 229: my $options=&Apache::lonxml::get_param('options',$parstack,
230: $safeeval);
231: if ($options !~ /multipart/) { $options.=',multipart'; }
1.83 bisitz 232: $result .='<span class="LC_nobreak">'.
1.7 albertel 233: &Apache::edit::text_arg('Starting Molecule:','molecule',
234: $token,40);
1.2 albertel 235: my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
236: $safeeval);
1.30 www 237: $result .=&separate_jme_window(undef,
1.2 albertel 238: &Apache::edit::html_element_name('molecule'),
1.12 albertel 239: $molecule,$options);
1.83 bisitz 240: $result .='</span><br /><span class="LC_nobreak">';
1.2 albertel 241: $result .=&Apache::edit::text_arg('Correct Answer:','answer',
242: $token,40);
1.85 raeburn 243: $result .='</span><br /><span class="LC_nobreak">';
1.86 raeburn 244: $result .=&Apache::edit::text_arg('JME string of the answer - automatically updated by "Insert Answer" in the JME pop-up (click pencil):',
1.67 albertel 245: 'jmeanswer',$token);
1.2 albertel 246: my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
247: $safeeval);
1.30 www 248: $result .=&separate_jme_window(
1.2 albertel 249: &Apache::edit::html_element_name('answer'),
250: &Apache::edit::html_element_name('jmeanswer'),
1.12 albertel 251: $jmeanswer,$options);
1.83 bisitz 252: $result .='</span><br />';
1.12 albertel 253: $result .=&Apache::edit::checked_arg('Options:','options',
1.29 www 254: [ ['autoez','Auto E,Z stereochemistry'],
1.18 albertel 255: ['multipart','Multipart Structures'],
1.12 albertel 256: ['nostereo','No stereochemistry'],
257: ['reaction','Is a reaction'],
1.18 albertel 258: ['number','Able to number atoms'] ],
1.12 albertel 259: ,$token);
1.44 albertel 260: $result .=&Apache::edit::text_arg('Width of correct answer image:',
261: 'width',$token,10);
1.2 albertel 262: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
263: } elsif ($target eq 'modified') {
264: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
265: $safeeval,'molecule',
1.6 albertel 266: 'answer','jmeanswer',
1.44 albertel 267: 'options','width');
1.2 albertel 268: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.1 albertel 269: }
1.63 albertel 270:
1.1 albertel 271: return $result;
272: }
273:
1.6 albertel 274: sub end_organicresponse {
1.1 albertel 275: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
276: my $result;
1.64 albertel 277:
278: my $partid = $Apache::inputtags::part;
279: my $id = $Apache::inputtags::response['-1'];
280:
281: if ($target eq 'grade'
282: && &Apache::response::submitted()
283: && $Apache::lonhomework::type eq 'exam') {
284:
285: &Apache::response::scored_response($partid,$id);
286:
287: } elsif ($target eq 'grade'
288: && &Apache::response::submitted()
289: && $Apache::lonhomework::type ne 'exam') {
290:
1.31 albertel 291: &Apache::response::setup_params($$tagstack[-1],$safeeval);
1.1 albertel 292: my $response = &Apache::response::getresponse();
293: if ( $response =~ /[^\s]/) {
1.13 albertel 294: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.1 albertel 295: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
296: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
297: my $ad;
1.13 albertel 298: foreach my $answer (@answers) {
299: &Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
300: if ($response eq $answer) {
301: $ad='EXACT_ANS';
302: last;
303: } else {
304: $ad='INCORRECT';
305: }
1.1 albertel 306: }
1.87 raeburn 307: if ($ad) {
308: if ($Apache::lonhomework::type eq 'survey') {
309: $ad='SUBMITTED';
310: } elsif ($Apache::lonhomework::type eq 'surveycred') {
311: $ad='SUBMITTED_CREDIT';
312: } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
313: $ad='ANONYMOUS';
314: } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
315: $ad='ANONYMOUS_CREDIT';
316: }
317: }
1.1 albertel 318: &Apache::response::handle_previous(\%previous,$ad);
319: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1.50 albertel 320: $Apache::lonhomework::results{"resource.$partid.$id.molecule"}=$env{"form.MOLECULE_$id"};
1.1 albertel 321: }
1.2 albertel 322: } elsif ($target eq "edit") {
323: $result.= &Apache::edit::tag_end($target,$token,'');
1.15 albertel 324: } elsif ($target eq 'answer') {
325: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
326: $safeeval);
327: $result.=&Apache::response::answer_header('organicresponse');
328: foreach my $answer (@answers) {
329: $result.=&Apache::response::answer_part('organicresponse',$answer);
330: }
331: $result.=&Apache::response::answer_footer('organicresponse');
1.1 albertel 332: }
1.73 albertel 333: if ($target eq 'web') {
334: &Apache::response::setup_prior_tries_hash(\&format_prior_answer_organic,
335: ['molecule'])
336: }
1.63 albertel 337:
338: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
339: $target eq 'tex' || $target eq 'analyze') {
1.90 raeburn 340: my $repetition = &Apache::response::repetition();
341: &Apache::lonxml::increment_counter($repetition,"$partid.$id"); # part.response
1.74 foxr 342: if ($target eq 'analyze') {
1.76 raeburn 343: $Apache::lonhomework::analyze{"$partid.$id.type"} = 'organicresponse';
1.84 raeburn 344: push (@{ $Apache::lonhomework::analyze{"parts"} },"$partid.$id");
1.74 foxr 345: &Apache::lonhomework::set_bubble_lines();
346: }
1.63 albertel 347: }
1.86 raeburn 348: if ($target eq 'web' ) {
349: my ($showpencil,$shown_text);
350: if ($Apache::inputtags::status['-1'] eq 'CAN_ANSWER') {
351: $showpencil = 1;
352: } elsif (&Apache::response::show_answer()) {
353: my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack, $safeeval);
354: if ($jmeanswer eq '') {
355: $showpencil = 1;
356: $shown_text="Show Your Last Answer";
357: }
358: }
359: if ($showpencil) {
1.79 riegler 360: my $options=&Apache::lonxml::get_param('options',$parstack,
361: $safeeval);
1.86 raeburn 362:
1.79 riegler 363: my $molecule;
364: if (defined($Apache::lonhomework::history{"resource.$partid.$id.molecule"})) {
365: $molecule=$Apache::lonhomework::history{"resource.$partid.$id.molecule"};
366: } else {
367: $molecule=&Apache::lonxml::get_param('molecule',$parstack,
368: $safeeval);
369: }
1.86 raeburn 370: $result.=&separate_jme_window("HWVAL_$id","MOLECULE_$id",$molecule,
371: $options,$shown_text);
372: }
1.79 riegler 373: }
1.63 albertel 374: &Apache::response::end_response();
1.1 albertel 375: return $result;
376: }
377:
1.73 albertel 378: sub format_prior_answer_organic {
379: my ($mode,$answer,$other_data) = @_;
380: my $result=&mt('Smile representation: "[_1]"','<tt>'.$answer.'</tt>');
381: my $jme=$other_data->[0];
382: $result.=&jme_img($jme,$answer,400);
383: return $result;
384: }
385:
1.6 albertel 386: sub start_organicstructure {
1.3 albertel 387: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
388: my $result;
389: if ($target eq 'web') {
390: my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
391: my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
1.12 albertel 392: my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.23 albertel 393: my $id=&Apache::loncommon::get_cgi_id();
1.19 albertel 394: $result="<img src='/cgi-bin/convertjme.pl?$id'";
395: if ($options =~ /border/) { $result.= ' border="1"'; }
396: $result.=' />';
1.16 albertel 397: &Apache::lonnet::appenv(
1.78 raeburn 398: {'cgi.'.$id.'.JME' => &escape($molecule),
399: 'cgi.'.$id.'.PNG' => 1,
400: 'cgi.'.$id.'.WIDTH' => $width});
1.17 albertel 401: } elsif ($target eq 'tex') {
1.38 albertel 402: my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1);
1.58 foxr 403: my $webwidth=&Apache::lonxml::get_param('width', $parstack, $safeeval);
404: my $webheight=&Apache::lonxml::get_param('height', $parstack, $safeeval);
1.62 foxr 405: if (!$webheight) { $webheight = $webwidth; }
1.17 albertel 406: if (!$texwidth) { $texwidth='90'; }
1.58 foxr 407: $result = "%DYNAMICIMAGE:$webwidth:$webheight:$texwidth\n";
1.17 albertel 408: my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
409: my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.50 albertel 410: my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
1.17 albertel 411: '_'.time.'_'.$$.int(rand(1000)).'_organicstructure';
412: my $id=$filename;
413: &Apache::lonnet::appenv(
1.78 raeburn 414: {'cgi.'.$id.'.JME' => &escape($molecule),
415: 'cgi.'.$id.'.PS' => 1,
416: 'cgi.'.$id.'.WIDTH' => $texwidth});
1.66 www 417: $id=&escape($id);
1.17 albertel 418: &Apache::lonxml::register_ssi("/cgi-bin/convertjme.pl?$id");
1.20 albertel 419: if ($options =~ /border/) { $result.= '\fbox{'; }
1.89 foxr 420: $result .= '\graphicspath{{'.LONCAPA::tempdir().
421: '}}\includegraphics[width='.$texwidth.' mm]{'.$filename.'.eps}';
1.20 albertel 422: if ($options =~ /border/) { $result.= '} '; }
1.3 albertel 423: } elsif ($target eq 'edit') {
424: $result .=&Apache::edit::tag_start($target,$token);
1.22 albertel 425: $result .=&Apache::edit::text_arg('Width (pixels):','width',$token,5);
426: $result .=&Apache::edit::text_arg('TeXwidth (mm):','texwidth',$token,5);
1.83 bisitz 427: $result .='<span class="LC_nobreak">';
1.3 albertel 428: $result .=&Apache::edit::text_arg('Molecule:','molecule',$token,40);
429: my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
430: $safeeval);
1.12 albertel 431: my $options=&Apache::lonxml::get_param('options',$parstack,
432: $safeeval);
433: if ($options !~ /reaction/) {
434: $options.= ',multipart,number';
435: }
436:
1.30 www 437: $result .=&separate_jme_window(undef,
1.12 albertel 438: &Apache::edit::html_element_name('molecule'),
439: $molecule,$options);
1.83 bisitz 440: $result.="</span><br />";
1.12 albertel 441: $result .=&Apache::edit::checked_arg('Options:','options',
1.18 albertel 442: [ ['reaction','Is a reaction'],
1.12 albertel 443: ['border','Draw a border'] ],
444: $token);
1.24 albertel 445: $result .=&Apache::edit::end_row();
1.3 albertel 446: } elsif ($target eq 'modified') {
447: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
448: $safeeval,'molecule',
1.22 albertel 449: 'width','texwidth',
450: 'options');
1.3 albertel 451: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
452: }
453: return $result;
1.1 albertel 454: }
455:
1.6 albertel 456: sub end_organicstructure {
1.3 albertel 457: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
458: my $result;
459: if ($target eq "edit") {
460: $result.= &Apache::edit::tag_end($target,$token,'');
461: }
1.4 albertel 462: return $result;
463: }
464:
1.9 albertel 465: sub edit_reaction_button {
1.11 albertel 466: my ($id,$field,$reaction)=@_;
1.66 www 467: my $id_es=&escape($id);
468: my $field_es=&escape($field);
469: my $reaction_es=&escape($reaction);
1.41 www 470: my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
1.80 riegler 471: my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
472: my $display=&mt('Edit Answer');
1.65 albertel 473: my $start_page =
474: &Apache::loncommon::start_page('LON-CAPA Reaction Editor',undef,
475: {'frameset' => 1,
476: 'js_ready' => 1,
477: 'add_entries' => {
1.68 albertel 478: 'rows' => "30%,*",
1.65 albertel 479: 'border' => "0",}},);
480: my $end_page =
481: &Apache::loncommon::end_page({'frameset' => 1,
482: 'js_ready' => 1});
1.9 albertel 483: my $result=<<EDITREACTION;
1.10 albertel 484: <script type="text/javascript">
1.47 albertel 485: // <!--
1.10 albertel 486: function create_reaction_window_${id}_${field} () {
487: editor=window.open('','','width=500,height=270,scrollbars=no,resizable=yes');
1.41 www 488: editor.$docopen;
1.65 albertel 489: 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 490: editor.document.close();
1.10 albertel 491: }
1.47 albertel 492: // -->
1.10 albertel 493: </script>
1.80 riegler 494: <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 495: EDITREACTION
496: return $result;
497: }
498:
1.4 albertel 499: sub start_reactionresponse {
500: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
501: my $result;
502: my $id = &Apache::response::start_response($parstack,$safeeval);
1.10 albertel 503: if ($target eq 'meta') {
1.28 albertel 504: $result=&Apache::response::meta_package_write('reactionresponse');
1.10 albertel 505: } elsif ($target eq 'web') {
1.11 albertel 506: my $partid = $Apache::inputtags::part;
507: my $id = $Apache::inputtags::response['-1'];
1.33 albertel 508: if ( &Apache::response::show_answer() ) {
509: my $ans=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
1.51 albertel 510: if (!$Apache::lonxml::default_homework_loaded) {
511: &Apache::lonxml::default_homework_load($safeeval);
512: }
513: @Apache::scripttag::parser_env = @_;
1.71 albertel 514: $Apache::inputtags::answertxt{$id}=[&Apache::run::run("return &chemparse(q\0$ans\0);",$safeeval)];
1.33 albertel 515: }
1.4 albertel 516: } elsif ($target eq "edit") {
1.9 albertel 517: $result .=&Apache::edit::tag_start($target,$token);
518: my $answer=&Apache::lonxml::get_param('answer',$parstack,
519: $safeeval);
1.82 raeburn 520: $result .='<span class="LC_nobreak">'.
1.10 albertel 521: &Apache::edit::text_arg('Answer:','answer',$token,40);
1.82 raeburn 522: $result .=&edit_reaction_button($id,&Apache::edit::html_element_name('answer'),$answer).'</span>';
1.35 albertel 523: my $initial=&Apache::lonxml::get_param('initial',$parstack,$safeeval);
1.82 raeburn 524: $result.='<span class="LC_nobreak">'.
1.43 albertel 525: &Apache::edit::text_arg('Initial Reaction:','initial',$token,40);
1.82 raeburn 526: $result .=&edit_reaction_button($id,&Apache::edit::html_element_name('initial'),$initial).'</span>';
1.9 albertel 527: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.10 albertel 528: } elsif ($target eq 'modified') {
529: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.35 albertel 530: $safeeval,'answer',
531: 'initial');
1.10 albertel 532: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.4 albertel 533: }
534: return $result;
535: }
536:
537: sub end_reactionresponse {
538: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
539: my $result;
1.64 albertel 540:
541: my $partid = $Apache::inputtags::part;
542: my $id = $Apache::inputtags::response['-1'];
543:
544: if ($target eq 'grade'
545: && &Apache::response::submitted()
546: && $Apache::lonhomework::type eq 'exam') {
547:
548: &Apache::response::scored_response($partid,$id);
549:
550: } elsif ($target eq 'grade'
551: && &Apache::response::submitted()
552: && $Apache::lonhomework::type ne 'exam') {
553:
1.31 albertel 554: &Apache::response::setup_params($$tagstack[-1],$safeeval);
1.10 albertel 555: my $response = &Apache::response::getresponse();
556: if ( $response =~ /[^\s]/) {
1.15 albertel 557: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.10 albertel 558: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
559: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
560: my $ad;
1.13 albertel 561: foreach my $answer (@answers) {
562: &Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
1.34 albertel 563: if (&chem_standard_order($response) eq
564: &chem_standard_order($answer)) {
1.13 albertel 565: $ad='EXACT_ANS';
566: } else {
567: $ad='INCORRECT';
568: }
1.10 albertel 569: }
1.87 raeburn 570: if ($ad) {
571: if ($Apache::lonhomework::type eq 'survey') {
572: $ad='SUBMITTED';
573: } elsif ($ad && $Apache::lonhomework::type eq 'surveycred') {
574: $ad='SUBMITTED_CREDIT';
575: } elsif ($ad && $Apache::lonhomework::type eq 'anonsurvey') {
576: $ad='ANONYMOUS';
577: } elsif ($ad && $Apache::lonhomework::type eq 'anonsurveycred') {
578: $ad='ANONYMOUS_CREDIT';
579: }
580: }
1.10 albertel 581: &Apache::response::handle_previous(\%previous,$ad);
582: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
583: }
584: } elsif ($target eq "edit") {
1.4 albertel 585: $result.= &Apache::edit::tag_end($target,$token,'');
1.15 albertel 586: } elsif ($target eq 'answer') {
587: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
588: $safeeval);
589: $result.=&Apache::response::answer_header('reactionresponse');
590: foreach my $answer (@answers) {
591: $result.=&Apache::response::answer_part('reactionresponse',
592: $answer);
593: }
594: $result.=&Apache::response::answer_footer('reactionresponse');
1.4 albertel 595: }
1.72 albertel 596: if ($target eq 'web') {
597: &Apache::response::setup_prior_tries_hash(\&format_prior_response_reaction);
598: }
1.63 albertel 599:
600: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
601: $target eq 'tex' || $target eq 'analyze') {
1.90 raeburn 602: my $repetition = &Apache::response::repetition();
603: &Apache::lonxml::increment_counter($repetition,"$partid.$id");
1.77 raeburn 604: if ($target eq 'analyze') {
605: $Apache::lonhomework::analyze{"$partid.$id.type"} = 'reactionresponse';
1.84 raeburn 606: push (@{ $Apache::lonhomework::analyze{"parts"} },"$partid.$id");
1.77 raeburn 607: &Apache::lonhomework::set_bubble_lines();
608: }
1.63 albertel 609: }
1.81 raeburn 610: my $status=$Apache::inputtags::status['-1'];
611: if (($target eq 'web') && ($Apache::lonhomework::type ne 'exam') && ($status eq 'CAN_ANSWER')) {
612: my $reaction=$Apache::lonhomework::history{"resource.$partid.$id.submission"};
613: if ($reaction eq '') { $reaction=&Apache::lonxml::get_param('initial',$parstack,$safeeval); }
614: $result.=&edit_reaction_button($id,"HWVAL_$id",$reaction);
615: }
1.63 albertel 616: &Apache::response::end_response();
1.3 albertel 617: return $result;
1.1 albertel 618: }
619:
1.72 albertel 620: sub format_prior_response_reaction {
621: my ($mode,$answer) =@_;
622: return '<span class="LC_prior_reaction">'.
623: &HTML::Entities::encode($answer,'"<>&').'</span>';
624: }
625:
1.46 albertel 626: sub start_chem {
627: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
628: my $result = '';
1.48 albertel 629: my $inside = &Apache::lonxml::get_all_text_unbalanced("/chem",$parser);
1.46 albertel 630: if ($target eq 'tex' || $target eq 'web') {
1.48 albertel 631: $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
632: if (!$Apache::lonxml::default_homework_loaded) {
633: &Apache::lonxml::default_homework_load($safeeval);
634: }
635: @Apache::scripttag::parser_env = @_;
636: $result=&Apache::run::run("return &chemparse(q\0$inside\0);",$safeeval);
1.46 albertel 637: }
638: return $result;
639: }
640:
641: sub end_chem {
642: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
643: my $result = '';
644: return $result;
645: }
646:
1.91 raeburn 647: my $uniq=0;
648: sub get_uniq_name {
649: $uniq++;
650: return 'uniquename'.$uniq;
651: }
652:
1.1 albertel 653: 1;
654: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>