File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.44: download - view: text, annotated - select for diffs
Tue Jun 26 21:09:08 2001 UTC (22 years, 10 months ago) by ng
Branches: MAIN
CVS tags: HEAD
fixed the bug that gives the extra word "web" when using the web and tex tags.

    1: # The LearningOnline Network with CAPA 
    2: # definition of tags that give a structure to a document
    3: # 2/19 Guy
    4: # 6/26/2001 fixed extra web display at end of <web></web> tags
    5: package Apache::structuretags; 
    6: 
    7: use strict;
    8: use Apache::lonnet;
    9: 
   10: sub BEGIN {
   11:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','web','tex','part','preduedate','postanswerdate','solved','notsolved','startouttext','endouttext'));
   12: #  &Apache::lonxml::register_insert('problem','',('part','postanswerdate','preduedate'))
   13: }
   14: 
   15: sub start_web {
   16:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   17:   my $bodytext=&Apache::lonxml::get_all_text("/web",$$parser[$#$parser]);
   18:   if ($target eq 'web') {
   19:     return $bodytext;
   20:   } 
   21:   return '';
   22: }
   23: 
   24: sub end_web {
   25:     return '';
   26: }
   27: 
   28: sub start_tex {
   29:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   30:   my $bodytext=&Apache::lonxml::get_all_text("/tex",$$parser[$#$parser]);
   31:   if ($target eq 'tex') {
   32:     return $bodytext
   33:   }
   34:   return '';
   35: }
   36: 
   37: sub end_tex {
   38:     return '';
   39: }
   40: 
   41: sub start_problem {
   42:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   43: 
   44: #intialize globals
   45:   $Apache::inputtags::part='0';
   46:   @Apache::inputtags::responselist = ();
   47:   $Apache::lonhomework::type=&Apache::lonnet::EXT('resource.0.type');
   48:   &Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
   49:   if ($Apache::lonhomework::type eq '') {
   50:     $Apache::lonhomework::type='homework';
   51:   }
   52: #adeed vars to the scripting enviroment
   53:   my $expression='$external::part='.$Apache::inputtags::part.';';
   54:   &Apache::run::run($expression,$safeeval);
   55:   my $status;
   56:   my $datemsg;
   57: 
   58:   #should get back a <html> or the neccesary stuff to start XML/MathML
   59:   my $result=&Apache::londefdef::start_html($target,$token,$tagstack,$parstack,$parser,$safeeval);
   60: 
   61:   my $head_tag_start='<head>'.&Apache::lonxml::registerurl();
   62:   my $body_tag_start='<body onLoad="'.&Apache::lonxml::loadevents().'" '.
   63: 	  'onUnload="'.&Apache::lonxml::unloadevents().'" '.
   64: 	    'bgcolor="#FFFFFF">';
   65:   if ($target eq 'web' || $target eq 'grade') {
   66:     ($status,$datemsg) = &Apache::lonhomework::check_date('0');
   67:     push (@Apache::inputtags::status,$status);
   68:     my $expression='$external::datestatus="'.$status.'";';
   69:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
   70:     &Apache::run::run($expression,$safeeval);
   71:     if ( $status eq 'CLOSED' ) {
   72:       my $bodytext=&Apache::lonxml::get_all_text("/problem",$$parser[$#$parser]);
   73:       if ( $target eq "web" ) {
   74: 	$result.= $head_tag_start.'</head>';
   75: 	return $result . $body_tag_start .
   76: 	  " <br />Problem is not open to be viewed. The problem $datemsg<br />";
   77:       }
   78:     }
   79:   }
   80:   if ($target eq 'web') {
   81:     my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
   82:     if ($name eq '') { 
   83:       $name=&Apache::lonnet::EXT('resource.title');
   84:       if ($name eq 'con_lost') { $name = ''; }
   85:     }
   86:     $Apache::lonhomework::name=$name;
   87:     if ($status eq 'CAN_ANSWER') {
   88:       # create a page header and exit
   89:       $result.="$head_tag_start<title>$name</title></head>\n
   90:               $body_tag_start\n
   91:               <form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">".
   92: 		'<input type="hidden" name="submitted" value="yes" />';
   93:       if ($ENV{'request.state'} eq "construct") {
   94: 	$result.='<input type="hidden" name="problemmode" value="View" />
   95:                  <input type="submit" name="problemmode" value="Edit" /><hr />';
   96:       }
   97:       return $result;
   98:     } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' || $status eq 'CLOSED') {
   99:       return $result.$head_tag_start."<title>$name</title></head>\n$body_tag_start\n";
  100:     }
  101:   }
  102:   if ($target eq 'edit') {
  103:     $result.=$head_tag_start."</head>".$body_tag_start.
  104:       '<form name="lonhomework" method="POST" action="'.$ENV{'request.uri'}.'">
  105:        <input type="hidden" name="submitted" value="edit" />
  106:        <input type="hidden" name="problemmode" value="Edit" />
  107:        <input type="submit" name="problemmode" value="View" />
  108:        <input type="submit" name="Undo" value="undo" /> <hr />
  109:       ';
  110:     my $temp=&Apache::edit::insertlist($target,$token);
  111:     $result.=$temp;
  112:     return $result;
  113:   }
  114:   if ($target eq 'modified') {
  115:     $result=$token->[4];
  116:     $result.=&Apache::edit::handle_insert();
  117:     return $result;
  118:   }
  119:   return '';
  120: }
  121: 
  122: sub end_problem {
  123:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  124:   my $result='';
  125:   my $status=$Apache::inputtags::status['-1'];
  126:   if ($target eq 'grade' || $target eq 'web' ) {
  127:     if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
  128:        $status eq 'CAN_ANSWER') {
  129:       # if part is zero, no <part>s existed, so we need to the grading
  130:       &Apache::inputtags::grade;
  131:     } elsif ($Apache::inputtags::part eq '0') {
  132:       # if part is zero, no <part>s existed, so we need show the current 
  133:       # grading status
  134:       $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
  135:     }
  136:     if ($target eq 'web') { 
  137:       if ($status eq 'CAN_ANSWER') {
  138: 	$result.="</form></body>\n"; 
  139:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
  140: 	$result.="</body>\n";
  141:       }
  142:     }
  143:   }
  144:   if ($target eq 'meta') {
  145:     if ($Apache::inputtags::part eq '0') {
  146:       $result=&Apache::response::mandatory_part_meta;
  147:     }
  148:   }
  149:   if ($target eq 'edit') {
  150:     &Apache::lonxml::debug("in end_problem with $target, edit");
  151:     $result='<br /><input type="submit" name="submit" value="Submit Changes" />';
  152:   }
  153:   return $result;
  154: }
  155: 
  156: sub start_block {
  157:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  158: 
  159:   if ($target eq 'web' || $target eq 'grade') {
  160:     my $code = @$parstack[$#$parstack];
  161:     $code =~ s/\"//g;
  162:     $code .=';return $condition;';
  163:     #  print "<br />$code<br />";
  164:     my $result = &Apache::run::run($code,$safeeval);
  165:     &Apache::lonxml::debug("block :$code: returned :$result:");
  166:     if ( ! $result ) { 
  167:       my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
  168:       &Apache::lonxml::debug("skipping ahead :$skip: $$parser[$#$parser]");
  169:     }
  170:   }
  171:   return "";
  172: }
  173: 
  174: sub end_block {
  175:   return '';
  176: }
  177: 
  178: sub start_while {
  179:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  180: 
  181:   my $code = @$parstack[$#$parstack];
  182:   $code =~ s/\"//g;
  183:   $code .=';return $condition;';
  184: 
  185:   push( @Apache::structuretags::whileconds, $code); 
  186:   my $result = &Apache::run::run($code,$safeeval);
  187:   my $bodytext=$$parser[$#$parser]->get_text("/while");
  188:   push( @Apache::structuretags::whilebody, $bodytext);
  189:   if ( $result ) { 
  190:     &Apache::lonxml::newparser($parser,\$bodytext);
  191:   }
  192:   return "";
  193: }
  194: 
  195: sub end_while {
  196:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  197:   my $code = pop @Apache::structuretags::whileconds;
  198:   my $bodytext = pop @Apache::structuretags::whilebody;
  199:   my $result = &Apache::run::run($code,$safeeval);
  200:   if ( $result ) { 
  201:     &Apache::lonxml::newparser($parser,\$bodytext);
  202:   } 
  203:   return "";
  204: }
  205: 
  206: # <randomlist> 
  207: #  <tag1>..</tag1>
  208: #  <tag2>..</tag2>
  209: #  <tag3>..</tag3>
  210: #  ... 
  211: # </randomlist>
  212: sub start_randomlist {
  213:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  214:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
  215:   my $b_parser= HTML::TokeParser->new(\$body);
  216:   my $b_tok;
  217:   my @randomlist;
  218:   my $list_item;
  219: 
  220:   while($b_tok = $b_parser->get_token() ) {
  221:     if($b_tok->[0] eq 'S') { # start tag
  222:     # get content of the tag until matching end tag
  223:     # get all text upto the matching tag
  224:     # and push the content into @randomlist
  225:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
  226:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  227:       push(@randomlist,$list_item);
  228:    #  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4], $list_item</b>";
  229:     }
  230:     if($b_tok->[0] eq 'T') { # text
  231:     # what to do with text in between tags?
  232:       #  print "<b>TEXT $b_tok->[1]</b><br />";
  233:     }
  234:     # if($b_tok->[0] eq 'E') { # end tag, should not happen
  235:       #  print "<b>END-TAG $b_tok->[1]</b><br />";
  236:     # }
  237:   }
  238:   my @idx_arr = (0 .. $#randomlist);
  239:   &Apache::structuretags::shuffle(\@idx_arr);
  240:   my $bodytext = '';
  241:   for(0 .. $#randomlist) {
  242:     $bodytext .= "$randomlist[ $idx_arr[$_] ]";
  243:   }
  244: 
  245:   &Apache::lonxml::newparser($parser,\$bodytext);
  246:   return "";
  247: }
  248: 
  249: sub shuffle {
  250:     my $a=shift;
  251:     my $i;
  252:     for($i=@$a;--$i;) {
  253:       my $j=int rand($i+1);
  254:       next if $i == $j;
  255:       @$a[$i,$j] = @$a[$j,$i];
  256:     }
  257: }
  258: 
  259: sub end_randomlist {
  260: }
  261: 
  262: sub start_part {
  263:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  264:   my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  265:   $Apache::inputtags::part=$id;
  266:   @Apache::inputtags::responselist = ();
  267:   if ($target eq 'meta') {
  268:     return &Apache::response::mandatory_part_meta;
  269:   } elsif ($target eq 'web' || $target eq 'grade') {
  270:     my ($status,$datemsg) = &Apache::lonhomework::check_date("OPEN_DATE",$id);
  271:     push (@Apache::inputtags::status,$status);
  272:     my $expression='$external::datestatus="'.$status.'";';
  273:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
  274:     &Apache::run::run($expression,$safeeval);
  275:     if ( $status eq 'CLOSED' ) {
  276:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
  277:       if ( $target eq "web" ) {
  278: 	return "<br />Part is not open to be viewed. It $datemsg<br />";
  279:       }
  280:     }
  281:   }
  282:   return '';
  283: }
  284: 
  285: sub end_part {
  286:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  287:   &Apache::lonxml::debug("in end_part $target ");
  288:   my $status=$Apache::inputtags::status['-1'];
  289:   pop @Apache::inputtags::status;
  290:   if ( $target eq 'meta' ) { return ''; }
  291:   if ( $target eq 'grade' && $status eq 'CAN_ANSWER') {
  292:     return &Apache::inputtags::grade;
  293:   }
  294:   if ($target eq 'web') {
  295:     return &Apache::inputtags::gradestatus($Apache::inputtags::part);
  296:   }
  297:   return '';
  298: }
  299: 
  300: sub start_preduedate {
  301:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  302:   if ($target eq 'web' || $target eq 'grade') {
  303:     if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  304: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
  305:       &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
  306:     }
  307:   }
  308:   return '';
  309: }
  310: 
  311: sub end_preduedate {
  312:   return '';
  313: }
  314: 
  315: sub start_postanswerdate {
  316:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  317:   if ($target eq 'web' || $target eq 'grade') {
  318:     if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  319:       &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
  320:     }
  321:   }
  322:   return '';
  323: }
  324: 
  325: sub end_postanswerdate {
  326:   return '';
  327: }
  328: 
  329: sub start_notsolved {
  330:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  331:   if ($target eq 'web' || $target eq 'grade') {
  332:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  333:     &Apache::lonxml::debug("not solved has :$gradestatus:");
  334:     if ($gradestatus =~ /^correct/) {
  335:       &Apache::lonxml::debug("skipping");
  336:       &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
  337:     }
  338:   }
  339:   return '';
  340: }
  341: 
  342: sub end_notsolved {
  343:   return '';
  344: }
  345: 
  346: sub start_solved {
  347:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  348:   if ($target eq 'web' || $target eq 'grade') {
  349:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  350:     if ($gradestatus !~ /^correct/) {
  351:       &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
  352:     }
  353:   }
  354:   return '';
  355: }
  356: 
  357: sub end_solved {
  358:   return '';
  359: }
  360: 
  361: sub start_startouttext {
  362:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  363:   my @result=(''.'');
  364:   if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
  365:   return (@result);
  366: }
  367: sub end_startouttext {
  368:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  369:   my $result='';
  370:   my $text='';
  371: 
  372:   if ($target eq 'edit') {
  373:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser[$#$parser]);
  374:     $result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
  375: <td>Delete:".
  376:   &Apache::edit::deletelist($target,$token)
  377:   ."</td>
  378: <td>".
  379:   &Apache::edit::insertlist($target,$token).
  380:     "</td>
  381: </tr><tr><td colspan=\"3\">\n".
  382: 	&Apache::edit::editfield($token->[1],$text,"",50,5);
  383:   }
  384:   if ($target eq 'modified') {
  385:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
  386:     $result='<startouttext />'.&Apache::edit::modifiedfield();
  387:   }
  388:   return $result;
  389: }
  390: sub start_endouttext {
  391:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  392:   my $result='';
  393:   if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
  394:   if ($target eq "modified") { $result='<endouttext />'; }
  395:   return $result;
  396: }
  397: sub end_endouttext {
  398:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  399:   my @result=('','');
  400:   if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
  401:   return (@result);
  402: }
  403: 
  404: 
  405: 1;
  406: __END__

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