File:  [LON-CAPA] / loncom / homework / lonsimpleproblemedit.pm
Revision 1.12: download - view: text, annotated - select for diffs
Tue Nov 30 23:28:57 2004 UTC (19 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Bug #2476, #3647, cleanup to editing simple problems

    1: # The LearningOnline Network
    2: # Simple Problem Parameter Setting "Editor"
    3: #
    4: # $Id: lonsimpleproblemedit.pm,v 1.12 2004/11/30 23:28:57 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: 	      'simple_edit_button' => 'off',
   56: 	      'devalidatecourseresdata'=>'on');
   57:     return &Apache::loncommon::get_student_view($symb,time,time,
   58: 						$ENV{'request.course.id'},
   59: 						'web',\%data);
   60: }
   61: 
   62: sub questiontext {
   63:     my $text=$qparms{$prefix.'questiontext'};
   64:     my $qt=&mt('Question Text');
   65:     return (<<ENDQUESTION);
   66: <table bgcolor="#dddd22" cellspacing="4" cellpadding="2">
   67: <tr><td><b>$qt</b><br />
   68: <textarea name="questiontext" cols="80" rows="8">$text</textarea>
   69: </td></tr>
   70: </table>
   71: <br />
   72: ENDQUESTION
   73: }
   74: 
   75: sub hint {
   76:     my $text=$qparms{$prefix.'hinttext'};
   77:     my $ht=&mt('Hint Text');
   78:     return (<<ENDHINT);
   79: <table bgcolor="#accacc" cellspacing="4" cellpadding="2">
   80: <tr><td><b>$ht</b><br />
   81: <textarea name="hinttext" cols="80" rows="4">$text</textarea>
   82: </td></tr>
   83: </table>
   84: <br />
   85: ENDHINT
   86: }
   87: 
   88: sub foil {
   89:     my $number=shift;
   90:     my %values='';
   91:     if ($qtype eq 'radio') {
   92: 	%values=('true' => 'True', 'false' => 'False');
   93:     } elsif ($qtype eq 'option') {
   94: 	%values=&evaloptionhash($qparms{$prefix.'options'});
   95:     }
   96:     $values{'unused'}='Not shown, not used';
   97:     my $value=$qparms{$prefix.'value'.$number};
   98:     unless (defined($value)) { $value='unused'; }
   99:     unless ($values{$value}) { $value='unused'; }
  100:     my $position=$qparms{$prefix.'position'.$number};
  101:     my %positions=('random' => 'Random position',
  102: 		   'top'    => 'Show always at top position',
  103: 		   'bottom' => 'Show always at bottom position');
  104:     unless (defined($position)) { $position='random'; }
  105:     unless ($positions{$position}) {
  106: 	$position='random';
  107:     }
  108:     my $selectvalue=&Apache::loncommon::select_form
  109: 	                                ($value,'value'.$number,%values);
  110:     my $selectposition=&Apache::loncommon::select_form
  111:                                ($position,'position'.$number,%positions);
  112:     my $text=$qparms{$prefix.'text'.$number};
  113:     my %lt=&Apache::lonlocal::texthash('foil'  => 'Foil',
  114: 				       'value' => 'Value',
  115: 				       'pos'   => 'Position',
  116: 				       'text'  => 'Text');
  117: 
  118:     return (<<ENDFOIL);
  119: <table bgcolor="#dd55ff" cellspacing="4" cellpadding="2">
  120: <tr><td colspan="2"><b>$lt{'foil'}</b></td></tr>
  121: <tr><td>$lt{'value'}: $selectvalue</td><td>$lt{'pos'}: $selectposition</td></tr>
  122: <tr><td colspan="2">$lt{'text'}:<br />
  123: <textarea name="text$number" cols="80" rows="4">$text</textarea>
  124: </td></tr>
  125: </table>
  126: <br />
  127: ENDFOIL
  128: }
  129: 
  130: sub handler {
  131:     my $r = shift;
  132: 
  133:     if ($r->header_only) {
  134:         &Apache::loncommon::content_type($r,'text/html');
  135:         $r->send_http_header;
  136:         return OK;
  137:     }
  138: 
  139: # -------------------------------------------------------------------- Allowed?
  140:     unless (&Apache::lonnet::allowed('mdc',$ENV{'request.course.id'})) {
  141: 	return HTTP_NOT_ACCEPTABLE; 
  142:     }
  143: # ----------------------------------------------------------------- Send header
  144:     &Apache::loncommon::content_type($r,'text/html');
  145:     $r->send_http_header;
  146: # ----------------------------------------------------- Figure out where we are
  147:     my $uri=$r->uri;
  148:     $uri=~s/\/smpedit$//;
  149:     my $symb=&Apache::lonnet::symbread($uri);
  150: 
  151: # ------------------------------------------------ Prefix for everything stored
  152:     $prefix=$ENV{'request.course.id'}.'.'.$symb.'.0.';
  153: # ---------------------------------------------------------- Anything to store?
  154: 
  155:     if (($symb) && (defined($ENV{'form.questiontype'}))) {
  156:         my %storecontent=();
  157:         undef %storecontent;
  158:         if ($ENV{'form.questiontype'} eq 'option') {
  159: 	    my %curoptions=&evaloptionhash($ENV{'form.options'});
  160: 	    if ($ENV{'form.delopt'}) {
  161: 		delete $curoptions{$ENV{'form.delopt'}};
  162: 	    }
  163: 	    if ($ENV{'form.newopt'}) {
  164: 		$ENV{'form.newopt'}=~s/\'/\\\'/g;
  165:                 $curoptions{$ENV{'form.newopt'}}=$ENV{'form.newopt'};
  166: 	    }
  167:             $ENV{'form.options'}="('".join("','",keys %curoptions)."')";
  168: 	}
  169: 	$ENV{'form.hiddenparts'}='!'.$ENV{'form.questiontype'};
  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 width="100%" bgcolor="#FFFFAA" border="2"><tr><td>'.
  201:                 '<a href="'.$uri.'">'.&mt('Student View').'</a> - '.&mt('Note: it can take up to 10 minutes for changes to take effect for all users.').
  202: 		  &Apache::loncommon::help_open_topic('Caching').'</td></tr></table>');
  203: 	$r->print('<table border="2" bgcolor="#FFFFFF" width="100%"><tr><td>'.
  204:                   &rawrendering($symb).
  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>'.&mt('Question Type').
  217: 		  ': '.&Apache::loncommon::select_form
  218: 	                               ($qtype,'questiontype',%questiontypes).
  219:   '</b><br /><input type="submit" value="'.&mt('Save and Edit').
  220:   '" /><p>&nbsp;</p>');
  221: # Question Text
  222:         $r->print(&questiontext());
  223: # Radio, Option ===
  224: 	if (($qtype eq 'radio') || ($qtype eq 'option')) {
  225: # Response
  226:             my $maxfoils=$qparms{$prefix.'maxfoils'};
  227:             unless (defined($maxfoils)) { $maxfoils=10; }
  228:             unless ($maxfoils=~/^\d+$/) { $maxfoils=10; }
  229:             if ($maxfoils<=0) { $maxfoils=10; }
  230: 	    my %randomizes=('yes' => 'Display foils in random order',
  231: 			    'no'  => 'Display foils in order given');
  232: 	    my $randomize=$qparms{$prefix.'randomize'};
  233:             unless (defined($randomize)) { $randomize='yes'; }
  234:             unless ($randomizes{$randomize}) { $randomize='yes'; }
  235: 	    $r->print(
  236: 		  '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
  237: 	          '<tr><td>'.&mt('Max number of foils displayed').
  238: ': <input type="text" size="3" name="maxfoils" value="'.$maxfoils.'" />&nbsp;&nbsp;'.
  239: 		      &Apache::loncommon::select_form
  240: 		      ($randomize,'randomize',%randomizes).
  241: 		  '</td></tr><tr><td bgcolor="#AAAAAA">');
  242: # Option Response: Options
  243: 	    if ($qtype eq 'option') {
  244: 		my $options=$qparms{$prefix.'options'};
  245:                 unless (defined($options)) { $options="('true','false')"; }
  246:                 my %optionshash=&evaloptionhash($options);
  247: 		$r->print(
  248: 		  '<table bgcolor="#ffcc22" cellspacing="4" cellpadding="2">'.
  249: 		  '<tr><td><input type="hidden" name="options" value="'.
  250:                   $options.'" />'.&mt('Add new option').': '.
  251:           '<input type="text" name="newopt" size="15" />'.
  252:           &mt('Delete an option').': '.
  253:           &Apache::loncommon::select_form('','delopt',('' => '',%optionshash)).
  254:           '</td></tr><tr><td>');
  255: 	    }
  256: # Foils
  257: 	    for (my $i=1;$i<=10;$i++) {
  258: 		$r->print(&foil($i));
  259: 	    }
  260: # End Options
  261: 	    if ($qtype eq 'option') {
  262: 		$r->print('</td></tr></table>');
  263: 	    }
  264: 
  265: # End Response
  266: 	    $r->print('</td></tr></table><br />');
  267: # Hint
  268: 	    $r->print(&hint());
  269: 	}
  270: 	if ($qtype eq 'string') {
  271:             my %stringtypes=(
  272: 	       'cs' => 'Case sensitive',
  273: 	       'ci' => 'Case Insensitive',
  274: 	       'mc' => 'Multiple Choice, Order of characters unchecked');
  275:             my $stringanswer=$qparms{$prefix.'stringanswer'};
  276:             unless (defined($stringanswer)) { $stringanswer=''; }
  277:             my $stringtype=$qparms{$prefix.'stringtype'};
  278:             unless (defined($stringtype)) { $stringtype='cs'; }
  279:             unless ($stringtypes{$stringtype}) { $stringtype='cs'; }
  280: 	    $r->print(
  281: 		  '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
  282: 	          '<tr><td>'.&mt('Correct answer').': <input type="text" size="20" name="stringanswer" value="'.$stringanswer.'" />&nbsp;&nbsp;'.
  283: 		      &Apache::loncommon::select_form
  284: 		      ($stringtype,'stringtype',%stringtypes).
  285: 		  '</td></tr></table><br />');
  286: # Hint
  287: 	    $r->print(&hint());
  288: 	}
  289: # Store Button
  290: 	$r->print(
  291:   '<input type="submit" value="'.&mt('Save and Edit').'" /></form>');
  292:     } else {
  293: 	$r->print(&mt('Could not identify problem.'));
  294:     }
  295:     $r->print('</body></html>');
  296:     return OK;
  297: } 
  298: 
  299: 1;
  300: __END__

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