--- loncom/auth/lonroles.pm 2012/07/21 21:09:03 1.269 +++ loncom/auth/lonroles.pm 2012/08/14 17:35:04 1.271 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # User Roles Screen # -# $Id: lonroles.pm,v 1.269 2012/07/21 21:09:03 raeburn Exp $ +# $Id: lonroles.pm,v 1.271 2012/08/14 17:35:04 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -225,6 +225,96 @@ sub handler { $updateresult .= &update_session_roles(); &Apache::lonnet::appenv({'user.update.time' => $now}); $update = $now; + &reqauthor_check(); + } + +# -------------------------------------------------- Check for author requests + + my $reqauthor; + if ($env{'form.requestauthor'}) { + if ($env{'environment.canrequest.author'}) { + unless (&is_active_author()) { + my $queued = &reqauthor_check(); + my $skipreq; + if ($queued =~ /^approval:\d+$/) { + my ($status,$timestamp) = split(/:/,$env{'environment.requestauthorqueued'}); + if ($status eq 'approval') { + $reqauthor = ''. + &mt('A request for authoring space submitted on [_1] is awaiting approval', + &Apache::lonlocal::locallocaltime($timestamp)). + ''; + } + $skipreq = 1; + } elsif ($queued =~ /^approved:\d+$/) { + my %roleshash = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},'userroles', + ['active'],['au'],[$env{'user.domain'}]); + if (keys(%roleshash) > 0) { + $skipreq = 1; + } + } + unless ($skipreq) { + my (@inststatuses,%domconfig); + %domconfig = + &Apache::lonnet::get_dom('configuration', + ['requestauthor'],$env{'user.domain'}); + my $val = &Apache::loncoursequeueadmin::get_processtype('requestauthor',$env{'user.name'}, + $env{'user.domain'},$env{'user.adv'}, + $env{'user.domain'},undef, + \@inststatuses,\%domconfig); + if ($val eq 'automatic') { + my $start = $now-1; + if (&Apache::lonnet::assignrole($env{'user.domain'},$env{'user.name'},'/'.$env{'user.domain'}.'/', + 'au',undef,$start,undef,undef,'requestauthor') eq 'ok') { + $reqauthor = ''. + &mt('Access to authoring space has been activated').''; + &update_session_roles(); + &Apache::lonnet::appenv({'user.update.time' => $now}); + $update = $now; + } else { + $reqauthor = ''. + &mt('An error occurred while activating your access to authoring space'); + } + } elsif ($val eq 'approval') { + my $domconfiguser = &Apache::lonnet::get_domainconfiguser($env{'user.domain'}); + if (&Apache::lonnet::put('requestauthorqueue',{ $env{'user.name'}.'_'.$val => $now }, + $env{'user.domain'},$domconfiguser) eq 'ok') { + my %userrequest = ( + author => { + timestamp => $now, + status => $val, + }, + author_status => $val, + ); + my $req_notifylist; + if (ref($domconfig{'requestauthor'}) eq 'HASH') { + if (ref($domconfig{'requestauthor'}{'notify'}) eq 'HASH') { + my $req_notifylist = $domconfig{'requestauthor'}{'notify'}{'approval'}; + if ($req_notifylist) { + my $fullname = &Apache::loncommon::plainname($env{'user.name'}, + $env{'user.domain'}); + my $sender = $env{'user.name'}.':'.$env{'user.domain'}; + my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description'); + &Apache::loncoursequeueadmin::send_selfserve_notification($req_notifylist, + "$fullname ($env{'user.name'}:$env{'user.domain'})",undef,$domdesc, + $now,'authorreq',$sender); + } + } + } + my $userresult = + &Apache::lonnet::put('requestauthor',\%userrequest,$env{'user.domain'},$env{'user.name'}); + $reqauthor = ''. + &mt('Your request for authoring space has been submitted for approval.'). + ''; + &Apache::lonnet::appenv({'environment.requestauthorqueued' => $val.':'.$now}); + } else { + $reqauthor = ''. + &mt('An error occurred saving your request for authoring space.'). + ''; + } + } + } + } + } } my $envkey; @@ -684,6 +774,12 @@ function setToUpdate(thisform) { thisform.submit(); } +function setToRequestAuthor(thisform) { + thisform.requestauthor.value='1'; + thisform.selectrole.value=''; + thisform.submit(); +} + // ]]> ENDHEADER @@ -750,7 +846,12 @@ ENDHEADER &Apache::loncommon::end_page()); return OK; } else { - $r->print($updateresult); + if ($updateresult || $reqauthor) { + $r->print(''. + $updateresult. + $reqauthor. + ''); + } if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) { $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'}; } @@ -778,6 +879,42 @@ ENDHEADER ''; } + my ($requestauthor,$requestcrs); + unless ($reqauthor) { + if ($env{'environment.canrequest.author'}) { + unless (&is_active_author()) { + my ($status,$timestamp) = split(/:/,$env{'environment.requestauthorqueued'}); + if ($status eq 'approval') { + $requestauthor = &mt('A request for authoring space submitted on [_1] is awaiting approval',&Apache::lonlocal::locallocaltime($timestamp)); + } elsif (($status eq 'approved') && ($do_update)) { + my %roleshash = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},'userroles', + ['active'],['au'],[$env{'user.domain'}]); + if (keys(%roleshash)) { + $requestauthor = &mt('Your request for an author role has been approved.').''; + if ($show_course) { + $requestauthor .= &mt('Use the "Check for new courses" button to update your list of roles.'); + } else { + $requestauthor .= &mt('Use the "Check for new roles" button to update your list of roles.'); + } + $requestauthor = ''.$requestauthor.''; + } + } + unless ($requestauthor) { + $requestauthor = + ''. + ''; + } + } + } + } + my $do_update; + unless (($env{'form.source'} eq 'login') || ($env{'form.doupdate'})) { + $do_update = ''. + ''; + } if ($env{'user.adv'}) { my $showall = ' '. ''; - if ($do_update) { + if ($do_update || $requestauthor) { $r->print(''. ''. &mt('Display').''. - $showall.''. - ''. - &mt('Changes?').''. - $do_update.''); + $showall.''); + if ($do_update) { + $r->print(''. + ''.&mt('Changes?').''. + $do_update.''); + } + if ($requestauthor) { + $r->print(''. + ''.&mt('Add author role').''. + $requestauthor.''); + } + $r->print(''); } else { $r->print($showall); } } else { - $r->print(''.$do_update.''); + if ($do_update && $requestauthor) { + $r->print(''. + ''.&mt('Changes?').''. + $do_update.''. + ''. + ''.&mt('Add author role').''. + $requestauthor.''); + } elsif ($do_update) { + $r->print(''.$do_update.''); + } elsif ($requestauthor) { + $r->print(''.$requestauthor.''); + } if ($countactive > 0) { $r->print(&Apache::loncoursequeueadmin::queued_selfenrollment()); my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description'); @@ -976,6 +1132,33 @@ ENDHEADER return OK; } +sub is_active_author { + if ($env{'user.role.au./'.$env{'user.domain'}.'/'} =~ /^(\d*)\.(\d*)$/) { + if ((!$1 || $1 < time) && + (!$2 || $2 > time)) { + return 1; + } + } + return; +} + +sub reqauthor_check { + my $queued = $env{'environment.requestauthorqueued'}; + my %reqauthor = &Apache::lonnet::get('requestauthor',['author_status','author'], + $env{'user.domain'},$env{'user.name'}); + my $reqstatus = $reqauthor{'author_status'}; + if (($reqstatus eq '' && $queued ne '') || + ($env{'environment.requestauthorqueued'} !~ /^\Q$reqstatus\E/)) { + if (ref($reqauthor{'author'}) eq 'HASH') { + $queued = $reqstatus.':'.$reqauthor{'author'}{'timestamp'}; + } else { + undef($queued); + } + &Apache::lonnet::appenv({'environment.requestauthorqueued' => $queued}); + } + return $queued; +} + sub gather_roles { my ($update,$refresh,$now,$reinit,$nochoose,$roletext,$sortrole,$roleclass,$futureroles,$timezones,$loncaparev) = @_; my ($countactive,$countfuture,$inrole,$possiblerole) = (0,0,0,''); @@ -1889,7 +2072,7 @@ sub update_session_roles { } if ($env{$envkey} eq '') { my $status_in_db = - &curr_role_status($tstart,$tend,$refresh,$now); + &curr_role_status($tstart,$tend,$now,$now); &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db); if (($role eq 'st') && ($env{'request.role'} =~ m{^\Q$role\E\.\Q/$udom/$uname\E})) { if ($status_in_db eq 'active') { @@ -1933,7 +2116,7 @@ sub update_session_roles { my $status_in_env = &curr_role_status($currstart,$currend,$refresh,$update); my $status_in_db = - &curr_role_status($tstart,$tend,$refresh,$now); + &curr_role_status($tstart,$tend,$now,$now); if ($status_in_env ne $status_in_db) { if ($status_in_env eq 'active') { if ($role eq 'st') { @@ -2204,7 +2387,7 @@ sub update_session_roles { } $msg .= ''; } else { - $msg = ' '.$rolesmsg.''; + $msg = ' '.$rolesmsg.''; } return $msg; }
'.$do_update.'
'.$requestauthor.'