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

raeburn raeburn at source.lon-capa.org
Tue Dec 24 16:59:36 EST 2013


raeburn		Tue Dec 24 21:59:36 2013 EDT

  Modified files:              
    /loncom/interface	domainprefs.pm 
  Log:
  - Javascript to update select boxes when reordering available textbooks
    for use in "Textbook" courses.
  
  
Index: loncom/interface/domainprefs.pm
diff -u loncom/interface/domainprefs.pm:1.216 loncom/interface/domainprefs.pm:1.217
--- loncom/interface/domainprefs.pm:1.216	Tue Dec 24 19:15:10 2013
+++ loncom/interface/domainprefs.pm	Tue Dec 24 21:59:36 2013
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to set domain-wide configuration settings
 #
-# $Id: domainprefs.pm,v 1.216 2013/12/24 19:15:10 raeburn Exp $
+# $Id: domainprefs.pm,v 1.217 2013/12/24 21:59:36 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -2054,7 +2054,7 @@
     my $confname = $dom.'-domainconfig';
     my $switchserver = &check_switchserver($dom,$confname);
     my $maxnum = scalar(keys(%ordered));
-    my $datatable;
+    my $datatable = &textbookcourses_javascript(\%ordered);
     if (keys(%ordered)) {
         my @items = sort { $a <=> $b } keys(%ordered);
         for (my $i=0; $i<@items; $i++) {
@@ -2074,7 +2074,7 @@
                     $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
                 }
             }
-            my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'','$key','$i'".');"';
+            my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$key'".');"';
             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
                          .'<select name="'.$key.'"'.$chgstr.'>';
             for (my $k=0; $k<=$maxnum; $k++) {
@@ -2115,7 +2115,7 @@
         }
     }
     $css_class = $itemcount%2?' class="LC_odd_row"':'';
-    my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'','addbook_pos','$maxnum'".');"';
+    my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'addbook_pos'".');"';
     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
                   '<input type="hidden" name="book_maxnum" value="'.$maxnum.'" />'."\n".
                   '<select name="addbook_pos"'.$chgstr.'>';
@@ -2154,6 +2154,67 @@
     return $datatable;
 }
 
+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 $jstext = '    var textbooks = Array('."'".join("','", at jsarray)."'".');'."\n";
+    return <<"ENDSCRIPT";
+<script type="text/javascript">
+// <![CDATA[
+function reorderBooks(form,item) {
+    var changedVal;
+$jstext 
+    var newpos = 'addbook_pos';
+    var current = new Array;
+    var maxh = 1 + $num;
+    var current = new Array;
+    var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
+    if (item == newpos) {
+        changedVal = newitemVal;
+    } else {
+        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;
+            }
+        }
+    }
+    var oldVal;
+    for (var j=0; j<maxh; j++) {
+        if (current[j] == undefined) {
+            oldVal = j;
+        }
+    }
+    if (oldVal < changedVal) {
+        for (var k=oldVal+1; k<=changedVal ; k++) {
+           var elementName = current[k];
+           form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
+        }
+    } else {
+        for (var k=changedVal; k<oldVal; k++) {
+            var elementName = current[k];
+            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
+        }
+    }
+    return;
+}
+
+// ]]>
+</script>
+
+ENDSCRIPT
+}
+
 sub print_autoenroll {
     my ($dom,$settings,$rowtotal) = @_;
     my $autorun = &Apache::lonnet::auto_run(undef,$dom),




More information about the LON-CAPA-cvs mailing list