$msg
$end_page ENDREDIR return; @@ -81,29 +255,205 @@ ENDREDIR sub error_page { my ($r,$error,$dest)=@_; - &Apache::loncommon::content_type($r,'text/html'); - &Apache::loncommon::no_cache($r); - $r->send_http_header; - return OK if $r->header_only; - $r->print(&Apache::loncommon::start_page('Problems during Course Initialization'). - ''. - ''.&mt('The following problems occurred:'). - $error. - '
$lt{'tfp'}
+
+$error
+
' + .&mt('This LON-CAPA server is version [_1]',$r->dir_config('lonVersion')) + .'
'); + } + $r->print(&Apache::loncommon::end_page()); + return OK; +} + +sub roles_from_env { + my ($roleshash,$update) = @_; + my $count = 0; + if (ref($roleshash) eq 'HASH') { + foreach my $envkey (keys(%env)) { + if ($envkey =~ m{^user\.role\.(\w+)[./]}) { + next if ($1 eq 'gr'); + $roleshash->{$envkey} = $env{$envkey}; + my ($start,$end) = split(/\./,$env{$envkey}); + unless ($end && $end<$update) { + $count ++; + } + } + } + } + return $count; +} + +sub gather_roles { + my ($update,$refresh,$now,$reinit,$nochoose,$roles_in_env,$roletext,$sortrole,$roleclass,$futureroles, + $timezones,$loncaparev) = @_; + my ($countactive,$countfuture,$inrole,$possiblerole) = (0,0,0,''); + my $advanced = $env{'user.adv'}; + my $tryagain = $env{'form.tryagain'}; + my @ids = &Apache::lonnet::current_machine_ids(); + if (ref($roles_in_env) eq 'HASH') { + my %adhocdesc; + foreach my $envkey (sort(keys(%{$roles_in_env}))) { + my $button = 1; + my $switchserver=''; + my $switchwarning; + my ($role_text,$role_text_end,$sortkey,$role,$where,$trolecode,$tstart, + $tend,$tremark,$tstatus,$tpstart,$tpend); + &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where, + \$trolecode,\$tstatus,\$tstart,\$tend); next if (!defined($role) || $role eq '' || $role =~ /^gr/); $tremark=''; $tpstart=' '; $tpend=' '; - $tfont='#000000'; - if ($tstart) { - $tpstart=&Apache::lonlocal::locallocaltime($tstart); - } - if ($tend) { - $tpend=&Apache::lonlocal::locallocaltime($tend); - } if ($env{'request.role'} eq $trolecode) { - $tstatus='selected'; + $tstatus='selected'; } my $tbg; - if (($tstatus eq 'is') - || ($tstatus eq 'selected') - || ($tstatus eq 'will') - || ($tstatus eq 'future') - || ($env{'form.showall'})) { + if (($tstatus eq 'is') + || ($tstatus eq 'selected') + || ($tstatus eq 'future') + || ($env{'form.display'} eq 'showall')) { + my $timezone = &role_timezone($where,$timezones); + if ($tstart) { + $tpstart=&Apache::lonlocal::locallocaltime($tstart,$timezone); + } + if ($tend) { + $tpend=&Apache::lonlocal::locallocaltime($tend,$timezone); + } if ($tstatus eq 'is') { - $tbg='#77FF77'; - $tfont='#003300'; - $possiblerole=$trolecode; - $countactive++; + $tbg='LC_roles_is'; + $possiblerole=$trolecode; + $countactive++; } elsif ($tstatus eq 'future') { - $tbg='#FFFF77'; + $tbg='LC_roles_future'; $button=0; - $futureroles{$trolecode} = $tstart.':'.$tend; + $futureroles->{$trolecode} = $tstart.':'.$tend; $countfuture ++; - } elsif ($tstatus eq 'will') { - $tbg='#FFAA77'; - $tremark.=&mt('Active at next login. '); - $roles_nextlogin{$trolecode} = $tstart.':'.$tend; - $countwill ++; } elsif ($tstatus eq 'expired') { - $tbg='#FF7777'; - $tfont='#330000'; + $tbg='LC_roles_expired'; $button=0; } elsif ($tstatus eq 'will_not') { - $tbg='#AAFF77'; - $tremark.=&mt('Expired after logout. '); + $tbg='LC_roles_will_not'; + $tremark.=&mt('Expired after logout.').' '; } elsif ($tstatus eq 'selected') { - $tbg='#11CC55'; - $tfont='#002200'; - $inrole=1; - $countactive++; - $tremark.=&mt('Currently selected. '); + $tbg='LC_roles_selected'; + $inrole=1; + $countactive++; + $tremark.=&mt('Currently selected.').' '; } my $trole; if ($role =~ /^cr\//) { my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role); - if ($tremark) { $tremark.=''.&mt('[_1]Visit the [_2]Course Catalog[_3] to view all [_4] LON-CAPA courses.','','','',$domdesc).'
'.&mt('If a course is [_1]not[_2] in your list of current courses below, you may be able to enroll if self-enrollment is permitted.','','').'
'
- .&mt('This is LON-CAPA [_1]',$r->dir_config('lonVersion'))
- .'
'
- .''.&mt('Logout').' '
- .''
- .&mt('Course Catalog')
- .'
'); } - $r->print(' | '.&mt('User Role').' | '.&mt('Extent') - .' | '.&mt('Start').' | '.&mt('End') - .' | '.&mt('User Role').' | ' + .''.&mt('Extent').' | ' + .''.&mt('Start').' | ' + .''.&mt('End').' | ' + .&Apache::loncommon::end_data_table_header_row() + ); $doheaders=-1; my @roletypes = &roletypes(); foreach my $type (@roletypes) { @@ -831,12 +1845,12 @@ sub roletable_headers { } sub roletypes { - my @types = ('Domain','Construction Space','Course','Unavailable','System'); + my @types = ('Domain','Authoring Space','Course','Community','Unavailable','System'); return @types; } sub print_rolerows { - my ($r,$doheaders,$roleclass,$sortrole,$dcroles,$roletext) = @_; + my ($r,$doheaders,$roleclass,$sortrole,$dcroles,$roletext,$update,$then) = @_; if ((ref($roleclass) eq 'HASH') && (ref($sortrole) eq 'HASH')) { my @types = &roletypes(); foreach my $type (@types) { @@ -844,22 +1858,36 @@ sub print_rolerows { foreach my $which (sort {uc($a) cmp uc($b)} (keys(%{$sortrole}))) { if ($roleclass->{$sortrole->{$which}} =~ /^\Q$type\E/) { if (ref($roletext) eq 'HASH') { - $output.=$roletext->{$sortrole->{$which}}; - if ($sortrole->{$which} =~ m-dc\./($match_domain)/-) { + if (ref($roletext->{$sortrole->{$which}}) eq 'ARRAY') { + $output.= &Apache::loncommon::start_data_table_row(). + $roletext->{$sortrole->{$which}}->[0]. + &Apache::loncommon::end_data_table_row(); + if ($roletext->{$sortrole->{$which}}->[1] ne '') { + $output .= &Apache::loncommon::continue_data_table_row(). + $roletext->{$sortrole->{$which}}->[1]. + &Apache::loncommon::end_data_table_row(); + } + } + if ($sortrole->{$which} =~ m{^user\.role\.dc\./($match_domain)/}) { if (ref($dcroles) eq 'HASH') { if ($dcroles->{$1}) { $output .= &adhoc_roles_row($1,''); } } + } elsif ($sortrole->{$which} =~ m{^user\.role\.(dh|da)\./($match_domain)/}) { + $output .= &adhoc_customroles_row($1,$2,'',$update,$then); } } } } if ($output) { if ($doheaders > 0) { - $r->print("|||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
". - &mt($type)." | ' + .&mt($type) + .' | ' + .&Apache::loncommon::end_data_table_empty_row() + ); } $r->print($output); } @@ -868,23 +1896,100 @@ sub print_rolerows { } sub findcourse_advice { - my ($r) = @_; + my ($r,$cattype,$elapsed) = @_; my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description'); my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&'); if (&Apache::lonnet::auto_run(undef,$env{'user.domain'})) { - $r->print(&mt('If you were expecting to see an active role listed for a particular course in the [_1] domain, it may be missing for one of the following reasons:',$domdesc).' + $r->print('||||||||||||
'.&mt('Switch Server').' | '; + $roletext.='' + .'' + .&mt('Switch Server') + .' | '; } else { - $roletext.=(''); + $roletext.=(' | '); + } + if ($switchwarning) { + if ($tremark eq '') { + $tremark = $switchwarning; + } else { + $tremark .= ' | '. + ' | '; } elsif ($tryagain) { $roletext.= - ''. + ' | '; } elsif ($advanced) { $roletext.= - ''. + ' | '; + } elsif ($reinit) { + $roletext.= + ''. + ' | '; } else { - $roletext.=''; + $roletext.= + ' | '. + ' | '; } } - if ($trolecode !~ m/^(dc|ca|au|aa)\./) { + if (($trolecode !~ m/^(dc|ca|au|aa)\./) && (!$skipcal)) { $tremark.=&Apache::lonannounce::showday(time,1, &Apache::lonannounce::readcalendar($tdom.'_'.$trest)); } - $roletext.=''.$trole. - ' | '.$twhere. - ' | '.$tpstart. - ' | '.$tpend. - ' |
'.$tremark. - ' | |||||||||||||
'.$trole.' | ' + .''.$twhere.' | ' + .''.$tpstart.' | ' + .''.$tpend.' | '; + unless ($poss_adhoc) { + $roletext_end = ''. + $tremark.' '. + ' | '; } - return $roletext; + return ($roletext,$roletext_end); } sub check_author_homeserver { @@ -1036,59 +2141,70 @@ sub check_author_homeserver { } } -sub check_privs { - my ($cdom,$cnum,$then,$now,$checkrole) = @_; - my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum; - if ($env{$cckey}) { - my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend,$tfont); - &role_status($cckey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend); - unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) { - &set_privileges($cdom,$cnum,$checkrole); - } - } else { - &set_privileges($cdom,$cnum,$checkrole); - } -} - -sub check_fordc { - my ($dcroles,$then) = @_; +sub check_for_adhoc { + my ($dcroles,$helpdeskroles,$update,$then) = @_; my $numdc = 0; - if ($env{'user.adv'}) { - foreach my $envkey (sort keys %env) { - if ($envkey=~/^user\.role\.dc\.\/($match_domain)\/$/) { - my $dcdom = $1; - my $livedc = 1; + my $numhelpdesk = 0; + my $numadhoc = 0; + my $num_custom_adhoc = 0; + if (($env{'user.adv'}) || ($env{'user.rar'})) { + foreach my $envkey (sort(keys(%env))) { + if ($envkey=~/^user\.role\.(dc|dh|da)\.\/($match_domain)\/$/) { + my $role = $1; + my $roledom = $2; + my $liverole = 1; my ($tstart,$tend)=split(/\./,$env{$envkey}); - if ($tstart && $tstart>$then) { $livedc = 0; } - if ($tend && $tend <$then) { $livedc = 0; } - if ($livedc) { - $$dcroles{$dcdom} = $envkey; - $numdc++; + my $limit = $update; + if ($env{'request.role'} eq "$role./$roledom/") { + $limit = $then; + } + if ($tstart && $tstart>$limit) { $liverole = 0; } + if ($tend && $tend <$limit) { $liverole = 0; } + if ($liverole) { + if ($role eq 'dc') { + $dcroles->{$roledom} = $envkey; + $numdc++; + } else { + $helpdeskroles->{$roledom} = $envkey; + my %domdefaults = &Apache::lonnet::get_domain_defaults($roledom); + if (ref($domdefaults{'adhocroles'}) eq 'HASH') { + if (keys(%{$domdefaults{'adhocroles'}})) { + $numadhoc ++; + } + } + $numhelpdesk++; + } } } } } - return $numdc; + return ($numdc,$numhelpdesk,$numadhoc); } sub adhoc_course_role { - my ($then) = @_; - my ($cdom,$cnum); + my ($refresh,$update,$then) = @_; + my ($cdom,$cnum,$crstype); $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'}; $cnum = $env{'course.'.$env{'request.course.id'}.'.num'}; - if (&check_forcc($cdom,$cnum,$then)) { + $crstype = &Apache::loncommon::course_type(); + if (&check_forcc($cdom,$cnum,$refresh,$update,$then,$crstype)) { my $setprivs; if (!defined($env{'user.role.'.$env{'form.switchrole'}})) { $setprivs = 1; } else { my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.switchrole'}}); - if (($start && ($start>$then || $start == -1)) || - ($end && $end<$then)) { + if (($start && ($start>$refresh || $start == -1)) || + ($end && $end<$update)) { + $setprivs = 1; + } + } + unless ($setprivs) { + if (!exists($env{'user.priv.'.$env{'form.switchrole'}.'./'})) { $setprivs = 1; } - } + } if ($setprivs) { - if ($env{'form.switchrole'} =~ m-^(in|ta|ep|ad|st|cr)([\w/]*)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) { + if ($env{'form.switchrole'} =~ m-^(in|ta|ep|ad|st|cr)(.*?)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) { my $role = $1; my $custom_role = $2; my $usec = $3; @@ -1099,16 +2215,34 @@ sub adhoc_course_role { return; } } - my (%userroles,%newrole,%newgroups); + my (%userroles,%newrole,%newgroups,%group_privs); + my %cgroups = + &Apache::lonnet::get_active_groups($env{'user.domain'}, + $env{'user.name'},$cdom,$cnum); + my $ccrole; + if ($crstype eq 'Community') { + $ccrole = 'co'; + } else { + $ccrole = 'cc'; + } + foreach my $group (keys(%cgroups)) { + $group_privs{$group} = + $env{'user.priv.'.$ccrole.'./'.$cdom.'/'.$cnum.'./'.$cdom.'/'.$cnum.'/'.$group}; + } + $newgroups{'/'.$cdom.'/'.$cnum} = \%group_privs; my $area = '/'.$cdom.'/'.$cnum; my $spec = $role.'.'.$area; if ($usec ne '') { $spec .= '/'.$usec; $area .= '/'.$usec; } - &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,$area); - &Apache::lonnet::set_userprivs(\%userroles,\%newrole,%newgroups); - my $adhocstart = $then-1; + if ($role =~ /^cr/) { + &Apache::lonnet::custom_roleprivs(\%newrole,$role,$cdom,$cnum,$spec,$area); + } else { + &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,$area); + } + &Apache::lonnet::set_userprivs(\%userroles,\%newrole,\%newgroups); + my $adhocstart = $refresh-1; $userroles{'user.role.'.$spec} = $adhocstart.'.'; &Apache::lonnet::appenv(\%userroles,[$role,'cm']); } @@ -1118,42 +2252,52 @@ sub adhoc_course_role { } sub check_forcc { - my ($cdom,$cnum,$then) = @_; - my $is_cc; - if ($cdom ne '' && $cnum ne '') { - if (&Apache::lonnet::is_course($cdom,$cnum)) { - my $envkey = 'user.role.cc./'.$cdom.'/'.$cnum; - if (defined($env{$envkey})) { - $is_cc = 1; - my ($tstart,$tend)=split(/\./,$env{$envkey}); - if ($tstart && $tstart>$then) { $is_cc = 0; } - if ($tend && $tend <$then) { $is_cc = 0; } + my ($cdom,$cnum,$refresh,$update,$then,$crstype) = @_; + my ($is_cc,$ccrole); + if ($crstype eq 'Community') { + $ccrole = 'co'; + } else { + $ccrole = 'cc'; + } + if (&Apache::lonnet::is_course($cdom,$cnum)) { + my $envkey = 'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum; + if (defined($env{$envkey})) { + $is_cc = 1; + my ($tstart,$tend)=split(/\./,$env{$envkey}); + my $limit = $update; + if ($env{'request.role'} eq $ccrole.'./'.$cdom.'/'.$cnum) { + $limit = $then; } + if ($tstart && $tstart>$refresh) { $is_cc = 0; } + if ($tend && $tend <$limit) { $is_cc = 0; } } } return $is_cc; } sub courselink { - my ($dcdom,$rowtype) = @_; + my ($roledom,$rowtype,$role) = @_; my $courseform=&Apache::loncommon::selectcourse_link - ('rolechoice','dccourse'.$rowtype.'_'.$dcdom, - 'dcdomain'.$rowtype.'_'.$dcdom,'coursedesc'.$rowtype.'_'. - $dcdom,$dcdom,undef); - my $hiddenitems = ''. - ''. - ''. - ''; + ('rolechoice','course'.$rowtype.'_'.$roledom.'_'.$role, + 'domain'.$rowtype.'_'.$roledom.'_'.$role, + 'coursedesc'.$rowtype.'_'.$roledom.'_'.$role, + $roledom.':'.$role,undef,'Course/Community'); + my $hiddenitems = ''. + ''. + ''. + ''; return $courseform.$hiddenitems; } sub coursepick_jscript { - my %lt = &Apache::lonlocal::texthash( - plsu => "Please use the 'Select Course' link to open a separate pick course window where you may select the course you wish to enter.", - youc => 'You can only use this screen to select courses in the current domain.', + my %js_lt = &Apache::lonlocal::texthash( + plsu => "Please use the 'Select Course/Community' link to open a separate pick course window where you may select the course or community you wish to enter.", + youc => 'You can only use this screen to select courses and communities in the current domain.', ); + &js_escape(\%js_lt); my $verify_script = <<"END"; END return $verify_script; @@ -1196,86 +2341,119 @@ sub coauthorlink { sub display_cc_role { my $rolekey = shift; - my $roletext; + my ($roletext,$roletext_end); my $advanced = $env{'user.adv'}; my $tryagain = $env{'form.tryagain'}; unless ($rolekey =~/^error\:/) { - if ($rolekey =~ m-^user\.role.cc\./($match_domain)/($match_courseid)$-) { - my $tcourseid = $1.'_'.$2; - my $trolecode = 'cc./'.$1.'/'.$2; + if ($rolekey =~ m{^user\.role\.(cc|co)\./($match_domain)/($match_courseid)$}) { + my $ccrole = $1; + my $tdom = $2; + my $trest = $3; + my $tcourseid = $tdom.'_'.$trest; + my $trolecode = $ccrole.'./'.$tdom.'/'.$trest; my $twhere; my $ttype; - my $tbg='#77FF77'; - my $tfont='#003300'; + my $skipcal; + my $tbg='LC_roles_is'; my %newhash=&Apache::lonnet::coursedescription($tcourseid); if (%newhash) { $twhere=$newhash{'description'}. - ' '. - &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$2,$1,$tfont). - ''; + ' '. + &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom). + ''; $ttype = $newhash{'type'}; } else { $twhere=&mt('Currently not available'); $env{'course.'.$tcourseid.'.description'}=$twhere; + $skipcal = 1; + } + my $trole = &Apache::lonnet::plaintext($ccrole,$ttype,$tcourseid); + $twhere.="|||||||||
| |||||||||||||
'
+ .&mt('[_1]Ad hoc[_2] course/community roles in domain [_3]',
+ '','',$dhdom)
+ .' -- '.&courselink($dhdom,$rowtype,$role);
+ }
+ }
+ return;
+}
+
sub recent_filename {
my $area=shift;
return 'nohist_recent_'.&escape($area);
}
-sub set_privileges {
-# role can be cc or ca
- my ($dcdom,$pickedcourse,$role) = @_;
- my $area = '/'.$dcdom.'/'.$pickedcourse;
- my $spec = $role.'.'.$area;
- my %userroles = &Apache::lonnet::set_arearole($role,$area,'','',
- $env{'user.domain'},
- $env{'user.name'});
- my %ccrole = ();
- &Apache::lonnet::standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
- my ($author,$adv)= &Apache::lonnet::set_userprivs(\%userroles,\%ccrole);
- &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
-
- &Apache::lonnet::log($env{'user.domain'},
- $env{'user.name'},
- $env{'user.home'},
- "Role ".$role);
- &Apache::lonnet::appenv(
- {'request.role' => $spec,
- 'request.role.domain' => $dcdom,
- 'request.course.sec' => ''});
- my $tadv=0;
- if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
- &Apache::lonnet::appenv({'request.role.adv' => $tadv});
-}
-
sub courseloadpage {
my ($courseid) = @_;
my $startpage;
@@ -1293,6 +2471,952 @@ sub courseloadpage {
return $startpage;
}
+sub update_session_roles {
+ my $then=$env{'user.login.time'};
+ my $refresh=$env{'user.refresh.time'};
+ if (!$refresh) {
+ $refresh = $then;
+ }
+ my $update = $env{'user.update.time'};
+ if (!$update) {
+ $update = $then;
+ }
+ my $now = time;
+ my %roleshash =
+ &Apache::lonnet::get_my_roles('','','userroles',
+ ['active','future','previous'],
+ undef,undef,1);
+ my ($msg,@newsec,$oldsec,$currrole_expired,@changed_roles,
+ %changed_groups,%dbroles,%deletedroles,%allroles,%allgroups,
+ %userroles,%checkedgroup,%crprivs,$hasgroups,%rolechange,
+ %groupchange,%newrole,%newgroup,%customprivchg,%groups_roles,
+ @rolecodes);
+ my @possroles = ('cr','st','ta','ad','ep','in','co','cc');
+ my %courseroles;
+ foreach my $item (keys(%roleshash)) {
+ my ($uname,$udom,$role,$remainder) = split(/:/,$item,4);
+ my ($tstart,$tend) = split(/:/,$roleshash{$item});
+ my ($section,$group,@group_privs);
+ if ($role =~ m{^gr/(\w*)$}) {
+ $role = 'gr';
+ my $priv = $1;
+ next if ($tstart eq '-1');
+ if (&curr_role_status($tstart,$tend,$refresh,$now) eq 'active') {
+ if ($priv ne '') {
+ push(@group_privs,$priv);
+ }
+ }
+ if ($remainder =~ /:/) {
+ (my $additional_privs,$group) =
+ ($remainder =~ /^([\w:]+):([^:]+)$/);
+ if ($additional_privs ne '') {
+ if (&curr_role_status($tstart,$tend,$refresh,$now) eq 'active') {
+ push(@group_privs,split(/:/,$additional_privs));
+ @group_privs = sort(@group_privs);
+ }
+ }
+ } else {
+ $group = $remainder;
+ }
+ } else {
+ $section = $remainder;
+ }
+ my $where = "/$udom/$uname";
+ if ($section ne '') {
+ $where .= "/$section";
+ } elsif ($group ne '') {
+ $where .= "/$group";
+ }
+ my $rolekey = "$role.$where";
+ my $envkey = "user.role.$rolekey";
+ $dbroles{$envkey} = 1;
+ if (($env{'request.role'} eq $rolekey) && ($role ne 'st')) {
+ if (&curr_role_status($tstart,$tend,$refresh,$now) ne 'active') {
+ $currrole_expired = 1;
+ }
+ }
+ if ($env{$envkey} eq '') {
+ my $status_in_db =
+ &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') {
+ if ($section eq '') {
+ push(@newsec,'none');
+ } else {
+ push(@newsec,$section);
+ }
+ }
+ } else {
+ unless (grep(/^\Q$role\E$/,@changed_roles)) {
+ push(@changed_roles,$role);
+ }
+ if ($status_in_db ne 'previous') {
+ if ($role eq 'gr') {
+ $newgroup{$rolekey} = $status_in_db;
+ if ($status_in_db eq 'active') {
+ unless (ref($courseroles{$udom}) eq 'HASH') {
+ %{$courseroles{$udom}} =
+ &Apache::lonnet::get_my_roles('','','userroles',
+ ['active'],\@possroles,
+ [$udom],1);
+ }
+ &Apache::lonnet::get_groups_roles($udom,$uname,
+ $courseroles{$udom},
+ \@rolecodes,\%groups_roles);
+ }
+ } else {
+ $newrole{$rolekey} = $status_in_db;
+ }
+ }
+ }
+ } else {
+ my ($currstart,$currend) = split(/\./,$env{$envkey});
+ if ($role eq 'gr') {
+ if (&curr_role_status($currstart,$currend,$refresh,$update) ne 'previous') {
+ $hasgroups = 1;
+ }
+ }
+ if (($currstart ne $tstart) || ($currend ne $tend)) {
+ my $status_in_env =
+ &curr_role_status($currstart,$currend,$refresh,$update);
+ my $status_in_db =
+ &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') {
+ if ($env{'request.role'} eq $rolekey) {
+ my $switchsection;
+ unless (ref($courseroles{$udom}) eq 'HASH') {
+ %{$courseroles{$udom}} =
+ &Apache::lonnet::get_my_roles('','','userroles',
+ ['active'],
+ \@possroles,[$udom],1);
+ }
+ foreach my $crsrole (keys(%{$courseroles{$udom}})) {
+ if ($crsrole =~ /^\Q$uname\E:\Q$udom\E:st/) {
+ $switchsection = 1;
+ last;
+ }
+ }
+ if ($switchsection) {
+ if ($section eq '') {
+ $oldsec = 'none';
+ } else {
+ $oldsec = $section;
+ }
+ &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
+ } else {
+ $currrole_expired = 1;
+ next;
+ }
+ }
+ }
+ unless ($rolekey eq $env{'request.role'}) {
+ if ($role eq 'gr') {
+ &Apache::lonnet::delete_env_groupprivs($where,\%courseroles,\@possroles);
+ } else {
+ &Apache::lonnet::delenv("user.priv.$rolekey",undef,[$role]);
+ &Apache::lonnet::delenv("user.priv.cm.$where",undef,['cm']);
+ }
+ &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
+ }
+ } elsif ($status_in_db eq 'active') {
+ if (($role eq 'st') &&
+ ($env{'request.role'} =~ m{^\Q$role\E\.\Q/$udom/$uname\E})) {
+ if ($section eq '') {
+ push(@newsec,'none');
+ } else {
+ push(@newsec,$section);
+ }
+ } elsif ($role eq 'gr') {
+ unless (ref($courseroles{$udom}) eq 'HASH') {
+ %{$courseroles{$udom}} =
+ &Apache::lonnet::get_my_roles('','','userroles',
+ ['active'],
+ \@possroles,[$udom],1);
+ }
+ &Apache::lonnet::get_groups_roles($udom,$uname,
+ $courseroles{$udom},
+ \@rolecodes,\%groups_roles);
+ }
+ &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
+ }
+ unless (grep(/^\Q$role\E$/,@changed_roles)) {
+ push(@changed_roles,$role);
+ }
+ if ($role eq 'gr') {
+ $groupchange{"/$udom/$uname"}{$group} = $status_in_db;
+ } else {
+ $rolechange{$rolekey} = $status_in_db;
+ }
+ }
+ } else {
+ if ($role eq 'gr') {
+ unless ($checkedgroup{$where}) {
+ my $status_in_db =
+ &curr_role_status($tstart,$tend,$refresh,$now);
+ if ($tstart eq '-1') {
+ $status_in_db = 'deleted';
+ }
+ unless (ref($courseroles{$udom}) eq 'HASH') {
+ %{$courseroles{$udom}} =
+ &Apache::lonnet::get_my_roles('','','userroles',
+ ['active'],
+ \@possroles,[$udom],1);
+ }
+ if (ref($courseroles{$udom}) eq 'HASH') {
+ foreach my $item (keys(%{$courseroles{$udom}})) {
+ next unless ($item =~ /^\Q$uname\E/);
+ my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
+ my $area = '/'.$cdom.'/'.$cnum;
+ if ($crssec ne '') {
+ $area .= '/'.$crssec;
+ }
+ my $crsrolekey = $crsrole.'.'.$area;
+ my $currprivs = $env{'user.priv.'.$crsrole.'.'.$area.'.'.$where};
+ $currprivs =~ s/^://;
+ $currprivs =~ s/\&F$//;
+ my @curr_grp_privs = split(/\&F:/,$currprivs);
+ @curr_grp_privs = sort(@curr_grp_privs);
+ my @diffs;
+ if (@group_privs > 0 || @curr_grp_privs > 0) {
+ @diffs = &Apache::loncommon::compare_arrays(\@group_privs,\@curr_grp_privs);
+ }
+ if (@diffs == 0) {
+ last;
+ } else {
+ unless(grep(/^\Qgr\E$/,@rolecodes)) {
+ push(@rolecodes,'gr');
+ }
+ &gather_roleprivs(\%allroles,\%allgroups,
+ \%userroles,$where,$role,
+ $tstart,$tend,$status_in_db);
+ if ($status_in_db eq 'active') {
+ &Apache::lonnet::get_groups_roles($udom,$uname,
+ $courseroles{$udom},
+ \@rolecodes,\%groups_roles);
+ }
+ $changed_groups{$udom.'_'.$uname}{$group} = $status_in_db;
+ last;
+ }
+ }
+ }
+ $checkedgroup{$where} = 1;
+ }
+ } elsif ($role =~ /^cr/) {
+ my $status_in_db =
+ &curr_role_status($tstart,$tend,$refresh,$now);
+ my ($rdummy,$rest) = split(/\//,$role,2);
+ my %currpriv;
+ unless (exists($crprivs{$rest})) {
+ my ($rdomain,$rauthor,$rrole)=split(/\//,$rest);
+ my $homsvr=&Apache::lonnet::homeserver($rauthor,$rdomain);
+ if (&Apache::lonnet::hostname($homsvr) ne '') {
+ my ($rdummy,$roledef)=
+ &Apache::lonnet::get('roles',["rolesdef_$rrole"],
+ $rdomain,$rauthor);
+ if (($rdummy ne 'con_lost') && ($roledef ne '')) {
+ my $i = 0;
+ my @scopes = ('sys','dom','crs');
+ my @privs = split(/\_/,$roledef);
+ foreach my $priv (@privs) {
+ my ($blank,@prv) = split(/:/,$priv);
+ @prv = map { $_ .= (/\&\w+$/ ? '':'&F') } @prv;
+ if (@prv) {
+ $priv = ':'.join(':',sort(@prv));
+ }
+ $crprivs{$rest}{$scopes[$i]} = $priv;
+ $i++;
+ }
+ }
+ }
+ }
+ my $status_in_env =
+ &curr_role_status($currstart,$currend,$refresh,$update);
+ if ($status_in_env eq 'active') {
+ $currpriv{sys} = $env{"user.priv.$rolekey./"};
+ $currpriv{dom} = $env{"user.priv.$rolekey./$udom/"};
+ $currpriv{crs} = $env{"user.priv.$rolekey.$where"};
+ if (keys(%crprivs)) {
+ if (($crprivs{$rest}{sys} ne $currpriv{sys}) ||
+ ($crprivs{$rest}{dom} ne $currpriv{dom})
+ ||
+ ($crprivs{$rest}{crs} ne $currpriv{crs})) {
+ &gather_roleprivs(\%allroles,\%allgroups,
+ \%userroles,$where,$role,
+ $tstart,$tend,$status_in_db);
+ unless (grep(/^\Q$role\E$/,@changed_roles)) {
+ push(@changed_roles,$role);
+ }
+ $customprivchg{$rolekey} = $status_in_env;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ foreach my $envkey (keys(%env)) {
+ next unless ($envkey =~ /^user\.role\./);
+ next if ($dbroles{$envkey});
+ next if ($envkey eq 'user.role.'.$env{'request.role'});
+ my ($currstart,$currend) = split(/\./,$env{$envkey});
+ my $status_in_env =
+ &curr_role_status($currstart,$currend,$refresh,$update);
+ my ($rolekey) = ($envkey =~ /^user\.role\.(.+)$/);
+ my ($role,$rest)=split(m{\./},$rolekey,2);
+ $rest = '/'.$rest;
+ if (&Apache::lonnet::delenv($envkey,undef,[$role])) {
+ if ($status_in_env eq 'active') {
+ if ($role eq 'gr') {
+ &Apache::lonnet::delete_env_groupprivs($rest,\%courseroles,
+ \@possroles);
+ } else {
+ &Apache::lonnet::delenv("user.priv.$rolekey",undef,[$role]);
+ &Apache::lonnet::delenv("user.priv.cm.$rest",undef,['cm']);
+ }
+ unless (grep(/^\Q$role\E$/,@changed_roles)) {
+ push(@changed_roles,$role);
+ }
+ $deletedroles{$rolekey} = 1;
+ }
+ }
+ }
+ if (($oldsec) && (@newsec > 0)) {
+ if (@newsec > 1) {
+ $msg = ' '.&mt('The section has changed for your current role. Log-out and log-in again to select a role for the new section.').' '; + } else { + my $newrole = $env{'request.role'}; + if ($newsec[0] eq 'none') { + $newrole =~ s{(/[^/])$}{}; + } elsif ($oldsec eq 'none') { + $newrole .= '/'.$newsec[0]; + } else { + $newrole =~ s{([^/]+)$}{$newsec[0]}; + } + my $coursedesc = $env{'course.'.$env{'request.course.id'}.'.description'}; + my ($curr_role) = ($env{'request.role'} =~ m{^(\w+)\./$match_domain/$match_courseid}); + my %temp=('logout_'.$env{'request.course.id'} => time); + &Apache::lonnet::put('email_status',\%temp); + &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'}); + &Apache::lonnet::appenv({"request.course.id" => '', + "request.course.fn" => '', + "request.course.uri" => '', + "request.course.sec" => '', + "request.role" => 'cm', + "request.role.adv" => $env{'user.adv'}, + "request.role.domain" => $env{'user.domain'}}); + my $rolename = &Apache::loncommon::plainname($curr_role); + $msg = ''; + my $button = ''; + if ($newsec[0] eq 'none') { + $msg .= &mt('[_1] to continue with your new section-less role.',$button); + } else { + $msg .= &mt('[_1] to continue with your new role in section ([_2]).',$button,$newsec[0]); + } + $msg .= ''; + } + } elsif ($currrole_expired) { + $msg .= ' ';
+ if (&Apache::loncommon::show_course()) {
+ $msg .= &mt('Your role in the current course has expired.');
+ } else {
+ $msg .= &mt('Your current role has expired.');
+ }
+ $msg .= '
'; + } + return $msg; + } + } + my $changemsg; + if (@changed_roles > 0) { + if (keys(%newgroup) > 0) { + my $groupmsg; + my (%curr_groups,%groupdescs,$currcrs); + foreach my $item (sort(keys(%newgroup))) { + if (&is_active_course($item,$refresh,$update,\%roleshash)) { + if ($item =~ m{^gr\./($match_domain/$match_courseid)/(\w+)$}) { + my ($cdom,$cnum) = split(/\//,$1); + my $group = $2; + if ($currcrs ne $cdom.'_'.$cnum) { + if ($currcrs) { + $groupmsg .= '
'.$tremark; + } + } + return $output; +} + +sub curr_role_status { + my ($start,$end,$refresh,$update) = @_; + if (($start) && ($start<0)) { return 'deleted' }; + my $status = 'active'; + if (($end) && ($end<=$update)) { + $status = 'previous'; + } + if (($start) && ($refresh<$start)) { + $status = 'future'; + } + return $status; +} + +sub gather_roleprivs { + my ($allroles,$allgroups,$userroles,$area,$role,$tstart,$tend,$status) = @_; + return unless ((ref($allroles) eq 'HASH') && (ref($allgroups) eq 'HASH') && (ref($userroles) eq 'HASH')); + if (($area ne '') && ($role ne '')) { + &Apache::lonnet::userrolelog($role,$env{'user.name'},$env{'user.domain'}, + $area,$tstart,$tend); + my $spec=$role.'.'.$area; + $userroles->{'user.role.'.$spec} = $tstart.'.'.$tend; + my ($tdummy,$tdomain,$trest)=split(/\//,$area); + if ($status eq 'active') { + if ($role =~ /^cr\//) { + &Apache::lonnet::custom_roleprivs($allroles,$role,$tdomain,$trest,$spec,$area); + } elsif ($role eq 'gr') { + my %rolehash = &Apache::lonnet::get('roles',[$area.'_'.$role], + $env{'user.domain'}, + $env{'user.name'}); + my ($trole) = split(/_/,$rolehash{$area.'_'.$role},2); + (undef,my $group_privs) = split(/\//,$trole); + $group_privs = &unescape($group_privs); + &Apache::lonnet::group_roleprivs($allgroups,$area,$group_privs,$tend,$tstart); + } else { + &Apache::lonnet::standard_roleprivs($allroles,$role,$tdomain,$spec,$trest,$area); + } + } + } + return; +} + +sub is_active_course { + my ($rolekey,$refresh,$update,$roleshashref) = @_; + return unless(ref($roleshashref) eq 'HASH'); + my ($role,$cdom,$cnum) = split(/\//,$rolekey); + my $is_active; + foreach my $key (keys(%{$roleshashref})) { + if ($key =~ /^\Q$cnum\E:\Q$cdom\E:/) { + my ($tstart,$tend) = split(/:/,$roleshashref->{$key}); + my $status = &curr_role_status($tstart,$tend,$refresh,$update); + if ($status eq 'active') { + $is_active = 1; + last; + } + } + } + return $is_active; +} + +sub get_roles_functions { + my ($rolescount,$cattype) = @_; + my @links; + push(@links,["javascript:rolesView('doupdate');",'start-here-22x22',&mt('Check for changes')]); + if ($env{'environment.canrequest.author'}) { + unless (&Apache::loncoursequeueadmin::is_active_author()) { + push(@links,["javascript:rolesView('requestauthor');",'list-add-22x22',&mt('Request author role')]); + } + } + if (($rolescount > 3) || ($env{'environment.recentroles'})) { + push(@links,['/adm/preferences?action=changerolespref&returnurl=/adm/roles','role_hotlist-22x22',&mt('Hotlist')]); + } + if (&Apache::lonmenu::check_for_rcrs()) { + push(@links,['/adm/requestcourse','rcrs-22x22',&mt('Request course')]); + } + if ($env{'form.state'} eq 'queued') { + push(@links,["javascript:rolesView('noqueued');",'selfenrl-queue-22x22',&mt('Hide queued')]); + } else { + push(@links,["javascript:rolesView('queued');",'selfenrl-queue-22x22',&mt('Show queued')]); + } + if ($env{'user.adv'}) { + if ($env{'form.display'} eq 'showall') { + push(@links,["javascript:rolesView('noshowall');",'edit-redo-22x22',&mt('Exclude expired')]); + } else { + push(@links,["javascript:rolesView('showall');",'edit-undo-22x22',&mt('Include expired')]); + } + } + unless ($cattype eq 'none') { + push(@links,['/adm/coursecatalog','ccat-22x22',&mt('Course catalog')]); + } + my $funcs; + if ($env{'browser.mobile'}) { + my @functions; + foreach my $link (@links) { + push(@functions,[$link->[0],$link->[2]]); + } + my $title = 'Display options'; + if ($env{'user.adv'}) { + $title = 'Roles options'; + } + $funcs = &Apache::lonmenu::create_submenu('','',$title,\@functions,1,'LC_breadcrumbs_hoverable'); + $funcs = ' '; + } else { + $funcs = &Apache::lonhtmlcommon::start_funclist(); + foreach my $link (@links) { + $funcs .= &Apache::lonhtmlcommon::add_item_funclist( + ''. + ''. + $link->[2].''); + } + $funcs .= &Apache::lonhtmlcommon::end_funclist(); + $funcs = &Apache::loncommon::head_subbox($funcs); + } + return $funcs; +} + +sub get_queued { + my ($output,%reqcrs); + my ($types,$typenames) = &Apache::loncommon::course_types(); + my %statusinfo = &Apache::lonnet::dump('courserequests',$env{'user.domain'}, + $env{'user.name'},'^status:'); + foreach my $key (keys(%statusinfo)) { + next unless (($statusinfo{$key} eq 'approval') || ($statusinfo{$key} eq 'pending')); + (undef,my($cdom,$cnum)) = split(/:/,$key); + my $requestkey = $cdom.'_'.$cnum; + if ($requestkey =~ /^($match_domain)_($match_courseid)$/) { + my %history = &Apache::lonnet::restore($requestkey,'courserequests', + $env{'user.domain'},$env{'user.name'}); + next if ((exists($history{'status'})) && ($history{'status'} eq 'created')); + my $reqtime = $history{'reqtime'}; + my $lastupdate = $history{'timestamp'}; + my $showtype = $history{'crstype'}; + if (defined($typenames->{$history{'crstype'}})) { + $showtype = $typenames->{$history{'crstype'}}; + } + my $description; + if (ref($history{'details'}) eq 'HASH') { + $description = $history{details}{'cdescr'}; + } + @{$reqcrs{$reqtime}} = ($description,$showtype); + } + } + my @sortedtimes = sort {$a <=> $b} (keys(%reqcrs)); + if (@sortedtimes > 0) { + $output .= ' '.&mt('Course/Community requests').' | '.&mt('Date requested').' | '. + ''.&mt('Course title').' | '. + ''.&mt('Course type').' | '; + &Apache::loncommon::end_data_table_header_row(); + foreach my $reqtime (@sortedtimes) { + next unless (ref($reqcrs{$reqtime}) eq 'ARRAY'); + $output .= &Apache::loncommon::start_data_table_row(). + ''.&Apache::lonlocal::locallocaltime($reqtime).' | '. + ''.join(' | ',@{$reqcrs{$reqtime}}).' | '. + &Apache::loncommon::end_data_table_row(); + } + $output .= &Apache::loncommon::end_data_table(). + '