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

1.1       www         1: # The LearningOnline Network
                      2: # Simple Problem Parameter Setting "Editor"
                      3: #
1.26    ! truskell    4: # $Id: lonsimpleproblemedit.pm,v 1.25 2009/08/17 19:46:49 www 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;
1.26    ! truskell   43: my $weightprefix;
1.2       www        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 {
1.11      albertel   58:     my ($symb)=@_;
                     59:     my %data=('show_errors'=>'on',
1.12      albertel   60: 	      'simple_edit_button' => 'off',
1.11      albertel   61: 	      'devalidatecourseresdata'=>'on');
                     62:     return &Apache::loncommon::get_student_view($symb,time,time,
1.15      albertel   63: 						$env{'request.course.id'},
1.11      albertel   64: 						'web',\%data);
1.2       www        65: }
                     66: 
1.1       www        67: sub questiontext {
1.2       www        68:     my $text=$qparms{$prefix.'questiontext'};
1.9       www        69:     my $qt=&mt('Question Text');
1.16      albertel   70:     my $spell_link=
                     71: 	&Apache::lonhtmlcommon::spelllink('simpleedit','questiontext');
1.1       www        72:     return (<<ENDQUESTION);
1.19      albertel   73: <table bgcolor="#dddd22" cellspacing="4" cellpadding="2" style="width:100%">
1.9       www        74: <tr><td><b>$qt</b><br />
1.14      albertel   75: <textarea style="width:100%" name="questiontext" cols="80" rows="8">$text</textarea>
1.16      albertel   76: <br />$spell_link
1.1       www        77: </td></tr>
                     78: </table>
                     79: <br />
                     80: ENDQUESTION
                     81: }
                     82: 
                     83: sub hint {
1.2       www        84:     my $text=$qparms{$prefix.'hinttext'};
1.9       www        85:     my $ht=&mt('Hint Text');
1.16      albertel   86:     my $spell_link=
                     87: 	&Apache::lonhtmlcommon::spelllink('simpleedit','hinttext');
1.1       www        88:     return (<<ENDHINT);
                     89: <table bgcolor="#accacc" cellspacing="4" cellpadding="2">
1.9       www        90: <tr><td><b>$ht</b><br />
1.14      albertel   91: <textarea style="width:100%" name="hinttext" cols="80" rows="4">$text</textarea>
1.16      albertel   92: <br />$spell_link
1.1       www        93: </td></tr>
                     94: </table>
                     95: <br />
                     96: ENDHINT
                     97: }
                     98: 
1.24      www        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: 
1.1       www       112: sub foil {
1.2       www       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:     }
1.1       www       120:     $values{'unused'}='Not shown, not used';
1.2       www       121:     my $value=$qparms{$prefix.'value'.$number};
1.1       www       122:     unless (defined($value)) { $value='unused'; }
                    123:     unless ($values{$value}) { $value='unused'; }
1.2       www       124:     my $position=$qparms{$prefix.'position'.$number};
1.1       www       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);
1.2       www       136:     my $text=$qparms{$prefix.'text'.$number};
1.9       www       137:     my %lt=&Apache::lonlocal::texthash('foil'  => 'Foil',
                    138: 				       'value' => 'Value',
                    139: 				       'pos'   => 'Position',
                    140: 				       'text'  => 'Text');
                    141: 
1.16      albertel  142:     my $spell_link=
                    143: 	&Apache::lonhtmlcommon::spelllink('simpleedit',"text$number");
1.1       www       144:     return (<<ENDFOIL);
                    145: <table bgcolor="#dd55ff" cellspacing="4" cellpadding="2">
1.9       www       146: <tr><td colspan="2"><b>$lt{'foil'}</b></td></tr>
1.10      www       147: <tr><td>$lt{'value'}: $selectvalue</td><td>$lt{'pos'}: $selectposition</td></tr>
1.9       www       148: <tr><td colspan="2">$lt{'text'}:<br />
1.14      albertel  149: <textarea style="width:100%" name="text$number" cols="80" rows="4">$text</textarea>
1.16      albertel  150: <br />$spell_link
1.1       www       151: </td></tr>
                    152: </table>
                    153: <br />
                    154: ENDFOIL
                    155: }
                    156: 
1.13      albertel  157: sub get_parent_uri {
                    158:     my ($cur_symb)=@_;
                    159:     my $navmap = Apache::lonnavmaps::navmap->new();
1.20      raeburn   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;
1.13      albertel  168:     }
1.20      raeburn   169:     return;
1.13      albertel  170: }
                    171: 
1.1       www       172: sub handler {
                    173:     my $r = shift;
                    174: 
                    175:     if ($r->header_only) {
1.6       www       176:         &Apache::loncommon::content_type($r,'text/html');
1.1       www       177:         $r->send_http_header;
                    178:         return OK;
                    179:     }
                    180: 
                    181: # -------------------------------------------------------------------- Allowed?
1.15      albertel  182:     unless (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
1.1       www       183: 	return HTTP_NOT_ACCEPTABLE; 
                    184:     }
                    185: # ----------------------------------------------------------------- Send header
1.6       www       186:     &Apache::loncommon::content_type($r,'text/html');
1.1       www       187:     $r->send_http_header;
                    188: # ----------------------------------------------------- Figure out where we are
                    189:     my $uri=$r->uri;
                    190:     $uri=~s/\/smpedit$//;
1.13      albertel  191:     my $symb=&Apache::lonnet::symbread();
1.1       www       192: 
1.2       www       193: # ------------------------------------------------ Prefix for everything stored
1.15      albertel  194:     $prefix=$env{'request.course.id'}.'.'.$symb.'.0.';
1.26    ! truskell  195: #------------------------------------------------- Prefix for storing weight of Problem Parts
        !           196:     $weightprefix=$env{'request.course.id'}.'.'.$symb.'.';
        !           197:     
1.1       www       198: # ---------------------------------------------------------- Anything to store?
                    199: 
1.15      albertel  200:     if (($symb) && (defined($env{'form.questiontype'}))) {
1.1       www       201:         my %storecontent=();
                    202:         undef %storecontent;
1.15      albertel  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'}};
1.2       www       207: 	    }
1.15      albertel  208: 	    if ($env{'form.newopt'}) {
                    209: 		$env{'form.newopt'}=~s/\'/\\\'/g;
                    210:                 $curoptions{$env{'form.newopt'}}=$env{'form.newopt'};
1.2       www       211: 	    }
1.15      albertel  212:             $env{'form.options'}="('".join("','",keys %curoptions)."')";
1.2       www       213: 	}
1.15      albertel  214: 	$env{'form.hiddenparts'}='!'.$env{'form.questiontype'};
                    215:         foreach (keys %env) {
1.1       www       216: 	    if ($_=~/^form\.(\w+)$/) {
1.2       www       217:                 my $parm=$1;
1.15      albertel  218: 		$storecontent{$prefix.$parm}=$env{'form.'.$parm};
1.2       www       219:                 $storecontent{$prefix.$parm}=~s/^\s+//s;
                    220: 		$storecontent{$prefix.$parm}=~s/\s+$//s;
1.1       www       221: 	    }
                    222: 	}
1.26    ! truskell  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: 
1.1       www       233: 	my $reply=&Apache::lonnet::cput
                    234: 	    ('resourcedata',\%storecontent,
1.15      albertel  235: 	     $env{'course.'.$env{'request.course.id'}.'.domain'},
                    236: 	     $env{'course.'.$env{'request.course.id'}.'.num'});
1.26    ! truskell  237: 
1.25      www       238:         &Apache::lonnet::devalidatecourseresdata(
                    239:                             $env{'course.'.$env{'request.course.id'}.'.num'},
                    240:                             $env{'course.'.$env{'request.course.id'}.'.domain'});
1.1       www       241: 
                    242:     }
1.2       www       243: # ------------------------------------------------------------------- Read Data
                    244: 
                    245:     %qparms=&Apache::lonnet::dump('resourcedata',
1.15      albertel  246: 		     $env{'course.'.$env{'request.course.id'}.'.domain'},
                    247: 		     $env{'course.'.$env{'request.course.id'}.'.num'},
                    248: 		     $env{'request.course.id'}.'.'.$symb);
1.2       www       249: 
1.1       www       250: # ------------------------------------------------------------ Print the screen
1.16      albertel  251:     my $spell_header=&Apache::lonhtmlcommon::spellheader();
1.17      albertel  252:     $r->print(&Apache::loncommon::start_page('Simple Problem Editor',
                    253: 					     $spell_header));
1.1       www       254:     if ($symb) {
                    255: 	$r->print('<h1>'.&Apache::lonnet::gettitle($symb).'</h1>');
1.20      raeburn   256:         my $displaylink = &get_parent_uri($symb);
1.21      bisitz    257:         if ($displaylink ne '') {
1.23      bisitz    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));
1.20      raeburn   264:         } else {
1.21      bisitz    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:             );
1.20      raeburn   272:         }
1.23      bisitz    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:         );
1.21      bisitz    278: 
1.2       www       279: 	$r->print('<table border="2" bgcolor="#FFFFFF" width="100%"><tr><td>'.
1.11      albertel  280:                   &rawrendering($symb).
1.2       www       281:                   '</td></tr></table><br />');
1.22      bisitz    282:         $r->print('<form name="simpleedit" method="post">');
1.2       www       283: # Question Type        
                    284: 	my %questiontypes=('radio'  => 
                    285:                                '1 out of N multiple choice (radio button)',
1.3       www       286: 			   'option' => 'Option response',
                    287:                            'string' => 'Short string response',
1.24      www       288:                            'essay'  => 'Essay, open end',
                    289:                            'numerical' => 'Numerical response');
1.2       www       290:         $qtype=$qparms{$prefix.'questiontype'};
                    291:         unless (defined($qtype)) { $qtype='radio'; }
                    292:         unless ($questiontypes{$qtype}) { $qtype='radio'; }
1.9       www       293:         $r->print('<b>'.&mt('Question Type').
                    294: 		  ': '.&Apache::loncommon::select_form
1.2       www       295: 	                               ($qtype,'questiontype',%questiontypes).
1.12      albertel  296:   '</b><br /><input type="submit" value="'.&mt('Save and Edit').
1.6       www       297:   '" /><p>&nbsp;</p>');
1.24      www       298: # Script
                    299:         if ($qtype eq 'numerical') {
                    300:            $r->print(&script());
                    301:         }
1.2       www       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">'.
1.6       www       318: 	          '<tr><td>'.&mt('Max number of foils displayed').
                    319: ': <input type="text" size="3" name="maxfoils" value="'.$maxfoils.'" />&nbsp;&nbsp;'.
1.2       www       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="'.
1.9       www       331:                   $options.'" />'.&mt('Add new option').': '.
1.6       www       332:           '<input type="text" name="newopt" size="15" />'.
                    333:           &mt('Delete an option').': '.
1.2       www       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: 	    }
1.1       www       345: 
1.2       www       346: # End Response
                    347: 	    $r->print('</td></tr></table><br />');
1.3       www       348: # Hint
                    349: 	    $r->print(&hint());
1.2       www       350: 	}
1.3       www       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">'.
1.25      www       363: 	          '<tr><td><label>'.&mt('Correct answer').': <input type="text" size="20" name="stringanswer" value="'.$stringanswer.'" /></label>&nbsp;&nbsp;'.
1.3       www       364: 		      &Apache::loncommon::select_form
                    365: 		      ($stringtype,'stringtype',%stringtypes).
                    366: 		  '</td></tr></table><br />');
1.2       www       367: # Hint
1.3       www       368: 	    $r->print(&hint());
                    369: 	}
1.24      www       370:         if ($qtype eq 'numerical') {
                    371:            my $numericalanswer=$qparms{$prefix.'numericalanswer'};
                    372:            unless (defined($numericalanswer)) { $numericalanswer=''; }
1.25      www       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'; }
1.24      www       377: 
                    378:            $r->print(
                    379:                   '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
1.25      www       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>'.
1.24      www       385:                   '</td></tr></table><br />');
                    386:            $r->print(&hint());
                    387:         }
1.2       www       388: # Store Button
                    389: 	$r->print(
1.12      albertel  390:   '<input type="submit" value="'.&mt('Save and Edit').'" /></form>');
1.1       www       391:     } else {
1.6       www       392: 	$r->print(&mt('Could not identify problem.'));
1.1       www       393:     }
1.17      albertel  394:     $r->print(&Apache::loncommon::end_page());
1.1       www       395:     return OK;
                    396: } 
                    397: 
                    398: 1;
                    399: __END__

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