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

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