File:  [LON-CAPA] / loncom / homework / lonsimpleproblemedit.pm
Revision 1.10: download - view: text, annotated - select for diffs
Thu Dec 11 21:23:58 2003 UTC (20 years, 4 months ago) by www
Branches: MAIN
CVS tags: version_1_2_X, version_1_2_99_0, version_1_2_1, version_1_2_0, version_1_1_X, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, version_1_1_99_0, version_1_1_3, version_1_1_2, version_1_1_1, version_1_1_0, version_1_0_99_3, version_1_0_99_2, HEAD
Stupid i18n error. Should go into 1.1

    1: # The LearningOnline Network
    2: # Simple Problem Parameter Setting "Editor"
    3: #
    4: # $Id: lonsimpleproblemedit.pm,v 1.10 2003/12/11 21:23:58 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:     my $qt=&mt('Question Text');
   75:     return (<<ENDQUESTION);
   76: <table bgcolor="#dddd22" cellspacing="4" cellpadding="2">
   77: <tr><td><b>$qt</b><br />
   78: <textarea name="questiontext" cols="80" rows="8">$text</textarea>
   79: </td></tr>
   80: </table>
   81: <br />
   82: ENDQUESTION
   83: }
   84: 
   85: sub hint {
   86:     my $text=$qparms{$prefix.'hinttext'};
   87:     my $ht=&mt('Hint Text');
   88:     return (<<ENDHINT);
   89: <table bgcolor="#accacc" cellspacing="4" cellpadding="2">
   90: <tr><td><b>$ht</b><br />
   91: <textarea name="hinttext" cols="80" rows="4">$text</textarea>
   92: </td></tr>
   93: </table>
   94: <br />
   95: ENDHINT
   96: }
   97: 
   98: sub foil {
   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:     }
  106:     $values{'unused'}='Not shown, not used';
  107:     my $value=$qparms{$prefix.'value'.$number};
  108:     unless (defined($value)) { $value='unused'; }
  109:     unless ($values{$value}) { $value='unused'; }
  110:     my $position=$qparms{$prefix.'position'.$number};
  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);
  122:     my $text=$qparms{$prefix.'text'.$number};
  123:     my %lt=&Apache::lonlocal::texthash('foil'  => 'Foil',
  124: 				       'value' => 'Value',
  125: 				       'pos'   => 'Position',
  126: 				       'text'  => 'Text');
  127: 
  128:     return (<<ENDFOIL);
  129: <table bgcolor="#dd55ff" cellspacing="4" cellpadding="2">
  130: <tr><td colspan="2"><b>$lt{'foil'}</b></td></tr>
  131: <tr><td>$lt{'value'}: $selectvalue</td><td>$lt{'pos'}: $selectposition</td></tr>
  132: <tr><td colspan="2">$lt{'text'}:<br />
  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) {
  144:         &Apache::loncommon::content_type($r,'text/html');
  145:         $r->send_http_header;
  146:         return OK;
  147:     }
  148: 
  149: # -------------------------------------------------------------------- Allowed?
  150:     unless (&Apache::lonnet::allowed('mdc',$ENV{'request.course.id'})) {
  151: 	return HTTP_NOT_ACCEPTABLE; 
  152:     }
  153: # ----------------------------------------------------------------- Send header
  154:     &Apache::loncommon::content_type($r,'text/html');
  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: 
  161: # ------------------------------------------------ Prefix for everything stored
  162:     $prefix=$ENV{'request.course.id'}.'.'.$symb.'.0.';
  163: # ---------------------------------------------------------- Anything to store?
  164: 
  165:     if (($symb) && (defined($ENV{'form.questiontype'}))) {
  166:         my %storecontent=();
  167:         undef %storecontent;
  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: 	}
  179: 	$ENV{'form.hiddenparts'}='!'.$ENV{'form.questiontype'};
  180:         foreach (keys %ENV) {
  181: 	    if ($_=~/^form\.(\w+)$/) {
  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;
  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:     }
  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: 
  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>');
  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)',
  217: 			   'option' => 'Option response',
  218:                            'string' => 'Short string response',
  219:                            'essay'  => 'Essay, open end');
  220:         $qtype=$qparms{$prefix.'questiontype'};
  221:         unless (defined($qtype)) { $qtype='radio'; }
  222:         unless ($questiontypes{$qtype}) { $qtype='radio'; }
  223:         $r->print('<b>'.&mt('Question Type').
  224: 		  ': '.&Apache::loncommon::select_form
  225: 	                               ($qtype,'questiontype',%questiontypes).
  226:   '</b><br /><input type="submit" value="'.&mt('Store Changes').
  227:   '" /><p>&nbsp;</p>');
  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">'.
  244: 	          '<tr><td>'.&mt('Max number of foils displayed').
  245: ': <input type="text" size="3" name="maxfoils" value="'.$maxfoils.'" />&nbsp;&nbsp;'.
  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="'.
  257:                   $options.'" />'.&mt('Add new option').': '.
  258:           '<input type="text" name="newopt" size="15" />'.
  259:           &mt('Delete an option').': '.
  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: 	    }
  271: 
  272: # End Response
  273: 	    $r->print('</td></tr></table><br />');
  274: # Hint
  275: 	    $r->print(&hint());
  276: 	}
  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">'.
  289: 	          '<tr><td>'.&mt('Correct answer').': <input type="text" size="20" name="stringanswer" value="'.$stringanswer.'" />&nbsp;&nbsp;'.
  290: 		      &Apache::loncommon::select_form
  291: 		      ($stringtype,'stringtype',%stringtypes).
  292: 		  '</td></tr></table><br />');
  293: # Hint
  294: 	    $r->print(&hint());
  295: 	}
  296: # Store Button
  297: 	$r->print(
  298:   '<input type="submit" value="'.&mt('Store Changes').'" /></form>');
  299:     } else {
  300: 	$r->print(&mt('Could not identify problem.'));
  301:     }
  302:     $r->print('</body></html>');
  303:     return OK;
  304: } 
  305: 
  306: 1;
  307: __END__

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