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