File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.47: download - view: text, annotated - select for diffs
Fri Jul 13 14:02:35 2001 UTC (22 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- <part> can now find dates
- inputags will use curdepth for the id if there isn't one yet

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

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