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

raeburn raeburn@source.lon-capa.org
Mon, 17 Aug 2009 04:39:15 -0000


This is a MIME encoded message

--raeburn1250483955
Content-Type: text/plain

raeburn		Mon Aug 17 04:39:15 2009 EDT

  Modified files:              
    /loncom/interface	lonrequestcourse.pm loncoursequeueadmin.pm 
  Log:
  Course Requests
  - Client-side javascript validation to check a course description was provided.
  - Server-side checking that requestor has rights to clone a course selected for cloning.
  
  
--raeburn1250483955
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20090817043915.txt"

Index: loncom/interface/lonrequestcourse.pm
diff -u loncom/interface/lonrequestcourse.pm:1.14 loncom/interface/lonrequestcourse.pm:1.15
--- loncom/interface/lonrequestcourse.pm:1.14	Sun Aug 16 19:16:16 2009
+++ loncom/interface/lonrequestcourse.pm	Mon Aug 17 04:39:15 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Request a course
 #
-# $Id: lonrequestcourse.pm,v 1.14 2009/08/16 19:16:16 raeburn Exp $
+# $Id: lonrequestcourse.pm,v 1.15 2009/08/17 04:39:15 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -689,13 +689,13 @@
                 $r->print('<div>'.&Apache::lonhtmlcommon::start_pick_box().$codepicker.
                           &Apache::lonhtmlcommon::end_pick_box().'</div>');
             } else {
-                $r->print(&courseinfo_form($dom,$formname,$crstype));
+                $r->print(&courseinfo_form($dom,$formname,$crstype,$next));
             }
         } else {
-            $r->print(&courseinfo_form($dom,$formname,$crstype));
+            $r->print(&courseinfo_form($dom,$formname,$crstype,$next));
         }
     } elsif ($prev eq 'codepick') {
-        $r->print(&courseinfo_form($dom,$formname,$crstype));
+        $r->print(&courseinfo_form($dom,$formname,$crstype,$next));
     } elsif ($state eq 'enrollment') {
         if ($crstype eq 'official') {
             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
@@ -743,7 +743,7 @@
         push(@excluded,'instcode');
     }
     $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</form>');
-    &display_navbuttons($r,$formname,$prev,$navtxt{'prev'},$next,$navtxt{'next'});
+    &display_navbuttons($r,$formname,$prev,$navtxt{'prev'},$next,$navtxt{'next'},$state);
     return;
 }
 
@@ -1216,33 +1216,35 @@
     $inst_headers .= '<th>'.&mt('Clone From').'</th>';
     if (($env{'form.clonecrs'} =~ /^$match_name$/) && 
         ($env{'form.clonedom'} =~ /^$match_domain$/)) {
-        my %coursehash = 
-            &Apache::lonnet::courseiddump($env{'form.clonedom'},'.',1,'.','.',
-                                          $env{'form.clonecrs'},undef,undef,'.');
-        my $cloneid = $env{'form.clonedom'}.'_'.$env{'form.clonecrs'};
-        if (ref($coursehash{$cloneid}) eq 'HASH') {
-            $inst_headers .= '<th>'.$ctxt{'dsh'}.'</th>';
-            my $clonedesc = $coursehash{$cloneid}{'description'};
-            my $cloneinst = $coursehash{$cloneid}{'inst_code'};
-
-            $inst_values .= '<td>'.$clonedesc.'&nbsp;';
-            if ($cloneinst ne '') {
-                $inst_values .= &mt('([_1] in [_2])',$cloneinst,$env{'form.clonedom'}); 
-            } else {
-                $inst_values .= &mt('(from [_1])',$env{'form.clonedom'});
-            }
-            $inst_values .= '</td><td>';
-            if ($env{'form.datemode'} eq 'preserve') {
-                $inst_values .= $ctxt{'pcd'}; 
-            } elsif ($env{'form.datemode'} eq 'shift') {
-                $inst_values .= &mt('Shift dates by [_1] days',$env{'form.dateshift'});
-            } else {
-                $inst_values .= $ctxt{'ncd'};
-            }
-            $inst_values .= '</td>';
-        } else {
-            $inst_values .= '<td>'.&mt('Unknown').'</td>';
-        }
+        my $canclone = &Apache::loncoursequeueadmin::can_clone_course($env{'user.name'},
+                           $env{'user.domain'},$env{'form.clonecrs'}, $env{'form.clonedom'});
+        if ($canclone) {
+            my %courseenv = &Apache::lonnet::userenvironment($env{'form.clonedom'},
+                              $env{'form.clonecrs'},('description','internal.coursecode'));
+            if (keys(%courseenv) > 0) {
+                $inst_headers .= '<th>'.$ctxt{'dsh'}.'</th>';
+                $inst_values .= '<td>'.$courseenv{'description'}.'&nbsp;';
+                my $cloneinst = $courseenv{'internal.coursecode'};
+                if ($cloneinst ne '') {
+                    $inst_values .= &mt('([_1] in [_2])',$cloneinst,$env{'form.clonedom'});
+                } else {
+                    $inst_values .= &mt('(from [_1])',$env{'form.clonedom'});
+                }
+                $inst_values .= '</td><td>';
+                if ($env{'form.datemode'} eq 'preserve') {
+                    $inst_values .= $ctxt{'pcd'}; 
+                } elsif ($env{'form.datemode'} eq 'shift') {
+                    $inst_values .= &mt('Shift dates by [_1] days',$env{'form.dateshift'});
+                } else {
+                    $inst_values .= $ctxt{'ncd'};
+                }
+                $inst_values .= '</td>';
+             } else {
+                 $inst_values .= '<td>'.&mt('Unknown').'</td>';
+             }
+         } else {
+             $inst_values .= '<td>'.&mt('Not permitted'),'</td>';
+         }
     } else {
         $inst_values .= '<td>'.&mt('None').'</td>';
     }
@@ -1346,9 +1348,26 @@
 }
 
 sub courseinfo_form {
-    my ($dom,$formname,$crstype) = @_;
-    my $output = '<div>'.&Apache::lonhtmlcommon::start_pick_box().
-                 &Apache::lonhtmlcommon::row_title('Course Description').
+    my ($dom,$formname,$crstype,$next) = @_;
+    my $nodescr = &mt('You must provide a (brief) course description.');
+    my $js_validate = <<"ENDJS";
+<script type="text/javascript">
+// <![CDATA['
+
+function validateForm() {
+    if ((document.$formname.cdescr.value == "")  || (document.$formname.cdescr.value == "undefined")) {
+        alert('$nodescr');
+        return;
+    }
+    nextPage(document.$formname,'$next');
+}
+// ]]
+</script>
+
+ENDJS
+
+    my $output .= $js_validate."\n".'<div>'.&Apache::lonhtmlcommon::start_pick_box().
+                  &Apache::lonhtmlcommon::row_title('Course Description').
                  '<input type="text" size="40" name="cdescr" />';
     my ($home_server_pick,$numlib) =
         &Apache::loncommon::home_server_form_item($dom,'chome',
@@ -1510,7 +1529,7 @@
 }
 
 sub display_navbuttons {
-    my ($r,$formname,$prev,$prevtext,$next,$nexttext) = @_;
+    my ($r,$formname,$prev,$prevtext,$next,$nexttext,$state) = @_;
     $r->print('<div class="LC_navbuttons">');
     if ($prev) {
         $r->print('
@@ -1523,7 +1542,10 @@
     onclick="javascript:history.back()"/>
    &nbsp;&nbsp;&nbsp;');
     }
-    if ($next) {
+    if ($state eq 'courseinfo') {
+        $r->print('
+      <input type="button" name="next" value="'.$nexttext.'" onclick="javascript:validateForm();" />');
+    } elsif ($next) {
         $r->print('
       <input type="button" name="next" value="'.$nexttext.'"
  onclick="javascript:nextPage(document.'.$formname.','."'".$next."'".')" />');
@@ -1653,6 +1675,20 @@
     if (exists($env{'form.instcode'})) {
         $instcode = $env{'form.instcode'};
     }
+    my $clonecrs = '';
+    my $clonedom = '';
+    if (($env{'form.clonecrs'} =~ /^($match_courseid)$/) && 
+        ($env{'form.clonedom'} =~ /^($match_domain)$/)) {
+        my $clonehome = &Apache::lonnet::homeserver($env{'form.clonedom'},$env{'form.clonecrs'});
+        if ($clonehome ne 'no_host') {  
+            my $canclone = &can_clone_course($env{'user.name'},$env{'user.domain'},
+                                             $env{'form.clonecrs'}, $env{'form.clonedom'});
+            if ($canclone) {
+                $clonecrs = $env{'form.clonecrs'};
+                $clonedom = $env{'form.clonedom'};
+            }
+        }
+    }
     my $details = {
                     owner          => $env{'user.name'},
                     domain         => $env{'user.domain'}, 
@@ -1662,8 +1698,8 @@
                     cdescr         => $env{'form.cdescr'},
                     crstype        => $env{'form.crstype'},
                     instcode       => $instcode,
-                    clonedom       => $env{'form.clonedom'},
-                    clonecrs       => $env{'form.clonecrs'},
+                    clonedom       => $clonedom,
+                    clonecrs       => $clonecrs,
                     datemode       => $env{'form.datemode'},
                     dateshift      => $env{'form.dateshift'},
                     sectotal       => $sectotal,
Index: loncom/interface/loncoursequeueadmin.pm
diff -u loncom/interface/loncoursequeueadmin.pm:1.3 loncom/interface/loncoursequeueadmin.pm:1.4
--- loncom/interface/loncoursequeueadmin.pm:1.3	Sun Aug 16 23:04:42 2009
+++ loncom/interface/loncoursequeueadmin.pm	Mon Aug 17 04:39:15 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Utilities to administer domain course requests and course self-enroll requests 
 #
-# $Id: loncoursequeueadmin.pm,v 1.3 2009/08/16 23:04:42 raeburn Exp $
+# $Id: loncoursequeueadmin.pm,v 1.4 2009/08/17 04:39:15 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -895,4 +895,30 @@
     return %batchhash;
 }
 
+sub can_clone_course {
+    my ($uname,$udom,$clonecrs,$clonedom) = @_;
+    my $canclone;
+    my %roleshash = &Apache::lonnet::get_my_roles($uname,$udom,'userroles',['active'],
+                                                  ['cc'],[$clonedom]);
+    if (exists($roleshash{$clonedom.':'.$clonecrs.':cc'})) {
+        $canclone = 1;
+    } else {
+        my %courseenv = &Apache::lonnet::userenvironment($clonedom,$clonecrs,('cloners'));
+        my $cloners = $courseenv{'cloners'};
+        if ($cloners ne '') {
+            my @cloneable = split(',',$cloners);
+            if (grep(/^\*$/,@cloneable)) {
+                $canclone = 1;
+            }
+            if (grep(/^\*:\Q$udom\E$/,@cloneable)) {
+                $canclone = 1;
+            }
+            if (grep(/^\Q$uname\E:\Q$udom\E$/,@cloneable)) {
+                $canclone = 1;
+            }
+        }
+    }
+    return $canclone;
+}
+
 1;

--raeburn1250483955--