File:  [LON-CAPA] / loncom / interface / lonsyllabus.pm
Revision 1.111: download - view: text, annotated - select for diffs
Thu Jun 16 10:00:57 2011 UTC (12 years, 10 months ago) by christianto
Branches: MAIN
CVS tags: version_2_10_X, version_2_10_1, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
better separation of course personnel

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

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