--- loncom/interface/loncreateuser.pm	2006/09/07 21:43:20	1.125.2.7
+++ loncom/interface/loncreateuser.pm	2006/06/29 17:41:06	1.127
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.125.2.7 2006/09/07 21:43:20 albertel Exp $
+# $Id: loncreateuser.pm,v 1.127 2006/06/29 17:41:06 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -321,10 +321,10 @@ sub print_user_modification_page {
             else { 
                 var coursename = "_$dcdom"+"_"+course+"_"+userrole
                 document.cu.elements[numcourse].name = "act"+coursename
-                document.cu.elements[numcourse+4].name = "sec"+coursename
-                document.cu.elements[numcourse+4].value = section
-                document.cu.elements[numcourse+6].name = "start"+coursename
-                document.cu.elements[numcourse+7].name = "end"+coursename
+                document.cu.elements[numcourse+5].name = "sec"+coursename
+                document.cu.elements[numcourse+5].value = section
+                document.cu.elements[numcourse+7].name = "start"+coursename
+                document.cu.elements[numcourse+8].name = "end"+coursename
             }
         }
         document.cu.submit();
@@ -340,7 +340,7 @@ sub print_user_modification_page {
     }
 
     function setType() {
-        var crstype = 'Course'
+        var crstype = document.cu.crstype.options[document.cu.crstype.selectedIndex].value;
         rolevals = new Array("$rolevalslist");
         if (crstype == 'Group') {
             if (document.cu.currsec.options[0].text == "$pickcrsfirst") {
@@ -378,6 +378,7 @@ ENDSCRIPT
             var str = document.cu.elements[i].name;
             var checkcurr = str.match(re1);
             if (checkcurr != null) {
+                var re2 = /^currsec_[a-zA-Z0-9]+_[a-zA-Z0-9]+_(\\w+)\$/;
                 if (document.cu.elements[i-1].checked == true) {
                     var re2 = /^currsec_[a-zA-Z0-9]+_[a-zA-Z0-9]+_(\\w+)\$/;
                     match = re2.exec(str);
@@ -438,8 +439,8 @@ ENDSCRIPT
                                     }
                                 }
                             }
+                            document.cu.elements[i+2].value = sections;
                         }
-                        document.cu.elements[i+2].value = sections;
                     }
                 }
             }
@@ -604,7 +605,7 @@ END
                if ($area =~ /^\/(\w+)\/(\d\w+)/ ) {
 		   $class='Course';
                    my ($coursedom,$coursedir) = ($1,$2);
-		   $sortkey.="\0$coursedom";
+		   $sortkey.="\0$1";
                    # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
                    my %coursedata=
                        &Apache::lonnet::coursedescription($1.'_'.$2);
@@ -619,7 +620,6 @@ END
 		       $carea=&mt('Unavailable course').': '.$area;
 		       $sortkey.="\0".&mt('Unavailable course').': '.$area;
 		   }
-		   $sortkey.="\0$coursedir";
                    $inccourses{$1.'_'.$2}=1;
                    if ((&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2)) ||
                        (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
@@ -1251,7 +1251,7 @@ END
                 my $output;
                 if ($role eq 'st') {
                     if ($url =~ m-^/(\w+)/(\w+)/?(\w*)$-) {
-                        my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$1,$2,$3);
+                        my $result = &commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$1,$2,$3);
                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course')) {
                             $output = "Error: $result\n";
                         } else {
@@ -1298,7 +1298,7 @@ END
                 my %sections = ();
                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
                 if ($num_sections == 0) {
-                    $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end));
+                    $r->print(&commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end));
                 } else {
 		    my %curr_groups =
 			&Apache::longroup::coursegroups($one,$two);
@@ -1309,7 +1309,7 @@ END
                             next;
                         }
                         my $securl = $url.'/'.$sec;
-		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end));
+		        $r->print(&commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end));
                     }
                 }
 	    } elsif ($_=~/^form\.act\_([^\_]+)\_(\w+)\_([^\_]+)$/) {
@@ -1328,7 +1328,7 @@ END
                 my %sections = ();
                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
                 if ($num_sections == 0) {
-                    $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,''));
+                    $r->print(&commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,''));
                 } else {
                     my %curr_groups = 
 			&Apache::longroup::coursegroups($one,$two);
@@ -1342,13 +1342,13 @@ END
                                 next;
                             }
                             my $securl = $url.'/'.$sec;
-                            $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec));
+                            $r->print(&commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec));
                         } else {
                             $emptysec = 1;
                         }
                     }
                     if ($emptysec) {
-                        $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,''));
+                        $r->print(&commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,''));
                     }
                 } 
 	    } elsif ($_=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
@@ -1365,19 +1365,19 @@ END
                 my %sections = ();
                 my $num_sections = &build_roles($env{'form.sec_'.$1.'_'.$2},\%sections,$2);
                 if ($num_sections == 0) {
-                    $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$2,$start,$end,$1,undef,''));
+                    $r->print(&commit_standardrole($udom,$uname,$url,$2,$start,$end,$1,undef,''));
                 } else {
                     my $emptysec = 0;
                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                         if ($sec ne '') {
                             my $securl = $url.'/'.$sec;
-                            $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$2,$start,$end,$1,undef,$sec));
+                            $r->print(&commit_standardrole($udom,$uname,$securl,$2,$start,$end,$1,undef,$sec));
                         } else {
                             $emptysec = 1;
                         }
                     }
                     if ($emptysec) {
-                        $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$2,$start,$end,$1,undef,''));
+                        $r->print(&commit_standardrole($udom,$uname,$url,$2,$start,$end,$1,undef,''));
                     }
                 }
 	    } else {
@@ -1399,6 +1399,89 @@ END
     $r->print(&Apache::loncommon::end_page());
 }
 
+sub commit_customrole {
+    my ($udom,$uname,$url,$three,$four,$five,$start,$end) = @_;
+    my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.'@'.$three.' in '.$url.
+                         ($start?', '.&mt('starting').' '.localtime($start):'').
+                         ($end?', ending '.localtime($end):'').': <b>'.
+              &Apache::lonnet::assigncustomrole(
+                 $udom,$uname,$url,$three,$four,$five,$end,$start).
+                 '</b><br />';
+    return $output;
+}
+
+sub commit_standardrole {
+    my ($udom,$uname,$url,$three,$start,$end,$one,$two,$sec) = @_;
+    my $output;
+    my $logmsg;
+    if ($three eq 'st') {
+        my $result = &commit_studentrole(\$logmsg,$udom,$uname,$url,$three,$start,$end,$one,$two,$sec);
+        if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course')) {
+            $output = "Error: $result\n"; 
+        } else {
+            $output = &mt('Assigning').' '.$three.' in '.$url.
+               ($start?', '.&mt('starting').' '.localtime($start):'').
+               ($end?', '.&mt('ending').' '.localtime($end):'').
+               ': <b>'.$result.'</b><br />'.
+               &mt('Add to classlist').': <b>ok</b><br />';
+        }
+    } else {
+        $output = &mt('Assigning').' '.$three.' in '.$url.
+               ($start?', '.&mt('starting').' '.localtime($start):'').
+               ($end?', '.&mt('ending').' '.localtime($end):'').': <b>'.
+               &Apache::lonnet::assignrole(
+                   $udom,$uname,$url,$three,$end,$start).
+                   '</b><br />';
+    }
+    return $output;
+}
+
+sub commit_studentrole {
+    my ($logmsg,$udom,$uname,$url,$three,$start,$end,$one,$two,$sec) = @_;
+    my $linefeed =  '<br />'."\n";
+    my $result;
+    if (defined($one) && defined($two)) {
+        my $cid=$one.'_'.$two;
+        my $oldsec=&Apache::lonnet::getsection($udom,$uname,$cid);
+        my $secchange = 0;
+        my $expire_role_result;
+        my $modify_section_result;
+        unless ($oldsec eq '-1') {
+            unless ($sec eq $oldsec) {
+                $secchange = 1;
+                my $uurl='/'.$cid;
+                $uurl=~s/\_/\//g;
+                if ($oldsec) {
+                    $uurl.='/'.$oldsec;
+                }
+                $expire_role_result = &Apache::lonnet::assignrole($udom,$uname,$uurl,'st',time);
+                $result = $expire_role_result;
+            }
+        }
+        if (($expire_role_result eq 'ok') || ($secchange == 0)) {
+            $modify_section_result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,'','',$cid);
+            if ($modify_section_result =~ /^ok/) {
+                if ($secchange == 1) {
+                    $$logmsg .= "Section for $uname switched from old section: $oldsec to new section: $sec".$linefeed;
+                } elsif ($oldsec eq '-1') {
+                    $$logmsg .= "New student role for $uname in section $sec in course $cid".$linefeed;
+                } else {
+                    $$logmsg .= "Student $uname assigned to unchanged section $sec in course $cid".$linefeed;
+                }
+            } else {
+                $$logmsg .= "Error when attempting section change for $uname from old section $oldsec to new section: $sec in course $cid -error: $modify_section_result".$linefeed;
+            }
+            $result = $modify_section_result;
+        } elsif ($secchange == 1) {
+            $$logmsg .= "Error when attempting to expire role for $uname in old section $oldsec in course $cid -error: $expire_role_result".$linefeed;
+        }
+    } else {
+        $$logmsg .= "Incomplete course id defined.  Addition of user $uname from domain $udom to course $one\_$two, section $sec not completed.$linefeed";
+        $result = "error: incomplete course id\n";
+    }
+    return $result;
+}
+
 sub build_roles {
     my ($sectionstr,$sections,$role) = @_;
     my $num_sections = 0;
@@ -1428,7 +1511,7 @@ sub build_roles {
             $num_sections ++;
         }
     }
-
+                                                                                     
     return $num_sections;
 }
 
@@ -1503,7 +1586,7 @@ sub custom_role_editor {
     }
     my %lt=&Apache::lonlocal::texthash(
 		    'prv'  => "Privilege",
-		    'crl'  => "Course Level",
+		    'crl'  => "Course/Group Level",
                     'dml'  => "Domain Level",
                     'ssl'  => "System Level"
 				       );
@@ -1653,7 +1736,7 @@ sub course_level_table {
             'new'  => "Define new section",
             'ssd'  => "Set Start Date",
             'sed'  => "Set End Date",
-            'crl'  => "Course Level",
+            'crl'  => "Course/Group Level",
             'act'  => "Activate",
             'rol'  => "Role",
             'ext'  => "Extent",
@@ -1800,7 +1883,7 @@ sub course_level_dc {
                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
                       '<input type="hidden" name="dccourse" value="" />';
     my $courseform='<b>'.&Apache::loncommon::selectcourse_link
-            ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Course').'</b>';
+            ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Course/Group').'</b>';
     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom);
     my %lt=&Apache::lonlocal::texthash(
                     'typ' => "Type",
@@ -1813,9 +1896,14 @@ sub course_level_dc {
                     'ssd'  => "Set Start Date",
                     'sed'  => "Set End Date"
                   );
-    my $header = '<h4>'.&mt('Course Level').'</h4>'.
-                 '<table border="2"><tr><th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th></tr>';
-    my $otheritems = '<tr><td><input type="text" name="coursedesc" value="" onFocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc',".')" /></td>'."\n".
+    my $header = '<h4>'.&mt('Course/Group Level').'</h4>'.
+                 '<table border="2"><tr><th>'.$lt{'typ'}.'</th><th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th></tr>';
+    my $otheritems = '<tr><td><select name="crstype" onChange="javascript:setType();">'."\n".
+                     ' <option value="">'.&mt('Please select')."\n".
+                     ' <option value="Course">'.&mt('Course')."\n".
+                     ' <option value="Group">'.&mt('Group')."\n".
+                     '</select>'."\n".
+                     '<td><input type="text" name="coursedesc" value="" onFocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc','".')" /></td>'."\n".
                      '<td><select name="role">'."\n";
     foreach  ('st','ta','ep','in','cc') {
         my $plrole=&Apache::lonnet::plaintext($_);