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

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

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