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