Annotation of loncom/interface/lonsyllabus.pm, revision 1.106.2.3

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

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