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

raeburn lon-capa-cvs@mail.lon-capa.org
Thu, 22 Jun 2006 13:02:23 -0000


This is a MIME encoded message

--raeburn1150981343
Content-Type: text/plain

raeburn		Thu Jun 22 09:02:23 2006 EDT

  Modified files:              
    /loncom/interface	loncommon.pm lonpickcourse.pm 
  Log:
  Funtionality in pickcourse used to build contents of section select box moved to loncommon and extended to permit building of select boxes containing available roles in a course, status types and course groups. 
  
  pickcourse extended to support use by portfolio.pm when picking a course for course-based access controls, and providing role-specific,status-specific,section-specific and group-specific access controls.      
  
  
--raeburn1150981343
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20060622090223.txt"

Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.383 loncom/interface/loncommon.pm:1.384
--- loncom/interface/loncommon.pm:1.383	Wed Jun 14 14:59:25 2006
+++ loncom/interface/loncommon.pm	Thu Jun 22 09:02:22 2006
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.383 2006/06/14 18:59:25 albertel Exp $
+# $Id: loncommon.pm,v 1.384 2006/06/22 13:02:22 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -3984,6 +3984,40 @@
     return;
 }
 
+sub get_secgrprole_info {
+    my ($cdom,$cnum,$needroles,$type)  = @_;
+    my %sections_count = &get_sections($cdom,$cnum);
+    my @sections =  (sort {$a <=> $b} keys(%sections_count));
+    my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
+    my @groups = sort(keys(%curr_groups));
+    my $allroles = [];
+    my $rolehash;
+    my $accesshash = {
+                     active => 'Currently has access',
+                     future => 'Will have future access',
+                     previous => 'Previously had access',
+                  };
+    if ($needroles) {
+        $rolehash = {'all' => 'all'};
+        my %dumphash = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
+        foreach my $item (keys(%dumphash)) {
+            my ($role)=split(/\:/,$item,2);
+            if ($role eq 'cr') { next; }
+            if ($role =~ /^cr/) {
+                $$rolehash{$role} = (split('/',$role))[3];
+            } else {
+                $$rolehash{$role} = &Apache::lonnet::plaintext($role,$type);
+            }
+        }
+        foreach my $key (sort(keys(%{$rolehash}))) {
+            push(@{$allroles},$key);
+        }
+        push (@{$allroles},'st');
+        $$rolehash{'st'} = &Apache::lonnet::plaintext('st',$type);
+    }
+    return (\@sections,\@groups,$allroles,$rolehash,$accesshash);
+}
+
 =pod
 
 =item * get_unprocessed_cgi($query,$possible_names)
Index: loncom/interface/lonpickcourse.pm
diff -u loncom/interface/lonpickcourse.pm:1.48 loncom/interface/lonpickcourse.pm:1.49
--- loncom/interface/lonpickcourse.pm:1.48	Tue May 30 16:12:15 2006
+++ loncom/interface/lonpickcourse.pm	Thu Jun 22 09:02:22 2006
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Pick a course
 #
-# $Id: lonpickcourse.pm,v 1.48 2006/05/30 20:12:15 raeburn Exp $
+# $Id: lonpickcourse.pm,v 1.49 2006/06/22 13:02:22 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -100,28 +100,125 @@
                       ['-1','86400','604800','2592000','7776000',
 		      '15552000','31104000']));
 # if called from loncreateuser, report sections, then close
-    my (%loaditem,$seclist,$groupslist);
+    my (%loaditem,$seclist,$groupslist,$roleslist,$rolesnameslist,$sec_element,
+        $grplist_element,$accesslist,$accessnameslist);
+    my $sec_element = 'currsec';
+    my $grplist_element = 'groups';
     my $num_sections = 0;
-    if ($env{'form.form'} eq 'cu' && $env{'form.pickedcourse'}) {
-        $loaditem{'onload'} ="setSections()";
-        my ($cdom,$cnum) = split/_/,$env{'form.pickedcourse'};
-
-        my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
-        my @sections =  (sort {$a <=> $b} keys(%sections_count));
-	$seclist = join('","',@sections);
-        $num_sections = scalar(@sections);
-        my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
-	$groupslist = join(',',sort(keys(%curr_groups)));
+    my $num_groups = 0;
+    my $num_roles = 0;
+    my $num_access = 0;
+    my ($cdom,$cnum);
+    if ($env{'form.pickedcourse'}) {
+        ($cdom,$cnum) = split/_/,$env{'form.pickedcourse'};
     }
-    my $jscript;
     my $type = $env{'form.type'};
     if (!defined($env{'form.type'})) {
         $type = 'Course';
     }
-    my $typeselectform =  '<select name="type" size="1"';
-    if (!$type) {
-        $typeselectform .= ' selected="selected"';
+    if ($env{'form.form'} eq 'cu' && $env{'form.pickedcourse'}) {
+        $loaditem{'onload'} ="setSections()";
+        ($seclist,$num_sections,$groupslist,$num_groups) = 
+             &build_selection_lists($cdom,$cnum,$type,undef,undef,
+                                    $env{'form.form'});
+    }
+# if called from portfolio, report roles, sections, groups and close
+    my ($roles_element,$groups_element,$status_element,$setitems_function);
+    if ($env{'form.form'} eq 'portform' && $env{'form.pickedcourse'}) {
+        my ($row_id) = ($env{'form.cnumelement'} =~ /^crsnum_(\d+)$/);
+        $roles_element = 'role_'.$row_id.'_1';
+        $groups_element = 'group_'.$row_id.'_1';
+        $sec_element = 'section_'.$row_id.'_1';
+        $status_element = 'access_'.$row_id.'_1';
+        $grplist_element = 'grplist_'.$row_id.'_1';
+        $loaditem{'onload'} ="setItems()";
+        ($seclist,$num_sections,$groupslist,$num_groups,$roleslist,$num_roles,
+         $rolesnameslist,$accesslist,$num_access,$accessnameslist) =
+               &build_selection_lists($cdom,$cnum,$type,1,1,$env{'form.form'});
+        $setitems_function = qq|
+function setItems() {
+    opener.document.$env{"form.form"}.activate.checked = true;
+    setRoles();
+    setGroups();
+    setStatus();
+    setSections();
+}
+
+function setRoles() {
+    var numRoles = $num_roles
+    var rolesArray = new Array("$roleslist");
+    var roleNamesArray = new Array("$rolesnameslist");
+    opener.document.$env{"form.form"}.$roles_element.length = 0;
+    if (numRoles == 0) {
+        opener.document.$env{"form.form"}.$roles_element.multiple=false;
+        opener.document.$env{"form.form"}.$roles_element.size=1;
+        opener.document.$env{"form.form"}.$roles_element.options[0] = new Option('No existing roles','',false,false);
+    } else {
+        if (numRoles == 1) {
+            opener.document.$env{"form.form"}.$roles_element.multiple=false;
+            opener.document.$env{"form.form"}.$roles_element.size=1;
+            opener.document.$env{"form.form"}.$roles_element.options[0] = new Option('Select','',true,true);
+            opener.document.$env{"form.form"}.$roles_element.options[1] = new Option(roleNamesArray[0],rolesArray[0],false,false);
+        } else {
+            for (var i=0; i<numRoles; i++) {
+                opener.document.$env{"form.form"}.$roles_element.options[i] = new Option(roleNamesArray[i],rolesArray[i],false,false);
+            }
+            opener.document.$env{"form.form"}.$roles_element.multiple=true;
+            if (numRoles < 3) {
+                opener.document.$env{"form.form"}.$roles_element.size=numRoles;
+            } else {
+                opener.document.$env{"form.form"}.$roles_element.size=3;
+            }
+            opener.document.$env{"form.form"}.$roles_element.options[0].selected = false;
+        }
+    }
+}
+
+function setStatus() {
+    var numStatus = $num_access;
+    var statusArray = new Array("$accesslist");
+    var statusNamesArray = new Array("$accessnameslist");
+    opener.document.$env{"form.form"}.$status_element.multiple=true;
+    opener.document.$env{"form.form"}.$status_element.size=3;
+    for (var i=0; i<numStatus; i++) {
+        opener.document.$env{"form.form"}.$status_element.options[i] = new Option(statusNamesArray[i],statusArray[i],false,false);
+    }
+}
+
+function setGroups() {
+    var numGroups = $num_groups;
+    var groupsArray = new Array("$groupslist");
+    opener.document.$env{"form.form"}.$groups_element.length = 0;
+    if (numGroups == 0) {
+        opener.document.$env{"form.form"}.$groups_element.multiple=false;
+        opener.document.$env{"form.form"}.$groups_element.size=1;
+        opener.document.$env{"form.form"}.$groups_element.options[0] = new Option('No existing groups','',false,false);
+    } else {
+        if (numGroups == 1) {
+            opener.document.$env{"form.form"}.$groups_element.multiple=false;
+            opener.document.$env{"form.form"}.$groups_element.size=1;
+            opener.document.$env{"form.form"}.$groups_element.options[0] = new Option('Select','',true,true);
+            opener.document.$env{"form.form"}.$groups_element.options[1] = new Option('No group','',false,false);
+            opener.document.$env{"form.form"}.$groups_element.options[2] = new Option(groupsArray[0],groupsArray[0],false,false);
+        } else {
+            for (var i=0; i<numGroups; i++) {
+                opener.document.$env{"form.form"}.$groups_element.options[i] = new Option(groupsArray[i],groupsArray[i],false,false);
+            }
+            opener.document.$env{"form.form"}.$groups_element.multiple=true;
+            if (numGroups < 3) {
+                opener.document.$env{"form.form"}.$groups_element.size=numGroups;
+            } else {
+                opener.document.$env{"form.form"}.$groups_element.size=3;
+            }
+            opener.document.$env{"form.form"}.$groups_element.options[0].selected = false;
+        }
+    }
+}
+
+|;
     }
+    my $jscript;
+    my $typeselectform =  '<select name="type" size="1"';
     $typeselectform .= ">\n";
     foreach my $posstype ('Course','Group') { 
         $typeselectform.='<option value="'.$posstype.'" "'.
@@ -163,6 +260,10 @@
         $lastaction = 'document.courselist.pickedcourse.value = cdom+"_"+cname;'."\n".
                       'document.courselist.submit();';
     }
+    if ($env{'form.form'} eq 'portform') {
+        $lastaction = 'document.courselist.pickedcourse.value = cdom+"_"+cname;'."\n".
+                      'document.courselist.submit();';
+    }
     my $roledom = $env{'form.roleelement'};
     if ($roledom) {
         $roleelement = '<input type="hidden" name="roleelement" value="'.$roledom.'" />';
@@ -251,35 +352,37 @@
 function setSections() {
     var numSections = $num_sections
     var sectionsArray = new Array("$seclist")
-    opener.document.$env{"form.form"}.currsec.length = 0
+    opener.document.$env{"form.form"}.$sec_element.length = 0
     if (numSections == 0) {
-        opener.document.$env{"form.form"}.currsec.multiple=false
-        opener.document.$env{"form.form"}.currsec.size=1;
-        opener.document.$env{"form.form"}.currsec.options[0] = new Option('No existing sections','',false,false)
+        opener.document.$env{"form.form"}.$sec_element.multiple=false
+        opener.document.$env{"form.form"}.$sec_element.size=1;
+        opener.document.$env{"form.form"}.$sec_element.options[0] = new Option('No existing sections','',false,false)
     } else {
         if (numSections == 1) {
-            opener.document.$env{"form.form"}.currsec.multiple=false;
-            opener.document.$env{"form.form"}.currsec.size=1;
-            opener.document.$env{"form.form"}.currsec.options[0] = new Option('Select','',true,true);
-            opener.document.$env{"form.form"}.currsec.options[1] = new Option('No section','',false,false) 
-            opener.document.$env{"form.form"}.currsec.options[2] = new Option(sectionsArray[0],sectionsArray[0],false,false);
+            opener.document.$env{"form.form"}.$sec_element.multiple=false;
+            opener.document.$env{"form.form"}.$sec_element.size=1;
+            opener.document.$env{"form.form"}.$sec_element.options[0] = new Option('Select','',true,true);
+            opener.document.$env{"form.form"}.$sec_element.options[1] = new Option('No section','',false,false) 
+            opener.document.$env{"form.form"}.$sec_element.options[2] = new Option(sectionsArray[0],sectionsArray[0],false,false);
         } else {
             for (var i=0; i<numSections; i++) {
-                opener.document.$env{"form.form"}.currsec.options[i] = new Option(sectionsArray[i],sectionsArray[i],false,false)
+                opener.document.$env{"form.form"}.$sec_element.options[i] = new Option(sectionsArray[i],sectionsArray[i],false,false)
             }
-            opener.document.$env{"form.form"}.currsec.multiple=true
+            opener.document.$env{"form.form"}.$sec_element.multiple=true
             if (numSections < 3) {
-                opener.document.$env{"form.form"}.currsec.size=numSections;
+                opener.document.$env{"form.form"}.$sec_element.size=numSections;
             } else {
-                opener.document.$env{"form.form"}.currsec.size=3;
+                opener.document.$env{"form.form"}.$sec_element.size=3;
             }
-            opener.document.$env{"form.form"}.currsec.options[0].selected = false
+            opener.document.$env{"form.form"}.$sec_element.options[0].selected = false
         }
     }
-    opener.document.$env{"form.form"}.groups.value='$groupslist';
+    opener.document.$env{"form.form"}.$grplist_element.value='$groupslist';
     self.close()
 }
 
+$setitems_function
+
 $jscript
 </script>
 $submitopener
@@ -392,7 +495,53 @@
                   "'".$cnum."','".$cdom."','".$cleandesc."')".'" />'."\n";
     }
     return $output;
-} 
+}
+
+sub build_selection_lists {
+    my ($cdom,$cnum,$type,$needroles,$needaccess,$caller) = @_;
+    my ($sections,$groups,$allroles,$roleshash,$accesshash) = 
+          &Apache::loncommon::get_secgrprole_info($cdom,$cnum,$needroles,$type);
+    if ($caller eq 'portform') {
+        if (@{$sections} == 0) {
+            @{$sections} = ('none');
+        } else {
+            unshift(@{$sections},('all','none'));
+        }
+        if (@{$groups} == 0) {
+            @{$groups} = ('none');
+        } else {
+            unshift(@{$groups},('all','none'));
+        }
+
+    } 
+    my $num_sections = scalar(@{$sections});
+    my $seclist = join('","',@{$sections});
+    my $num_groups = scalar(@{$groups});
+    my $groupslist = join('","',@{$groups});
+    my ($roleslist,$num_roles,$rolesnameslist,$num_access,$accesslist,
+        $accessnameslist);
+    if ($needroles) {
+        $num_roles = @{$allroles};
+        $roleslist = join('","',@{$allroles});
+        my @allrolesnames; 
+        foreach my $role (@{$allroles}) {
+            push(@allrolesnames,$$roleshash{$role});
+        }  
+        $rolesnameslist = join('","',@allrolesnames);
+    }
+    if ($needaccess) {
+        $num_access = keys(%{$accesshash});
+        my (@allaccess,@allaccessnames);
+        foreach my $key (sort(keys(%{$accesshash}))) {
+            push(@allaccess,$key);
+            push(@allaccessnames,$$accesshash{$key}); 
+        }
+        $accesslist = join('","',@allaccess);
+        $accessnameslist = join('","',@allaccessnames);
+    }
+    return ($seclist,$num_sections,$groupslist,$num_groups,$roleslist,
+            $num_roles,$rolesnameslist,$accesslist,$num_access,$accessnameslist);
+}
 
 1;
 __END__

--raeburn1150981343--