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

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Wed, 05 Dec 2007 16:49:55 -0000


This is a MIME encoded message

--raeburn1196873395
Content-Type: text/plain

raeburn		Wed Dec  5 11:49:55 2007 EDT

  Modified files:              
    /loncom/interface	loncreateuser.pm lonuserutils.pm 
  Log:
  Move routines from loncreateuser.pm to lonuserutils.pm
  - &classlist_drop()
  - &active_student_roles()
  - &course_sections()
  
  Moved determination of a list of groups for incorportion in a javascript Array() to &&get_groupslist() routine in lonuserutils.pm
  
  &setsections_javascript() routine created in lonuersutils.pm - provides javascript function that aggregates and verifies section selections when adding a new role.
  
  Modified in readiness to support section changing for multiple users/roles in a course.  Corresponding javascript function eliminated from loncreateuser.
  
  
--raeburn1196873395
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20071205114955.txt"

Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.197 loncom/interface/loncreateuser.pm:1.198
--- loncom/interface/loncreateuser.pm:1.197	Fri Nov 30 22:49:08 2007
+++ loncom/interface/loncreateuser.pm	Wed Dec  5 11:49:55 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.197 2007/12/01 03:49:08 albertel Exp $
+# $Id: loncreateuser.pm,v 1.198 2007/12/05 16:49:55 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -627,82 +627,8 @@
     }
 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) {
-                if (document.cu.elements[i-1].checked == true) {
-		    var match = str.split('_');
-                    var role = match[3];
-                    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;
-			var numsplit;
-                        if (newsecs != null && newsecs != "") {
-                            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 if (numsplit != null) {
-                            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();
-    }
-ENDSECCODE
+        $nondc_setsection_code =
+            &Apache::lonuserutils::setsections_javascript('cu',$groupslist);
     }
     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
                                    $nondc_setsection_code,$groupslist);
@@ -1214,7 +1140,7 @@
         }
     } else {
         $r->print(&course_level_table(%inccourses));
-        $r->print('<br /><input type="button" value="'.&mt('Modify User').'" onClick="setSections()" />'."\n");
+        $r->print('<br /><input type="button" value="'.&mt('Modify User').'" onClick="setSections(this.form)" />'."\n");
     }
     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate','ccuname','ccdomain']));
     $r->print('<input type="hidden" name="currstate" value="" />');
@@ -1876,8 +1802,10 @@
 	        $r->print(&mt('Revoking [_1] in [_2]: [_3]',
 			      $role,$scope,'<b>'.$result.'</b>').'<br />');
 		if ($role eq 'st') {
-		    my $result = &classlist_drop($scope,$env{'form.ccuname'},
-						 $env{'form.ccdomain'},$now);
+                    my $result =
+                        &Apache::lonuserutils::classlist_drop($scope,
+                            $env{'form.ccuname'},$env{'form.ccdomain'},
+                            $now);
 		    $r->print($result);
 		}
 	    }
@@ -1901,8 +1829,10 @@
 	        $r->print(&mt('Deleting [_1] in [_2]: [_3]',$role,$scope,
 			      '<b>'.$result.'</b>').'<br />');
 		if ($role eq 'st') {
-		    my $result = &classlist_drop($scope,$env{'form.ccuname'},
-						 $env{'form.ccdomain'},$now);
+                    my $result =
+                        &Apache::lonuserutils::classlist_drop($scope,
+                            $env{'form.ccuname'},$env{'form.ccdomain'},
+                            $now);
 		    $r->print($result);
 		}
             }
@@ -2101,30 +2031,6 @@
     return $outcome;
 }
 
-sub classlist_drop {
-    my ($scope,$uname,$udom,$now) = @_;
-    my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
-    my $cid=$cdom.'_'.$cnum;
-    my $user = $uname.':'.$udom;
-    if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
-	my $result = 
-	    &Apache::lonnet::cput('classlist',
-				  { $user => $now },
-				  $env{'course.'.$cid.'.domain'},
-				  $env{'course.'.$cid.'.num'});
-	return &mt('Drop from classlist: [_1]',
-		   '<b>'.$result.'</b>').'<br />';
-    }
-}
-
-sub active_student_roles {
-    my ($cnum,$cdom,$uname,$udom) = @_;
-    my %roles = 
-	&Apache::lonnet::get_my_roles($uname,$udom,'userroles',
-				      ['future','active'],['st']);
-    return exists($roles{"$cnum:$cdom:st"});
-}
-
 sub quota_admin {
     my ($setquota,$changeHash) = @_;
     my $quotachanged;
@@ -3230,7 +3136,9 @@
 <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
 	        if ($role ne 'cc') {
                     if (%sections_count) {
-                        my $currsec = &course_sections(\%sections_count,$protectedcourse.'_'.$role);
+                        my $currsec =
+                            &Apache::lonuserutils::course_sections(\%sections_count,
+                                                       $protectedcourse.'_'.$role);
                         $table .= 
                     '<td><table class="LC_createuser">'.
                      '<tr class="LC_section_row">
@@ -3270,7 +3178,9 @@
 <td>'.$plrole.'</td>
 <td>'.$area.'</td>'."\n";
                 if (%sections_count) {
-                    my $currsec = &course_sections(\%sections_count,$customrole);
+                    my $currsec =
+                        &Apache::lonuserutils::course_sections(\%sections_count,
+                                                               $customrole);
                     $table.=
                    '<td><table class="LC_createuser">'.
                    '<tr class="LC_section_row"><td valign="top">'.
@@ -3314,28 +3224,6 @@
     return $result;
 }
 
-sub course_sections {
-    my ($sections_count,$role) = @_;
-    my $output = '';
-    my @sections = (sort {$a <=> $b} keys %{$sections_count});
-    if (scalar(@sections) == 1) {
-        $output = '<select name="currsec_'.$role.'" >'."\n".
-                  '  <option value="">Select</option>'."\n".
-                  '  <option value="">No section</option>'."\n".
-                  '  <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
-    } else {
-        $output = '<select name="currsec_'.$role.'" ';
-        my $multiple = 4;
-        if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
-        $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
-        foreach my $sec (@sections) {
-            $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
-        }
-    }
-    $output .= '</select>'; 
-    return $output;
-}
-
 sub course_level_dc {
     my ($dcdom) = @_;
     my %customroles=&Apache::lonuserutils::my_custom_roles();
Index: loncom/interface/lonuserutils.pm
diff -u loncom/interface/lonuserutils.pm:1.7 loncom/interface/lonuserutils.pm:1.8
--- loncom/interface/lonuserutils.pm:1.7	Fri Nov 30 23:18:04 2007
+++ loncom/interface/lonuserutils.pm	Wed Dec  5 11:49:55 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Utility functions for managing LON-CAPA user accounts
 #
-# $Id: lonuserutils.pm,v 1.7 2007/12/01 04:18:04 albertel Exp $
+# $Id: lonuserutils.pm,v 1.8 2007/12/05 16:49:55 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -35,7 +35,8 @@
 use Apache::loncommon();
 use Apache::lonhtmlcommon;
 use Apache::lonlocal;
-use LONCAPA();
+use Apache::longroup;
+use LONCAPA qw(:DEFAULT :match);
 
 ###############################################################
 ###############################################################
@@ -2873,12 +2874,34 @@
     $r->print('<p>'.&mt('Re-enrollment will re-activate data.')) if ($count);
 }
 
-sub section_check_js {
-    my $groupslist;
-    my %curr_groups = &Apache::longroup::coursegroups();
-    if (%curr_groups) {
-        $groupslist = join('","',sort(keys(%curr_groups)));
+sub classlist_drop {
+    my ($scope,$uname,$udom,$now,$action) = @_;
+    my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
+    my $cid=$cdom.'_'.$cnum;
+    my $user = $uname.':'.$udom;
+    if ($action eq 'drop') {
+        if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
+            my $result =
+                &Apache::lonnet::cput('classlist',
+                                      { $user => $now },
+                                      $env{'course.'.$cid.'.domain'},
+                                      $env{'course.'.$cid.'.num'});
+            return &mt('Drop from classlist: [_1]',
+                       '<b>'.$result.'</b>').'<br />';
+        }
     }
+}
+
+sub active_student_roles {
+    my ($cnum,$cdom,$uname,$udom) = @_;
+    my %roles =
+        &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
+                                      ['future','active'],['st']);
+    return exists($roles{"$cnum:$cdom:st"});
+}
+
+sub section_check_js {
+    my $groupslist= &get_groupslist();
     return <<"END";
 function validate(caller) {
     var groups = new Array("$groupslist");
@@ -2928,5 +2951,144 @@
     return $response;
 }
 
+sub course_sections {
+    my ($sections_count,$role) = @_;
+    my $output = '';
+    my @sections = (sort {$a <=> $b} keys %{$sections_count});
+    if (scalar(@sections) == 1) {
+        $output = '<select name="currsec_'.$role.'" >'."\n".
+                  '  <option value="">Select</option>'."\n".
+                  '  <option value="">No section</option>'."\n".
+                  '  <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
+    } else {
+        $output = '<select name="currsec_'.$role.'" ';
+        my $multiple = 4;
+        if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
+        $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
+        foreach my $sec (@sections) {
+            $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
+        }
+    }
+    $output .= '</select>';
+    return $output;
+}
+
+sub get_groupslist {
+    my $groupslist;
+    my %curr_groups = &Apache::longroup::coursegroups();
+    if (%curr_groups) {
+        $groupslist = join('","',sort(keys(%curr_groups)));
+        $groupslist = '"'.$groupslist.'"';
+    }
+    return $groupslist;
+}
+
+sub setsections_javascript {
+    my ($form,$groupslist) = @_;
+    my ($checkincluded,$finish,$roleplace,$setsection_js);
+    if ($form eq 'cu') {
+        $checkincluded = 'formname.elements[i-1].checked == true';
+        $finish = 'formname.submit()';
+        $roleplace = 3;
+    } else {
+        $checkincluded = 'formname.name == "'.$form.'"';
+        $finish = "seccheck = 'ok';";
+        $roleplace = 1;
+        $setsection_js = "var seccheck = 'alert';";
+    }
+    my %alerts = &Apache::lonlocal::texthash(
+                    secd => 'Section designations do not apply to Course Coordinator roles.',
+                    accr => 'A course coordinator role will be added with access to all sections.',
+                    inea => 'In each course, each user may only have one student role at a time.',
+                    youh => 'You had selected ',
+                    secs => 'sections.',
+                    plmo => 'Please modify your selections so they include no more than one section.',
+                    mayn => 'may not be used as the name for a section, as it is a reserved word.',
+                    plch => 'Please choose a different section name.',
+                    mnot => 'may not be used as a section name, as it is the name of a course group.',
+                    secn => 'Section names and group names must be distinct. Please choose a different section name.',
+                 );
+    $setsection_js .= <<"ENDSECCODE";
+
+function setSections(formname) {
+    var re1 = /^currsec_/;
+    var groups = new Array($groupslist);
+    for (var i=0;i<formname.elements.length;i++) {
+        var str = formname.elements[i].name;
+        var checkcurr = str.match(re1);
+        if (checkcurr != null) {
+            if ($checkincluded) {
+                var match = str.split('_');
+                var role = match[$roleplace];
+                if (role == 'cc') {
+                    alert("$alerts{'secd'}\\n$alerts{'accr'}");
+                }
+                else {
+                    var sections = '';
+                    var numsec = 0;
+                    var sections;
+                    for (var j=0; j<formname.elements[i].length; j++) {
+                        if (formname.elements[i].options[j].selected == true ) {
+                            if (formname.elements[i].options[j].value != "") {
+                                if (numsec == 0) {
+                                    if (formname.elements[i].options[j].value != "") {
+                                        sections = formname.elements[i].options[j].value;
+                                        numsec ++;
+                                    }
+                                }
+                                else {
+                                    sections = sections + "," +  formname.elements[i].options[j].value
+                                    numsec ++;
+                                }
+                            }
+                        }
+                    }
+                    if (numsec > 0) {
+                        if (formname.elements[i+1].value != "" && formname.elements[i+1].value != null) {
+                            sections = sections + "," +  formname.elements[i+1].value;
+                        }
+                    }
+                    else {
+                        sections = formname.elements[i+1].value;
+                    }
+                    var newsecs = formname.elements[i+1].value;
+                    var numsplit;
+                    if (newsecs != null && newsecs != "") {
+                        numsplit = newsecs.split(/,/g);
+                        numsec = numsec + numsplit.length;
+                    }
+
+                    if ((role == 'st') && (numsec > 1)) {
+                        alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}")
+                        return;
+                    }
+                    else {
+                        if (numsplit != null) {
+                            for (var j=0; j<numsplit.length; j++) {
+                                if ((numsplit[j] == 'all') ||
+                                    (numsplit[j] == 'none')) {
+                                    alert("'"+numsplit[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
+                                    return;
+                                }
+                                for (var k=0; k<groups.length; k++) {
+                                    if (numsplit[j] == groups[k]) {
+                                        alert("'"+numsplit[j]+"' $alerts{'mnot'}\\n$alerts{'secn'}");
+                                        return;
+                                    }
+                                }
+                            }
+                        }
+                        formname.elements[i+2].value = sections;
+                    }
+                }
+            }
+        }
+    }
+    $finish
+}
+ENDSECCODE
+    return $setsection_js;
+}
+
 1;
 

--raeburn1196873395--