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

raeburn raeburn at source.lon-capa.org
Fri May 9 11:02:32 EDT 2014


raeburn		Fri May  9 15:02:32 2014 EDT

  Modified files:              
    /loncom/interface	domainprefs.pm lonrequestcourse.pm 
  Log:
  - "Textbook" course requests can specify cloning from one of two types of 
    prefabricated courses: either (a) "textbooks" or (b) "templates".
  
  
-------------- next part --------------
Index: loncom/interface/domainprefs.pm
diff -u loncom/interface/domainprefs.pm:1.241 loncom/interface/domainprefs.pm:1.242
--- loncom/interface/domainprefs.pm:1.241	Mon May  5 23:28:53 2014
+++ loncom/interface/domainprefs.pm	Fri May  9 15:02:31 2014
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to set domain-wide configuration settings
 #
-# $Id: domainprefs.pm,v 1.241 2014/05/05 23:28:53 raeburn Exp $
+# $Id: domainprefs.pm,v 1.242 2014/05/09 15:02:31 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -369,6 +369,8 @@
                               col2 => 'Value'},
                              {col1 => 'Available textbooks',
                               col2 => ''},
+                             {col1 => 'Available templates',
+                              col2 => ''},
                              {col1 => 'Validation (not official courses)',
                               col2 => 'Value'},],
                   print => \&print_quotas,
@@ -771,7 +773,18 @@
              <tr class="LC_info_row">
               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
-                       &print_textbookcourses($dom,$settings,\$rowtotal).'
+                       &textbookcourses_javascript($settings).
+                       &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
+            </table>
+           </td>
+          </tr>
+         <tr>
+           <td>
+            <table class="LC_nested">
+             <tr class="LC_info_row">
+              <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
+              <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
+                       &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
             </table>
            </td>
           </tr>
@@ -779,8 +792,8 @@
            <td>
             <table class="LC_nested">
              <tr class="LC_info_row">
-              <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
-              <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
+              <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
+              <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
              </tr>'.
             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
         } elsif ($action eq 'requestauthor') {
@@ -2093,14 +2106,14 @@
 }
 
 sub print_textbookcourses {
-    my ($dom,$settings,$rowtotal) = @_;
+    my ($dom,$type,$settings,$rowtotal) = @_;
     my $rownum = 0;
     my $css_class;
     my $itemcount = 1;
     my $maxnum = 0;
     my $bookshash;
     if (ref($settings) eq 'HASH') {
-        $bookshash = $settings->{'textbooks'};
+        $bookshash = $settings->{$type};
     }
     my %ordered;
     if (ref($bookshash) eq 'HASH') {
@@ -2113,8 +2126,8 @@
     }
     my $confname = $dom.'-domainconfig';
     my $switchserver = &check_switchserver($dom,$confname);
-    $maxnum = scalar(keys(%ordered));
-    my $datatable = &textbookcourses_javascript(\%ordered);
+    my $maxnum = scalar(keys(%ordered));
+    my $datatable;
     if (keys(%ordered)) {
         my @items = sort { $a <=> $b } keys(%ordered);
         for (my $i=0; $i<@items; $i++) {
@@ -2126,17 +2139,19 @@
             if (ref($bookshash->{$key}) eq 'HASH') {
                 $subject = $bookshash->{$key}->{'subject'};
                 $title = $bookshash->{$key}->{'title'};
-                $author = $bookshash->{$key}->{'author'};
-                $image = $bookshash->{$key}->{'image'};
-                if ($image ne '') {
-                    my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
-                    my $imagethumb = "$path/tn-".$imagefile;
-                    $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
+                if ($type eq 'textbooks') {
+                    $author = $bookshash->{$key}->{'author'};
+                    $image = $bookshash->{$key}->{'image'};
+                    if ($image ne '') {
+                        my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
+                        my $imagethumb = "$path/tn-".$imagefile;
+                        $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
+                    }
                 }
             }
-            my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$key'".');"';
+            my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
-                         .'<select name="'.$key.'"'.$chgstr.'>';
+                         .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
             for (my $k=0; $k<=$maxnum; $k++) {
                 my $vpos = $k+1;
                 my $selstr;
@@ -2146,39 +2161,41 @@
                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
             }
             $datatable .= '</select>'.(' 'x2).
-                '<label><input type="checkbox" name="book_del" value="'.$key.'" />'.
+                '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
                 &mt('Delete?').'</label></span></td>'.
                 '<td colspan="2">'.
-                '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="book_subject_'.$i.'" value="'.$subject.'" /></span> '.
-                (' 'x2).
-                '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="book_title_'.$i.'" value="'.$title.'" /></span> '.
+                '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
                 (' 'x2).
-                '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="book_author_'.$i.'" value="'.$author.'" /></span> '.
-                (' 'x2).
-                '<span class="LC_nobreak">'.&mt('Thumbnail:');
-            if ($image) {
-                $datatable .= '<span class="LC_nobreak">'.
-                              $imgsrc.
-                              '<label><input type="checkbox" name="book_image_del"'.
-                              ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
-                              '<span class="LC_nobreak"> '.&mt('Replace:').' ';
-            }
-            if ($switchserver) {
-                $datatable .= &mt('Upload to library server: [_1]',$switchserver);
-            } else {
-                $datatable .= '<input type="file" name="book_image_'.$i.'" value="" />';
+                '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
+            if ($type eq 'textbooks') {
+                $datatable .= (' 'x2).
+                              '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
+                              (' 'x2).
+                              '<span class="LC_nobreak">'.&mt('Thumbnail:');
+                if ($image) {
+                    $datatable .= '<span class="LC_nobreak">'.
+                                  $imgsrc.
+                                  '<label><input type="checkbox" name="'.$type.'_image_del"'.
+                                  ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
+                                  '<span class="LC_nobreak"> '.&mt('Replace:').' ';
+                }
+                if ($switchserver) {
+                    $datatable .= &mt('Upload to library server: [_1]',$switchserver);
+                } else {
+                    $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
+                }
             }
-            $datatable .= '<input type="hidden" name="book_id_'.$i.'" value="'.$key.'" /></span> '.
+            $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').' '.
                           $coursetitle.'</span></td></tr>'."\n";
             $itemcount ++;
         }
     }
     $css_class = $itemcount%2?' class="LC_odd_row"':'';
-    my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'addbook_pos'".');"';
+    my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
-                  '<input type="hidden" name="book_maxnum" value="'.$maxnum.'" />'."\n".
-                  '<select name="addbook_pos"'.$chgstr.'>';
+                  '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
+                  '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
     for (my $k=0; $k<$maxnum+1; $k++) {
         my $vpos = $k+1;
         my $selstr;
@@ -2188,26 +2205,28 @@
         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
     }
     $datatable .= '</select> '."\n".
-                  '<input type="checkbox" name="addbook" value="1" />'.&mt('Add').'</td>'."\n".
+                  '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
                   '<td colspan="2">'.
-                  '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="addbook_subject" value="" /></span> '."\n".
+                  '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
                   (' 'x2).
-                  '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="addbook_title" value="" /></span> '."\n".
-                  (' 'x2).
-                  '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="addbook_author" value="" /></span> '."\n".
-                  (' 'x2).
-                  '<span class="LC_nobreak">'.&mt('Image:').' ';
-    if ($switchserver) {
-        $datatable .= &mt('Upload to library server: [_1]',$switchserver);
-    } else {
-        $datatable .= '<input type="file" name="addbook_image" value="" />';
+                  '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
+                  (' 'x2);
+    if ($type eq 'textbooks') {
+        $datatable .= '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
+                      (' 'x2).
+                      '<span class="LC_nobreak">'.&mt('Image:').' ';
+        if ($switchserver) {
+            $datatable .= &mt('Upload to library server: [_1]',$switchserver);
+        } else {
+            $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
+        }
     }
     $datatable .= '</span>'."\n".
                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').' '.
-                  &Apache::loncommon::select_dom_form($env{'request.role.domain'},'addbook_cdom').
-                  '<input type="text" size="25" name="addbook_cnum" value="" />'.
+                  &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
+                  '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
                   &Apache::loncommon::selectcourse_link
-                      ('display','addbook_cnum','addbook_cdom',undef,undef,undef,'Course');
+                      ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
                   '</span></td>'."\n".
                   '</tr>'."\n";
     $itemcount ++;
@@ -2215,23 +2234,43 @@
 }
 
 sub textbookcourses_javascript {
-    my ($textbooks) = @_;
-    return unless(ref($textbooks) eq 'HASH');
-    my $num = scalar(keys(%{$textbooks}));
-    my @jsarray;
-    foreach my $item (sort {$a <=> $b } (keys(%{$textbooks}))) {
-        push(@jsarray,$textbooks->{$item});
+    my ($settings) = @_;
+    return unless(ref($settings) eq 'HASH');
+    my (%ordered,%total,%jstext);
+    foreach my $type ('textbooks','templates') {
+        $total{$type} = 0;
+        if (ref($settings->{$type}) eq 'HASH') {
+            foreach my $item (keys(%{$settings->{$type}})) {
+                if (ref($settings->{$type}->{$item}) eq 'HASH') {
+                    my $num = $settings->{$type}->{$item}{'order'};
+                    $ordered{$type}{$num} = $item;
+                }
+            }
+            $total{$type} = scalar(keys(%{$settings->{$type}}));
+        }
+        my @jsarray = ();
+        foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
+            push(@jsarray,$ordered{$type}{$item});
+        }
+        $jstext{$type} = '    var '.$type.' = Array('."'".join("','", at jsarray)."'".');'."\n";
     }
-    my $jstext = '    var textbooks = Array('."'".join("','", at jsarray)."'".');'."\n";
     return <<"ENDSCRIPT";
 <script type="text/javascript">
 // <![CDATA[
-function reorderBooks(form,item) {
+function reorderBooks(form,item,caller) {
     var changedVal;
-$jstext 
-    var newpos = 'addbook_pos';
+$jstext{'textbooks'};
+$jstext{'templates'};
+    var newpos;
+    var maxh;
+    if (caller == 'textbooks') {  
+        newpos = 'textbooks_addbook_pos';
+        maxh = 1 + $total{'textbooks'};
+    } else {
+        newpos = 'templates_addbook_pos';
+        maxh = 1 + $total{'templates'};
+    }
     var current = new Array;
-    var maxh = 1 + $num;
     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
     if (item == newpos) {
         changedVal = newitemVal;
@@ -2239,12 +2278,25 @@
         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
         current[newitemVal] = newpos;
     }
-    for (var i=0; i<textbooks.length; i++) {
-        var elementName = textbooks[i];
-        if (elementName != item) {
-            if (form.elements[elementName]) {
-                var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
-                current[currVal] = elementName;
+    if (caller == 'textbooks') {
+        for (var i=0; i<textbooks.length; i++) {
+            var elementName = 'textbooks_'+textbooks[i];
+            if (elementName != item) {
+                if (form.elements[elementName]) {
+                    var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
+                    current[currVal] = elementName;
+                }
+            }
+        }
+    }
+    if (caller == 'templates') {
+        for (var i=0; i<templates.length; i++) {
+            var elementName = 'templates_'+templates[i];
+            if (elementName != item) {
+                if (form.elements[elementName]) {
+                    var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
+                    current[currVal] = elementName;
+                }
             }
         }
     }
@@ -6830,29 +6882,35 @@
                 $confhash{'uniquecode'}{$type} = 1;
             }
         }
-        my ($newbook, at allpos);
+        my (%newbook,%allpos);
         if ($context eq 'requestcourses') {
-            if ($env{'form.addbook'}) {
-                if (($env{'form.addbook_cnum'} =~ /^$match_courseid$/) &&
-                    ($env{'form.addbook_cdom'} =~ /^$match_domain$/)) {
-                    if (&Apache::lonnet::homeserver($env{'form.addbook_cnum'},
-                                                    $env{'form.addbook_cdom'}) eq 'no_host') {
-                        $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
-                                   '</span></li>';
-                    } else {
-                        $newbook = $env{'form.addbook_cdom'}.'_'.$env{'form.addbook_cnum'};
-                        my $position = $env{'form.addbook_pos'};
-                        $position =~ s/\D+//g;
-                        if ($position ne '') {
-                            $allpos[$position] = $newbook;
+            foreach my $type ('textbooks','templates') {
+                @{$allpos{$type}} = (); 
+                my $invalid;
+                if ($type eq 'textbooks') {
+                    $invalid = &mt('Invalid LON-CAPA course for textbook');
+                } else {
+                    $invalid = &mt('Invalid LON-CAPA course for template');
+                }
+                if ($env{'form.'.$type.'_addbook'}) {
+                    if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
+                        ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
+                        if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
+                                                        $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
+                            $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
+                        } else {
+                            $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
+                            my $position = $env{'form.'.$type.'_addbook_pos'};
+                            $position =~ s/\D+//g;
+                            if ($position ne '') {
+                                $allpos{$type}[$position] = $newbook{$type};
+                            }
                         }
+                    } else {
+                        $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
                     }
-                } else {
-                    $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
-                               '</span></li>';
                 }
-            }
-
+            } 
         }
         if (ref($domconfig{$action}) eq 'HASH') {
             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
@@ -6885,56 +6943,60 @@
                 $changes{'uniquecode'} = 1;
             }
             if ($context eq 'requestcourses') {
-                if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
-                    my %deletions;
-                    my @todelete = &Apache::loncommon::get_env_multiple('form.book_del');
-                    if (@todelete) {
-                        map { $deletions{$_} = 1; } @todelete;
-                    }
-                    my %imgdeletions;
-                    my @todeleteimages = &Apache::loncommon::get_env_multiple('form.book_image_del');
-                    if (@todeleteimages) {
-                        map { $imgdeletions{$_} = 1; } @todeleteimages;
-                    }
-                    my $maxnum = $env{'form.book_maxnum'};
-                    for (my $i=0; $i<=$maxnum; $i++) {
-                        my $key = $env{'form.book_id_'.$i};
-                        if (ref($domconfig{$action}{'textbooks'}{$key}) eq 'HASH') {
-                            if ($deletions{$key}) {
-                                if ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
-                                    #FIXME need to obsolete item in RES space
+                foreach my $type ('textbooks','templates') {
+                    if (ref($domconfig{$action}{$type}) eq 'HASH') {
+                        my %deletions;
+                        my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
+                        if (@todelete) {
+                            map { $deletions{$_} = 1; } @todelete;
+                        }
+                        my %imgdeletions;
+                        my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
+                        if (@todeleteimages) {
+                            map { $imgdeletions{$_} = 1; } @todeleteimages;
+                        }
+                        my $maxnum = $env{'form.'.$type.'_maxnum'};
+                        for (my $i=0; $i<=$maxnum; $i++) {
+                            my $itemid = $env{'form.'.$type.'_id_'.$i};
+                            my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
+                            if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
+                                if ($deletions{$key}) {
+                                    if ($domconfig{$action}{$type}{$key}{'image'}) {
+                                        #FIXME need to obsolete item in RES space
+                                    }
+                                    next;
+                                } else {
+                                    my $newpos = $env{'form.'.$itemid};
+                                    $newpos =~ s/\D+//g;
+                                    foreach my $item ('subject','title','author') {
+                                        next if (($item eq 'author') && ($type eq 'templates'));
+                                        $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
+                                        if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
+                                            $changes{$type}{$key} = 1;
+                                        }
+                                    }
+                                    $allpos{$type}[$newpos] = $key;
                                 }
-                                next;
-                            } else {
-                                my $newpos = $env{'form.'.$key};
-                                $newpos =~ s/\D+//g;
-                                foreach my $item ('subject','title','author') {
-                                    $confhash{'textbooks'}{$key}{$item} = $env{'form.book_'.$item.'_'.$i};
-                                    if ($domconfig{$action}{'textbooks'}{$key}{$item} ne $confhash{'textbooks'}{$key}{$item}) {
-                                        $changes{'textbooks'}{$key} = 1;
+                                if ($imgdeletions{$key}) {
+                                    $changes{$type}{$key} = 1;
+                                    #FIXME need to obsolete item in RES space
+                                } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
+                                    my ($cdom,$cnum) = split(/_/,$key);
+                                    my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
+                                                                                  $cdom,$cnum,$type,$configuserok,
+                                                                                  $switchserver,$author_ok);
+                                    if ($imgurl) {
+                                        $confhash{$type}{$key}{'image'} = $imgurl;
+                                        $changes{$type}{$key} = 1; 
                                     }
+                                    if ($error) {
+                                        &Apache::lonnet::logthis($error);
+                                        $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
+                                    } 
+                                } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
+                                    $confhash{$type}{$key}{'image'} = 
+                                        $domconfig{$action}{$type}{$key}{'image'};
                                 }
-                                $allpos[$newpos] = $key;
-                            }
-                            if ($imgdeletions{$key}) {
-                                $changes{'textbooks'}{$key} = 1;
-                                #FIXME need to obsolete item in RES space
-                            } elsif ($env{'form.book_image_'.$i.'.filename'}) {
-                                my ($cdom,$cnum) = split(/_/,$key);
-                                my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,'book_image_'.$i,
-                                                                              $cdom,$cnum,$configuserok,
-                                                                              $switchserver,$author_ok);
-                                if ($imgurl) {
-                                    $confhash{'textbooks'}{$key}{'image'} = $imgurl;
-                                    $changes{'textbooks'}{$key} = 1; 
-                                }
-                                if ($error) {
-                                    &Apache::lonnet::logthis($error);
-                                    $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
-                                } 
-                            } elsif ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
-                                $confhash{'textbooks'}{$key}{'image'} = 
-                                    $domconfig{$action}{'textbooks'}{$key}{'image'};
                             }
                         }
                     }
@@ -6949,43 +7011,48 @@
             }
         }
         if ($context eq 'requestcourses') {
-            if ($newbook) {
-                $changes{'textbooks'}{$newbook} = 1;
-                foreach my $item ('subject','title','author') {
-                    $env{'form.addbook_'.$item} =~ s/(`)/'/g;
-                    if ($env{'form.addbook_'.$item}) {
-                        $confhash{'textbooks'}{$newbook}{$item} = $env{'form.addbook_'.$item};
-                    }
-                }
-                if ($env{'form.addbook_image.filename'} ne '') {
-                    my ($cdom,$cnum) = split(/_/,$newbook);
-                    my ($imageurl,$error) =
-                        &process_textbook_image($r,$dom,$confname,'addbook_image',$cdom,$cnum,$configuserok,
-                                                $switchserver,$author_ok);
-                    if ($imageurl) {
-                        $confhash{'textbooks'}{$newbook}{'image'} = $imageurl;
-                    }
-                    if ($error) {
-                        &Apache::lonnet::logthis($error);
-                        $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
+            foreach my $type ('textbooks','templates') {
+                if ($newbook{$type}) {
+                    $changes{$type}{$newbook{$type}} = 1;
+                    foreach my $item ('subject','title','author') {
+                        next if (($item eq 'author') && ($type eq 'template'));
+                        $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
+                        if ($env{'form.'.$type.'_addbook_'.$item}) {
+                            $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
+                        }
+                    }
+                    if ($type eq 'textbooks') {
+                        if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
+                            my ($cdom,$cnum) = split(/_/,$newbook{$type});
+                            my ($imageurl,$error) =
+                                &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
+                                                        $configuserok,$switchserver,$author_ok);
+                            if ($imageurl) {
+                                $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
+                            }
+                            if ($error) {
+                                &Apache::lonnet::logthis($error);
+                                $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
+                            }
+                        }
                     }
                 }
-            }
-            if (@allpos > 0) {
-                my $idx = 0;
-                foreach my $item (@allpos) {
-                    if ($item ne '') {
-                        $confhash{'textbooks'}{$item}{'order'} = $idx;
-                        if (ref($domconfig{$action}) eq 'HASH') {
-                            if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
-                                if (ref($domconfig{$action}{'textbooks'}{$item}) eq 'HASH') {
-                                    if ($domconfig{$action}{'textbooks'}{$item}{'order'} ne $idx) {
-                                        $changes{'textbooks'}{$item} = 1;
+                if (@{$allpos{$type}} > 0) {
+                    my $idx = 0;
+                    foreach my $item (@{$allpos{$type}}) {
+                        if ($item ne '') {
+                            $confhash{$type}{$item}{'order'} = $idx;
+                            if (ref($domconfig{$action}) eq 'HASH') {
+                                if (ref($domconfig{$action}{$type}) eq 'HASH') {
+                                    if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
+                                        if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
+                                            $changes{$type}{$item} = 1;
+                                        }
                                     }
                                 }
                             }
+                            $idx ++;
                         }
-                        $idx ++;
                     }
                 }
             }
@@ -7202,7 +7269,7 @@
         $domdefaults{'requestauthor'} = \%confhash;
     } else {
         foreach my $key (keys(%confhash)) {
-            unless (($context eq 'requestcourses') && ($key eq 'textbooks')) {
+            unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
                 $domdefaults{$key} = $confhash{$key};
             }
         }
@@ -7354,27 +7421,32 @@
                                        '</li>';
                     }
                 }
-                if (ref($changes{'textbooks'}) eq 'HASH') {
-                    $resulttext .= '<li>'.&mt('Available textbooks updated').'<ul>';
-                    foreach my $key (sort(keys(%{$changes{'textbooks'}}))) {
-                        my %coursehash = &Apache::lonnet::coursedescription($key);
-                        my $coursetitle = $coursehash{'description'};
-                        my $position = $confhash{'textbooks'}{$key}{'order'} + 1;
-                        $resulttext .= '<li>';
-                        foreach my $item ('subject','title','author') {
-                            my $name = $item.':';
-                            $name =~ s/^(\w)/\U$1/;
-                            $resulttext .= &mt($name).' '.$confhash{'textbooks'}{$key}{$item}.'<br />';
-                        }
-                        $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
-                        if ($confhash{'textbooks'}{$key}{'image'}) {
-                            $resulttext .= ' '.&mt('Image: [_1]',
-                                               '<img src="'.$confhash{'textbooks'}{$key}{'image'}.'"'.
-                                               ' alt="Textbook cover" />').'<br />';
+                foreach my $type ('textbooks','templates') {
+                    if (ref($changes{$type}) eq 'HASH') {
+                        $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
+                        foreach my $key (sort(keys(%{$changes{$type}}))) {
+                            my %coursehash = &Apache::lonnet::coursedescription($key);
+                            my $coursetitle = $coursehash{'description'};
+                            my $position = $confhash{$type}{$key}{'order'} + 1;
+                            $resulttext .= '<li>';
+                            foreach my $item ('subject','title','author') {
+                                next if (($item eq 'author') && ($type eq 'templates'));
+                                my $name = $item.':';
+                                $name =~ s/^(\w)/\U$1/;
+                                $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
+                            }
+                            $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
+                            if ($type eq 'textbooks') {
+                                if ($confhash{$type}{$key}{'image'}) {
+                                    $resulttext .= ' '.&mt('Image: [_1]',
+                                                   '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
+                                                   ' alt="Textbook cover" />').'<br />';
+                                }
+                            }
+                            $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
                         }
-                        $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
+                        $resulttext .= '</ul></li>';
                     }
-                    $resulttext .= '</ul></li>';
                 }
                 if (ref($changes{'validation'}) eq 'HASH') {
                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
@@ -7422,7 +7494,7 @@
 }
 
 sub process_textbook_image {
-    my ($r,$dom,$confname,$caller,$cdom,$cnum,$configuserok,$switchserver,$author_ok) = @_;
+    my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
     my $filename = $env{'form.'.$caller.'.filename'};
     my ($error,$url);
     my ($width,$height) = (50,50);
@@ -7433,7 +7505,7 @@
         } elsif ($author_ok eq 'ok') {
             my ($result,$imageurl) =
                 &publishlogo($r,'upload',$caller,$dom,$confname,
-                             "textbooks/$dom/$cnum/cover",$width,$height);
+                             "$type/$dom/$cnum/cover",$width,$height);
             if ($result eq 'ok') {
                 $url = $imageurl;
             } else {
Index: loncom/interface/lonrequestcourse.pm
diff -u loncom/interface/lonrequestcourse.pm:1.80 loncom/interface/lonrequestcourse.pm:1.81
--- loncom/interface/lonrequestcourse.pm:1.80	Tue May  6 20:42:27 2014
+++ loncom/interface/lonrequestcourse.pm	Fri May  9 15:02:31 2014
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Request a course
 #
-# $Id: lonrequestcourse.pm,v 1.80 2014/05/06 20:42:27 raeburn Exp $
+# $Id: lonrequestcourse.pm,v 1.81 2014/05/09 15:02:31 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -4393,25 +4393,26 @@
 
 sub print_textbook_form {
     my ($r,$dom,$incdoms,$domdefs,$settings,$can_request) = @_;
-    my ($bookshash,%ordered);
+    my (%prefab,%ordered,%numprefab);
     my $crstype = 'textbook';
 #
-#  Retrieve list of textbook courses cloneable by user
+#  Retrieve list of prefabricated courses (textbook courses and templates) cloneable by user
 #
-    my $numbook = 0;
-    if (ref($settings) eq 'HASH') {
-        $bookshash = $settings->{'textbooks'};
-        if (ref($bookshash) eq 'HASH') {
-            foreach my $item (keys(%{$bookshash})) {
-                my ($clonedom,$clonecrs) = split(/_/,$item);
-                if (ref($bookshash->{$item}) eq 'HASH') {
+    foreach my $type ('textbooks','templates') {
+        $numprefab{$type} = 0;
+        if (ref($settings) eq 'HASH') {
+            $prefab{$type} = $settings->{$type};
+            if (ref($prefab{$type}) eq 'HASH') {
+                foreach my $item (keys(%{$prefab{$type}})) {
                     my ($clonedom,$clonecrs) = split(/_/,$item);
-                    if (&Apache::loncoursequeueadmin::can_clone_course($env{'user.name'},
-                                        $env{'user.domain'},$clonecrs,$clonedom,$crstype)) {
-
-                        my $num = $bookshash->{$item}{'order'};
-                        $ordered{$num} = $item;
-                        $numbook ++;
+                    if (ref($prefab{$type}{$item}) eq 'HASH') {
+                        if (&Apache::loncoursequeueadmin::can_clone_course($env{'user.name'},
+                                          $env{'user.domain'},$clonecrs,$clonedom,$crstype)) {
+
+                            my $num = $prefab{$type}{$item}{'order'};
+                            $ordered{$type}{$num} = $item;
+                            $numprefab{$type} ++;
+                        }
                     }
                 }
             }
@@ -4447,7 +4448,7 @@
 
     my $numcurrent = scalar(keys(%cloneable));
 
-    my $jscript = &textbook_request_javascript($numbook,$numcurrent);
+    my $jscript = &textbook_request_javascript(\%numprefab,$numcurrent);
     my %loaditems;
     $loaditems{'onload'} = 'javascript:uncheckAllRadio();';
     $r->print(&header('Course Request',$jscript,\%loaditems));
@@ -4498,9 +4499,16 @@
         $r->print('<div>'. 
                   '<fieldset><legend>'.&mt('Course Content').'</legend>');
         if (keys(%ordered)) {
-            $r->print('<span class="LC_nobreak"><label>'.
-                      '<input type="radio" name="cloning" value="textbook" onclick="javascript:cloneChoice();" />'.
-                      &mt('Load textbook content').'</span>'.(' 'x2).' ');
+            if (ref($ordered{'textbooks'}) eq 'HASH') {
+                $r->print('<span class="LC_nobreak"><label>'.
+                          '<input type="radio" name="cloning" value="textbook" onclick="javascript:cloneChoice();" />'.
+                          &mt('Load textbook content').'</span>'.(' 'x2).' ');
+            }
+            if (ref($ordered{'templates'}) eq 'HASH') {
+                $r->print('<span class="LC_nobreak"><label>'.
+                          '<input type="radio" name="cloning" value="template" onclick="javascript:cloneChoice();" />'.
+                          &mt('Load template content').'</span>'.(' 'x2).' ');
+            }
         }
         if (keys(%cloneable)) {
             $r->print('<span class="LC_nobreak"><label>'.
@@ -4518,31 +4526,49 @@
 # Table of cloneable textbook courses
 #
     if (keys(%ordered)) {
-        $r->print('<div id="showtextbook" style="display:none">'.
-                  &Apache::loncommon::start_data_table().
-                  &Apache::loncommon::start_data_table_header_row().
-                  '<th>'.&mt('Title').'</th>'.
-                  '<th>'.&mt('Author(s)').'</th>'.
-                  '<th>'.&mt('Subject').'</th>'.
-                  '<th>'.&mt('Book').'</th>'.
-                  &Apache::loncommon::end_data_table_header_row());
-        my @items = sort { $a <=> $b } keys(%ordered);
-        foreach my $num (@items) {
-            my $item = $ordered{$num};
-            my $cleantitle=&HTML::Entities::encode($bookshash->{$item}->{'title'},'<>&"');
-            $cleantitle=~s/'/\\'/g;
-            $cleantitle =~ s/^\s+//;
-            $r->print(&Apache::loncommon::start_data_table_row().
-                      '<td><label><input type="radio" name="book" value="'.$item.'" />'.
-                      $cleantitle.'</label></td>'.
-                      '<td>'.$bookshash->{$item}->{'author'}.'</td>'.
-                      '<td>'.$bookshash->{$item}->{'subject'}.'</td>'.
-                      '<td><img border="0" src="'.$bookshash->{$item}->{'image'}.
-                      '" alt="'.$cleantitle.'" /></td>'. 
-                      &Apache::loncommon::end_data_table_row());
+        foreach my $type ('textbooks','templates') {
+            my $divid = 'showtextbook';
+            my $radioid = 'book';
+            if ($type eq 'templates') {
+                $divid = 'showtemplate'; 
+                $radioid = 'template';
+            }
+            if (ref($ordered{$type}) eq 'HASH') {
+                $r->print('<div id="'.$divid.'" style="display:none">'.
+                          &Apache::loncommon::start_data_table().
+                          &Apache::loncommon::start_data_table_header_row().
+                          '<th>'.&mt('Title').'</th>');
+                if ($type eq 'textbooks') {
+                    $r->print('<th>'.&mt('Author(s)').'</th>');
+                }
+                $r->print('<th>'.&mt('Subject').'</th>');
+                if ($type eq 'textbooks') {
+                    $r->print('<th>'.&mt('Book').'</th>');
+                }
+                $r->print(&Apache::loncommon::end_data_table_header_row());
+                my @items = sort { $a <=> $b } keys(%{$ordered{$type}});
+                foreach my $num (@items) {
+                    my $item = $ordered{$type}{$num};
+                    my $cleantitle=&HTML::Entities::encode($prefab{$type}{$item}{'title'},'<>&"');
+                    $cleantitle=~s/'/\\'/g;
+                    $cleantitle =~ s/^\s+//;
+                    $r->print(&Apache::loncommon::start_data_table_row().
+                              '<td><label><input type="radio" name="'.$radioid.'" value="'.$item.'" />'.
+                              $cleantitle.'</label></td>');
+                    if ($type eq 'textbooks') {
+                         $r->print('<td>'.$prefab{$type}{$item}{'author'}.'</td>');
+                    }
+                    $r->print('<td>'.$prefab{$type}{$item}{'subject'}.'</td>');
+                    if ($type eq 'textbooks') {
+                        $r->print('<td><img border="0" src="'.$prefab{$type}{$item}{'image'}.
+                                  '" alt="'.$cleantitle.'" /></td>');
+                    }
+                    $r->print(&Apache::loncommon::end_data_table_row());
+                }
+                $r->print(&Apache::loncommon::end_data_table().
+                          '</div>');
+            }
         }
-        $r->print(&Apache::loncommon::end_data_table().
-                  '</div>');
     }
 
 #
@@ -4710,6 +4736,8 @@
     my ($clonefrom,$clonedom,$clonecrs);
     if ($reqtype eq 'textbook') {
         $clonefrom = $env{'form.book'};
+    } elsif ($reqtype eq 'template') {
+        $clonefrom = $env{'form.template'};
     } elsif ($reqtype eq 'existing') {
         $clonefrom = $env{'form.owned'};
     }
@@ -4781,21 +4809,23 @@
 }
 
 sub textbook_request_javascript {
-    my ($numbook,$numcurrent) = @_;
+    my ($numprefab,$numcurrent) = @_;
+    return unless (ref($numprefab) eq 'HASH');
+    return if (!$numprefab->{'textbooks'} && !$numprefab->{'templates'} && !$numcurrent);
     my %lt = &Apache::lonlocal::texthash(
                  choose   => 'Please select a content option.',
                  textbook => 'Please select a textbook, or choose a different option.',
+                 template => 'Please select a template, or choose a different option.',        
                  existing => 'Please select one of your existing courses to copy, or choose a different option.',
                  title    => 'Please enter a course title.',
              );
-    return if (!$numbook && !$numcurrent);
     return <<"ENDSCRIPT";
 function cloneChoice() {
     if (document.requestcourse.cloning) {
         var radioLength = document.requestcourse.cloning.length;
         if (radioLength == undefined) {
             var val = document.requestcourse.cloning.value;
-            if ((val == 'textbook') || (val == 'existing')) {
+            if ((val == 'textbook') || (val == 'template') || (val == 'existing')) {
                 var elem = document.getElementById('show'+val);
                 if (document.requestcourse.cloning.checked) {
                     elem.style.display = 'block';
@@ -4807,7 +4837,7 @@
         } else {
             for (var i=0; i<radioLength; i++) {
                 var val = document.requestcourse.cloning[i].value;
-                if ((val == 'textbook') || (val == 'existing')) {
+                if ((val == 'textbook') || (val == 'template') || (val == 'existing')) {
                     var elem = document.getElementById('show'+val);
                     if (document.requestcourse.cloning[i].checked) {
                         elem.style.display = 'block';
@@ -4815,6 +4845,9 @@
                         if (val == 'textbook') {
                             uncheckRadio('book');
                         }
+                        if (val == 'template') {
+                            uncheckRadio('template');
+                        }
                         if (val == 'existing') {
                             uncheckRadio('owned');
                         }
@@ -4842,11 +4875,15 @@
 
 function uncheckAllRadio() {
     uncheckRadio('cloning');
-    var numbook = $numbook;
+    var numbook = $numprefab->{'textbooks'};
+    var numtemplate = $numprefab->{'templates'};
     var numcurrent = $numcurrent;
     if (numbook > 0) {
         uncheckRadio('textbook'); 
     }
+    if (nutemplate > 0) {
+        uncheckRadio('template');
+    }m
     if (numcurrent > 0) {
         uncheckRadio('existing');
     }
@@ -4877,12 +4914,16 @@
             }
         }
         var group;
-        if ((cloneChoice == 'textbook') || (cloneChoice == 'existing')) {
+        if ((cloneChoice == 'textbook') || (cloneChoice == 'template') || (cloneChoice == 'existing')) {
             var group;
             if (cloneChoice == 'textbook') {
                 group = document.getElementsByName('book');
             } else {
-                group = document.getElementsByName('owned');
+                if (cloneChoice == 'template') {
+                    group = document.getElementsByName('template');
+                } else {
+                    group = document.getElementsByName('owned');
+                }
             }
             var groupLength = group.length;
             var chosen = 0;
@@ -4902,7 +4943,11 @@
                if (cloneChoice == 'textbook') {
                    alert("$lt{'textbook'}");
                } else {
-                   alert("$lt{'existing'}");
+                   if (cloneChoice == 'template') {
+                       alert("$lt{'template'}");
+                   } else {
+                       alert("$lt{'existing'}");
+                   }
                }
                return false;
             }


More information about the LON-CAPA-cvs mailing list