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

1.1       www         1: # The LearningOnline Network
                      2: # Simple Problem Parameter Setting "Editor"
                      3: #
1.17    ! albertel    4: # $Id: lonsimpleproblemedit.pm,v 1.16 2005/05/26 20:35:53 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.17    ! albertel  215:     $r->print(&Apache::loncommon::start_page('Simple Problem Editor',
        !           216: 					     $spell_header));
1.1       www       217:     if ($symb) {
                    218: 	$r->print('<h1>'.&Apache::lonnet::gettitle($symb).'</h1>');
1.12      albertel  219: 	$r->print('<table width="100%" bgcolor="#FFFFAA" border="2"><tr><td>'.
1.13      albertel  220:                 '<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  221: 		  &Apache::loncommon::help_open_topic('Caching').'</td></tr></table>');
1.2       www       222: 	$r->print('<table border="2" bgcolor="#FFFFFF" width="100%"><tr><td>'.
1.11      albertel  223:                   &rawrendering($symb).
1.2       www       224:                   '</td></tr></table><br />');
1.16      albertel  225:         $r->print('<form name="simpleedit" method="POST">');
1.2       www       226: # Question Type        
                    227: 	my %questiontypes=('radio'  => 
                    228:                                '1 out of N multiple choice (radio button)',
1.3       www       229: 			   'option' => 'Option response',
                    230:                            'string' => 'Short string response',
                    231:                            'essay'  => 'Essay, open end');
1.2       www       232:         $qtype=$qparms{$prefix.'questiontype'};
                    233:         unless (defined($qtype)) { $qtype='radio'; }
                    234:         unless ($questiontypes{$qtype}) { $qtype='radio'; }
1.9       www       235:         $r->print('<b>'.&mt('Question Type').
                    236: 		  ': '.&Apache::loncommon::select_form
1.2       www       237: 	                               ($qtype,'questiontype',%questiontypes).
1.12      albertel  238:   '</b><br /><input type="submit" value="'.&mt('Save and Edit').
1.6       www       239:   '" /><p>&nbsp;</p>');
1.2       www       240: # Question Text
                    241:         $r->print(&questiontext());
                    242: # Radio, Option ===
                    243: 	if (($qtype eq 'radio') || ($qtype eq 'option')) {
                    244: # Response
                    245:             my $maxfoils=$qparms{$prefix.'maxfoils'};
                    246:             unless (defined($maxfoils)) { $maxfoils=10; }
                    247:             unless ($maxfoils=~/^\d+$/) { $maxfoils=10; }
                    248:             if ($maxfoils<=0) { $maxfoils=10; }
                    249: 	    my %randomizes=('yes' => 'Display foils in random order',
                    250: 			    'no'  => 'Display foils in order given');
                    251: 	    my $randomize=$qparms{$prefix.'randomize'};
                    252:             unless (defined($randomize)) { $randomize='yes'; }
                    253:             unless ($randomizes{$randomize}) { $randomize='yes'; }
                    254: 	    $r->print(
                    255: 		  '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
1.6       www       256: 	          '<tr><td>'.&mt('Max number of foils displayed').
                    257: ': <input type="text" size="3" name="maxfoils" value="'.$maxfoils.'" />&nbsp;&nbsp;'.
1.2       www       258: 		      &Apache::loncommon::select_form
                    259: 		      ($randomize,'randomize',%randomizes).
                    260: 		  '</td></tr><tr><td bgcolor="#AAAAAA">');
                    261: # Option Response: Options
                    262: 	    if ($qtype eq 'option') {
                    263: 		my $options=$qparms{$prefix.'options'};
                    264:                 unless (defined($options)) { $options="('true','false')"; }
                    265:                 my %optionshash=&evaloptionhash($options);
                    266: 		$r->print(
                    267: 		  '<table bgcolor="#ffcc22" cellspacing="4" cellpadding="2">'.
                    268: 		  '<tr><td><input type="hidden" name="options" value="'.
1.9       www       269:                   $options.'" />'.&mt('Add new option').': '.
1.6       www       270:           '<input type="text" name="newopt" size="15" />'.
                    271:           &mt('Delete an option').': '.
1.2       www       272:           &Apache::loncommon::select_form('','delopt',('' => '',%optionshash)).
                    273:           '</td></tr><tr><td>');
                    274: 	    }
                    275: # Foils
                    276: 	    for (my $i=1;$i<=10;$i++) {
                    277: 		$r->print(&foil($i));
                    278: 	    }
                    279: # End Options
                    280: 	    if ($qtype eq 'option') {
                    281: 		$r->print('</td></tr></table>');
                    282: 	    }
1.1       www       283: 
1.2       www       284: # End Response
                    285: 	    $r->print('</td></tr></table><br />');
1.3       www       286: # Hint
                    287: 	    $r->print(&hint());
1.2       www       288: 	}
1.3       www       289: 	if ($qtype eq 'string') {
                    290:             my %stringtypes=(
                    291: 	       'cs' => 'Case sensitive',
                    292: 	       'ci' => 'Case Insensitive',
                    293: 	       'mc' => 'Multiple Choice, Order of characters unchecked');
                    294:             my $stringanswer=$qparms{$prefix.'stringanswer'};
                    295:             unless (defined($stringanswer)) { $stringanswer=''; }
                    296:             my $stringtype=$qparms{$prefix.'stringtype'};
                    297:             unless (defined($stringtype)) { $stringtype='cs'; }
                    298:             unless ($stringtypes{$stringtype}) { $stringtype='cs'; }
                    299: 	    $r->print(
                    300: 		  '<table bgcolor="#00ee44" cellspacing="4" cellpadding="2">'.
1.6       www       301: 	          '<tr><td>'.&mt('Correct answer').': <input type="text" size="20" name="stringanswer" value="'.$stringanswer.'" />&nbsp;&nbsp;'.
1.3       www       302: 		      &Apache::loncommon::select_form
                    303: 		      ($stringtype,'stringtype',%stringtypes).
                    304: 		  '</td></tr></table><br />');
1.2       www       305: # Hint
1.3       www       306: 	    $r->print(&hint());
                    307: 	}
1.2       www       308: # Store Button
                    309: 	$r->print(
1.12      albertel  310:   '<input type="submit" value="'.&mt('Save and Edit').'" /></form>');
1.1       www       311:     } else {
1.6       www       312: 	$r->print(&mt('Could not identify problem.'));
1.1       www       313:     }
1.17    ! albertel  314:     $r->print(&Apache::loncommon::end_page());
1.1       www       315:     return OK;
                    316: } 
                    317: 
                    318: 1;
                    319: __END__

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