File:  [LON-CAPA] / loncom / homework / lonsimpleproblemedit.pm
Revision 1.16: download - view: text, annotated - select for diffs
Thu May 26 20:35:53 2005 UTC (18 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_X, version_2_0_99_1, version_2_0_2, version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, version_1_99_1, version_1_99_0, HEAD
- added spell check links to simple problem editor

    1: # The LearningOnline Network
    2: # Simple Problem Parameter Setting "Editor"
    3: #
    4: # $Id: lonsimpleproblemedit.pm,v 1.16 2005/05/26 20:35:53 albertel Exp $
    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;
   35: use Apache::lonlocal;
   36: use Apache::lonnavmaps;
   37: 
   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 {
   54:     my ($symb)=@_;
   55:     my %data=('show_errors'=>'on',
   56: 	      'simple_edit_button' => 'off',
   57: 	      'devalidatecourseresdata'=>'on');
   58:     return &Apache::loncommon::get_student_view($symb,time,time,
   59: 						$env{'request.course.id'},
   60: 						'web',\%data);
   61: }
   62: 
   63: sub questiontext {
   64:     my $text=$qparms{$prefix.'questiontext'};
   65:     my $qt=&mt('Question Text');
   66:     my $spell_link=
   67: 	&Apache::lonhtmlcommon::spelllink('simpleedit','questiontext');
   68:     return (<<ENDQUESTION);
   69: <table bgcolor="#dddd22" cellspacing="4" cellpadding="2">
   70: <tr><td><b>$qt</b><br />
   71: <textarea style="width:100%" name="questiontext" cols="80" rows="8">$text</textarea>
   72: <br />$spell_link
   73: </td></tr>
   74: </table>
   75: <br />
   76: ENDQUESTION
   77: }
   78: 
   79: sub hint {
   80:     my $text=$qparms{$prefix.'hinttext'};
   81:     my $ht=&mt('Hint Text');
   82:     my $spell_link=
   83: 	&Apache::lonhtmlcommon::spelllink('simpleedit','hinttext');
   84:     return (<<ENDHINT);
   85: <table bgcolor="#accacc" cellspacing="4" cellpadding="2">
   86: <tr><td><b>$ht</b><br />
   87: <textarea style="width:100%" name="hinttext" cols="80" rows="4">$text</textarea>
   88: <br />$spell_link
   89: </td></tr>
   90: </table>
   91: <br />
   92: ENDHINT
   93: }
   94: 
   95: sub foil {
   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:     }
  103:     $values{'unused'}='Not shown, not used';
  104:     my $value=$qparms{$prefix.'value'.$number};
  105:     unless (defined($value)) { $value='unused'; }
  106:     unless ($values{$value}) { $value='unused'; }
  107:     my $position=$qparms{$prefix.'position'.$number};
  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);
  119:     my $text=$qparms{$prefix.'text'.$number};
  120:     my %lt=&Apache::lonlocal::texthash('foil'  => 'Foil',
  121: 				       'value' => 'Value',
  122: 				       'pos'   => 'Position',
  123: 				       'text'  => 'Text');
  124: 
  125:     my $spell_link=
  126: 	&Apache::lonhtmlcommon::spelllink('simpleedit',"text$number");
  127:     return (<<ENDFOIL);
  128: <table bgcolor="#dd55ff" cellspacing="4" cellpadding="2">
  129: <tr><td colspan="2"><b>$lt{'foil'}</b></td></tr>
  130: <tr><td>$lt{'value'}: $selectvalue</td><td>$lt{'pos'}: $selectposition</td></tr>
  131: <tr><td colspan="2">$lt{'text'}:<br />
  132: <textarea style="width:100%" name="text$number" cols="80" rows="4">$text</textarea>
  133: <br />$spell_link
  134: </td></tr>
  135: </table>
  136: <br />
  137: ENDFOIL
  138: }
  139: 
  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: 
  152: sub handler {
  153:     my $r = shift;
  154: 
  155:     if ($r->header_only) {
  156:         &Apache::loncommon::content_type($r,'text/html');
  157:         $r->send_http_header;
  158:         return OK;
  159:     }
  160: 
  161: # -------------------------------------------------------------------- Allowed?
  162:     unless (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
  163: 	return HTTP_NOT_ACCEPTABLE; 
  164:     }
  165: # ----------------------------------------------------------------- Send header
  166:     &Apache::loncommon::content_type($r,'text/html');
  167:     $r->send_http_header;
  168: # ----------------------------------------------------- Figure out where we are
  169:     my $uri=$r->uri;
  170:     $uri=~s/\/smpedit$//;
  171:     my $symb=&Apache::lonnet::symbread();
  172: 
  173: # ------------------------------------------------ Prefix for everything stored
  174:     $prefix=$env{'request.course.id'}.'.'.$symb.'.0.';
  175: # ---------------------------------------------------------- Anything to store?
  176: 
  177:     if (($symb) && (defined($env{'form.questiontype'}))) {
  178:         my %storecontent=();
  179:         undef %storecontent;
  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'}};
  184: 	    }
  185: 	    if ($env{'form.newopt'}) {
  186: 		$env{'form.newopt'}=~s/\'/\\\'/g;
  187:                 $curoptions{$env{'form.newopt'}}=$env{'form.newopt'};
  188: 	    }
  189:             $env{'form.options'}="('".join("','",keys %curoptions)."')";
  190: 	}
  191: 	$env{'form.hiddenparts'}='!'.$env{'form.questiontype'};
  192:         foreach (keys %env) {
  193: 	    if ($_=~/^form\.(\w+)$/) {
  194:                 my $parm=$1;
  195: 		$storecontent{$prefix.$parm}=$env{'form.'.$parm};
  196:                 $storecontent{$prefix.$parm}=~s/^\s+//s;
  197: 		$storecontent{$prefix.$parm}=~s/\s+$//s;
  198: 	    }
  199: 	}
  200: 	my $reply=&Apache::lonnet::cput
  201: 	    ('resourcedata',\%storecontent,
  202: 	     $env{'course.'.$env{'request.course.id'}.'.domain'},
  203: 	     $env{'course.'.$env{'request.course.id'}.'.num'});
  204: 
  205:     }
  206: # ------------------------------------------------------------------- Read Data
  207: 
  208:     %qparms=&Apache::lonnet::dump('resourcedata',
  209: 		     $env{'course.'.$env{'request.course.id'}.'.domain'},
  210: 		     $env{'course.'.$env{'request.course.id'}.'.num'},
  211: 		     $env{'request.course.id'}.'.'.$symb);
  212: 
  213: # ------------------------------------------------------------ Print the screen
  214:     my $spell_header=&Apache::lonhtmlcommon::spellheader();
  215:     $r->print(<<ENDDOCUMENT);
  216: <html>
  217: <head>
  218: <title>The LearningOnline Network with CAPA</title>
  219: $spell_header
  220: </head>
  221: ENDDOCUMENT
  222:     $r->print(&Apache::loncommon::bodytag('Simple Problem Editor'));
  223:     if ($symb) {
  224: 	$r->print('<h1>'.&Apache::lonnet::gettitle($symb).'</h1>');
  225: 	$r->print('<table width="100%" bgcolor="#FFFFAA" border="2"><tr><td>'.
  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.').
  227: 		  &Apache::loncommon::help_open_topic('Caching').'</td></tr></table>');
  228: 	$r->print('<table border="2" bgcolor="#FFFFFF" width="100%"><tr><td>'.
  229:                   &rawrendering($symb).
  230:                   '</td></tr></table><br />');
  231:         $r->print('<form name="simpleedit" method="POST">');
  232: # Question Type        
  233: 	my %questiontypes=('radio'  => 
  234:                                '1 out of N multiple choice (radio button)',
  235: 			   'option' => 'Option response',
  236:                            'string' => 'Short string response',
  237:                            'essay'  => 'Essay, open end');
  238:         $qtype=$qparms{$prefix.'questiontype'};
  239:         unless (defined($qtype)) { $qtype='radio'; }
  240:         unless ($questiontypes{$qtype}) { $qtype='radio'; }
  241:         $r->print('<b>'.&mt('Question Type').
  242: 		  ': '.&Apache::loncommon::select_form
  243: 	                               ($qtype,'questiontype',%questiontypes).
  244:   '</b><br /><input type="submit" value="'.&mt('Save and Edit').
  245:   '" /><p>&nbsp;</p>');
  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">'.
  262: 	          '<tr><td>'.&mt('Max number of foils displayed').
  263: ': <input type="text" size="3" name="maxfoils" value="'.$maxfoils.'" />&nbsp;&nbsp;'.
  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="'.
  275:                   $options.'" />'.&mt('Add new option').': '.
  276:           '<input type="text" name="newopt" size="15" />'.
  277:           &mt('Delete an option').': '.
  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: 	    }
  289: 
  290: # End Response
  291: 	    $r->print('</td></tr></table><br />');
  292: # Hint
  293: 	    $r->print(&hint());
  294: 	}
  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">'.
  307: 	          '<tr><td>'.&mt('Correct answer').': <input type="text" size="20" name="stringanswer" value="'.$stringanswer.'" />&nbsp;&nbsp;'.
  308: 		      &Apache::loncommon::select_form
  309: 		      ($stringtype,'stringtype',%stringtypes).
  310: 		  '</td></tr></table><br />');
  311: # Hint
  312: 	    $r->print(&hint());
  313: 	}
  314: # Store Button
  315: 	$r->print(
  316:   '<input type="submit" value="'.&mt('Save and Edit').'" /></form>');
  317:     } else {
  318: 	$r->print(&mt('Could not identify problem.'));
  319:     }
  320:     $r->print('</body></html>');
  321:     return OK;
  322: } 
  323: 
  324: 1;
  325: __END__

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