File:  [LON-CAPA] / loncom / homework / lonsimpleproblemedit.pm
Revision 1.26: download - view: text, annotated - select for diffs
Thu Sep 24 00:50:03 2009 UTC (14 years, 7 months ago) by truskell
Branches: MAIN
CVS tags: HEAD
The weight of unused, hidden parts should be zero for "Simple Problems".

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

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