File:  [LON-CAPA] / loncom / homework / lonsimpleproblemedit.pm
Revision 1.2: download - view: text, annotated - select for diffs
Wed Jul 9 06:23:20 2003 UTC (20 years, 10 months ago) by www
Branches: MAIN
CVS tags: HEAD
Simpleproblem now works for Radio and Option response

    1: # The LearningOnline Network
    2: # Simple Problem Parameter Setting "Editor"
    3: #
    4: # $Id: lonsimpleproblemedit.pm,v 1.2 2003/07/09 06:23:20 www Exp $
    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;
   35: 
   36: my %qparms;
   37: my $prefix;
   38: my $qtype;
   39: 
   40: sub evaloptionhash {
   41:     my $options=shift;
   42:     $options=~s/^\(\'//;
   43:     $options=~s/\'\)$//;
   44:     my %returnhash=();
   45:     foreach (split(/\'\,\'/,$options)) {
   46: 	$returnhash{$_}=$_;
   47:     }
   48:     return %returnhash;
   49: }
   50: 
   51: sub rawrendering {
   52:     my ($request,$uri)=@_;
   53:     my $problem=&Apache::lonnet::getfile
   54:                             (&Apache::lonnet::filelocation('',$uri));
   55:     &Apache::lonnet::devalidatecourseresdata(
   56: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
   57: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.domain'});
   58:     my $uname=$ENV{'user.name'};
   59:     my $udom=$ENV{'user.domain'};
   60:     $ENV{'user.name'}=time;
   61:     $ENV{'user.domain'}=time;
   62:     my $result = &Apache::lonxml::xmlparse($request,'web', $problem);
   63:     $ENV{'user.name'}=$uname;
   64:     $ENV{'user.domain'}=$udom;
   65:     $result=~s/^.*\<body[^\>]*\>//si;
   66:     $result=~s/\<\/body[^\>]*\>.*$//si;
   67:     return $result;
   68: }
   69: 
   70: sub questiontext {
   71:     my $text=$qparms{$prefix.'questiontext'};
   72:     return (<<ENDQUESTION);
   73: <table bgcolor="#dddd22" cellspacing="4" cellpadding="2">
   74: <tr><td><b>Question Text</b><br />
   75: <textarea name="questiontext" cols="80" rows="8">$text</textarea>
   76: </td></tr>
   77: </table>
   78: <br />
   79: ENDQUESTION
   80: }
   81: 
   82: sub hint {
   83:     my $text=$qparms{$prefix.'hinttext'};
   84:     return (<<ENDHINT);
   85: <table bgcolor="#accacc" cellspacing="4" cellpadding="2">
   86: <tr><td><b>Hint Text</b><br />
   87: <textarea name="hinttext" cols="80" rows="4">$text</textarea>
   88: </td></tr>
   89: </table>
   90: <br />
   91: ENDHINT
   92: }
   93: 
   94: sub foil {
   95:     my $number=shift;
   96:     my %values='';
   97:     if ($qtype eq 'radio') {
   98: 	%values=('true' => 'True', 'false' => 'False');
   99:     } elsif ($qtype eq 'option') {
  100: 	%values=&evaloptionhash($qparms{$prefix.'options'});
  101:     }
  102:     $values{'unused'}='Not shown, not used';
  103:     my $value=$qparms{$prefix.'value'.$number};
  104:     unless (defined($value)) { $value='unused'; }
  105:     unless ($values{$value}) { $value='unused'; }
  106:     my $position=$qparms{$prefix.'position'.$number};
  107:     my %positions=('random' => 'Random position',
  108: 		   'top'    => 'Show always at top position',
  109: 		   'bottom' => 'Show always at bottom position');
  110:     unless (defined($position)) { $position='random'; }
  111:     unless ($positions{$position}) {
  112: 	$position='random';
  113:     }
  114:     my $selectvalue=&Apache::loncommon::select_form
  115: 	                                ($value,'value'.$number,%values);
  116:     my $selectposition=&Apache::loncommon::select_form
  117:                                ($position,'position'.$number,%positions);
  118:     my $text=$qparms{$prefix.'text'.$number};
  119:     return (<<ENDFOIL);
  120: <table bgcolor="#dd55ff" cellspacing="4" cellpadding="2">
  121: <tr><td colspan="2"><b>Foil</b></td></tr>
  122: <tr><td>Value: $selectvalue</td><td>Position: $selectposition</td></tr>
  123: <tr><td colspan="2">Text:<br />
  124: <textarea name="text$number" cols="80" rows="4">$text</textarea>
  125: </td></tr>
  126: </table>
  127: <br />
  128: ENDFOIL
  129: }
  130: 
  131: sub handler {
  132:     my $r = shift;
  133: 
  134:     if ($r->header_only) {
  135:         $r->content_type('text/html');
  136:         $r->send_http_header;
  137:         return OK;
  138:     }
  139: 
  140: # -------------------------------------------------------------------- Allowed?
  141:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  142: 	return HTTP_NOT_ACCEPTABLE; 
  143:     }
  144: # ----------------------------------------------------------------- Send header
  145:     $r->content_type('text/html');
  146:     $r->send_http_header;
  147: # ----------------------------------------------------- Figure out where we are
  148:     my $uri=$r->uri;
  149:     $uri=~s/\/smpedit$//;
  150:     my $symb=&Apache::lonnet::symbread($uri);
  151: 
  152: # ------------------------------------------------ Prefix for everything stored
  153:     $prefix=$ENV{'request.course.id'}.'.'.$symb.'.0.';
  154: # ---------------------------------------------------------- Anything to store?
  155: 
  156:     if (($symb) && (defined($ENV{'form.questiontype'}))) {
  157:         my %storecontent=();
  158:         undef %storecontent;
  159:         if ($ENV{'form.questiontype'} eq 'option') {
  160: 	    my %curoptions=&evaloptionhash($ENV{'form.options'});
  161: 	    if ($ENV{'form.delopt'}) {
  162: 		delete $curoptions{$ENV{'form.delopt'}};
  163: 	    }
  164: 	    if ($ENV{'form.newopt'}) {
  165: 		$ENV{'form.newopt'}=~s/\'/\\\'/g;
  166:                 $curoptions{$ENV{'form.newopt'}}=$ENV{'form.newopt'};
  167: 	    }
  168:             $ENV{'form.options'}="('".join("','",keys %curoptions)."')";
  169: 	}
  170:         foreach (keys %ENV) {
  171: 	    if ($_=~/^form\.(\w+)$/) {
  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;
  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:     }
  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: 
  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>');
  200: 	$r->print('<table border="2" bgcolor="#FFFFFF" width="100%"><tr><td>'.
  201:                   &rawrendering($r,$uri).
  202:                   '</td></tr></table><br />');
  203:         $r->print('<form method="post">');
  204: # Question Type        
  205: 	my %questiontypes=('radio'  => 
  206:                                '1 out of N multiple choice (radio button)',
  207: 			   'option' => 'Option Response');
  208:         $qtype=$qparms{$prefix.'questiontype'};
  209:         unless (defined($qtype)) { $qtype='radio'; }
  210:         unless ($questiontypes{$qtype}) { $qtype='radio'; }
  211:         $r->print('Question Type: '.&Apache::loncommon::select_form
  212: 	                               ($qtype,'questiontype',%questiontypes).
  213:                   '<p>&nbsp;</p>');
  214: # Question Text
  215:         $r->print(&questiontext());
  216: # Radio, Option ===
  217: 	if (($qtype eq 'radio') || ($qtype eq 'option')) {
  218: # Response
  219:             my $maxfoils=$qparms{$prefix.'maxfoils'};
  220:             unless (defined($maxfoils)) { $maxfoils=10; }
  221:             unless ($maxfoils=~/^\d+$/) { $maxfoils=10; }
  222:             if ($maxfoils<=0) { $maxfoils=10; }
  223: 	    my %randomizes=('yes' => 'Display foils in random order',
  224: 			    'no'  => 'Display foils in order given');
  225: 	    my $randomize=$qparms{$prefix.'randomize'};
  226:             unless (defined($randomize)) { $randomize='yes'; }
  227:             unless ($randomizes{$randomize}) { $randomize='yes'; }
  228: 	    $r->print(
  229: 		  '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
  230: 	          '<tr><td>Max number of foils displayed: <input type="text" size="3" name="maxfoils" value="'.$maxfoils.'" />&nbsp;&nbsp;'.
  231: 		      &Apache::loncommon::select_form
  232: 		      ($randomize,'randomize',%randomizes).
  233: 		  '</td></tr><tr><td bgcolor="#AAAAAA">');
  234: # Option Response: Options
  235: 	    if ($qtype eq 'option') {
  236: 		my $options=$qparms{$prefix.'options'};
  237:                 unless (defined($options)) { $options="('true','false')"; }
  238:                 my %optionshash=&evaloptionhash($options);
  239: 		$r->print(
  240: 		  '<table bgcolor="#ffcc22" cellspacing="4" cellpadding="2">'.
  241: 		  '<tr><td><input type="hidden" name="options" value="'.
  242:                   $options.'" />Add new option: '.
  243:           '<input type="text" name="newopt" size="15" />Delete an option: '.
  244:           &Apache::loncommon::select_form('','delopt',('' => '',%optionshash)).
  245:           '</td></tr><tr><td>');
  246: 	    }
  247: # Foils
  248: 	    for (my $i=1;$i<=10;$i++) {
  249: 		$r->print(&foil($i));
  250: 	    }
  251: # End Options
  252: 	    if ($qtype eq 'option') {
  253: 		$r->print('</td></tr></table>');
  254: 	    }
  255: 
  256: # End Response
  257: 	    $r->print('</td></tr></table><br />');
  258: 	}
  259: # Hint
  260:         $r->print(&hint());
  261: # Store Button
  262: 	$r->print(
  263:   '<input type="submit" name="storeproblem" value="Store Changes" /></form>');
  264:     } else {
  265: 	$r->print('Could not identify problem.');
  266:     }
  267:     $r->print('</body></html>');
  268:     return OK;
  269: } 
  270: 
  271: 1;
  272: __END__

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