Annotation of loncom/interface/selfenroll.pm, revision 1.5
1.1 raeburn 1: # The LearningOnline Network
2: # Allow users to self-enroll in a course
3: #
1.5 ! bisitz 4: # $Id: selfenroll.pm,v 1.4 2008/06/03 03:07:38 raeburn 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);
283: my @inststatuses = &unescape(split(/:/,$userhash{'inststatus'}));
284: if (@inststatuses == 0) {
285: push(@inststatuses,'other');
286: }
287: my $selfenroll = 0;
288: if ($alldoms) {
289: $selfenroll = 1;
290: } elsif (ref($enrolltypes) eq 'HASH') {
291: if (ref($enrolltypes->{$udom}) eq 'ARRAY') {
292: if (grep(/^any$/,@{$enrolltypes->{$udom}})) {
293: $selfenroll = 1;
294: } else {
295: foreach my $type (@inststatuses) {
296: if (grep(/^\Q$type\E$/,@{$enrolltypes->{$udom}})) {
297: $selfenroll = 1;
298: last;
299: }
300: }
301: }
302: }
303: }
304: if ($selfenroll) {
305: my $usec = $selfenroll_section;
306: if ($selfenroll_section eq 'none') {
307: $usec = '';
308: }
309: if ($selfenroll_registered) {
310: my ($registered,$instsec,$message) = &check_registered($cdom,$cnum);
311: $usec = $instsec;
312: if (!$registered) {
313: $r->print(&mt('Self-enrollment is restricted to officially registered students.').'<br />');
314: if ($message) {
315: $r->print($message);
316: } else {
317: $r->print(&mt('As you are not currently registered for this course, self-enrollment is unavailable.'));
318: }
319: return;
320: }
321: }
322: my $enrollresult =
323: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,
324: undef,undef,$usec,$selfenroll_access_end,$selfenroll_access_start, 'manual',undef,$cdom.'_'.$cnum,$selfenroll);
325: if ($enrollresult eq 'ok') {
326: my (%userroles,%newrole,%newgroups);
327: my $role = 'st';
328: my $area = '/'.$cdom.'/'.$cnum;
329: my $spec = $role.'.'.$area;
330: if ($usec ne '') {
331: $spec .= '/'.$usec;
332: $area .= '/'.$usec;
333: }
334: &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,
335: $area);
336: &Apache::lonnet::set_userprivs(\%userroles,\%newrole,%newgroups);
337: $userroles{'user.role.'.$spec} = $selfenroll_access_start.'.'.$selfenroll_access_end;
1.2 raeburn 338: &Apache::lonnet::appenv(\%userroles,[$role]);
1.1 raeburn 339: if ($selfenroll_access_end && $selfenroll_access_end <= $now) {
340: $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.'));
341: } else {
342: $r->print(&mt('Self-enrollment in this course was successful.').'<br />');
343: my $showstart = &Apache::lonlocal::locallocaltime($selfenroll_access_start);
344: my $showend = &Apache::lonlocal::locallocaltime($selfenroll_access_end);
345: if ($selfenroll_access_start && $selfenroll_access_start >$now) {
346: $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));
347: } else {
348: my $newrole = 'st./'.$cdom.'/'.$cnum;
349: if ($usec ne '') {
350: $newrole .= '/'.$usec;
351: }
352: my $rolelink = &jump_to_role($newrole);
353: $r->print(&mt('Your new role is available immediately, and will provide access to the course until [_1].',$showend).'<br /><br />'."\n".
354: $rolelink);
355: }
356: }
357: } else {
358: $r->print(&mt('Self-enrollment in this course failed.'));
359: if ($enrollresult ne '') {
360: $r->print('<span class="LC_error">'.$enrollresult.'</span>');
361: }
362: }
363: } else {
364: $r->print(&mt('You are not permitted to enroll yourself in this course.').'<br />');
365: $r->print(&print_selfenroll_types($longtypes));
366: }
367: return;
368: }
369:
370: sub jump_to_role {
371: my ($role) = @_;
372: my $output = <<"END";
373: <script type="text/javascript">
374: function SelectRole() {
375: document.rolechoice.selectrole.value = '1';
376: document.rolechoice.submit();
377: }
378: </script>
379: END
380: $output .= '<a href="javascript:SelectRole()">'."\n".
381: &mt('Enter course now').'</a>'."\n".
382: '<form name="rolechoice" action="/adm/roles">'."\n".
383: '<input type="hidden" name="selectrole" value="" />'."\n".
384: '<input type="hidden" name="'.$role.'" value="1" />'."\n".
385: '</form>';
386: return $output;
387: }
388:
389: sub get_selfenroll_filters {
390: my ($selfenroll_types,$domain) = @_;
391: my (%enrolltypes,%longtypes,$alldoms);
392: my @selfenrolldoms = split(/;/,$selfenroll_types);
393: foreach my $item (@selfenrolldoms) {
394: my ($selfdom,$type_str) = split(/:/,$item);
395: if ($selfdom eq '*') {
396: $alldoms = 1;
397: last;
398: }
399: if ($domain ne '') {
400: next if ($selfdom ne $domain);
401: }
402: if ($selfdom =~ /^$match_domain$/) {
403: if (&Apache::lonnet::domain($selfdom) ne '') {
404: my @types = split(/,/,$type_str);
405: my ($othertitle,$usertypes,$types) =
406: &Apache::loncommon::sorted_inst_types($selfdom);
407: if (ref($usertypes) eq 'HASH') {
408: $usertypes->{'any'} = &mt('any');
409: $usertypes->{'other'} = &mt('other');
410: @{$enrolltypes{$selfdom}} = @types;
411: @{$longtypes{$selfdom}} = map {$usertypes->{$_}} @types;
412: }
413: }
414: }
415: }
416: return (\%enrolltypes,\%longtypes,$alldoms);
417: }
418:
419: sub print_selfenroll_types {
420: my ($longtypes,$domain) = @_;
421: my $output;
422: if (ref($longtypes) eq 'HASH') {
423: if (keys(%{$longtypes}) > 0) {
424: if ($domain ne '') {
425: if (ref($longtypes->{$domain}) eq 'ARRAY') {
426: if (grep(/^any$/,@{$longtypes->{$domain}})) {
427: $output = &mt('Self-enrollment in this course is only available to users in domain "[_1]".',$domain);
428: } else {
429: my $status_str = join(', ',@{$longtypes->{$domain}});
430: $output = &mt('Self-enrollment in this course is only available to users in domain "[_1]" who have the following status: "[_2]".',$domain,$status_str);
431: }
432: }
433: } else {
1.5 ! bisitz 434: $output = &mt('Self-enrollment in this course is only available to users in the following domains (with the following status):').' <ul>';
1.1 raeburn 435: foreach my $selfdom (sort(keys(%{$longtypes}))) {
436: if (ref($longtypes->{$selfdom}) eq 'ARRAY') {
437: $output .= '<li>'.$selfdom.': '.join(', ',@{$longtypes->{$selfdom}}).'</li>';
438: }
439: }
440: $output .= '</ul>';
441: }
442: } else {
1.5 ! bisitz 443: $output = &mt('Self-enrollment is not currently available for this course.');
1.1 raeburn 444: }
445: }
446: return $output;
447: }
448:
449: sub check_registered {
450: my ($cdom,$cnum) = @_;
451: my ($registered,$instsec,$message);
452: my %settings = &Apache::lonnet::get('environment',['internal.coursecode',
453: 'internal.sectionnums',
454: 'internal.crosslistings'],$cdom,$cnum);
455: my (@allcourses,%LC_code,%affiliates,%reply);
456: &Apache::loncommon::get_institutional_codes(\%settings,\@allcourses,\%LC_code);
457: if (@allcourses > 0) {
458: @{$affiliates{$cnum}} = @allcourses;
459: my $outcome = &Apache::lonnet::fetch_enrollment_query('updatenow',\%affiliates,\%reply,$cdom,$cnum);
460: if ($outcome eq 'ok') {
461: if ($reply{$cnum} > 0) {
462: foreach my $class (@allcourses) {
463: my %enrolled;
464: my $dir = $Apache::lonnet::perlvar{'lonDaemons'};
465: my %place = &LONCAPA::Enrollment::place_hash();
466: &LONCAPA::Enrollment::parse_classlist($dir,$cdom,$cnum,$class,
467: \%place,$LC_code{$class},\%enrolled);
468: if (defined($enrolled{$env{'user.name'}})) {
469: $registered = 1;
470: $instsec = $LC_code{$class};
471: last;
472: }
473: }
474: } else {
475: $message = &mt('Your registration status could not be verified.');
476: }
477: } else {
478: $message = &mt('Your registration status could not determined, because a problem occurred retrieving data.');
479: }
480: } else {
481: $message = &mt('As no institutional course sections are currently associated with this course, your registration status is undetermined.');
482: }
483: return ($registered,$instsec,$message);
484: }
485:
486: 1;
487:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>