Annotation of loncom/homework/structuretags.pm, revision 1.41

1.34      albertel    1: # The LearningOnline Network with CAPA 
                      2: # definition of tags that give a structure to a document
1.33      albertel    3: # 2/19 Guy
1.1       albertel    4: package Apache::structuretags; 
                      5: 
                      6: use strict;
                      7: use Apache::lonnet;
                      8: 
                      9: sub BEGIN {
1.34      albertel   10:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','web','tex','part','preduedate','postanswerdate','solved','notsolved','startouttext','endouttext'));
1.37      albertel   11: #  &Apache::lonxml::register_insert('problem','',('part','postanswerdate','preduedate'))
1.10      albertel   12: }
                     13: 
                     14: sub start_web {
1.40      albertel   15:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.10      albertel   16:   my $bodytext=&Apache::lonxml::get_all_text("/web",$$parser[$#$parser]);
1.19      albertel   17:   if ($target eq 'web') {
                     18:     return $bodytext;
                     19:   } 
                     20:   return '';
1.10      albertel   21: }
                     22: 
                     23: sub end_web {
                     24: }
                     25: 
                     26: sub start_tex {
1.40      albertel   27:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.10      albertel   28:   my $bodytext=&Apache::lonxml::get_all_text("/tex",$$parser[$#$parser]);
1.19      albertel   29:   if ($target eq 'tex') {
                     30:     return $bodytext
                     31:   }
1.10      albertel   32:   return '';
                     33: }
                     34: 
                     35: sub end_tex {
1.9       albertel   36: }
                     37: 
                     38: sub start_problem {
1.40      albertel   39:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.19      albertel   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);
1.22      albertel   48:   my $status;
                     49:   my $datemsg;
1.36      albertel   50: 
1.41    ! albertel   51:   #should get back a <html> or the neccesary stuff to start XML/MathML
1.40      albertel   52:   my $result=&Apache::londefdef::start_html($target,$token,$tagstack,$parstack,$parser,$safeeval);
1.36      albertel   53: 
1.41    ! albertel   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">';
1.34      albertel   58:   if ($target eq 'web' || $target eq 'grade') {
1.22      albertel   59:     ($status,$datemsg) = &Apache::lonhomework::check_date('0');
                     60:     push (@Apache::inputtags::status,$status);
1.24      albertel   61:     my $expression='$external::datestatus="'.$status.'";';
                     62:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
                     63:     &Apache::run::run($expression,$safeeval);
1.22      albertel   64:     if ( $status eq 'CLOSED' ) {
1.21      albertel   65:       my $bodytext=&Apache::lonxml::get_all_text("/problem",$$parser[$#$parser]);
                     66:       if ( $target eq "web" ) {
1.41    ! albertel   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 />";
1.21      albertel   70:       }
1.41    ! albertel   71:     }
1.21      albertel   72:   }
1.19      albertel   73:   if ($target eq 'web') {
1.39      albertel   74:     my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.30      albertel   75:     if ($name eq '') { 
                     76:       $name=&Apache::lonnet::EXT('resource.title');
                     77:       if ($name eq 'con_lost') { $name = ''; }
                     78:     }
                     79:     $Apache::lonhomework::name=$name;
1.22      albertel   80:     if ($status eq 'CAN_ANSWER') {
                     81:       # create a page header and exit
1.41    ! albertel   82:       $result.="$head_tag_start<title>$name</title></head>\n
        !            83:               $body_tag_start\n
1.36      albertel   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;
1.29      albertel   91:     } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' || $status eq 'CLOSED') {
1.41    ! albertel   92:       return $result.$head_tag_start."<title>$name</title></head>\n$body_tag_start\n";
1.22      albertel   93:     }
                     94:   }
1.34      albertel   95:   if ($target eq 'edit') {
1.41    ! albertel   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:       ';
1.39      albertel  103:     my $temp=&Apache::edit::insertlist($target,$token);
1.36      albertel  104:     $result.=$temp;
                    105:     return $result;
1.34      albertel  106:   }
1.37      albertel  107:   if ($target eq 'modified') {
                    108:     $result=$token->[4];
                    109:     $result.=&Apache::edit::handle_insert();
                    110:     return $result;
                    111:   }
1.19      albertel  112:   return '';
1.9       albertel  113: }
                    114: 
                    115: sub end_problem {
1.40      albertel  116:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.16      albertel  117:   my $result='';
1.24      albertel  118:   my $status=$Apache::inputtags::status['-1'];
1.40      albertel  119:   if ($target eq 'grade' || $target eq 'web' ) {
1.28      albertel  120:     if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
                    121:        $status eq 'CAN_ANSWER') {
1.19      albertel  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
1.20      albertel  127:       $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
1.41    ! albertel  128:     }
1.22      albertel  129:     if ($target eq 'web') { 
                    130:       if ($status eq 'CAN_ANSWER') {
                    131: 	$result.="</form></body>\n"; 
1.28      albertel  132:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
1.41    ! albertel  133: 	$result.="</body>\n";
        !           134:       }
1.22      albertel  135:     }
1.41    ! albertel  136:   }
1.34      albertel  137:   if ($target eq 'meta') {
1.18      albertel  138:     if ($Apache::inputtags::part eq '0') {
1.16      albertel  139:       $result=&Apache::response::mandatory_part_meta;
                    140:     }
                    141:   }
1.34      albertel  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:   }
1.16      albertel  146:   return $result;
1.1       albertel  147: }
                    148: 
                    149: sub start_block {
1.40      albertel  150:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.1       albertel  151: 
1.38      albertel  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:     }
1.1       albertel  163:   }
                    164:   return "";
                    165: }
                    166: 
                    167: sub end_block {
1.38      albertel  168:   return '';
1.4       tsai      169: }
                    170: 
                    171: sub start_while {
1.40      albertel  172:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.4       tsai      173: 
                    174:   my $code = @$parstack[$#$parstack];
                    175:   $code =~ s/\"//g;
                    176:   $code .=';return $condition;';
                    177: 
1.5       tsai      178:   push( @Apache::structuretags::whileconds, $code); 
1.4       tsai      179:   my $result = &Apache::run::run($code,$safeeval);
                    180:   my $bodytext=$$parser[$#$parser]->get_text("/while");
1.5       tsai      181:   push( @Apache::structuretags::whilebody, $bodytext);
                    182:   if ( $result ) { 
1.8       albertel  183:     &Apache::lonxml::newparser($parser,\$bodytext);
1.4       tsai      184:   }
                    185:   return "";
                    186: }
                    187: 
                    188: sub end_while {
1.40      albertel  189:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.5       tsai      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 ) { 
1.8       albertel  194:     &Apache::lonxml::newparser($parser,\$bodytext);
1.5       tsai      195:   } 
                    196:   return "";
1.1       albertel  197: }
1.6       tsai      198: 
                    199: # <randomlist> 
                    200: #  <tag1>..</tag1>
                    201: #  <tag2>..</tag2>
                    202: #  <tag3>..</tag3>
                    203: #  ... 
                    204: # </randomlist>
                    205: sub start_randomlist {
1.40      albertel  206:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.9       albertel  207:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
1.6       tsai      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
1.9       albertel  218:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
1.7       tsai      219:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
1.6       tsai      220:       push(@randomlist,$list_item);
1.32      albertel  221:    #  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4], $list_item</b>";
1.6       tsai      222:     }
                    223:     if($b_tok->[0] eq 'T') { # text
                    224:     # what to do with text in between tags?
1.32      albertel  225:       #  print "<b>TEXT $b_tok->[1]</b><br />";
1.6       tsai      226:     }
                    227:     # if($b_tok->[0] eq 'E') { # end tag, should not happen
1.32      albertel  228:       #  print "<b>END-TAG $b_tok->[1]</b><br />";
1.6       tsai      229:     # }
                    230:   }
1.7       tsai      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:   }
1.8       albertel  237: 
                    238:   &Apache::lonxml::newparser($parser,\$bodytext);
1.6       tsai      239:   return "";
1.7       tsai      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:     }
1.6       tsai      250: }
                    251: 
                    252: sub end_randomlist {
                    253: }
                    254: 
1.11      albertel  255: sub start_part {
1.40      albertel  256:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.39      albertel  257:   my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
1.14      albertel  258:   $Apache::inputtags::part=$id;
1.18      albertel  259:   @Apache::inputtags::responselist = ();
1.15      www       260:   if ($target eq 'meta') {
1.16      albertel  261:     return &Apache::response::mandatory_part_meta;
1.37      albertel  262:   } elsif ($target eq 'web' || $target eq 'grade') {
1.23      albertel  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' ) {
1.21      albertel  269:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
                    270:       if ( $target eq "web" ) {
1.32      albertel  271: 	return "<br />Part is not open to be viewed. It $datemsg<br />";
1.21      albertel  272:       }
                    273:     }
1.15      www       274:   }
1.19      albertel  275:   return '';
1.11      albertel  276: }
                    277: 
                    278: sub end_part {
1.40      albertel  279:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.18      albertel  280:   &Apache::lonxml::debug("in end_part $target ");
1.28      albertel  281:   my $status=$Apache::inputtags::status['-1'];
1.23      albertel  282:   pop @Apache::inputtags::status;
1.19      albertel  283:   if ( $target eq 'meta' ) { return ''; }
1.37      albertel  284:   if ( $target eq 'grade' && $status eq 'CAN_ANSWER') {
                    285:     return &Apache::inputtags::grade;
1.28      albertel  286:   }
1.37      albertel  287:   if ($target eq 'web') {
                    288:     return &Apache::inputtags::gradestatus($Apache::inputtags::part);
                    289:   }
                    290:   return '';
1.11      albertel  291: }
1.1       albertel  292: 
1.25      albertel  293: sub start_preduedate {
1.40      albertel  294:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  295:   if ($target eq 'web' || $target eq 'grade') {
1.29      albertel  296:     if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
                    297: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
1.26      albertel  298:       &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
1.24      albertel  299:     }
                    300:   }
                    301:   return '';
                    302: }
                    303: 
1.25      albertel  304: sub end_preduedate {
1.24      albertel  305:   return '';
                    306: }
                    307: 
1.25      albertel  308: sub start_postanswerdate {
1.40      albertel  309:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  310:   if ($target eq 'web' || $target eq 'grade') {
1.24      albertel  311:     if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
1.26      albertel  312:       &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
1.24      albertel  313:     }
                    314:   }
                    315:   return '';
                    316: }
                    317: 
1.25      albertel  318: sub end_postanswerdate {
1.24      albertel  319:   return '';
                    320: }
                    321: 
1.25      albertel  322: sub start_notsolved {
1.40      albertel  323:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  324:   if ($target eq 'web' || $target eq 'grade') {
1.24      albertel  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");
1.26      albertel  329:       &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
1.24      albertel  330:     }
                    331:   }
                    332:   return '';
                    333: }
                    334: 
1.25      albertel  335: sub end_notsolved {
1.24      albertel  336:   return '';
                    337: }
                    338: 
                    339: sub start_solved {
1.40      albertel  340:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  341:   if ($target eq 'web' || $target eq 'grade') {
1.24      albertel  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: }
1.34      albertel  353: 
                    354: sub start_startouttext {
1.40      albertel  355:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.35      albertel  356:   my @result=(''.'');
                    357:   if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
                    358:   return (@result);
1.34      albertel  359: }
                    360: sub end_startouttext {
1.40      albertel  361:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  362:   my $result='';
1.35      albertel  363:   my $text='';
                    364: 
1.34      albertel  365:   if ($target eq 'edit') {
1.35      albertel  366:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser[$#$parser]);
                    367:     $result=
1.37      albertel  368:       &Apache::edit::tag_start($target,$token).
                    369: 	&Apache::edit::editfield($token->[1],$text,"Text Block",50,5);
1.35      albertel  370:   }
                    371:   if ($target eq 'modified') {
                    372:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
                    373:     $result='<startouttext />'.&Apache::edit::modifiedfield();
1.34      albertel  374:   }
                    375:   return $result;
                    376: }
                    377: sub start_endouttext {
1.40      albertel  378:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  379:   my $result='';
1.37      albertel  380:   if ($target eq "edit" ) { $result=&Apache::edit::tag_end($target,$token); }
1.35      albertel  381:   if ($target eq "modified") { $result='<endouttext />'; }
1.34      albertel  382:   return $result;
                    383: }
                    384: sub end_endouttext {
1.40      albertel  385:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.35      albertel  386:   my @result=('','');
                    387:   if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
                    388:   return (@result);
1.34      albertel  389: }
                    390: 
                    391: 
1.1       albertel  392: 1;
                    393: __END__

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