--- rat/lonpage.pm 2015/07/17 19:00:47 1.110 +++ rat/lonpage.pm 2015/07/17 19:11:03 1.111 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Page Handler # -# $Id: lonpage.pm,v 1.110 2015/07/17 19:00:47 raeburn Exp $ +# $Id: lonpage.pm,v 1.111 2015/07/17 19:11:03 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -40,6 +40,7 @@ use Apache::lonhtmlcommon; use Apache::lonxml(); use Apache::lonlocal; use Apache::lonmenu; +use Apache::lonhomework; use HTML::TokeParser; use GDBM_File; use Apache::lonsequence; @@ -187,6 +188,18 @@ sub handler { my $firstres=$hash{'map_start_'.$requrl}; my $lastres=$hash{'map_finish_'.$requrl}; if (($firstres) && ($lastres)) { +# ------------------------------------------------------------- Countdown Timer + my $now = time; + my ($pagefirstaccess,%hastimeleft,%countdowndisp); + my ($pagesymb,$courseid,$domain,$name)=&Apache::lonnet::whichuser(); + if (($pagesymb && ($courseid ne '') && ($domain ne '') && ($name ne '')) { + my %times=&Apache::lonnet::get('firstaccesstimes', + [$courseid."\0".$pagesymb], + $domain,$name); + if ($times{$courseid."\0".$pagesymb} =~ /^\d+$/) { + $pagefirstaccess = $times{$courseid."\0".$pagesymb}; + } + } # ----------------------------------------------------------------- Render page @rows=(); @@ -417,6 +430,59 @@ ENDEXT s/\<((?:input|select)[^\>]+\Qjavascript:setSubmittedPart\E)\(\s*[\'\"]([^\'\"]+)[\'\"]*\s*\)/\<$1('$2','$prefix')/gsi; $output=~ s/\<(input[^\>]+\Qonfocus=\"javascript:disableAutoComplete\E)\(\'([^\']+)\'\)(;\")/\<$1('$idprefix$2')$3/gsi; + unless ($hastimer) { + if ($src =~ /$LONCAPA::assess_re/) { + %Apache::lonhomework::history = + &Apache::lonnet::restore($symb,$courseid,$domain,$name); + my $type = 'problem'; + if ($src =~ /\.task$/) { + $type = 'Task'; + } + my ($status,$accessmsg,$slot_name,$slot) = + &Apache::lonhomework::check_slot_access('0',$type,$symb); + undef(%Apache::lonhomework::history); + my $probstatus = &Apache::lonnet::EXT("resource.0.problemstatus",$symb); + if (($status eq 'CAN_ANSWER') || (($status eq 'CANNOT_ANSWER') && + (($probstatus eq 'no') || ($probstatus eq 'no_feedback_ever')))) { + my ($slothastime,$timerhastime); + if ($slot_name ne '') { + if (ref($slot) eq 'HASH') { + if (($slot->{'starttime'} < $now) && + ($slot->{'endtime'} > $now)) { + $slothastime = $now - $slot->{'endtime'}; + } + } + } + my $duedate = &Apache::lonnet::EXT("resource.0.duedate",$symb); + my @interval=&Apache::lonnet::EXT("resource.0.interval",$symb); + if (@interval > 1) { + my $first_access=&Apache::lonnet::get_first_access($interval[1],$symb); + if ($first_access > 0) { + my $timeremains = $first_access+$interval[0] - $now; + if ($timeremains > 0) { + $timerhastime = $timeremains; + } + } + } + if (($duedate && $duedate > $now) || + (!$duedate && $timerhastime > 0) || + ($slot_name ne '' && $slothastime)) { + if ((@interval > 1 && $timerhastime) || + ($type eq 'Task' && $slothastime)) { + $countdowndisp{$symb} = 'inline'; + if ((@interval > 1) && ($timerhastime)) { + $hastimeleft{$symb} = $timerhastime; + } else { + $hastimeleft{$symb} = $slothastime; + } + } else { + $hastimeleft{$symb} = $duedate - $now; + $countdowndisp{$symb} = 'none'; + } + } + } + } + } } $thisdir=~s/\/[^\/]*$//; foreach (@rlinks) { @@ -483,6 +549,33 @@ ENDEXT '// ]]>'. "\n\n"; } + if (keys(%hastimeleft)) { + my (%uniquetimes,%uniquedisplays); + foreach my $item (values(%hastimeleft)) { + if (exists($uniquetimes{$item})) { + $uniquetimes{$item} ++; + } else { + $uniquetimes{$item} = 1; + } + } + if (keys(%uniquetimes) == 1) { + my (%uniquedisplays,%uniquedones,$currdisp); + if (keys(%countdowndisp)) { + foreach my $item (values(%countdowndisp)) { + if (exists($uniquedisplays{$item})) { + $uniquedisplays{$item} ++; + } else { + $uniquedisplays{$item} = 1; + } + } + my @countdowndisplay = keys(%uniquedisplays); + if (scalar(@countdowndisplay) == 1) { + $currdisp = $countdowndisplay[0]; + } + } + &add_countdown_timer($currdisp); + } + } # ------------------------------------------------------------------ Start body $r->print(&Apache::loncommon::start_page(undef,$allscript, {'force_register' => 1, @@ -753,6 +846,35 @@ sub get_buttons { return $metainfo; } +sub add_countdown_timer { + my ($currdisp) = @_; + my ($collapse,$expand,$alttxt,$title); + if ($currdisp eq 'inline') { + $collapse = '► '; + } else { + $expand = '◄ '; + } + unless ($env{'environment.icons'} eq 'iconsonly') { + $alttxt = &mt('Timer'); + $title = $alttxt.' '; + } + my $desc = &mt('Countdown to due date/time'); + my $output = < + +$collapse + + + +$expand +$alttxt$title +END + &Apache::lonhtmlcommon::clear_breadcrumb_tools(); + &Apache::lonhtmlcommon::add_breadcrumb_tool('tools',$output); + return; +} + + 1; __END__