1: # The LearningOnline Network
2: # Simple Problem Parameter Setting "Editor"
3: #
4: # $Id: lonsimpleproblemedit.pm,v 1.13 2004/12/14 18:24:17 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::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:
38: my %qparms;
39: my $prefix;
40: my $qtype;
41:
42: sub evaloptionhash {
43: my $options=shift;
44: $options=~s/^\(\'//;
45: $options=~s/\'\)$//;
46: my %returnhash=();
47: foreach (split(/\'\,\'/,$options)) {
48: $returnhash{$_}=$_;
49: }
50: return %returnhash;
51: }
52:
53: sub rawrendering {
54: my ($symb)=@_;
55: my %data=('show_errors'=>'on',
56: 'simple_edit_button' => 'off',
57: 'devalidatecourseresdata'=>'on');
58: return &Apache::loncommon::get_student_view($symb,time,time,
59: $ENV{'request.course.id'},
60: 'web',\%data);
61: }
62:
63: sub questiontext {
64: my $text=$qparms{$prefix.'questiontext'};
65: my $qt=&mt('Question Text');
66: return (<<ENDQUESTION);
67: <table bgcolor="#dddd22" cellspacing="4" cellpadding="2">
68: <tr><td><b>$qt</b><br />
69: <textarea name="questiontext" cols="80" rows="8">$text</textarea>
70: </td></tr>
71: </table>
72: <br />
73: ENDQUESTION
74: }
75:
76: sub hint {
77: my $text=$qparms{$prefix.'hinttext'};
78: my $ht=&mt('Hint Text');
79: return (<<ENDHINT);
80: <table bgcolor="#accacc" cellspacing="4" cellpadding="2">
81: <tr><td><b>$ht</b><br />
82: <textarea name="hinttext" cols="80" rows="4">$text</textarea>
83: </td></tr>
84: </table>
85: <br />
86: ENDHINT
87: }
88:
89: sub foil {
90: my $number=shift;
91: my %values='';
92: if ($qtype eq 'radio') {
93: %values=('true' => 'True', 'false' => 'False');
94: } elsif ($qtype eq 'option') {
95: %values=&evaloptionhash($qparms{$prefix.'options'});
96: }
97: $values{'unused'}='Not shown, not used';
98: my $value=$qparms{$prefix.'value'.$number};
99: unless (defined($value)) { $value='unused'; }
100: unless ($values{$value}) { $value='unused'; }
101: my $position=$qparms{$prefix.'position'.$number};
102: my %positions=('random' => 'Random position',
103: 'top' => 'Show always at top position',
104: 'bottom' => 'Show always at bottom position');
105: unless (defined($position)) { $position='random'; }
106: unless ($positions{$position}) {
107: $position='random';
108: }
109: my $selectvalue=&Apache::loncommon::select_form
110: ($value,'value'.$number,%values);
111: my $selectposition=&Apache::loncommon::select_form
112: ($position,'position'.$number,%positions);
113: my $text=$qparms{$prefix.'text'.$number};
114: my %lt=&Apache::lonlocal::texthash('foil' => 'Foil',
115: 'value' => 'Value',
116: 'pos' => 'Position',
117: 'text' => 'Text');
118:
119: return (<<ENDFOIL);
120: <table bgcolor="#dd55ff" cellspacing="4" cellpadding="2">
121: <tr><td colspan="2"><b>$lt{'foil'}</b></td></tr>
122: <tr><td>$lt{'value'}: $selectvalue</td><td>$lt{'pos'}: $selectposition</td></tr>
123: <tr><td colspan="2">$lt{'text'}:<br />
124: <textarea name="text$number" cols="80" rows="4">$text</textarea>
125: </td></tr>
126: </table>
127: <br />
128: ENDFOIL
129: }
130:
131: sub get_parent_uri {
132: my ($cur_symb)=@_;
133: my $navmap = Apache::lonnavmaps::navmap->new();
134: my $it = $navmap->getIterator(undef, undef, undef, 1);
135: while ( my $res=$it->next()) {
136: if (ref($res) && $res->symb() eq $cur_symb) { last; }
137: }
138: my ($src,$symb,$anchor)=&Apache::lonnavmaps::getLinkForResource($it->getStack());
139: if (defined($anchor)) { $anchor='#'.$anchor; }
140: return $src.'?symb='.&Apache::lonnet::escape($symb).$anchor;
141: }
142:
143: sub handler {
144: my $r = shift;
145:
146: if ($r->header_only) {
147: &Apache::loncommon::content_type($r,'text/html');
148: $r->send_http_header;
149: return OK;
150: }
151:
152: # -------------------------------------------------------------------- Allowed?
153: unless (&Apache::lonnet::allowed('mdc',$ENV{'request.course.id'})) {
154: return HTTP_NOT_ACCEPTABLE;
155: }
156: # ----------------------------------------------------------------- Send header
157: &Apache::loncommon::content_type($r,'text/html');
158: $r->send_http_header;
159: # ----------------------------------------------------- Figure out where we are
160: my $uri=$r->uri;
161: $uri=~s/\/smpedit$//;
162: my $symb=&Apache::lonnet::symbread();
163:
164: # ------------------------------------------------ Prefix for everything stored
165: $prefix=$ENV{'request.course.id'}.'.'.$symb.'.0.';
166: # ---------------------------------------------------------- Anything to store?
167:
168: if (($symb) && (defined($ENV{'form.questiontype'}))) {
169: my %storecontent=();
170: undef %storecontent;
171: if ($ENV{'form.questiontype'} eq 'option') {
172: my %curoptions=&evaloptionhash($ENV{'form.options'});
173: if ($ENV{'form.delopt'}) {
174: delete $curoptions{$ENV{'form.delopt'}};
175: }
176: if ($ENV{'form.newopt'}) {
177: $ENV{'form.newopt'}=~s/\'/\\\'/g;
178: $curoptions{$ENV{'form.newopt'}}=$ENV{'form.newopt'};
179: }
180: $ENV{'form.options'}="('".join("','",keys %curoptions)."')";
181: }
182: $ENV{'form.hiddenparts'}='!'.$ENV{'form.questiontype'};
183: foreach (keys %ENV) {
184: if ($_=~/^form\.(\w+)$/) {
185: my $parm=$1;
186: $storecontent{$prefix.$parm}=$ENV{'form.'.$parm};
187: $storecontent{$prefix.$parm}=~s/^\s+//s;
188: $storecontent{$prefix.$parm}=~s/\s+$//s;
189: }
190: }
191: my $reply=&Apache::lonnet::cput
192: ('resourcedata',\%storecontent,
193: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
194: $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
195:
196: }
197: # ------------------------------------------------------------------- Read Data
198:
199: %qparms=&Apache::lonnet::dump('resourcedata',
200: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
201: $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
202: $ENV{'request.course.id'}.'.'.$symb);
203:
204: # ------------------------------------------------------------ Print the screen
205: $r->print(<<ENDDOCUMENT);
206: <html>
207: <head>
208: <title>The LearningOnline Network with CAPA</title>
209: ENDDOCUMENT
210: $r->print(&Apache::loncommon::bodytag('Simple Problem Editor'));
211: if ($symb) {
212: $r->print('<h1>'.&Apache::lonnet::gettitle($symb).'</h1>');
213: $r->print('<table width="100%" bgcolor="#FFFFAA" border="2"><tr><td>'.
214: '<a href="'.&get_parent_uri($symb).'">'.&mt('Student View').'</a> - '.&mt('Note: it can take up to 10 minutes for changes to take effect for all users.').
215: &Apache::loncommon::help_open_topic('Caching').'</td></tr></table>');
216: $r->print('<table border="2" bgcolor="#FFFFFF" width="100%"><tr><td>'.
217: &rawrendering($symb).
218: '</td></tr></table><br />');
219: $r->print('<form method="post">');
220: # Question Type
221: my %questiontypes=('radio' =>
222: '1 out of N multiple choice (radio button)',
223: 'option' => 'Option response',
224: 'string' => 'Short string response',
225: 'essay' => 'Essay, open end');
226: $qtype=$qparms{$prefix.'questiontype'};
227: unless (defined($qtype)) { $qtype='radio'; }
228: unless ($questiontypes{$qtype}) { $qtype='radio'; }
229: $r->print('<b>'.&mt('Question Type').
230: ': '.&Apache::loncommon::select_form
231: ($qtype,'questiontype',%questiontypes).
232: '</b><br /><input type="submit" value="'.&mt('Save and Edit').
233: '" /><p> </p>');
234: # Question Text
235: $r->print(&questiontext());
236: # Radio, Option ===
237: if (($qtype eq 'radio') || ($qtype eq 'option')) {
238: # Response
239: my $maxfoils=$qparms{$prefix.'maxfoils'};
240: unless (defined($maxfoils)) { $maxfoils=10; }
241: unless ($maxfoils=~/^\d+$/) { $maxfoils=10; }
242: if ($maxfoils<=0) { $maxfoils=10; }
243: my %randomizes=('yes' => 'Display foils in random order',
244: 'no' => 'Display foils in order given');
245: my $randomize=$qparms{$prefix.'randomize'};
246: unless (defined($randomize)) { $randomize='yes'; }
247: unless ($randomizes{$randomize}) { $randomize='yes'; }
248: $r->print(
249: '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
250: '<tr><td>'.&mt('Max number of foils displayed').
251: ': <input type="text" size="3" name="maxfoils" value="'.$maxfoils.'" /> '.
252: &Apache::loncommon::select_form
253: ($randomize,'randomize',%randomizes).
254: '</td></tr><tr><td bgcolor="#AAAAAA">');
255: # Option Response: Options
256: if ($qtype eq 'option') {
257: my $options=$qparms{$prefix.'options'};
258: unless (defined($options)) { $options="('true','false')"; }
259: my %optionshash=&evaloptionhash($options);
260: $r->print(
261: '<table bgcolor="#ffcc22" cellspacing="4" cellpadding="2">'.
262: '<tr><td><input type="hidden" name="options" value="'.
263: $options.'" />'.&mt('Add new option').': '.
264: '<input type="text" name="newopt" size="15" />'.
265: &mt('Delete an option').': '.
266: &Apache::loncommon::select_form('','delopt',('' => '',%optionshash)).
267: '</td></tr><tr><td>');
268: }
269: # Foils
270: for (my $i=1;$i<=10;$i++) {
271: $r->print(&foil($i));
272: }
273: # End Options
274: if ($qtype eq 'option') {
275: $r->print('</td></tr></table>');
276: }
277:
278: # End Response
279: $r->print('</td></tr></table><br />');
280: # Hint
281: $r->print(&hint());
282: }
283: if ($qtype eq 'string') {
284: my %stringtypes=(
285: 'cs' => 'Case sensitive',
286: 'ci' => 'Case Insensitive',
287: 'mc' => 'Multiple Choice, Order of characters unchecked');
288: my $stringanswer=$qparms{$prefix.'stringanswer'};
289: unless (defined($stringanswer)) { $stringanswer=''; }
290: my $stringtype=$qparms{$prefix.'stringtype'};
291: unless (defined($stringtype)) { $stringtype='cs'; }
292: unless ($stringtypes{$stringtype}) { $stringtype='cs'; }
293: $r->print(
294: '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
295: '<tr><td>'.&mt('Correct answer').': <input type="text" size="20" name="stringanswer" value="'.$stringanswer.'" /> '.
296: &Apache::loncommon::select_form
297: ($stringtype,'stringtype',%stringtypes).
298: '</td></tr></table><br />');
299: # Hint
300: $r->print(&hint());
301: }
302: # Store Button
303: $r->print(
304: '<input type="submit" value="'.&mt('Save and Edit').'" /></form>');
305: } else {
306: $r->print(&mt('Could not identify problem.'));
307: }
308: $r->print('</body></html>');
309: return OK;
310: }
311:
312: 1;
313: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>