Annotation of loncom/homework/lonsimpleproblemedit.pm, revision 1.11

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>