File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.36: download - view: text, annotated - select for diffs
Fri May 4 21:21:45 2001 UTC (23 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- <problem> is a bit better about it's html
- asks how to present a problem in construct space

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

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