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

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Mon, 21 Apr 2008 16:23:56 -0000


This is a MIME encoded message

--raeburn1208795036
Content-Type: text/plain

raeburn		Mon Apr 21 12:23:56 2008 EDT

  Modified files:              
    /loncom/interface	domainprefs.pm 
  Log:
  Bug 5650 scantronformat.tab file defined in configuration.db for domain.
   - published to RES space to be available for grading courses in domain on servers in other domains.
  -  On first load of scantron config page:
     -  if a custom scantronformat.tab file exists it is copied to domainconfig user's scantron directory in CSTR and published as custom.tab
     - default_scantronformat.tab file is copied to scantron dirctory and published as default.tab
  
  
--raeburn1208795036
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20080421122356.txt"

Index: loncom/interface/domainprefs.pm
diff -u loncom/interface/domainprefs.pm:1.45 loncom/interface/domainprefs.pm:1.46
--- loncom/interface/domainprefs.pm:1.45	Fri Feb 29 16:01:36 2008
+++ loncom/interface/domainprefs.pm	Mon Apr 21 12:23:55 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to set domain-wide configuration settings
 #
-# $Id: domainprefs.pm,v 1.45 2008/02/29 21:01:36 raeburn Exp $
+# $Id: domainprefs.pm,v 1.46 2008/04/21 16:23:55 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -70,10 +70,11 @@
     my %domconfig =
       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
                 'quotas','autoenroll','autoupdate','directorysrch',
-                'usercreation','usermodification','contacts','defaults'],$dom);
+                'usercreation','usermodification','contacts','defaults','scantron'],
+                $dom);
     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
                        'autoupdate','directorysrch','contacts',
-                       'usercreation','usermodification');
+                       'usercreation','usermodification','scantron');
     my %prefs = (
         'rolecolors' =>
                    { text => 'Default color schemes',
@@ -150,6 +151,13 @@
                                {col1 => 'Target user has role',
                                 col2 => 'User information updateable in course context'}],
                   },
+        'scantron' => 
+                  { text => 'Scantron format file',
+                    help => 'Domain_Scantron_Formats',
+                    header => [ {col1 => 'Item',
+                                 col2 => '',
+                              }],
+                  },
     );
     my @roles = ('student','coordinator','author','admin');
     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
@@ -343,6 +351,8 @@
         $output = &modify_contacts($dom,%domconfig);
     } elsif ($action eq 'defaults') {
         $output = &modify_defaults($dom,$r);
+    } elsif ($action eq 'scantron') {
+        $output = &modify_scantron($r,$dom,$confname,\%domconfig);
     }
     return $output;
 }
@@ -466,6 +476,8 @@
             $output .= &print_contacts($dom,$settings,\$rowtotal);
         } elsif ($action eq 'defaults') {
             $output .= &print_defaults($dom,\$rowtotal);
+        } elsif ($action eq 'scantron') {
+            $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
         }
     }
     $output .= '
@@ -1788,6 +1800,127 @@
     return (\%titles);
 }
 
+sub print_scantronformat {
+    my ($r,$dom,$confname,$settings,$rowtotal) = @_;
+    my $itemcount = 1;
+    my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls);
+    my $switchserver = &check_switchserver($dom,$confname);
+    my %lt = &Apache::lonlocal::texthash (
+                default => 'Default scantron format file error',
+                custom  => 'Custom scantron format file error',
+             );
+    my %scantronfiles = (
+        default => 'default.tab',
+        custom => 'custom.tab',
+    );
+    foreach my $key (keys(%scantronfiles)) {
+        $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
+                              .$scantronfiles{$key};
+    }
+    my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
+    if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
+        if (!$switchserver) {
+            my $servadm = $r->dir_config('lonAdmEMail');
+            my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
+            if ($configuserok eq 'ok') {
+                if ($author_ok eq 'ok') {
+                    my %legacyfile = (
+ default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
+ custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
+                    );
+                    my %md5chk;
+                    foreach my $type (keys(%legacyfile)) {
+                        $md5chk{$type} = split(/ /,`md5sum $legacyfile{$type}`);
+                        chop($md5chk{$type});
+                    }
+                    if ($md5chk{'default'} ne $md5chk{'custom'}) {
+                        foreach my $type (keys(%legacyfile)) {
+                            ($scantronurls{$type},$error{$type}) = 
+                                &legacy_scantronformat($r,$dom,$confname,
+                                                 $type,$legacyfile{$type},
+                                                 $scantronurls{$type},
+                                                 $scantronfiles{$type});
+                        }
+                    } else {
+                        ($scantronurls{'default'},$error{'default'}) =
+                            &legacy_scantronformat($r,$dom,$confname,
+                                          'default',$legacyfile{'default'},
+                                          $scantronurls{'default'},
+                                          $scantronfiles{'default'});
+                    }
+                }
+            }
+        } else {
+            $error{'default'} = &mt("Unable to copy default scantron formatfile to domain's RES space: [_1]",$switchserver);
+        }
+    }
+    if (ref($settings) eq 'HASH') {
+        if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
+            my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
+            if ((!@info) || ($info[0] eq 'no_such_dir')) {
+                $scantronurl = '';
+            } else {
+                $scantronurl = $settings->{'scantronformat'};
+            }
+            $is_custom = 1;
+        } else {
+            $scantronurl = $scantronurls{'default'};
+        }
+    } else {
+        $scantronurl = $scantronurls{'default'};
+    }
+    $css_class = $itemcount%2?' class="LC_odd_row"':'';
+    $datatable .= '<tr'.$css_class.'>';
+    if (!$is_custom) {
+        $datatable .= '<td>'.&mt('Default in use:').'<br />';
+        if ($scantronurl) {
+            $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
+                          &mt('Default scantron format file').'</a>';
+        } else {
+            $datatable = &mt('File unavailable for display');
+        }
+        $datatable .= '</td><td valign="bottom">'.&mt('Upload:').'<br />';
+    } else {
+        if (keys(%error) > 0) {
+            my $errorstr;
+            foreach my $key (sort(keys(%error))) {
+                $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
+            } 
+            $datatable .= '<td>'.$errorstr.'</td><td>';
+        } elsif ($scantronurl) {
+            $datatable .= '<td><a href="'.$scantronurl.'" target="_blank">'                          .&mt('Custom scantron format file').'</a>'
+                          .'<span class="LC_nobreak"><label>'
+                          .'<input type="checkbox" name="scantronformat_del"'
+                          .'" value="1" />'.&mt('Delete?').'</label></td>'
+                          .'<td>'.&mt('Replace:').'</span><br />';
+        }
+    }
+    if (keys(%error) == 0) {
+        if ($switchserver) {
+            $datatable .= &mt('Upload to library server: [_1]',$switchserver);
+        } else {
+            $datatable .='&nbsp;<input type="file" name="scantronformat" />';
+        }
+    }
+    $datatable .= '</td></tr>';
+    $$rowtotal ++;
+    return $datatable;
+}
+
+sub legacy_scantronformat {
+    my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
+    my ($url,$error);
+    my @statinfo = &Apache::lonnet::stat_file($newurl);
+    if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
+        (my $result,$url) =
+            &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
+                         '','',$newfile);
+        if ($result ne 'ok') {
+            $error = &mt("An error occurred publishing the [_1] scantron format file in RES space. Error was: [_2].",$newfile,$result);
+        }
+    }
+    return ($url,$error);
+}
 
 sub modifiable_userdata_row {
     my ($context,$role,$settings,$numinrow,$rowcount) = @_;
@@ -2128,16 +2261,8 @@
         foreach my $item (@bgs,@links,@logintext) {
             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
         }
-        my ($configuserok,$author_ok,$switchserver,%currroles);
-        my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
-        ($configuserok,%currroles) = &check_configuser($uhome,$dom,
-                                                       $confname,$servadm);
-        if ($configuserok eq 'ok') {
-            $switchserver = &check_switchserver($dom,$confname);
-            if ($switchserver eq '') {
-                $author_ok = &check_authorstatus($dom,$confname,%currroles);
-            }
-        }
+        my ($configuserok,$author_ok,$switchserver) = 
+            &config_check($dom,$confname,$servadm);
         my ($width,$height) = &thumb_dimensions();
         if (ref($domconfig->{$role}) ne 'HASH') {
             $domconfig->{$role} = {};
@@ -2169,11 +2294,11 @@
                                 $error = &mt("Upload of [_1] image for $role page(s) failed because an error occurred publishing the file in RES space. Error was: [_2].",$choices{img},$result);
                             }
                         } else {
-                            $error = &mt("Upload of [_1] image for $role page(s) failed because an author role could not be assigned to a Domain Configuation user ([_2]) in domain: [_3].  Error was: [_4].",$choices{$img},$confname,$dom,$author_ok);
+                            $error = &mt("Upload of [_1] image for $role page(s) failed because an author role could not be assigned to a Domain Configuration user ([_2]) in domain: [_3].  Error was: [_4].",$choices{$img},$confname,$dom,$author_ok);
                         }
                     }
                 } else {
-                    $error = &mt("Upload of [_1] image for $role page(s) failed because a Domain Configuation user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$choices{$img},$confname,$dom,$configuserok);
+                    $error = &mt("Upload of [_1] image for $role page(s) failed because a Domain Configuration user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$choices{$img},$confname,$dom,$configuserok);
                 }
                 if ($error) {
                     &Apache::lonnet::logthis($error);
@@ -2272,6 +2397,21 @@
     return ($errors,%changes);
 }
 
+sub config_check {
+    my ($dom,$confname,$servadm) = @_;
+    my ($configuserok,$author_ok,$switchserver,%currroles);
+    my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
+    ($configuserok,%currroles) = &check_configuser($uhome,$dom,
+                                                   $confname,$servadm);
+    if ($configuserok eq 'ok') {
+        $switchserver = &check_switchserver($dom,$confname);
+        if ($switchserver eq '') {
+            $author_ok = &check_authorstatus($dom,$confname,%currroles);
+        }
+    }
+    return ($configuserok,$author_ok,$switchserver);
+}
+
 sub default_change_checker {
     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
     foreach my $item (@{$links}) {
@@ -2411,7 +2551,7 @@
 }
 
 sub publishlogo {
-    my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight) = @_;
+    my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
     my ($output,$fname,$logourl);
     if ($action eq 'upload') {
         $fname=$env{'form.'.$formname.'.filename'};
@@ -2419,6 +2559,9 @@
     } else {
         ($fname) = ($formname =~ /([^\/]+)$/);
     }
+    if ($savefileas ne '') {
+        $fname = $savefileas;
+    }
     $fname=&Apache::lonnet::clean_filename($fname);
 # See if there is anything left
     unless ($fname) { return ('error: no uploaded file'); }
@@ -2446,7 +2589,7 @@
         !defined(&Apache::loncommon::fileembstyle($1))) {
         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
-        $output = &mt('File name not allowed a rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
+        $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
     } elsif (-d "$filepath/$file") {
         $output = &mt('File name is a directory name - rename the file and re-upload');
     } else {
@@ -3609,4 +3752,84 @@
     return $resulttext;
 }
 
+sub modify_scantron {
+    my ($r,$dom,$confname,$domconfig) = @_;
+    my ($resulttext,%confhash,%changes,$errors);
+    my $custom = 'custom.tab';
+    my $default = 'default.tab';
+    my $servadm = $r->dir_config('lonAdmEMail');
+    my ($configuserok,$author_ok,$switchserver) = 
+        &config_check($dom,$confname,$servadm);
+    if ($env{'form.scantronformat.filename'} ne '') {
+        my $error;
+        if ($configuserok eq 'ok') {
+            if ($switchserver) {
+                $error = &mt("Upload of scantron format file is not permitted to this server: [_1]",$switchserver);
+            } else {
+                if ($author_ok eq 'ok') {
+                    my ($result,$scantronurl) =
+                        &publishlogo($r,'upload','scantronformat',$dom,
+                                     $confname,'scantron','','',$custom);
+                    if ($result eq 'ok') {
+                        $confhash{'scantron'}{'scantronformat'} = $scantronurl;
+                        $changes{'scantron'}{'scantronformat'} = 1;
+                    } else {
+                        $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
+                    }
+                } else {
+                    $error = &mt("Upload of [_1] failed because an author role could not be assigned to a Domain Configuration user ([_2]) in domain: [_3].  Error was: [_4].",$custom,$confname,$dom,$author_ok);
+                }
+            }
+        } else {
+            $error = &mt("Upload of [_1] failed because a Domain Configuration user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$custom,$confname,$dom,$configuserok);
+        }
+        if ($error) {
+            &Apache::lonnet::logthis($error);
+            $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
+        }
+    }
+    if (ref($domconfig) eq 'HASH') {
+        if (ref($domconfig->{'scantron'}) eq 'HASH') {
+            if ($domconfig->{'scantron'}{'scantronformat'} ne '') {
+                if ($env{'form.scantronformat_del'}) {
+                    $confhash{'scantron'}{'scantronformat'} = '';
+                    $changes{'scantron'}{'scantronformat'} = 1;
+                }
+            }
+        }
+    }
+    if (keys(%confhash) > 0) {
+        my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
+                                                 $dom);
+        if ($putresult eq 'ok') {
+            if (keys(%changes) > 0) {
+                $resulttext = &mt('Changes made:').'<ul>';
+                if (ref($changes{'scantron'}) eq 'HASH') {
+                    if ($changes{'scantron'}{'scantronformat'}) { 
+                        if ($confhash{'scantron'}{'scantronformat'} eq '') {
+                            $resulttext .= '<li>'.&mt('[_1] scantron format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
+                        } else {
+                            $resulttext .= '<li>'.&mt('Custom scantron format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
+                        }
+                    }
+                }
+                $resulttext .= '</ul>';
+                &Apache::loncommon::devalidate_domconfig_cache($dom);
+            } else {
+                $resulttext = &mt('No changes made to scantron format file');
+            }
+        } else {
+            $resulttext = '<span class="LC_error">'.
+                &mt('An error occurred: [_1]',$putresult).'</span>';
+        }
+    } else {
+        $resulttext = &mt('No changes made to scantron format file'); 
+    }
+    if ($errors) {
+        $resulttext .= &mt('The following errors occurred: ').'<ul>'.
+                       $errors.'</ul>';
+    }
+    return $resulttext;
+}
+
 1;

--raeburn1208795036--