File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.43: download - view: text, annotated - select for diffs
Tue Jun 12 22:42:55 2001 UTC (22 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added color to edit mode <table>
- added start_table function to coordinate the color assignment and other attributes for <table>.

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

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