Annotation of loncom/interface/selfenroll.pm, revision 1.1
1.1 ! raeburn 1: # The LearningOnline Network
! 2: # Allow users to self-enroll in a course
! 3: #
! 4: # $Id: selfenroll.pm $
! 5: #
! 6: # Copyright Michigan State University Board of Trustees
! 7: #
! 8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
! 9: #
! 10: # LON-CAPA is free software; you can redistribute it and/or modify
! 11: # it under the terms of the GNU General Public License as published by
! 12: # the Free Software Foundation; either version 2 of the License, or
! 13: # (at your option) any later version.
! 14: #
! 15: # LON-CAPA is distributed in the hope that it will be useful,
! 16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
! 17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 18: # GNU General Public License for more details.
! 19: #
! 20: # You should have received a copy of the GNU General Public License
! 21: # along with LON-CAPA; if not, write to the Free Software
! 22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
! 23: #
! 24: # /home/httpd/html/adm/gpl.txt
! 25: #
! 26: # http://www.lon-capa.org/
! 27: #
! 28: #
! 29: package Apache::selfenroll;
! 30:
! 31: use strict;
! 32: use Apache::Constants qw(:common);
! 33: use Apache::lonnet;
! 34: use Apache::loncommon;
! 35: use Apache::lonlocal;
! 36: use LONCAPA qw(:DEFAULT :match);
! 37:
! 38: sub handler {
! 39: my $r = shift;
! 40: &Apache::loncommon::content_type($r,'text/html');
! 41: $r->send_http_header;
! 42: if ($r->header_only) {
! 43: return OK;
! 44: }
! 45: my $handle = &Apache::lonnet::check_for_valid_session($r);
! 46: my $lonidsdir=$r->dir_config('lonIDsDir');
! 47: if ($handle ne '') {
! 48: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
! 49: }
! 50: &Apache::lonacc::get_posted_cgi($r);
! 51: &Apache::lonlocal::get_language_handle($r);
! 52: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['cid']);
! 53: my ($coursechk,$cid) = &validate_course_id($env{'form.cid'});
! 54: my $start_page =
! 55: &Apache::loncommon::start_page('Self-enroll in a LON-CAPA course','',
! 56: {'no_inline_link' => 1,});
! 57: $r->print($start_page);
! 58: &Apache::lonhtmlcommon::clear_breadcrumbs();
! 59: &Apache::lonhtmlcommon::add_breadcrumb
! 60: ({href=>"/adm/selfenroll?cid=$cid",
! 61: text=>"Self-enroll"});
! 62: $r->print(&Apache::lonhtmlcommon::breadcrumbs('Self-enroll in course'));
! 63: if ($coursechk ne 'ok') {
! 64: $r->print(&mt('Invalid domain or course number'));
! 65: $r->print(&Apache::loncommon::end_page());
! 66: return OK;
! 67: }
! 68: my ($cdom,$cnum,$canenroll,$selfenroll_types,$selfenroll_registered,
! 69: $cancreate,$knownuser,$selfenroll_access_start,$selfenroll_access_end,
! 70: $selfenroll_section,%curr_role,%coursehash);
! 71: my $now = time;
! 72: %coursehash = &Apache::lonnet::coursedescription($cid);
! 73: $cdom = $coursehash{'domain'};
! 74: $cnum = $coursehash{'num'};
! 75: $selfenroll_types = $coursehash{'internal.selfenroll_types'};
! 76: $selfenroll_registered = $coursehash{'internal.selfenroll_registered'};
! 77: $selfenroll_section = $coursehash{'internal.selfenroll_section'};
! 78: $selfenroll_access_start = $coursehash{'internal.selfenroll_start_access'};
! 79: $selfenroll_access_end = $coursehash{'internal.selfenroll_end_access'};
! 80: if ($selfenroll_types ne '') {
! 81: my $start = $coursehash{'internal.selfenroll_start_date'};
! 82: my $end = $coursehash{'internal.selfenroll_end_date'};
! 83: if (($start > 0 && $start < $now) && ($end > 0 && $end > $now)) {
! 84: $canenroll = 1;
! 85: }
! 86: }
! 87: if (!$canenroll) {
! 88: $r->print(&mt('Self-enrollment is not currently available for this course.').'<br /><br />');
! 89: }
! 90: $knownuser = &user_is_known();
! 91: if ($knownuser) {
! 92: foreach my $key (keys(%env)) {
! 93: if ($key =~ m-^user\.role\.st\./$cdom/$cnum/?(\w*)$-) {
! 94: my $sec = $1;
! 95: if ($sec eq '') {
! 96: $sec = &mt('none');
! 97: }
! 98: my ($start,$end) = split(/\./,$env{$key});
! 99: my $status = 'active';
! 100: if (($end) && ($end<=$now)) {
! 101: $status = 'previous';
! 102: }
! 103: if (($start) && ($now<$start)) {
! 104: $status = 'future';
! 105: }
! 106: if ($status eq 'active' || $status eq 'future') {
! 107: $curr_role{'status'} = $status;
! 108: $curr_role{'section'} = $sec;
! 109: $curr_role{'start'} = &Apache::lonlocal::locallocaltime($start);
! 110: $curr_role{'role'} = 'st./'.$cdom.'/'.$cnum;
! 111: if ($sec ne '') {
! 112: $curr_role{'role'} .= '/'.$sec;
! 113: }
! 114: }
! 115: }
! 116: }
! 117: }
! 118: if (!$canenroll) {
! 119: if (keys(%curr_role)) {
! 120: $r->print(&has_role(%curr_role));
! 121: }
! 122: $r->print(&Apache::loncommon::end_page());
! 123: return OK;
! 124: }
! 125: $cancreate = &can_create($cdom);
! 126: my ($login_path,$firsturl,$create_path,$sso_url,$missing_formitem);
! 127: $login_path = '/adm/login';
! 128: $firsturl= '/adm/selfenroll?cid='.$cid;
! 129: $create_path = '/adm/createaccount';
! 130: $sso_url = $r->dir_config('lonSSOReloginServer');
! 131: if ($sso_url eq '') {
! 132: $sso_url = $login_path;
! 133: }
! 134: my $missing_formitem = &mt('The link to the requested page could not be followed.')."\\n".&mt('The placeholder for the courseID is absent.');
! 135: if ($knownuser) {
! 136: if (keys(%curr_role)) {
! 137: $r->print(&has_role(%curr_role));
! 138: return OK;
! 139: }
! 140: &process_self_enroll($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,
! 141: $selfenroll_access_start,$selfenroll_access_end,
! 142: $selfenroll_section,$now);
! 143: } elsif ($cancreate) {
! 144: $r->print(<<END);
! 145: <script type="text/javascript">
! 146: function setPath(formaction,item,arg) {
! 147: document.forms.logmein.action = formaction;
! 148: var itemid = getIndexByName("setting");
! 149: if (itemid > -1) {
! 150: document.logmein.elements[itemid].name = item;
! 151: document.logmein.elements[itemid].value = arg;
! 152: document.logmein.submit();
! 153: } else {
! 154: alert("$missing_formitem");
! 155: }
! 156: return;
! 157: }
! 158:
! 159: function getIndexByName(item) {
! 160: for (var i=0;i<document.logmein.elements.length;i++) {
! 161: if (document.logmein.elements[i].name == item) {
! 162: return i;
! 163: }
! 164: }
! 165: return -1;
! 166: }
! 167: </script>
! 168: END
! 169: $r->print(&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('."'$login_path','firsturl','$firsturl'".')">','</a>').'</li>');
! 170: if ($cancreate eq 'any') {
! 171: $r->print('<li>'.&mt('Otherwise [_1]create an account[_2].','<a href="javascript:setPath('."'$create_path','courseid','$cid'".')">','</a>'));
! 172: } elsif ($cancreate eq 'login') {
! 173: $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>'));
! 174: } elsif ($cancreate eq 'email') {
! 175: $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>'));
! 176: } elsif ($cancreate eq 'sso') {
! 177: $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>'));
! 178: }
! 179: if ($selfenroll_types eq '*') {
! 180: $r->print('<br />'.&mt('You will have an opportunity to self enroll in the course once the account has been created.').'</li></ul>');
! 181: } else {
! 182: $r->print('</li></ul>');
! 183: my ($enrolltypes,$longtypes,$alldoms) =
! 184: &get_selfenroll_filters($selfenroll_types,$cdom);
! 185: $r->print(&print_selfenroll_types($longtypes,$cdom));
! 186: }
! 187: } else {
! 188: $r->print(&mt('You must [_1]log-in[_2] to LON-CAPA with an existing account to be able to enroll in this course, as account creation is not permitted when self-enrolling.','<a href="'.$login_path.'">','</a>'));
! 189: if ($selfenroll_types ne '*') {
! 190: my ($enrolltypes,$longtypes,$alldoms) =
! 191: &get_selfenroll_filters($selfenroll_types);
! 192: $r->print('<br /><br />'.&print_selfenroll_types($longtypes));
! 193: }
! 194: }
! 195: $r->print('<form name ="logmein" method="post" action="">'."\n".
! 196: '<input type="hidden" name="setting" value=""/>'."\n".
! 197: '</form>'."\n".
! 198: &Apache::loncommon::end_page());
! 199: return OK;
! 200: }
! 201:
! 202: sub validate_course_id {
! 203: my ($cid) = @_;
! 204: my ($cdom,$cnum) = ($env{'form.cid'} =~ /^($match_domain)_($match_courseid)$/);
! 205: if ($cdom ne '' && $cnum ne '') {
! 206: if (&Apache::lonnet::is_course($cdom,$cnum)) {
! 207: return ('ok',$cid);
! 208: }
! 209: }
! 210: return;
! 211: }
! 212:
! 213: sub user_is_known {
! 214: my $known = 0;
! 215: if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public'
! 216: && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
! 217: $known = 1;
! 218: }
! 219: return $known;
! 220: }
! 221:
! 222: sub can_create {
! 223: my ($cdom) = @_;
! 224: my $cancreate = '';
! 225: my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$cdom);
! 226: if (ref($domconfig{'usercreation'}) eq 'HASH') {
! 227: if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
! 228: if ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') {
! 229: $cancreate = $domconfig{'usercreation'}{'cancreate'}{'selfcreate'};
! 230: }
! 231: }
! 232: }
! 233: if ($cancreate eq 'none') {
! 234: $cancreate = '';
! 235: }
! 236: return $cancreate;
! 237: }
! 238:
! 239: sub has_role {
! 240: my (%curr_role) = @_;
! 241: my $output;
! 242: if ($curr_role{'status'} eq 'active') {
! 243: my $rolelink = &jump_to_role($curr_role{'role'});
! 244: $output = &mt('You currently have an active role (section: "[_1]") in this course.',$curr_role{'section'}).' '.$rolelink;
! 245: } elsif ($curr_role{'status'} eq 'future') {
! 246: $output = &mt('You have a role (section: "[_1]") in this course which will become active [_2].',$curr_role{'section'},$curr_role{'start'});
! 247: }
! 248: return $output;
! 249: }
! 250:
! 251: sub process_self_enroll {
! 252: my ($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,
! 253: $selfenroll_access_start,$selfenroll_access_end,$selfenroll_section,$now) = @_;
! 254: my $udom = $env{'user.domain'};
! 255: my $uname = $env{'user.name'};
! 256: my @info = ['inststatus'];
! 257: my %userhash = &Apache::lonnet::userenvironment($udom,$uname,@info);
! 258: my ($enrolltypes,$longtypes,$alldoms) =
! 259: &get_selfenroll_filters($selfenroll_types,$udom);
! 260: my @inststatuses = &unescape(split(/:/,$userhash{'inststatus'}));
! 261: if (@inststatuses == 0) {
! 262: push(@inststatuses,'other');
! 263: }
! 264: my $selfenroll = 0;
! 265: if ($alldoms) {
! 266: $selfenroll = 1;
! 267: } elsif (ref($enrolltypes) eq 'HASH') {
! 268: if (ref($enrolltypes->{$udom}) eq 'ARRAY') {
! 269: if (grep(/^any$/,@{$enrolltypes->{$udom}})) {
! 270: $selfenroll = 1;
! 271: } else {
! 272: foreach my $type (@inststatuses) {
! 273: if (grep(/^\Q$type\E$/,@{$enrolltypes->{$udom}})) {
! 274: $selfenroll = 1;
! 275: last;
! 276: }
! 277: }
! 278: }
! 279: }
! 280: }
! 281: if ($selfenroll) {
! 282: my $usec = $selfenroll_section;
! 283: if ($selfenroll_section eq 'none') {
! 284: $usec = '';
! 285: }
! 286: if ($selfenroll_registered) {
! 287: my ($registered,$instsec,$message) = &check_registered($cdom,$cnum);
! 288: $usec = $instsec;
! 289: if (!$registered) {
! 290: $r->print(&mt('Self-enrollment is restricted to officially registered students.').'<br />');
! 291: if ($message) {
! 292: $r->print($message);
! 293: } else {
! 294: $r->print(&mt('As you are not currently registered for this course, self-enrollment is unavailable.'));
! 295: }
! 296: return;
! 297: }
! 298: }
! 299: my $enrollresult =
! 300: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,
! 301: undef,undef,$usec,$selfenroll_access_end,$selfenroll_access_start, 'manual',undef,$cdom.'_'.$cnum,$selfenroll);
! 302: if ($enrollresult eq 'ok') {
! 303: my (%userroles,%newrole,%newgroups);
! 304: my $role = 'st';
! 305: my $area = '/'.$cdom.'/'.$cnum;
! 306: my $spec = $role.'.'.$area;
! 307: if ($usec ne '') {
! 308: $spec .= '/'.$usec;
! 309: $area .= '/'.$usec;
! 310: }
! 311: &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,
! 312: $area);
! 313: &Apache::lonnet::set_userprivs(\%userroles,\%newrole,%newgroups);
! 314: $userroles{'user.role.'.$spec} = $selfenroll_access_start.'.'.$selfenroll_access_end;
! 315: &Apache::lonnet::appenv(%userroles);
! 316: if ($selfenroll_access_end && $selfenroll_access_end <= $now) {
! 317: $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.'));
! 318: } else {
! 319: $r->print(&mt('Self-enrollment in this course was successful.').'<br />');
! 320: my $showstart = &Apache::lonlocal::locallocaltime($selfenroll_access_start);
! 321: my $showend = &Apache::lonlocal::locallocaltime($selfenroll_access_end);
! 322: if ($selfenroll_access_start && $selfenroll_access_start >$now) {
! 323: $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));
! 324: } else {
! 325: my $newrole = 'st./'.$cdom.'/'.$cnum;
! 326: if ($usec ne '') {
! 327: $newrole .= '/'.$usec;
! 328: }
! 329: my $rolelink = &jump_to_role($newrole);
! 330: $r->print(&mt('Your new role is available immediately, and will provide access to the course until [_1].',$showend).'<br /><br />'."\n".
! 331: $rolelink);
! 332: }
! 333: }
! 334: } else {
! 335: $r->print(&mt('Self-enrollment in this course failed.'));
! 336: if ($enrollresult ne '') {
! 337: $r->print('<span class="LC_error">'.$enrollresult.'</span>');
! 338: }
! 339: }
! 340: } else {
! 341: $r->print(&mt('You are not permitted to enroll yourself in this course.').'<br />');
! 342: $r->print(&print_selfenroll_types($longtypes));
! 343: }
! 344: return;
! 345: }
! 346:
! 347: sub jump_to_role {
! 348: my ($role) = @_;
! 349: my $output = <<"END";
! 350: <script type="text/javascript">
! 351: function SelectRole() {
! 352: document.rolechoice.selectrole.value = '1';
! 353: document.rolechoice.submit();
! 354: }
! 355: </script>
! 356: END
! 357: $output .= '<a href="javascript:SelectRole()">'."\n".
! 358: &mt('Enter course now').'</a>'."\n".
! 359: '<form name="rolechoice" action="/adm/roles">'."\n".
! 360: '<input type="hidden" name="selectrole" value="" />'."\n".
! 361: '<input type="hidden" name="'.$role.'" value="1" />'."\n".
! 362: '</form>';
! 363: return $output;
! 364: }
! 365:
! 366: sub get_selfenroll_filters {
! 367: my ($selfenroll_types,$domain) = @_;
! 368: my (%enrolltypes,%longtypes,$alldoms);
! 369: my @selfenrolldoms = split(/;/,$selfenroll_types);
! 370: foreach my $item (@selfenrolldoms) {
! 371: my ($selfdom,$type_str) = split(/:/,$item);
! 372: if ($selfdom eq '*') {
! 373: $alldoms = 1;
! 374: last;
! 375: }
! 376: if ($domain ne '') {
! 377: next if ($selfdom ne $domain);
! 378: }
! 379: if ($selfdom =~ /^$match_domain$/) {
! 380: if (&Apache::lonnet::domain($selfdom) ne '') {
! 381: my @types = split(/,/,$type_str);
! 382: my ($othertitle,$usertypes,$types) =
! 383: &Apache::loncommon::sorted_inst_types($selfdom);
! 384: if (ref($usertypes) eq 'HASH') {
! 385: $usertypes->{'any'} = &mt('any');
! 386: $usertypes->{'other'} = &mt('other');
! 387: @{$enrolltypes{$selfdom}} = @types;
! 388: @{$longtypes{$selfdom}} = map {$usertypes->{$_}} @types;
! 389: }
! 390: }
! 391: }
! 392: }
! 393: return (\%enrolltypes,\%longtypes,$alldoms);
! 394: }
! 395:
! 396: sub print_selfenroll_types {
! 397: my ($longtypes,$domain) = @_;
! 398: my $output;
! 399: if (ref($longtypes) eq 'HASH') {
! 400: if (keys(%{$longtypes}) > 0) {
! 401: if ($domain ne '') {
! 402: if (ref($longtypes->{$domain}) eq 'ARRAY') {
! 403: if (grep(/^any$/,@{$longtypes->{$domain}})) {
! 404: $output = &mt('Self-enrollment in this course is only available to users in domain "[_1]".',$domain);
! 405: } else {
! 406: my $status_str = join(', ',@{$longtypes->{$domain}});
! 407: $output = &mt('Self-enrollment in this course is only available to users in domain "[_1]" who have the following status: "[_2]".',$domain,$status_str);
! 408: }
! 409: }
! 410: } else {
! 411: $output = &mt('Self-enrollment in this course is only available to users in the following domains (with the following status): ').'<ul>';
! 412: foreach my $selfdom (sort(keys(%{$longtypes}))) {
! 413: if (ref($longtypes->{$selfdom}) eq 'ARRAY') {
! 414: $output .= '<li>'.$selfdom.': '.join(', ',@{$longtypes->{$selfdom}}).'</li>';
! 415: }
! 416: }
! 417: $output .= '</ul>';
! 418: }
! 419: } else {
! 420: $output = &mt('Self-enrollment is not currently available in this course');
! 421: }
! 422: }
! 423: return $output;
! 424: }
! 425:
! 426: sub check_registered {
! 427: my ($cdom,$cnum) = @_;
! 428: my ($registered,$instsec,$message);
! 429: my %settings = &Apache::lonnet::get('environment',['internal.coursecode',
! 430: 'internal.sectionnums',
! 431: 'internal.crosslistings'],$cdom,$cnum);
! 432: my (@allcourses,%LC_code,%affiliates,%reply);
! 433: &Apache::loncommon::get_institutional_codes(\%settings,\@allcourses,\%LC_code);
! 434: if (@allcourses > 0) {
! 435: @{$affiliates{$cnum}} = @allcourses;
! 436: my $outcome = &Apache::lonnet::fetch_enrollment_query('updatenow',\%affiliates,\%reply,$cdom,$cnum);
! 437: if ($outcome eq 'ok') {
! 438: if ($reply{$cnum} > 0) {
! 439: foreach my $class (@allcourses) {
! 440: my %enrolled;
! 441: my $dir = $Apache::lonnet::perlvar{'lonDaemons'};
! 442: my %place = &LONCAPA::Enrollment::place_hash();
! 443: &LONCAPA::Enrollment::parse_classlist($dir,$cdom,$cnum,$class,
! 444: \%place,$LC_code{$class},\%enrolled);
! 445: if (defined($enrolled{$env{'user.name'}})) {
! 446: $registered = 1;
! 447: $instsec = $LC_code{$class};
! 448: last;
! 449: }
! 450: }
! 451: } else {
! 452: $message = &mt('Your registration status could not be verified.');
! 453: }
! 454: } else {
! 455: $message = &mt('Your registration status could not determined, because a problem occurred retrieving data.');
! 456: }
! 457: } else {
! 458: $message = &mt('As no institutional course sections are currently associated with this course, your registration status is undetermined.');
! 459: }
! 460: return ($registered,$instsec,$message);
! 461: }
! 462:
! 463: 1;
! 464:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>