[LON-CAPA-cvs] cvs: loncom /auth lonroles.pm /interface createaccount.pm selfenroll.pm /lonnet/perl lonnet.pm

droeschl droeschl at source.lon-capa.org
Wed May 16 17:19:45 EDT 2012


droeschl		Wed May 16 21:19:45 2012 EDT

  Modified files:              
    /loncom/lonnet/perl	lonnet.pm 
    /loncom/interface	selfenroll.pm createaccount.pm 
    /loncom/auth	lonroles.pm 
  Log:
  Replaced calls to selfenroll::validate_course_id and createaccount::validate_course by lonnet::is_course.
  Extended functionality of lonnet::is_course.
  Code cleanup.
  
  
-------------- next part --------------
Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.1166 loncom/lonnet/perl/lonnet.pm:1.1167
--- loncom/lonnet/perl/lonnet.pm:1.1166	Wed Apr 25 21:22:15 2012
+++ loncom/lonnet/perl/lonnet.pm	Wed May 16 21:19:33 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.1166 2012/04/25 21:22:15 raeburn Exp $
+# $Id: lonnet.pm,v 1.1167 2012/05/16 21:19:33 droeschl Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -97,6 +97,7 @@
 use LONCAPA qw(:DEFAULT :match);
 use LONCAPA::Configuration;
 use LONCAPA::lonmetadata;
+use LONCAPA::Lond;
 
 use File::Copy;
 
@@ -5071,6 +5072,7 @@
     if (!$udomain) { $udomain=$env{'user.domain'}; }
     if (!$uname) { $uname=$env{'user.name'}; }
     my $uhome=&homeserver($uname,$udomain);
+
     if ($regexp) {
 	$regexp=&escape($regexp);
     } else {
@@ -8099,13 +8101,16 @@
 }
 
 sub is_course {
-    my ($cdom,$cnum) = @_;
-    my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
-				undef,'.');
-    if (exists($courses{$cdom.'_'.$cnum})) {
-        return 1;
-    }
-    return 0;
+    my ($cdom, $cnum) = scalar(@_) == 1 ? 
+         ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
+
+    return unless $cdom and $cnum;
+
+    my %courses = &courseiddump($cdom, '.', 1, '.', '.', $cnum, undef, undef,
+        '.');
+
+    return unless exists($courses{$cdom.'_'.$cnum});
+    return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
 }
 
 sub store_userdata {
@@ -11976,6 +11981,19 @@
 
 generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
 
+=item *
+
+is_course($courseid), is_course($cdom, $cnum)
+
+Accepts either a combined $courseid (in the form of domain_courseid) or the
+two component version $cdom, $cnum. It checks if the specified course exists.
+
+Returns:
+    undef if the course doesn't exist, otherwise
+    in scalar context the combined courseid.
+    in list context the two components of the course identifier, domain and 
+    courseid.    
+
 =back
 
 =head2 Resource Subroutines
Index: loncom/interface/selfenroll.pm
diff -u loncom/interface/selfenroll.pm:1.26 loncom/interface/selfenroll.pm:1.27
--- loncom/interface/selfenroll.pm:1.26	Mon Mar 22 20:11:22 2010
+++ loncom/interface/selfenroll.pm	Wed May 16 21:19:39 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Allow users to self-enroll in a course
 #
-# $Id: selfenroll.pm,v 1.26 2010/03/22 20:11:22 droeschl Exp $
+# $Id: selfenroll.pm,v 1.27 2012/05/16 21:19:39 droeschl Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -54,9 +54,11 @@
     &Apache::lonlocal::get_language_handle($r);
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['courseid']);
     my $js = &Apache::createaccount::catreturn_js();
-    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('<h3>'.&mt('Self-enrollment error').'</h3>'.
                   '<span class="LC_error">'.
                   &mt('Invalid domain or course number').'</span>');
@@ -358,17 +360,6 @@
     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'
Index: loncom/interface/createaccount.pm
diff -u loncom/interface/createaccount.pm:1.47 loncom/interface/createaccount.pm:1.48
--- loncom/interface/createaccount.pm:1.47	Thu Sep 29 13:34:32 2011
+++ loncom/interface/createaccount.pm	Wed May 16 21:19:39 2012
@@ -3,7 +3,7 @@
 # institutional log-in ID (institutional authentication required - localauth
 #  or kerberos) or an e-mail address.
 #
-# $Id: createaccount.pm,v 1.47 2011/09/29 13:34:32 raeburn Exp $
+# $Id: createaccount.pm,v 1.48 2012/05/16 21:19:39 droeschl Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -45,6 +45,8 @@
 use LONCAPA qw(:DEFAULT :match);
 use HTML::Entities;
 
+#TODO this module needs documentation
+
 sub handler {
     my $r = shift;
     &Apache::loncommon::content_type($r,'text/html');
@@ -65,14 +67,8 @@
     if ($sso_username ne '' && $sso_domain ne '') {
         $domain = $sso_domain; 
     } else {
-        $domain = &Apache::lonnet::default_login_domain();
-        if (defined($env{'form.courseid'})) {
-            if (&validate_course($env{'form.courseid'})) {
-                if ($env{'form.courseid'} =~ /^($match_domain)_($match_courseid)$/) {
-                    $domain = $1; 
-                }
-            }
-        }
+        ($domain, undef) = Apache::lonnet::is_course($env{'form.courseid'}); 
+        $domain ||= &Apache::lonnet::default_login_domain();
     }
     my $domdesc = &Apache::lonnet::domain($domain,'description');
     my $contact_name = &mt('LON-CAPA helpdesk');
@@ -98,9 +94,7 @@
     }
 
     my ($js,$courseid,$title);
-    if (defined($env{'form.courseid'})) {
-        $courseid = &validate_course($env{'form.courseid'});
-    }
+    $courseid = Apache::lonnet::is_course($env{'form.courseid'});
     if ($courseid ne '') {
         $js = &catreturn_js();
         $title = 'Self-enroll in a LON-CAPA course';
@@ -295,17 +289,6 @@
     return;
 }
 
-sub validate_course {
-    my ($courseid) = @_;
-    my ($cdom,$cnum) = ($courseid =~ /^($match_domain)_($match_courseid)$/);
-    if (($cdom ne '') && ($cnum ne '')) {
-        if (&Apache::lonnet::is_course($cdom,$cnum)) {
-            return ($courseid);
-        }
-    }
-    return;
-}
-
 sub javascript_setforms {
     my ($now) =  @_;
     my $js = <<ENDSCRIPT;
@@ -652,9 +635,8 @@
                     } else {
                         $msg .= &mt('An error occurred when sending e-mail to [_1] confirming creation of your LON-CAPA account.',$data{'username'});
                     }
-                    my %form = &start_session($r,$data{'username'},$domain, 
-                                              $lonhost,$data{'courseid'},
-                                              $token);
+                    &start_session($r, $data{'username'}, $domain, $lonhost,
+                        $data{'courseid'}, $token);
                     $nostart = 1;
                     $noend = 1;
                 } else {
@@ -681,31 +663,24 @@
 }
 
 sub start_session {
-    my ($r,$username,$domain,$lonhost,$courseid,$token) = @_;
-    my %form = (
-                uname => $username,
-                udom  => $domain,
-               );
-    my $firsturl = '/adm/roles';
-    if (defined($courseid)) {
-        $courseid = &validate_course($courseid);
-        if ($courseid ne '') {
-            $form{'courseid'} = $courseid;
-            $firsturl = '/adm/selfenroll?courseid='.$courseid;
-        }
-    }
+    my ($r, $username, $domain, $lonhost, $courseid, $token) = @_;
+
     if ($r->dir_config('lonBalancer') eq 'yes') {
-        &Apache::lonauth::success($r,$form{'uname'},$form{'udom'},
-                                  $lonhost,'noredirect',undef,\%form);
-        if ($token ne '') { 
-            my $delete = &Apache::lonnet::tmpdel($token);
-        }
+        Apache::lonauth::success($r, $username, $domain, $lonhost,
+            'noredirect', undef, {});
+
+        Apache::lonnet::tmpdel($token) if $token;
+
         $r->internal_redirect('/adm/switchserver');
     } else {
-        &Apache::lonauth::success($r,$form{'uname'},$form{'udom'},
-                                  $lonhost,$firsturl,undef,\%form);
+        $courseid = Apache::lonnet::is_course($courseid); 
+
+        Apache::lonauth::success($r, $username, $domain, $lonhost,
+            ($courseid ? "/adm/selfenroll?courseid=$courseid" : '/adm/roles'),
+            undef, {}); 
     }
-    return %form;
+
+    return;
 }
 
 
@@ -877,7 +852,7 @@
     if ($uhome ne 'no_host') {
         my $result = &Apache::lonnet::authenticate($username,$upass,$domain);
         if ($result ne 'no_host') { 
-            my %form = &start_session($r,$username,$domain,$lonhost,$courseid);
+            &start_session($r, $username, $domain, $lonhost, $courseid);
             $output = '<br /><br />'.&mt('A LON-CAPA account already exists for username [_1] at this institution ([_2]).','<tt>'.$username.'</tt>',$domdesc).'<br />'.&mt('The password entered was also correct so you have been logged in.');
             return ('existingaccount',$output);
         } else {
@@ -1101,7 +1076,7 @@
         if ($result eq 'ok') {
             my $delete = &Apache::lonnet::tmpdel($env{'form.authtoken'});
             $output = &mt('A LON-CAPA account has been created for username: [_1] in domain: [_2].',$username,$domain);
-            my %form = &start_session($r,$username,$domain,$lonhost,$courseid);
+            &start_session($r, $username, $domain, $lonhost, $courseid);
             my $nostart = 1;
             return ('ok',$output,$nostart);
         } else {
Index: loncom/auth/lonroles.pm
diff -u loncom/auth/lonroles.pm:1.265 loncom/auth/lonroles.pm:1.266
--- loncom/auth/lonroles.pm:1.265	Mon May 14 04:17:16 2012
+++ loncom/auth/lonroles.pm	Wed May 16 21:19:44 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # User Roles Screen
 #
-# $Id: lonroles.pm,v 1.265 2012/05/14 04:17:16 raeburn Exp $
+# $Id: lonroles.pm,v 1.266 2012/05/16 21:19:44 droeschl Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1613,19 +1613,17 @@
     } else {
         $ccrole = 'cc';
     }
-    if ($cdom ne '' && $cnum ne '') {
-        if (&Apache::lonnet::is_course($cdom,$cnum)) {
-            my $envkey = 'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum;
-            if (defined($env{$envkey})) {
-                $is_cc = 1;
-                my ($tstart,$tend)=split(/\./,$env{$envkey});
-                my $limit = $update;
-                if ($env{'request.role'} eq $ccrole.'./'.$cdom.'/'.$cnum) {
-                    $limit = $then;
-                }
-                if ($tstart && $tstart>$refresh) { $is_cc = 0; }
-                if ($tend   && $tend  <$limit) { $is_cc = 0; }
+    if (&Apache::lonnet::is_course($cdom,$cnum)) {
+        my $envkey = 'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum;
+        if (defined($env{$envkey})) {
+            $is_cc = 1;
+            my ($tstart,$tend)=split(/\./,$env{$envkey});
+            my $limit = $update;
+            if ($env{'request.role'} eq $ccrole.'./'.$cdom.'/'.$cnum) {
+                $limit = $then;
             }
+            if ($tstart && $tstart>$refresh) { $is_cc = 0; }
+            if ($tend   && $tend  <$limit) { $is_cc = 0; }
         }
     }
     return $is_cc;


More information about the LON-CAPA-cvs mailing list