File:  [LON-CAPA] / loncom / interface / lonsyllabus.pm
Revision 1.106.2.4: download - view: text, annotated - select for diffs
Wed Feb 17 04:48:09 2010 UTC (14 years, 4 months ago) by faziophi
Branches: bz6209
Diff to branchpoint 1.106: preferred, unified
Work-in-progress commit for bug 6209.

Continuing work on jQuery based interface. At this point, none of the
buttons actually function, but the following changes are
notable:

SECTIONS/FIELDS LIST:
-- Reorder now only is allowed along the y-axis, and the "up-down arrow"
   icon serves as the handle by which sections can be dragged.
-- The list is now as long as the number of sections, as opposed to
   using 'overflow-y: scroll'.
-- Hovering over a section name shows a "Delete" icon.
-- New non-functional buttons to "Add Section" and "Revert Order"

ACTIVITY BAR:
-- Activity bar (whose major purpose here is to save) has been put in.
-- The "Save" button becomes highlighted once a change has been made.

--NOT YET DONE--
These are things that I need to do, or have been told about, but
have not yet been touched.
-- Letting lonnet::put manage the freezing of nested data structures.
-- Removing 2.x pseudo-compatibility.

Screenshot attached to bug 6209 on Bugzilla.

    1: # The LearningOnline Network
    2: # Syllabus
    3: #
    4: # $Id: lonsyllabus.pm,v 1.106.2.4 2010/02/17 04:48:09 faziophi Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::lonsyllabus;
   30: 
   31: use strict;
   32: use Apache::lontemplate;
   33: use Apache::Constants qw(:common);
   34: use Apache::loncommon;
   35: use Apache::lonnet;
   36: use Apache::lontexconvert;
   37: use Apache::lonfeedback;
   38: use Apache::lonannounce;
   39: use Apache::lonlocal;
   40: use Apache::lonhtmlcommon;
   41: use Apache::lonspeller();
   42: use HTML::Entities();
   43: use Digest::MD5 qw(md5_hex);
   44: use Storable qw(freeze thaw);
   45: 
   46: # These are strings representing types of fields
   47: # that will used to parse/display a field correctly
   48: use constant {
   49: 	TYPE_TEXT_HTML => 'html',  #<-- default
   50: 	TYPE_TEXT_PLAIN => 'text',
   51: 	TYPE_URL_INCLUDE => 'include-url',
   52: };
   53: 
   54: sub handler {
   55:     my $r = shift;
   56:     &Apache::loncommon::content_type($r,'text/html');
   57:     $r->send_http_header;
   58:     return OK if $r->header_only;
   59: 
   60:     my $target=$env{'form.grade_target'};
   61: # --------------------------------------------------- Get course info from URL
   62:     my (undef,undef,$cdom,$cnum)=split(/\//,$r->uri);
   63: # ------------------------------------------------------------ Get query string
   64:     &Apache::loncommon::get_unprocessed_cgi
   65:                         ($ENV{'QUERY_STRING'},['delete', 'rename', 'field', 'forcestudent','register','forceedit','forceflush','wrapperdisplay']);
   66: # ----------------------------------------------------- Is this even a course?
   67:     my $homeserver=&Apache::lonnet::homeserver($cnum,$cdom);
   68:     if ($homeserver eq 'no_host') {
   69:         &Apache::loncommon::content_type($r,'text/html');
   70:         $r->send_http_header;
   71:         &Apache::loncommon::simple_error_page($r,'No syllabus available',
   72:                           'No syllabus available');
   73:         return OK;
   74:     }
   75: # ------------------------------------- There is such a course, get environment
   76:     my %courseenv=&Apache::lonnet::dump('environment',$cdom,$cnum);
   77: 
   78: # ------------------------------------------------------------ Print the screen
   79:     if ($target eq 'tex') {
   80:         $r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'}));
   81:     }
   82:     
   83: # -------------------------------------------------- Let's see who handles this
   84:     my $externalsyllabus=$courseenv{'externalsyllabus'};
   85:     if ($externalsyllabus=~/\w/) {
   86:        if ($env{'form.wrapperdisplay'} eq 'menu') {
   87:            $r->print(&Apache::lonwrapper::simple_menu());
   88:        } else {
   89:            $r->print(&Apache::lonwrapper::wrapper("/public/$cdom/$cnum/syllabus?wrapperdisplay=menu",
   90:                            $externalsyllabus));
   91:        }
   92:        return OK;
   93:     }
   94: 
   95: # --------------------------------------------------------- The old syllabus fields
   96:     my %syllabusfields=&Apache::lonlocal::texthash(
   97:        'aaa_instructorinfo' => 'Instructor Information',
   98:        'bbb_description'    => 'Course Description',
   99:        'ccc_prereq'         => 'Prerequisites',
  100:        'cdc_classhours'     => 'Class Hours',
  101:        'ddd_officehours'    => 'Office Hours',
  102:        'eee_helproom'       => 'Helproom Hours',
  103:        'efe_projectinfo'    => 'Project Information',
  104:        'fff_examinfo'       => 'Exam Information',
  105:        'fgf_deadlines'      => 'Deadlines',
  106:        'ggg_grading'        => 'Grading Information',
  107:        'hhh_readings'       => 'Readings',
  108:        'iii_coursepack'     => 'Coursepack',
  109:        'jjj_weblinks'       => 'Web Links',
  110:        'kkk_textbook'       => 'Textbook',
  111:        'lll_includeurl'     => 'URLs To Include in Syllabus');
  112:        
  113: # --------------------------------------------------------------- Force Student
  114:     my $forcestudent='';
  115:     if ($env{'form.forcestudent'}) { $forcestudent='student'; };
  116:     my $forceedit='';
  117:     if ($env{'form.forceedit'}) { $forceedit='edit'; }
  118: 
  119: # ----------------------------------------------------------------- Make header
  120:     if ($target ne 'tex') {
  121:         my $rss_link = &Apache::lonrss::rss_link($cnum,$cdom);
  122:         my $js;
  123:         if ($env{'form.backto'} eq 'coursecatalog') {
  124:             $js .= <<"ENDSCRIPT";
  125: 
  126: <script type="text/javascript">
  127: function ToCatalog(caller) {
  128:     numidx = getIndexByName('coursenum');
  129:         if (numidx > -1) {
  130:             if (caller != 'details') {
  131:                 document.backtocat.elements[numidx].value = '';
  132:             }
  133:         }
  134:     document.backtocat.submit();
  135: }
  136: 
  137: function getIndexByName(item) {
  138:     for (var i=0;i<document.backtocat.elements.length;i++) {
  139:         if (document.backtocat.elements[i].name == item) {
  140:             return i;
  141:         }
  142:     }
  143:     return -1;
  144: }
  145: 
  146: </script>
  147: 
  148: ENDSCRIPT
  149:         }
  150:         $js .= <<'ENDSCRIPT';
  151: 
  152: <style type="text/css">
  153: 	.LC_Sortable ul { list-style-type: none; margin: 0px; padding: 0px}
  154: 	.LC_Sortable li { list-style-type: none; cursor: pointer; margin: 0px 5px 5px 5px; padding: 5px; padding-left: 1.5em; width: 200px; font-size:90% }
  155: 	.LC_Sortable li span.left { float: left; cursor: move; margin-left: -1.5em; }
  156: 	.LC_Sortable li span.right { float: right; cursor: auto; margin-right: 0em }
  157: 	.LC_EllipseOverflow { overflow: hidden; text-overflow: ellipsis; -o-text-overflow: ellipsis; white-space: nowrap; }
  158: 	.LC_ActivityBarButton-IconLeft { padding-left: 20px }
  159: 	.LC_ActivityBarButton-IconLeft .ui-icon { float:left; margin-left: -18px; }
  160: 	#scrollable-fields-container {float:left; background-color: white; width: 235px; border: 1px solid grey; margin: 0px; -moz-border-radius: 4px; -webkit-border-radius: 4px; border-radius: 4px;}
  161: 	#syllabus-fields-actions {text-align: center}
  162: 	#syllabus-content {margin-left: 245px}
  163: 	#activity-bar {-moz-border-radius: 4px; -webkit-border-radius: 4px; border-radius: 4px; border: solid 1px grey; background-color: #dddddd; padding: 0px 0px;margin: 5px 0 2px 0;}	
  164: 	#activity-bar button { font-size: 100%; vertical-align: middle }
  165: </style>
  166: 
  167: <script type="text/javascript">
  168: 	// public domain code to emulate text-overflow:ellipsis in Firefox using jQuery
  169: 	(function($) {
  170: 		$.fn.ellipsis = function(enableUpdating){
  171: 			var s = document.documentElement.style;
  172: 			if (!('textOverflow' in s || 'OTextOverflow' in s)) {
  173: 				return this.each(function(){
  174: 					var el = $(this);
  175: 					if(el.css("overflow") == "hidden"){
  176: 						var originalText = el.html();
  177: 						var w = el.width();
  178: 						
  179: 						var t = $(this.cloneNode(true)).hide().css({
  180: 							'position': 'absolute',
  181: 							'width': 'auto',
  182: 							'overflow': 'visible',
  183: 							'max-width': 'inherit'
  184: 						});
  185: 						el.after(t);
  186: 						
  187: 						var text = originalText;
  188: 						while(text.length > 0 && t.width() > el.width()){
  189: 							text = text.substr(0, text.length - 1);
  190: 							t.html(text + "...");
  191: 						}
  192: 						el.html(t.html());
  193: 						
  194: 						t.remove();
  195: 						
  196: 						if(enableUpdating == true){
  197: 							var oldW = el.width();
  198: 							setInterval(function(){
  199: 								if(el.width() != oldW){
  200: 									oldW = el.width();
  201: 									el.html(originalText);
  202: 									el.ellipsis();
  203: 								}
  204: 							}, 200);
  205: 						}
  206: 					}
  207: 				});
  208: 			} else return this;
  209: 		};
  210: 	})(jQuery);
  211: 	
  212: 	var changesMade = false;
  213: 	var changesBreakVersion = false;
  214: 	
  215: 	jQuery(document).ready(function() {
  216: 		jQuery('.LC_EllipseOverflow').ellipsis();
  217: 		jQuery('#syllabus-fields li').click(function() {
  218: 			jQuery(this).parent().children('li').removeClass('ui-state-highlight');
  219: 			jQuery(this).addClass('ui-state-highlight');
  220: 			var id = /title-([0-9_]+)/i.exec(jQuery(this).attr('id'));
  221: 			jQuery('#syllabus-form .LC_Box').hide();
  222: 			jQuery('#box-'+id[1]).show();
  223: 		});
  224: 		jQuery('#syllabus-fields').sortable({
  225: 			revert: true,
  226: 			axis: 'y',
  227: 			cursor: 'move',
  228: 			placeholder: 'ui-state-highlight',
  229: 			handle: 'span.ui-icon-arrowthick-2-n-s',
  230: 			forcePlaceholderSize: true,
  231: 			start: function(event, ui) {
  232: 				jQuery(ui.item).css('font-style', 'italic');
  233: 			},
  234: 			stop: function(event, ui) {
  235: 				var order = [];
  236: 				jQuery(ui.item).parent().children('li').each(function() {
  237: 					var id = /title-([0-9_]+)/i.exec(jQuery(this).attr('id'));
  238: 					order.push(id[1]);
  239: 				});
  240: 				if (!changesMade) {
  241: 					changesMade = true;
  242: 					$('#save-button').addClass('ui-state-highlight');
  243: 					$('#save-button').removeClass('ui-state-disabled');
  244: 				}
  245: 			}
  246: 		});
  247: 		jQuery('#syllabus-fields li').disableSelection();
  248: 		jQuery('.RemoveSection').hide();
  249: 		jQuery('#syllabus-fields li').hover(
  250: 			function(){
  251: 				jQuery(this).find('.RemoveSection').toggle();
  252: 			},
  253: 			function(){
  254: 				jQuery(this).find('.RemoveSection').toggle();
  255: 			}
  256: 		);
  257: 		jQuery('#syllabus-fields li span.right').hover(
  258: 			function(){
  259: 				jQuery(this).css('background-image', 'url("/adm/jQuery/css/smoothness/images/ui-icons_2e83ff_256x240.png")');
  260: 			},
  261: 			function(){
  262: 				jQuery(this).css('background-image', 'url("/adm/jQuery/css/smoothness/images/ui-icons_454545_256x240.png")');
  263: 			}
  264: 		);
  265: 		jQuery('#syllabus-form .LC_Box').hide();
  266: 		jQuery('.LC_ActivityBarButton').hover(
  267: 			function(){ 
  268: 				$(this).addClass("ui-state-hover"); 
  269: 			},
  270: 			function(){ 
  271: 				$(this).removeClass("ui-state-hover"); 
  272: 			}
  273: 		);
  274: 	});
  275: </script>
  276: ENDSCRIPT
  277:         my $start_page =
  278:          &Apache::loncommon::start_page("Syllabus", $rss_link.$js,
  279:                        {'function'       => undef,
  280:                         'domain'         => $cdom,
  281:                         'force_register' =>
  282:                         $env{'form.register'},});
  283: 
  284:         $r->print($start_page);
  285:         if ($env{'form.backto'} eq 'coursecatalog') {
  286:             &Apache::lonhtmlcommon::clear_breadcrumbs();
  287:             &Apache::lonhtmlcommon::add_breadcrumb
  288:                 ({href=>"javascript:ToCatalog()",
  289:                 text=>"Course/Community Catalog"});
  290:             if ($env{'form.coursenum'} ne '') {
  291:                 &Apache::lonhtmlcommon::add_breadcrumb
  292:                     ({href=>"javascript:ToCatalog('details')",
  293:                     text=>"Course details"});
  294:             }
  295:             &Apache::lonhtmlcommon::add_breadcrumb
  296:                 ({href=>$r->uri,
  297:                 text=>"Course syllabus"});
  298:             $r->print(&Apache::lonhtmlcommon::breadcrumbs());
  299:         }
  300:     }
  301: # ---------------------------------------------------------- Load syllabus info
  302:     my %syllabus=&Apache::lonnet::dump('syllabus',$cdom,$cnum);  # load db
  303:     my $allowed=0;  # can we edit this page?
  304:     my $privileged=0;
  305:     my %data;
  306:     if ($env{'form.forceflush'}) {
  307:     	delete $syllabus{'data.fields'};
  308:     	&Apache::lonnet::del('syllabus', ['data.fields'], $cdom, $cnum);
  309:     	delete $syllabus{'properties.v2_conflict'};
  310:     	&Apache::lonnet::del('syllabus', ['properties.v2_conflict'], $cdom, $cnum);
  311:     	delete $syllabus{'properties.v2_conflict_fail'};
  312:     	&Apache::lonnet::del('syllabus', ['properties.v2_conflict_fail'], $cdom, $cnum);
  313:     	delete $syllabus{'properties.last_modified'};
  314:     	&Apache::lonnet::del('syllabus', ['properties.last_modified'], $cdom, $cnum);
  315:     	delete $syllabus{'properties.v2_converted'};
  316:     	&Apache::lonnet::del('syllabus', ['properties.v2_converted'], $cdom, $cnum);
  317:     	delete $syllabus{'data.old_new_map'};
  318:     	&Apache::lonnet::del('syllabus', ['data.old_new_map'], $cdom, $cnum);
  319:     	delete $syllabus{'data.deleted_fields'};
  320:     	&Apache::lonnet::del('syllabus', ['data.deleted_fields'], $cdom, $cnum);
  321: 		%syllabus=&Apache::lonnet::dump('syllabus',$cdom,$cnum);  # load db
  322:     	$r->print("Flushed syllabus DB file.<br />");
  323:     	$r->print("Syllabus conflict: ".$syllabus{'properties.v2_conflict'}."<br />");
  324:    	}
  325:    	#$r->print("Existing fields: ".$syllabus{'data.fields'}."<br />");
  326:    	#$r->print("Old-new map: ".$syllabus{'data.old_new_map'}."<br />");
  327:    	#$r->print("Deleted fields: ".$syllabus{'data.deleted_fields'}."<br />");
  328:     if (!exists($syllabus{'data.fields'})) {
  329:     	# convert existing 2.x data to new DB fields
  330:     	# which become new primary data source for document
  331: 		%data = %{convert_from_v2($r, \%syllabus, \%syllabusfields, 0)};
  332: 		#$r->print("New fields order: ".$data{'data.fields'}."<br />");
  333: 		&Apache::lonnet::put('syllabus',\%data,$cdom,$cnum);
  334:     } elsif (  !exists($syllabus{'properties.v2_converted'}) && 
  335:     		   exists($syllabus{'uploaded.lastmodified'}) &&
  336:     		   exists($syllabus{'properties.last_modified'}) &&
  337:     		   ($syllabus{'uploaded.lastmodified'} !=
  338:     		   $syllabus{'properties.last_modified'})) {
  339:     	# if the document has been saved in 3.x and later edited in 
  340:     	# 2.x, reconvert the existing document, with extra warning
  341:     	%data = %{convert_from_v2($r, \%syllabus, \%syllabusfields, 1)};
  342:     	delete $data{'properties.v2_converted'};
  343:     	&Apache::lonnet::del('syllabus', ['properties.v2_converted'], $cdom, $cnum);
  344:     	$data{'properties.v2_conflict'} = 1;
  345: 		&Apache::lonnet::put('syllabus',\%data,$cdom,$cnum);    	
  346:     } else {
  347:     	%data = %syllabus;
  348:     	
  349:     }
  350: 
  351: # ----------------------------------------------------- Only if not public call
  352:     if ($env{'user.environment'}) { # does this user have privileges to post, etc?
  353:         if ($env{'request.course.id'}
  354:         && $cdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}
  355:         && $cnum eq $env{'course.'.$env{'request.course.id'}.'.num'}) {
  356:             $allowed=&Apache::lonnet::allowed('mdc',$env{'request.course.id'});
  357:             $privileged=$allowed;
  358:             if (($data{'uploaded.lastmodified'}) && (!$forceedit)) {
  359:                 $forcestudent='student';
  360:             }
  361:             if ($forcestudent or $target eq 'tex') { $allowed=0; }
  362:         }
  363: 		#store what the user typed in
  364: 		my @fields = @{thaw($data{'data.fields'})};
  365: 		if (($allowed) && ($env{'form.delete'})) {
  366: 			my $field = $env{'form.delete'};
  367: 			chomp($field);
  368: 			#allow only numbers, underscores
  369: 			$field=~s/[^0-9_]//g;
  370: 			#check if the field exists
  371: 			#do not delete if file in v2 conversion mode
  372: 			if (exists($data{'data.field.'.$field}) &&
  373: 				!exists($data{'properties.v2_converted'})) {
  374: 				#$r->print("Field '$field' can be deleted.<br />");
  375: 				# linearly parse "data.fields" and remove it
  376: 				for (my $i = 0; $i < length(@fields); $i++) {
  377: 					if ($fields[$i] eq $field) {
  378: 						splice(@fields, $i, 1);
  379: 						#$r->print("Removed entry $i from 'data.fields'<br />");
  380: 					}
  381: 				}
  382: 				# if "data.deleted" does not exist, create it
  383: 				my @deleted;
  384: 				if (!exists($data{'data.deleted_fields'})) {
  385: 					@deleted = [];
  386: 				} else {
  387: 					@deleted = @{thaw($data{'data.deleted_fields'})};
  388: 				}
  389: 				# only if deleted does not exist in 'data.deleted', push it
  390: 				my $push = 1;
  391: 				for (my $i = 0; $i < length(@deleted); $i++) {
  392: 					if ($deleted[$i] eq $field) {
  393: 						$push = 0;
  394: 						last;
  395: 					}
  396: 				}
  397: 				unless (!$push) {
  398: 					push(@deleted, $field);
  399: 				}
  400: 				$data{'data.fields'} = freeze(\@fields);
  401: 				$data{'data.deleted_fields'} = freeze(\@deleted);
  402: 				
  403: 				&Apache::lonnet::put('syllabus',\%data,$cdom,$cnum);
  404: 			}
  405: 		}
  406: 		if (($allowed) && ($env{'form.rename'})) {
  407: 			my $field = $env{'form.rename'};
  408: 			my $new_title = "Hello, World!";
  409: 			chomp($field);
  410: 			$field=~s/[^0-9_]//g;
  411: 			#check if the field exists
  412: 			#do not delete if file in v2 conversion mode
  413: 			if (exists($data{'data.field.'.$field}) &&
  414: 				!exists($data{'properties.v2_converted'})) {
  415: 				#sanitize HTML content
  416: 				#$r->print("Rename -- field found.<br />");
  417: 				my %db_entry = %{thaw($data{'data.field.'.$field})};
  418: 				$new_title = &Apache::lonfeedback::clear_out_html($new_title, 1);
  419: 				$db_entry{title} = $new_title;
  420: 				$data{'data.field.'.$field} = freeze(\%db_entry);
  421: 				&Apache::lonnet::put('syllabus',\%data,$cdom,$cnum);
  422: 			}
  423: 		}
  424:         if (($allowed) && ($env{'form.storesyl'})) {
  425:             foreach my $syl_field (@fields) {
  426:                 my $field=$env{'form.'.$syl_field};
  427:                 my $type;
  428:                 my %field_hash;
  429:                 # only update a field if it already exists!
  430:                 if (exists($data{'data.field.'.$syl_field})) {
  431:                 	#$r->print("Creating/updated field ".$syl_field."<br />");
  432:                 	%field_hash = exists($data{'data.field.'.$syl_field}) ?
  433:                 						%{thaw($data{'data.field.'.$syl_field})} : ();
  434:                 	$type = exists($field_hash{type}) ? $field_hash{type} : TYPE_TEXT_HTML;
  435: 					chomp($field);
  436: 					$field=~s/\s+$//s;
  437: 					$field=~s/^\s+//s;
  438: 					$field=~s/\<br\s*\/*\>$//s;
  439: 					$field=&Apache::lonfeedback::clear_out_html($field,1);
  440: 					$field_hash{content}=$field;
  441: 					if ($type eq TYPE_URL_INCLUDE) { # clean up included URLs
  442: 						my $field='';
  443: 						foreach my $value (split(/\n/,$field_hash{content})) {
  444: 							my $url=$value;
  445: 							# get rid of leading and trailing spaces
  446: 							$url=~s/^\s+//;
  447: 							$url=~s/\s+$//;
  448: 							if ($url=~m|^https?\://([^/]+)/(.+)$|) {
  449: 								my $host = $1;
  450: 								my $remainder=$2;
  451: 								# remove the hostname from internal URLs
  452: 								my $hostname = &Apache::lonnet::hostname($host);
  453: 								my %all_hostnames = &Apache::lonnet::all_hostnames();
  454: 								foreach my $possible_host (keys(%all_hostnames)) {
  455: 									if ($possible_host =~ /\Q$hostname\E/i) {
  456: 										$url=$remainder;
  457: 									}
  458: 								}
  459: 							}
  460: 							# norm internal URLs
  461: 							unless ($url=~/^https?\:/) {
  462: 								$url=&Apache::lonnet::clutter($url);
  463: 							}
  464: 							# re-assemble field
  465: 							if ($url) {
  466: 								$field.=$url."\n";
  467: 							}
  468: 						}
  469: 	    	               $field_hash{content}=$field;
  470: 		                   $field_hash{type}=TYPE_URL_INCLUDE;
  471: 					}
  472: 					$data{'data.field.'.$syl_field} = freeze(\%field_hash);
  473: 				} 
  474:             }
  475:             $data{'uploaded.domain'}=$env{'user.domain'};
  476:             $data{'uploaded.name'}=$env{'user.name'};
  477:             my $time = $^T;
  478:             $data{'uploaded.lastmodified'}=$time;
  479:             $data{'properties.last_modified'}=$time;
  480:             delete $data{'properties.v2_converted'};
  481:             delete $data{'properties.v2_conflict'};
  482:             delete $data{'properties.v2_conflict_fail'};
  483:             &Apache::lonnet::del('syllabus', ['properties.v2_converted', 
  484:             	'properties.v2_conflict', 'properties.v2_conflict_fail'], $cdom, $cnum);
  485:             	
  486:             #2.x compatibility: write to old fields with new mapped fields
  487:             my %old_new_map = %{thaw($data{'data.old_new_map'})};
  488:             foreach my $old_field (keys(%old_new_map)) {
  489:             	#$r->print("Looking for: ".$old_field." at ".$old_new_map{$old_field}."<br />");
  490:             	if (exists($data{'data.field.'.$old_new_map{$old_field}})) {
  491:             		#$r->print("updating old field ".$old_field."<br />");
  492:             		my %new_field = %{thaw($data{'data.field.'.$old_new_map{$old_field}})};
  493:             		$data{$old_field} = $new_field{content};
  494:             	}
  495:             }
  496:               
  497:             &Apache::lonnet::put('syllabus',\%data,$cdom,$cnum);
  498:         }
  499:     }
  500: 
  501: #--------Functions
  502:     if( ($allowed || $privileged) && $target ne 'tex') {
  503:         my $functions=&Apache::lonhtmlcommon::start_funclist();
  504:         if ($allowed) {
  505: 			#if you have the register flag, keep it
  506: 			if($env{'form.register'} == 1) {
  507:             	$functions.=&Apache::lonhtmlcommon::add_item_funclist(
  508:                           '<a href="'.$r->uri.'?forcestudent=1&amp;register=1">'
  509:                            .&mt('Show Public View').'</a>'
  510:                            .&Apache::loncommon::help_open_topic(
  511:                                 'Uploaded_Templates_PublicView'));
  512: 			} else {
  513:             	$functions.=&Apache::lonhtmlcommon::add_item_funclist(
  514:                           '<a href="'.$r->uri.'?forcestudent=1">'
  515:                            .&mt('Show Public View').'</a>'
  516:                            .&Apache::loncommon::help_open_topic(
  517:                                 'Uploaded_Templates_PublicView'));
  518: 			}
  519:         } elsif ($privileged) {
  520: 			if($env{'form.register'} == 1) {
  521: 	            $functions.=&Apache::lonhtmlcommon::add_item_funclist(
  522:                            '<a href="'.$r->uri.'?forceedit=1&amp;register=1">'
  523:                             .&mt('Edit').'</a>');
  524: 			} else {
  525: 	            $functions.=&Apache::lonhtmlcommon::add_item_funclist(
  526:                            '<a href="'.$r->uri.'?forceedit=1">'
  527:                             .&mt('Edit').'</a>');
  528: 			}
  529:         }
  530: 
  531:         $functions.=&Apache::lonhtmlcommon::end_funclist();
  532:         $r->print(&Apache::loncommon::head_subbox($functions));
  533:     }
  534: 
  535: #---------------------Print External URL Syllabus Info and Help Text
  536:     if( ($allowed) && ($target ne 'tex') ) {
  537:         my $protocol = $Apache::lonnet::protocol{$homeserver};
  538:           $protocol = 'http' if ($protocol ne 'https');
  539:         $r->print('<p class="LC_info">'
  540:                  .&mt('This syllabus can be publicly viewed at [_1]'
  541:                      ,'<tt>'.$protocol.'://'.&Apache::lonnet::hostname($homeserver).$r->uri.'</tt>')
  542:                  .'&nbsp;'.&Apache::loncommon::help_open_topic('Syllabus_ExtLink')
  543:                  .'</p>'
  544:                  .'<p class="LC_info">'
  545:                  .&mt('Instead of using this template you can specify an external URL as Syllabus in the [_1]Course Configuration[_2].'
  546:                      ,'<a href="/adm/courseprefs?actions=courseinfo&amp;phase=display">','</a>')
  547:                  .'</p>'
  548:         );
  549:         #-Print Help Text
  550:         $r->print(&Apache::loncommon::help_open_topic( 
  551:                         'Uploaded_Templates_TextBoxes',
  552:                         &mt('Help with filling in text boxes')));
  553:     }
  554: 
  555: #----------Print last update
  556:     my $lastmod=$syllabus{'uploaded.lastmodified'};
  557:     $lastmod=($lastmod?&Apache::lonlocal::locallocaltime($lastmod):&mt('never'));
  558:     my $who = &Apache::loncommon::aboutmewrapper(
  559:         &Apache::loncommon::plainname($syllabus{'uploaded.name'},
  560:         $syllabus{'uploaded.domain'}),$syllabus{'uploaded.name'},
  561:         $syllabus{'uploaded.domain'});
  562:     if ($target ne 'tex') {
  563:         $r->print('<div class="LC_info">'.&mt('Last updated').': '.
  564:             $lastmod . ' '.
  565:             ($who ? &mt('by').' '.$who
  566:                            : '' ) .
  567:              '</div>' );
  568: 
  569:     } else {
  570:         $r->print('\\\\ '.&mt('Last updated').': '.$lastmod.' '.
  571:             ($who? &mt('by').'\\\\ '.
  572:                     &Apache::loncommon::plainname($syllabus{'uploaded.name'},$syllabus{'uploaded.domain'})
  573:                   :'')
  574:              .'\\\\');
  575:     }
  576:     if ($allowed && $data{'properties.v2_converted'} == 1) {
  577:     	$r->print("<em>This document was created with LON-CAPA 2.x.  Modifying it may cause it to not display correctly on older servers.</em><br/>");
  578: 	}
  579: 	if ($allowed && $data{'properties.v2_conflict'} == 1) {
  580: 		$r->print("<em>This document was saved with LON-CAPA 3.x, then further edited in LON-CAPA 2.x.</em><br/>");
  581: 		if ($data{'properties.v2_conflict_fail'} == 1) {
  582: 			$r->print("<em>Some fields in LON-CAPA 2.x no longer have an equivalent in LON-CAPA 3.x.  These fields were appended; some fields may be duplicated or not match.</em><br />");
  583: 		} else {
  584: 			$r->print("<em>These changes were automatically transferred to LON-CAPA 3.x</em>");
  585: 		}
  586: 	}
  587: 
  588: #----------------------------Print Headtitle
  589:     if ($target ne 'tex') {
  590:         $r->print('<h1>'.$courseenv{'description'}.'</h1>');
  591:         $r->print('<h3>'.  &Apache::lonnet::domain($cdom,'description').'</h3>');
  592:     } else {
  593:         $r->print('\noindent{\large\textbf{'.$courseenv{'description'}.'}}\\\\\\\\\textbf{'.
  594:         &Apache::lonnet::domain($cdom,'description').'}\\\\');
  595:     }
  596: 
  597: # -------------------------------------------------------- Get course personnel
  598:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles($cdom.'/'.$cnum);
  599:     if ($target ne 'tex') {
  600:         $r->print(&Apache::lonhtmlcommon::start_pick_box());
  601:     } else {
  602:         $r->print('\begin{tabular}{|p{0.45\textwidth}|p{0.45\textwidth}|}\hline');
  603:     }
  604:     my @personnel=sort(keys(%coursepersonnel));
  605:     my $lastpers=$personnel[$#personnel];
  606:     foreach my $element (@personnel) {
  607:         if ($target ne 'tex') {
  608:             $r->print(&Apache::lonhtmlcommon::row_title($element));
  609:         } else {
  610:             $r->print(' '.&Apache::lonxml::xmlparse($r,'tex',$element).' & ');
  611:         }
  612:         foreach (split(/\,/,$coursepersonnel{$element})) {
  613:             my ($puname,$pudom)=split(/\:/,$_);
  614:             if ($target ne 'tex') {
  615:                 my $courseperson = &Apache::loncommon::plainname($puname,$pudom);
  616:                 if (($env{'user.name'} eq '') || ($env{'user.name'} eq 'public') ||
  617:                     ($env{'user.domain'} eq '') || ($env{'user.domain'} eq 'public')) {
  618:                     $r->print(' '.$courseperson);
  619:                 } else {
  620:                     $r->print(' '.&Apache::loncommon::aboutmewrapper($courseperson,
  621:                               $puname,$pudom));
  622:                 }
  623:             } else {
  624:                 $r->print(' '.&Apache::loncommon::plainname($puname,
  625:                               $pudom).' ');
  626:             }
  627:         }
  628:         if ($target ne 'tex') {
  629:             my $lastclose=$element eq $lastpers?1:0;
  630:             $r->print(&Apache::lonhtmlcommon::row_closure($lastclose));
  631:         } else {
  632:             $r->print('\\\\ \hline');
  633:         }
  634:     }
  635:     if ($target ne 'tex') {
  636:         $r->print(&Apache::lonhtmlcommon::end_pick_box());
  637:     } else {
  638:         $r->print('\end{tabular}\\\\');
  639:     }
  640: # -------------------------------------------------------------- Announcements?
  641:     my $day = &Apache::lonannounce::showday(time,2,
  642:              &Apache::lonannounce::readcalendar($cdom.'_'.$cnum));
  643:     if ($target ne 'tex') {
  644:     	if (($syllabus{'uploaded.lastmodified'}) || ($allowed)) {
  645:     		&print_activity_bar($r, \%data, $target, $allowed, Apache::lontemplate->RICH_TEXT_DETECT_HTML);
  646:     		&print_field_sortable($r, \%data, $target, $allowed, Apache::lontemplate->RICH_TEXT_DETECT_HTML);
  647:         }
  648:         $r->print("<div id='syllabus-content'>\n");
  649: 
  650:     } else {
  651:         $r->print(&Apache::lonxml::xmlparse($r,'tex',$day));
  652:     }
  653:     
  654: # ---------------------------------------------------------------- Get syllabus
  655:     if (($syllabus{'uploaded.lastmodified'}) || ($allowed)) {
  656:         if ($allowed) {
  657:             $r->print('<form id="syllabus-form" method="post" action="">'.
  658:             '<input type="hidden" name="forceedit" value="edit" />');
  659:         }
  660:         my @htmlids=();
  661: 		my $url_include_handler = sub {
  662: 			my ($r, $field, $json_ref, $group, $target, $allowed) = @_;
  663: 			my $message = $json_ref->{items}{$field}{content};
  664: 			my $title = $json_ref->{items}{$field}{title};
  665: 			my $urls = $message;
  666: 			foreach my $filelink (split(/\n/,$urls)) {
  667: 				my $output='';
  668: 			   # embed style?
  669: 				my ($curfext)=($filelink=~/\.([^\.]+)$/);
  670: 				my $embstyle=&Apache::loncommon::fileembstyle($curfext);
  671: 				if (($embstyle eq 'ssi') || ($curfext=~/\/$/)) {# make ssi call and remove everything but the body contents
  672: 					$output=&Apache::lonnet::ssi_body($filelink);
  673: 				} elsif ($embstyle eq 'img') {# embed as an image
  674: 					$output='<img src="'.$filelink.'" />';
  675: 				}
  676: 				if ($output ne '') {
  677: 					   $message='';
  678: 					   if ($target ne 'tex') {
  679: 						   $message.='<p>'.$output.'</p>';
  680: 					   } else {
  681: 						   $message.=' '.&Apache::lonxml::xmlparse($r,'tex','<p>'.$output.'</p>').' ';
  682: 					   }
  683: 				}
  684: 			}
  685: 			if ($allowed) {
  686: 				 &Apache::lonfeedback::newline_to_br(\$urls);
  687: 				 &Apache::lontemplate::print_start_template($r,$title.
  688: 						  &Apache::loncommon::help_open_topic('Syllabus_URLs'),'LC_Box');
  689: 				 $r->print($urls);
  690: 				 $r->print("<br /><div>");
  691: 				 &Apache::lontemplate::print_textarea_template($r, $message,
  692: 					$field, Apache::lontemplate->RICH_TEXT_ALWAYS_OFF);
  693: 				 &Apache::lontemplate::print_saveall_template($r);                         
  694: 				 $r->print("</div>");
  695: 				 &Apache::lontemplate::print_end_template($r);
  696: 
  697: 			} else {
  698: 				$r->print($message);
  699: 			}
  700: 		};
  701: 		my %custom_hash = ( TYPE_URL_INCLUDE() => $url_include_handler );
  702:  		@htmlids = &print_template_new_fields($r, \%data, 
  703:  			$target, $allowed, Apache::lontemplate->RICH_TEXT_DETECT_HTML, \%custom_hash);
  704:         if ($allowed) {
  705:             $r->print('</form>');
  706:             #&Apache::lonhtmlcommon::htmlareaselectactive(@htmlids));
  707:         }
  708:         if ($target ne 'tex') {$r->print('</div><p style="clear:both">&nbsp;</p>');}
  709:         if ($allowed) {
  710:             &Apache::lontemplate::print_start_template($r,'RSS Feeds and Blogs','LC_Box');
  711:             $r->print(&Apache::lonrss::advertisefeeds($cnum,$cdom,$forceedit));
  712:             my $editurl= &Apache::lonnet::absolute_url().'/adm/'.$cdom.'/'.$cnum.'/_rss.html';
  713:             $r->print( '<a href="'.$editurl.'">'.&mt('New RSS Feed or Blog').'</a>');
  714:             &Apache::lontemplate::print_end_template($r);
  715:         } elsif (&Apache::lonrss::advertisefeeds($cnum,$cdom) ne '') {
  716:             &Apache::lontemplate::print_start_template($r,'RSS Feeds and Blogs','LC_Box');
  717:             $r->print(&Apache::lonrss::advertisefeeds($cnum,$cdom,$forceedit));
  718:             &Apache::lontemplate::print_end_template($r);
  719:         }
  720:     } else {
  721:         if ($target ne 'tex') {$r->print('<p>');} else {$r->print('\par ');}
  722:         $r->print(&mt('No syllabus information provided.'));
  723:         if ($target ne 'tex') {$r->print('</p>');}
  724:     }
  725:     if ($target ne 'tex') {
  726:         if ($env{'form.backto'} eq 'coursecatalog') {
  727:             $r->print('<form name="backtocat" method="post" action="/adm/coursecatalog">'.
  728:                       &Apache::lonhtmlcommon::echo_form_input(['backto','courseid']).
  729:                       '</form>');
  730:         }
  731:         $r->print(&Apache::loncommon::end_page());
  732:     } else {
  733:         $r->print('\end{document}');
  734:     }
  735:     return OK;
  736: }
  737: 
  738: sub print_activity_bar {
  739: 	my ($r, $data_ref, $target, $allowed, $default_rich_text, $group) = @_;
  740: 	$r->print("<div id='activity-bar'>
  741: <button id='save-button' type='button' class='LC_ActivityBarButton LC_ActivityBarButton-IconLeft ui-state-default ui-priority-primary ui-corner-all'>
  742: 	<span class='ui-icon ui-icon-disk'></span><a href='#'>Save</a>
  743: </button>
  744: 
  745: </div>");
  746: }
  747: 
  748: sub print_field_sortable {
  749: 	my ($r, $data_ref, $target, $allowed, $default_rich_text, $group) = @_;
  750: 	my %data = %{$data_ref};
  751: 	my @fields = @{thaw($data{'data.fields'})};
  752: 	$r->print("<div id='scrollable-fields-container'>
  753: <div id='syllabus-fields-container'>
  754: <ui id='syllabus-fields' class='LC_Sortable LC_SyllabusFields'>\n");
  755: 	foreach my $key (@fields) {
  756: 		my %field = %{thaw($data{'data.field.'.$key})};
  757: 		$r->print("<li id='title-$key' class='ui-state-default LC_EllipseOverflow' title='$field{title}'><span class='ui-icon ui-icon-arrowthick-2-n-s left'></span><span id='remove-$key' class='RemoveSection right ui-icon ui-icon-closethick' title='Delete'></span>$field{title}</li>\n");
  758: 	}
  759: 	$r->print("
  760: </ui>
  761: </div>
  762: <div id='syllabus-fields-actions'>
  763: <button id='add-section-button' type='button' class='LC_ActivityBarButton LC_ActivityBarButton-IconLeft ui-state-default ui-priority-secondary ui-corner-all'>
  764: 	<span class='ui-icon ui-icon-circle-plus'></span><a href='#'>Add&nbsp;Section</a>
  765: </button>
  766: <button id='revert-order-button' type='button' class='LC_ActivityBarButton LC_ActivityBarButton-IconLeft ui-state-default ui-priority-secondary ui-corner-all'>
  767: 	<span class='ui-icon ui-icon-arrowreturnthick-1-w'></span><a href='#'>Revert&nbsp;Order</a>
  768: </button>
  769: </div>
  770: </div>\n");
  771: }	
  772: 
  773: sub print_template_new_fields {
  774: 	my ($r, $data_ref, $target, $allowed, $default_rich_text, $custom_handlers_ref, $group) = @_;
  775: 	my @html_ids = ();
  776: 	my %data = %{$data_ref};
  777: 	my @fields = @{thaw($data{'data.fields'})};
  778: 	my %custom_handlers = %{$custom_handlers_ref};
  779: 
  780: 	foreach my $key (@fields) {
  781: 		my %field = %{thaw($data{'data.field.'.$key})};
  782: 		my $title = $field{title};
  783: 		my $raw_message = $field{content};
  784: 		my $type = $field{type};
  785: 		my $message = $raw_message if (($raw_message=~/\w/) || ($allowed));
  786: 		if ((%custom_handlers) && exists($custom_handlers{$type})) {
  787: 			#$custom_handlers{$type}->($r, $field, $json_ref, $group, $target, $allowed);
  788: 		} else {
  789: 			if (($raw_message=~/\w/) || ($allowed)) {
  790: 				if (!&Apache::lonfeedback::contains_block_html($message)) {
  791: 					&Apache::lonfeedback::newline_to_br(\$message);
  792: 				} else {
  793: 					$message = &Apache::lonfeedback::tidy_html($message);
  794: 				}
  795: 				$message=&Apache::lonhtmlcommon::raw_href_to_link($message);
  796: 				if ($allowed) {
  797: 					$message=&Apache::lonspeller::markeduptext($message);
  798: 				}
  799: 				$message=&Apache::lontexconvert::msgtexconverted($message);
  800: 				if ($target ne 'tex') {
  801: 					#output of syllabusfields will be generated here. 
  802: 					&Apache::lontemplate::print_start_template($r,$title,'LC_Box', 'box-'.$key);
  803: 					$r->print($message);
  804: 					if ($allowed) {
  805: 						$r->print("<br /><div>");
  806: 						&Apache::lontemplate::print_textarea_template($r, $raw_message,
  807: 							$key, $default_rich_text);
  808: 						&Apache::lontemplate::print_saveall_template($r);
  809: 						if (!exists($data{'properties.v2_converted'})) {
  810: 							$r->print("<a href='?delete=$key&forceedit=1'>Delete</a> ");
  811: 							$r->print("<a href='?rename=$key&forceedit=1'>Rename to \"Hello, World!\"</a>");
  812: 						}
  813: 						$r->print("</div>");
  814: 					} 
  815: 					&Apache::lontemplate::print_end_template($r);
  816: 				} else {
  817: 				    my $safeinit;
  818:                     $r->print(&Apache::lonxml::xmlparse($r,'tex','<h3>'.$title.'</h3>'));
  819:                     $r->print(&Apache::lonxml::xmlparse($r,'tex',$message));
  820: 				}
  821: 				push(@html_ids,"hello");
  822: 			}
  823: 		}
  824: 	}
  825: 	
  826: 	return @html_ids;	
  827: }
  828: 
  829: sub convert_from_v2 {
  830: 	my ($r, $data_ref, $fields_ref, $conflict) = @_;
  831: 	my %data = %{$data_ref};
  832: 	my %fields = %{$fields_ref};
  833: 	my @fields_order = (!$conflict) ? () : @{thaw($data{'data.fields'})};
  834: 	my %old_new_map = (!$conflict) ? () : %{thaw($data{'data.old_new_map'})};
  835: 	my $repeat_int = 0;  #ensure fields with created timestamp are unique
  836: 	foreach my $element (sort(keys(%fields))) {
  837: 		my %new_element = ();
  838: 		my $title = $fields{$element};
  839: 		my $title_hash = time."_".$$;
  840: 		if (exists($data{'data.field.'.$title_hash})) {
  841: 			$title_hash .= "_".$repeat_int++;
  842: 		}
  843: 		my $content = $data{$element};
  844: 		$new_element{title} = $title;
  845: 		$new_element{content} = $content;
  846: 		if ($element eq 'lll_includeurl') {
  847: 			$new_element{type} = TYPE_URL_INCLUDE;
  848: 		} else {
  849: 			$new_element{type} = TYPE_TEXT_HTML;
  850: 		}
  851: 		if (!$conflict) {
  852: 			$r->print("Creating new field with ID: ".$title_hash."<br />");
  853: 			$data{'data.field.'.$title_hash} = freeze(\%new_element);
  854: 			$old_new_map{$element} = $title_hash;
  855: 			push(@fields_order, $title_hash);
  856: 		} else {
  857: 			if (exists($old_new_map{$element})) {
  858: 				$r->print("Transferring old field ".$element." to new ID: ".$old_new_map{$element}."<br />");
  859: 				if (exists($data{'data.field.'.$old_new_map{$element}})) {
  860: 					my %new_field = %{thaw($data{'data.field.'.$old_new_map{$element}})};
  861: 					$new_field{content} = $content;
  862: 					$data{'data.field.'.$old_new_map{$element}} = freeze(\%new_field);
  863: 				}
  864: 			} else {
  865: 				$data{'data.field.'.$title_hash} = freeze(\%new_element);
  866: 				$old_new_map{$element} = $title_hash;
  867: 				$data{'properties.v2_conflict_fail'} = 1;
  868: 				push(@fields_order, $title_hash);
  869: 			}
  870: 		}
  871: 	}
  872: 	$data{'data.fields'} = freeze(\@fields_order);
  873: 	$data{'data.old_new_map'} = freeze(\%old_new_map);
  874: 	$data{'properties.last_modified'} = time;
  875: 	$data{'properties.v2_converted'} = 1;
  876: 	$data{'properties.type'} = 'syllabus';
  877: 	
  878: 	return \%data;
  879: }
  880: 
  881: 1;
  882: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>