Diff for /loncom/auth/lonroles.pm between versions 1.335 and 1.345

version 1.335, 2017/10/27 17:43:47 version 1.345, 2021/04/19 15:05:55
Line 145  use GDBM_File; Line 145  use GDBM_File;
 use LONCAPA qw(:DEFAULT :match);  use LONCAPA qw(:DEFAULT :match);
 use HTML::Entities;  use HTML::Entities;
   
   my $registered_cleanup;
   my $rosterupdates;
   
 sub redirect_user {  sub redirect_user {
     my ($r,$title,$url,$msg) = @_;      my ($r,$title,$url,$msg) = @_;
Line 153  sub redirect_user { Line 155  sub redirect_user {
     &Apache::loncommon::no_cache($r);      &Apache::loncommon::no_cache($r);
     $r->send_http_header;      $r->send_http_header;
   
     # Breadcrumbs      my $start_page;
     my $brcrum = [{'href' => $url,      if ($env{'request.lti.login'}) {
                    'text' => 'Switching Role'},];          $start_page = &Apache::loncommon::start_page(undef,undef,
     my $start_page = &Apache::loncommon::start_page('Switching Role',undef,                                                       {'redirect' => [0,$url],}).$msg;
                                                     {'redirect' => [1,$url],      } else {
                                                      'bread_crumbs' => $brcrum,});          # Breadcrumbs
     my $end_page   = &Apache::loncommon::end_page();          my $brcrum = [{'href' => $url,
                          'text' => 'Switching Role'},];
           $start_page = &Apache::loncommon::start_page('Switching Role',undef,
                                                        {'redirect' => [1,$url],
                                                         'bread_crumbs' => $brcrum,}).
                         "\n<p>$msg</p>";
       }
       my $end_page = &Apache::loncommon::end_page();
   
 # Note to style police:   # Note to style police: 
 # This must only replace the spaces, nothing else, or it bombs elsewhere.  # This must only replace the spaces, nothing else, or it bombs elsewhere.
     $url=~s/ /\%20/g;      $url=~s/ /\%20/g;
     $r->print(<<ENDREDIR);      $r->print(<<ENDREDIR);
 $start_page  $start_page
 <p>$msg</p>  
 $end_page  $end_page
 ENDREDIR  ENDREDIR
     return;      return;
Line 222  sub handler { Line 230  sub handler {
         $update = $then;          $update = $then;
     }      }
   
       $registered_cleanup=0;
       @{$rosterupdates}=();
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});      &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
   
 # -------------------------------------------------- Check if setting hot list   # -------------------------------------------------- Check if setting hot list 
Line 262  sub handler { Line 272  sub handler {
   
 # ================================================================== Roles Init  # ================================================================== Roles Init
     if ($env{'form.selectrole'}) {      if ($env{'form.selectrole'}) {
           if (($env{'request.lti.login'}) && ($env{'request.lti.target'} eq '')) {
               if ($env{'form.ltitarget'} eq 'iframe') {
                   &Apache::lonnet::appenv({'request.lti.target' => 'iframe'});
                   delete($env{'form.ltitarget'});
               }
           }
   
         my $locknum=&Apache::lonnet::get_locks();          my $locknum=&Apache::lonnet::get_locks();
         if ($locknum) { return 409; }          if ($locknum) { return 409; }
Line 489  sub handler { Line 505  sub handler {
  my $end_page=&Apache::loncommon::end_page();   my $end_page=&Apache::loncommon::end_page();
  my $buttontext=&mt('Enter Course');   my $buttontext=&mt('Enter Course');
  my $message=&mt('Successfully registered key');   my $message=&mt('Successfully registered key');
                                    my $ip = &Apache::lonnet::get_requestor_ip();
  my $assignresult=   my $assignresult=
      &Apache::lonnet::assign_access_key(       &Apache::lonnet::assign_access_key(
      $env{'form.newkey'},       $env{'form.newkey'},
Line 497  sub handler { Line 514  sub handler {
                                                      $env{'user.domain'},                                                       $env{'user.domain'},
      $env{'user.name'},       $env{'user.name'},
                                                      &mt('Assigned from [_1] at [_2] for [_3]'                                                       &mt('Assigned from [_1] at [_2] for [_3]'
                                                         ,$ENV{'REMOTE_ADDR'}                                                          ,$ip
                                                         ,&Apache::lonlocal::locallocaltime()                                                          ,&Apache::lonlocal::locallocaltime()
                                                         ,$trolecode)                                                          ,$trolecode)
                                                      );                                                       );
Line 636  ENDENTERKEY Line 653  ENDENTERKEY
                             if (($ferr) && ($tadv)) {                              if (($ferr) && ($tadv)) {
  &error_page($r,$ferr,$dest);   &error_page($r,$ferr,$dest);
     } else {      } else {
                                   if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
                                       if (($env{'form.orgurl'} ne '') && ($env{'form.symb'} ne '')) {
                                           unless (&Apache::lonnet::symbverify($env{'form.symb'},$env{'form.orgurl'}) {
                                               $dest=$env{'form.orgurl'};
                                           }
                                       } 
                                   }
                                 if ($dest =~ m{^/adm/coursedocs\?folderpath}) {                                  if ($dest =~ m{^/adm/coursedocs\?folderpath}) {
                                     if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {                                       if ($env{'request.course.id'} eq $cdom.'_'.$cnum) { 
                                         my $chome = &Apache::lonnet::homeserver($cnum,$cdom);                                          my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
Line 643  ENDENTERKEY Line 667  ENDENTERKEY
                                                                                        $cdom.'_'.$cnum);                                                                                         $cdom.'_'.$cnum);
                                     }                                      }
                                 }                                  }
                                   if (($env{'request.lti.login'}) &&
                                       ($env{'request.lti.rosterid'} || $env{'request.lti.passbackid'})) {
                                       &process_lti($r,$cdom,$cnum);
                                   }
  $r->internal_redirect($dest);   $r->internal_redirect($dest);
     }      }
     return OK;      return OK;
Line 664  ENDENTERKEY Line 692  ENDENTERKEY
     if (($ferr) && ($tadv)) {      if (($ferr) && ($tadv)) {
  &error_page($r,$ferr,$furl);   &error_page($r,$ferr,$furl);
     } else {      } else {
                                   if (($env{'request.lti.login'}) &&
                                       ($env{'request.lti.rosterid'} || $env{'request.lti.passbackid'})) {
                                       &process_lti($r,$cdom,$cnum);
                                   }
  # Check to see if the user is a CC entering a course    # Check to see if the user is a CC entering a course 
  # for the first time   # for the first time
  if ((($role eq 'cc') || ($role eq 'co'))    if ((($role eq 'cc') || ($role eq 'co')) 
Line 697  ENDENTERKEY Line 729  ENDENTERKEY
                                             if (($dest =~ m{^\Q/public/$cdom/$cnum/syllabus\E.*(\?|\&)usehttp=1}) ||                                              if (($dest =~ m{^\Q/public/$cdom/$cnum/syllabus\E.*(\?|\&)usehttp=1}) ||
                                                 ($dest =~ m{^\Q/adm/wrapper/ext/\E(?!https:)})) {                                                  ($dest =~ m{^\Q/adm/wrapper/ext/\E(?!https:)})) {
                                                 if ($ENV{'SERVER_PORT'} == 443) {                                                  if ($ENV{'SERVER_PORT'} == 443) {
                                                     my $hostname = $r->hostname();                                                      unless (&Apache::lonnet::uses_sts()) {
                                                     if ($hostname ne '') {                                                          my $hostname = $r->hostname();
                                                         $dest = 'http://'.$hostname.$dest;                                                          if ($hostname ne '') {
                                                               $dest = 'http://'.$hostname.$dest;
                                                           }
                                                     }                                                      }
                                                 }                                                  }
                                             }                                              }
Line 738  ENDENTERKEY Line 772  ENDENTERKEY
                                                 $dest .= (($dest =~/\?/)? '&':'?').'symb='.$esc_symb;                                                  $dest .= (($dest =~/\?/)? '&':'?').'symb='.$esc_symb;
                                             }                                              }
                                         }                                          }
                                         &redirect_user($r, &mt('Entering [_1]',                                          my $title;
                                                        $env{'course.'.$cdom.'_'.$cnum.'.description'}),                                          unless ($env{'request.lti.login'}) {
                                                        $dest, $msg);                                              $title = &mt('Entering [_1]',
                                                            $env{'course.'.$cdom.'_'.$cnum.'.description'});
                                           }
                                           &redirect_user($r,$title,$dest,$msg);
                                         return OK;                                          return OK;
                                     }                                      }
     if (&Apache::lonnet::allowed('whn',      if (&Apache::lonnet::allowed('whn',
Line 778  ENDENTERKEY Line 815  ENDENTERKEY
                                 } elsif ($access eq 'B') {                                  } elsif ($access eq 'B') {
                                     $furl = '/adm/navmaps?showOnlyHomework=1';                                      $furl = '/adm/navmaps?showOnlyHomework=1';
                                 }                                  }
                                 $msg = &mt('Entering [_1] ...',                                  my $title;
    $env{'course.'.$cdom.'_'.$cnum.'.description'});                                  if ($env{'request.lti.login'}) {
  &redirect_user($r, &mt('Entering [_1]',                                      undef($msg);
                                $env{'course.'.$cdom.'_'.$cnum.'.description'}),                                  } else {
                                $furl, $msg);                                      $title = &mt('Entering [_1]',
                                                    $env{'course.'.$cdom.'_'.$cnum.'.description'});
                                       $msg = &mt('Entering [_1] ...',
          $env{'course.'.$cdom.'_'.$cnum.'.description'});
                                   }
    &redirect_user($r,$title,$furl,$msg);
     }      }
     return OK;      return OK;
  }   }
Line 1074  ENDHEADER Line 1116  ENDHEADER
   
 # No active roles  # No active roles
     if ($countactive==0) {      if ($countactive==0) {
         &requestcourse_advice($r,$cattype,$inrole);           my $elapsed = 0;
           if ($now && $update) {
               $elapsed = $now - $update;
           }
           &requestcourse_advice($r,$cattype,$inrole,$elapsed); 
  $r->print('</form>');   $r->print('</form>');
         if ($countfuture) {          if ($countfuture) {
             $r->print(&mt('The following [quant,_1,role,roles] will become active in the future:',$countfuture));              $r->print(&mt('The following [quant,_1,role,roles] will become active in the future:',$countfuture));
Line 1334  sub gather_roles { Line 1380  sub gather_roles {
                     if (!$allowed) {                      if (!$allowed) {
                         $button=0;                          $button=0;
                         unless ($trustchecked{$tdom}) {                          unless ($trustchecked{$tdom}) {
                             if ((&Apache::lonnet::will_trust('othcoau',$tdom,$env{'user.domain'})) &&                              if ((&Apache::lonnet::will_trust('othcoau',$env{'user.domain'},$tdom)) &&
                                 (&Apache::lonnet::will_trust('coremau',$env{'user.domain'},$tdom))) {                                  (&Apache::lonnet::will_trust('coaurem',$tdom,$env{'user.domain'}))) {
                                 $willtrust{$tdom} = 1;                                  $willtrust{$tdom} = 1;
                                 $trustchecked{$tdom} = 1;                                  $trustchecked{$tdom} = 1;
                             }                              }
Line 1624  sub print_rolerows { Line 1670  sub print_rolerows {
 }  }
   
 sub findcourse_advice {  sub findcourse_advice {
     my ($r,$cattype) = @_;      my ($r,$cattype,$elapsed) = @_;
     my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');      my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
     my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');      my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
     if (&Apache::lonnet::auto_run(undef,$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('<p>'.&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).'
 <ul>  <ul>
  <li>'.&mt('The course has yet to be created.').'</li>   <li>'.&mt('The course has yet to be created.').'</li>
  <li>'.&mt('Automatic enrollment of registered students has not been enabled for the course.').'</li>   <li>'.&mt('Automatic enrollment of registered students has not been enabled for the course.').'</li>
  <li>'.&mt('You are in a section of course for which automatic enrollment in the corresponding LON-CAPA course is not active.').'</li>   <li>'.&mt('You are in a section of course for which automatic enrollment in the corresponding LON-CAPA course is not active.').'</li>
  <li>'.&mt('The start date for automated enrollment has yet to be reached.').'</li>   <li>'.&mt('The start date for automated enrollment has yet to be reached.').'</li>
  <li>'.&mt('You registered for the course recently and there is a time lag between the time you register, and the time this information becomes available for the update of LON-CAPA course rosters.').'</li>   <li>'.&mt('You registered for the course recently and there is a time lag between the time you register, and the time this information becomes available for the update of LON-CAPA course rosters.').'</li>
  </ul>');   <li>'.&mt('Automated enrollment added you to the course in the time since you last logged-in.').' '.&mt('If that is the case you can use the "Check for changes" link in the gray Functions bar to update the list of your available course roles.').'</li>   
    </ul></p>');
     } else {      } else {
         $r->print(&mt('If you were expecting to see an active role listed for a particular course, that course may not have been created yet.').'<br />');          $r->print('<p>'.&mt('If you were expecting to see an active role listed for a particular course, that course may not have been created yet.').'</p>');
           if ($elapsed > 600) {
               $r->print('<p>'.&mt('You may also have been assigned to a course in the time since you last logged-in, or checked for changes').
                         '<br />'.
                         &mt('If that is the case you can use the "Check for changes" link in the gray Functions bar to update the list of your available course roles.').'</p>');
           }  
     }      }
     if (($cattype eq 'std') || ($cattype eq 'domonly')) {      if (($cattype eq 'std') || ($cattype eq 'domonly')) {
         $r->print('<h3>'.&mt('Self-Enrollment').'</h3>'.          $r->print('<h3>'.&mt('Self-Enrollment').'</h3>'.
Line 1649  sub findcourse_advice { Line 1701  sub findcourse_advice {
 }  }
   
 sub requestcourse_advice {  sub requestcourse_advice {
     my ($r,$cattype,$inrole) = @_;      my ($r,$cattype,$inrole,$elapsed) = @_;
     my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');      my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
     my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');      my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
     my (%can_request,%request_doms,$output);      my (%can_request,%request_doms,$output);
Line 1710  sub requestcourse_advice { Line 1762  sub requestcourse_advice {
         } else {          } else {
             $r->print('<h3>'.&mt('Currently no active roles, courses or communities').'</h3>');              $r->print('<h3>'.&mt('Currently no active roles, courses or communities').'</h3>');
         }          }
         &findcourse_advice($r,$cattype);          &findcourse_advice($r,$cattype,$elapsed);
     }      }
     return;      return;
 }  }
Line 3079  sub get_queued { Line 3131  sub get_queued {
            $output.'</fieldset></div><br clear="all" />';             $output.'</fieldset></div><br clear="all" />';
 }  }
   
   sub process_lti {
       my ($r,$cdom,$cnum) = @_;
       my %lti = &Apache::lonnet::get_domain_lti($cdom,'provider');
       my $uriscope = &LONCAPA::ltiutils::lti_provider_scope($env{'request.lti.uri'},
                                                             $cdom,$cnum);
       my $lonhost = $r->dir_config('lonHostID');
       my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
       if ($env{'request.lti.rosterid'} &&
           $env{'request.lti.rosterurl'}) {
           if (ref($lti{$env{'request.lti.login'}}) eq 'HASH') {
               if ($lti{$env{'request.lti.login'}}{'roster'}) {
                   my @lcroles = ('in','ta','ep','st');
                   my @possibleroles;
                   foreach my $role (@lcroles) {
                       if (&Apache::lonnet::allowed('c'.$role,"$cdom/$cnum")) {
                           push(@possibleroles,$role);
                       }
                   }
                   my $owner = $env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'};
                   if ($owner eq $env{'user.name'}.':'.$env{'user.domain'}) {
                       my $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
                       if ($crstype eq 'Community') {
                           unshift(@possibleroles,'co');
                       } else {
                           unshift(@possibleroles,'cc');
                       }
                   }
                   if (@possibleroles) {
                       push(@{$rosterupdates},{cid        => $cdom.'_'.$cnum,
                                               lti        => $env{'request.lti.login'},
                                               ltiref     => $lti{$env{'request.lti.login'}},
                                               id         => $env{'request.lti.rosterid'},
                                               url        => $env{'request.lti.rosterurl'},
                                               sourcecrs  => $env{'request.lti.sourcecrs'},
                                               uriscope   => $uriscope,
                                               possroles  => \@possibleroles,
                                               intdoms    => $internet_names,
                                              });
                       unless ($registered_cleanup) {
                           my $handlers = $r->get_handlers('PerlCleanupHandler');
                           $r->set_handlers('PerlCleanupHandler' =>
                                            [\&ltienroll,@{$handlers}]);
                           $registered_cleanup=1;
                       }
                   }
               }
           }
       }
       if ($env{'request.lti.passbackid'} &&
           $env{'request.lti.passbackurl'}) {
           if (ref($lti{$env{'request.lti.login'}}) eq 'HASH') {
               if ($lti{$env{'request.lti.login'}}{'passback'}) {
                   my ($pbnum,$error) =
                       &LONCAPA::ltiutils::store_passbackurl($env{'request.lti.login'},
                                                             $env{'request.lti.passbackurl'},
                                                             $cdom,$cnum);
                   if ($pbnum eq '') {
                       $pbnum = $env{'request.lti.passbackurl'};
                   }
                   &Apache::lonnet::put('nohist_'.$cdom.'_'.$cnum.'_passback',
                                        {"$uriscope\0$env{'request.lti.sourcecrs'}\0$env{'request.lti.login'}" =>
                                        "$pbnum\0$env{'request.lti.passbackid'}"});
               }
           }
       }
       return;
   }
   
   sub ltienroll {
       if (ref($rosterupdates) eq 'ARRAY') {
           foreach my $item (@{$rosterupdates}) {
               if (ref($item) eq 'HASH') {
                   &LONCAPA::ltiutils::batchaddroster($item);
               }
           }
       }
   }
   
 1;  1;
 __END__  __END__
   

Removed from v.1.335  
changed lines
  Added in v.1.345


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