--- loncom/interface/lonhtmlcommon.pm	2009/10/31 14:27:48	1.237
+++ loncom/interface/lonhtmlcommon.pm	2010/01/07 16:15:59	1.259
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common html routines
 #
-# $Id: lonhtmlcommon.pm,v 1.237 2009/10/31 14:27:48 raeburn Exp $
+# $Id: lonhtmlcommon.pm,v 1.259 2010/01/07 16:15:59 faziophi Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -62,6 +62,22 @@ use Apache::lonlocal;
 use Apache::lonnet;
 use LONCAPA;
 
+
+sub coursepreflink {
+   my ($text,$category)=@_;
+   if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
+      return '<a href="/adm/courseprefs?phase=display&actions='.$category.'">'.$text.'</a>';
+   } else {
+      return '';
+   }
+}
+
+sub raw_href_to_link {
+   my ($message)=@_;
+   $message=~s/(https?\:\/\/[^\s\'\"]+)(\s|$)/<a href="$1"><tt>$1<\/tt><\/a>$2/gi;
+   return $message;
+}
+
 ##############################################
 ##############################################
 
@@ -111,7 +127,7 @@ sub dragmath_button {
     }
     my $buttontext=&mt('Edit Math');
     return <<ENDDRAGMATH;
-                <input type="button" value="$buttontext", onclick="javascript:mathedit('$textarea',document)" />$help_text
+                <input type="button" value="$buttontext" onclick="javascript:mathedit('$textarea',document)" />$help_text
 ENDDRAGMATH
 }
 
@@ -222,6 +238,9 @@ sub select_recent {
 	unless ($value =~/^error\:/) {
 	    my $escaped = &Apache::loncommon::escape_url($value);
 	    &Apache::loncommon::inhibit_menu_check(\$escaped);
+            if ($area eq 'residx') {
+                next if ((!&Apache::lonnet::allowed('bre',$value)) && (!&Apache::lonnet::allowed('bro',$value)));
+            }
 	    $return.="\n<option value='$escaped'>".
 		&unescape((split(/\&/,$recent{$value}))[1]).
 		'</option>';
@@ -364,7 +383,8 @@ dname_hour, dname_min, and dname_sec.
 
 The current setting for this time parameter.  A unix format time
 (time in seconds since the beginning of Jan 1st, 1970, GMT.  
-An undefined value is taken to indicate the value is the current time.
+An undefined value is taken to indicate the value is the current time
+unless it is requested to leave it empty. See $includeempty.
 Also, to be explicit, a value of 'now' also indicates the current time.
 
 =item $special
@@ -374,6 +394,9 @@ the date_setter.  See lonparmset for exa
 
 =item $includeempty 
 
+If it is set (true) and no date/time value is provided,
+the date/time fields are left empty.
+
 =item $state
 
 Specifies the initial state of the form elements.  Either 'disabled' or empty.
@@ -393,7 +416,11 @@ sub date_setter {
     my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
         $no_hh_mm_ss,$defhour,$defmin,$defsec,$nolink) = @_;
     my $now = time;
-    my $wasdefined=1;
+
+    my $tzname;
+    my ($sec,$min,$hour,$mday,$month,$year) = ('', '', undef,''.''.'');
+    #other potentially useful values:    wkday,yrday,is_daylight_savings
+
     if (! defined($state) || $state ne 'disabled') {
         $state = '';
     }
@@ -401,33 +428,24 @@ sub date_setter {
         $no_hh_mm_ss = 0;
     }
     if ($currentvalue eq 'now') {
-	$currentvalue = $now;
+        $currentvalue = $now;
     }
-    if ((!defined($currentvalue)) || ($currentvalue eq '')) {
-	$wasdefined=0;
-	if ($includeempty) {
-	    $currentvalue = 0;
-	} else {
-	    $currentvalue = $now;
-	}
+    
+    # Default value: Set empty date field to current time
+    # unless empty inclusion is requested
+    if ((!$includeempty) && (!$currentvalue)) {
+        $currentvalue = $now;
     }
-    # other potentially useful values:     wkday,yrday,is_daylight_savings
-    my $tzname;
-    my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
+    # Do we have a date? Split it!
     if ($currentvalue) {
-        ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($currentvalue); 
-    }
-    unless ($wasdefined) {
-        ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($now);
-	if (($defhour) || ($defmin) || ($defsec)) {
-	    $sec=($defsec?$defsec:0);
-	    $min=($defmin?$defmin:0);
-	    $hour=($defhour?$defhour:0);
-	} elsif (!$includeempty) {
-	    $sec=0;
-	    $min=0;
-	    $hour=0;
-	}
+	($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($currentvalue);
+
+        #No values provided for hour, min, sec? Use default 0
+        if (($defhour) || ($defmin) || ($defsec)) {
+            $sec  = ($defsec  ? $defsec  : 0);
+            $min  = ($defmin  ? $defmin  : 0);
+            $hour = ($defhour ? $defhour : 0);
+        }
     }
     my $result = "\n<!-- $dname date setting form -->\n";
     $result .= <<ENDJS;
@@ -1117,47 +1135,45 @@ sub r_print {
 # ------------------------------------------------------- Puts directory header
 
 sub crumbs {
-    my ($uri,$target,$prefix,$form,$size,$noformat,$skiplast)=@_;
-    if (! defined($size)) {
-        $size = '+2';
-    }
+    my ($uri,$target,$prefix,$form,$skiplast)=@_;
     if ($target) {
         $target = ' target="'.
                   &Apache::loncommon::escape_single($target).'"';
     }
-    my $output='';
-    unless ($noformat) { $output.='<br /><tt><b>'; }
-    $output.='<font size="'.$size.'">'.$prefix.'/';
-    if ($env{'user.adv'}) {
-	my $path=$prefix.'/';
-	foreach my $dir (split('/',$uri)) {
+    my $output='<span class="LC_filename">';
+    $output.=$prefix.'/';
+    if (($env{'user.adv'}) || ($env{'user.author'})) {
+        my $path=$prefix.'/';
+        foreach my $dir (split('/',$uri)) {
             if (! $dir) { next; }
             $path .= $dir;
-	    if ($path eq $uri) {
-		if ($skiplast) {
-		    $output.=$dir;
+            if ($path eq $uri) {
+                if ($skiplast) {
+                    $output.=$dir;
                     last;
-		} 
-	    } else {
-		$path.='/'; 
-	    }	    
+                } 
+            } else {
+                $path.='/'; 
+            }
             my $href_path = &HTML::Entities::encode($path,'<>&"');
-	    &Apache::loncommon::inhibit_menu_check(\$href_path);
-	    if ($form) {
-	        my $href = 'javascript:'.$form.".action='".$href_path."';".$form.'.submit();';
-	        $output.=qq{<a href="$href" $target>$dir</a>/};
-	    } else {
-	        $output.=qq{<a href="$href_path" $target>$dir</a>/};
-	    }
-	}
+            &Apache::loncommon::inhibit_menu_check(\$href_path);
+            if ($form) {
+                my $href = 'javascript:'.$form.".action='".$href_path."';".$form.'.submit();';
+                $output.=qq{<a href="$href"$target>$dir</a>/};
+            } else {
+                $output.=qq{<a href="$href_path"$target>$dir</a>/};
+            }
+        }
     } else {
-	foreach my $dir (split('/',$uri)) {
+        foreach my $dir (split('/',$uri)) {
             if (! $dir) { next; }
-	    $output.=$dir.'/';
-	}
+            $output.=$dir.'/';
+        }
     }
     if ($uri !~ m|/$|) { $output=~s|/$||; }
-    return $output.'</font>'.($noformat?'':'</b></tt><br />');
+    $output.='</span>';
+
+    return $output;
 }
 
 # --------------------- A function that generates a window for the spellchecker
@@ -1224,11 +1240,19 @@ ENDLINK
 }
 
 sub htmlareaheaders {
-    return if (&htmlareablocked());
-    return if (!&htmlareabrowser());
-    return (<<ENDHEADERS);
+	my $s="";
+	if (!&htmlareablocked() && &htmlareabrowser()) {
+		$s.=(<<ENDEDITOR);
 <script type="text/javascript" src="/fckeditor/fckeditor.js"></script>
-ENDHEADERS
+<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
+ENDEDITOR
+	}
+    $s.=(<<ENDJQUERY);
+<script type="text/javascript" src="/adm/jQuery/js/jquery-1.3.2.min.js"></script>
+<script type="text/javascript" src="/adm/jQuery/js/jquery-ui-1.7.2.custom.min.js"></script>
+<link rel="stylesheet" type="text/css" href="/adm/jQuery/css/smoothness/jquery-ui-1.7.2.custom.css" />
+ENDJQUERY
+	return $s;
 }
 
 # ----------------------------------------------------------------- Preferences
@@ -1267,16 +1291,98 @@ sub htmlareaselectactive {
     my $output='<script type="text/javascript" defer="1">'."\n"
               .'// <![CDATA['."\n";
     my $lang = &htmlarea_lang();
+    $output.='
+    
+    function containsBlockHtml(id) {
+		var re = $("#"+id).html().search(/(?:\&lt\;|\<)(br|h1|h2|h3|h4|h5|h6|p|ol|ul|table|pre|address|blockquote|center|div)[\s]*((?:\/[\s]*(?:\&gt\;|\>)|(?:\&gt\;|\>)[\s\S]*(?:\&lt\;|\<)\/[\s]*\1[\s]*\(?:\&gt\;|\>))/im);
+    	return (re >= 0);
+    }
+    
+    function startRichEditor(id) {
+    	CKEDITOR.replace(id, 
+    		{
+    			customConfig: "/ckeditor/loncapaconfig.js"
+    		}
+    	);
+    }
+    
+    function destroyRichEditor(id) {
+    	CKEDITOR.instances[id].destroy();
+    }
+    
+    function editorHandler(event) {
+    	var rawid = $(this).attr("id");
+    	var id = new RegExp("LC_rt_(.*)").exec(rawid)[1]
+    	event.preventDefault();
+    	if ($(this).hasClass("LC_enable_rt")) {
+    		startRichEditor(id);
+			$("#LC_rt_"+id).html("<b>&laquo; Plain text</b>");
+			$("#LC_rt_"+id).attr("title", "Disable rich text formatting and edit in plain text");
+			$("#LC_rt_"+id).addClass("LC_disable_rt");
+			$("#LC_rt_"+id).removeClass("LC_enable_rt");
+    	} else {
+			destroyRichEditor(id);
+			$("#LC_rt_"+id).html("<b>Rich formatting &raquo;</b>");
+			$("#LC_rt_"+id).attr("title", "Enable rich text formatting (bold, italic, etc.)");
+			$("#LC_rt_"+id).addClass("LC_enable_rt");
+			$("#LC_rt_"+id).removeClass("LC_disable_rt");
+		}
+	}
+    
+    $(document).ready(function(){
+		$(".LC_richAlwaysOn").each(function() {
+			startRichEditor($(this).attr("id"));
+		});
+		$(".LC_richDetectHtml").each(function() {
+			var id = $(this).attr("id");
+			if(containsBlockHtml(id)) {
+				$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Disable rich text formatting and edit in plain text\" class=\"LC_disable_rt\"><b>&laquo; Plain text</b></a></div>");				
+				startRichEditor(id);
+				$("#LC_rt_"+id).click(editorHandler);				
+			}
+			else {
+				$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Enable rich text formatting (bold, italic, etc.)\" class=\"LC_enable_rt\"><b>Rich formatting &raquo;</b></a></div>");
+				$("#LC_rt_"+id).click(editorHandler);
+			}
+		});
+		$(".LC_richDefaultOn").each(function() {
+			var id = $(this).attr("id");
+			$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Disable rich text formatting and edit in plain text\" class=\"LC_disable_rt\"><b>&laquo; Plain text</b></a></div>");				
+			startRichEditor(id);
+			$("#LC_rt_"+id).click(editorHandler);
+		});
+		$(".LC_richDefaultOff").each(function() {
+			var id = $(this).attr("id");
+			$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Enable rich text formatting (bold, italic, etc.)\" class=\"LC_enable_rt\"><b>Rich formatting &raquo;</b></a></div>");
+			$("#LC_rt_"+id).click(editorHandler);			
+		});
+		
+	});
+';
+    
     foreach my $field (@fields) {
-	$output.="
+	$output.='
+	
 {
-    var oFCKeditor = new FCKeditor('$field');
-    oFCKeditor.Config['CustomConfigurationsPath'] = 
-	'/fckeditor/loncapaconfig.js';    
-    oFCKeditor.ReplaceTextarea();
-    oFCKeditor.Config['AutoDetectLanguage'] = false;
-    oFCKeditor.Config['DefaultLanguage'] = '$lang';
-}";
+	$(document).ready(function() {
+		if (!($("#'.$field.'").hasClass("LC_richAlwaysOn"))) {
+			if (!($("#'.$field.'").hasClass("LC_richAlwaysOff"))) {
+				if (!($("#'.$field.'").hasClass("LC_richDetectHtml"))) {
+					if (!($("#'.$field.'").hasClass("LC_richDefaultOn"))) {
+						if (!($("#'.$field.'").hasClass("LC_richDefaultOff"))) {
+							var oFCKeditor = new FCKeditor("'.$field.'");
+							oFCKeditor.Config["CustomConfigurationsPath"] = 
+							"/fckeditor/loncapaconfig.js";    
+							oFCKeditor.ReplaceTextarea();
+							oFCKeditor.Config["AutoDetectLanguage"] = false;
+							oFCKeditor.Config["DefaultLanguage"] = "'.$lang.'";
+						}
+					}
+				}		
+			}
+		}
+    });
+}';
     }
     $output.="\nwindow.status='Activated Editfields';\n"
             .'// ]]>'."\n"
@@ -1342,6 +1448,7 @@ returns: nothing
 ############################################################
 {
     my @Crumbs;
+    my %tools = ();
     
     sub breadcrumbs {
         my ($component,$component_help,$menulink,$helplink,$css_class,$no_mt, $CourseBreadcrumbs) = @_;
@@ -1438,8 +1545,12 @@ returns: nothing
                              .$links;
         }
         
+        #SD START (work in progress!)
+        add_tools(\$links);
+        #SD END
         $links = htmltag('div', $links, 
                         { id => "LC_breadcrumbs" }) unless ($CourseBreadcrumbs) ;
+        add_advtools(\$links);
 
         # Return the @Crumbs stack to what we started with
         push(@Crumbs,$last);
@@ -1450,11 +1561,62 @@ returns: nothing
 
     sub clear_breadcrumbs {
         undef(@Crumbs);
+        undef(%tools);
     }
 
     sub add_breadcrumb {
         push(@Crumbs,@_);
     }
+    
+
+    #SD START (work in progress!)
+    sub add_breadcrumb_tool {
+        my ($category, $html) = @_;
+        return unless $html;
+        if (!defined(%tools)) { 
+            my %tools = ( A => [], B => [], C => []);
+        }
+        push @{$tools{$category}}, $html;
+    }
+
+    sub clear_breadcrumb_tools {
+        undef(%tools);
+    }
+
+    sub add_tools {
+        my ($links) = @_;
+        return unless defined %tools;
+        my $html = '<ul class="LC_bct">';
+        for my $category ('A','B') {
+            $html .= '<li class="LC_breadcrumb_tools">'
+                   . '<ul class="LC_breadcrumb_tools'
+                   . " LC_breadcrumb_tools_$category\">";
+            for my $item (@{$tools{$category}}){
+                #SD ugly! I'll fix that later on
+                $item =~ s/align="(right|left)"//;
+                $item =~ s/<span(.*?)\/span>//;
+                $html .= "<li>$item</li>";
+            }
+            $html .= '</ul></li>';
+            if ($category eq 'A') { $html .= "<li>$$links</li>"; }
+        }
+        $$links = $html.'</ul>';
+    }
+
+    sub add_advtools {
+        my ($links) = @_;
+        return unless (defined $tools{'C'}) and (scalar (@{$tools{'C'}}) > 0);
+        my $html = start_funclist();
+        for my $item (@{$tools{'C'}}){
+                next unless $item;
+                $item =~ s/align="(right|left)"//;
+                $html .= add_item_funclist($item);
+        }
+        $html   .= end_funclist();
+        $html    = Apache::loncommon::head_subbox($html);
+        $$links .= $html;
+    }
+    #SD END
 
 } # End of scope for @Crumbs
 
@@ -1547,7 +1709,7 @@ END
 }
 
 sub row_title {
-    my ($title,$css_title_class,$css_value_class) = @_;
+    my ($title,$css_title_class,$css_value_class, $css_value_furtherAttributes) = @_;
     $row_count[0]++;
     my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
     $css_title_class ||= 'LC_pick_box_title';
@@ -1559,7 +1721,7 @@ sub row_title {
         $title .= ':';
     }
     my $output = <<"ENDONE";
-           <tr class="LC_pick_box_row">
+           <tr class="LC_pick_box_row" $css_value_furtherAttributes> 
             <td $css_title_class>
 	       $title
             </td>
@@ -1727,7 +1889,7 @@ sub course_selection {
             $output .= '</tr></table><br />';
         }
     }
-    $output .= '<input type="radio" name="coursepick" value="specific" onclick="coursePick(this.form);opencrsbrowser('."'".$formname."','dccourse','dcdomain','coursedesc','','1'".')" />'.$pickspec.' '.$courseform.'&nbsp;&nbsp;<input type="text" value="0" size="4" name="coursetotal" /><input type="hidden" name="courselist" value="" />selected.<br />'."\n";
+    $output .= '<input type="radio" name="coursepick" value="specific" onclick="coursePick(this.form);opencrsbrowser('."'".$formname."','dccourse','dcdomain','coursedesc','','1','$crstype'".')" />'.$pickspec.' '.$courseform.'&nbsp;&nbsp;<input type="text" value="0" size="4" name="coursetotal" /><input type="hidden" name="courselist" value="" />selected.<br />'."\n";
     return $output;
 }
 
@@ -1815,16 +1977,21 @@ sub course_custom_roles {
 
 # topic_bar
 #
-# Generates a div containing a numbered (static image) followed by a title
-# with a background color defined in the corresponding CSS: LC_topic_bar
-#
+# Generates a div containing an (optional) number with a white background followed by a 
+# title with a background color defined in the corresponding CSS: LC_topic_bar
+# Inputs:
+# 1. number to display.
+#    If input for number is empty only the title will be displayed. 
+# 2. title text to display.
+# Outputs - a scalar containing html mark-up for the div.
+
 sub topic_bar {
-    my ($imgnum,$title) = @_;
-    return '<div class="LC_topic_bar">'
-          .'<img src="/res/adm/pages/bl_step'.$imgnum.'.gif"'
-          .' alt="'.&mt('Step [_1]',$imgnum).'" />'
-          .' '.$title
-          .'</div>';
+    my ($num,$title) = @_;
+    my $number = '';
+    if ($num ne '') {
+        $number = '<span>'.$num.'</span>';
+    }
+    return '<div class="LC_topic_bar">'.$number.$title.'</div>';
 }
 
 ##############################################
@@ -2285,8 +2452,7 @@ Returns: HTML code with function list st
 sub start_funclist {
     my($legendtext)=@_;
     $legendtext=&mt('Functions') if !$legendtext;
-    return "<fieldset>\n<legend>$legendtext</legend>\n"
-          .'<ul class="LC_funclist">'."\n";
+    return '<ul class="LC_funclist"><li style="font-weight:bold; margin-left:0.8em;">'.$legendtext.'</li>'."\n";
 }
 
 
@@ -2344,8 +2510,7 @@ Returns: HTML code with function list en
 ##############################################
 
 sub end_funclist {
-    my($r)=@_;
-    return "</ul>\n</fieldset>\n";
+    return "</ul>\n";
 }
 
 1;