File:
[LON-CAPA] /
loncom /
homework /
lonsimpleproblemedit.pm
Revision
1.30:
download - view:
text,
annotated -
select for diffs
Wed Dec 2 18:33:20 2009 UTC (14 years, 5 months ago) by
bisitz
Branches:
MAIN
CVS tags:
HEAD,
GCI_3
Localization - Corrected "loncommon::select_form" usage:
Only translate once but translate what needs to be translated. Don't translate custom text, e.g. custom folder names.
Even avoid ISE: Unusual but possible special folder names on messages screen used to be misinterpreted.
1: # The LearningOnline Network
2: # Simple Problem Parameter Setting "Editor"
3: #
4: # $Id: lonsimpleproblemedit.pm,v 1.30 2009/12/02 18:33:20 bisitz 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='';
114: if ($qtype eq 'radio') {
115: %values=('true' => 'True', 'false' => 'False');
116: } elsif ($qtype eq 'option') {
117: %values=&evaloptionhash($qparms{$prefix.'options'});
118: }
119: $values{'unused'}='Not shown, not used';
120: my $value=$qparms{$prefix.'value'.$number};
121: unless (defined($value)) { $value='unused'; }
122: unless ($values{$value}) { $value='unused'; }
123: my $position=$qparms{$prefix.'position'.$number};
124: my %positions=('random' => 'Random position',
125: 'top' => 'Show always at top position',
126: 'bottom' => 'Show always at bottom position');
127: unless (defined($position)) { $position='random'; }
128: unless ($positions{$position}) {
129: $position='random';
130: }
131: my $selectvalue=&Apache::loncommon::select_form(
132: $value,
133: 'value'.$number,
134: &Apache::lonlocal::texthash(%values));
135: my $selectposition=&Apache::loncommon::select_form(
136: $position,
137: 'position'.$number,
138: &Apache::lonlocal::texthash(%positions));
139: my $text=$qparms{$prefix.'text'.$number};
140: my %lt=&Apache::lonlocal::texthash('foil' => 'Foil',
141: 'value' => 'Value',
142: 'pos' => 'Position',
143: 'text' => 'Text');
144:
145: my $spell_link=
146: &Apache::lonhtmlcommon::spelllink('simpleedit',"text$number");
147: return (<<ENDFOIL);
148: <table bgcolor="#E8D8EE" cellspacing="2" cellpadding="1" style="width:100%">
149: <tr><td colspan="2"><b>$lt{'foil'}</b></td></tr>
150: <tr><td>$lt{'value'}: $selectvalue</td><td>$lt{'pos'}: $selectposition</td></tr>
151: <tr><td colspan="2">$lt{'text'}:<br />
152: <textarea style="width:99%" name="text$number" cols="80" rows="4">$text</textarea>
153: <br />$spell_link
154: </td></tr>
155: </table>
156: <br />
157: ENDFOIL
158: }
159:
160: sub get_parent_uri {
161: my ($cur_symb)=@_;
162: my $navmap = Apache::lonnavmaps::navmap->new();
163: if (defined($navmap)) {
164: my $it = $navmap->getIterator(undef, undef, undef, 1);
165: while ( my $res=$it->next()) {
166: if (ref($res) && $res->symb() eq $cur_symb) { last; }
167: }
168: my ($src,$symb,$anchor)=&Apache::lonnavmaps::getLinkForResource($it->getStack());
169: if (defined($anchor)) { $anchor='#'.$anchor; }
170: return $src.'?symb='.&escape($symb).$anchor;
171: }
172: return;
173: }
174:
175: sub handler {
176: my $r = shift;
177:
178: if ($r->header_only) {
179: &Apache::loncommon::content_type($r,'text/html');
180: $r->send_http_header;
181: return OK;
182: }
183:
184: # -------------------------------------------------------------------- Allowed?
185: unless (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
186: return HTTP_NOT_ACCEPTABLE;
187: }
188: # ----------------------------------------------------------------- Send header
189: &Apache::loncommon::content_type($r,'text/html');
190: $r->send_http_header;
191: # ----------------------------------------------------- Figure out where we are
192: my $uri=$r->uri;
193: $uri=~s/\/smpedit$//;
194: my $symb=&Apache::lonnet::symbread();
195:
196: # ------------------------------------------------ Prefix for everything stored
197: $prefix=$env{'request.course.id'}.'.'.$symb.'.0.';
198: #------------------------------------------------- Prefix for storing weight of Problem Parts
199: my $weightprefix=$env{'request.course.id'}.'.'.$symb.'.';
200:
201: # ---------------------------------------------------------- Anything to store?
202:
203: if (($symb) && (defined($env{'form.questiontype'}))) {
204: my %storecontent=();
205: undef %storecontent;
206: if ($env{'form.questiontype'} eq 'option') {
207: my %curoptions=&evaloptionhash($env{'form.options'});
208: if ($env{'form.delopt'}) {
209: delete $curoptions{$env{'form.delopt'}};
210: }
211: if ($env{'form.newopt'}) {
212: $env{'form.newopt'}=~s/\'/\\\'/g;
213: $curoptions{$env{'form.newopt'}}=$env{'form.newopt'};
214: }
215: $env{'form.options'}="('".join("','",keys %curoptions)."')";
216: }
217: $env{'form.hiddenparts'}='!'.$env{'form.questiontype'};
218: foreach (keys %env) {
219: if ($_=~/^form\.(\w+)$/) {
220: my $parm=$1;
221: $storecontent{$prefix.$parm}=$env{'form.'.$parm};
222: $storecontent{$prefix.$parm}=~s/^\s+//s;
223: $storecontent{$prefix.$parm}=~s/\s+$//s;
224: }
225: }
226: # ---------------------------------------------------- Set weights of hidden parts to zero
227: # ------------------------------------------------------ and used part to 1
228: $storecontent{$weightprefix.'essay.weight'}=0;
229: $storecontent{$weightprefix.'numerical.weight'}=0;
230: $storecontent{$weightprefix.'option.weight'}=0;
231: $storecontent{$weightprefix.'radio.weight'}=0;
232: $storecontent{$weightprefix.'string.weight'}=0;
233: $storecontent{$weightprefix.$env{'form.questiontype'}.'.weight'}=1;
234:
235:
236: my $reply=&Apache::lonnet::cput
237: ('resourcedata',\%storecontent,
238: $env{'course.'.$env{'request.course.id'}.'.domain'},
239: $env{'course.'.$env{'request.course.id'}.'.num'});
240:
241: &Apache::lonnet::devalidatecourseresdata(
242: $env{'course.'.$env{'request.course.id'}.'.num'},
243: $env{'course.'.$env{'request.course.id'}.'.domain'});
244:
245: }
246: # ------------------------------------------------------------------- Read Data
247:
248: %qparms=&Apache::lonnet::dump('resourcedata',
249: $env{'course.'.$env{'request.course.id'}.'.domain'},
250: $env{'course.'.$env{'request.course.id'}.'.num'},
251: $env{'request.course.id'}.'.'.$symb);
252:
253: # ------------------------------------------------------------ Print the screen
254: my $spell_header=&Apache::lonhtmlcommon::spellheader();
255: $r->print(&Apache::loncommon::start_page('Simple Problem Editor',
256: $spell_header));
257: if ($symb) {
258: my $title='<h1>'.&Apache::lonnet::gettitle($symb).'</h1>';
259: my $displaylink = &get_parent_uri($symb);
260: if ($displaylink ne '') {
261: my $functions=&Apache::lonhtmlcommon::start_funclist()
262: .&Apache::lonhtmlcommon::add_item_funclist(
263: '<a href="'.$displaylink.'">'
264: .&mt('Student View').'</a>')
265: .&Apache::lonhtmlcommon::end_funclist();
266: $r->print(&Apache::loncommon::head_subbox($functions)
267: .$title);
268: } else {
269: $r->print($title
270: .'<p class="LC_error">'
271: .&mt('An error occurred retrieving the link to this problem.')
272: .'<br />'
273: .&mt('You may need to [_1]re-select the course[_2] and then return to this resource to view it.'
274: ,'<a href="/adm/roles">','</a>')
275: .'</p>'
276: );
277: }
278: $r->print('<p class="LC_warning">'
279: .&mt('Note: it can take up to 10 minutes for changes to take effect for all users.')
280: .&Apache::loncommon::help_open_topic('Caching')
281: .'</p>'
282: );
283:
284: $r->print(
285: '<div class="LC_Box">'
286: .&rawrendering($symb)
287: .'</div>'
288: );
289: $r->print('<form name="simpleedit" method="post">');
290: # Question Type
291: my %questiontypes=(
292: 'radio' => '1 out of N multiple choice (radio button)',
293: 'option' => 'Option Response',
294: 'string' => 'Short string response',
295: 'essay' => 'Essay, open end',
296: 'numerical' => 'Numerical Response');
297: $qtype=$qparms{$prefix.'questiontype'};
298: unless (defined($qtype)) { $qtype='radio'; }
299: unless ($questiontypes{$qtype}) { $qtype='radio'; }
300: $r->print(
301: '<fieldset style="width:400px;">'
302: .'<legend>'.&mt('Question Type').'</legend>'
303: .&Apache::loncommon::select_form(
304: $qtype,
305: 'questiontype',
306: &Apache::lonlocal::texthash(%questiontypes))
307: .'</fieldset>'
308: );
309: $r->print(
310: '<p>'
311: .'<input type="submit" value="'.&mt('Save and Edit').'" />'
312: .'</p>'
313: );
314: # Script
315: if ($qtype eq 'numerical') {
316: $r->print(&script());
317: }
318: # Question Text
319: $r->print(&questiontext());
320: # Radio, Option ===
321: if (($qtype eq 'radio') || ($qtype eq 'option')) {
322: # Response
323: my $maxfoils=$qparms{$prefix.'maxfoils'};
324: unless (defined($maxfoils)) { $maxfoils=10; }
325: unless ($maxfoils=~/^\d+$/) { $maxfoils=10; }
326: if ($maxfoils<=0) { $maxfoils=10; }
327: my %randomizes=(
328: 'yes' => 'Display foils in random order',
329: 'no' => 'Display foils in order given');
330: my $randomize=$qparms{$prefix.'randomize'};
331: unless (defined($randomize)) { $randomize='yes'; }
332: unless ($randomizes{$randomize}) { $randomize='yes'; }
333: $r->print(
334: '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2" style="width:100%">'.
335: '<tr><td>'.&mt('Max number of foils displayed').
336: ': <input type="text" size="3" name="maxfoils" value="'.$maxfoils.'" /> '.
337: &Apache::loncommon::select_form(
338: $randomize,
339: 'randomize',
340: &Apache::lonlocal::texthash(%randomizes)).
341: '</td></tr><tr><td bgcolor="#F0F0F0">');
342: # Option Response: Options
343: if ($qtype eq 'option') {
344: my $options=$qparms{$prefix.'options'};
345: unless (defined($options)) { $options="('true','false')"; }
346: my %optionshash=&evaloptionhash($options);
347: $r->print(
348: '<table bgcolor="#ffcc22" cellspacing="4" cellpadding="2" style="width:100%">'.
349: '<tr><td><input type="hidden" name="options" value="'.
350: $options.'" />'.&mt('Add new option').': '.
351: '<input type="text" name="newopt" size="15" />'.
352: &mt('Delete an option').': '.
353: &Apache::loncommon::select_form('','delopt',('' => '',%optionshash)).
354: '</td></tr><tr><td>');
355: }
356: # Foils
357: for (my $i=1;$i<=10;$i++) {
358: $r->print(&foil($i));
359: }
360: # End Options
361: if ($qtype eq 'option') {
362: $r->print('</td></tr></table>');
363: }
364:
365: # End Response
366: $r->print('</td></tr></table><br />');
367: # Hint
368: $r->print(&hint());
369: }
370: if ($qtype eq 'string') {
371: my %stringtypes=(
372: 'cs' => 'Case sensitive',
373: 'ci' => 'Case Insensitive',
374: 'mc' => 'Multiple Choice, Order of characters unchecked');
375: my $stringanswer=$qparms{$prefix.'stringanswer'};
376: unless (defined($stringanswer)) { $stringanswer=''; }
377: my $stringtype=$qparms{$prefix.'stringtype'};
378: unless (defined($stringtype)) { $stringtype='cs'; }
379: unless ($stringtypes{$stringtype}) { $stringtype='cs'; }
380: $r->print(
381: '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2" style="width:100%">'.
382: '<tr><td><label>'.&mt('Correct answer').': <input type="text" size="20" name="stringanswer" value="'.$stringanswer.'" /></label> '.
383: &Apache::loncommon::select_form(
384: $stringtype,
385: 'stringtype',
386: &Apache::lonlocal::texthash(%stringtypes)).
387: '</td></tr></table><br />');
388: # Hint
389: $r->print(&hint());
390: }
391: if ($qtype eq 'numerical') {
392: my $numericalanswer=$qparms{$prefix.'numericalanswer'};
393: unless (defined($numericalanswer)) { $numericalanswer=''; }
394: my $numericaltolerance=$qparms{$prefix.'numericaltolerance'};
395: unless (defined($numericaltolerance)) { $numericaltolerance='5%'; }
396: my $numericalsigfigs=$qparms{$prefix.'numericalsigfigs'};
397: unless (defined($numericalsigfigs)) { $numericalsigfigs='1,15'; }
398:
399: $r->print(
400: '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2" style="width:100%">'.
401: '<tr><td><label>'.&mt('Correct answer').': <input type="text" size="20" name="numericalanswer" value="'.$numericalanswer.'" /></label> '.
402: '<label>'.&mt('Unit').': <input type="text" size="5" name="numericalunit" value="'.$qparms{$prefix.'numericalunit'}.'" /></label> '.
403: '<label>'.&mt('Format').': <input type="text" size="5" name="numericalformat" value="'.$qparms{$prefix.'numericalformat'}.'" /></label> '.
404: '<label>'.&mt('Tolerance').': <input type="text" size="5" name="numericaltolerance" value="'.$numericaltolerance.'" /></label> '.
405: '<label>'.&mt('Significant digits').': <input type="text" size="5" name="numericalsigfigs" value="'.$numericalsigfigs.'" /></label>'.
406: '</td></tr></table><br />');
407: $r->print(&hint());
408: }
409: # Store Button
410: $r->print(
411: '<input type="submit" value="'.&mt('Save and Edit').'" /></form>');
412: } else {
413: $r->print(&mt('Could not identify problem.'));
414: }
415: $r->print(&Apache::loncommon::end_page());
416: return OK;
417: }
418:
419: 1;
420: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>