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

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.91      amueller  150:         my $start_page =
                    151:          &Apache::loncommon::start_page("Syllabus", $rss_link.$js,
1.97      amueller  152:                        {'function'       => undef,
1.91      amueller  153:                         'domain'         => $cdom,
                    154:                         'force_register' =>
                    155:                         $env{'form.register'},});
1.49      albertel  156: 
1.91      amueller  157:         $r->print($start_page);
1.65      raeburn   158:         if ($env{'form.backto'} eq 'coursecatalog') {
                    159:             &Apache::lonhtmlcommon::clear_breadcrumbs();
                    160:             &Apache::lonhtmlcommon::add_breadcrumb
1.90      amueller  161:                 ({href=>"javascript:ToCatalog()",
1.100     bisitz    162:                 text=>"Course/Community Catalog"});
1.65      raeburn   163:             if ($env{'form.coursenum'} ne '') {
                    164:                 &Apache::lonhtmlcommon::add_breadcrumb
1.90      amueller  165:                     ({href=>"javascript:ToCatalog('details')",
                    166:                     text=>"Course details"});
1.65      raeburn   167:             }
                    168:             &Apache::lonhtmlcommon::add_breadcrumb
1.90      amueller  169:                 ({href=>$r->uri,
                    170:                 text=>"Course syllabus"});
1.65      raeburn   171:             $r->print(&Apache::lonhtmlcommon::breadcrumbs());
1.86      bisitz    172:         }
1.17      www       173:     }
                    174: # ---------------------------------------------------------- Load syllabus info
1.106.2.1  faziophi  175:     my %syllabus=&Apache::lonnet::dump('syllabus',$cdom,$cnum);  # load db
                    176:     my $allowed=0;  # can we edit this page?
1.27      www       177:     my $privileged=0;
1.106.2.1  faziophi  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);
1.106.2.2! faziophi  192:     	delete $syllabus{'data.deleted_fields'};
        !           193:     	&Apache::lonnet::del('syllabus', ['data.deleted_fields'], $cdom, $cnum);
1.106.2.1  faziophi  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 />");
1.106.2.2! faziophi  200:    	$r->print("Deleted fields: ".$syllabus{'data.deleted_fields'}."<br />");
1.106.2.1  faziophi  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:     }
1.4       www       223: 
1.2       www       224: # ----------------------------------------------------- Only if not public call
1.106.2.1  faziophi  225:     if ($env{'user.environment'}) { # does this user have privileges to post, etc?
1.90      amueller  226:         if ($env{'request.course.id'}
1.91      amueller  227:         && $cdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}
                    228:         && $cnum eq $env{'course.'.$env{'request.course.id'}.'.num'}) {
1.90      amueller  229:             $allowed=&Apache::lonnet::allowed('mdc',$env{'request.course.id'});
1.91      amueller  230:             $privileged=$allowed;
1.106.2.1  faziophi  231:             if (($data{'uploaded.lastmodified'}) && (!$forceedit)) {
1.91      amueller  232:                 $forcestudent='student';
                    233:             }
1.90      amueller  234:             if ($forcestudent or $target eq 'tex') { $allowed=0; }
                    235:         }
1.98      amueller  236: 		#store what the user typed in
1.106.2.1  faziophi  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'})) {
1.106.2.2! faziophi  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);
1.106.2.1  faziophi  295: 			}
                    296: 		}
1.90      amueller  297:         if (($allowed) && ($env{'form.storesyl'})) {
1.106.2.1  faziophi  298:             foreach my $syl_field (@fields) {
1.90      amueller  299:                 my $field=$env{'form.'.$syl_field};
1.106.2.1  faziophi  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}) ?
1.106.2.2! faziophi  306:                 						%{thaw($data{'data.field.'.$syl_field})} : ();
1.106.2.1  faziophi  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:             	}
1.90      amueller  368:             }
1.106.2.1  faziophi  369:               
                    370:             &Apache::lonnet::put('syllabus',\%data,$cdom,$cnum);
1.90      amueller  371:         }
1.4       www       372:     }
1.85      bisitz    373: 
1.93      bisitz    374: #--------Functions
1.94      droeschl  375:     if( ($allowed || $privileged) && $target ne 'tex') {
                    376:         my $functions=&Apache::lonhtmlcommon::start_funclist();
                    377:         if ($allowed) {
1.97      amueller  378: 			#if you have the register flag, keep it
                    379: 			if($env{'form.register'} == 1) {
                    380:             	$functions.=&Apache::lonhtmlcommon::add_item_funclist(
1.99      amueller  381:                           '<a href="'.$r->uri.'?forcestudent=1&amp;register=1">'
1.97      amueller  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(
1.94      droeschl  387:                           '<a href="'.$r->uri.'?forcestudent=1">'
                    388:                            .&mt('Show Public View').'</a>'
                    389:                            .&Apache::loncommon::help_open_topic(
                    390:                                 'Uploaded_Templates_PublicView'));
1.97      amueller  391: 			}
1.93      bisitz    392:         } elsif ($privileged) {
1.97      amueller  393: 			if($env{'form.register'} == 1) {
                    394: 	            $functions.=&Apache::lonhtmlcommon::add_item_funclist(
1.99      amueller  395:                            '<a href="'.$r->uri.'?forceedit=1&amp;register=1">'
1.97      amueller  396:                             .&mt('Edit').'</a>');
                    397: 			} else {
                    398: 	            $functions.=&Apache::lonhtmlcommon::add_item_funclist(
1.94      droeschl  399:                            '<a href="'.$r->uri.'?forceedit=1">'
                    400:                             .&mt('Edit').'</a>');
1.97      amueller  401: 			}
1.93      bisitz    402:         }
1.94      droeschl  403: 
1.93      bisitz    404:         $functions.=&Apache::lonhtmlcommon::end_funclist();
                    405:         $r->print(&Apache::loncommon::head_subbox($functions));
                    406:     }
                    407: 
1.94      droeschl  408: #---------------------Print External URL Syllabus Info and Help Text
1.90      amueller  409:     if( ($allowed) && ($target ne 'tex') ) {
1.91      amueller  410:         my $protocol = $Apache::lonnet::protocol{$homeserver};
                    411:           $protocol = 'http' if ($protocol ne 'https');
1.85      bisitz    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">'
1.96      raeburn   418:                  .&mt('Instead of using this template you can specify an external URL as Syllabus in the [_1]Course Configuration[_2].'
1.99      amueller  419:                      ,'<a href="/adm/courseprefs?actions=courseinfo&amp;phase=display">','</a>')
1.85      bisitz    420:                  .'</p>'
                    421:         );
1.94      droeschl  422:         #-Print Help Text
                    423:         $r->print(&Apache::loncommon::help_open_topic( 
                    424:                         'Uploaded_Templates_TextBoxes',
                    425:                         &mt('Help with filling in text boxes')));
1.90      amueller  426:     }
1.85      bisitz    427: 
1.88      amueller  428: #----------Print last update
1.90      amueller  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') {
1.91      amueller  436:         $r->print('<div class="LC_info">'.&mt('Last updated').': '.
                    437:             $lastmod . ' '.
                    438:             ($who ? &mt('by').' '.$who
1.89      bisitz    439:                            : '' ) .
1.88      amueller  440:              '</div>' );
1.89      bisitz    441: 
1.90      amueller  442:     } else {
1.91      amueller  443:         $r->print('\\\\ '.&mt('Last updated').': '.$lastmod.' '.
                    444:             ($who? &mt('by').'\\\\ '.
                    445:                     &Apache::loncommon::plainname($syllabus{'uploaded.name'},$syllabus{'uploaded.domain'})
                    446:                   :'')
                    447:              .'\\\\');
1.90      amueller  448:     }
1.106.2.1  faziophi  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: 
1.80      neumanie  461: #----------------------------Print Headtitle
1.90      amueller  462:     if ($target ne 'tex') {
1.91      amueller  463:         $r->print('<h1>'.$courseenv{'description'}.'</h1>');
                    464:         $r->print('<h3>'.  &Apache::lonnet::domain($cdom,'description').'</h3>');
1.90      amueller  465:     } else {
1.91      amueller  466:         $r->print('\noindent{\large\textbf{'.$courseenv{'description'}.'}}\\\\\\\\\textbf{'.
                    467:         &Apache::lonnet::domain($cdom,'description').'}\\\\');
1.90      amueller  468:     }
1.106.2.1  faziophi  469: 
1.80      neumanie  470: # -------------------------------------------------------- Get course personnel
                    471:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles($cdom.'/'.$cnum);
                    472:     if ($target ne 'tex') {
1.91      amueller  473:         $r->print(&Apache::lonhtmlcommon::start_pick_box());
1.80      neumanie  474:     } else {
1.91      amueller  475:         $r->print('\begin{tabular}{|p{0.45\textwidth}|p{0.45\textwidth}|}\hline');
1.80      neumanie  476:     }
                    477:     my @personnel=sort(keys(%coursepersonnel));
                    478:     my $lastpers=$personnel[$#personnel];
                    479:     foreach my $element (@personnel) {
1.91      amueller  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:         }
1.80      neumanie  485:         foreach (split(/\,/,$coursepersonnel{$element})) {
1.91      amueller  486:             my ($puname,$pudom)=split(/\:/,$_);
                    487:             if ($target ne 'tex') {
1.80      neumanie  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')) {
1.91      amueller  491:                     $r->print(' '.$courseperson);
1.80      neumanie  492:                 } else {
                    493:                     $r->print(' '.&Apache::loncommon::aboutmewrapper($courseperson,
                    494:                               $puname,$pudom));
                    495:                 }
1.91      amueller  496:             } else {
                    497:                 $r->print(' '.&Apache::loncommon::plainname($puname,
1.80      neumanie  498:                               $pudom).' ');
1.91      amueller  499:             }
                    500:         }
                    501:         if ($target ne 'tex') {
1.80      neumanie  502:             my $lastclose=$element eq $lastpers?1:0;
                    503:             $r->print(&Apache::lonhtmlcommon::row_closure($lastclose));
1.91      amueller  504:         } else {
                    505:             $r->print('\\\\ \hline');
                    506:         }
1.80      neumanie  507:     }
                    508:     if ($target ne 'tex') {
1.91      amueller  509:         $r->print(&Apache::lonhtmlcommon::end_pick_box());
1.80      neumanie  510:     } else {
1.91      amueller  511:         $r->print('\end{tabular}\\\\');
1.80      neumanie  512:     }
1.79      neumanie  513: # -------------------------------------------------------------- Announcements?
                    514:     my $day = &Apache::lonannounce::showday(time,2,
1.91      amueller  515:              &Apache::lonannounce::readcalendar($cdom.'_'.$cnum));
1.80      neumanie  516:     if ($target ne 'tex') {
1.91      amueller  517:         if ($allowed) {
1.92      bisitz    518:             &Apache::lontemplate::print_start_template($r,'RSS Feeds and Blogs','LC_Box');
1.91      amueller  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 '') {
1.92      bisitz    524:             &Apache::lontemplate::print_start_template($r,'RSS Feeds and Blogs','LC_Box');
1.91      amueller  525:             $r->print(&Apache::lonrss::advertisefeeds($cnum,$cdom,$forceedit));
                    526:             &Apache::lontemplate::print_end_template($r);
                    527:         }
1.86      bisitz    528: 
1.79      neumanie  529:     } else {
1.91      amueller  530:         $r->print(&Apache::lonxml::xmlparse($r,'tex',$day));
1.86      bisitz    531:     }
1.106.2.1  faziophi  532:     
1.79      neumanie  533: # ---------------------------------------------------------------- Get syllabus
1.86      bisitz    534:     if (($syllabus{'uploaded.lastmodified'}) || ($allowed)) {
1.90      amueller  535:         if ($allowed) {
1.95      bisitz    536:             $r->print('<form method="post" action="">'.
1.91      amueller  537:             '<input type="hidden" name="forceedit" value="edit" />');
1.90      amueller  538:         }
                    539:         my @htmlids=();
1.106     faziophi  540: 		my $url_include_handler = sub {
1.106.2.1  faziophi  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;
1.106     faziophi  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 '') {
1.106.2.1  faziophi  556: 					   $message='';
1.106     faziophi  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);
1.106.2.1  faziophi  566: 				 &Apache::lontemplate::print_start_template($r,$title.
1.106     faziophi  567: 						  &Apache::loncommon::help_open_topic('Syllabus_URLs'),'LC_Box');
                    568: 				 $r->print($urls);
                    569: 				 $r->print("<br /><div>");
1.106.2.1  faziophi  570: 				 &Apache::lontemplate::print_textarea_template($r, $message,
1.106     faziophi  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);
1.86      bisitz    575: 
1.106     faziophi  576: 			} else {
                    577: 				$r->print($message);
                    578: 			}
                    579: 		};
1.106.2.1  faziophi  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);
1.90      amueller  583:         if ($allowed) {
1.91      amueller  584:             $r->print('</form>'.
                    585:             &Apache::lonhtmlcommon::htmlareaselectactive(@htmlids));
1.90      amueller  586:         }
1.4       www       587:     } else {
1.91      amueller  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>');}
1.1       www       591:     }
1.86      bisitz    592:     if ($target ne 'tex') {
1.65      raeburn   593:         if ($env{'form.backto'} eq 'coursecatalog') {
                    594:             $r->print('<form name="backtocat" method="post" action="/adm/coursecatalog">'.
1.66      raeburn   595:                       &Apache::lonhtmlcommon::echo_form_input(['backto','courseid']).
1.65      raeburn   596:                       '</form>');
                    597:         }
1.91      amueller  598:         $r->print(&Apache::loncommon::end_page());
1.48      albertel  599:     } else {
1.91      amueller  600:         $r->print('\end{document}');
1.48      albertel  601:     }
1.1       www       602:     return OK;
1.86      bisitz    603: }
1.1       www       604: 
1.106.2.1  faziophi  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'})) {
1.106.2.2! faziophi  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>");
1.106.2.1  faziophi  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: 
1.1       www       713: 1;
                    714: __END__

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