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

raeburn raeburn at source.lon-capa.org
Tue May 14 10:30:15 EDT 2013


raeburn		Tue May 14 14:30:15 2013 EDT

  Modified files:              
    /loncom/interface	lonsyllabus.pm 
  Log:
  - Move code used to update course environment when changing syllabus type
    to new subroutine(): update_syllabus_env to facilitate reuse.  
  
  
-------------- next part --------------
Index: loncom/interface/lonsyllabus.pm
diff -u loncom/interface/lonsyllabus.pm:1.122 loncom/interface/lonsyllabus.pm:1.123
--- loncom/interface/lonsyllabus.pm:1.122	Fri May 10 17:49:11 2013
+++ loncom/interface/lonsyllabus.pm	Tue May 14 14:30:15 2013
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Syllabus
 #
-# $Id: lonsyllabus.pm,v 1.122 2013/05/10 17:49:11 raeburn Exp $
+# $Id: lonsyllabus.pm,v 1.123 2013/05/14 14:30:15 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1059,28 +1059,9 @@
         $syllabus->{'uploaded.lastmodified'} = $now;
         my $putres = &Apache::lonnet::put('syllabus',$syllabus,$cdom,$cnum);
         if ($putres eq 'ok') {
-            my %storehash;
-            if ($courseenv->{'uploadedsyllabus'}) {
-                &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.uploadedsyllabus');
-                $storehash{'uploadedsyllabus'} = '';
-            }
-            if ($courseenv->{'externalsyllabus'}) {
-                &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.externalsyllabus');
-                $storehash{'externalsyllabus'} = '';
-            }
-            if ($courseenv->{'minimalsyllabus'}) {
-                &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.minimalsyllabus');
-                $storehash{'minimalsyllabus'} = '';
-            }
-            $storehash{'updatedsyllabus'} = $now;
-            &Apache::lonnet::put('environment',\%storehash,$cdom,$cnum);
-            &Apache::lonnet::appenv({'course.'.$env{'request.course.id'}.'.updatedsyllabus' => $now});
-            if (($courseenv->{'externalsyllabus'} || $courseenv->{'uploadedsyllabus'}) ||
-                ($courseenv->{'minimalsyllabus'})) {
-                undef($uploaded);
-                undef($external);
-                undef($minimal);
-            }
+            ($uploaded,$minimal,$external) = 
+                &update_syllabus_env($cdom,$cnum,$courseenv,$env{'form.choice'},$uploaded,
+                                     $minimal,$external);
             $output = '<div>'.
                       &Apache::lonhtmlcommon::confirm_success(&mt('Template saved.')).
                       '</div>';
@@ -1095,21 +1076,9 @@
                 $output = '<div class="LC_info">'.
                           &mt('External URL unchanged.').
                           '</div>';
-                if ($uploaded) {
-                    my $prefix;
-                    my $home=&Apache::lonnet::homeserver($cnum,$cdom);
-                    if ($home ne 'no_host') {
-                        my $protocol = $Apache::lonnet::protocol{$home};
-                        $protocol = 'http' if ($protocol ne 'https');
-                        $prefix = $protocol.'://'.&Apache::lonnet::hostname($home);
-                    }
-                    unless ($external =~ m{^\Q$prefix/uploaded/$cdom/$cnum/portfolio/syllabus\E}) {
-                        &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.uploadedsyllabus');
-                        &Apache::lonnet::put('environment',{uploadedsyllabus => ''},
-                                             $cdom,$cnum);
-                        undef($uploaded);
-                    }
-                }
+                ($uploaded,$minimal,$external) =
+                    &update_syllabus_env($cdom,$cnum,$courseenv,$env{'form.choice'},$uploaded,
+                                         $minimal,$external);
             } else {
                 $external=$env{'form.externalsyllabus'};
                 $external =~ s/(`)//g;
@@ -1121,21 +1090,9 @@
                     $output = '<div>'.
                               &Apache::lonhtmlcommon::confirm_success(&mt('External URL saved.')).
                              '</div>';
-                    if ($uploaded) {
-                        my $prefix;
-                        my $home=&Apache::lonnet::homeserver($cnum,$cdom);
-                        if ($home ne 'no_host') {
-                            my $protocol = $Apache::lonnet::protocol{$home};
-                            $protocol = 'http' if ($protocol ne 'https');
-                            $prefix = $protocol.'://'.&Apache::lonnet::hostname($home);
-                        }
-                        unless ($external =~ m{^\Q$prefix/uploaded/$cdom/$cnum/portfolio/syllabus\E}) {
-                            &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.uploadedsyllabus');
-                            &Apache::lonnet::put('environment',{uploadedsyllabus => ''},
-                                                 $cdom,$cnum);
-                            undef($uploaded);
-                        }
-                    }
+                    ($uploaded,$minimal,$external) =
+                        &update_syllabus_env($cdom,$cnum,$courseenv,$env{'form.choice'},$uploaded,
+                                             $minimal,$external);
                 } else {
                     $output = '<div class="LC_error">'.
                               &mt('An error occurred storing the external URL: [_1]',$putres).
@@ -1187,12 +1144,9 @@
                                                        'portfolio/syllabus/loncapa.html');
         }
         if ($url =~ m{^/uploaded/\Q$cdom\E/\Q$cnum\E.*/[^/]+$}) {
-            my $exturl;
-            my $home=&Apache::lonnet::homeserver($cnum,$cdom);
-            if ($home ne 'no_host') {
-                my $protocol = $Apache::lonnet::protocol{$home};
-                $protocol = 'http' if ($protocol ne 'https');
-                $exturl = $protocol.'://'.&Apache::lonnet::hostname($home).$url;
+            my $exturl = &home_http_host($cdom,$cnum);
+            if ($exturl) {
+                $exturl .= $url;
             }
             my %storehash;
             if ($env{'form.choice'} eq 'minimal') {
@@ -1224,6 +1178,9 @@
                     $output .= &return_to_editor($cdom,$cnum);
                     $earlyout = 1;
                 }
+                ($uploaded,$minimal,$external) =
+                    &update_syllabus_env($cdom,$cnum,$courseenv,$env{'form.choice'},$uploaded,
+                                         $minimal,$external);
             } else {
                 $error = 1;
                 $errormsg = $putres;
@@ -1273,6 +1230,88 @@
     return ($earlyout,$uploaded,$external,$minimal,$output);
 }
 
+sub update_syllabus_env {
+    my ($cdom,$cnum,$courseenv,$saved,$uploaded,$minimal,$external);
+    return unless(ref($courseenv) eq 'HASH');
+    my $now = time;
+    my (@envkeys,%storehash);
+    if ($saved eq 'template') {
+        if ($uploaded) {
+            push(@envkeys,'uploaded');
+        }
+        if ($minimal) {
+            push(@envkeys,'minimal');
+        }
+        if ($external) {
+            push(@envkeys,'external');
+        }
+        $storehash{'updatedsyllabus'} = $now;
+        &Apache::lonnet::appenv({'course.'.$env{'request.course.id'}.'.updatedsyllabus' => $now
+});
+
+    } elsif ($saved eq 'url') {
+        my $prefix = &home_http_host($cdom,$cnum);
+        if ($external =~ m{^\Q$prefix/uploaded/$cdom/$cnum/portfolio/syllabus/\E(.+)$}) {
+            my $file = $1;
+            if ($file eq 'loncapa.html') {
+                if ($uploaded) {
+                    push(@envkeys,'uploaded');
+                }
+            } elsif ($minimal) {
+                push(@envkeys,'minimal');
+            }
+        } else {
+            if ($uploaded) {
+                push(@envkeys,'uploaded');
+            }
+            if ($minimal) {
+                push(@envkeys,'minimal');
+            }
+        }
+    } elsif ($saved eq 'file') {
+        if ($minimal) {
+            push(@envkeys,'minimal');
+        }
+    } elsif ($saved eq 'minimal') {
+        if ($uploaded) {
+            push(@envkeys,'uploaded');
+        }
+    }
+    if (@envkeys > 0) {
+        foreach my $item (@envkeys) {
+            my $key = $item.'syllabus';
+            if ($courseenv->{$key}) {
+                &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.'.$key);
+                $storehash{$key} = '';
+            }
+            if ($item eq 'uploaded') {
+                undef($uploaded);
+            }
+            if ($item eq 'external') {
+                undef($external);
+            }
+            if ($item eq 'minimal') {
+                undef($minimal);
+            }
+        }
+    }
+    if (keys(%storehash) > 0) {
+        &Apache::lonnet::put('environment',\%storehash,$cdom,$cnum);
+    }
+    return ($uploaded,$minimal,$external);
+}
+
+sub home_http_host {
+    my ($cdom,$cnum) = @_;
+    my $home=&Apache::lonnet::homeserver($cnum,$cdom);
+    if ($home ne 'no_host') {
+        my $protocol = $Apache::lonnet::protocol{$home};
+        $protocol = 'http' if ($protocol ne 'https');
+        return $protocol.'//'.$home;
+    }
+    return;
+}
+
 sub process_upload {
     my ($upload_output,$cnum,$cdom,$allfiles,$codebase) = @_;
     my ($parseaction,$showupload,$mimetype);


More information about the LON-CAPA-cvs mailing list