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