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

raeburn lon-capa-cvs@mail.lon-capa.org
Mon, 08 May 2006 21:40:20 -0000


This is a MIME encoded message

--raeburn1147124420
Content-Type: text/plain

raeburn		Mon May  8 17:40:20 2006 EDT

  Modified files:              
    /loncom/interface	lonpickcourse.pm loncreateuser.pm 
  Log:
  Prevent course sections created through use of CUSR being named 'none', 'all' or the same name as a course group.  
  
  
--raeburn1147124420
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20060508174020.txt"

Index: loncom/interface/lonpickcourse.pm
diff -u loncom/interface/lonpickcourse.pm:1.42 loncom/interface/lonpickcourse.pm:1.43
--- loncom/interface/lonpickcourse.pm:1.42	Mon Apr 10 17:40:09 2006
+++ loncom/interface/lonpickcourse.pm	Mon May  8 17:40:20 2006
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Pick a course
 #
-# $Id: lonpickcourse.pm,v 1.42 2006/04/10 21:40:09 albertel Exp $
+# $Id: lonpickcourse.pm,v 1.43 2006/05/08 21:40:20 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -96,7 +96,7 @@
                       ['-1','86400','604800','2592000','7776000',
 		      '15552000','31104000']));
 # if called from loncreateuser, report sections, then close
-    my (%loaditem,$seclist);
+    my (%loaditem,$seclist,$groupslist);
     my $num_sections = 0;
     if ($env{'form.form'} eq 'cu' && $env{'form.pickedcourse'}) {
         $loaditem{'onload'} ="setSections()";
@@ -108,6 +108,12 @@
             @sections = (sort {$a <=> $b} keys(%sections_count));
             $seclist = join('","',@sections);
         }
+        my (%curr_groups,$numgroups);
+        $numgroups = &Apache::loncommon::coursegroups(\%curr_groups,$cdom,
+                                                         $cnum);
+        if ($numgroups > 0) {
+            $groupslist = join(',',sort(keys(%curr_groups)));
+        }
     }
     my $jscript;
     my $title = 'Selecting a course';
@@ -236,6 +242,7 @@
             opener.document.$env{"form.form"}.currsec.options[0].selected = false
         }
     }
+    opener.document.$env{"form.form"}.groups.value='$groupslist';
     self.close()
 }
 
Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.112 loncom/interface/loncreateuser.pm:1.113
--- loncom/interface/loncreateuser.pm:1.112	Mon Apr 10 15:59:53 2006
+++ loncom/interface/loncreateuser.pm	Mon May  8 17:40:20 2006
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.112 2006/04/10 19:59:53 albertel Exp $
+# $Id: loncreateuser.pm,v 1.113 2006/05/08 21:40:20 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -170,7 +170,8 @@
 
 
 sub user_modification_js {
-    my ($pjump_def, $dc_setcourse_code)=@_;
+    my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
+    
     return <<END;
 <script type="text/javascript" language="Javascript">
 
@@ -189,8 +190,11 @@
         pclose();
     }
 
+    $nondc_setsection_code
+
     function setSections() {
         var re1 = /^currsec_/;
+        var groups = new Array($groupslist);
         for (var i=0;i<document.cu.elements.length;i++) {
             var str = document.cu.elements[i].name;
             var checkcurr = str.match(re1);
@@ -226,7 +230,7 @@
                         if (numsec > 0) {
                             if (document.cu.elements[i+1].value != "" && document.cu.elements[i+1].value != null) {
                                 sections = sections + "," +  document.cu.elements[i+1].value;
-                            } 
+                            }
                         }
                         else {
                             sections = document.cu.elements[i+1].value;    
@@ -240,7 +244,20 @@
                             alert("In each course, each user may only have one student role at a time. You had selected "+numsec+" sections.\\nPlease modify your selections so they include no more than one section.")  
                             return;
                         }
-                        else { 
+                        else {
+                            for (var j=0; j<numsplit.length; j++) {
+                                if ((numsplit[j] == 'all') || 
+                                    (numsplit[j] == 'none')) {
+                                    alert("'"+numsplit[j]+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
+                                    return;
+                                }
+                                for (var k=0; k<groups.length; k++) {
+                                    if (numsplit[j] == groups[k]) {
+                                        alert("'"+numsplit[j]+"' may not be used as a section name, as it is the name of a course group.\\nSection names and group names must be distinct. Please choose a different section name.");
+                                        return;
+                                    }
+                                }
+                            }
                             document.cu.elements[i+2].value = sections;
                         }
                     }
@@ -283,7 +300,19 @@
     $ccdomain=~s/\W//g;
     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
     my $dc_setcourse_code = '';
+    my $nondc_setsection_code = '';
     my %loaditem;
+    my (%curr_groups,$groupslist,$numgroups);
+    if (exists($env{'request.course.id'})) {
+        my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+        my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+        $numgroups = &Apache::loncommon::coursegroups(\%curr_groups,$cdom,
+                                                         $cnum);
+    }
+    if ($numgroups > 0) {
+        $groupslist = join('","',sort(keys(%curr_groups)));
+        $groupslist = '"'.$groupslist.'"';   
+    }
     if ($env{'request.role'} =~ m-^dc\./(\w+)/$-) {
         my $dcdom = $1;
         $loaditem{'onload'} = "document.cu.coursedesc.value=''";
@@ -326,6 +355,21 @@
                 alert("In each course, each user may only have one student role at a time. You had selected "+numsections+" sections.\\nPlease modify your selections so they include no more than one section.")
                 return;
             }
+            for (var j=0; j<numsplit.length; j++) {
+                if ((numsplit[j] == 'all') || (numsplit[j] == 'none')) {
+                    alert("'"+numsplit[j]+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
+                    return;
+                }
+                if (document.cu.groups.value != '') {
+                    var groups = document.cu.groups.value.split(/,/g);
+                    for (var k=0; k<groups.length; k++) {
+                        if (numsplit[j] == groups[k]) {
+                            alert("'"+numsplit[j]+"' may not be used as the name for a section, as it is the name of a course group.\\nSection names and group names must be distinct. Please choose a different section name.");
+                            return; 
+                        }
+                    }
+                }
+            }
             if ((userrole == 'cc') && (numsections > 0)) {
                 alert("Section designations do not apply to Course Coordinator roles.\\nA course coordinator role will be added with access to all sections.");
                 section = "";
@@ -356,9 +400,86 @@
         return -1;
     }
 ENDSCRIPT
+    } else {
+        $nondc_setsection_code = <<"ENDSECCODE";
+    function setSections() {
+        var re1 = /^currsec_/;
+        var groups = new Array($groupslist);
+        for (var i=0;i<document.cu.elements.length;i++) {
+            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);
+                    var role = match[1];
+                    if (role == 'cc') {
+                        alert("Section designations do not apply to Course Coordinator roles.\\nA course coordinator role will be added with access to all sections.");
+                    }
+                    else {
+                        var sections = '';
+                        var numsec = 0;
+                        var sections;
+                        for (var j=0; j<document.cu.elements[i].length; j++) {
+                            if (document.cu.elements[i].options[j].selected == true ) {
+                                if (document.cu.elements[i].options[j].value != "") {
+                                    if (numsec == 0) {
+                                        if (document.cu.elements[i].options[j].value != "") {
+                                            sections = document.cu.elements[i].options[j].value;
+                                            numsec ++;
+                                        }
+                                    }
+                                    else {
+                                        sections = sections + "," +  document.cu.elements[i].options[j].value
+                                        numsec ++;
+                                    }
+                                }
+                            }
+                        }
+                        if (numsec > 0) {
+                            if (document.cu.elements[i+1].value != "" && document.cu.elements[i+1].value != null) {
+                                sections = sections + "," +  document.cu.elements[i+1].value;
+                            }
+                        }
+                        else {
+                            sections = document.cu.elements[i+1].value;
+                        }
+                        var newsecs = document.cu.elements[i+1].value;
+                        if (newsecs != null && newsecs != "") {
+                            var numsplit = newsecs.split(/,/g);
+                            numsec = numsec + numsplit.length;
+                        }
+                        if ((role == 'st') && (numsec > 1)) {
+                            alert("In each course, each user may only have one student role at a time. You had selected "+numsec+" sections.\\nPlease modify your selections so they include no more than one section.")
+                            return;
+                        }
+                        else {
+                            for (var j=0; j<numsplit.length; j++) {
+                                if ((numsplit[j] == 'all') ||
+                                    (numsplit[j] == 'none')) {
+                                    alert("'"+numsplit[j]+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
+                                    return;
+                                }
+                                for (var k=0; k<groups.length; k++) {
+                                    if (numsplit[j] == groups[k]) {
+                                        alert("'"+numsplit[j]+"' may not be used as a section name, as it is the name of a course group.\\nSection names and group names must be distinct. Please choose a different section name.");
+                                        return;
+                                    }
+                                }
+                            }
+                            document.cu.elements[i+2].value = sections;
+                        }
+                    }
+                }
+            }
+        }
+        document.cu.submit();
     }
-
-    my $js = &user_modification_js($pjump_def, $dc_setcourse_code);
+ENDSECCODE
+    }
+    my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
+                                   $nondc_setsection_code,$groupslist);
     my $start_page = 
 	&Apache::loncommon::start_page('Create Users, Change User Privileges',
 				       $js,{'add_entries' => \%loaditem,});
@@ -548,7 +669,7 @@
                    $bgcol=~s/[^7-9a-e]//g;
                    $bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',2,6);
                    if ($area=~/^\/(\w+)\/(\d\w+)\/(\w+)/) {
-                       $carea.='<br />Section/Group: '.$3;
+                       $carea.='<br />Section: '.$3;
 		       $sortkey.="\0$3";
                    }
                    $area=$carea;
@@ -884,6 +1005,7 @@
         $title='Modify User Privileges';
     }
     $r->print(&Apache::loncommon::start_page($title));
+    my %disallowed;
     # Check Inputs
     if (! $env{'form.ccuname'} ) {
 	$r->print($error.&mt('No login name specified').'.'.$end);
@@ -1087,6 +1209,7 @@
         }
     }
     ##
+    my %curr_groups;
     my $now=time;
     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
     foreach (keys (%env)) {
@@ -1113,7 +1236,7 @@
 	    } 
 	    if ($_=~/^form\.rev\:([^\_]+)\_cr\.cr\/(\w+)\/(\w+)\/(\w+)$/) {
 # Revoke custom role
-		$r->print(&mt('Revoking custom role').
+		$r->print(&mt('Revoking custom role:').
                       ' '.$4.' by '.$3.'@'.$2.' in '.$1.': <b>'.
                       &Apache::lonnet::revokecustomrole($env{'form.ccdomain'},
 				  $env{'form.ccuname'},$1,$2,$3,$4).
@@ -1136,7 +1259,7 @@
                          &Apache::lonnet::escape($now.':'),
 	                     $env{'course.'.$cid.'.home'}).'</b><br />');
 		}
-	    } 
+	    }
 	} elsif ($_=~/^form\.ren/) {
             my $udom = $env{'form.ccdomain'};
             my $uname = $env{'form.ccuname'};
@@ -1164,7 +1287,7 @@
 			      $role,$url,$result).'<br />';
 		}
                 $r->print($output);
-	    } 
+	    }
 	} elsif ($_=~/^form\.act/) {
             my $udom = $env{'form.ccdomain'};
             my $uname = $env{'form.ccuname'};
@@ -1187,8 +1310,15 @@
                 if ($num_sections == 0) {
                     $r->print(&commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end));
                 } else {
-                    foreach (sort {$a cmp $b} keys %sections) {
-                        my $securl = $url.'/'.$_;
+                    my $numgrp=&Apache::loncommon::coursegroups(\%curr_groups,
+                                                                $one,$two);
+                    foreach my $sec (sort {$a cmp $b} keys %sections) {
+                        if (($sec eq 'none') || ($sec eq 'all') || 
+                            exists($curr_groups{$sec})) {
+                            $disallowed{$sec} = $url;
+                            next;
+                        }
+                        my $securl = $url.'/'.$sec;
 		        $r->print(&commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end));
                     }
                 }
@@ -1210,10 +1340,17 @@
                 if ($num_sections == 0) {
                     $r->print(&commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,''));
                 } else {
+                    my $numgrp=&Apache::loncommon::coursegroups(\%curr_groups,
+                                                                $one,$two);
                     my $emptysec = 0;
                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                         $sec =~ s/\W//g;
-                        if ($sec ne '') {  
+                        if ($sec ne '') {
+                            if (($sec eq 'none') || ($sec eq 'all') || 
+                                exists($curr_groups{$sec})) {
+                                $disallowed{$sec} = $url;
+                                next;
+                            }
                             my $securl = $url.'/'.$sec;
                             $r->print(&commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec));
                         } else {
@@ -1256,7 +1393,15 @@
 	    } else {
 		$r->print('<p>'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$_.'</tt></p><br />');
             }
-	} 
+            foreach my $key (sort(keys(%disallowed))) {
+                if (($key eq 'none') || ($key eq 'all')) {  
+                    $r->print('<p>'.&mt('[_1] may not be used as the name for a section, as it is a reserved word.',$key));
+                } else {
+                    $r->print('<p>'.&mt('[_1] may not be used as the name for a section, as it is the name of a course group.',$key));
+                }
+                $r->print(' '.&mt('Please <a href="javascript:history.go(-1)">go back</a> and choose a different section name.').'</p><br />');
+            }
+	}
     } # End of foreach (keys(%env))
 # Flush the course logs so reverse user roles immediately updated
     &Apache::lonnet::flushcourselogs();
@@ -1342,7 +1487,7 @@
         }
     } 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";
+        $result = "error: incomplete course id\n";
     }
     return $result;
 }
@@ -1604,7 +1749,7 @@
             'act'  => "Activate",
             'rol'  => "Role",
             'ext'  => "Extent",
-            'grs'  => "Group/Section",
+            'grs'  => "Section",
             'sta'  => "Start",
             'end'  => "End"
     );
@@ -1753,7 +1898,7 @@
                     'crl'  => "Course Level",
                     'crt'  => "Course Title",
                     'rol'  => "Role",
-                    'grs'  => "Group/Section",
+                    'grs'  => "Section",
                     'exs'  => "Existing sections",
                     'new'  => "Define new section", 
                     'sta'  => "Start",
@@ -1782,7 +1927,8 @@
                      ' <option value=""><--'.&mt('Pick course first').'</select></td>'.
                      '<td>&nbsp;&nbsp;</td>'.
                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
-                     '<input type="text" name="newsec" value="" /></td>'.
+                     '<input type="text" name="newsec" value="" />'.
+                     '<input type="hidden" name="groups" value="" /></td>'.
                      '</tr></table></td>';
     $otheritems .= <<ENDTIMEENTRY;
 <td><input type=hidden name="start" value=''>

--raeburn1147124420--