[LON-CAPA-cvs] cvs: loncom /interface lonrequestcourse.pm

raeburn raeburn@source.lon-capa.org
Mon, 07 Sep 2009 06:30:49 -0000


This is a MIME encoded message

--raeburn1252305049
Content-Type: text/plain

raeburn		Mon Sep  7 06:30:49 2009 EDT

  Modified files:              
    /loncom/interface	lonrequestcourse.pm 
  Log:
  - Side effect of changes to address bug 5595 was that checking of user creation privileges etc. no longer guaranteed to have occurred via mechanisms in lonpickuser.pm.
    - Check for user information for existing users, and if new user for rights to create one - &get_cancreate_status() and &check_newuser_rules() added. 
    - Messages displayed above review table if requested user was excluded.
  - Earlier change (rev 1.27) to multiple forms on main menu page requires different javascript functions to set form elements on this page compred with all other pages - &mainmenu_javascript() provides this.
    - onload call changed for main menu page accordingly.
    - $loaditems included in call to print_main_menu for view action as well as new action.  
  
  
--raeburn1252305049
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20090907063049.txt"

Index: loncom/interface/lonrequestcourse.pm
diff -u loncom/interface/lonrequestcourse.pm:1.29 loncom/interface/lonrequestcourse.pm:1.30
--- loncom/interface/lonrequestcourse.pm:1.29	Sun Sep  6 19:09:54 2009
+++ loncom/interface/lonrequestcourse.pm	Mon Sep  7 06:30:49 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Request a course
 #
-# $Id: lonrequestcourse.pm,v 1.29 2009/09/06 19:09:54 raeburn Exp $
+# $Id: lonrequestcourse.pm,v 1.30 2009/09/07 06:30:49 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -122,6 +122,7 @@
 use Apache::loncommon;
 use Apache::lonlocal;
 use Apache::loncoursequeueadmin;
+use Apache::lonuserutils;
 use LONCAPA qw(:DEFAULT :match);
 
 sub handler {
@@ -262,7 +263,11 @@
         if (($state eq 'courseinfo') && ($env{'form.clonedom'} eq '')) {
             $env{'form.clonedom'} = $dom;
         }
-        $jscript = &Apache::lonhtmlcommon::set_form_elements($elementsref,\%stored);
+        if ($state eq 'crstype') {
+            $jscript = &mainmenu_javascript();
+        } else {
+            $jscript = &Apache::lonhtmlcommon::set_form_elements($elementsref,\%stored);
+        }
     }
 
     if ($state eq 'personnel') {
@@ -291,7 +296,7 @@
         }
     } elsif ($action eq 'view') {
         if ($state eq 'crstype') {
-            &print_main_menu($r,\%can_request,\%states,$dom,$jscript,'',$crumb);
+            &print_main_menu($r,\%can_request,\%states,$dom,$jscript,$loaditems,$crumb);
         } else {
             &request_administration($r,$action,$state,$page,\%states,$dom,$jscript,
                                     $loaditems,$crumb);
@@ -315,6 +320,30 @@
     return OK;
 }
 
+sub mainmenu_javascript {
+    return <<"END";
+function setType(courseForm) {
+    for (var i=0; i<courseForm.crstype.length; i++) {
+        if (courseForm.crstype.options[i].value == "$env{'form.crstype'}") {
+            courseForm.crstype.options[i].selected = true;
+        } else {
+            courseForm.crstype.options[i].selected = false;
+        }
+    }
+}
+
+function setAction(courseForm) {
+    for (var i=0; i<courseForm.action.length; i++) {
+        if (courseForm.action.options[i].value == "$env{'form.action'}") {
+            courseForm.action.options[i].selected = true;
+        } else {
+            courseForm.action.options[i].selected = false;
+        }
+    }
+}
+END
+}
+
 sub get_breadcrumbs {
     my ($dom,$action,$state,$states,$trail) = @_;
     my ($crumb,$newinstcode,$codechk,$checkedcode,$numtitles);
@@ -555,7 +584,11 @@
     my ($action,$state) = @_;
     my %loaditems;
     if (($action eq 'new') || ($action eq 'view')) {
-        $loaditems{'onload'} = 'javascript:setFormElements(document.requestcrs)';
+        if ($state eq 'crstype') {
+            $loaditems{'onload'} = 'javascript:setAction(document.mainmenu_action);javascript:setType(document.mainmenu_coursetype)';
+        } else {
+            $loaditems{'onload'} = 'javascript:setFormElements(document.requestcrs)';
+        }
     }
     return \%loaditems;
 }
@@ -792,7 +825,7 @@
         my $form = '<form method="post" name="'.$formname.'" action="/adm/requestcourse" />';
         if ($state eq 'pick_request') {
             $r->print('<h3>'.&mt('Pending course requests').'</h3><div>'."\n".$form."\n".
-                      &print_request_status($dom).'</form>');
+                      &print_request_status($dom).'</form></div>');
         } elsif ($state eq 'details') {
             my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
             my $origcnum = $env{'form.cnum'};
@@ -934,7 +967,8 @@
                                              );
     $crstype = $env{'form.crstype'};
     $r->print('<form name="'.$formname.'" method="post" action="/adm/requestcourse">');
-    my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
+    my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk,
+        @disallowed);
     if ($crstype eq 'official') {
         if ($env{'form.instcode'} ne '') {
             $instcode = $env{'form.instcode'};
@@ -997,6 +1031,79 @@
     } elsif ($state eq 'personnel') {
         $r->print(&print_personnel_menu($dom,$formname,$crstype,$invalidcrosslist));
     } elsif ($state eq 'review') {
+        my (%alerts,%rulematch,%inst_results,%curr_rules,%got_rules,%disallowmsg);
+        my $now = time;
+        for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
+            my $personname = $env{'form.person_'.$i.'_uname'};
+            my $persondom = $env{'form.person_'.$i.'_dom'};
+            if (($personname =~ /^$match_username$/) && 
+                ($persondom =~ /^$match_domain$/)) {
+                if (&Apache::lonnet::domain($persondom)) {
+                    my $personhome = 
+                        &Apache::lonnet::homeserver($personname,$persondom);
+                    if ($personhome eq 'no_host') {
+                        if ($persondom ne $dom) {
+                            my $skipuser = 1;
+                            if ($env{'user.role.dc./'.$persondom.'/'}) {
+                                my ($start,$end) = split('.',$env{'user.role.dc./'.$persondom.'/'});
+                                if (((!$start) || ($start < $now)) && 
+                                    ((!$end) || ($end > $now))) {
+                                    $skipuser = 0;
+                                }
+                            }
+                            if ($skipuser) {
+                                push(@disallowed,$i);
+                                $disallowmsg{$i} = &mt('[_1] was excluded because new users need be from the course domain','<tt>'.$personname.':'.$persondom.'</tt>');
+                                next;
+                            }
+                        }
+                        if (&get_cancreate_status($persondom,$personname,$dom)) {
+                            my ($allowed,$msg) = 
+                                &check_newuser_rules($persondom,$personname,
+                                    \%alerts,\%rulematch,\%inst_results,
+                                    \%curr_rules,\%got_rules);
+                            if ($allowed) {
+                                if (ref($inst_results{$personname.':'.$persondom}) eq 'HASH') {
+                                    if ($inst_results{$personname.':'.$persondom}{'lastname'} ne '') {
+                                        $env{'form.person_'.$i.'_lastname'} = $inst_results{$personname.':'.$persondom}{'lastname'};
+                                    }
+                                    if ($inst_results{$personname.':'.$persondom}{'firstname'} ne '') {
+                                        $env{'form.person_'.$i.'_firstname'} = $inst_results{$personname.':'.$persondom}{'firstname'};
+                                    }
+                                    if ($inst_results{$personname.':'.$persondom}{'permanentemail'} ne '') {
+                                        $env{'form.person_'.$i.'_emailaddr'} = $inst_results{$personname.':'.$persondom}{'permanentemail'};
+                                    }
+                                }
+                            } else {
+                                push(@disallowed,$i);
+                                $disallowmsg{$i} = &mt('[_1] was excluded because the username violated format rules for the domain','<tt>'.$personname.':'.$persondom.'</tt>');  
+                            }
+                        } else {
+                            push(@disallowed,$i);
+                            $disallowmsg{$i} = &mt('[_1] was excluded because you may not request new users in the domain','<tt>'.$personname.':'.$persondom.'</tt>');
+                        }
+                    } else {
+                        my %userenv = 
+                            &Apache::lonnet::userenvironment($persondom,$personname,'lastname','firstname','permanentemail');
+                        if ($env{'form.person_'.$i.'_lastname'} eq '') {
+                            $env{'form.person_'.$i.'_lastname'} = $userenv{'lastname'};
+                        }
+                        if ($env{'form.person_'.$i.'_firstname'} eq '') {
+                            $env{'form.person_'.$i.'_firstname'} = $userenv{'firstname'};
+                        }
+                        if ($env{'form.person_'.$i.'_emailaddr'} eq '') {
+                            $env{'form.person_'.$i.'_emailaddr'} = $userenv{'permanentemail'};
+                        }
+                    }
+                } elsif ($personname ne '') {
+                    push(@disallowed,$i);
+                    $disallowmsg{$i} = &mt('[_1] was excluded because the domain is invalid','<tt>'.$personname.':'.$persondom.'</tt>');
+                }
+            } elsif ($personname ne '') {
+                push(@disallowed,$i);
+                $disallowmsg{$i} = &mt('[_1] was excluded because the username or domain is invalid.','<tt>'.$personname.':'.$persondom.'</tt>');
+            }
+        }
         my $cnum;
         if ($env{'form.origcnum'} =~ /^($match_courseid)$/) {
             $cnum = $env{'form.origcnum'};
@@ -1006,7 +1113,7 @@
         &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                                                  \%cat_order,\@code_order);
         $r->print('<h3>'.&mt('Review course request details before submission').'</h3>'.
-                  &print_review($dom,\@codetitles,\%cat_titles,\%cat_order,\@code_order).
+                  &print_review($dom,\@codetitles,\%cat_titles,\%cat_order,\@code_order,'','',\@disallowed,\%disallowmsg).
                   '<input type="hidden" name="cnum" value="'.$cnum.'" />');
         $navtxt{'next'} = &mt('Submit course request');
     }  elsif ($state eq 'process') {
@@ -1031,6 +1138,32 @@
     if ($state eq 'personnel') {
         push(@excluded,'persontotal');
     }
+    if ($state eq 'review') {
+        if (@disallowed > 0) {
+            my @items = qw(uname dom lastname firstname emailaddr hidedom role newsec);
+            my @currsecs = &current_lc_sections();
+            if (@currsecs) {
+                push(@items,'sec');
+            }
+            my $count = 0;
+            for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
+                unless ($env{'form.person_'.$i.'_uname'} eq '') {
+                    if (grep(/^$i$/,@disallowed)) {
+                        foreach my $item (@items) {
+                            $env{'form.person_'.$i.'_'.$item} = '';
+                        }
+                    } else { 
+                        foreach my $item (@items) {
+                            $env{'form.person_'.$count.'_'.$item} = $env{'form.person_'.$i.'_'.$item};
+                        }
+                    }
+                }
+                $count ++;
+            }
+            $env{'form.persontotal'} = $count;
+             
+        }
+    }
     if ($state eq 'enrollment') {
         push(@excluded,'crosslisttotal');
     }
@@ -1039,6 +1172,45 @@
     return;
 }
 
+sub get_cancreate_status {
+    my ($persondom,$personname,$dom) = @_;
+    my ($rules,$ruleorder) =
+        &Apache::lonnet::inst_userrules($persondom,'username');
+    my $usertype = &Apache::lonuserutils::check_usertype($persondom,$personname,
+                                                         $rules);
+    return &Apache::lonuserutils::can_create_user($dom,'requestcrs',$usertype);
+}
+
+sub check_newuser_rules {
+    my ($persondom,$personname,$alerts,$rulematch,$inst_results,$curr_rules,
+        $got_rules) = @_;
+    my $allowed = 1;
+    my $newuser = 1;
+    my ($checkhash,$userchkmsg);
+    my $checks = { 'username' => 1 };
+    $checkhash->{$personname.':'.$persondom} = { 'newuser' => $newuser };
+    &Apache::loncommon::user_rule_check($checkhash,$checks,$alerts,$rulematch,
+                                        $inst_results,$curr_rules,$got_rules);
+    if (ref($alerts->{'username'}) eq 'HASH') {
+        if (ref($alerts->{'username'}{$persondom}) eq 'HASH') {
+            my $domdesc =
+                &Apache::lonnet::domain($persondom,'description');
+            if ($alerts->{'username'}{$persondom}{$personname}) {
+                if (ref($curr_rules->{$persondom}) eq 'HASH') {
+                    $userchkmsg =
+                        &Apache::loncommon::instrule_disallow_msg('username',
+                                                                  $domdesc,1).
+                        &Apache::loncommon::user_rule_formats($persondom,
+                            $domdesc,$curr_rules->{$persondom}{'username'},
+                            'username');
+                }
+                $allowed = 0;
+            }
+        }
+    }
+    return ($allowed,$userchkmsg);
+}
+
 sub get_excluded_elements {
     my ($dom,$states,$action,$state) = @_;
     my @excluded = ('counter');
@@ -1272,17 +1444,7 @@
         }
     }
 
-    my @currsecs;
-    if ($env{'form.sectotal'}) {
-        for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
-            if (defined($env{'form.loncapasec_'.$i})) {
-                my $lcsec = $env{'form.loncapasec_'.$i};
-                unless (grep(/^\Q$lcsec\E$/,@currsecs)) {
-                    push(@currsecs,$lcsec);
-                }
-            }
-        }
-    }
+    my @currsecs = &current_lc_sections();
 
     my ($existtitle,$existops,$existmult,$newtitle,$seccolspan);
     if (@currsecs) {
@@ -1360,6 +1522,21 @@
     return $output;
 }
 
+sub current_lc_sections {
+    my @currsecs;
+    if ($env{'form.sectotal'}) {
+        for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
+            if (defined($env{'form.loncapasec_'.$i})) {
+                my $lcsec = $env{'form.loncapasec_'.$i};
+                unless (grep(/^\Q$lcsec\E$/,@currsecs)) {
+                    push(@currsecs,$lcsec);
+                }
+            }
+        }
+    }
+    return @currsecs;
+}
+
 sub print_request_status {
     my ($dom) = @_;
     my %statusinfo = &Apache::lonnet::dump('courserequests',$env{'user.domain'},
@@ -1468,8 +1645,7 @@
         $output .= '<div>'.&mt('You have no matching course requests awaiting approval by a Domain Coordinator or held in a queue pending administrative action at your institution.').'</div>';
     }
     $output .= '
-<br /><input type="button" name="prev" value="'.&mt('Back').'" onclick="javascript:backPage(document.'.$formname.",'crstype'".')" />
-</form></div>';
+<br /><input type="button" name="prev" value="'.&mt('Back').'" onclick="javascript:backPage(document.'.$formname.",'crstype'".')" />';
     return $output;
 }
 
@@ -1575,7 +1751,8 @@
 }
 
 sub print_review {
-    my ($dom,$codetitles,$cat_titles,$cat_order,$code_order,$uname,$udom) = @_;
+    my ($dom,$codetitles,$cat_titles,$cat_order,$code_order,$uname,$udom,
+        $disallowed,$disallowmsg) = @_;
     my ($types,$typename) = &course_types();
     my ($owner,$ownername,$owneremail);
     if ($uname eq '' || $udom eq '') {
@@ -1753,6 +1930,9 @@
                          '<td>'.&mt('None').'</td></tr>';
     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
         if ($env{'form.person_'.$i.'_uname'} ne '') {
+            if (ref($disallowed) eq 'ARRAY') {
+                next if (grep(/^$i$/,@{$disallowed}));
+            } 
             my @allsecs = &Apache::loncommon::get_env_multiple('form.person_'.$i.'_sec');
             my $newsec = $env{'form.person_'.$i.'_newsec'};
             $newsec =~ s/^\s+//;
@@ -1787,8 +1967,20 @@
                 '<td>'.$showsec.'</td></tr>';
         }
     }
-    my $output =  
-                  '<div>'.&Apache::lonhtmlcommon::start_pick_box().
+    my $output;
+    if (ref($disallowed) eq 'ARRAY') {
+        if (@{$disallowed} > 0) {
+            if (ref($disallowmsg) eq 'HASH') {
+                $output = '<p class="LC_warning">'.
+                          &mt('Not all requested personnel could be included.').'<ul>'; 
+                foreach my $item (@{$disallowed}) {
+                    $output .= '<li>'.$disallowmsg->{$item}.'</li>';
+                }
+                $output .= '</ul></p>';
+            }
+        }
+    }
+    $output .=    '<div>'.&Apache::lonhtmlcommon::start_pick_box().
                   &Apache::lonhtmlcommon::row_title(&mt('Owner')).
                   '<table class="LC_innerpickbox"><tr>'.
                   '<th>'.&mt('Name').'</th>'.

--raeburn1252305049--