File:  [LON-CAPA] / loncom / homework / lonsimpleproblemedit.pm
Revision 1.8: download - view: text, annotated - select for diffs
Tue Nov 4 19:18:16 2003 UTC (20 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Undef global in lonhomework that we are parsing a problem after bypassing
the normal rendering calls.

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

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