1: # The LearningOnline Network
2: # Simple Problem Parameter Setting "Editor"
3: #
4: # $Id: lonsimpleproblemedit.pm,v 1.34 2012/11/09 03:28:01 raeburn 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::lonsimpleproblemedit;
30:
31: use strict;
32: use Apache::Constants qw(:common :http);
33: use Apache::loncommon;
34: use Apache::lonnet;
35: use Apache::lonlocal;
36: use Apache::lonnavmaps;
37: use lib '/home/httpd/lib/perl/';
38: use LONCAPA;
39:
40:
41: my %qparms;
42: my $prefix;
43: my $qtype;
44:
45: sub evaloptionhash {
46: my $options=shift;
47: $options=~s/^\(\'//;
48: $options=~s/\'\)$//;
49: my %returnhash=();
50: foreach (split(/\'\,\'/,$options)) {
51: $returnhash{$_}=$_;
52: }
53: return %returnhash;
54: }
55:
56: sub rawrendering {
57: my ($symb)=@_;
58: my %data=('show_errors'=>'on',
59: 'simple_edit_button' => 'off',
60: 'devalidatecourseresdata'=>'on');
61: return &Apache::loncommon::get_student_view($symb,time,time,
62: $env{'request.course.id'},
63: 'web',\%data);
64: }
65:
66: sub questiontext {
67: my $text=$qparms{$prefix.'questiontext'};
68: my $qt=&mt('Question Text');
69: my $spell_link=
70: &Apache::lonhtmlcommon::spelllink('simpleedit','questiontext');
71: return (<<ENDQUESTION);
72: <table bgcolor="#dddd22" cellspacing="4" cellpadding="2" style="width:100%">
73: <tr><td><b>$qt</b><br />
74: <textarea style="width:99%" name="questiontext" cols="80" rows="8">$text</textarea>
75: <br />$spell_link
76: </td></tr>
77: </table>
78: <br />
79: ENDQUESTION
80: }
81:
82: sub hint {
83: my $text=$qparms{$prefix.'hinttext'};
84: my $ht=&mt('Hint Text');
85: my $spell_link=
86: &Apache::lonhtmlcommon::spelllink('simpleedit','hinttext');
87: return (<<ENDHINT);
88: <table bgcolor="#accacc" cellspacing="4" cellpadding="2" style="width:100%">
89: <tr><td><b>$ht</b><br />
90: <textarea style="width:99%" name="hinttext" cols="80" rows="4">$text</textarea>
91: <br />$spell_link
92: </td></tr>
93: </table>
94: <br />
95: ENDHINT
96: }
97:
98: sub script {
99: my $text=$qparms{$prefix.'numericalscript'};
100: my $ht=&mt('Scripting (optional)');
101: return (<<ENDSCRIPT);
102: <table bgcolor="#ccccaa" cellspacing="4" cellpadding="2" style="width:100%">
103: <tr><td><b>$ht</b><br />
104: <textarea style="width:99%" name="numericalscript" cols="80" rows="4">$text</textarea>
105: </td></tr>
106: </table>
107: <br />
108: ENDSCRIPT
109: }
110:
111: sub foil {
112: my $number=shift;
113: my (%values,%defaultvalues,%customvalues);
114: %defaultvalues = &Apache::lonlocal::texthash(
115: 'unused' => 'Not shown, not used'
116: );
117: if ($qtype eq 'radio') {
118: %customvalues = &Apache::lonlocal::texthash(
119: 'true' => 'True',
120: 'false' => 'False'
121: );
122: } elsif ($qtype eq 'option') {
123: %customvalues=&evaloptionhash($qparms{$prefix.'options'});
124: }
125: %values = (%defaultvalues,%customvalues);
126: my $value=$qparms{$prefix.'value'.$number};
127: unless (defined($value)) { $value='unused'; }
128: unless ($values{$value}) { $value='unused'; }
129: my $position=$qparms{$prefix.'position'.$number};
130: my %positions=('random' => 'Random position',
131: 'top' => 'Show always at top position',
132: 'bottom' => 'Show always at bottom position');
133: unless (defined($position)) { $position='random'; }
134: unless ($positions{$position}) {
135: $position='random';
136: }
137: my $selectvalue=&Apache::loncommon::select_form(
138: $value,
139: 'value'.$number,
140: \%values);
141: my $selectposition=&Apache::loncommon::select_form(
142: $position,
143: 'position'.$number,
144: {&Apache::lonlocal::texthash(%positions)});
145: my $text=$qparms{$prefix.'text'.$number};
146: my %lt=&Apache::lonlocal::texthash('foil' => 'Foil',
147: 'value' => 'Value',
148: 'pos' => 'Position',
149: 'text' => 'Text');
150:
151: my $spell_link=
152: &Apache::lonhtmlcommon::spelllink('simpleedit',"text$number");
153: return (<<ENDFOIL);
154: <table bgcolor="#E8D8EE" cellspacing="2" cellpadding="1" style="width:100%">
155: <tr><td colspan="2"><b>$lt{'foil'}</b></td></tr>
156: <tr><td>$lt{'value'}: $selectvalue</td><td>$lt{'pos'}: $selectposition</td></tr>
157: <tr><td colspan="2">$lt{'text'}:<br />
158: <textarea style="width:99%" name="text$number" cols="80" rows="4">$text</textarea>
159: <br />$spell_link
160: </td></tr>
161: </table>
162: <br />
163: ENDFOIL
164: }
165:
166: sub get_parent_uri {
167: my ($cur_symb)=@_;
168: my $navmap = Apache::lonnavmaps::navmap->new();
169: if (defined($navmap)) {
170: my $it = $navmap->getIterator(undef, undef, undef, 1);
171: while ( my $res=$it->next()) {
172: if (ref($res) && $res->symb() eq $cur_symb) { last; }
173: }
174: my ($src,$symb,$anchor)=&Apache::lonnavmaps::getLinkForResource($it->getStack());
175: if (defined($anchor)) { $anchor='#'.$anchor; }
176: return $src.'?symb='.&escape($symb).$anchor;
177: }
178: return;
179: }
180:
181: sub handler {
182: my $r = shift;
183:
184: if ($r->header_only) {
185: &Apache::loncommon::content_type($r,'text/html');
186: $r->send_http_header;
187: return OK;
188: }
189:
190: # -------------------------------------------------------------------- Allowed?
191: unless (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
192: return HTTP_NOT_ACCEPTABLE;
193: }
194: # ----------------------------------------------------------------- Send header
195: &Apache::loncommon::content_type($r,'text/html');
196: $r->send_http_header;
197: # ----------------------------------------------------- Figure out where we are
198: my $uri=$r->uri;
199: $uri=~s/\/smpedit$//;
200: my $symb=&Apache::lonnet::symbread();
201:
202: # ------------------------------------------------ Prefix for everything stored
203: $prefix=$env{'request.course.id'}.'.'.$symb.'.0.';
204: #------------------------------------------------- Prefix for storing weight of Problem Parts
205: my $weightprefix=$env{'request.course.id'}.'.'.$symb.'.';
206:
207: # ---------------------------------------------------------- Anything to store?
208: my $storeresult;
209: if (($symb) && (defined($env{'form.questiontype'}))) {
210: my %storecontent=();
211: undef %storecontent;
212: if ($env{'form.questiontype'} eq 'option') {
213: my %curoptions=&evaloptionhash($env{'form.options'});
214: if ($env{'form.delopt'}) {
215: delete $curoptions{$env{'form.delopt'}};
216: }
217: if ($env{'form.newopt'}) {
218: $env{'form.newopt'}=~s/\'/\\\'/g;
219: $curoptions{$env{'form.newopt'}}=$env{'form.newopt'};
220: }
221: $env{'form.options'}="('".join("','",keys %curoptions)."')";
222: }
223: $env{'form.hiddenparts'}='!'.$env{'form.questiontype'};
224: foreach (keys %env) {
225: if ($_=~/^form\.(\w+)$/) {
226: my $parm=$1;
227: $storecontent{$prefix.$parm}=$env{'form.'.$parm};
228: $storecontent{$prefix.$parm}=~s/^\s+//s;
229: $storecontent{$prefix.$parm}=~s/\s+$//s;
230: }
231: }
232: # ---------------------------------------------------- Set weights of hidden parts to zero
233: # ------------------------------------------------------ and used part to 1
234: $storecontent{$weightprefix.'essay.weight'}=0;
235: $storecontent{$weightprefix.'numerical.weight'}=0;
236: $storecontent{$weightprefix.'option.weight'}=0;
237: $storecontent{$weightprefix.'radio.weight'}=0;
238: $storecontent{$weightprefix.'string.weight'}=0;
239: $storecontent{$weightprefix.$env{'form.questiontype'}.'.weight'}=1;
240:
241:
242: my $reply=&Apache::lonnet::cput
243: ('resourcedata',\%storecontent,
244: $env{'course.'.$env{'request.course.id'}.'.domain'},
245: $env{'course.'.$env{'request.course.id'}.'.num'});
246:
247: &Apache::lonnet::devalidatecourseresdata(
248: $env{'course.'.$env{'request.course.id'}.'.num'},
249: $env{'course.'.$env{'request.course.id'}.'.domain'});
250: if ($reply eq 'ok') {
251: if ($env{'form.forceview'}) {
252: my $dest = &get_parent_uri($symb);
253: if ($dest) {
254: $r->internal_redirect($dest);
255: }
256: }
257: } else {
258: $storeresult = $reply;
259: }
260: }
261: # ------------------------------------------------------------------- Read Data
262:
263: %qparms=&Apache::lonnet::dump('resourcedata',
264: $env{'course.'.$env{'request.course.id'}.'.domain'},
265: $env{'course.'.$env{'request.course.id'}.'.num'},
266: $env{'request.course.id'}.'.'.$symb);
267:
268: my $js = <<"ENDJS";
269:
270: <script type="text/javascript">
271: // <![CDATA[
272:
273: function setForceView() {
274: if (document.getElementById('spview')) {
275: document.getElementById('spview').value = 1;
276: }
277: return true;
278: }
279: // END LON-CAPA Internal -->
280: // ]]>
281: </script>
282:
283: ENDJS
284:
285: # ------------------------------------------------------------ Print the screen
286: my $spell_header=&Apache::lonhtmlcommon::spellheader();
287: $r->print(&Apache::loncommon::start_page('Simple Problem Editor',
288: $spell_header.$js));
289: if ($symb) {
290: my $title='<h1>'.&Apache::lonnet::gettitle($symb).'</h1>';
291: if (&get_parent_uri($symb)) {
292: $r->print($title);
293: if ($storeresult) {
294: $r->print('<p class="LC_error">'.&mt('An error: [_1] occurred saving your changes',$storeresult).'</p>');
295: }
296: } else {
297: $r->print($title
298: .'<p class="LC_error">'
299: .&mt('An error occurred retrieving the link to this problem.')
300: .'<br />'
301: .&mt('You may need to [_1]re-select the course[_2] and then return to this resource to view it.'
302: ,'<a href="/adm/roles">','</a>')
303: .'</p>'
304: );
305: }
306: $r->print('<p class="LC_warning">'
307: .&mt('Note: it can take up to 10 minutes for changes to take effect for all users.')
308: .&Apache::loncommon::help_open_topic('Caching')
309: .'</p>'
310: );
311:
312: $r->print(
313: '<div class="LC_Box">'
314: .&rawrendering($symb)
315: .'</div>'
316: );
317: $r->print('<form name="simpleedit" method="post">');
318: # Question Type
319: my %questiontypes=(
320: 'radio' => '1 out of N multiple choice (radio button)',
321: 'option' => 'Option Response',
322: 'string' => 'Short string response',
323: 'essay' => 'Essay, open end',
324: 'numerical' => 'Numerical Response');
325: $qtype=$qparms{$prefix.'questiontype'};
326: unless (defined($qtype)) { $qtype='radio'; }
327: unless ($questiontypes{$qtype}) { $qtype='radio'; }
328: $r->print(
329: '<fieldset style="width:400px;">'
330: .'<legend>'.&mt('Question Type').'</legend>'
331: .&Apache::loncommon::select_form(
332: $qtype,
333: 'questiontype',
334: {&Apache::lonlocal::texthash(%questiontypes)})
335: .'</fieldset>'
336: );
337: $r->print(
338: '<p>'
339: .'<input type="hidden" name="forceview" value="" id="spview" />'
340: .'<input type="submit" value="'.&mt('Save and Edit').'" />'
341: .(' ' x3)
342: .'<input type="submit" value="'.&mt('Save and View').'" onclick="javascript:setForceView();" />'
343: .'</p>'
344: );
345: # Script
346: if ($qtype eq 'numerical') {
347: $r->print(&script());
348: }
349: # Question Text
350: $r->print(&questiontext());
351: # Radio, Option ===
352: if (($qtype eq 'radio') || ($qtype eq 'option')) {
353: # Response
354: my $maxfoils=$qparms{$prefix.'maxfoils'};
355: unless (defined($maxfoils)) { $maxfoils=10; }
356: unless ($maxfoils=~/^\d+$/) { $maxfoils=10; }
357: if ($maxfoils<=0) { $maxfoils=10; }
358: my %randomizes=(
359: 'yes' => 'Display foils in random order',
360: 'no' => 'Display foils in order given');
361: my $randomize=$qparms{$prefix.'randomize'};
362: unless (defined($randomize)) { $randomize='yes'; }
363: unless ($randomizes{$randomize}) { $randomize='yes'; }
364: $r->print(
365: '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2" style="width:100%">'.
366: '<tr><td>'.&mt('Max number of foils displayed').
367: ': <input type="text" size="3" name="maxfoils" value="'.$maxfoils.'" /> '.
368: &Apache::loncommon::select_form(
369: $randomize,
370: 'randomize',
371: {&Apache::lonlocal::texthash(%randomizes)}).
372: '</td></tr><tr><td bgcolor="#F0F0F0">');
373: # Option Response: Options
374: if ($qtype eq 'option') {
375: my $options=$qparms{$prefix.'options'};
376: unless (defined($options)) { $options="('true','false')"; }
377: my %optionshash=&evaloptionhash($options);
378: $r->print(
379: '<table bgcolor="#ffcc22" cellspacing="4" cellpadding="2" style="width:100%">'.
380: '<tr><td><input type="hidden" name="options" value="'.
381: $options.'" />'.&mt('Add new option').': '.
382: '<input type="text" name="newopt" size="15" />'.
383: &mt('Delete an option').': '.
384: &Apache::loncommon::select_form('','delopt',{'' => '',%optionshash}).
385: '</td></tr><tr><td>');
386: }
387: # Foils
388: for (my $i=1;$i<=10;$i++) {
389: $r->print(&foil($i));
390: }
391: # End Options
392: if ($qtype eq 'option') {
393: $r->print('</td></tr></table>');
394: }
395:
396: # End Response
397: $r->print('</td></tr></table><br />');
398: # Hint
399: $r->print(&hint());
400: }
401: if ($qtype eq 'string') {
402: my %stringtypes=(
403: 'cs' => 'Case sensitive',
404: 'ci' => 'Case Insensitive',
405: 'mc' => 'Multiple Choice, Order of characters unchecked');
406: my $stringanswer=$qparms{$prefix.'stringanswer'};
407: unless (defined($stringanswer)) { $stringanswer=''; }
408: my $stringtype=$qparms{$prefix.'stringtype'};
409: unless (defined($stringtype)) { $stringtype='cs'; }
410: unless ($stringtypes{$stringtype}) { $stringtype='cs'; }
411: $r->print(
412: '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2" style="width:100%">'.
413: '<tr><td><label>'.&mt('Correct answer').': <input type="text" size="20" name="stringanswer" value="'.$stringanswer.'" /></label> '.
414: &Apache::loncommon::select_form(
415: $stringtype,
416: 'stringtype',
417: {&Apache::lonlocal::texthash(%stringtypes)}).
418: '</td></tr></table><br />');
419: # Hint
420: $r->print(&hint());
421: }
422: if ($qtype eq 'numerical') {
423: my $numericalanswer=$qparms{$prefix.'numericalanswer'};
424: unless (defined($numericalanswer)) { $numericalanswer=''; }
425: my $numericaltolerance=$qparms{$prefix.'numericaltolerance'};
426: unless (defined($numericaltolerance)) { $numericaltolerance='5%'; }
427: my $numericalsigfigs=$qparms{$prefix.'numericalsigfigs'};
428: unless (defined($numericalsigfigs)) { $numericalsigfigs='1,15'; }
429:
430: $r->print(
431: '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2" style="width:100%">'.
432: '<tr><td><label>'.&mt('Correct answer').': <input type="text" size="20" name="numericalanswer" value="'.$numericalanswer.'" /></label> '.
433: '<label>'.&mt('Unit').': <input type="text" size="5" name="numericalunit" value="'.$qparms{$prefix.'numericalunit'}.'" /></label> '.
434: '<label>'.&mt('Format').': <input type="text" size="5" name="numericalformat" value="'.$qparms{$prefix.'numericalformat'}.'" /></label> '.
435: '<label>'.&mt('Tolerance').': <input type="text" size="5" name="numericaltolerance" value="'.$numericaltolerance.'" /></label> '.
436: '<label>'.&mt('Significant digits').': <input type="text" size="5" name="numericalsigfigs" value="'.$numericalsigfigs.'" /></label>'.
437: '</td></tr></table><br />');
438: $r->print(&hint());
439: }
440: # Store Button
441: $r->print(
442: '<input type="submit" value="'.&mt('Save and Edit').'" />'.
443: (' ' x3).
444: '<input type="submit" value="'.&mt('Save and View').'" onclick="javascript:setForceView();" />'.
445: '</form>');
446: } else {
447: $r->print(&mt('Could not identify problem.'));
448: }
449: $r->print(&Apache::loncommon::end_page());
450: return OK;
451: }
452:
453: 1;
454: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>