File:  [LON-CAPA] / loncom / interface / lonsyllabus.pm
Revision 1.84: download - view: text, annotated - select for diffs
Fri Mar 27 15:46:38 2009 UTC (15 years, 2 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Functionslist Optimizations:
- Changed sub routines to allow collection of output data instead of being forced to directly print.
   This allows a much wider range of usage in other moduls
  (Now return the output data instead of direct printing)
- Replaced hardcoded fieldset legend text by customized text (new parameter).
  Default text "Functions" if no parameter text provided.

Adjusted related scripts (lonaboutme.pm, lonsimplepage.pm, lonsyllabus.pm) accordingly.
- Added some line breaks for better code readability
- Removed some " " (-> CSS)

    1: # The LearningOnline Network
    2: # Syllabus
    3: #
    4: # $Id: lonsyllabus.pm,v 1.84 2009/03/27 15:46:38 bisitz 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: 
   44: sub handler {
   45:     my $r = shift;
   46:     &Apache::loncommon::content_type($r,'text/html');
   47:     $r->send_http_header;
   48:     return OK if $r->header_only;
   49: 
   50:     my $target=$env{'form.grade_target'};
   51: # --------------------------------------------------- Get course info from URL
   52:     my (undef,undef,$cdom,$cnum)=split(/\//,$r->uri);
   53: # ------------------------------------------------------------ Get query string
   54:     &Apache::loncommon::get_unprocessed_cgi
   55:                         ($ENV{'QUERY_STRING'},['forcestudent','register','forceedit','wrapperdisplay']);
   56: # ----------------------------------------------------- Is this even a course?
   57:     my $homeserver=&Apache::lonnet::homeserver($cnum,$cdom);
   58:     if ($homeserver eq 'no_host') {
   59:         &Apache::loncommon::content_type($r,'text/html');
   60:         $r->send_http_header;
   61:       	&Apache::loncommon::simple_error_page($r,'No syllabus available',
   62: 					      'No syllabus available');
   63:         return OK;
   64:     }
   65: # ------------------------------------- There is such a course, get environment
   66:     my %courseenv=&Apache::lonnet::dump('environment',$cdom,$cnum);
   67: 
   68: # ------------------------------------------------------------ Print the screen
   69: 
   70:     if ($target eq 'tex') {
   71: 	$r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'}));
   72:     } 
   73: # -------------------------------------------------- Let's see who handles this
   74:     my $externalsyllabus=$courseenv{'externalsyllabus'};
   75: 
   76:     if ($externalsyllabus=~/\w/) {
   77: 	
   78:        if ($env{'form.wrapperdisplay'} eq 'menu') {
   79: 	   $r->print(&Apache::lonwrapper::simple_menu());
   80:        } else {	    
   81: 	   $r->print(&Apache::lonwrapper::wrapper("/public/$cdom/$cnum/syllabus?wrapperdisplay=menu",
   82: 						   $externalsyllabus));
   83:        }
   84:        return OK;
   85:      } 
   86: 
   87: # ------------------------------ The buck stops here: internal syllabus display
   88: # --------------------------------------------------------- The syllabus fields
   89:     my %syllabusfields=&Apache::lonlocal::texthash(
   90:        'aaa_instructorinfo' => 'Instructor Information',
   91:        'bbb_description'    => 'Course Description',
   92:        'ccc_prereq'         => 'Prerequisites',
   93:        'cdc_classhours'     => 'Class Hours',
   94:        'ddd_officehours'    => 'Office Hours',
   95:        'eee_helproom'       => 'Helproom Hours',
   96:        'efe_projectinfo'    => 'Project Information',
   97:        'fff_examinfo'       => 'Exam Information',
   98:        'fgf_deadlines'      => 'Deadlines',
   99:        'ggg_grading'        => 'Grading Information',
  100:        'hhh_readings'       => 'Readings',
  101:        'iii_coursepack'     => 'Coursepack',
  102:        'jjj_weblinks'       => 'Web Links',
  103:        'kkk_textbook'       => 'Textbook',
  104:        'lll_includeurl'     => 'URLs To Include in Syllabus');
  105: # --------------------------------------------------------------- Force Student
  106:     my $forcestudent='';
  107:     if ($env{'form.forcestudent'}) { $forcestudent='student'; };
  108:     my $forceedit='';
  109:     if ($env{'form.forceedit'}) { $forceedit='edit'; }
  110:        
  111: # ----------------------------------------------------------------- Make header 
  112:     if ($target ne 'tex') {
  113: 	my $rss_link = &Apache::lonrss::rss_link($cnum,$cdom);
  114:         my $js;
  115:         if ($env{'form.backto'} eq 'coursecatalog') {
  116:             $js .= <<"ENDSCRIPT";
  117: 
  118: <script type="text/javascript">
  119: function ToCatalog(caller) {
  120:     numidx = getIndexByName('coursenum');
  121:     if (numidx > -1) {
  122:         if (caller != 'details') {
  123:             document.backtocat.elements[numidx].value = '';
  124:         }
  125:     }
  126:     document.backtocat.submit();
  127: }
  128: 
  129: function getIndexByName(item) {
  130:     for (var i=0;i<document.backtocat.elements.length;i++) {
  131:         if (document.backtocat.elements[i].name == item) {
  132:             return i;
  133:         }
  134:     }
  135:     return -1;
  136: }
  137: 
  138: </script>
  139: 
  140: ENDSCRIPT
  141:         }
  142: 	my $start_page = 
  143: 	    &Apache::loncommon::start_page("Syllabus", $rss_link.$js,
  144: 					   {'function'       => $forcestudent,
  145: 					    'domain'         => $cdom,
  146: 					    'force_register' =>
  147: 						$env{'form.register'},});
  148: 
  149: 	$r->print($start_page);
  150:         if ($env{'form.backto'} eq 'coursecatalog') {
  151:             &Apache::lonhtmlcommon::clear_breadcrumbs();
  152:             &Apache::lonhtmlcommon::add_breadcrumb
  153:              ({href=>"javascript:ToCatalog()",
  154:                text=>"Course Catalog"});
  155:             if ($env{'form.coursenum'} ne '') {
  156:                 &Apache::lonhtmlcommon::add_breadcrumb
  157:                   ({href=>"javascript:ToCatalog('details')",
  158:                    text=>"Course details"});
  159:             }
  160:             &Apache::lonhtmlcommon::add_breadcrumb
  161:               ({href=>$r->uri,
  162:                text=>"Course syllabus"});
  163:             $r->print(&Apache::lonhtmlcommon::breadcrumbs());
  164:         }	
  165: 	
  166:     }
  167: # ---------------------------------------------------------- Load syllabus info
  168:     my %syllabus=&Apache::lonnet::dump('syllabus',$cdom,$cnum);
  169:     my $allowed=0;
  170:     my $privileged=0;
  171: 
  172: # This handler might be called anonymously ...
  173: # ----------------------------------------------------- Only if not public call
  174:     if ($env{'user.environment'}) {
  175: # does this user have privileges to post, etc?
  176:        if ($env{'request.course.id'}
  177: 	   && $cdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}
  178: 	   && $cnum eq $env{'course.'.$env{'request.course.id'}.'.num'}) {
  179:           $allowed=&Apache::lonnet::allowed('mdc',$env{'request.course.id'});
  180: 	  $privileged=$allowed;
  181: 	  if (($syllabus{'uploaded.lastmodified'}) && (!$forceedit)) {
  182: 	      $forcestudent='student';
  183: 	  }
  184:           if ($forcestudent or $target eq 'tex') { $allowed=0; }
  185:        }    
  186:        if (($allowed) && ($env{'form.storesyl'})) {
  187: 	   foreach my $syl_field (keys(%syllabusfields)) {
  188:                my $field=$env{'form.'.$syl_field};
  189: 	       chomp($field);
  190:                $field=~s/\s+$//s;
  191: 	       $field=~s/^\s+//s;
  192: 	       $field=~s/\<br\s*\/*\>$//s;
  193: 	       $field=&Apache::lonfeedback::clear_out_html($field,1);
  194: 	       $syllabus{$syl_field}=$field;
  195:                if ($syl_field eq 'lll_includeurl') { # clean up included URLs
  196:                   my $field='';
  197: 	          foreach my $value (split(/\n/,$syllabus{$syl_field})) {
  198: 		      my $url=$value;
  199: # get rid of leading and trailing spaces
  200:                       $url=~s/^\s+//;
  201:                       $url=~s/\s+$//;
  202:                       if ($url=~m|^https?\://([^/]+)/(.+)$|) {
  203: 			  my $host = $1;
  204:                           my $remainder=$2;
  205: # remove the hostname from internal URLs
  206: 			  my $hostname = &Apache::lonnet::hostname($host);
  207: 			  my %all_hostnames = &Apache::lonnet::all_hostnames();
  208: 		          foreach my $possible_host (keys(%all_hostnames)) {
  209:                               if ($possible_host =~ /\Q$hostname\E/i) {
  210: 			         $url=$remainder;
  211: 			      }
  212: 		          }
  213: 		      }
  214: # norm internal URLs
  215:                       unless ($url=~/^https?\:/) {
  216: 		          $url=&Apache::lonnet::clutter($url);
  217:                       }
  218: # re-assemble field
  219:                       if ($url) {
  220: 		          $field.=$url."\n";
  221:                       }
  222: 		  }
  223:                   $syllabus{$syl_field}=$field;
  224: 	      }
  225:            }
  226:            $syllabus{'uploaded.domain'}=$env{'user.domain'};
  227:            $syllabus{'uploaded.name'}=$env{'user.name'};
  228:            $syllabus{'uploaded.lastmodified'}=time;
  229:            &Apache::lonnet::put('syllabus',\%syllabus,$cdom,$cnum);
  230:        }
  231:     }
  232: #-Pritn Help Text
  233: if ($target ne 'tex') {
  234: 	if($allowed){ 
  235: 		$r->print(&Apache::loncommon::help_open_topic('Uploaded_Templates_TextBoxes',&mt('Help with filling in text boxes')));
  236: 	}
  237: }
  238: #--------Functions
  239: if ($target ne 'tex') {
  240: 		if($allowed || $privileged){		
  241:                 $r->print(&Apache::lontemplate::start_functionslist());
  242: 		if($allowed){ 		
  243:                     $r->print(&Apache::lontemplate::item_functionslist(
  244:                         '<a href="'.$r->uri.'?forcestudent=1">'.&mt('Show Public View').'</a>'
  245:                        .&Apache::loncommon::help_open_topic('Uploaded_Templates_PublicView')));
  246: 		}elsif($privileged){
  247:                     $r->print(&Apache::lontemplate::item_functionslist(
  248:                         '<a href="'.$r->uri.'?forceedit=1">'.&mt('Edit').'</a>'));
  249: 		}
  250:                 $r->print(&Apache::lontemplate::end_functionslist());
  251: 		}
  252:  }
  253: #----------------------------Print Headtitle
  254: if($target ne 'tex'){
  255: 	$r->print('<h1>'.$courseenv{'description'}.'</h1>'); 	
  256: 	$r->print('<h3>'.  &Apache::lonnet::domain($cdom,'description').'</h3>');
  257: }else{
  258: 	$r->print('\noindent{\large\textbf{'.$courseenv{'description'}.'}}\\\\\\\\\textbf{'.
  259: 	&Apache::lonnet::domain($cdom,'description').'}\\\\');
  260: }
  261: # -------------------------------------------------------- Get course personnel
  262:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles($cdom.'/'.$cnum);
  263:     if ($target ne 'tex') {
  264: 	$r->print(&Apache::lonhtmlcommon::start_pick_box());
  265:     } else {
  266: 	$r->print('\begin{tabular}{|p{0.45\textwidth}|p{0.45\textwidth}|}\hline');
  267:     }
  268:     my @personnel=sort(keys(%coursepersonnel));
  269:     my $lastpers=$personnel[$#personnel];
  270:     foreach my $element (@personnel) {
  271: 	if ($target ne 'tex') {
  272: 	    $r->print(&Apache::lonhtmlcommon::row_title($element));
  273: 	} else {
  274: 	    $r->print(' '.&Apache::lonxml::xmlparse($r,'tex',$element).' & '); 
  275: 	}
  276:         foreach (split(/\,/,$coursepersonnel{$element})) {
  277: 	    my ($puname,$pudom)=split(/\:/,$_);
  278: 	    if ($target ne 'tex') {
  279:                 my $courseperson = &Apache::loncommon::plainname($puname,$pudom);
  280:                 if (($env{'user.name'} eq '') || ($env{'user.name'} eq 'public') ||
  281:                     ($env{'user.domain'} eq '') || ($env{'user.domain'} eq 'public')) {
  282: 		    $r->print(' '.$courseperson);
  283:                 } else {
  284:                     $r->print(' '.&Apache::loncommon::aboutmewrapper($courseperson,
  285:                               $puname,$pudom));
  286:                 }
  287: 	    } else {
  288: 		$r->print(' '.&Apache::loncommon::plainname($puname,
  289:                               $pudom).' ');
  290: 	    }
  291: 	}
  292: 	if ($target ne 'tex') {
  293:             my $lastclose=$element eq $lastpers?1:0;
  294:             $r->print(&Apache::lonhtmlcommon::row_closure($lastclose));
  295: 	} else {
  296: 	    $r->print('\\\\ \hline');
  297: 	}
  298:     }
  299:     if ($target ne 'tex') {
  300: 	$r->print(&Apache::lonhtmlcommon::end_pick_box());
  301:     } else {
  302: 	$r->print('\end{tabular}\\\\');
  303:     }
  304: #----------Print last update
  305:  my $lastmod=$syllabus{'uploaded.lastmodified'};
  306:        $lastmod=($lastmod?&Apache::lonlocal::locallocaltime($lastmod):&mt('never'));
  307:        my $who = &Apache::loncommon::aboutmewrapper(
  308:                     &Apache::loncommon::plainname($syllabus{'uploaded.name'},
  309:                      $syllabus{'uploaded.domain'}),$syllabus{'uploaded.name'},
  310:                      $syllabus{'uploaded.domain'});      
  311:  if ($target ne 'tex') {
  312: 	   $r->print('<table><tr><td>'.&mt('Last updated').':</td><td>'.
  313: 		     $lastmod.'</td><td>'.
  314: 		     ($who ? &mt('by').' '.$who
  315:                            : '' ).
  316: 		     '</td></tr></table>');
  317:        } else {
  318: 	   $r->print('\\\\ '.&mt('Last updated').': '.$lastmod.' '.
  319: 		     ($who? &mt('by').'\\\\ '.
  320: 		            &Apache::loncommon::plainname($syllabus{'uploaded.name'},$syllabus{'uploaded.domain'})
  321: 		          :'')
  322: 		     .'\\\\');
  323:        }
  324: #---------------------Print Extern URL Course Info
  325: if( ($allowed) && ($target ne 'tex') ) {
  326: 	my $protocol = $Apache::lonnet::protocol{$homeserver};
  327:   	$protocol = 'http' if ($protocol ne 'https');
  328: 	$r->print('<p>'.&mt('This syllabus can be publicly viewed at').' <tt>'.$protocol.'://'.&Apache::lonnet::hostname($homeserver).$r->uri.'</tt>'.&Apache::loncommon::help_open_topic('Syllabus_ExtLink').'</p>'.'<p>'.&mt('You can specify an external URL as Syllabus in the [_1].','<a href="/adm/parmset?action=crsenv">'.&mt('Course Parameters').'</a>').'</p>');
  329: }
  330: # -------------------------------------------------------------- Announcements?
  331:     my $day = &Apache::lonannounce::showday(time,2,
  332: 			 &Apache::lonannounce::readcalendar($cdom.'_'.$cnum));
  333:     if ($target ne 'tex') {
  334: 	if($allowed){
  335: 		&Apache::lontemplate::print_start_template($r,'RSS Feeds and Blogs','LC_ContentBoxSpecial');
  336: 		$r->print(&Apache::lonrss::advertisefeeds($cnum,$cdom,$forceedit));			
  337: 		&Apache::lontemplate::print_end_template($r);
  338: 	}
  339: 	elsif(&Apache::lonrss::advertisefeeds($cnum,$cdom) ne ''){		
  340: 		&Apache::lontemplate::print_start_template($r,'RSS Feeds and Blogs','LC_ContentBoxSpecial');
  341: 		$r->print(&Apache::lonrss::advertisefeeds($cnum,$cdom,$forceedit));		
  342: 		&Apache::lontemplate::print_end_template($r);
  343: 	}
  344: 	
  345:     } else {
  346: 	$r->print(&Apache::lonxml::xmlparse($r,'tex',$day));
  347:     }	
  348: # ---------------------------------------------------------------- Get syllabus
  349:     if (($syllabus{'uploaded.lastmodified'}) || ($allowed)) {     
  350:        if ($allowed) {
  351: 	   $r->print('<form method="post">'.
  352: 		     '<input type="hidden" name="forceedit" value="edit" />');
  353:        }
  354:        my @htmlids=();
  355: 	
  356: 	foreach my $field (sort(keys(%syllabusfields))) {
  357: 	   if (($syllabus{$field}=~/\w/) || ($allowed)) {
  358: 	       my $message=$syllabus{$field};
  359: 	       if ($field eq 'lll_includeurl') { # this is the "included" field
  360: 		   my $urls=$message;		
  361: 		   $message='';		  
  362: 		   foreach my $filelink (split(/\n/,$urls)) {			
  363: 		       my $output='';
  364: 			# embed style?
  365: 		       my ($curfext)=($filelink=~/\.([^\.]+)$/);		
  366: 		       my $embstyle=&Apache::loncommon::fileembstyle($curfext);			
  367: 		       if (($embstyle eq 'ssi') || ($curfext=~/\/$/)) {# make ssi call and remove everything but the body contents
  368: 			   $output=&Apache::lonnet::ssi_body($filelink);
  369: 		       } elsif ($embstyle eq 'img') {# embed as an image
  370: 			   $output='<img src="'.$filelink.'" />';
  371: 		       }
  372: 		       if($output ne ''){
  373: 		       		if ($target ne 'tex') {				
  374: 			  	 	$message.='<p>'.$output.'</p>';
  375: 		       		} else {
  376: 			   		$message.=' '.&Apache::lonxml::xmlparse($r,'tex','<p>'.$output.'</p>').' ';
  377: 		       		}
  378: 			}     
  379: 		   }
  380: 		   if ($allowed) {
  381: 		       $r->print('<h3>'.$syllabusfields{$field}.
  382: 			 &Apache::loncommon::help_open_topic('Syllabus_URLs').'</h3>');
  383: 		   } else {
  384: 		       $r->print($message);
  385: 		   } 
  386: 	       } else {
  387: 		   &Apache::lonfeedback::newline_to_br(\$message);
  388: 		   $message =~s|(https?\://[^\s]+)|<a href="$1"><tt>$1</tt></a>|g;
  389: 		   if ($allowed) {
  390: 		       $message=&Apache::lonspeller::markeduptext($message);
  391: 		   }
  392: 		   $message=&Apache::lontexconvert::msgtexconverted($message);
  393: 		   if ($target ne 'tex') {
  394: 			if($allowed){
  395: 				$r->print('<p>');
  396: 			}			
  397: 			&Apache::lontemplate::print_template($r, $syllabusfields{$field}, $message,$allowed,'LC_ContentBoxSpecial');
  398: 		   } else {
  399: 		       $r->print('\\\\\textbf{'.$syllabusfields{$field}.'}\\\\'.
  400: 				 &Apache::lonxml::xmlparse($r,'tex',$message).'\\\\');
  401: 		   }
  402: 		   push(@htmlids,$field);
  403: 	       }
  404: 	       if ($allowed) {
  405: 			if($target ne 'tex'){
  406: 				$r->print('</p>');
  407: 				&Apache::lontemplate::print_editbox_template($r, $syllabus{$field}, $field);
  408: 			}
  409: 			
  410: 	       }
  411: 	   }
  412:        }    
  413: 	
  414:        if ($allowed) {
  415: 	   $r->print('</form>'.
  416: 		     &Apache::lonhtmlcommon::htmlareaselectactive(@htmlids));
  417:        }
  418:       # if ($target ne 'tex') {$r->print('</p>');} else {$r->print('\\\\');}
  419:     } else {
  420: 	if ($target ne 'tex') {$r->print('<p>');} else {$r->print('\par ');} 
  421: 	$r->print(&mt('No syllabus information provided.'));
  422: 	if ($target ne 'tex') {$r->print('</p>');}
  423:     }
  424:     if ($target ne 'tex') {	
  425:         if ($env{'form.backto'} eq 'coursecatalog') {
  426:             $r->print('<form name="backtocat" method="post" action="/adm/coursecatalog">'.
  427:                       &Apache::lonhtmlcommon::echo_form_input(['backto','courseid']).
  428:                       '</form>');
  429:         }
  430: 	$r->print(&Apache::loncommon::end_page());
  431:     } else {
  432: 	$r->print('\end{document}');
  433:     }
  434:     return OK;
  435: } 
  436: 
  437: 1;
  438: __END__

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