File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.46: download - view: text, annotated - select for diffs
Thu Jul 12 12:27:15 2001 UTC (22 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- need something to prvent returning the argument string

    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:   return '';
  261: }
  262: 
  263: sub start_part {
  264:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  265:   my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  266:   $Apache::inputtags::part=$id;
  267:   @Apache::inputtags::responselist = ();
  268:   if ($target eq 'meta') {
  269:     return &Apache::response::mandatory_part_meta;
  270:   } elsif ($target eq 'web' || $target eq 'grade') {
  271:     my ($status,$datemsg) = &Apache::lonhomework::check_date("OPEN_DATE",$id);
  272:     push (@Apache::inputtags::status,$status);
  273:     my $expression='$external::datestatus="'.$status.'";';
  274:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
  275:     &Apache::run::run($expression,$safeeval);
  276:     if ( $status eq 'CLOSED' ) {
  277:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
  278:       if ( $target eq "web" ) {
  279: 	return "<br />Part is not open to be viewed. It $datemsg<br />";
  280:       }
  281:     }
  282:   }
  283:   return '';
  284: }
  285: 
  286: sub end_part {
  287:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  288:   &Apache::lonxml::debug("in end_part $target ");
  289:   my $status=$Apache::inputtags::status['-1'];
  290:   pop @Apache::inputtags::status;
  291:   if ( $target eq 'meta' ) { return ''; }
  292:   if ( $target eq 'grade' && $status eq 'CAN_ANSWER') {
  293:     return &Apache::inputtags::grade;
  294:   }
  295:   if ($target eq 'web') {
  296:     return &Apache::inputtags::gradestatus($Apache::inputtags::part);
  297:   }
  298:   return '';
  299: }
  300: 
  301: sub start_preduedate {
  302:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  303:   if ($target eq 'web' || $target eq 'grade') {
  304:     if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  305: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
  306:       &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
  307:     }
  308:   }
  309:   return '';
  310: }
  311: 
  312: sub end_preduedate {
  313:   return '';
  314: }
  315: 
  316: sub start_postanswerdate {
  317:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  318:   if ($target eq 'web' || $target eq 'grade') {
  319:     if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  320:       &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
  321:     }
  322:   }
  323:   return '';
  324: }
  325: 
  326: sub end_postanswerdate {
  327:   return '';
  328: }
  329: 
  330: sub start_notsolved {
  331:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  332:   if ($target eq 'web' || $target eq 'grade') {
  333:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  334:     &Apache::lonxml::debug("not solved has :$gradestatus:");
  335:     if ($gradestatus =~ /^correct/) {
  336:       &Apache::lonxml::debug("skipping");
  337:       &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
  338:     }
  339:   }
  340:   return '';
  341: }
  342: 
  343: sub end_notsolved {
  344:   return '';
  345: }
  346: 
  347: sub start_solved {
  348:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  349:   if ($target eq 'web' || $target eq 'grade') {
  350:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  351:     if ($gradestatus !~ /^correct/) {
  352:       &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
  353:     }
  354:   }
  355:   return '';
  356: }
  357: 
  358: sub end_solved {
  359:   return '';
  360: }
  361: 
  362: sub start_startouttext {
  363:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  364:   my @result=(''.'');
  365:   if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
  366:   return (@result);
  367: }
  368: sub end_startouttext {
  369:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  370:   my $result='';
  371:   my $text='';
  372: 
  373:   if ($target eq 'edit') {
  374:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser[$#$parser]);
  375:     $result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
  376: <td>Delete:".
  377:   &Apache::edit::deletelist($target,$token)
  378:   ."</td>
  379: <td>".
  380:   &Apache::edit::insertlist($target,$token).
  381:     "</td>
  382: </tr><tr><td colspan=\"3\">\n".
  383: 	&Apache::edit::editfield($token->[1],$text,"",50,4);
  384:   }
  385:   if ($target eq 'modified') {
  386:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
  387:     $result='<startouttext />'.&Apache::edit::modifiedfield();
  388:   }
  389:   return $result;
  390: }
  391: sub start_endouttext {
  392:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  393:   my $result='';
  394:   if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
  395:   if ($target eq "modified") { $result='<endouttext />'; }
  396:   return $result;
  397: }
  398: sub end_endouttext {
  399:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  400:   my @result=('','');
  401:   if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
  402:   return (@result);
  403: }
  404: sub delete_startouttext {
  405:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  406: #  my $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
  407:   my $text=$$parser['-1']->get_text("/endouttext");
  408:   my $token=$$parser['-1']->get_token();
  409:   &Apache::lonxml::debug("Deleting :$text: and :$token->[0]:$token->[1]:$token->[2]: for startouttext");
  410:   &Apache::lonxml::end_tag($tagstack,$parstack,$token);
  411:   # Deleting 2 parallel tag pairs, but we need the numbers later to look like 
  412:   # they did the last time round
  413:   &Apache::lonxml::increasedepth($token);
  414:   &Apache::lonxml::decreasedepth($token);
  415:   return 1;
  416: }
  417: 
  418: 1;
  419: __END__

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