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

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

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