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

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Sat, 08 Mar 2008 03:48:41 -0000


This is a MIME encoded message

--raeburn1204948121
Content-Type: text/plain

raeburn		Fri Mar  7 22:48:41 2008 EDT

  Modified files:              
    /loncom/interface	loncreateuser.pm 
  Log:
  - Ability to configure self-enrollment in a course from User Management menu.
  Work-in-progress. 
  
  
--raeburn1204948121
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20080307224841.txt"

Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.236 loncom/interface/loncreateuser.pm:1.237
--- loncom/interface/loncreateuser.pm:1.236	Fri Mar  7 06:19:16 2008
+++ loncom/interface/loncreateuser.pm	Fri Mar  7 22:48:41 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.236 2008/03/07 11:19:16 bisitz Exp $
+# $Id: loncreateuser.pm,v 1.237 2008/03/08 03:48:41 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -2970,6 +2970,25 @@
                      '<span class="LC_error">'.&mt('You do not have permission to modify dates or sections for users').'</span>'. 
                      &Apache::loncommon::end_page());
         }
+    } elsif ($env{'form.action'} eq 'selfenroll') {
+        $r->print(&header());
+        &Apache::lonhtmlcommon::add_breadcrumb
+            ({href=>'/adm/createuser?action=selfenroll',
+              text=>"Configure Self-enrollment"});
+        if (!exists($env{'form.state'})) {
+            $r->print(&Apache::lonhtmlcommon::breadcrumbs('Configure Self-enrollment',
+                                                          'Course_Self_Enrollment'));
+
+            &print_selfenroll_menu($r,$context,$permission);
+        } elsif ($env{'form.state'} eq 'done') {
+            &Apache::lonhtmlcommon::add_breadcrumb
+            ({href=>'/adm/createuser?action=selfenroll',
+              text=>"Result"});
+            $r->print(&Apache::lonhtmlcommon::breadcrumbs('Self-enrollment result',
+                                                          'Course_Self_Enrollment'));
+            &update_selfenroll_config($r,$context);
+        }
+        $r->print(&Apache::loncommon::end_page());
     } else {
         $r->print(&header());
         $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management'));
@@ -3078,7 +3097,12 @@
                  permission => (&Apache::lonnet::auto_run($cnum,$cdom)
                                 && $permission->{'cusr'}),
                  url  => '/adm/populate',
-                 });
+                 },
+               { text => 'Configure User Self-enrollment',
+                 help => 'Course_Self_Enrollment',
+                 action => 'selfenroll',
+                 permission => $permission->{'cusr'},    
+               });
         }
         push(@courselinks,
                { text => 'Manage Course Groups',
@@ -3120,6 +3144,126 @@
 					 \%saveable_parameters);
 }
 
+sub print_selfenroll_menu {
+    my ($r,$context,$permission) = @_;
+    my $formname = 'enrollstudent';
+    my $nolink = 1;
+    my ($row,$lt) = &get_selfenroll_titles();
+    my $groupslist = &Apache::lonuserutils::get_groupslist();
+    my $setsec_js = 
+        &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
+    my $output = '<script type="text/javascript">'."\n".
+                 $setsec_js."\n".
+                 '</script>'."\n".
+                 '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n".
+                 '<form name="'.$formname.'" method="post">'."\n".
+                 &Apache::lonhtmlcommon::start_pick_box();
+    if (ref($row) eq 'ARRAY') {
+        foreach my $item (@{$row}) {
+            my $title = $item; 
+            if (ref($lt) eq 'HASH') {
+                $title = $lt->{$item};
+            }
+            $output .= 
+                &Apache::lonhtmlcommon::row_title($title,
+                             'LC_selfenroll_pick_box_title','LC_oddrow_value')."\n";
+            if ($item eq 'types') {
+                my $curr_types = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_types'};
+                $output .= '<input type="text" name="selfenroll_types" value="'.$curr_types.'" size="50" />';
+            } elsif ($item eq 'registered') {
+                my ($regon,$regoff);
+                if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_registered'}) {
+                    $regon = ' checked="checked" ';
+                    $regoff = ' ';
+                } else {
+                    $regon = ' ';
+                    $regoff = ' checked="checked" ';
+                }
+                $output .= '<label>'.
+                           '<input type="radio" name="registered" value="0"'.$regoff.'/>'.
+                           &mt('No').'</label>&nbsp;&nbsp;<label>'.
+                           '<input type="radio" name="registered" value="1"'.$regon.'/>'.
+                           &mt('Yes').'</label>';
+            } elsif ($item eq 'enroll_dates') {
+                my $starttime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_start_date'};
+                my $endtime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_end_date'};
+                if ($starttime eq '') {
+                    $starttime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
+                }
+                if ($endtime eq '') {
+                    $endtime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
+                }
+                my $startform =
+                    &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_date',$starttime,
+                                      undef,undef,undef,undef,undef,undef,undef,$nolink);
+                my $endform =
+                    &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_date',$endtime,
+                                      undef,undef,undef,undef,undef,undef,undef,$nolink);
+                $output .= &selfenroll_date_forms($startform,$endform);
+            } elsif ($item eq 'access_dates') {
+                my $starttime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_start_access'};
+                my $endtime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_end_access'};
+                if ($starttime eq '') {
+                    $starttime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
+                }
+                if ($endtime eq '') {
+                    $endtime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
+                }
+                my $startform =
+                    &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_access',$starttime,
+                                      undef,undef,undef,undef,undef,undef,undef,$nolink);
+                my $endform =
+                    &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_access',$endtime,
+                                      undef,undef,undef,undef,undef,undef,undef,$nolink);
+                $output .= &selfenroll_date_forms($startform,$endform);
+            } elsif ($item eq 'section') {
+                my $currsec = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_section'}; 
+                my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+                my $cnum = $env{'course.'.$env{'request.course.id'},'.num'};
+                my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
+                my $newsecval;
+                if ($currsec ne 'none' && $currsec ne '') {
+                    if (!defined($sections_count{$currsec})) {
+                        $newsecval = $currsec;
+                    }
+                }
+                my $sections_select = 
+                    &Apache::lonuserutils::course_sections(\%sections_count,'st',$currsec);
+                $output .= '<table class="LC_createuser">'."\n".
+                           '<tr class="LC_section_row">'."\n".
+                           '<td align="center">'.&mt('Existing sections')."\n".
+                           '<br />'.$sections_select.'</td><td align="center">'.
+                           &mt('New section').'<br />'."\n".
+                           '<input type="text" name="newsec" size="15" value="'.$newsecval.'" />'."\n".
+                           '<input type="hidden" name="sections" value="" />'."\n".
+                           '<input type="hidden" name="state" value="done" />'."\n".
+                           '</td></tr></table>'."\n";
+            }
+            $output .= &Apache::lonhtmlcommon::row_closure(1);
+        }
+    }
+    $output .= &Apache::lonhtmlcommon::end_pick_box().
+               '<br /><input type="button" name="selfenrollconf" value="'.
+               &mt('Save changes').'" onclick="setSections(this.form);" /></form>';
+    $r->print($output);
+    return;
+}
+
+sub selfenroll_date_forms {
+    my ($startform,$endform) = @_;
+    my $output .= &Apache::lonhtmlcommon::start_pick_box()."\n".
+                  &Apache::lonhtmlcommon::row_title(&mt('Starts'),
+                                                    'LC_oddrow_value')."\n".
+                  $startform."\n".
+                  &Apache::lonhtmlcommon::row_closure(1).
+                  &Apache::lonhtmlcommon::row_title(&mt('Ends'),
+                                                   'LC_oddrow_value')."\n".
+                  $endform."\n".
+                  &Apache::lonhtmlcommon::row_closure(1).
+                  &Apache::lonhtmlcommon::end_pick_box();
+    return $output;
+}
+
 #-------------------------------------------------- functions for &phase_two
 sub user_search_result {
     my ($context,$srch) = @_;
@@ -3726,7 +3870,7 @@
                      '<td>&nbsp;&nbsp;</td>'.
                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
                      '<input type="text" name="newsec" value="" />'.
-                     '<input type="hidden" name="sections" value="" />'.
+                     '<input type="hidden" name="section" value="" />'.
                      '<input type="hidden" name="groups" value="" /></td>'.
                      '</tr></table></td>';
     $otheritems .= <<ENDTIMEENTRY;
@@ -3742,6 +3886,144 @@
     return $cb_jscript.$header.$hiddenitems.$otheritems;
 }
 
+sub update_selfenroll_config {
+    my ($r,$context) = @_;
+    my ($row,$lt) = &get_selfenroll_titles();
+    my $groupslist = &Apache::lonuserutils::get_groupslist();
+    my (%changes,%warning);
+    my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+    my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+    $r->print('<h3>'.$lt->{'selfenroll'}.'</h3>'."\n");
+    if (ref($row) eq 'ARRAY') {
+        foreach my $item (@{$row}) {
+            if ($item eq 'enroll_dates') {
+                my (%currenrolldate,%newenrolldate);
+                foreach my $type ('start','end') {
+                    $currenrolldate{$type} = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$type.'_date'};
+                    $newenrolldate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_date');
+                    if ($newenrolldate{$type} ne $currenrolldate{$type}) {
+                        $changes{'internal.selfenroll_'.$type.'_date'} = $newenrolldate{$type};
+                    }
+                }
+            } elsif ($item eq 'access_dates') {
+                my (%currdate,%newdate);
+                foreach my $type ('start','end') {
+                    $currdate{$type} = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$type.'_access'};
+                    $newdate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_access');
+                    if ($newdate{$type} ne $currdate{$type}) {
+                        $changes{'internal.selfenroll_'.$type.'_access'} = $newdate{$type};
+                    }
+                }
+            } else {
+                my $curr_val = 
+                    $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$item};
+                my $newval = $env{'form.selfenroll_'.$item};
+                if ($item eq 'section') {
+                    $newval = $env{'form.sections'};
+                    if (grep(/^\Q$newval\E$/,keys(%{$groupslist}))) {
+                        $newval = $curr_val;
+                        $warning{$item} = &mt('Section for self-enrolled users unchanged as the proposed section is a group').'<br />'.&mt('Group names and section names must be distinct');
+                    } elsif ($newval eq 'all') {
+                        $newval = $curr_val;
+                        $warning{$item} = &mt("Section for self-enrolled users unchanged, as 'all' is a reserved section name.");
+                    }
+                    if ($newval eq '') {
+                        $newval = 'none';
+                    }
+                }
+                if ($newval ne $curr_val) {
+                    $changes{'internal.selfenroll_'.$item} = $newval;
+                }
+            } 
+        }
+        if (keys(%warning) > 0) {
+            foreach my $item (@{$row}) {
+                if (exists($warning{$item})) {
+                    $r->print($warning{$item}.'<br />');
+                }
+            } 
+        }
+        if (keys(%changes) > 0) {
+            my $putresult = &Apache::lonnet::put('environment',\%changes,$cdom,$cnum);
+            if ($putresult eq 'ok') {
+                if ((exists($changes{'internal.selfenroll_types'})) ||
+                    (exists($changes{'internal.selfenroll_start_date'}))  ||
+                    (exists($changes{'internal.selfenroll_end_date'}))) {
+                    my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',
+                                                                $cnum,undef,undef,'Course');
+                    my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
+                    if (ref($crsinfo{$env{'request.course.id'}}) eq 'HASH') {
+                        foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
+                            if (exists($changes{'internal.'.$item})) {
+                                $crsinfo{$env{'request.course.id'}}{$item} = 
+                                    $changes{'internal.'.$item};
+                            }
+                        }
+                        my $crsputresult =
+                            &Apache::lonnet::courseidput($cdom,\%crsinfo,
+                                                         $chome,'notime');
+                    }
+                }
+                $r->print(&mt('The following changes were made to self-enrollment settings:').'<ul>');
+                foreach my $item (@{$row}) {
+                    my $title = $item;
+                    if (ref($lt) eq 'HASH') {
+                        $title = $lt->{$item};
+                    }
+                    if ($item eq 'enroll_dates') {
+                        foreach my $type ('start','end') {
+                            if (exists($changes{'internal.selfenroll_'.$type.'_date'})) {
+                                my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_date'});
+                                $r->print('<li>'.&mt('[_1]: [_2] set to "[_3]".',
+                                          $title,$type,$newdate).'</li>');
+                            }
+                        }
+                    } elsif ($item eq 'access_dates') {
+                        foreach my $type ('start','end') {
+                            if (exists($changes{'internal.selfenroll_'.$type.'_access'})) {
+                                my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_access'});
+                                $r->print('<li>'.&mt('[_1]: [_2] set to "[_3]".',
+                                          $title,$type,$newdate).'</li>');
+                            }
+                        }
+                    } else {
+                        if (exists($changes{'internal.selfenroll_'.$item})) {
+                            $r->print('<li>'.&mt('[_1] set to "[_2]".',$title,
+                                      $changes{'internal.selfenroll_'.$item}).'</li>'."\n");
+                        }
+                    }
+                }
+                $r->print('</ul>');
+                my %newenvhash;
+                foreach my $key (keys(%changes)) {
+                    $newenvhash{'course.'.$env{'request.course.id'}.'.'.$key} = $changes{$key};
+                }
+                &Apache::lonnet::appenv(%newenvhash);
+            } else {
+                $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.&mt('The error was: [_1].',$putresult));
+            }
+        } else {
+            $r->print(&mt('No changes were needed to the existing self-enrollment settings in this course.'));
+        }
+    } else {
+        $r->print(&mt('No changes were needed to the existing self-enrollment settings in this course.'));
+    }
+    return;
+}
+
+sub get_selfenroll_titles {
+    my @row = ('types','registered','enroll_dates','access_dates','section');
+    my %lt = &Apache::lonlocal::texthash (
+                selfenroll   => 'Self-enrollment with a student role',
+                types        => 'Users allowed to self-enroll in this course',
+                registered   => 'Restrict self-enrollment to registered students?',
+                enroll_dates => 'Dates self-enrollment available',
+                access_dates => 'Access dates for self-enrolled users',
+                section      => 'Section',
+             );
+    return (\@row,\%lt);
+}
+
 #---------------------------------------------- end functions for &phase_two
 
 #--------------------------------- functions for &phase_two and &phase_three

--raeburn1204948121--