File:  [LON-CAPA] / loncom / interface / lonsyllabus.pm
Revision 1.106.2.2: download - view: text, annotated - select for diffs
Mon Feb 15 16:05:32 2010 UTC (14 years, 3 months ago) by faziophi
Branches: bz6209
Diff to branchpoint 1.106: preferred, unified
Work-in-progress commit for bug 6209.

--> Adds capability to delete fields from display for lonsyllabus.
--> Allows basic field title renaming capability.

    1: # The LearningOnline Network
    2: # Syllabus
    3: #
    4: # $Id: lonsyllabus.pm,v 1.106.2.2 2010/02/15 16:05:32 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:         my $start_page =
  151:          &Apache::loncommon::start_page("Syllabus", $rss_link.$js,
  152:                        {'function'       => undef,
  153:                         'domain'         => $cdom,
  154:                         'force_register' =>
  155:                         $env{'form.register'},});
  156: 
  157:         $r->print($start_page);
  158:         if ($env{'form.backto'} eq 'coursecatalog') {
  159:             &Apache::lonhtmlcommon::clear_breadcrumbs();
  160:             &Apache::lonhtmlcommon::add_breadcrumb
  161:                 ({href=>"javascript:ToCatalog()",
  162:                 text=>"Course/Community Catalog"});
  163:             if ($env{'form.coursenum'} ne '') {
  164:                 &Apache::lonhtmlcommon::add_breadcrumb
  165:                     ({href=>"javascript:ToCatalog('details')",
  166:                     text=>"Course details"});
  167:             }
  168:             &Apache::lonhtmlcommon::add_breadcrumb
  169:                 ({href=>$r->uri,
  170:                 text=>"Course syllabus"});
  171:             $r->print(&Apache::lonhtmlcommon::breadcrumbs());
  172:         }
  173:     }
  174: # ---------------------------------------------------------- Load syllabus info
  175:     my %syllabus=&Apache::lonnet::dump('syllabus',$cdom,$cnum);  # load db
  176:     my $allowed=0;  # can we edit this page?
  177:     my $privileged=0;
  178:     my %data;
  179:     if ($env{'form.forceflush'}) {
  180:     	delete $syllabus{'data.fields'};
  181:     	&Apache::lonnet::del('syllabus', ['data.fields'], $cdom, $cnum);
  182:     	delete $syllabus{'properties.v2_conflict'};
  183:     	&Apache::lonnet::del('syllabus', ['properties.v2_conflict'], $cdom, $cnum);
  184:     	delete $syllabus{'properties.v2_conflict_fail'};
  185:     	&Apache::lonnet::del('syllabus', ['properties.v2_conflict_fail'], $cdom, $cnum);
  186:     	delete $syllabus{'properties.last_modified'};
  187:     	&Apache::lonnet::del('syllabus', ['properties.last_modified'], $cdom, $cnum);
  188:     	delete $syllabus{'properties.v2_converted'};
  189:     	&Apache::lonnet::del('syllabus', ['properties.v2_converted'], $cdom, $cnum);
  190:     	delete $syllabus{'data.old_new_map'};
  191:     	&Apache::lonnet::del('syllabus', ['data.old_new_map'], $cdom, $cnum);
  192:     	delete $syllabus{'data.deleted_fields'};
  193:     	&Apache::lonnet::del('syllabus', ['data.deleted_fields'], $cdom, $cnum);
  194: 		%syllabus=&Apache::lonnet::dump('syllabus',$cdom,$cnum);  # load db
  195:     	$r->print("Flushed syllabus DB file.<br />");
  196:     	$r->print("Syllabus conflict: ".$syllabus{'properties.v2_conflict'}."<br />");
  197:    	}
  198:    	$r->print("Existing fields: ".$syllabus{'data.fields'}."<br />");
  199:    	$r->print("Old-new map: ".$syllabus{'data.old_new_map'}."<br />");
  200:    	$r->print("Deleted fields: ".$syllabus{'data.deleted_fields'}."<br />");
  201:     if (!exists($syllabus{'data.fields'})) {
  202:     	# convert existing 2.x data to new DB fields
  203:     	# which become new primary data source for document
  204: 		%data = %{convert_from_v2($r, \%syllabus, \%syllabusfields, 0)};
  205: 		$r->print("New fields order: ".$data{'data.fields'}."<br />");
  206: 		&Apache::lonnet::put('syllabus',\%data,$cdom,$cnum);
  207:     } elsif (  !exists($syllabus{'properties.v2_converted'}) && 
  208:     		   exists($syllabus{'uploaded.lastmodified'}) &&
  209:     		   exists($syllabus{'properties.last_modified'}) &&
  210:     		   ($syllabus{'uploaded.lastmodified'} !=
  211:     		   $syllabus{'properties.last_modified'})) {
  212:     	# if the document has been saved in 3.x and later edited in 
  213:     	# 2.x, reconvert the existing document, with extra warning
  214:     	%data = %{convert_from_v2($r, \%syllabus, \%syllabusfields, 1)};
  215:     	delete $data{'properties.v2_converted'};
  216:     	&Apache::lonnet::del('syllabus', ['properties.v2_converted'], $cdom, $cnum);
  217:     	$data{'properties.v2_conflict'} = 1;
  218: 		&Apache::lonnet::put('syllabus',\%data,$cdom,$cnum);    	
  219:     } else {
  220:     	%data = %syllabus;
  221:     	
  222:     }
  223: 
  224: # ----------------------------------------------------- Only if not public call
  225:     if ($env{'user.environment'}) { # does this user have privileges to post, etc?
  226:         if ($env{'request.course.id'}
  227:         && $cdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}
  228:         && $cnum eq $env{'course.'.$env{'request.course.id'}.'.num'}) {
  229:             $allowed=&Apache::lonnet::allowed('mdc',$env{'request.course.id'});
  230:             $privileged=$allowed;
  231:             if (($data{'uploaded.lastmodified'}) && (!$forceedit)) {
  232:                 $forcestudent='student';
  233:             }
  234:             if ($forcestudent or $target eq 'tex') { $allowed=0; }
  235:         }
  236: 		#store what the user typed in
  237: 		my @fields = @{thaw($data{'data.fields'})};
  238: 		if (($allowed) && ($env{'form.delete'})) {
  239: 			my $field = $env{'form.delete'};
  240: 			chomp($field);
  241: 			#allow only numbers, underscores
  242: 			$field=~s/[^0-9_]//g;
  243: 			#check if the field exists
  244: 			#do not delete if file in v2 conversion mode
  245: 			if (exists($data{'data.field.'.$field}) &&
  246: 				!exists($data{'properties.v2_converted'})) {
  247: 				$r->print("Field '$field' can be deleted.<br />");
  248: 				# linearly parse "data.fields" and remove it
  249: 				for (my $i = 0; $i < length(@fields); $i++) {
  250: 					if ($fields[$i] eq $field) {
  251: 						splice(@fields, $i, 1);
  252: 						$r->print("Removed entry $i from 'data.fields'<br />");
  253: 					}
  254: 				}
  255: 				# if "data.deleted" does not exist, create it
  256: 				my @deleted;
  257: 				if (!exists($data{'data.deleted_fields'})) {
  258: 					@deleted = [];
  259: 				} else {
  260: 					@deleted = @{thaw($data{'data.deleted_fields'})};
  261: 				}
  262: 				# only if deleted does not exist in 'data.deleted', push it
  263: 				my $push = 1;
  264: 				for (my $i = 0; $i < length(@deleted); $i++) {
  265: 					if ($deleted[$i] eq $field) {
  266: 						$push = 0;
  267: 						last;
  268: 					}
  269: 				}
  270: 				unless (!$push) {
  271: 					push(@deleted, $field);
  272: 				}
  273: 				$data{'data.fields'} = freeze(\@fields);
  274: 				$data{'data.deleted_fields'} = freeze(\@deleted);
  275: 				
  276: 				&Apache::lonnet::put('syllabus',\%data,$cdom,$cnum);
  277: 			}
  278: 		}
  279: 		if (($allowed) && ($env{'form.rename'})) {
  280: 			my $field = $env{'form.rename'};
  281: 			my $new_title = "Hello, World!";
  282: 			chomp($field);
  283: 			$field=~s/[^0-9_]//g;
  284: 			#check if the field exists
  285: 			#do not delete if file in v2 conversion mode
  286: 			if (exists($data{'data.field.'.$field}) &&
  287: 				!exists($data{'properties.v2_converted'})) {
  288: 				#sanitize HTML content
  289: 				$r->print("Rename -- field found.<br />");
  290: 				my %db_entry = %{thaw($data{'data.field.'.$field})};
  291: 				$new_title = &Apache::lonfeedback::clear_out_html($new_title, 1);
  292: 				$db_entry{title} = $new_title;
  293: 				$data{'data.field.'.$field} = freeze(\%db_entry);
  294: 				&Apache::lonnet::put('syllabus',\%data,$cdom,$cnum);
  295: 			}
  296: 		}
  297:         if (($allowed) && ($env{'form.storesyl'})) {
  298:             foreach my $syl_field (@fields) {
  299:                 my $field=$env{'form.'.$syl_field};
  300:                 my $type;
  301:                 my %field_hash;
  302:                 # only update a field if it already exists!
  303:                 if (exists($data{'data.field.'.$syl_field})) {
  304:                 	$r->print("Creating/updated field ".$syl_field."<br />");
  305:                 	%field_hash = exists($data{'data.field.'.$syl_field}) ?
  306:                 						%{thaw($data{'data.field.'.$syl_field})} : ();
  307:                 	$type = exists($field_hash{type}) ? $field_hash{type} : TYPE_TEXT_HTML;
  308: 					chomp($field);
  309: 					$field=~s/\s+$//s;
  310: 					$field=~s/^\s+//s;
  311: 					$field=~s/\<br\s*\/*\>$//s;
  312: 					$field=&Apache::lonfeedback::clear_out_html($field,1);
  313: 					$field_hash{content}=$field;
  314: 					if ($type eq TYPE_URL_INCLUDE) { # clean up included URLs
  315: 						my $field='';
  316: 						foreach my $value (split(/\n/,$field_hash{content})) {
  317: 							my $url=$value;
  318: 							# get rid of leading and trailing spaces
  319: 							$url=~s/^\s+//;
  320: 							$url=~s/\s+$//;
  321: 							if ($url=~m|^https?\://([^/]+)/(.+)$|) {
  322: 								my $host = $1;
  323: 								my $remainder=$2;
  324: 								# remove the hostname from internal URLs
  325: 								my $hostname = &Apache::lonnet::hostname($host);
  326: 								my %all_hostnames = &Apache::lonnet::all_hostnames();
  327: 								foreach my $possible_host (keys(%all_hostnames)) {
  328: 									if ($possible_host =~ /\Q$hostname\E/i) {
  329: 										$url=$remainder;
  330: 									}
  331: 								}
  332: 							}
  333: 							# norm internal URLs
  334: 							unless ($url=~/^https?\:/) {
  335: 								$url=&Apache::lonnet::clutter($url);
  336: 							}
  337: 							# re-assemble field
  338: 							if ($url) {
  339: 								$field.=$url."\n";
  340: 							}
  341: 						}
  342: 	    	               $field_hash{content}=$field;
  343: 		                   $field_hash{type}=TYPE_URL_INCLUDE;
  344: 					}
  345: 					$data{'data.field.'.$syl_field} = freeze(\%field_hash);
  346: 				} 
  347:             }
  348:             $data{'uploaded.domain'}=$env{'user.domain'};
  349:             $data{'uploaded.name'}=$env{'user.name'};
  350:             my $time = $^T;
  351:             $data{'uploaded.lastmodified'}=$time;
  352:             $data{'properties.last_modified'}=$time;
  353:             delete $data{'properties.v2_converted'};
  354:             delete $data{'properties.v2_conflict'};
  355:             delete $data{'properties.v2_conflict_fail'};
  356:             &Apache::lonnet::del('syllabus', ['properties.v2_converted', 
  357:             	'properties.v2_conflict', 'properties.v2_conflict_fail'], $cdom, $cnum);
  358:             	
  359:             #2.x compatibility: write to old fields with new mapped fields
  360:             my %old_new_map = %{thaw($data{'data.old_new_map'})};
  361:             foreach my $old_field (keys(%old_new_map)) {
  362:             	$r->print("Looking for: ".$old_field." at ".$old_new_map{$old_field}."<br />");
  363:             	if (exists($data{'data.field.'.$old_new_map{$old_field}})) {
  364:             		$r->print("updating old field ".$old_field."<br />");
  365:             		my %new_field = %{thaw($data{'data.field.'.$old_new_map{$old_field}})};
  366:             		$data{$old_field} = $new_field{content};
  367:             	}
  368:             }
  369:               
  370:             &Apache::lonnet::put('syllabus',\%data,$cdom,$cnum);
  371:         }
  372:     }
  373: 
  374: #--------Functions
  375:     if( ($allowed || $privileged) && $target ne 'tex') {
  376:         my $functions=&Apache::lonhtmlcommon::start_funclist();
  377:         if ($allowed) {
  378: 			#if you have the register flag, keep it
  379: 			if($env{'form.register'} == 1) {
  380:             	$functions.=&Apache::lonhtmlcommon::add_item_funclist(
  381:                           '<a href="'.$r->uri.'?forcestudent=1&amp;register=1">'
  382:                            .&mt('Show Public View').'</a>'
  383:                            .&Apache::loncommon::help_open_topic(
  384:                                 'Uploaded_Templates_PublicView'));
  385: 			} else {
  386:             	$functions.=&Apache::lonhtmlcommon::add_item_funclist(
  387:                           '<a href="'.$r->uri.'?forcestudent=1">'
  388:                            .&mt('Show Public View').'</a>'
  389:                            .&Apache::loncommon::help_open_topic(
  390:                                 'Uploaded_Templates_PublicView'));
  391: 			}
  392:         } elsif ($privileged) {
  393: 			if($env{'form.register'} == 1) {
  394: 	            $functions.=&Apache::lonhtmlcommon::add_item_funclist(
  395:                            '<a href="'.$r->uri.'?forceedit=1&amp;register=1">'
  396:                             .&mt('Edit').'</a>');
  397: 			} else {
  398: 	            $functions.=&Apache::lonhtmlcommon::add_item_funclist(
  399:                            '<a href="'.$r->uri.'?forceedit=1">'
  400:                             .&mt('Edit').'</a>');
  401: 			}
  402:         }
  403: 
  404:         $functions.=&Apache::lonhtmlcommon::end_funclist();
  405:         $r->print(&Apache::loncommon::head_subbox($functions));
  406:     }
  407: 
  408: #---------------------Print External URL Syllabus Info and Help Text
  409:     if( ($allowed) && ($target ne 'tex') ) {
  410:         my $protocol = $Apache::lonnet::protocol{$homeserver};
  411:           $protocol = 'http' if ($protocol ne 'https');
  412:         $r->print('<p class="LC_info">'
  413:                  .&mt('This syllabus can be publicly viewed at [_1]'
  414:                      ,'<tt>'.$protocol.'://'.&Apache::lonnet::hostname($homeserver).$r->uri.'</tt>')
  415:                  .'&nbsp;'.&Apache::loncommon::help_open_topic('Syllabus_ExtLink')
  416:                  .'</p>'
  417:                  .'<p class="LC_info">'
  418:                  .&mt('Instead of using this template you can specify an external URL as Syllabus in the [_1]Course Configuration[_2].'
  419:                      ,'<a href="/adm/courseprefs?actions=courseinfo&amp;phase=display">','</a>')
  420:                  .'</p>'
  421:         );
  422:         #-Print Help Text
  423:         $r->print(&Apache::loncommon::help_open_topic( 
  424:                         'Uploaded_Templates_TextBoxes',
  425:                         &mt('Help with filling in text boxes')));
  426:     }
  427: 
  428: #----------Print last update
  429:     my $lastmod=$syllabus{'uploaded.lastmodified'};
  430:     $lastmod=($lastmod?&Apache::lonlocal::locallocaltime($lastmod):&mt('never'));
  431:     my $who = &Apache::loncommon::aboutmewrapper(
  432:         &Apache::loncommon::plainname($syllabus{'uploaded.name'},
  433:         $syllabus{'uploaded.domain'}),$syllabus{'uploaded.name'},
  434:         $syllabus{'uploaded.domain'});
  435:     if ($target ne 'tex') {
  436:         $r->print('<div class="LC_info">'.&mt('Last updated').': '.
  437:             $lastmod . ' '.
  438:             ($who ? &mt('by').' '.$who
  439:                            : '' ) .
  440:              '</div>' );
  441: 
  442:     } else {
  443:         $r->print('\\\\ '.&mt('Last updated').': '.$lastmod.' '.
  444:             ($who? &mt('by').'\\\\ '.
  445:                     &Apache::loncommon::plainname($syllabus{'uploaded.name'},$syllabus{'uploaded.domain'})
  446:                   :'')
  447:              .'\\\\');
  448:     }
  449:     if ($allowed && $data{'properties.v2_converted'} == 1) {
  450:     	$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/>");
  451: 	}
  452: 	if ($allowed && $data{'properties.v2_conflict'} == 1) {
  453: 		$r->print("<em>This document was saved with LON-CAPA 3.x, then further edited in LON-CAPA 2.x.</em><br/>");
  454: 		if ($data{'properties.v2_conflict_fail'} == 1) {
  455: 			$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 />");
  456: 		} else {
  457: 			$r->print("<em>These changes were automatically transferred to LON-CAPA 3.x</em>");
  458: 		}
  459: 	}
  460: 
  461: #----------------------------Print Headtitle
  462:     if ($target ne 'tex') {
  463:         $r->print('<h1>'.$courseenv{'description'}.'</h1>');
  464:         $r->print('<h3>'.  &Apache::lonnet::domain($cdom,'description').'</h3>');
  465:     } else {
  466:         $r->print('\noindent{\large\textbf{'.$courseenv{'description'}.'}}\\\\\\\\\textbf{'.
  467:         &Apache::lonnet::domain($cdom,'description').'}\\\\');
  468:     }
  469: 
  470: # -------------------------------------------------------- Get course personnel
  471:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles($cdom.'/'.$cnum);
  472:     if ($target ne 'tex') {
  473:         $r->print(&Apache::lonhtmlcommon::start_pick_box());
  474:     } else {
  475:         $r->print('\begin{tabular}{|p{0.45\textwidth}|p{0.45\textwidth}|}\hline');
  476:     }
  477:     my @personnel=sort(keys(%coursepersonnel));
  478:     my $lastpers=$personnel[$#personnel];
  479:     foreach my $element (@personnel) {
  480:         if ($target ne 'tex') {
  481:             $r->print(&Apache::lonhtmlcommon::row_title($element));
  482:         } else {
  483:             $r->print(' '.&Apache::lonxml::xmlparse($r,'tex',$element).' & ');
  484:         }
  485:         foreach (split(/\,/,$coursepersonnel{$element})) {
  486:             my ($puname,$pudom)=split(/\:/,$_);
  487:             if ($target ne 'tex') {
  488:                 my $courseperson = &Apache::loncommon::plainname($puname,$pudom);
  489:                 if (($env{'user.name'} eq '') || ($env{'user.name'} eq 'public') ||
  490:                     ($env{'user.domain'} eq '') || ($env{'user.domain'} eq 'public')) {
  491:                     $r->print(' '.$courseperson);
  492:                 } else {
  493:                     $r->print(' '.&Apache::loncommon::aboutmewrapper($courseperson,
  494:                               $puname,$pudom));
  495:                 }
  496:             } else {
  497:                 $r->print(' '.&Apache::loncommon::plainname($puname,
  498:                               $pudom).' ');
  499:             }
  500:         }
  501:         if ($target ne 'tex') {
  502:             my $lastclose=$element eq $lastpers?1:0;
  503:             $r->print(&Apache::lonhtmlcommon::row_closure($lastclose));
  504:         } else {
  505:             $r->print('\\\\ \hline');
  506:         }
  507:     }
  508:     if ($target ne 'tex') {
  509:         $r->print(&Apache::lonhtmlcommon::end_pick_box());
  510:     } else {
  511:         $r->print('\end{tabular}\\\\');
  512:     }
  513: # -------------------------------------------------------------- Announcements?
  514:     my $day = &Apache::lonannounce::showday(time,2,
  515:              &Apache::lonannounce::readcalendar($cdom.'_'.$cnum));
  516:     if ($target ne 'tex') {
  517:         if ($allowed) {
  518:             &Apache::lontemplate::print_start_template($r,'RSS Feeds and Blogs','LC_Box');
  519:             $r->print(&Apache::lonrss::advertisefeeds($cnum,$cdom,$forceedit));
  520:             my $editurl= &Apache::lonnet::absolute_url().'/adm/'.$cdom.'/'.$cnum.'/_rss.html';
  521:             $r->print( '<a href="'.$editurl.'">'.&mt('New RSS Feed or Blog').'</a>');
  522:             &Apache::lontemplate::print_end_template($r);
  523:         } elsif (&Apache::lonrss::advertisefeeds($cnum,$cdom) ne '') {
  524:             &Apache::lontemplate::print_start_template($r,'RSS Feeds and Blogs','LC_Box');
  525:             $r->print(&Apache::lonrss::advertisefeeds($cnum,$cdom,$forceedit));
  526:             &Apache::lontemplate::print_end_template($r);
  527:         }
  528: 
  529:     } else {
  530:         $r->print(&Apache::lonxml::xmlparse($r,'tex',$day));
  531:     }
  532:     
  533: # ---------------------------------------------------------------- Get syllabus
  534:     if (($syllabus{'uploaded.lastmodified'}) || ($allowed)) {
  535:         if ($allowed) {
  536:             $r->print('<form method="post" action="">'.
  537:             '<input type="hidden" name="forceedit" value="edit" />');
  538:         }
  539:         my @htmlids=();
  540: 		my $url_include_handler = sub {
  541: 			my ($r, $field, $json_ref, $group, $target, $allowed) = @_;
  542: 			my $message = $json_ref->{items}{$field}{content};
  543: 			my $title = $json_ref->{items}{$field}{title};
  544: 			my $urls = $message;
  545: 			foreach my $filelink (split(/\n/,$urls)) {
  546: 				my $output='';
  547: 			   # embed style?
  548: 				my ($curfext)=($filelink=~/\.([^\.]+)$/);
  549: 				my $embstyle=&Apache::loncommon::fileembstyle($curfext);
  550: 				if (($embstyle eq 'ssi') || ($curfext=~/\/$/)) {# make ssi call and remove everything but the body contents
  551: 					$output=&Apache::lonnet::ssi_body($filelink);
  552: 				} elsif ($embstyle eq 'img') {# embed as an image
  553: 					$output='<img src="'.$filelink.'" />';
  554: 				}
  555: 				if ($output ne '') {
  556: 					   $message='';
  557: 					   if ($target ne 'tex') {
  558: 						   $message.='<p>'.$output.'</p>';
  559: 					   } else {
  560: 						   $message.=' '.&Apache::lonxml::xmlparse($r,'tex','<p>'.$output.'</p>').' ';
  561: 					   }
  562: 				}
  563: 			}
  564: 			if ($allowed) {
  565: 				 &Apache::lonfeedback::newline_to_br(\$urls);
  566: 				 &Apache::lontemplate::print_start_template($r,$title.
  567: 						  &Apache::loncommon::help_open_topic('Syllabus_URLs'),'LC_Box');
  568: 				 $r->print($urls);
  569: 				 $r->print("<br /><div>");
  570: 				 &Apache::lontemplate::print_textarea_template($r, $message,
  571: 					$field, Apache::lontemplate->RICH_TEXT_ALWAYS_OFF);
  572: 				 &Apache::lontemplate::print_saveall_template($r);                         
  573: 				 $r->print("</div>");
  574: 				 &Apache::lontemplate::print_end_template($r);
  575: 
  576: 			} else {
  577: 				$r->print($message);
  578: 			}
  579: 		};
  580: 		my %custom_hash = ( TYPE_URL_INCLUDE() => $url_include_handler );
  581:  		@htmlids = &print_template_new_fields($r, \%data, 
  582:  			$target, $allowed, Apache::lontemplate->RICH_TEXT_DETECT_HTML, \%custom_hash);
  583:         if ($allowed) {
  584:             $r->print('</form>'.
  585:             &Apache::lonhtmlcommon::htmlareaselectactive(@htmlids));
  586:         }
  587:     } else {
  588:         if ($target ne 'tex') {$r->print('<p>');} else {$r->print('\par ');}
  589:         $r->print(&mt('No syllabus information provided.'));
  590:         if ($target ne 'tex') {$r->print('</p>');}
  591:     }
  592:     if ($target ne 'tex') {
  593:         if ($env{'form.backto'} eq 'coursecatalog') {
  594:             $r->print('<form name="backtocat" method="post" action="/adm/coursecatalog">'.
  595:                       &Apache::lonhtmlcommon::echo_form_input(['backto','courseid']).
  596:                       '</form>');
  597:         }
  598:         $r->print(&Apache::loncommon::end_page());
  599:     } else {
  600:         $r->print('\end{document}');
  601:     }
  602:     return OK;
  603: }
  604: 
  605: sub print_template_new_fields {
  606: 	my ($r, $data_ref, $target, $allowed, $default_rich_text, $custom_handlers_ref, $group) = @_;
  607: 	my @html_ids = ();
  608: 	my %data = %{$data_ref};
  609: 	my @fields = @{thaw($data{'data.fields'})};
  610: 	my %custom_handlers = %{$custom_handlers_ref};
  611: 
  612: 	foreach my $key (@fields) {
  613: 		my %field = %{thaw($data{'data.field.'.$key})};
  614: 		my $title = $field{title};
  615: 		my $raw_message = $field{content};
  616: 		my $type = $field{type};
  617: 		my $message = $raw_message if (($raw_message=~/\w/) || ($allowed));
  618: 		if ((%custom_handlers) && exists($custom_handlers{$type})) {
  619: 			#$custom_handlers{$type}->($r, $field, $json_ref, $group, $target, $allowed);
  620: 		} else {
  621: 			if (($raw_message=~/\w/) || ($allowed)) {
  622: 				if (!&Apache::lonfeedback::contains_block_html($message)) {
  623: 					&Apache::lonfeedback::newline_to_br(\$message);
  624: 				} else {
  625: 					$message = &Apache::lonfeedback::tidy_html($message);
  626: 				}
  627: 				$message=&Apache::lonhtmlcommon::raw_href_to_link($message);
  628: 				if ($allowed) {
  629: 					$message=&Apache::lonspeller::markeduptext($message);
  630: 				}
  631: 				$message=&Apache::lontexconvert::msgtexconverted($message);
  632: 				if ($target ne 'tex') {
  633: 					#output of syllabusfields will be generated here. 
  634: 					&Apache::lontemplate::print_start_template($r,$title,'LC_Box');
  635: 					$r->print($message);
  636: 					if ($allowed) {
  637: 						$r->print("<br /><div>");
  638: 						&Apache::lontemplate::print_textarea_template($r, $raw_message,
  639: 							$key, $default_rich_text);
  640: 						&Apache::lontemplate::print_saveall_template($r);
  641: 						if (!exists($data{'properties.v2_converted'})) {
  642: 							$r->print("<a href='?delete=$key&forceedit=1'>Delete</a> ");
  643: 							$r->print("<a href='?rename=$key&forceedit=1'>Rename to \"Hello, World!\"</a>");
  644: 						}
  645: 						$r->print("</div>");
  646: 					} 
  647: 					&Apache::lontemplate::print_end_template($r);
  648: 				} else {
  649: 				    my $safeinit;
  650:                     $r->print(&Apache::lonxml::xmlparse($r,'tex','<h3>'.$title.'</h3>'));
  651:                     $r->print(&Apache::lonxml::xmlparse($r,'tex',$message));
  652: 				}
  653: 				push(@html_ids,"hello");
  654: 			}
  655: 		}
  656: 	}
  657: 	
  658: 	return @html_ids;	
  659: }
  660: 
  661: sub convert_from_v2 {
  662: 	my ($r, $data_ref, $fields_ref, $conflict) = @_;
  663: 	my %data = %{$data_ref};
  664: 	my %fields = %{$fields_ref};
  665: 	my @fields_order = (!$conflict) ? () : @{thaw($data{'data.fields'})};
  666: 	my %old_new_map = (!$conflict) ? () : %{thaw($data{'data.old_new_map'})};
  667: 	my $repeat_int = 0;  #ensure fields with created timestamp are unique
  668: 	foreach my $element (sort(keys(%fields))) {
  669: 		my %new_element = ();
  670: 		my $title = $fields{$element};
  671: 		my $title_hash = time."_".$$;
  672: 		if (exists($data{'data.field.'.$title_hash})) {
  673: 			$title_hash .= "_".$repeat_int++;
  674: 		}
  675: 		my $content = $data{$element};
  676: 		$new_element{title} = $title;
  677: 		$new_element{content} = $content;
  678: 		if ($element eq 'lll_includeurl') {
  679: 			$new_element{type} = TYPE_URL_INCLUDE;
  680: 		} else {
  681: 			$new_element{type} = TYPE_TEXT_HTML;
  682: 		}
  683: 		if (!$conflict) {
  684: 			$r->print("Creating new field with ID: ".$title_hash."<br />");
  685: 			$data{'data.field.'.$title_hash} = freeze(\%new_element);
  686: 			$old_new_map{$element} = $title_hash;
  687: 			push(@fields_order, $title_hash);
  688: 		} else {
  689: 			if (exists($old_new_map{$element})) {
  690: 				$r->print("Transferring old field ".$element." to new ID: ".$old_new_map{$element}."<br />");
  691: 				if (exists($data{'data.field.'.$old_new_map{$element}})) {
  692: 					my %new_field = %{thaw($data{'data.field.'.$old_new_map{$element}})};
  693: 					$new_field{content} = $content;
  694: 					$data{'data.field.'.$old_new_map{$element}} = freeze(\%new_field);
  695: 				}
  696: 			} else {
  697: 				$data{'data.field.'.$title_hash} = freeze(\%new_element);
  698: 				$old_new_map{$element} = $title_hash;
  699: 				$data{'properties.v2_conflict_fail'} = 1;
  700: 				push(@fields_order, $title_hash);
  701: 			}
  702: 		}
  703: 	}
  704: 	$data{'data.fields'} = freeze(\@fields_order);
  705: 	$data{'data.old_new_map'} = freeze(\%old_new_map);
  706: 	$data{'properties.last_modified'} = time;
  707: 	$data{'properties.v2_converted'} = 1;
  708: 	$data{'properties.type'} = 'syllabus';
  709: 	
  710: 	return \%data;
  711: }
  712: 
  713: 1;
  714: __END__

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