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

1.1       www         1: # The LearningOnline Network
                      2: # Simple Problem Parameter Setting "Editor"
                      3: #
1.16    ! albertel    4: # $Id: lonsimpleproblemedit.pm,v 1.15 2005/04/07 06:56:22 albertel 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.1       www        37: 
1.2       www        38: my %qparms;
                     39: my $prefix;
                     40: my $qtype;
                     41: 
                     42: sub evaloptionhash {
                     43:     my $options=shift;
                     44:     $options=~s/^\(\'//;
                     45:     $options=~s/\'\)$//;
                     46:     my %returnhash=();
                     47:     foreach (split(/\'\,\'/,$options)) {
                     48: 	$returnhash{$_}=$_;
                     49:     }
                     50:     return %returnhash;
                     51: }
                     52: 
                     53: sub rawrendering {
1.11      albertel   54:     my ($symb)=@_;
                     55:     my %data=('show_errors'=>'on',
1.12      albertel   56: 	      'simple_edit_button' => 'off',
1.11      albertel   57: 	      'devalidatecourseresdata'=>'on');
                     58:     return &Apache::loncommon::get_student_view($symb,time,time,
1.15      albertel   59: 						$env{'request.course.id'},
1.11      albertel   60: 						'web',\%data);
1.2       www        61: }
                     62: 
1.1       www        63: sub questiontext {
1.2       www        64:     my $text=$qparms{$prefix.'questiontext'};
1.9       www        65:     my $qt=&mt('Question Text');
1.16    ! albertel   66:     my $spell_link=
        !            67: 	&Apache::lonhtmlcommon::spelllink('simpleedit','questiontext');
1.1       www        68:     return (<<ENDQUESTION);
                     69: <table bgcolor="#dddd22" cellspacing="4" cellpadding="2">
1.9       www        70: <tr><td><b>$qt</b><br />
1.14      albertel   71: <textarea style="width:100%" name="questiontext" cols="80" rows="8">$text</textarea>
1.16    ! albertel   72: <br />$spell_link
1.1       www        73: </td></tr>
                     74: </table>
                     75: <br />
                     76: ENDQUESTION
                     77: }
                     78: 
                     79: sub hint {
1.2       www        80:     my $text=$qparms{$prefix.'hinttext'};
1.9       www        81:     my $ht=&mt('Hint Text');
1.16    ! albertel   82:     my $spell_link=
        !            83: 	&Apache::lonhtmlcommon::spelllink('simpleedit','hinttext');
1.1       www        84:     return (<<ENDHINT);
                     85: <table bgcolor="#accacc" cellspacing="4" cellpadding="2">
1.9       www        86: <tr><td><b>$ht</b><br />
1.14      albertel   87: <textarea style="width:100%" name="hinttext" cols="80" rows="4">$text</textarea>
1.16    ! albertel   88: <br />$spell_link
1.1       www        89: </td></tr>
                     90: </table>
                     91: <br />
                     92: ENDHINT
                     93: }
                     94: 
                     95: sub foil {
1.2       www        96:     my $number=shift;
                     97:     my %values='';
                     98:     if ($qtype eq 'radio') {
                     99: 	%values=('true' => 'True', 'false' => 'False');
                    100:     } elsif ($qtype eq 'option') {
                    101: 	%values=&evaloptionhash($qparms{$prefix.'options'});
                    102:     }
1.1       www       103:     $values{'unused'}='Not shown, not used';
1.2       www       104:     my $value=$qparms{$prefix.'value'.$number};
1.1       www       105:     unless (defined($value)) { $value='unused'; }
                    106:     unless ($values{$value}) { $value='unused'; }
1.2       www       107:     my $position=$qparms{$prefix.'position'.$number};
1.1       www       108:     my %positions=('random' => 'Random position',
                    109: 		   'top'    => 'Show always at top position',
                    110: 		   'bottom' => 'Show always at bottom position');
                    111:     unless (defined($position)) { $position='random'; }
                    112:     unless ($positions{$position}) {
                    113: 	$position='random';
                    114:     }
                    115:     my $selectvalue=&Apache::loncommon::select_form
                    116: 	                                ($value,'value'.$number,%values);
                    117:     my $selectposition=&Apache::loncommon::select_form
                    118:                                ($position,'position'.$number,%positions);
1.2       www       119:     my $text=$qparms{$prefix.'text'.$number};
1.9       www       120:     my %lt=&Apache::lonlocal::texthash('foil'  => 'Foil',
                    121: 				       'value' => 'Value',
                    122: 				       'pos'   => 'Position',
                    123: 				       'text'  => 'Text');
                    124: 
1.16    ! albertel  125:     my $spell_link=
        !           126: 	&Apache::lonhtmlcommon::spelllink('simpleedit',"text$number");
1.1       www       127:     return (<<ENDFOIL);
                    128: <table bgcolor="#dd55ff" cellspacing="4" cellpadding="2">
1.9       www       129: <tr><td colspan="2"><b>$lt{'foil'}</b></td></tr>
1.10      www       130: <tr><td>$lt{'value'}: $selectvalue</td><td>$lt{'pos'}: $selectposition</td></tr>
1.9       www       131: <tr><td colspan="2">$lt{'text'}:<br />
1.14      albertel  132: <textarea style="width:100%" name="text$number" cols="80" rows="4">$text</textarea>
1.16    ! albertel  133: <br />$spell_link
1.1       www       134: </td></tr>
                    135: </table>
                    136: <br />
                    137: ENDFOIL
                    138: }
                    139: 
1.13      albertel  140: sub get_parent_uri {
                    141:     my ($cur_symb)=@_;
                    142:     my $navmap = Apache::lonnavmaps::navmap->new();
                    143:     my $it = $navmap->getIterator(undef, undef, undef, 1);
                    144:     while ( my $res=$it->next()) {
                    145: 	if (ref($res) &&  $res->symb() eq  $cur_symb) { last; }
                    146:     }
                    147:     my ($src,$symb,$anchor)=&Apache::lonnavmaps::getLinkForResource($it->getStack());
                    148:     if (defined($anchor)) { $anchor='#'.$anchor; }
                    149:     return $src.'?symb='.&Apache::lonnet::escape($symb).$anchor;
                    150: }
                    151: 
1.1       www       152: sub handler {
                    153:     my $r = shift;
                    154: 
                    155:     if ($r->header_only) {
1.6       www       156:         &Apache::loncommon::content_type($r,'text/html');
1.1       www       157:         $r->send_http_header;
                    158:         return OK;
                    159:     }
                    160: 
                    161: # -------------------------------------------------------------------- Allowed?
1.15      albertel  162:     unless (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
1.1       www       163: 	return HTTP_NOT_ACCEPTABLE; 
                    164:     }
                    165: # ----------------------------------------------------------------- Send header
1.6       www       166:     &Apache::loncommon::content_type($r,'text/html');
1.1       www       167:     $r->send_http_header;
                    168: # ----------------------------------------------------- Figure out where we are
                    169:     my $uri=$r->uri;
                    170:     $uri=~s/\/smpedit$//;
1.13      albertel  171:     my $symb=&Apache::lonnet::symbread();
1.1       www       172: 
1.2       www       173: # ------------------------------------------------ Prefix for everything stored
1.15      albertel  174:     $prefix=$env{'request.course.id'}.'.'.$symb.'.0.';
1.1       www       175: # ---------------------------------------------------------- Anything to store?
                    176: 
1.15      albertel  177:     if (($symb) && (defined($env{'form.questiontype'}))) {
1.1       www       178:         my %storecontent=();
                    179:         undef %storecontent;
1.15      albertel  180:         if ($env{'form.questiontype'} eq 'option') {
                    181: 	    my %curoptions=&evaloptionhash($env{'form.options'});
                    182: 	    if ($env{'form.delopt'}) {
                    183: 		delete $curoptions{$env{'form.delopt'}};
1.2       www       184: 	    }
1.15      albertel  185: 	    if ($env{'form.newopt'}) {
                    186: 		$env{'form.newopt'}=~s/\'/\\\'/g;
                    187:                 $curoptions{$env{'form.newopt'}}=$env{'form.newopt'};
1.2       www       188: 	    }
1.15      albertel  189:             $env{'form.options'}="('".join("','",keys %curoptions)."')";
1.2       www       190: 	}
1.15      albertel  191: 	$env{'form.hiddenparts'}='!'.$env{'form.questiontype'};
                    192:         foreach (keys %env) {
1.1       www       193: 	    if ($_=~/^form\.(\w+)$/) {
1.2       www       194:                 my $parm=$1;
1.15      albertel  195: 		$storecontent{$prefix.$parm}=$env{'form.'.$parm};
1.2       www       196:                 $storecontent{$prefix.$parm}=~s/^\s+//s;
                    197: 		$storecontent{$prefix.$parm}=~s/\s+$//s;
1.1       www       198: 	    }
                    199: 	}
                    200: 	my $reply=&Apache::lonnet::cput
                    201: 	    ('resourcedata',\%storecontent,
1.15      albertel  202: 	     $env{'course.'.$env{'request.course.id'}.'.domain'},
                    203: 	     $env{'course.'.$env{'request.course.id'}.'.num'});
1.1       www       204: 
                    205:     }
1.2       www       206: # ------------------------------------------------------------------- Read Data
                    207: 
                    208:     %qparms=&Apache::lonnet::dump('resourcedata',
1.15      albertel  209: 		     $env{'course.'.$env{'request.course.id'}.'.domain'},
                    210: 		     $env{'course.'.$env{'request.course.id'}.'.num'},
                    211: 		     $env{'request.course.id'}.'.'.$symb);
1.2       www       212: 
1.1       www       213: # ------------------------------------------------------------ Print the screen
1.16    ! albertel  214:     my $spell_header=&Apache::lonhtmlcommon::spellheader();
1.1       www       215:     $r->print(<<ENDDOCUMENT);
                    216: <html>
                    217: <head>
                    218: <title>The LearningOnline Network with CAPA</title>
1.16    ! albertel  219: $spell_header
        !           220: </head>
1.1       www       221: ENDDOCUMENT
                    222:     $r->print(&Apache::loncommon::bodytag('Simple Problem Editor'));
                    223:     if ($symb) {
                    224: 	$r->print('<h1>'.&Apache::lonnet::gettitle($symb).'</h1>');
1.12      albertel  225: 	$r->print('<table width="100%" bgcolor="#FFFFAA" border="2"><tr><td>'.
1.13      albertel  226:                 '<a href="'.&get_parent_uri($symb).'">'.&mt('Student View').'</a> - '.&mt('Note: it can take up to 10 minutes for changes to take effect for all users.').
1.12      albertel  227: 		  &Apache::loncommon::help_open_topic('Caching').'</td></tr></table>');
1.2       www       228: 	$r->print('<table border="2" bgcolor="#FFFFFF" width="100%"><tr><td>'.
1.11      albertel  229:                   &rawrendering($symb).
1.2       www       230:                   '</td></tr></table><br />');
1.16    ! albertel  231:         $r->print('<form name="simpleedit" method="POST">');
1.2       www       232: # Question Type        
                    233: 	my %questiontypes=('radio'  => 
                    234:                                '1 out of N multiple choice (radio button)',
1.3       www       235: 			   'option' => 'Option response',
                    236:                            'string' => 'Short string response',
                    237:                            'essay'  => 'Essay, open end');
1.2       www       238:         $qtype=$qparms{$prefix.'questiontype'};
                    239:         unless (defined($qtype)) { $qtype='radio'; }
                    240:         unless ($questiontypes{$qtype}) { $qtype='radio'; }
1.9       www       241:         $r->print('<b>'.&mt('Question Type').
                    242: 		  ': '.&Apache::loncommon::select_form
1.2       www       243: 	                               ($qtype,'questiontype',%questiontypes).
1.12      albertel  244:   '</b><br /><input type="submit" value="'.&mt('Save and Edit').
1.6       www       245:   '" /><p>&nbsp;</p>');
1.2       www       246: # Question Text
                    247:         $r->print(&questiontext());
                    248: # Radio, Option ===
                    249: 	if (($qtype eq 'radio') || ($qtype eq 'option')) {
                    250: # Response
                    251:             my $maxfoils=$qparms{$prefix.'maxfoils'};
                    252:             unless (defined($maxfoils)) { $maxfoils=10; }
                    253:             unless ($maxfoils=~/^\d+$/) { $maxfoils=10; }
                    254:             if ($maxfoils<=0) { $maxfoils=10; }
                    255: 	    my %randomizes=('yes' => 'Display foils in random order',
                    256: 			    'no'  => 'Display foils in order given');
                    257: 	    my $randomize=$qparms{$prefix.'randomize'};
                    258:             unless (defined($randomize)) { $randomize='yes'; }
                    259:             unless ($randomizes{$randomize}) { $randomize='yes'; }
                    260: 	    $r->print(
                    261: 		  '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
1.6       www       262: 	          '<tr><td>'.&mt('Max number of foils displayed').
                    263: ': <input type="text" size="3" name="maxfoils" value="'.$maxfoils.'" />&nbsp;&nbsp;'.
1.2       www       264: 		      &Apache::loncommon::select_form
                    265: 		      ($randomize,'randomize',%randomizes).
                    266: 		  '</td></tr><tr><td bgcolor="#AAAAAA">');
                    267: # Option Response: Options
                    268: 	    if ($qtype eq 'option') {
                    269: 		my $options=$qparms{$prefix.'options'};
                    270:                 unless (defined($options)) { $options="('true','false')"; }
                    271:                 my %optionshash=&evaloptionhash($options);
                    272: 		$r->print(
                    273: 		  '<table bgcolor="#ffcc22" cellspacing="4" cellpadding="2">'.
                    274: 		  '<tr><td><input type="hidden" name="options" value="'.
1.9       www       275:                   $options.'" />'.&mt('Add new option').': '.
1.6       www       276:           '<input type="text" name="newopt" size="15" />'.
                    277:           &mt('Delete an option').': '.
1.2       www       278:           &Apache::loncommon::select_form('','delopt',('' => '',%optionshash)).
                    279:           '</td></tr><tr><td>');
                    280: 	    }
                    281: # Foils
                    282: 	    for (my $i=1;$i<=10;$i++) {
                    283: 		$r->print(&foil($i));
                    284: 	    }
                    285: # End Options
                    286: 	    if ($qtype eq 'option') {
                    287: 		$r->print('</td></tr></table>');
                    288: 	    }
1.1       www       289: 
1.2       www       290: # End Response
                    291: 	    $r->print('</td></tr></table><br />');
1.3       www       292: # Hint
                    293: 	    $r->print(&hint());
1.2       www       294: 	}
1.3       www       295: 	if ($qtype eq 'string') {
                    296:             my %stringtypes=(
                    297: 	       'cs' => 'Case sensitive',
                    298: 	       'ci' => 'Case Insensitive',
                    299: 	       'mc' => 'Multiple Choice, Order of characters unchecked');
                    300:             my $stringanswer=$qparms{$prefix.'stringanswer'};
                    301:             unless (defined($stringanswer)) { $stringanswer=''; }
                    302:             my $stringtype=$qparms{$prefix.'stringtype'};
                    303:             unless (defined($stringtype)) { $stringtype='cs'; }
                    304:             unless ($stringtypes{$stringtype}) { $stringtype='cs'; }
                    305: 	    $r->print(
                    306: 		  '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
1.6       www       307: 	          '<tr><td>'.&mt('Correct answer').': <input type="text" size="20" name="stringanswer" value="'.$stringanswer.'" />&nbsp;&nbsp;'.
1.3       www       308: 		      &Apache::loncommon::select_form
                    309: 		      ($stringtype,'stringtype',%stringtypes).
                    310: 		  '</td></tr></table><br />');
1.2       www       311: # Hint
1.3       www       312: 	    $r->print(&hint());
                    313: 	}
1.2       www       314: # Store Button
                    315: 	$r->print(
1.12      albertel  316:   '<input type="submit" value="'.&mt('Save and Edit').'" /></form>');
1.1       www       317:     } else {
1.6       www       318: 	$r->print(&mt('Could not identify problem.'));
1.1       www       319:     }
                    320:     $r->print('</body></html>');
                    321:     return OK;
                    322: } 
                    323: 
                    324: 1;
                    325: __END__

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