--- loncom/interface/selfenroll.pm 2008/07/17 12:22:28 1.12 +++ loncom/interface/selfenroll.pm 2016/07/24 14:35:00 1.33 @@ -1,7 +1,7 @@ # The LearningOnline Network # Allow users to self-enroll in a course # -# $Id: selfenroll.pm,v 1.12 2008/07/17 12:22:28 raeburn Exp $ +# $Id: selfenroll.pm,v 1.33 2016/07/24 14:35:00 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -34,6 +34,8 @@ use Apache::lonnet; use Apache::loncommon; use Apache::lonlocal; use Apache::createaccount; +use Apache::loncoursequeueadmin; +use Apache::lonuserutils; use LONCAPA qw(:DEFAULT :match); sub handler { @@ -53,10 +55,11 @@ sub handler { &Apache::lonlocal::get_language_handle($r); &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['courseid']); my $js = &Apache::createaccount::catreturn_js(); - my $desc; - my ($coursechk,$courseid) = &validate_course_id($env{'form.courseid'}); - if ($coursechk ne 'ok') { - &page_header($r,$courseid,$js); + + my $courseid = Apache::lonnet::is_course($env{'form.courseid'}); + + unless ($courseid) { + &page_header($r,$env{'form.courseid'},$js); $r->print('

'.&mt('Self-enrollment error').'

'. ''. &mt('Invalid domain or course number').''); @@ -79,20 +82,49 @@ sub handler { } my ($canenroll,$selfenroll_types,$selfenroll_registered,@cancreate, $knownuser,$selfenroll_access_start,$selfenroll_access_end, - $selfenroll_section,$selfenroll_future,%curr_role,$cdomdesc); + $selfenroll_section,$selfenroll_future,%curr_role,$cdomdesc, + $selfenroll_approval,$selfenroll_limit,$selfenroll_cap, + $selfenroll_notifylist,$owner,$crstype); $selfenroll_types = $coursehash{'internal.selfenroll_types'}; $selfenroll_registered = $coursehash{'internal.selfenroll_registered'}; $selfenroll_section = $coursehash{'internal.selfenroll_section'}; $selfenroll_access_start = $coursehash{'internal.selfenroll_start_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 '') { my $start = $coursehash{'internal.selfenroll_start_date'}; my $end = $coursehash{'internal.selfenroll_end_date'}; 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)) { 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); + } } } } @@ -101,6 +133,14 @@ sub handler { $r->print('

'.&mt('Self-enrollment unavailable').'

'. &mt('Self-enrollment is not currently available for this course.'). '

'); + 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_future ne '') { if ($selfenroll_types eq '*') { @@ -181,7 +221,8 @@ sub handler { if ($sso_url eq '') { $sso_url = $login_path; } - $missing_formitem = &mt('The link to the requested page could not be followed.')."\\n".&mt('The placeholder for the courseID is absent.'); + $missing_formitem = &mt('The link to the requested page could not be followed.')."\n".&mt('The placeholder for the courseID is absent.'); + &js_escape(\$missing_formitem); if ($knownuser) { if (keys(%curr_role)) { $r->print('

'.&mt('Self-enrollment unavailable').'

'. @@ -191,7 +232,8 @@ sub handler { } &process_self_enroll($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered, $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') { my $submit_text = &mt('Log in'); $r->print('

'.&mt('Log-in to LON-CAPA').'

'); @@ -283,11 +325,32 @@ END 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 { my ($r,$courseid,$js,$desc) = @_; my $start_page = - &Apache::loncommon::start_page('Self-enroll in a LON-CAPA course',$js, - {'no_inline_link' => 1,}); + &Apache::loncommon::start_page('Self-enroll in a LON-CAPA course',$js); $r->print($start_page); &Apache::lonhtmlcommon::clear_breadcrumbs(); &Apache::createaccount::selfenroll_crumbs($r,$courseid,$desc); @@ -303,17 +366,6 @@ sub page_footer { return; } -sub validate_course_id { - my ($courseid) = @_; - my ($cdom,$cnum) = ($env{'form.courseid'} =~ /^($match_domain)_($match_courseid)$/); - if ($cdom ne '' && $cnum ne '') { - if (&Apache::lonnet::is_course($cdom,$cnum)) { - return ('ok',$courseid); - } - } - return; -} - sub user_is_known { my $known = 0; if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public' @@ -349,7 +401,7 @@ sub has_role { my $output; if ($curr_role{'status'} eq 'active') { 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'}).'
'.$rolelink; + $output = &mt('You already have an active student role (section: "[_1]") in this course.',$curr_role{'section'}).'
'.$rolelink; } 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'}); } @@ -358,7 +410,8 @@ sub has_role { sub process_self_enroll { 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 $uname = $env{'user.name'}; my $selfenroll = 0; @@ -385,11 +438,14 @@ sub process_self_enroll { if ($selfenroll_section eq 'none') { $usec = ''; } + my $instcid; if ($selfenroll_registered) { - my ($registered,$instsec,$message) = &check_registered($cdom,$cnum); + my ($registered,$instsec,$message); + my ($registered,$instsec,$instcid,$message) = &check_registered($cdom,$cnum); $usec = $instsec; if (!$registered) { - $r->print('

'.&mt('Self-enrollment unavailable').'

'.&mt('Self-enrollment is restricted to students officially registered for this course.').'
'); + $r->print('

'.&mt('Self-enrollment unavailable').'

'. + &mt('Self-enrollment is restricted to students officially registered for this course.').'
'); if ($message) { $r->print($message); } else { @@ -398,48 +454,56 @@ sub process_self_enroll { return; } } - my $enrollresult = - &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef, - undef,undef,$usec,$selfenroll_access_end,$selfenroll_access_start, - 'manual',undef,$cdom.'_'.$cnum,$selfenroll); - if ($enrollresult eq 'ok') { - my (%userroles,%newrole,%newgroups); - my $role = 'st'; - 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); - $userroles{'user.role.'.$spec} = $selfenroll_access_start.'.'.$selfenroll_access_end; - &Apache::lonnet::appenv(\%userroles,[$role,'cm']); - $r->print('

'.&mt('Enrollment process complete').'

'); - 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.').'
'.&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 { - $r->print(&mt('Self-enrollment in this course was successful.').'
'); - my $showstart = &Apache::lonlocal::locallocaltime($selfenroll_access_start); - 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.').'
'.&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_approval) { + my $outcome = + &store_selfenroll_request($udom,$uname,$usec,$cdom,$cnum, + $selfenroll_notifylist,$owner, + $selfenroll_approval,$crstype,$lonhost,$handle); + $r->print($outcome); + } else { + my $enrollresult = + &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef, + undef,undef,$usec,$selfenroll_access_end,$selfenroll_access_start, + 'selfenroll',undef,$cdom.'_'.$cnum,$selfenroll,'selfenroll','',$instcid); + if ($enrollresult eq 'ok') { + my (%userroles,%newrole,%newgroups); + my $role = 'st'; + 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); + $userroles{'user.role.'.$spec} = $selfenroll_access_start.'.'.$selfenroll_access_end; + &Apache::lonnet::appenv(\%userroles,[$role,'cm']); + $r->print('

'.&mt('Enrollment process complete').'

'); + 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.').'
'.&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 { - my $newrole = 'st./'.$cdom.'/'.$cnum; - if ($usec ne '') { - $newrole .= '/'.$usec; + $r->print(&mt('Self-enrollment in this course was successful.').'
'); + my $showstart = &Apache::lonlocal::locallocaltime($selfenroll_access_start); + 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.').'
'.&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).'

'."\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).'

'."\n". - $rolelink); } - } - } else { - $r->print('

'.&mt('Enrollment incomplete').'

'. - &mt('Self-enrollment in this course failed.')); - if ($enrollresult ne '') { - $r->print(''.$enrollresult.''); + } else { + $r->print('

'.&mt('Enrollment incomplete').'

'. + &mt('Self-enrollment in this course failed.')); + if ($enrollresult ne '') { + $r->print(''.$enrollresult.''); + } } } } else { @@ -453,14 +517,12 @@ sub process_self_enroll { sub user_can_selfenroll { my ($udom,$uname,$domenrolltypes) = @_; my $selfenroll = 0; - my @info = ['inststatus']; - my %userhash = &Apache::lonnet::userenvironment($udom,$uname,@info); + my %userhash = &Apache::lonnet::userenvironment($udom,$uname,'inststatus'); my @inststatuses; if ($userhash{'inststatus'} eq '') { push(@inststatuses,'other'); } else { - my @esc_statuses = split(/:/,$userhash{'inststatus'}); - @inststatuses = map { &unescape($_); } (@esc_statuses); + @inststatuses = split(':',$userhash{'inststatus'}); } foreach my $type (@inststatuses) { if (ref($domenrolltypes) eq 'ARRAY') { @@ -473,6 +535,154 @@ sub user_can_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}) { + my $status; + $output = &mt('A self-enrollment request already exists for you for this course.').'
'; + my %info = &Apache::lonnet::get($namespace,[$cdom.'_'.$cnum],$udom,$uname); + if (ref($info{$cdom.'_'.$cnum}) eq 'HASH') { + $status = $info{$cdom.'_'.$cnum}{'status'}; + } + if ($status eq 'pending') { + my $token = $info{$cdom.'_'.$cnum}{'token'}; + my ($statusupdate,$pendingform) = &pending_selfenrollment_form($cdom,$cnum,$crstype,$token,$lonhost); + if ($statusupdate eq 'pending') { + $output .= $pendingform; + } + } else { + $output .= &mt('Your earlier request is in a queue awaiting action by a Course Coordinator.'). + '

'.&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.').'
'; + if ($status eq 'pending') { + my ($statusupdate,$pendingform) = &pending_selfenrollment_form($cdom,$cnum,$crstype,$token,$lonhost); + if ($statusupdate eq 'request') { + $status = $statusupdate; + } else { + $output .= $pendingform; + } + } + 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.').'
'. + &mt('To access your LON-CAPA message, go to the Main Menu and click on "Send and Receive Messages".').'
'; + 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).'
'; + } + if ($warning) { + $output .= ''.$warning.'
'; + } + $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 = ''.&mt('An error occurred when recording your request.').''; + + } + } + return $output; +} + +sub pending_selfenrollment_form { + my ($cdom,$cnum,$crstype,$token,$lonhost) = @_; + my ($status,$output); + 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, + ); + 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 .= '
'."\n"; + foreach my $field (@fields) { + if ($postvalues{$field}) { + $output .= ''."\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 .= ''."\n". + ''."\n". + ''."\n". + '
'."\n"; + $status = 'pending'; + } else { + $status = 'request'; + } + } + } else { + $status = 'request'; + } + return ($status,$output); +} + sub jump_to_role { my ($role) = @_; my $output = <<"END"; @@ -508,12 +718,13 @@ sub get_selfenroll_filters { if ($selfdom =~ /^$match_domain$/) { if (&Apache::lonnet::domain($selfdom) ne '') { my @types = split(/,/,$type_str); + my @unesc_types = map { &unescape($_); } @types; my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($selfdom); if (ref($usertypes) eq 'HASH') { my $anytype = 1; foreach my $key (keys(%{$usertypes})) { - if (!grep(/^\Q$key\E$/,@types)) { + if (!grep(/^\Q$key\E$/,@unesc_types)) { $anytype = 0; last; } @@ -525,13 +736,16 @@ sub get_selfenroll_filters { } $usertypes->{'any'} = &mt('Any users'); $usertypes->{'other'} = &mt('Others'); + my @showtypes; if ($anytype) { @{$enrolltypes{$selfdom}} = ('any'); + @showtypes = ('any'); } else { @{$enrolltypes{$selfdom}} = @types; + @showtypes = @unesc_types; } @{$longtypes{$selfdom}} = - map {$usertypes->{$_}} @{$enrolltypes{$selfdom}} + map {$usertypes->{$_}} @showtypes; } } } @@ -573,7 +787,7 @@ sub print_selfenroll_types { sub check_registered { my ($cdom,$cnum) = @_; - my ($registered,$instsec,$message); + my ($registered,$instsec,$instcid,$message); my %settings = &Apache::lonnet::get('environment',['internal.coursecode', 'internal.sectionnums', 'internal.crosslistings'],$cdom,$cnum); @@ -593,6 +807,7 @@ sub check_registered { if (defined($enrolled{$env{'user.name'}})) { $registered = 1; $instsec = $LC_code{$class}; + $instcid = $class; last; } } @@ -605,7 +820,7 @@ sub check_registered { } else { $message = &mt('As no institutional course sections are currently associated with this course, your registration status is undetermined.'); } - return ($registered,$instsec,$message); + return ($registered,$instsec,$instcid,$message); } 1;