Annotation of loncom/homework/structuretags.pm, revision 1.77

1.34      albertel    1: # The LearningOnline Network with CAPA 
                      2: # definition of tags that give a structure to a document
1.74      albertel    3: #
1.77    ! sakharuk    4: # $Id: structuretags.pm,v 1.74 2001/12/04 15:17:56 albertel Exp $
1.74      albertel    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: #
1.33      albertel   28: # 2/19 Guy
1.44      ng         29: # 6/26/2001 fixed extra web display at end of <web></web> tags
1.60      www        30: # 8/17,8/18,8/20 Gerd Kortemeyer
1.54      www        31: 
1.1       albertel   32: package Apache::structuretags; 
                     33: 
                     34: use strict;
                     35: use Apache::lonnet;
                     36: 
1.77    ! sakharuk   37: sub BEGIN {
1.48      albertel   38:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','library','web','tex','part','preduedate','postanswerdate','solved','notsolved','startouttext','endouttext'));
1.37      albertel   39: #  &Apache::lonxml::register_insert('problem','',('part','postanswerdate','preduedate'))
1.10      albertel   40: }
                     41: 
                     42: sub start_web {
1.40      albertel   43:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.10      albertel   44:   my $bodytext=&Apache::lonxml::get_all_text("/web",$$parser[$#$parser]);
1.19      albertel   45:   if ($target eq 'web') {
                     46:     return $bodytext;
                     47:   } 
                     48:   return '';
1.10      albertel   49: }
                     50: 
                     51: sub end_web {
1.44      ng         52:     return '';
1.10      albertel   53: }
                     54: 
                     55: sub start_tex {
1.40      albertel   56:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.10      albertel   57:   my $bodytext=&Apache::lonxml::get_all_text("/tex",$$parser[$#$parser]);
1.19      albertel   58:   if ($target eq 'tex') {
                     59:     return $bodytext
                     60:   }
1.10      albertel   61:   return '';
                     62: }
                     63: 
                     64: sub end_tex {
1.44      ng         65:     return '';
1.9       albertel   66: }
                     67: 
1.48      albertel   68: sub page_start {
                     69:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                     70:   my $result=&Apache::londefdef::start_html($target,$token,$tagstack,$parstack,$parser,$safeeval);
                     71:   my $head_tag_start='<head>'.&Apache::lonxml::registerurl();
                     72:   my $body_tag_start='<body onLoad="'.&Apache::lonxml::loadevents().'" '.
1.50      albertel   73: 	  'onUnload="'.&Apache::lonxml::unloadevents().'" ';
                     74:   my $background=&Apache::lonxml::get_param('background',$parstack,$safeeval);
                     75:   if ($background) {
                     76:     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
                     77:                                         $background;
                     78:     $body_tag_start.='background="'.$background.'" ';
                     79:   } else {
                     80:     my $bgcolor=&Apache::lonxml::get_param('bgcolor',$parstack,$safeeval);
                     81:     if ($bgcolor) {
                     82:       $body_tag_start.='bgcolor="'.$bgcolor.'" ';
                     83:     } else {
                     84:       $body_tag_start.='bgcolor="#ffffff"';
                     85:     }
                     86:   }
                     87:   $body_tag_start.='>';
1.48      albertel   88:   return ($result,$head_tag_start,$body_tag_start);
                     89: }
                     90: 
1.65      albertel   91: sub initialize_storage {
                     92:   %Apache::lonhomework::results=();
1.66      albertel   93:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
1.67      albertel   94:   if ($ENV{'request.state'} eq 'construct') {
                     95:     %Apache::lonhomework::history=
1.72      albertel   96:       &Apache::lonnet::tmprestore($ENV{'request.uri'},'',$domain,$name);
1.67      albertel   97:     my ($temp)=keys %Apache::lonhomework::history ;
                     98:     &Apache::lonxml::debug("Return message of $temp");
                     99:   } else {
                    100:     %Apache::lonhomework::history=
                    101:       &Apache::lonnet::restore($symb,$courseid,$domain,$name);
                    102:   }
1.65      albertel  103:   #ignore error conditions
                    104:   my ($temp)=keys %Apache::lonhomework::history ;
                    105:   if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
                    106: }
                    107: 
                    108: # -------------------------------------------------------------finalize_storage
                    109: # Stores away the result has to a student's environment
                    110: # checks form.grade_ for specific values, other wises stores
                    111: # to the running users environment
                    112: sub finalize_storage {
                    113:   my $result;
                    114:   my ($temp) = keys %Apache::lonhomework::results;
                    115:   if ( $temp ne '' ) {
1.66      albertel  116:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
1.67      albertel  117:     if ($ENV{'request.state'} eq 'construct') {
1.72      albertel  118:       $result=&Apache::lonnet::tmpstore(\%Apache::lonhomework::results,
                    119: 				      $ENV{'request.uri'},'',$domain,$name);
1.67      albertel  120:       &Apache::lonxml::debug('Construct Store return message:'.$result);
                    121:     } else {
                    122:       $result=&Apache::lonnet::cstore(\%Apache::lonhomework::results,
                    123: 				      $symb,$courseid,$domain,$name);
                    124:       &Apache::lonxml::debug('Store return message:'.$result);
                    125:     }
1.65      albertel  126:   }
                    127:   return $result;
                    128: }
                    129: 
                    130: sub checkout_msg {
                    131: return (<<ENDCHECKOUT);
                    132: <h2>The resource needs to be checked out</h2>
                    133: As a resource gets checked out, a unique timestamped ID is given to it, and a
                    134: permanent record is left in the system.<p />
                    135: <font color=red>
                    136: Checking out resources is subject to course policies, and may exclude future
                    137: credit even if done erroneously.<p />
                    138: </font>  
                    139: <form method=post>
                    140: <input type=button name="doescheckout" 
                    141: value="Check out Exam for Viewing" 
                    142: onClick="if (confirm('Check out Exam?')) { this.form.submit(); }" />
                    143: </form>
                    144: ENDCHECKOUT
                    145: }
                    146: 
1.9       albertel  147: sub start_problem {
1.40      albertel  148:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.19      albertel  149: 
                    150: #intialize globals
                    151:   $Apache::inputtags::part='0';
                    152:   @Apache::inputtags::responselist = ();
1.51      albertel  153:   @Apache::inputtags::previous=();
1.65      albertel  154:   &initialize_storage();
1.73      albertel  155:   if ($target eq 'web') {
                    156:     &Apache::lonhomework::showhash(%Apache::lonhomework::history);
                    157:   }
1.42      albertel  158:   $Apache::lonhomework::type=&Apache::lonnet::EXT('resource.0.type');
                    159:   &Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
                    160:   if ($Apache::lonhomework::type eq '') {
1.53      albertel  161:     my $uri=$ENV{'request.uri'};
                    162:     if ($uri=~/\.(\w+)$/) {
                    163:       $Apache::lonhomework::type=$1;
                    164:       &Apache::lonxml::debug("Using type of $1");
                    165:     } else {
                    166:       $Apache::lonhomework::type='problem';
                    167:       &Apache::lonxml::debug("Using default type, problem, :$uri:");
                    168:     }
1.42      albertel  169:   }
1.58      www       170: 
1.67      albertel  171:   #added vars to the scripting enviroment
1.19      albertel  172:   my $expression='$external::part='.$Apache::inputtags::part.';';
                    173:   &Apache::run::run($expression,$safeeval);
1.22      albertel  174:   my $status;
1.55      www       175:   my $accessmsg;
1.36      albertel  176: 
1.41      albertel  177:   #should get back a <html> or the neccesary stuff to start XML/MathML
1.48      albertel  178:   my ($result,$head_tag_start,$body_tag_start)=
                    179:     &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
1.36      albertel  180: 
1.67      albertel  181: 
1.68      albertel  182:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
1.67      albertel  183:     #handle exam checkout
                    184:     if ($Apache::lonhomework::type eq 'exam') {
                    185:       my $token=$Apache::lonhomework::history{"resource.0.outtoken"};
                    186:       if (($ENV{'form.doescheckout'}) && (!$token)) {
                    187: 	$token=&Apache::lonxml::maketoken();
                    188: 	$Apache::lonhomework::history{"resource.0.outtoken"}=$token;
                    189:       }
                    190:       $body_tag_start.=&Apache::lonxml::printtokenheader($target,$token);
                    191:     }
                    192: 
                    193:     #handle rand seed in construction space
                    194:     my $rndseed;
                    195:     if ($ENV{'request.state'} eq "construct") {
                    196:       $rndseed=$ENV{'form.rndseed'};
                    197:       if (!$rndseed) {
                    198: 	$rndseed=time;
                    199: 	$ENV{'form.rndseed'}=$rndseed;
1.58      www       200:       }
1.67      albertel  201:       &Apache::run::run('$external::randomseed='.$rndseed.';',$safeeval);
                    202:     }
1.55      www       203:     ($status,$accessmsg) = &Apache::lonhomework::check_access('0');
1.22      albertel  204:     push (@Apache::inputtags::status,$status);
1.24      albertel  205:     my $expression='$external::datestatus="'.$status.'";';
                    206:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
                    207:     &Apache::run::run($expression,$safeeval);
1.54      www       208:     if (( $status eq 'CLOSED' ) ||
                    209:         ( $status eq 'UNCHECKEDOUT') ||
                    210:         ( $status eq 'BANNED')) {
1.21      albertel  211:       my $bodytext=&Apache::lonxml::get_all_text("/problem",$$parser[$#$parser]);
                    212:       if ( $target eq "web" ) {
1.41      albertel  213: 	$result.= $head_tag_start.'</head>';
1.54      www       214:         my $msg=$body_tag_start.
                    215: 	    '<h1>Not open to be viewed</h1>';
                    216:         if ($status eq 'CLOSED') {
1.55      www       217: 	    $msg.='The problem '.$accessmsg;
1.54      www       218: 	} elsif ($status eq 'UNCHECKEDOUT') {
1.65      albertel  219:             $msg.=&checkout_msg;
1.54      www       220:         }
1.67      albertel  221: 	$result.=$msg.'<br />';
                    222:       }
                    223:     } elsif ($target eq 'web') {
                    224:       my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    225:       if ($name eq '') { 
                    226: 	$name=&Apache::lonnet::EXT('resource.title');
                    227: 	if ($name eq 'con_lost') { $name = ''; }
1.21      albertel  228:       }
1.67      albertel  229:       $Apache::lonhomework::name=$name;
                    230:       if ($status eq 'CAN_ANSWER') {
                    231: 	# create a page header and exit
                    232: 	$result.="$head_tag_start<title>$name</title></head>\n
1.41      albertel  233:               $body_tag_start\n
1.36      albertel  234:               <form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">".
                    235: 		'<input type="hidden" name="submitted" value="yes" />';
1.67      albertel  236: 	if ($ENV{'request.state'} eq "construct") {
                    237: 	  $result.=
                    238: 	    '<input type="hidden" name="problemmode" value="View" />
                    239:              <input type="submit" name="problemmode" value="Edit" />
1.73      albertel  240:              <input type="submit" name="problemmode" value="EditXML" />
1.67      albertel  241:              Random Seed:<input type="text" name="rndseed" width="10" value="'.
1.72      albertel  242: 	       $rndseed.'" />
1.73      albertel  243:              <input type="submit" name="changerandseed" value="Change" />
1.72      albertel  244:              <input type="submit" name="resetdata" value="Reset Submissions" />
                    245:              <hr />';
1.67      albertel  246: 	}
                    247: 	# if we are viewing someone else preserve that info
                    248: 	if (defined $ENV{'form.grade_symb'}) {
                    249: 	  foreach my $field ('symb','courseid','domain','username') {
                    250: 	    $result .= '<input type="hidden" name="grade_'.$field.
                    251: 	      '" value="'.$ENV{"form.grade_$field"}.'" />'."\n";
                    252: 	  }
1.65      albertel  253: 	}
1.67      albertel  254:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER'
                    255: 	       || $status eq 'CLOSED') {
                    256: 	$result.=$head_tag_start.
                    257: 	  "<title>$name</title></head>\n$body_tag_start\n";
1.65      albertel  258:       }
1.22      albertel  259:     }
1.67      albertel  260:   } elsif ($target eq 'edit') {
1.41      albertel  261:     $result.=$head_tag_start."</head>".$body_tag_start.
1.67      albertel  262:       '<form name="lonhomework" method="POST" action="'.
                    263: 	$ENV{'request.uri'}.'">
1.41      albertel  264:        <input type="hidden" name="submitted" value="edit" />
                    265:        <input type="hidden" name="problemmode" value="Edit" />
                    266:        <input type="submit" name="problemmode" value="View" />
1.73      albertel  267:        <input type="submit" name="problemmode" value="EditXML" />
1.41      albertel  268:        <input type="submit" name="Undo" value="undo" /> <hr />
1.49      albertel  269:        <input type="submit" name="submit" value="Submit Changes" /><br />
1.41      albertel  270:       ';
1.39      albertel  271:     my $temp=&Apache::edit::insertlist($target,$token);
1.36      albertel  272:     $result.=$temp;
1.67      albertel  273:   } elsif ($target eq 'modified') {
1.37      albertel  274:     $result=$token->[4];
                    275:     $result.=&Apache::edit::handle_insert();
1.77    ! sakharuk  276:   } elsif ($target eq 'tex') {
        !           277:       $result .= '\begin{document}\noindent\textbf{Problem.}\newline';
1.67      albertel  278:   } else {
                    279:     # page_start returned a starting result, delete it if we don't need it
                    280:     $result = '';
1.37      albertel  281:   }
1.67      albertel  282:   return $result;
1.9       albertel  283: }
                    284: 
                    285: sub end_problem {
1.40      albertel  286:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.16      albertel  287:   my $result='';
1.24      albertel  288:   my $status=$Apache::inputtags::status['-1'];
1.68      albertel  289:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ) {
1.28      albertel  290:     if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
                    291:        $status eq 'CAN_ANSWER') {
1.19      albertel  292:       # if part is zero, no <part>s existed, so we need to the grading
                    293:       &Apache::inputtags::grade;
1.68      albertel  294:     } elsif ( $target eq 'web' && $Apache::inputtags::part eq '0') {
1.19      albertel  295:       # if part is zero, no <part>s existed, so we need show the current 
                    296:       # grading status
1.20      albertel  297:       $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
1.41      albertel  298:     }
1.68      albertel  299:     if (
                    300: 	($target eq 'web' && ($ENV{'request.state'} ne 'construct')) ||
                    301: 	($target eq 'answer')
                    302:        ) {
1.22      albertel  303:       if ($status eq 'CAN_ANSWER') {
                    304: 	$result.="</form></body>\n"; 
1.28      albertel  305:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
1.41      albertel  306: 	$result.="</body>\n";
                    307:       }
1.50      albertel  308:       $result.=&Apache::lonxml::xmlend();
1.22      albertel  309:     }
1.65      albertel  310:     if ($target eq 'grade') { 
                    311:       &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                    312:       &finalize_storage();
                    313:     }
                    314:   } elsif ($target eq 'meta') {
1.18      albertel  315:     if ($Apache::inputtags::part eq '0') {
1.16      albertel  316:       $result=&Apache::response::mandatory_part_meta;
                    317:     }
1.65      albertel  318:   } elsif ($target eq 'edit') {
1.34      albertel  319:     &Apache::lonxml::debug("in end_problem with $target, edit");
1.48      albertel  320:     $result='<br /><input type="submit" name="submit" value="Submit Changes" />';
1.77    ! sakharuk  321:   } elsif ($target eq 'tex') {
        !           322:       $result .= '\end{document}';
1.48      albertel  323:   }
                    324:   return $result;
                    325: }
                    326: 
                    327: sub start_library {
                    328:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    329:   my ($result,$head_tag_start,$body_tag_start)=
                    330:     &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
                    331:   if ($target eq 'edit') {
                    332:     $result.=$head_tag_start."</head>".$body_tag_start.
                    333:       '<form name="lonhomework" method="POST" action="'.$ENV{'request.uri'}.'">
                    334:        <input type="hidden" name="submitted" value="edit" />
                    335:        <input type="hidden" name="problemmode" value="Edit" />
                    336:        <input type="submit" name="problemmode" value="View" />
                    337:        <input type="submit" name="Undo" value="undo" /> <hr />
                    338:       ';
                    339:     my $temp=&Apache::edit::insertlist($target,$token);
                    340:     $result.=$temp;
                    341:     return $result;
                    342:   }
                    343:   if ($target eq 'modified') {
                    344:     $result=$token->[4];
                    345:     $result.=&Apache::edit::handle_insert();
                    346:     return $result;
                    347:   }
                    348:   return '';
                    349: }
                    350: 
                    351: sub end_library {
                    352:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    353:   my $result='';
                    354:   if ($target eq 'edit') {
1.34      albertel  355:     $result='<br /><input type="submit" name="submit" value="Submit Changes" />';
                    356:   }
1.16      albertel  357:   return $result;
1.1       albertel  358: }
                    359: 
                    360: sub start_block {
1.40      albertel  361:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.1       albertel  362: 
1.68      albertel  363:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
1.38      albertel  364:     my $code = @$parstack[$#$parstack];
                    365:     $code =~ s/\"//g;
                    366:     $code .=';return $condition;';
                    367:     #  print "<br />$code<br />";
                    368:     my $result = &Apache::run::run($code,$safeeval);
                    369:     &Apache::lonxml::debug("block :$code: returned :$result:");
                    370:     if ( ! $result ) { 
                    371:       my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
                    372:       &Apache::lonxml::debug("skipping ahead :$skip: $$parser[$#$parser]");
                    373:     }
1.1       albertel  374:   }
                    375:   return "";
                    376: }
                    377: 
                    378: sub end_block {
1.38      albertel  379:   return '';
1.4       tsai      380: }
                    381: 
                    382: sub start_while {
1.40      albertel  383:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.4       tsai      384: 
                    385:   my $code = @$parstack[$#$parstack];
                    386:   $code =~ s/\"//g;
                    387:   $code .=';return $condition;';
                    388: 
1.5       tsai      389:   push( @Apache::structuretags::whileconds, $code); 
1.4       tsai      390:   my $result = &Apache::run::run($code,$safeeval);
                    391:   my $bodytext=$$parser[$#$parser]->get_text("/while");
1.5       tsai      392:   push( @Apache::structuretags::whilebody, $bodytext);
                    393:   if ( $result ) { 
1.8       albertel  394:     &Apache::lonxml::newparser($parser,\$bodytext);
1.4       tsai      395:   }
                    396:   return "";
                    397: }
                    398: 
                    399: sub end_while {
1.40      albertel  400:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.5       tsai      401:   my $code = pop @Apache::structuretags::whileconds;
                    402:   my $bodytext = pop @Apache::structuretags::whilebody;
                    403:   my $result = &Apache::run::run($code,$safeeval);
                    404:   if ( $result ) { 
1.8       albertel  405:     &Apache::lonxml::newparser($parser,\$bodytext);
1.5       tsai      406:   } 
                    407:   return "";
1.1       albertel  408: }
1.6       tsai      409: 
                    410: # <randomlist> 
                    411: #  <tag1>..</tag1>
                    412: #  <tag2>..</tag2>
                    413: #  <tag3>..</tag3>
                    414: #  ... 
                    415: # </randomlist>
                    416: sub start_randomlist {
1.40      albertel  417:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.71      albertel  418:   if ($target eq 'answer' || $target eq 'grade' || $target eq 'web') {
                    419:     my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
                    420:     my $b_parser= HTML::TokeParser->new(\$body);
                    421:     my $b_tok;
                    422:     my @randomlist;
                    423:     my $list_item;
                    424:     while($b_tok = $b_parser->get_token() ) {
                    425:       if($b_tok->[0] eq 'S') { # start tag
                    426: 	# get content of the tag until matching end tag
                    427: 	# get all text upto the matching tag
                    428: 	# and push the content into @randomlist
                    429: 	$list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
                    430: 	$list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
                    431: 	push(@randomlist,$list_item);
                    432: 	#  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4], $list_item</b>";
                    433:       }
                    434:       if($b_tok->[0] eq 'T') { # text
                    435: 	# what to do with text in between tags?
                    436: 	#  print "<b>TEXT $b_tok->[1]</b><br />";
                    437:       }
                    438:       # if($b_tok->[0] eq 'E') { # end tag, should not happen
                    439:       #  print "<b>END-TAG $b_tok->[1]</b><br />";
                    440:       # }
                    441:     }
1.6       tsai      442: 
1.71      albertel  443:     my @idx_arr = (0 .. $#randomlist);
                    444:     &Apache::structuretags::shuffle(\@idx_arr);
                    445:     my $bodytext = '';
                    446:     for(0 .. $#randomlist) {
                    447:       $bodytext .= "$randomlist[ $idx_arr[$_] ]";
1.6       tsai      448:     }
1.71      albertel  449:     &Apache::lonxml::newparser($parser,\$bodytext);
1.7       tsai      450:   }
1.6       tsai      451:   return "";
1.7       tsai      452: }
                    453: 
                    454: sub shuffle {
                    455:     my $a=shift;
                    456:     my $i;
1.70      albertel  457:     if (defined(@$a)) {
                    458:       &Apache::response::setrandomnumber();
                    459:       for($i=@$a;--$i;) {
                    460: 	my $j=int rand($i+1);
                    461: 	next if $i == $j;
                    462: 	@$a[$i,$j] = @$a[$j,$i];
                    463:       }
1.7       tsai      464:     }
1.6       tsai      465: }
                    466: 
                    467: sub end_randomlist {
1.46      albertel  468:   return '';
1.6       tsai      469: }
                    470: 
1.11      albertel  471: sub start_part {
1.40      albertel  472:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.39      albertel  473:   my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
1.47      albertel  474:   if ($id eq '') { $id = $Apache::lonxml::curdepth; }
1.14      albertel  475:   $Apache::inputtags::part=$id;
1.18      albertel  476:   @Apache::inputtags::responselist = ();
1.51      albertel  477:   @Apache::inputtags::previous=();
1.15      www       478:   if ($target eq 'meta') {
1.16      albertel  479:     return &Apache::response::mandatory_part_meta;
1.68      albertel  480:   } elsif ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
1.55      www       481:     my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
1.23      albertel  482:     push (@Apache::inputtags::status,$status);
                    483:     my $expression='$external::datestatus="'.$status.'";';
                    484:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
                    485:     &Apache::run::run($expression,$safeeval);
                    486:     if ( $status eq 'CLOSED' ) {
1.21      albertel  487:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
                    488:       if ( $target eq "web" ) {
1.55      www       489: 	return "<br />Part is not open to be viewed. It $accessmsg<br />";
1.21      albertel  490:       }
                    491:     }
1.15      www       492:   }
1.19      albertel  493:   return '';
1.11      albertel  494: }
                    495: 
                    496: sub end_part {
1.40      albertel  497:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.18      albertel  498:   &Apache::lonxml::debug("in end_part $target ");
1.28      albertel  499:   my $status=$Apache::inputtags::status['-1'];
1.23      albertel  500:   pop @Apache::inputtags::status;
1.19      albertel  501:   if ( $target eq 'meta' ) { return ''; }
1.37      albertel  502:   if ( $target eq 'grade' && $status eq 'CAN_ANSWER') {
                    503:     return &Apache::inputtags::grade;
1.28      albertel  504:   }
1.37      albertel  505:   if ($target eq 'web') {
                    506:     return &Apache::inputtags::gradestatus($Apache::inputtags::part);
                    507:   }
                    508:   return '';
1.11      albertel  509: }
1.1       albertel  510: 
1.25      albertel  511: sub start_preduedate {
1.40      albertel  512:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.68      albertel  513:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
1.29      albertel  514:     if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
                    515: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
1.26      albertel  516:       &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
1.24      albertel  517:     }
                    518:   }
                    519:   return '';
                    520: }
                    521: 
1.25      albertel  522: sub end_preduedate {
1.24      albertel  523:   return '';
                    524: }
                    525: 
1.25      albertel  526: sub start_postanswerdate {
1.40      albertel  527:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  528:   if ($target eq 'web' || $target eq 'grade') {
1.24      albertel  529:     if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
1.26      albertel  530:       &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
1.24      albertel  531:     }
                    532:   }
                    533:   return '';
                    534: }
                    535: 
1.25      albertel  536: sub end_postanswerdate {
1.24      albertel  537:   return '';
                    538: }
                    539: 
1.25      albertel  540: sub start_notsolved {
1.40      albertel  541:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.68      albertel  542:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
1.24      albertel  543:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                    544:     &Apache::lonxml::debug("not solved has :$gradestatus:");
                    545:     if ($gradestatus =~ /^correct/) {
                    546:       &Apache::lonxml::debug("skipping");
1.26      albertel  547:       &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
1.24      albertel  548:     }
                    549:   }
                    550:   return '';
                    551: }
                    552: 
1.25      albertel  553: sub end_notsolved {
1.24      albertel  554:   return '';
                    555: }
                    556: 
                    557: sub start_solved {
1.40      albertel  558:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.68      albertel  559:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
1.24      albertel  560:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                    561:     if ($gradestatus !~ /^correct/) {
                    562:       &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
                    563:     }
                    564:   }
                    565:   return '';
                    566: }
                    567: 
                    568: sub end_solved {
                    569:   return '';
                    570: }
1.34      albertel  571: 
                    572: sub start_startouttext {
1.40      albertel  573:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.35      albertel  574:   my @result=(''.'');
                    575:   if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
                    576:   return (@result);
1.34      albertel  577: }
                    578: sub end_startouttext {
1.40      albertel  579:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  580:   my $result='';
1.35      albertel  581:   my $text='';
                    582: 
1.34      albertel  583:   if ($target eq 'edit') {
1.70      albertel  584:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser[-1]);
1.43      albertel  585:     $result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
1.42      albertel  586: <td>Delete:".
                    587:   &Apache::edit::deletelist($target,$token)
                    588:   ."</td>
                    589: <td>".
                    590:   &Apache::edit::insertlist($target,$token).
1.77    ! sakharuk  591:     "</td>
        !           592: </tr><tr><td colspan=\"3\">\n".
1.45      albertel  593: 	&Apache::edit::editfield($token->[1],$text,"",50,4);
1.35      albertel  594:   }
                    595:   if ($target eq 'modified') {
                    596:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
                    597:     $result='<startouttext />'.&Apache::edit::modifiedfield();
1.34      albertel  598:   }
                    599:   return $result;
                    600: }
                    601: sub start_endouttext {
1.40      albertel  602:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  603:   my $result='';
1.43      albertel  604:   if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
1.35      albertel  605:   if ($target eq "modified") { $result='<endouttext />'; }
1.34      albertel  606:   return $result;
                    607: }
                    608: sub end_endouttext {
1.40      albertel  609:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.35      albertel  610:   my @result=('','');
                    611:   if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
                    612:   return (@result);
1.34      albertel  613: }
1.45      albertel  614: sub delete_startouttext {
                    615:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    616: #  my $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
                    617:   my $text=$$parser['-1']->get_text("/endouttext");
1.69      albertel  618:   my $ntoken=$$parser['-1']->get_token();
                    619:   &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
                    620:   &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
1.45      albertel  621:   # Deleting 2 parallel tag pairs, but we need the numbers later to look like 
                    622:   # they did the last time round
1.69      albertel  623:   &Apache::lonxml::increasedepth($ntoken);
                    624:   &Apache::lonxml::decreasedepth($ntoken);
1.45      albertel  625:   return 1;
                    626: }
1.34      albertel  627: 
1.1       albertel  628: 1;
                    629: __END__

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