File:  [LON-CAPA] / loncom / homework / lonsimpleproblemedit.pm
Revision 1.11: download - view: text, annotated - select for diffs
Tue Nov 30 22:57:16 2004 UTC (19 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
 - fixing at least BUG#3648, BUG#3649, simple problem editor now uses ::ssi to get the rendering of the problem.

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

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