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

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

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