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