File:  [LON-CAPA] / loncom / homework / lonsimpleproblemedit.pm
Revision 1.20.2.2: download - view: text, annotated - select for diffs
Wed Jan 27 20:38:08 2010 UTC (14 years, 3 months ago) by raeburn
Branches: version_2_9_X
Diff to branchpoint 1.20: preferred, unified
- Backport 1.24, 1.25, 1.26, 1.27.

    1: # The LearningOnline Network
    2: # Simple Problem Parameter Setting "Editor"
    3: #
    4: # $Id: lonsimpleproblemedit.pm,v 1.20.2.2 2010/01/27 20:38:08 raeburn 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" style="width:100%">
   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 script {
   99:     my $text=$qparms{$prefix.'numericalscript'};
  100:     my $ht=&mt('Scripting (optional)');
  101:     return (<<ENDSCRIPT);
  102: <table bgcolor="#ccccaa" cellspacing="4" cellpadding="2">
  103: <tr><td><b>$ht</b><br />
  104: <textarea style="width:100%" name="numericalscript" cols="80" rows="4">$text</textarea>
  105: </td></tr>
  106: </table>
  107: <br />
  108: ENDSCRIPT
  109: }
  110: 
  111: sub foil {
  112:     my $number=shift;
  113:     my %values='';
  114:     if ($qtype eq 'radio') {
  115: 	%values=('true' => 'True', 'false' => 'False');
  116:     } elsif ($qtype eq 'option') {
  117: 	%values=&evaloptionhash($qparms{$prefix.'options'});
  118:     }
  119:     $values{'unused'}='Not shown, not used';
  120:     my $value=$qparms{$prefix.'value'.$number};
  121:     unless (defined($value)) { $value='unused'; }
  122:     unless ($values{$value}) { $value='unused'; }
  123:     my $position=$qparms{$prefix.'position'.$number};
  124:     my %positions=('random' => 'Random position',
  125: 		   'top'    => 'Show always at top position',
  126: 		   'bottom' => 'Show always at bottom position');
  127:     unless (defined($position)) { $position='random'; }
  128:     unless ($positions{$position}) {
  129: 	$position='random';
  130:     }
  131:     my $selectvalue=&Apache::loncommon::select_form
  132: 	                                ($value,'value'.$number,%values);
  133:     my $selectposition=&Apache::loncommon::select_form
  134:                                ($position,'position'.$number,%positions);
  135:     my $text=$qparms{$prefix.'text'.$number};
  136:     my %lt=&Apache::lonlocal::texthash('foil'  => 'Foil',
  137: 				       'value' => 'Value',
  138: 				       'pos'   => 'Position',
  139: 				       'text'  => 'Text');
  140: 
  141:     my $spell_link=
  142: 	&Apache::lonhtmlcommon::spelllink('simpleedit',"text$number");
  143:     return (<<ENDFOIL);
  144: <table bgcolor="#dd55ff" cellspacing="4" cellpadding="2">
  145: <tr><td colspan="2"><b>$lt{'foil'}</b></td></tr>
  146: <tr><td>$lt{'value'}: $selectvalue</td><td>$lt{'pos'}: $selectposition</td></tr>
  147: <tr><td colspan="2">$lt{'text'}:<br />
  148: <textarea style="width:100%" name="text$number" cols="80" rows="4">$text</textarea>
  149: <br />$spell_link
  150: </td></tr>
  151: </table>
  152: <br />
  153: ENDFOIL
  154: }
  155: 
  156: sub get_parent_uri {
  157:     my ($cur_symb)=@_;
  158:     my $navmap = Apache::lonnavmaps::navmap->new();
  159:     if (defined($navmap)) {
  160:         my $it = $navmap->getIterator(undef, undef, undef, 1);
  161:         while ( my $res=$it->next()) {
  162: 	    if (ref($res) &&  $res->symb() eq  $cur_symb) { last; }
  163:         }
  164:         my ($src,$symb,$anchor)=&Apache::lonnavmaps::getLinkForResource($it->getStack());
  165:         if (defined($anchor)) { $anchor='#'.$anchor; }
  166:         return $src.'?symb='.&escape($symb).$anchor;
  167:     }
  168:     return;
  169: }
  170: 
  171: sub handler {
  172:     my $r = shift;
  173: 
  174:     if ($r->header_only) {
  175:         &Apache::loncommon::content_type($r,'text/html');
  176:         $r->send_http_header;
  177:         return OK;
  178:     }
  179: 
  180: # -------------------------------------------------------------------- Allowed?
  181:     unless (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
  182: 	return HTTP_NOT_ACCEPTABLE; 
  183:     }
  184: # ----------------------------------------------------------------- Send header
  185:     &Apache::loncommon::content_type($r,'text/html');
  186:     $r->send_http_header;
  187: # ----------------------------------------------------- Figure out where we are
  188:     my $uri=$r->uri;
  189:     $uri=~s/\/smpedit$//;
  190:     my $symb=&Apache::lonnet::symbread();
  191: 
  192: # ------------------------------------------------ Prefix for everything stored
  193:     $prefix=$env{'request.course.id'}.'.'.$symb.'.0.';
  194: #----------------------------------- Prefix for storing weight of Problem Parts
  195:     my $weightprefix=$env{'request.course.id'}.'.'.$symb.'.';
  196: # ---------------------------------------------------------- Anything to store?
  197: 
  198:     if (($symb) && (defined($env{'form.questiontype'}))) {
  199:         my %storecontent=();
  200:         undef %storecontent;
  201:         if ($env{'form.questiontype'} eq 'option') {
  202: 	    my %curoptions=&evaloptionhash($env{'form.options'});
  203: 	    if ($env{'form.delopt'}) {
  204: 		delete $curoptions{$env{'form.delopt'}};
  205: 	    }
  206: 	    if ($env{'form.newopt'}) {
  207: 		$env{'form.newopt'}=~s/\'/\\\'/g;
  208:                 $curoptions{$env{'form.newopt'}}=$env{'form.newopt'};
  209: 	    }
  210:             $env{'form.options'}="('".join("','",keys %curoptions)."')";
  211: 	}
  212: 	$env{'form.hiddenparts'}='!'.$env{'form.questiontype'};
  213:         foreach (keys %env) {
  214: 	    if ($_=~/^form\.(\w+)$/) {
  215:                 my $parm=$1;
  216: 		$storecontent{$prefix.$parm}=$env{'form.'.$parm};
  217:                 $storecontent{$prefix.$parm}=~s/^\s+//s;
  218: 		$storecontent{$prefix.$parm}=~s/\s+$//s;
  219: 	    }
  220: 	}
  221: # ---------------------------------------------------- Set weights of hidden parts to zero
  222: # ------------------------------------------------------ and used part to 1
  223:         $storecontent{$weightprefix.'essay.weight'}=0;
  224:         $storecontent{$weightprefix.'numerical.weight'}=0;
  225:         $storecontent{$weightprefix.'option.weight'}=0;
  226:         $storecontent{$weightprefix.'radio.weight'}=0;
  227:         $storecontent{$weightprefix.'string.weight'}=0;
  228:         $storecontent{$weightprefix.$env{'form.questiontype'}.'.weight'}=1;
  229: 
  230: 	my $reply=&Apache::lonnet::cput
  231: 	    ('resourcedata',\%storecontent,
  232: 	     $env{'course.'.$env{'request.course.id'}.'.domain'},
  233: 	     $env{'course.'.$env{'request.course.id'}.'.num'});
  234:     }
  235:         
  236:     &Apache::lonnet::devalidatecourseresdata(
  237:                         $env{'course.'.$env{'request.course.id'}.'.num'},
  238:                         $env{'course.'.$env{'request.course.id'}.'.domain'});
  239: # ------------------------------------------------------------------- Read Data
  240: 
  241:     %qparms=&Apache::lonnet::dump('resourcedata',
  242: 		     $env{'course.'.$env{'request.course.id'}.'.domain'},
  243: 		     $env{'course.'.$env{'request.course.id'}.'.num'},
  244: 		     $env{'request.course.id'}.'.'.$symb);
  245: 
  246: # ------------------------------------------------------------ Print the screen
  247:     my $spell_header=&Apache::lonhtmlcommon::spellheader();
  248:     $r->print(&Apache::loncommon::start_page('Simple Problem Editor',
  249: 					     $spell_header));
  250:     if ($symb) {
  251: 	$r->print('<h1>'.&Apache::lonnet::gettitle($symb).'</h1>');
  252: 	$r->print('<table width="100%" bgcolor="#FFFFAA" border="2"><tr><td>');
  253:         my $displaylink = &get_parent_uri($symb);
  254:         if ($displaylink ne '') {      
  255:             $r->print('<a href="'.$displaylink.'">'.&mt('Student View').'</a> - ');
  256:         } else {
  257:             $r->print('<span class="LC_error">'.&mt('An error occurred retrieving the link to this problem').'<br />'.&mt('You may need to [_1]re-select the course[_2] and then return to this resource to view it.','<a href="/adm/roles">','</a>').'</span><br />'); 
  258:         }
  259:         $r->print(&mt('Note: it can take up to 10 minutes for changes to take effect for all users.').
  260: 		  &Apache::loncommon::help_open_topic('Caching').'</td></tr></table>');
  261: 	$r->print('<table border="2" bgcolor="#FFFFFF" width="100%"><tr><td>'.
  262:                   &rawrendering($symb).
  263:                   '</td></tr></table><br />');
  264:         $r->print('<form name="simpleedit" method="post">');
  265: # Question Type        
  266: 	my %questiontypes=('radio'  => 
  267:                                '1 out of N multiple choice (radio button)',
  268: 			   'option' => 'Option response',
  269:                            'string' => 'Short string response',
  270:                            'essay'  => 'Essay, open end',
  271:                            'numerical' => 'Numerical response');
  272: 
  273:         $qtype=$qparms{$prefix.'questiontype'};
  274:         unless (defined($qtype)) { $qtype='radio'; }
  275:         unless ($questiontypes{$qtype}) { $qtype='radio'; }
  276:         $r->print('<b>'.&mt('Question Type').
  277: 		  ': '.&Apache::loncommon::select_form
  278: 	                               ($qtype,'questiontype',%questiontypes).
  279:   '</b><br /><input type="submit" value="'.&mt('Save and Edit').
  280:   '" /><p>&nbsp;</p>');
  281: # Script
  282:         if ($qtype eq 'numerical') {
  283:            $r->print(&script());
  284:         }
  285: # Question Text
  286:         $r->print(&questiontext());
  287: # Radio, Option ===
  288: 	if (($qtype eq 'radio') || ($qtype eq 'option')) {
  289: # Response
  290:             my $maxfoils=$qparms{$prefix.'maxfoils'};
  291:             unless (defined($maxfoils)) { $maxfoils=10; }
  292:             unless ($maxfoils=~/^\d+$/) { $maxfoils=10; }
  293:             if ($maxfoils<=0) { $maxfoils=10; }
  294: 	    my %randomizes=('yes' => 'Display foils in random order',
  295: 			    'no'  => 'Display foils in order given');
  296: 	    my $randomize=$qparms{$prefix.'randomize'};
  297:             unless (defined($randomize)) { $randomize='yes'; }
  298:             unless ($randomizes{$randomize}) { $randomize='yes'; }
  299: 	    $r->print(
  300: 		  '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
  301: 	          '<tr><td>'.&mt('Max number of foils displayed').
  302: ': <input type="text" size="3" name="maxfoils" value="'.$maxfoils.'" />&nbsp;&nbsp;'.
  303: 		      &Apache::loncommon::select_form
  304: 		      ($randomize,'randomize',%randomizes).
  305: 		  '</td></tr><tr><td bgcolor="#AAAAAA">');
  306: # Option Response: Options
  307: 	    if ($qtype eq 'option') {
  308: 		my $options=$qparms{$prefix.'options'};
  309:                 unless (defined($options)) { $options="('true','false')"; }
  310:                 my %optionshash=&evaloptionhash($options);
  311: 		$r->print(
  312: 		  '<table bgcolor="#ffcc22" cellspacing="4" cellpadding="2">'.
  313: 		  '<tr><td><input type="hidden" name="options" value="'.
  314:                   $options.'" />'.&mt('Add new option').': '.
  315:           '<input type="text" name="newopt" size="15" />'.
  316:           &mt('Delete an option').': '.
  317:           &Apache::loncommon::select_form('','delopt',('' => '',%optionshash)).
  318:           '</td></tr><tr><td>');
  319: 	    }
  320: # Foils
  321: 	    for (my $i=1;$i<=10;$i++) {
  322: 		$r->print(&foil($i));
  323: 	    }
  324: # End Options
  325: 	    if ($qtype eq 'option') {
  326: 		$r->print('</td></tr></table>');
  327: 	    }
  328: 
  329: # End Response
  330: 	    $r->print('</td></tr></table><br />');
  331: # Hint
  332: 	    $r->print(&hint());
  333: 	}
  334: 	if ($qtype eq 'string') {
  335:             my %stringtypes=(
  336: 	       'cs' => 'Case sensitive',
  337: 	       'ci' => 'Case Insensitive',
  338: 	       'mc' => 'Multiple Choice, Order of characters unchecked');
  339:             my $stringanswer=$qparms{$prefix.'stringanswer'};
  340:             unless (defined($stringanswer)) { $stringanswer=''; }
  341:             my $stringtype=$qparms{$prefix.'stringtype'};
  342:             unless (defined($stringtype)) { $stringtype='cs'; }
  343:             unless ($stringtypes{$stringtype}) { $stringtype='cs'; }
  344: 	    $r->print(
  345: 		  '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
  346:                   '<tr><td><label>'.&mt('Correct answer').': <input type="text" size="20" name="stringanswer" value="'.$stringanswer.'" /></label>&nbsp;&nbsp;'.
  347: 		      &Apache::loncommon::select_form
  348: 		      ($stringtype,'stringtype',%stringtypes).
  349: 		  '</td></tr></table><br />');
  350: # Hint
  351: 	    $r->print(&hint());
  352: 	}
  353:         if ($qtype eq 'numerical') {
  354:             my $numericalanswer=$qparms{$prefix.'numericalanswer'};
  355:             unless (defined($numericalanswer)) { $numericalanswer=''; }
  356:             my $numericaltolerance=$qparms{$prefix.'numericaltolerance'};
  357:             unless (defined($numericaltolerance)) { $numericaltolerance='5%'; }
  358:             my $numericalsigfigs=$qparms{$prefix.'numericalsigfigs'};
  359:             unless (defined($numericalsigfigs)) { $numericalsigfigs='1,15'; }
  360: 
  361:             $r->print(
  362:                 '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
  363:                   '<tr><td><label>'.&mt('Correct answer').': <input type="text" size="20" name="numericalanswer" value="'.$numericalanswer.'" /></label>&nbsp;&nbsp;'.
  364:                   '<label>'.&mt('Unit').':  <input type="text" size="5" name="numericalunit" value="'.$qparms{$prefix.'numericalunit'}.'" /></label>&nbsp;&nbsp;'.
  365:                   '<label>'.&mt('Format').':  <input type="text" size="5" name="numericalformat" value="'.$qparms{$prefix.'numericalformat'}.'" /></label>&nbsp;&nbsp;'.
  366:                   '<label>'.&mt('Tolerance').':  <input type="text" size="5" name="numericaltolerance" value="'.$numericaltolerance.'" /></label>&nbsp;&nbsp;'.
  367:                   '<label>'.&mt('Significant digits').':  <input type="text" size="5" name="numericalsigfigs" value="'.$numericalsigfigs.'" /></label>'.
  368:                    '</td></tr></table><br />');
  369:             $r->print(&hint());
  370:         }
  371: # Store Button
  372: 	$r->print(
  373:   '<input type="submit" value="'.&mt('Save and Edit').'" /></form>');
  374:     } else {
  375: 	$r->print(&mt('Could not identify problem.'));
  376:     }
  377:     $r->print(&Apache::loncommon::end_page());
  378:     return OK;
  379: } 
  380: 
  381: 1;
  382: __END__

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