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

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

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