Annotation of loncom/homework/lonsimpleproblemedit.pm, revision 1.27

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

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