Diff for /loncom/interface/selfenroll.pm between versions 1.11 and 1.27.2.2

version 1.11, 2008/07/14 10:25:25 version 1.27.2.2, 2014/04/05 13:17:16
Line 34  use Apache::lonnet; Line 34  use Apache::lonnet;
 use Apache::loncommon;  use Apache::loncommon;
 use Apache::lonlocal;  use Apache::lonlocal;
 use Apache::createaccount;  use Apache::createaccount;
   use Apache::loncoursequeueadmin;
   use Apache::lonuserutils;
 use LONCAPA qw(:DEFAULT :match);  use LONCAPA qw(:DEFAULT :match);
   
 sub handler {  sub handler {
Line 51  sub handler { Line 53  sub handler {
     }      }
     &Apache::lonacc::get_posted_cgi($r);      &Apache::lonacc::get_posted_cgi($r);
     &Apache::lonlocal::get_language_handle($r);      &Apache::lonlocal::get_language_handle($r);
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['cid']);      &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['courseid']);
     my ($coursechk,$cid) = &validate_course_id($env{'form.cid'});      my $js = &Apache::createaccount::catreturn_js();
     if ($coursechk ne 'ok') {  
         &page_header($r,$cid);      my $courseid = Apache::lonnet::is_course($env{'form.courseid'});
   
       unless ($courseid) {
           &page_header($r,$env{'form.courseid'},$js);
         $r->print('<h3>'.&mt('Self-enrollment error').'</h3>'.          $r->print('<h3>'.&mt('Self-enrollment error').'</h3>'.
                   '<span class="LC_error">'.                    '<span class="LC_error">'.
                   &mt('Invalid domain or course number').'</span>');                    &mt('Invalid domain or course number').'</span>');
         $r->print(&Apache::loncommon::end_page());          &page_footer($r);
         return OK;          return OK;
     }      }
     my $now = time;      my $now = time;
     my $js;  
     if ($env{'form.phase'} eq 'login') {      if ($env{'form.phase'} eq 'login') {
         $js = &Apache::createaccount::javascript_setforms($now);          $js .= "\n".&Apache::createaccount::javascript_setforms($now);
     }      }
     &page_header($r,$cid,$js);      my %coursehash = &Apache::lonnet::coursedescription($courseid);
       my $cdom = $coursehash{'domain'};
       my $cnum = $coursehash{'num'};
       my $desc = $coursehash{'description'};
       &page_header($r,$courseid,$js,$desc);
     my $include = $r->dir_config('lonIncludes');      my $include = $r->dir_config('lonIncludes');
     if ($env{'form.phase'} eq 'login') {      if ($env{'form.phase'} eq 'login') {
         my $jsh=Apache::File->new($include."/londes.js");          my $jsh=Apache::File->new($include."/londes.js");
         $r->print(<$jsh>);          $r->print(<$jsh>);
     }      }
     my ($cdom,$cnum,$desc,$canenroll,$selfenroll_types,$selfenroll_registered,      my ($canenroll,$selfenroll_types,$selfenroll_registered,@cancreate,
         @cancreate,$knownuser,$selfenroll_access_start,$selfenroll_access_end,          $knownuser,$selfenroll_access_start,$selfenroll_access_end,
         $selfenroll_section,$selfenroll_future,%curr_role,%coursehash,$cdomdesc);          $selfenroll_section,$selfenroll_future,%curr_role,$cdomdesc,
     %coursehash = &Apache::lonnet::coursedescription($cid);          $selfenroll_approval,$selfenroll_limit,$selfenroll_cap,
     $cdom = $coursehash{'domain'};          $selfenroll_notifylist,$owner,$crstype);
     $cnum = $coursehash{'num'};  
     $desc = $coursehash{'description'};  
     $selfenroll_types = $coursehash{'internal.selfenroll_types'};      $selfenroll_types = $coursehash{'internal.selfenroll_types'};
     $selfenroll_registered =  $coursehash{'internal.selfenroll_registered'};      $selfenroll_registered =  $coursehash{'internal.selfenroll_registered'};
     $selfenroll_section = $coursehash{'internal.selfenroll_section'};      $selfenroll_section = $coursehash{'internal.selfenroll_section'};
     $selfenroll_access_start =  $coursehash{'internal.selfenroll_start_access'};      $selfenroll_access_start =  $coursehash{'internal.selfenroll_start_access'};
     $selfenroll_access_end =  $coursehash{'internal.selfenroll_end_access'};      $selfenroll_access_end =  $coursehash{'internal.selfenroll_end_access'};
       $selfenroll_limit =  $coursehash{'internal.selfenroll_limit'};
       $selfenroll_cap = $coursehash{'internal.selfenroll_cap'};
       $selfenroll_approval = $coursehash{'internal.selfenroll_approval'};
       $selfenroll_notifylist = $coursehash{'internal.selfenroll_notifylist'};
       $owner = $coursehash{'internal.courseowner'};
       $crstype = $coursehash{'internal.type'};
       if ($crstype eq '') {
           $crstype = 'Course';
       }
       my $nospace;
     if ($selfenroll_types ne '') {      if ($selfenroll_types ne '') {
         my $start = $coursehash{'internal.selfenroll_start_date'};          my $start = $coursehash{'internal.selfenroll_start_date'};
         my $end = $coursehash{'internal.selfenroll_end_date'};          my $end = $coursehash{'internal.selfenroll_end_date'};
         if (($start > 0 && $start < $now) && (($end == 0) || ($end > 0 && $end > $now))) {          if (($start > 0 && $start < $now) && (($end == 0) || ($end > 0 && $end > $now))) {
             $canenroll = 1;              if (($selfenroll_limit eq 'allstudents') || 
                   ($selfenroll_limit eq 'selfenroll')) {
                   $nospace = 
                       &enrollment_limit_check($selfenroll_limit,$selfenroll_cap,
                                               $cdom,$cnum);
                   if (!$nospace) {
                       $canenroll = 1; 
                   }
               } else {
                   $canenroll = 1;
               }
         } elsif (($end == 0) || ($end > 0 && $end > $now)) {          } elsif (($end == 0) || ($end > 0 && $end > $now)) {
             if ($start > $now) {              if ($start > $now) {
                 $selfenroll_future = &Apache::lonlocal::locallocaltime($start);                  if (($selfenroll_limit eq 'allstudents') ||
                       ($selfenroll_limit eq 'selfenroll')) {
                       $nospace = 
                           &enrollment_limit_check($selfenroll_limit,$cdom,$cnum);
                   }
                   if (!$nospace) {
                       $selfenroll_future = &Apache::lonlocal::locallocaltime($start);
                   }
             }              }
         }          }
     }      }
Line 100  sub handler { Line 133  sub handler {
         $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3><span class="LC_warning">'.          $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3><span class="LC_warning">'.
                   &mt('Self-enrollment is not currently available for this course.').                    &mt('Self-enrollment is not currently available for this course.').
                   '</span><br /><br />');                    '</span><br /><br />');
           if ($nospace) {
               if ($selfenroll_limit eq 'allstudents') {
                   $r->print(&mt('The enrollment limit of [quant,_1,student] has been reached.',$selfenroll_cap));
               } else {
                   $r->print(&mt('The enrollment limit of [quant,_1,self-enrolled student] has been reached.',$selfenroll_cap));
   
               }
           } 
         if ($selfenroll_types ne '') {          if ($selfenroll_types ne '') {
             if ($selfenroll_future ne '') {              if ($selfenroll_future ne '') {
                 if ($selfenroll_types eq '*') {                  if ($selfenroll_types eq '*') {
Line 167  sub handler { Line 208  sub handler {
         if (keys(%curr_role)) {          if (keys(%curr_role)) {
             $r->print(&has_role(%curr_role));              $r->print(&has_role(%curr_role));
         }          }
         $r->print(&Apache::loncommon::end_page());          &page_footer($r);
         return OK;          return OK;
     }      }
     @cancreate = &can_create($cdom);      @cancreate = &can_create($cdom);
     my ($login_path,$firsturl,$create_path,$sso_url,$missing_formitem);      my ($form,$login_path,$firsturl,$create_path,$sso_url,$missing_formitem);
       $form = 'logmein';
     $login_path = '/adm/login';      $login_path = '/adm/login';
     $firsturl= '/adm/selfenroll?cid='.$cid;      $firsturl= '/adm/selfenroll?courseid='.$courseid;
     $create_path = '/adm/createaccount';      $create_path = '/adm/createaccount';
     $sso_url = $r->dir_config('lonSSOReloginServer');      $sso_url = $r->dir_config('lonSSOReloginServer');
     if ($sso_url eq '') {      if ($sso_url eq '') {
Line 184  sub handler { Line 226  sub handler {
         if (keys(%curr_role)) {          if (keys(%curr_role)) {
             $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.              $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.
                       '<span class="LC_warning">'.&has_role(%curr_role).'</span>');                        '<span class="LC_warning">'.&has_role(%curr_role).'</span>');
             $r->print(&Apache::loncommon::end_page());              &page_footer($r);
             return OK;              return OK;
         }          }
         &process_self_enroll($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,          &process_self_enroll($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,
                              $selfenroll_access_start,$selfenroll_access_end,                               $selfenroll_access_start,$selfenroll_access_end,
                              $selfenroll_section,$now);                               $selfenroll_section,$now,$selfenroll_approval,
                                $selfenroll_notifylist,$owner,$crstype,$lonhost,$handle);
     } elsif ($env{'form.phase'} eq 'login') {      } elsif ($env{'form.phase'} eq 'login') {
         my $submit_text = &mt('Log in');          my $submit_text = &mt('Log in');
         $r->print('<h3>'.&mt('Log-in to LON-CAPA').'</h3>');          $r->print('<h3>'.&mt('Log-in to LON-CAPA').'</h3>');
         my $udom = &Apache::lonnet::default_login_domain();          my $udom = &Apache::lonnet::default_login_domain();
         $r->print(&Apache::createaccount::login_box($now,$lonhost,$cid,          $r->print(&Apache::createaccount::login_box($now,$lonhost,$courseid,
                                            $submit_text,$udom,'selfenroll'));                                             $submit_text,$udom,'selfenroll'));
         $r->print(&mt('You will be able to self-enroll in the course you selected ([_1]) after you have successfully logged in.','<b>'.$desc.'</b>').          $r->print(&mt('You will be able to self-enroll in the course you selected ([_1]) after you have successfully logged in.','<b>'.$desc.'</b>'));
                   &Apache::loncommon::end_page());          &page_footer($r);
         return OK;          return OK;
     } elsif (@cancreate > 0) {      } elsif (@cancreate > 0) {
         $r->print(<<END);          $r->print(<<END);
 <script type="text/javascript">  <script type="text/javascript">
 function setPath(formaction,item,arg) {  function setPath(formname,formaction,item,arg) {
     if (formaction != '') {      var formidx = getFormByName(formname);
         document.forms.logmein.action = formaction;      if (formidx > -1) {
     }          if (formaction != '') {
     var itemid = getIndexByName("setting");              document.forms[formidx].action = formaction;
     if (itemid > -1) {          }
         document.logmein.elements[itemid].name = item;          var itemid = getIndexByName(formidx,'setting');
         document.logmein.elements[itemid].value = arg;          if (itemid > -1) {
         document.logmein.submit();              document.forms[formidx].elements[itemid].name = item;
     } else {              document.forms[formidx].elements[itemid].value = arg;
         alert("$missing_formitem");              document.forms[formidx].submit();
           } else {
               alert("$missing_formitem");
           }
     }      }
     return;      return;
 }  }
   
 function getIndexByName(item) {  
     for (var i=0;i<document.logmein.elements.length;i++) {  
         if (document.logmein.elements[i].name == item) {  
             return i;  
         }  
     }  
     return -1;  
 }  
 </script>  </script>
 END  END
         $r->print('<h3>'.&mt('LON-CAPA account required').'</h3>'.          $r->print('<h3>'.&mt('LON-CAPA account required').'</h3>'.
                   &mt('You need to be logged into LON-CAPA to self-enroll in a course.').'<ul><li>'.&mt('If you have an account in LON-CAPA, [_1]log-in[_2].','<a href="javascript:setPath('."'','phase','login'".')">','</a>').'</li>');                    &mt('You need to be logged into LON-CAPA to self-enroll in a course.').'<ul><li>'.&mt('If you have an account in LON-CAPA, [_1]log-in[_2].','<a href="javascript:setPath('."'$form','','phase','login'".')">','</a>').'</li>');
         if (@cancreate > 1) {          if (@cancreate > 1) {
             $r->print('<li>'.&mt('Otherwise:').'<br />');              $r->print('<li>'.&mt('Otherwise:').'<br />');
             my $count = 0;              my $count = 0;
Line 238  END Line 276  END
                 }                  }
                 if (grep(/^$type$/,@cancreate)) {                  if (grep(/^$type$/,@cancreate)) {
                     if ($type eq 'sso') {                      if ($type eq 'sso') {
                         $r->print('&nbsp;&nbsp;'.&mt("if you have an institutional log-in, use your institution's [_1]single sign on page[_2] to log-in, then create an account",'<a href="javascript:setPath('."'$sso_url','firsturl','$firsturl'".')">','</a>'));                          $r->print('&nbsp;&nbsp;'.&mt("if you have an institutional log-in, use your institution's [_1]single sign on page[_2] to log-in, then create an account",'<a href="javascript:setPath('."'$form','$sso_url','firsturl','$firsturl'".')">','</a>'));
                     } elsif ($type eq 'login') {                      } elsif ($type eq 'login') {
                         $r->print('&nbsp;&nbsp;'.&mt('if you have an institutional login, [_1]create an account[_2]','<a href="javascript:setPath('."'$create_path','courseid','$cid'".')">','</a>'));                          $r->print('&nbsp;&nbsp;'.&mt('if you have an institutional login, [_1]create an account[_2]','<a href="javascript:setPath('."'$form','$create_path','context','selfenroll_create'".')">','</a>'));
                     } elsif ($type eq 'email') {                      } elsif ($type eq 'email') {
                         $r->print('&nbsp;&nbsp;'.&mt('you can use an e-mail address to [_1]create an account[_2]','<a href="javascript:setPath('."'$create_path','courseid','$cid'".')">','</a>'));                          $r->print('&nbsp;&nbsp;'.&mt('you can use an e-mail address to [_1]create an account[_2]','<a href="javascript:setPath('."'$form','$create_path','context','selfenroll_create'".')">','</a>'));
                     }                      }
                     $count ++;                      $count ++;
                 }                  }
Line 250  END Line 288  END
             $r->print('.<br />');              $r->print('.<br />');
         } else {          } else {
             if ($cancreate[0] eq 'login') {              if ($cancreate[0] eq 'login') {
                 $r->print('<li>'.&mt('Otherwise, if you have an institutional login, [_1]create an account[_2].','<a href="javascript:setPath('."'$create_path','courseid','$cid'".')">','</a>'));                  $r->print('<li>'.&mt('Otherwise, if you have an institutional login, [_1]create an account[_2].','<a href="javascript:setPath('."'$form','$create_path','context','selfenroll_create'".')">','</a>'));
             } elsif ($cancreate[0] eq 'email') {              } elsif ($cancreate[0] eq 'email') {
                 $r->print('<li>'.&mt('Otherwise, you will use an e-mail address to [_1]create an account[_2].','<a href="javascript:setPath('."'$create_path','courseid','$cid'".')">','</a>'));                  $r->print('<li>'.&mt('Otherwise, you will use an e-mail address to [_1]create an account[_2].','<a href="javascript:setPath('."'$form','$create_path','context','selfenroll_create'".')">','</a>'));
             } elsif ($cancreate[0] eq 'sso') {              } elsif ($cancreate[0] eq 'sso') {
                 $r->print('<li>'.&mt("Otherwise, use your institution's [_1]single sign on page[_2] to log-in, then create an account.",'<a href="javascript:setPath('."'$sso_url','firsturl','$firsturl'".')">','</a>'));                  $r->print('<li>'.&mt("Otherwise, use your institution's [_1]single sign on page[_2] to log-in, then create an account.",'<a href="javascript:setPath('."'$form','$sso_url','firsturl','$firsturl'".')">','</a>'));
             }              }
         }          }
         if ($selfenroll_types eq '*') {          if ($selfenroll_types eq '*') {
Line 277  END Line 315  END
         my $displayurl = &escape($firsturl);          my $displayurl = &escape($firsturl);
         $r->print(&mt('Submit a request to the LON-CAPA [_1]helpdesk[_2] for [_3] if you require assistance.','<a href="/adm/helpdesk?origurl='.$displayurl.'">','</a>',$cdomdesc));          $r->print(&mt('Submit a request to the LON-CAPA [_1]helpdesk[_2] for [_3] if you require assistance.','<a href="/adm/helpdesk?origurl='.$displayurl.'">','</a>',$cdomdesc));
     }      }
     $r->print('<form name ="logmein" method="post" action="">'."\n".      $r->print("\n".'<form name="'.$form.'" method="post" action="">'."\n".
               '<input type="hidden" name=cid" value="'.$cid.'" />'."\n".                '<input type="hidden" name="courseid" value="'.$courseid.'" />'."\n".
               '<input type="hidden" name="setting" value=""/>'."\n".                '<input type="hidden" name="setting" value=""/>'."\n".
               '</form>'."\n".                &Apache::lonhtmlcommon::echo_form_input(['backto','courseid','context','phase'])."\n".
               &Apache::loncommon::end_page());                '</form>'."\n");
                 &page_footer($r);
     return OK;      return OK;
 }  }
   
   sub enrollment_limit_check {
       my ($selfenroll_limit,$selfenroll_cap,$cdom,$cnum) = @_;
       my $nospace = 0;
       my (%idx,%stucount);
       my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
       $idx{'type'} = &Apache::loncoursedata::CL_TYPE();
       $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
       while (my ($student,$data) = each(%$classlist)) {
           if (($data->[$idx{'status'}] eq 'Active') ||
               ($data->[$idx{'status'}] eq 'Future')) {
               if ($data->[$idx{'type'}] eq 'selfenroll') {
                   $stucount{'selfenroll'} ++;
               }
               $stucount{'allstudents'} ++;
           }
       }
       if ($stucount{$selfenroll_limit} >= $selfenroll_cap) {
           $nospace = 1;
       }
       return $nospace;
   }
   
 sub page_header {  sub page_header {
     my ($r,$cid,$js) = @_;      my ($r,$courseid,$js,$desc) = @_;
     my $start_page =      my $start_page =
         &Apache::loncommon::start_page('Self-enroll in a LON-CAPA course',$js,          &Apache::loncommon::start_page('Self-enroll in a LON-CAPA course',$js,
                                        {'no_inline_link'   => 1,});                                         {'no_inline_link'   => 1,});
     $r->print($start_page);      $r->print($start_page);
     &Apache::lonhtmlcommon::clear_breadcrumbs();      &Apache::lonhtmlcommon::clear_breadcrumbs();
     &Apache::lonhtmlcommon::add_breadcrumb      &Apache::createaccount::selfenroll_crumbs($r,$courseid,$desc);
                        ({href=>"/adm/selfenroll?cid=$cid",  
                          text=>"Self-enroll"});  
     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Self-enroll in course'));      $r->print(&Apache::lonhtmlcommon::breadcrumbs('Self-enroll in course'));
     return;      return;
 }  }
   
 sub validate_course_id {  sub page_footer {
     my ($cid) = @_;      my ($r) = @_;
     my ($cdom,$cnum) = ($env{'form.cid'} =~ /^($match_domain)_($match_courseid)$/);      $r->print('<form name="backupcrumbs" method="post" action="/adm/coursecatalog">'."\n".
     if ($cdom ne '' && $cnum ne '') {                &Apache::lonhtmlcommon::echo_form_input(['backto','courseid','phase','context']).
         if (&Apache::lonnet::is_course($cdom,$cnum)) {                '</form>'.&Apache::loncommon::end_page());
             return ('ok',$cid);  
         }  
     }   
     return;      return;
 }  }
   
Line 345  sub has_role { Line 401  sub has_role {
     my $output;      my $output;
     if ($curr_role{'status'} eq 'active') {      if ($curr_role{'status'} eq 'active') {
           my $rolelink = &jump_to_role($curr_role{'role'});            my $rolelink = &jump_to_role($curr_role{'role'});
           $output = &mt('You already have an active student role (section: "[_1]") in this course.',$curr_role{'section'}).'<br>'.$rolelink;            $output = &mt('You already have an active student role (section: "[_1]") in this course.',$curr_role{'section'}).'<br />'.$rolelink;
     } elsif ($curr_role{'status'} eq 'future') {      } elsif ($curr_role{'status'} eq 'future') {
         $output = &mt('You have a student role (section: "[_1]") in this course which will become active [_2].',$curr_role{'section'},$curr_role{'start'});          $output = &mt('You have a student role (section: "[_1]") in this course which will become active [_2].',$curr_role{'section'},$curr_role{'start'});
     }      }
Line 354  sub has_role { Line 410  sub has_role {
   
 sub process_self_enroll {  sub process_self_enroll {
     my ($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,      my ($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,
         $selfenroll_access_start,$selfenroll_access_end,$selfenroll_section,$now) = @_;          $selfenroll_access_start,$selfenroll_access_end,$selfenroll_section,
           $now,$selfenroll_approval,$selfenroll_notifylist,$owner,$crstype,$lonhost,$handle) = @_;
     my $udom = $env{'user.domain'};      my $udom = $env{'user.domain'};
     my $uname = $env{'user.name'};      my $uname = $env{'user.name'};
     my $selfenroll = 0;      my $selfenroll = 0;
Line 385  sub process_self_enroll { Line 442  sub process_self_enroll {
             my ($registered,$instsec,$message) = &check_registered($cdom,$cnum);              my ($registered,$instsec,$message) = &check_registered($cdom,$cnum);
             $usec = $instsec;              $usec = $instsec;
             if (!$registered) {              if (!$registered) {
                 $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.&mt('Self-enrollment is restricted to students officially registered for this course.').'<br />');                  $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.
                             &mt('Self-enrollment is restricted to students officially registered for this course.').'<br />');
                 if ($message) {                  if ($message) {
                     $r->print($message);                      $r->print($message);
                 } else {                  } else {
Line 394  sub process_self_enroll { Line 452  sub process_self_enroll {
                 return;                   return; 
             }               } 
         }          }
         my $enrollresult =           if ($selfenroll_approval) {
             &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,              my $outcome = 
                     undef,undef,$usec,$selfenroll_access_end,$selfenroll_access_start,                  &store_selfenroll_request($udom,$uname,$usec,$cdom,$cnum,
                    'manual',undef,$cdom.'_'.$cnum,$selfenroll);                                            $selfenroll_notifylist,$owner,
         if ($enrollresult eq 'ok') {                                            $selfenroll_approval,$crstype,$lonhost,$handle);
             my (%userroles,%newrole,%newgroups);              $r->print($outcome);
             my $role = 'st';          } else {
             my $area = '/'.$cdom.'/'.$cnum;              my $enrollresult = 
             my $spec = $role.'.'.$area;                  &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,
             if ($usec ne '') {                          undef,undef,$usec,$selfenroll_access_end,$selfenroll_access_start,
                 $spec .= '/'.$usec;                         'selfenroll',undef,$cdom.'_'.$cnum,$selfenroll);
                 $area .= '/'.$usec;              if ($enrollresult eq 'ok') {
             }                  my (%userroles,%newrole,%newgroups);
             &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,                  my $role = 'st';
                                                 $area);                  my $area = '/'.$cdom.'/'.$cnum;
             &Apache::lonnet::set_userprivs(\%userroles,\%newrole,%newgroups);                  my $spec = $role.'.'.$area;
             $userroles{'user.role.'.$spec} = $selfenroll_access_start.'.'.$selfenroll_access_end;                  if ($usec ne '') {
             &Apache::lonnet::appenv(\%userroles,[$role,'cm']);                      $spec .= '/'.$usec;
             $r->print('<h3>'.&mt('Enrollment process complete').'</h3>');                      $area .= '/'.$usec;
             if ($selfenroll_access_end && $selfenroll_access_end <= $now) {                  }
                 $r->print(&mt('The end date for access to this course for users who self-enroll has passed.').'<br />'.&mt('Consequently, although a new role was created for you in the course, it is an inactive role which does not provide access to the course.'));                  &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,
             } else {                                                      $area);
                 $r->print(&mt('Self-enrollment in this course was successful.').'<br />');                  &Apache::lonnet::set_userprivs(\%userroles,\%newrole,\%newgroups);
                 my $showstart = &Apache::lonlocal::locallocaltime($selfenroll_access_start);                  $userroles{'user.role.'.$spec} = $selfenroll_access_start.'.'.$selfenroll_access_end;
                 my $showend = &Apache::lonlocal::locallocaltime($selfenroll_access_end);                  &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
                 if ($selfenroll_access_start && $selfenroll_access_start >$now) {                  $r->print('<h3>'.&mt('Enrollment process complete').'</h3>');
                     $r->print(&mt('The start date for access to this course for users who self-enroll has yet to be reached.').'<br />'.&mt('Consequently, although a new role was created for you in the course, you will not be able to select this role until [_1].',$showstart));                  if ($selfenroll_access_end && $selfenroll_access_end <= $now) {
                       $r->print(&mt('The end date for access to this course for users who self-enroll has passed.').'<br />'.&mt('Consequently, although a new role was created for you in the course, it is an inactive role which does not provide access to the course.'));
                 } else {                  } else {
                     my $newrole = 'st./'.$cdom.'/'.$cnum;                      $r->print(&mt('Self-enrollment in this course was successful.').'<br />');
                     if ($usec ne '') {                      my $showstart = &Apache::lonlocal::locallocaltime($selfenroll_access_start);
                         $newrole .= '/'.$usec;                      my $showend = &Apache::lonlocal::locallocaltime($selfenroll_access_end);
                       if ($selfenroll_access_start && $selfenroll_access_start >$now) {
                           $r->print(&mt('The start date for access to this course for users who self-enroll has yet to be reached.').'<br />'.&mt('Consequently, although a new role was created for you in the course, you will not be able to select this role until [_1].',$showstart));
                       } else {
                           my $newrole = 'st./'.$cdom.'/'.$cnum;
                           if ($usec ne '') {
                               $newrole .= '/'.$usec;
                           }
                           my $rolelink = &jump_to_role($newrole);
                           $r->print(&mt('Your new role is available immediately, and will provide access to the course until [_1].',$showend).'<br /><br />'."\n".
                                    $rolelink); 
                     }                      }
                     my $rolelink = &jump_to_role($newrole);  
                     $r->print(&mt('Your new role is available immediately, and will provide access to the course until [_1].',$showend).'<br /><br />'."\n".  
                              $rolelink);   
                 }                  }
             }              } else {
         } else {                  $r->print('<h3>'.&mt('Enrollment incomplete').'</h3>'.
             $r->print('<h3>'.&mt('Enrollment incomplete').'</h3>'.                            &mt('Self-enrollment in this course failed.'));
                       &mt('Self-enrollment in this course failed.'));                  if ($enrollresult ne '') {
             if ($enrollresult ne '') {                      $r->print('<span class="LC_error">'.$enrollresult.'</span>');
                 $r->print('<span class="LC_error">'.$enrollresult.'</span>');                  }
             }              }
         }          }
     } else {      } else {
Line 449  sub process_self_enroll { Line 515  sub process_self_enroll {
 sub user_can_selfenroll {  sub user_can_selfenroll {
     my ($udom,$uname,$domenrolltypes) = @_;      my ($udom,$uname,$domenrolltypes) = @_;
     my $selfenroll = 0;      my $selfenroll = 0;
     my @info = ['inststatus'];      my %userhash = &Apache::lonnet::userenvironment($udom,$uname,'inststatus');
     my %userhash = &Apache::lonnet::userenvironment($udom,$uname,@info);  
     my @inststatuses;      my @inststatuses;
     if ($userhash{'inststatus'} eq '') {      if ($userhash{'inststatus'} eq '') {
         push(@inststatuses,'other');          push(@inststatuses,'other');
     } else {      } else {
         my @esc_statuses = split(/:/,$userhash{'inststatus'});          @inststatuses = split(':',$userhash{'inststatus'});
         @inststatuses = map { &unescape($_); } (@esc_statuses);  
     }      }
     foreach my $type (@inststatuses) {      foreach my $type (@inststatuses) {
         if (ref($domenrolltypes) eq 'ARRAY') {          if (ref($domenrolltypes) eq 'ARRAY') {
Line 469  sub user_can_selfenroll { Line 533  sub user_can_selfenroll {
     return $selfenroll;      return $selfenroll;
 }  }
   
   sub store_selfenroll_request {
       my ($udom,$uname,$usec,$cdom,$cnum,$selfenroll_notifylist,$owner,
           $selfenroll_approval,$crstype,$lonhost,$handle) = @_;
       my $namespace = 'selfenrollrequests';
       my $output;
       my $now = time;
       my %existing = 
           &Apache::lonnet::get($namespace,[$uname.':'.$udom],$cdom,$cnum);
       if ($existing{$uname.':'.$udom}) {
           $output = &mt('A self-enrollment request already exists for you for this course.').'<br />'.
                     &mt('Your earlier request is in a queue awaiting action by a Course Coordinator.').
                     '<br /><br />'.&Apache::loncoursequeueadmin::queued_selfenrollment();
       } else {
           my %selfenroll = (
                               $uname.':'.$udom => $now.':'.$usec,
                            );
           my $putresult = &Apache::lonnet::put($namespace,\%selfenroll,$cdom,$cnum);
           my $status = 'request';
           if ($selfenroll_approval eq '2') {
               $status = 'pending';
           } 
           if ($putresult eq 'ok') {
               my %userenroll = (
                                  $cdom.'_'.$cnum =>  {
                                                         timestamp => $now,
                                                         section   => $usec,
                                                         status    => $status,
                                                      });
               my $token;
               if ($status eq 'pending') {
                   $token = &Apache::lonnet::tmpput(\%selfenroll,$lonhost);
                   $userenroll{$cdom.'_'.$cnum}{'token'} = $token;
                   $userenroll{$cdom.'_'.$cnum}{'lonhost'} = $lonhost;
                   $userenroll{$cdom.'_'.$cnum}{'handle'} = $handle; 
               } 
               my $warning;
               my $userresult = &Apache::lonnet::put($namespace,\%userenroll,$udom,$uname);
               if ($userresult ne 'ok') {
                   $warning = &mt('An error occurred saving a personal record of your request.');
               }
               $output = &mt('Your request for self-enrollment has been recorded.').'<br />';
               if ($status eq 'pending') {
                   my $coursetype = &Apache::lonuserutils::get_extended_type($cdom,$cnum,$crstype); 
                   my %postvalues = (
                                      'username'   => $env{'user.name'},
                                      'domain'     => $env{'user.domain'},
                                      'course'     => $cdom.'_'.$cnum,
                                      'coursetype' => $coursetype,
                                      'token'      => $token,
                                    );
                   my %domconfig = &Apache::lonnet::get_dom('configuration',['selfenrollment'],$cdom);
   
                   if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
                       my ($url,$buttontext,$code,@fields);
                       if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
                           my %courseinfo = &Apache::lonnet::coursedescription($cdom.'_'.$cnum,{ 'one_time' => 1});
                           $postvalues{'uniquecode'} = $courseinfo{'internal.uniquecode'};
                           $postvalues{'description'} = $courseinfo{'description'};
                           $url = $domconfig{'selfenrollment'}{'validation'}{'url'};
                           if (ref($domconfig{'selfenrollment'}{'validation'}{'fields'}) eq 'ARRAY') {
                               @fields = @{$domconfig{'selfenrollment'}{'validation'}{'fields'}};
                           }
                           $buttontext = $domconfig{'selfenrollment'}{'validation'}{'button'};
   
                           $output .= $domconfig{'selfenrollment'}{'validation'}{'markup'};
                           if (($url =~ m{^(https?\://|/)}) && (@fields > 0)) {
                               $output .= '<form name="selfenrollvalidation" action="'.$url.'" method="post">'."\n";
                               foreach my $field (@fields) {
                                   if ($postvalues{$field}) {
                                       $output .= '<input type="hidden" name="'.$field.'" value="'.$postvalues{$field}.'" />'."\n";
                                   }
                               }
                               if ($buttontext eq '') {
                                   $buttontext = &mt('Complete my enrollment');
                               }
                               my $protocol = $Apache::lonnet::protocol{$lonhost};
                               $protocol = 'http' if ($protocol ne 'https');
                               my $enroller = $protocol.'://'.&Apache::lonnet::hostname($lonhost).'/cgi-bin/enrollqueued.pl';
                               $output .= '<input type="hidden" name="enroller" value="'.$enroller.'" />'."\n".
                                          '<input type="submit" name="validate" value="'.$buttontext.'" />'."\n".
                                          '</form>'."\n";
                           } else {
                               $status = 'request';
                           } 
                       }
                   } else {
                       $status = 'request';
                   }
               }
               if ($status eq 'request') {
                   $output .= &mt('A message will be sent to your LON-CAPA account when the course coordinator takes action on your request.').'<br />'.
                              &mt('To access your LON-CAPA message, go to the Main Menu and click on "Send and Receive Messages".').'<br />';
                   my %emails = &Apache::loncommon::getemails($uname,$udom);
                   if (($emails{'permanentemail'} ne '') || ($emails{'notification'} ne '')) {
                       my $address = $emails{'permanentemail'};
                       if ($address eq '') {
                           $address = $emails{'notification'};
                       }
                       $output.= &mt('An e-mail will also be sent to: [_1] when this occurs.',$address).'<br />';
                   }
                   if ($warning) { 
                       $output .= '<span class="LC_warning">'.$warning.'</span><br />';
                   }
                   $output .= &Apache::loncoursequeueadmin::queued_selfenrollment();
   
                   if ($selfenroll_notifylist) {
                       my $fullname = &Apache::loncommon::plainname($uname,$udom);
                       my %courseinfo = &Apache::lonnet::coursedescription($cdom.'_'.$cnum);
                       my $coursedesc = $courseinfo{'description'};
                       &Apache::loncoursequeueadmin::send_selfserve_notification(
                           $selfenroll_notifylist,$fullname,$cdom.'_'.$cnum,
                           $coursedesc,$now,'selfenrollreq',$owner);
                   }
               }
           } else {
               $output = '<span class="LC_error">'.&mt('An error occurred when recording your request.').'</span>';
   
           }
       }
       return $output;
   }
   
 sub jump_to_role {  sub jump_to_role {
     my ($role) = @_;      my ($role) = @_;
     my $output = <<"END";      my $output = <<"END";
Line 504  sub get_selfenroll_filters { Line 690  sub get_selfenroll_filters {
         if ($selfdom =~ /^$match_domain$/) {          if ($selfdom =~ /^$match_domain$/) {
             if (&Apache::lonnet::domain($selfdom) ne '') {              if (&Apache::lonnet::domain($selfdom) ne '') {
                 my @types = split(/,/,$type_str);                  my @types = split(/,/,$type_str);
                   my @unesc_types = map { &unescape($_); } @types;
                 my ($othertitle,$usertypes,$types) =                  my ($othertitle,$usertypes,$types) =
                     &Apache::loncommon::sorted_inst_types($selfdom);                      &Apache::loncommon::sorted_inst_types($selfdom);
                 if (ref($usertypes) eq 'HASH') {                  if (ref($usertypes) eq 'HASH') {
                     my $anytype = 1;                      my $anytype = 1;
                     foreach my $key (keys(%{$usertypes})) {                      foreach my $key (keys(%{$usertypes})) {
                         if (!grep(/^\Q$key\E$/,@types)) {                          if (!grep(/^\Q$key\E$/,@unesc_types)) {
                             $anytype = 0;                              $anytype = 0;
                             last;                              last;
                         }                          }
Line 521  sub get_selfenroll_filters { Line 708  sub get_selfenroll_filters {
                     }                      }
                     $usertypes->{'any'} = &mt('Any users');                      $usertypes->{'any'} = &mt('Any users');
                     $usertypes->{'other'} = &mt('Others');                      $usertypes->{'other'} = &mt('Others');
                       my @showtypes;
                     if ($anytype) {                      if ($anytype) {
                         @{$enrolltypes{$selfdom}} = ('any');                          @{$enrolltypes{$selfdom}} = ('any');
                           @showtypes =  ('any');
                     } else {                      } else {
                         @{$enrolltypes{$selfdom}} = @types;                          @{$enrolltypes{$selfdom}} = @types;
                           @showtypes = @unesc_types;
                     }                      }
                     @{$longtypes{$selfdom}} =                       @{$longtypes{$selfdom}} = 
                         map {$usertypes->{$_}} @{$enrolltypes{$selfdom}}                          map {$usertypes->{$_}} @showtypes;
                 }                  }
             }              }
         }          }
Line 552  sub print_selfenroll_types { Line 742  sub print_selfenroll_types {
                 $output = &mt('Self-enrollment is not currently available for this course for users affiliated with [_1].',$domdesc);                  $output = &mt('Self-enrollment is not currently available for this course for users affiliated with [_1].',$domdesc);
             }              }
         } elsif (keys(%{$longtypes}) > 0) {          } elsif (keys(%{$longtypes}) > 0) {
             $output = &mt('Self-enrollment in this course is only available to users affiliated with the following institutions, and who have the required status:').' <ul>';              $output = &mt('Self-enrollment in this course is only available to users affiliated with the following institutions, and who have the required status:').' <ul>'."\n";
             foreach my $selfdom (sort(keys(%{$longtypes}))) {              foreach my $selfdom (sort(keys(%{$longtypes}))) {
                 my $selfdomdesc = &Apache::lonnet::domain($selfdom,'description');                  my $selfdomdesc = &Apache::lonnet::domain($selfdom,'description');
                 if (ref($longtypes->{$selfdom}) eq 'ARRAY') {                  if (ref($longtypes->{$selfdom}) eq 'ARRAY') {
                     $output .= '<li>'.$selfdomdesc.': '.join(', ',@{$longtypes->{$selfdom}}).'</li>';                      $output .= '<li>'.$selfdomdesc.': '.join(', ',@{$longtypes->{$selfdom}}).'</li>'."\n";
                 }                  }
             }              }
             $output .= '</ul>';              $output .= '</ul>'."\n";
         } else {          } else {
             $output = &mt('Self-enrollment is not currently available for this course.');              $output = &mt('Self-enrollment is not currently available for this course.');
         }          }

Removed from v.1.11  
changed lines
  Added in v.1.27.2.2


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