File:  [LON-CAPA] / loncom / interface / lonsyllabus.pm
Revision 1.114: download - view: text, annotated - select for diffs
Mon Dec 3 23:53:03 2012 UTC (11 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- No breadcrumbs unless syllabus is being viewed in course and register=1,
  or the current user can edit it.

    1: # The LearningOnline Network
    2: # Syllabus
    3: #
    4: # $Id: lonsyllabus.pm,v 1.114 2012/12/03 23:53:03 raeburn 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'},['register','forceedit']);
   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:     } elsif (!&Apache::lonnet::is_course($cdom,$cnum)) {
   65:         &Apache::loncommon::content_type($r,'text/html');
   66:         $r->send_http_header;
   67:         &Apache::loncommon::simple_error_page($r,'No syllabus available',
   68:                           'The course/community for which the syllabus was requested does not exist.');
   69:         return OK;
   70:     }
   71: # ------------------------------------- There is such a course, get environment
   72:     my %courseenv=&Apache::lonnet::dump('environment',$cdom,$cnum);
   73: 
   74: # ------------------------------------------------------------ Print the screen
   75: 
   76:     if ($target eq 'tex') {
   77:         $r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'}));
   78:     }
   79: # -------------------------------------------------- Let's see who handles this
   80:     my $externalsyllabus=$courseenv{'externalsyllabus'};
   81: 
   82:     if ($externalsyllabus=~/\w/) {
   83:         $r->print( Apache::lonwrapper::wrapper($externalsyllabus) );
   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 ($forceedit,$forcestudent);
  107:     if ($env{'form.forceedit'}) { $forceedit=1; }
  108:     if (!$forceedit) {
  109:         $forcestudent=1;
  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 $forcereg;
  143:         if ($env{'form.register'}) {
  144:             $forcereg = 1;
  145:         }
  146:         my $brcrum = [];
  147:         if ($env{'form.backto'} eq 'coursecatalog') {
  148:             &Apache::lonhtmlcommon::clear_breadcrumbs();
  149:             $brcrum = [{href=>"javascript:ToCatalog();",
  150:                         text=>&mt('Course/Community Catalog'),
  151:                         no_mt=>1}
  152:                       ];
  153:             if ($env{'form.coursenum'} ne '') {
  154:                 push(@{$brcrum},
  155:                       {href=>"javascript:ToCatalog('details')",
  156:                        text=>"Course details"});
  157:             }
  158:             push(@{$brcrum},
  159:                   {href=>$r->uri,
  160:                    text=>"Course syllabus"});
  161:         }
  162:         my $args = {'function'       => undef,
  163:                     'domain'         => $cdom,
  164:                     'force_register' => $forcereg,};
  165:         if (@{$brcrum} > 0) {
  166:             $args->{'bread_crumbs'} = $brcrum;
  167:         }
  168:         my $start_page =
  169:             &Apache::loncommon::start_page("Syllabus", $rss_link.$js,$args);
  170:         $r->print($start_page);
  171:     }
  172: # ---------------------------------------------------------- Load syllabus info
  173:     my %syllabus=&Apache::lonnet::dump('syllabus',$cdom,$cnum);
  174:     my $allowed=0;
  175: 
  176: # This handler might be called anonymously ...
  177: # ----------------------------------------------------- Only if not public call
  178:     if ($env{'user.environment'}) {
  179: # does this user have privileges to post, etc?
  180:         if ($env{'request.course.id'}
  181:         && $cdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}
  182:         && $cnum eq $env{'course.'.$env{'request.course.id'}.'.num'}) {
  183:             $allowed=&Apache::lonnet::allowed('mdc',$env{'request.course.id'});
  184:             if ($forcestudent or $target eq 'tex') { $allowed=0; }
  185:         }
  186: #store what the user typed in
  187:         if (($allowed) && ($env{'form.storesyl'})) {
  188:             foreach my $syl_field (keys(%syllabusfields)) {
  189:                 my $field=$env{'form.'.$syl_field};
  190:                 chomp($field);
  191:                 $field=~s/\s+$//s;
  192:                 $field=~s/^\s+//s;
  193:                 $field=~s/\<br\s*\/*\>$//s;
  194:                 $field=&Apache::lonfeedback::clear_out_html($field,1);
  195: 				#here it will be stored
  196:                 $syllabus{$syl_field}=$field;
  197:                 if ($syl_field eq 'lll_includeurl') { # clean up included URLs
  198:                     my $field='';
  199:                     foreach my $value (split(/\n/,$syllabus{$syl_field})) {
  200:                         my $url=$value;
  201: # get rid of leading and trailing spaces
  202:                         $url=~s/^\s+//;
  203:                         $url=~s/\s+$//;
  204:                         if ($url=~m|^https?\://([^/]+)/(.+)$|) {
  205:                             my $host = $1;
  206:                             my $remainder=$2;
  207: # remove the hostname from internal URLs
  208:                             my $hostname = &Apache::lonnet::hostname($host);
  209:                             my %all_hostnames = &Apache::lonnet::all_hostnames();
  210:                             foreach my $possible_host (keys(%all_hostnames)) {
  211:                                 if ($possible_host =~ /\Q$hostname\E/i) {
  212:                                     $url=$remainder;
  213:                                 }
  214:                             }
  215:                         }
  216: # norm internal URLs
  217:                         unless ($url=~/^https?\:/) {
  218:                             $url=&Apache::lonnet::clutter($url);
  219:                         }
  220: # re-assemble field
  221:                         if ($url) {
  222:                             $field.=$url."\n";
  223:                         }
  224:                     }
  225:                     $syllabus{$syl_field}=$field;
  226:                 }
  227:             }
  228:             $syllabus{'uploaded.domain'}=$env{'user.domain'};
  229:             $syllabus{'uploaded.name'}=$env{'user.name'};
  230:             $syllabus{'uploaded.lastmodified'}=time;
  231:             &Apache::lonnet::put('syllabus',\%syllabus,$cdom,$cnum);
  232:         }
  233:     }
  234: 
  235:     if ($allowed) {
  236: #---------------------------------- Print External URL Syllabus Info if editing
  237:         if ($target ne 'tex') {
  238:             my $protocol = $Apache::lonnet::protocol{$homeserver};
  239:             $protocol = 'http' if ($protocol ne 'https');
  240:             $r->print('<div class="LC_info">'
  241:                      .'<p>'
  242:                      .&mt('This syllabus can be publicly viewed at [_1]'
  243:                           ,'<tt>'.$protocol.'://'.&Apache::lonnet::hostname($homeserver).$r->uri.'</tt>')
  244:                      .'&nbsp;'.&Apache::loncommon::help_open_topic('Syllabus_ExtLink')
  245:                      .'</p>'
  246:                      .'<p>'
  247:                      .&mt('Instead of using this template you can specify an external URL as Syllabus in the [_1]Course Configuration[_2].'
  248:                           ,'<a href="/adm/courseprefs?actions=courseinfo&amp;phase=display">','</a>')
  249:                      .'</p>'
  250:                      .'</div>');
  251:         }
  252:     } else {
  253: #--------------------------------------------- Print last update unless editing
  254:         my $lastmod=$syllabus{'uploaded.lastmodified'};
  255:         $lastmod=($lastmod?&Apache::lonlocal::locallocaltime($lastmod):&mt('never'));
  256:         my $who;
  257:         if ($syllabus{'uploaded.lastmodified'}) {
  258:             if (($env{'user.name'} ne 'public') && ($env{'user.domain'} ne 'public')) {
  259:                 $who = &Apache::loncommon::aboutmewrapper(
  260:                        &Apache::loncommon::plainname($syllabus{'uploaded.name'},
  261:                        $syllabus{'uploaded.domain'}),$syllabus{'uploaded.name'},
  262:                        $syllabus{'uploaded.domain'});
  263:             } else {
  264: # Public user?
  265: # Only display name of user, but no link to personal information page
  266:                 $who = &Apache::loncommon::plainname(
  267:                            $syllabus{'uploaded.name'},
  268:                            $syllabus{'uploaded.domain'});
  269:             }
  270:         }
  271:         if ($target ne 'tex') {
  272:             $r->print('<div class="LC_info">'.&mt('Last updated').': '.
  273:                       $lastmod . ' '.
  274:                       ($who ? &mt('by').' '.$who
  275:                            : '' ) .
  276:                       '</div>' );
  277:         } else {
  278:             $r->print('\\\\ '.&mt('Last updated').': '.$lastmod.' '.
  279:                      ($who? &mt('by').'\\\\ '.
  280:                      &Apache::loncommon::plainname($syllabus{'uploaded.name'},$syllabus{'uploaded.domain'})
  281:                      :'')
  282:                     .'\\\\');
  283:         }
  284:     }
  285: 
  286: #-------------------------------------------------------------- Print Headtitle
  287:     if ($target ne 'tex') {
  288:         $r->print('<div class="LC_Box">'.
  289:                    '<h2 class="LC_hcell">'.$courseenv{'description'}.'</h2>');
  290:         if ($allowed) {
  291:              $r->print('<div style="margin: 0; float:left;">'.
  292:                        '<h3>'.&Apache::lonnet::domain($cdom,'description').'</h3>'.
  293:                        '</div>');
  294: # Print Help Text if editing at right side of screen
  295:              $r->print('<div style="margin: 0; float:right;">'.
  296:                        &Apache::loncommon::help_open_topic('Uploaded_Templates_TextBoxes',&mt('Help with filling in text boxes')).
  297:                        '</div><br clear="all" />');
  298:         } else {
  299:             $r->print('<h3>'.&Apache::lonnet::domain($cdom,'description').'</h3>');
  300:         }
  301:     } else {
  302:         $r->print('\noindent{\large\textbf{'.$courseenv{'description'}.'}}\\\\\\\\\textbf{'.
  303:         &Apache::lonnet::domain($cdom,'description').'}\\\\');
  304:     }
  305: # -------------------------------------------------------- Get course personnel
  306:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles($cdom.'/'.$cnum);
  307:     if ($target ne 'tex') {
  308:         $r->print(&Apache::lonhtmlcommon::start_pick_box());
  309:     } else {
  310:         $r->print('\begin{tabular}{|p{0.45\textwidth}|p{0.45\textwidth}|}\hline');
  311:     }
  312:     my @personnel=sort(keys(%coursepersonnel));
  313:     my $lastpers=$personnel[$#personnel];
  314:     foreach my $element (@personnel) {
  315:         if ($target ne 'tex') {
  316:             $r->print(&Apache::lonhtmlcommon::row_title($element));
  317:         } else {
  318:             $r->print(' '.&Apache::lonxml::xmlparse($r,'tex',$element).' & ');
  319:         }
  320:         my @coursepersonlist;
  321:         foreach (split(/\,/,$coursepersonnel{$element})) {
  322:             my ($puname,$pudom)=split(/\:/,$_);
  323:             if ($target ne 'tex') {
  324:                 my $courseperson = &Apache::loncommon::plainname($puname,$pudom);
  325:                 if (($env{'user.name'} eq '') || ($env{'user.name'} eq 'public') ||
  326:                     ($env{'user.domain'} eq '') || ($env{'user.domain'} eq 'public')) {
  327:                     push(@coursepersonlist,$courseperson);
  328:                 } else {
  329:                     push(@coursepersonlist,&Apache::loncommon::aboutmewrapper($courseperson,
  330:                               $puname,$pudom));
  331:                 }
  332:             } else {
  333:                 push(@coursepersonlist,&Apache::loncommon::plainname($puname,
  334:                               $pudom).' ');
  335:             }
  336:         }
  337:         $r->print(join(", ",@coursepersonlist));
  338:         if ($target ne 'tex') {
  339:             my $lastclose=$element eq $lastpers?1:0;
  340:             $r->print(&Apache::lonhtmlcommon::row_closure($lastclose));
  341:         } else {
  342:             $r->print('\\\\ \hline');
  343:         }
  344:     }
  345:     if ($target ne 'tex') {
  346:         $r->print(&Apache::lonhtmlcommon::end_pick_box());
  347:     } else {
  348:         $r->print('\end{tabular}\\\\');
  349:     }
  350: # -------------------------------------------------------------- Announcements?
  351:     my $day = &Apache::lonannounce::showday(time,2,
  352:              &Apache::lonannounce::readcalendar($cdom.'_'.$cnum));
  353:     if ($target ne 'tex') {
  354:         if ($allowed) {
  355:             &Apache::lontemplate::print_start_template($r,&mt('RSS Feeds and Blogs'),'LC_Box');
  356:             $r->print(&Apache::lonrss::advertisefeeds($cnum,$cdom,$forceedit));
  357:             my $editurl= &Apache::lonnet::absolute_url().'/adm/'.$cdom.'/'.$cnum.'/_rss.html';
  358:             $r->print( '<a href="'.$editurl.'">'.&mt('New RSS Feed or Blog').'</a>');
  359:             &Apache::lontemplate::print_end_template($r);
  360:         } elsif (&Apache::lonrss::advertisefeeds($cnum,$cdom) ne '') {
  361:             &Apache::lontemplate::print_start_template($r,&mt('RSS Feeds and Blogs'),'LC_Box');
  362:             $r->print(&Apache::lonrss::advertisefeeds($cnum,$cdom,$forceedit));
  363:             &Apache::lontemplate::print_end_template($r);
  364:         }
  365: 
  366:     } else {
  367:         $r->print(&Apache::lonxml::xmlparse($r,'tex',$day));
  368:     }
  369: # ---------------------------------------------------------------- Get syllabus
  370:     if (($syllabus{'uploaded.lastmodified'}) || ($allowed)) {
  371:         if ($allowed) {
  372:             $r->print('<form method="post" action="">'.
  373:             '<input type="hidden" name="forceedit" value="'.$env{'form.forceedit'}.'" />');
  374:         }
  375: 		my $url_include_handler = sub {
  376: 			my ($r, $field, $message, $group, $data_ref, $fields_ref, $target, $allowed) = @_;
  377: 			my %data = %{$data_ref};
  378: 			my %fields = %{$fields_ref};
  379: 			my $urls=$message;
  380: 			$message='';
  381: 			foreach my $filelink (split(/\n/,$urls)) {
  382: 				my $output='';
  383: 			   # embed style?
  384: 				my ($curfext)=($filelink=~/\.([^\.]+)$/);
  385: 				my $embstyle=&Apache::loncommon::fileembstyle($curfext);
  386: 				if (($embstyle eq 'ssi') || ($curfext=~/\/$/)) {# make ssi call and remove everything but the body contents
  387: 					$output=&Apache::lonnet::ssi_body($filelink);
  388: 				} elsif ($embstyle eq 'img') {# embed as an image
  389: 					$output='<img src="'.$filelink.'" />';
  390: 				}
  391: 				if ($output ne '') {
  392: 					   if ($target ne 'tex') {
  393: 						   $message.='<p>'.$output.'</p>';
  394: 					   } else {
  395: 						   $message.=' '.&Apache::lonxml::xmlparse($r,'tex','<p>'.$output.'</p>').' ';
  396: 					   }
  397: 				}
  398: 			}
  399: 			if ($allowed) {
  400: 				 &Apache::lonfeedback::newline_to_br(\$urls);
  401: 				 &Apache::lontemplate::print_start_template($r,$fields{$field}.
  402: 						  &Apache::loncommon::help_open_topic('Syllabus_URLs'),'LC_Box');
  403: 				 $r->print($urls);
  404: 				 $r->print("<br /><div>");
  405: 				 &Apache::lontemplate::print_textarea_template($r, $data{$field},
  406: 					$field, Apache::lontemplate->RICH_TEXT_ALWAYS_OFF);
  407: 				 &Apache::lontemplate::print_saveall_template($r);                         
  408: 				 $r->print("</div>");
  409: 				 &Apache::lontemplate::print_end_template($r);
  410: 
  411: 			} else {
  412: 				$r->print($message);
  413: 			}
  414: 		};
  415: 		my %custom_hash = ( 'lll_includeurl' => $url_include_handler );
  416: 		&Apache::lontemplate::print_template_fields($r, \%syllabus, \%syllabusfields, 
  417: 			$target, $allowed, Apache::lontemplate->RICH_TEXT_DETECT_HTML, \%custom_hash);
  418:         if ($allowed) {
  419:             $r->print('</form>'.
  420:             &Apache::lonhtmlcommon::htmlareaselectactive());
  421:         }
  422:     } else {
  423:         if ($target ne 'tex') {$r->print('<p class="LC_info">');} else {$r->print('\par ');}
  424:         $r->print(&mt('No syllabus information provided.'));
  425:         if ($target ne 'tex') {$r->print('</p>');}
  426:     }
  427:     if ($target ne 'tex') {
  428:         $r->print('</div>');
  429:         if ($env{'form.backto'} eq 'coursecatalog') {
  430:             $r->print('<form name="backtocat" method="post" action="/adm/coursecatalog">'.
  431:                       &Apache::lonhtmlcommon::echo_form_input(['backto','courseid']).
  432:                       '</form>');
  433:         }
  434:         $r->print(&Apache::loncommon::end_page());
  435:     } else {
  436:         $r->print('\end{document}');
  437:     }
  438:     return OK;
  439: }
  440: 
  441: 1;
  442: __END__

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