Annotation of loncom/interface/selfenroll.pm, revision 1.6
1.1 raeburn 1: # The LearningOnline Network
2: # Allow users to self-enroll in a course
3: #
1.6 ! raeburn 4: # $Id: selfenroll.pm,v 1.5 2008/06/23 15:52:52 bisitz Exp $
1.1 raeburn 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,
1.3 raeburn 69: @cancreate,$knownuser,$selfenroll_access_start,$selfenroll_access_end,
1.1 raeburn 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: }
1.3 raeburn 125: @cancreate = &can_create($cdom);
1.1 raeburn 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);
1.3 raeburn 143: } elsif (@cancreate > 0) {
1.1 raeburn 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>');
1.3 raeburn 170: if (@cancreate > 1) {
171: $r->print('<li>'.&mt('Otherwise:').'<br />');
172: my $count = 0;
173: foreach my $type ('sso','login','email') {
1.4 raeburn 174: if (($count > 0) && ($count <@cancreate)) {
1.3 raeburn 175: $r->print(' , '.&mt('or').'<br />');
176: }
177: if (grep(/^$type$/,@cancreate)) {
178: if ($type eq 'sso') {
179: $r->print(' '.&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>'));
180: } elsif ($type eq 'login') {
181: $r->print(' '.&mt('if you have an institutional login, [_1]create an account[_2]','<a href="javascript:setPath('."'$create_path','courseid','$cid'".')">','</a>'));
182: } elsif ($type eq 'email') {
183: $r->print(' '.&mt('you can use an e-mail address to [_1]create an account[_2]','<a href="javascript:setPath('."'$create_path','courseid','$cid'".')">','</a>'));
184: }
185: $count ++;
186: }
187: }
188: $r->print('.<br />');
189: } else {
190: if ($cancreate[0] eq 'login') {
191: $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>'));
192: } elsif ($cancreate[0] eq 'email') {
193: $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>'));
194: } elsif ($cancreate[0] eq 'sso') {
195: $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>'));
196: }
1.1 raeburn 197: }
198: if ($selfenroll_types eq '*') {
199: $r->print('<br />'.&mt('You will have an opportunity to self enroll in the course once the account has been created.').'</li></ul>');
200: } else {
201: $r->print('</li></ul>');
202: my ($enrolltypes,$longtypes,$alldoms) =
203: &get_selfenroll_filters($selfenroll_types,$cdom);
204: $r->print(&print_selfenroll_types($longtypes,$cdom));
205: }
206: } else {
207: $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>'));
208: if ($selfenroll_types ne '*') {
209: my ($enrolltypes,$longtypes,$alldoms) =
210: &get_selfenroll_filters($selfenroll_types);
211: $r->print('<br /><br />'.&print_selfenroll_types($longtypes));
212: }
213: }
214: $r->print('<form name ="logmein" method="post" action="">'."\n".
215: '<input type="hidden" name="setting" value=""/>'."\n".
216: '</form>'."\n".
217: &Apache::loncommon::end_page());
218: return OK;
219: }
220:
221: sub validate_course_id {
222: my ($cid) = @_;
223: my ($cdom,$cnum) = ($env{'form.cid'} =~ /^($match_domain)_($match_courseid)$/);
224: if ($cdom ne '' && $cnum ne '') {
225: if (&Apache::lonnet::is_course($cdom,$cnum)) {
226: return ('ok',$cid);
227: }
228: }
229: return;
230: }
231:
232: sub user_is_known {
233: my $known = 0;
234: if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public'
235: && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
236: $known = 1;
237: }
238: return $known;
239: }
240:
241: sub can_create {
242: my ($cdom) = @_;
1.3 raeburn 243: my @cancreate;
1.1 raeburn 244: my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$cdom);
245: if (ref($domconfig{'usercreation'}) eq 'HASH') {
246: if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
247: if ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') {
1.3 raeburn 248: if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
249: @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
250: } else {
251: if (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') &&
252: ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
253: @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
254: }
255: }
1.1 raeburn 256: }
257: }
258: }
1.3 raeburn 259: return @cancreate;
1.1 raeburn 260: }
261:
262: sub has_role {
263: my (%curr_role) = @_;
264: my $output;
265: if ($curr_role{'status'} eq 'active') {
266: my $rolelink = &jump_to_role($curr_role{'role'});
267: $output = &mt('You currently have an active role (section: "[_1]") in this course.',$curr_role{'section'}).' '.$rolelink;
268: } elsif ($curr_role{'status'} eq 'future') {
269: $output = &mt('You have a role (section: "[_1]") in this course which will become active [_2].',$curr_role{'section'},$curr_role{'start'});
270: }
271: return $output;
272: }
273:
274: sub process_self_enroll {
275: my ($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,
276: $selfenroll_access_start,$selfenroll_access_end,$selfenroll_section,$now) = @_;
277: my $udom = $env{'user.domain'};
278: my $uname = $env{'user.name'};
279: my @info = ['inststatus'];
280: my %userhash = &Apache::lonnet::userenvironment($udom,$uname,@info);
281: my ($enrolltypes,$longtypes,$alldoms) =
282: &get_selfenroll_filters($selfenroll_types,$udom);
1.6 ! raeburn 283: my @inststatuses;
! 284: if ($userhash{'inststatus'} eq '') {
! 285: push(@inststatuses,'other');
! 286: } else {
! 287: my @esc_statuses = split(/:/,$userhash{'inststatus'});
! 288: @inststatuses = map { &unescape($_); } (@esc_statuses);
1.1 raeburn 289: }
290: my $selfenroll = 0;
291: if ($alldoms) {
292: $selfenroll = 1;
293: } elsif (ref($enrolltypes) eq 'HASH') {
294: if (ref($enrolltypes->{$udom}) eq 'ARRAY') {
295: if (grep(/^any$/,@{$enrolltypes->{$udom}})) {
296: $selfenroll = 1;
297: } else {
298: foreach my $type (@inststatuses) {
299: if (grep(/^\Q$type\E$/,@{$enrolltypes->{$udom}})) {
300: $selfenroll = 1;
301: last;
302: }
303: }
304: }
305: }
306: }
307: if ($selfenroll) {
308: my $usec = $selfenroll_section;
309: if ($selfenroll_section eq 'none') {
310: $usec = '';
311: }
312: if ($selfenroll_registered) {
313: my ($registered,$instsec,$message) = &check_registered($cdom,$cnum);
314: $usec = $instsec;
315: if (!$registered) {
1.6 ! raeburn 316: $r->print(&mt('Self-enrollment is restricted to students officially registered for this course.').'<br />');
1.1 raeburn 317: if ($message) {
318: $r->print($message);
319: } else {
320: $r->print(&mt('As you are not currently registered for this course, self-enrollment is unavailable.'));
321: }
322: return;
323: }
324: }
325: my $enrollresult =
326: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,
1.6 ! raeburn 327: undef,undef,$usec,$selfenroll_access_end,$selfenroll_access_start,
! 328: 'manual',undef,$cdom.'_'.$cnum,$selfenroll);
1.1 raeburn 329: if ($enrollresult eq 'ok') {
330: my (%userroles,%newrole,%newgroups);
331: my $role = 'st';
332: my $area = '/'.$cdom.'/'.$cnum;
333: my $spec = $role.'.'.$area;
334: if ($usec ne '') {
335: $spec .= '/'.$usec;
336: $area .= '/'.$usec;
337: }
338: &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,
339: $area);
340: &Apache::lonnet::set_userprivs(\%userroles,\%newrole,%newgroups);
341: $userroles{'user.role.'.$spec} = $selfenroll_access_start.'.'.$selfenroll_access_end;
1.2 raeburn 342: &Apache::lonnet::appenv(\%userroles,[$role]);
1.1 raeburn 343: if ($selfenroll_access_end && $selfenroll_access_end <= $now) {
344: $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.'));
345: } else {
346: $r->print(&mt('Self-enrollment in this course was successful.').'<br />');
347: my $showstart = &Apache::lonlocal::locallocaltime($selfenroll_access_start);
348: my $showend = &Apache::lonlocal::locallocaltime($selfenroll_access_end);
349: if ($selfenroll_access_start && $selfenroll_access_start >$now) {
350: $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));
351: } else {
352: my $newrole = 'st./'.$cdom.'/'.$cnum;
353: if ($usec ne '') {
354: $newrole .= '/'.$usec;
355: }
356: my $rolelink = &jump_to_role($newrole);
357: $r->print(&mt('Your new role is available immediately, and will provide access to the course until [_1].',$showend).'<br /><br />'."\n".
358: $rolelink);
359: }
360: }
361: } else {
362: $r->print(&mt('Self-enrollment in this course failed.'));
363: if ($enrollresult ne '') {
364: $r->print('<span class="LC_error">'.$enrollresult.'</span>');
365: }
366: }
367: } else {
368: $r->print(&mt('You are not permitted to enroll yourself in this course.').'<br />');
369: $r->print(&print_selfenroll_types($longtypes));
370: }
371: return;
372: }
373:
374: sub jump_to_role {
375: my ($role) = @_;
376: my $output = <<"END";
377: <script type="text/javascript">
378: function SelectRole() {
379: document.rolechoice.selectrole.value = '1';
380: document.rolechoice.submit();
381: }
382: </script>
383: END
384: $output .= '<a href="javascript:SelectRole()">'."\n".
385: &mt('Enter course now').'</a>'."\n".
386: '<form name="rolechoice" action="/adm/roles">'."\n".
387: '<input type="hidden" name="selectrole" value="" />'."\n".
388: '<input type="hidden" name="'.$role.'" value="1" />'."\n".
389: '</form>';
390: return $output;
391: }
392:
393: sub get_selfenroll_filters {
394: my ($selfenroll_types,$domain) = @_;
395: my (%enrolltypes,%longtypes,$alldoms);
396: my @selfenrolldoms = split(/;/,$selfenroll_types);
397: foreach my $item (@selfenrolldoms) {
398: my ($selfdom,$type_str) = split(/:/,$item);
399: if ($selfdom eq '*') {
400: $alldoms = 1;
401: last;
402: }
403: if ($domain ne '') {
404: next if ($selfdom ne $domain);
405: }
406: if ($selfdom =~ /^$match_domain$/) {
407: if (&Apache::lonnet::domain($selfdom) ne '') {
408: my @types = split(/,/,$type_str);
409: my ($othertitle,$usertypes,$types) =
410: &Apache::loncommon::sorted_inst_types($selfdom);
411: if (ref($usertypes) eq 'HASH') {
412: $usertypes->{'any'} = &mt('any');
413: $usertypes->{'other'} = &mt('other');
414: @{$enrolltypes{$selfdom}} = @types;
415: @{$longtypes{$selfdom}} = map {$usertypes->{$_}} @types;
416: }
417: }
418: }
419: }
420: return (\%enrolltypes,\%longtypes,$alldoms);
421: }
422:
423: sub print_selfenroll_types {
424: my ($longtypes,$domain) = @_;
425: my $output;
426: if (ref($longtypes) eq 'HASH') {
427: if (keys(%{$longtypes}) > 0) {
428: if ($domain ne '') {
429: if (ref($longtypes->{$domain}) eq 'ARRAY') {
430: if (grep(/^any$/,@{$longtypes->{$domain}})) {
431: $output = &mt('Self-enrollment in this course is only available to users in domain "[_1]".',$domain);
432: } else {
433: my $status_str = join(', ',@{$longtypes->{$domain}});
434: $output = &mt('Self-enrollment in this course is only available to users in domain "[_1]" who have the following status: "[_2]".',$domain,$status_str);
435: }
436: }
437: } else {
1.5 bisitz 438: $output = &mt('Self-enrollment in this course is only available to users in the following domains (with the following status):').' <ul>';
1.1 raeburn 439: foreach my $selfdom (sort(keys(%{$longtypes}))) {
440: if (ref($longtypes->{$selfdom}) eq 'ARRAY') {
441: $output .= '<li>'.$selfdom.': '.join(', ',@{$longtypes->{$selfdom}}).'</li>';
442: }
443: }
444: $output .= '</ul>';
445: }
446: } else {
1.5 bisitz 447: $output = &mt('Self-enrollment is not currently available for this course.');
1.1 raeburn 448: }
449: }
450: return $output;
451: }
452:
453: sub check_registered {
454: my ($cdom,$cnum) = @_;
455: my ($registered,$instsec,$message);
456: my %settings = &Apache::lonnet::get('environment',['internal.coursecode',
457: 'internal.sectionnums',
458: 'internal.crosslistings'],$cdom,$cnum);
459: my (@allcourses,%LC_code,%affiliates,%reply);
460: &Apache::loncommon::get_institutional_codes(\%settings,\@allcourses,\%LC_code);
461: if (@allcourses > 0) {
462: @{$affiliates{$cnum}} = @allcourses;
463: my $outcome = &Apache::lonnet::fetch_enrollment_query('updatenow',\%affiliates,\%reply,$cdom,$cnum);
464: if ($outcome eq 'ok') {
465: if ($reply{$cnum} > 0) {
466: foreach my $class (@allcourses) {
467: my %enrolled;
468: my $dir = $Apache::lonnet::perlvar{'lonDaemons'};
469: my %place = &LONCAPA::Enrollment::place_hash();
470: &LONCAPA::Enrollment::parse_classlist($dir,$cdom,$cnum,$class,
471: \%place,$LC_code{$class},\%enrolled);
472: if (defined($enrolled{$env{'user.name'}})) {
473: $registered = 1;
474: $instsec = $LC_code{$class};
475: last;
476: }
477: }
478: } else {
479: $message = &mt('Your registration status could not be verified.');
480: }
481: } else {
482: $message = &mt('Your registration status could not determined, because a problem occurred retrieving data.');
483: }
484: } else {
485: $message = &mt('As no institutional course sections are currently associated with this course, your registration status is undetermined.');
486: }
487: return ($registered,$instsec,$message);
488: }
489:
490: 1;
491:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>