File:  [LON-CAPA] / loncom / homework / lonsimpleproblemedit.pm
Revision 1.18: download - view: text, annotated - select for diffs
Tue May 30 12:45:37 2006 UTC (17 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
&Apache::lonnet::unescape -> &unescape
&Apache::lonnet::escape -> &escape

    1: # The LearningOnline Network
    2: # Simple Problem Parameter Setting "Editor"
    3: #
    4: # $Id: lonsimpleproblemedit.pm,v 1.18 2006/05/30 12:45:37 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: use Apache::lonnavmaps;
   37: use lib '/home/httpd/lib/perl/';
   38: use LONCAPA;
   39:  
   40: 
   41: my %qparms;
   42: my $prefix;
   43: my $qtype;
   44: 
   45: sub evaloptionhash {
   46:     my $options=shift;
   47:     $options=~s/^\(\'//;
   48:     $options=~s/\'\)$//;
   49:     my %returnhash=();
   50:     foreach (split(/\'\,\'/,$options)) {
   51: 	$returnhash{$_}=$_;
   52:     }
   53:     return %returnhash;
   54: }
   55: 
   56: sub rawrendering {
   57:     my ($symb)=@_;
   58:     my %data=('show_errors'=>'on',
   59: 	      'simple_edit_button' => 'off',
   60: 	      'devalidatecourseresdata'=>'on');
   61:     return &Apache::loncommon::get_student_view($symb,time,time,
   62: 						$env{'request.course.id'},
   63: 						'web',\%data);
   64: }
   65: 
   66: sub questiontext {
   67:     my $text=$qparms{$prefix.'questiontext'};
   68:     my $qt=&mt('Question Text');
   69:     my $spell_link=
   70: 	&Apache::lonhtmlcommon::spelllink('simpleedit','questiontext');
   71:     return (<<ENDQUESTION);
   72: <table bgcolor="#dddd22" cellspacing="4" cellpadding="2">
   73: <tr><td><b>$qt</b><br />
   74: <textarea style="width:100%" name="questiontext" cols="80" rows="8">$text</textarea>
   75: <br />$spell_link
   76: </td></tr>
   77: </table>
   78: <br />
   79: ENDQUESTION
   80: }
   81: 
   82: sub hint {
   83:     my $text=$qparms{$prefix.'hinttext'};
   84:     my $ht=&mt('Hint Text');
   85:     my $spell_link=
   86: 	&Apache::lonhtmlcommon::spelllink('simpleedit','hinttext');
   87:     return (<<ENDHINT);
   88: <table bgcolor="#accacc" cellspacing="4" cellpadding="2">
   89: <tr><td><b>$ht</b><br />
   90: <textarea style="width:100%" name="hinttext" cols="80" rows="4">$text</textarea>
   91: <br />$spell_link
   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:     my $spell_link=
  129: 	&Apache::lonhtmlcommon::spelllink('simpleedit',"text$number");
  130:     return (<<ENDFOIL);
  131: <table bgcolor="#dd55ff" cellspacing="4" cellpadding="2">
  132: <tr><td colspan="2"><b>$lt{'foil'}</b></td></tr>
  133: <tr><td>$lt{'value'}: $selectvalue</td><td>$lt{'pos'}: $selectposition</td></tr>
  134: <tr><td colspan="2">$lt{'text'}:<br />
  135: <textarea style="width:100%" name="text$number" cols="80" rows="4">$text</textarea>
  136: <br />$spell_link
  137: </td></tr>
  138: </table>
  139: <br />
  140: ENDFOIL
  141: }
  142: 
  143: sub get_parent_uri {
  144:     my ($cur_symb)=@_;
  145:     my $navmap = Apache::lonnavmaps::navmap->new();
  146:     my $it = $navmap->getIterator(undef, undef, undef, 1);
  147:     while ( my $res=$it->next()) {
  148: 	if (ref($res) &&  $res->symb() eq  $cur_symb) { last; }
  149:     }
  150:     my ($src,$symb,$anchor)=&Apache::lonnavmaps::getLinkForResource($it->getStack());
  151:     if (defined($anchor)) { $anchor='#'.$anchor; }
  152:     return $src.'?symb='.&escape($symb).$anchor;
  153: }
  154: 
  155: sub handler {
  156:     my $r = shift;
  157: 
  158:     if ($r->header_only) {
  159:         &Apache::loncommon::content_type($r,'text/html');
  160:         $r->send_http_header;
  161:         return OK;
  162:     }
  163: 
  164: # -------------------------------------------------------------------- Allowed?
  165:     unless (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
  166: 	return HTTP_NOT_ACCEPTABLE; 
  167:     }
  168: # ----------------------------------------------------------------- Send header
  169:     &Apache::loncommon::content_type($r,'text/html');
  170:     $r->send_http_header;
  171: # ----------------------------------------------------- Figure out where we are
  172:     my $uri=$r->uri;
  173:     $uri=~s/\/smpedit$//;
  174:     my $symb=&Apache::lonnet::symbread();
  175: 
  176: # ------------------------------------------------ Prefix for everything stored
  177:     $prefix=$env{'request.course.id'}.'.'.$symb.'.0.';
  178: # ---------------------------------------------------------- Anything to store?
  179: 
  180:     if (($symb) && (defined($env{'form.questiontype'}))) {
  181:         my %storecontent=();
  182:         undef %storecontent;
  183:         if ($env{'form.questiontype'} eq 'option') {
  184: 	    my %curoptions=&evaloptionhash($env{'form.options'});
  185: 	    if ($env{'form.delopt'}) {
  186: 		delete $curoptions{$env{'form.delopt'}};
  187: 	    }
  188: 	    if ($env{'form.newopt'}) {
  189: 		$env{'form.newopt'}=~s/\'/\\\'/g;
  190:                 $curoptions{$env{'form.newopt'}}=$env{'form.newopt'};
  191: 	    }
  192:             $env{'form.options'}="('".join("','",keys %curoptions)."')";
  193: 	}
  194: 	$env{'form.hiddenparts'}='!'.$env{'form.questiontype'};
  195:         foreach (keys %env) {
  196: 	    if ($_=~/^form\.(\w+)$/) {
  197:                 my $parm=$1;
  198: 		$storecontent{$prefix.$parm}=$env{'form.'.$parm};
  199:                 $storecontent{$prefix.$parm}=~s/^\s+//s;
  200: 		$storecontent{$prefix.$parm}=~s/\s+$//s;
  201: 	    }
  202: 	}
  203: 	my $reply=&Apache::lonnet::cput
  204: 	    ('resourcedata',\%storecontent,
  205: 	     $env{'course.'.$env{'request.course.id'}.'.domain'},
  206: 	     $env{'course.'.$env{'request.course.id'}.'.num'});
  207: 
  208:     }
  209: # ------------------------------------------------------------------- Read Data
  210: 
  211:     %qparms=&Apache::lonnet::dump('resourcedata',
  212: 		     $env{'course.'.$env{'request.course.id'}.'.domain'},
  213: 		     $env{'course.'.$env{'request.course.id'}.'.num'},
  214: 		     $env{'request.course.id'}.'.'.$symb);
  215: 
  216: # ------------------------------------------------------------ Print the screen
  217:     my $spell_header=&Apache::lonhtmlcommon::spellheader();
  218:     $r->print(&Apache::loncommon::start_page('Simple Problem Editor',
  219: 					     $spell_header));
  220:     if ($symb) {
  221: 	$r->print('<h1>'.&Apache::lonnet::gettitle($symb).'</h1>');
  222: 	$r->print('<table width="100%" bgcolor="#FFFFAA" border="2"><tr><td>'.
  223:                 '<a href="'.&get_parent_uri($symb).'">'.&mt('Student View').'</a> - '.&mt('Note: it can take up to 10 minutes for changes to take effect for all users.').
  224: 		  &Apache::loncommon::help_open_topic('Caching').'</td></tr></table>');
  225: 	$r->print('<table border="2" bgcolor="#FFFFFF" width="100%"><tr><td>'.
  226:                   &rawrendering($symb).
  227:                   '</td></tr></table><br />');
  228:         $r->print('<form name="simpleedit" method="POST">');
  229: # Question Type        
  230: 	my %questiontypes=('radio'  => 
  231:                                '1 out of N multiple choice (radio button)',
  232: 			   'option' => 'Option response',
  233:                            'string' => 'Short string response',
  234:                            'essay'  => 'Essay, open end');
  235:         $qtype=$qparms{$prefix.'questiontype'};
  236:         unless (defined($qtype)) { $qtype='radio'; }
  237:         unless ($questiontypes{$qtype}) { $qtype='radio'; }
  238:         $r->print('<b>'.&mt('Question Type').
  239: 		  ': '.&Apache::loncommon::select_form
  240: 	                               ($qtype,'questiontype',%questiontypes).
  241:   '</b><br /><input type="submit" value="'.&mt('Save and Edit').
  242:   '" /><p>&nbsp;</p>');
  243: # Question Text
  244:         $r->print(&questiontext());
  245: # Radio, Option ===
  246: 	if (($qtype eq 'radio') || ($qtype eq 'option')) {
  247: # Response
  248:             my $maxfoils=$qparms{$prefix.'maxfoils'};
  249:             unless (defined($maxfoils)) { $maxfoils=10; }
  250:             unless ($maxfoils=~/^\d+$/) { $maxfoils=10; }
  251:             if ($maxfoils<=0) { $maxfoils=10; }
  252: 	    my %randomizes=('yes' => 'Display foils in random order',
  253: 			    'no'  => 'Display foils in order given');
  254: 	    my $randomize=$qparms{$prefix.'randomize'};
  255:             unless (defined($randomize)) { $randomize='yes'; }
  256:             unless ($randomizes{$randomize}) { $randomize='yes'; }
  257: 	    $r->print(
  258: 		  '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
  259: 	          '<tr><td>'.&mt('Max number of foils displayed').
  260: ': <input type="text" size="3" name="maxfoils" value="'.$maxfoils.'" />&nbsp;&nbsp;'.
  261: 		      &Apache::loncommon::select_form
  262: 		      ($randomize,'randomize',%randomizes).
  263: 		  '</td></tr><tr><td bgcolor="#AAAAAA">');
  264: # Option Response: Options
  265: 	    if ($qtype eq 'option') {
  266: 		my $options=$qparms{$prefix.'options'};
  267:                 unless (defined($options)) { $options="('true','false')"; }
  268:                 my %optionshash=&evaloptionhash($options);
  269: 		$r->print(
  270: 		  '<table bgcolor="#ffcc22" cellspacing="4" cellpadding="2">'.
  271: 		  '<tr><td><input type="hidden" name="options" value="'.
  272:                   $options.'" />'.&mt('Add new option').': '.
  273:           '<input type="text" name="newopt" size="15" />'.
  274:           &mt('Delete an option').': '.
  275:           &Apache::loncommon::select_form('','delopt',('' => '',%optionshash)).
  276:           '</td></tr><tr><td>');
  277: 	    }
  278: # Foils
  279: 	    for (my $i=1;$i<=10;$i++) {
  280: 		$r->print(&foil($i));
  281: 	    }
  282: # End Options
  283: 	    if ($qtype eq 'option') {
  284: 		$r->print('</td></tr></table>');
  285: 	    }
  286: 
  287: # End Response
  288: 	    $r->print('</td></tr></table><br />');
  289: # Hint
  290: 	    $r->print(&hint());
  291: 	}
  292: 	if ($qtype eq 'string') {
  293:             my %stringtypes=(
  294: 	       'cs' => 'Case sensitive',
  295: 	       'ci' => 'Case Insensitive',
  296: 	       'mc' => 'Multiple Choice, Order of characters unchecked');
  297:             my $stringanswer=$qparms{$prefix.'stringanswer'};
  298:             unless (defined($stringanswer)) { $stringanswer=''; }
  299:             my $stringtype=$qparms{$prefix.'stringtype'};
  300:             unless (defined($stringtype)) { $stringtype='cs'; }
  301:             unless ($stringtypes{$stringtype}) { $stringtype='cs'; }
  302: 	    $r->print(
  303: 		  '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
  304: 	          '<tr><td>'.&mt('Correct answer').': <input type="text" size="20" name="stringanswer" value="'.$stringanswer.'" />&nbsp;&nbsp;'.
  305: 		      &Apache::loncommon::select_form
  306: 		      ($stringtype,'stringtype',%stringtypes).
  307: 		  '</td></tr></table><br />');
  308: # Hint
  309: 	    $r->print(&hint());
  310: 	}
  311: # Store Button
  312: 	$r->print(
  313:   '<input type="submit" value="'.&mt('Save and Edit').'" /></form>');
  314:     } else {
  315: 	$r->print(&mt('Could not identify problem.'));
  316:     }
  317:     $r->print(&Apache::loncommon::end_page());
  318:     return OK;
  319: } 
  320: 
  321: 1;
  322: __END__

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