File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.41: download - view: text, annotated - select for diffs
Tue Jun 5 16:32:18 2001 UTC (22 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- supporting new dynamic menu

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

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