File:
[LON-CAPA] /
loncom /
interface /
lonsyllabus.pm
Revision
1.106.2.1:
download - view:
text,
annotated -
select for diffs
Mon Feb 15 05:31:00 2010 UTC (14 years, 4 months ago) by
faziophi
Branches:
bz6209
Diff to branchpoint 1.106:
preferred,
unified
Work-in-progress commit for bug 6209.
--> Adds capability for arbitrary fields in the data structures for
lonsyllabus.
DESCRIPTION:
Existing 2.x fields are converted to new 'data.fields.TIME_PROCESS_INT'
DB entries, and a 'data.old_new_map' entry maps old 2.x field names to
their new 3.x equivalents to preserve backwards compatibility.
Field order and display is maintained by a 'data.fields' DB entry, which is
a frozen array of 3.x field entry IDs.
When encountering a document with no 'data.fields' entry, the 2.x structure
is converted to a new 3.x data structure as above. This indicates a
document never touched by 3.x.
Saves of 3.x documents are maintained by updating both the existing
'uploaded.lastmodified' DB entry as well as a new 'properties.last_modified'
entry which is exclusive to 3.x. Mismatched timestamps indicate that a 3.x
document has been further changed in 2.x -- in that event, if an existing
field (ex. 'aaa_instructorinfo') has an equivalent, the corresponding 3.x
field is updated; else the field is appended to the 3.x document and
a conflict fail flag is enabled.
Each new 3.x field DB entry is a frozen hash with structure as follows:
$field{title} = Name of field
$field{content} = Content of field
$field{type} = Type of field, to determine parsing/saving attitude
- TYPE_TEXT_HTML -- HTML content
- TYPE_TEXT_PLAIN -- plain content
- TYPE_URL_INCLUDE -- URLs to be included the course
NOTES:
--> There is still a lot of DEBUG code and output in here.
--> To reset a syllabus from 3.x to a 2.x document, add "&forceflush=1"
to the edit syllabus page.
--> While "Delete" links appear with individual fields once saved in
3.x format, these links do not function yet.
--> A temporary function, &print_template_new_fields(), takes the place
of &Apache::lontemplate::print_template_fields().
--> Documentation will get better over time.
--> Happy belated Valentine's Day.
1: # The LearningOnline Network
2: # Syllabus
3: #
4: # $Id: lonsyllabus.pm,v 1.106.2.1 2010/02/15 05:31:00 faziophi Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: package Apache::lonsyllabus;
30:
31: use strict;
32: use Apache::lontemplate;
33: use Apache::Constants qw(:common);
34: use Apache::loncommon;
35: use Apache::lonnet;
36: use Apache::lontexconvert;
37: use Apache::lonfeedback;
38: use Apache::lonannounce;
39: use Apache::lonlocal;
40: use Apache::lonhtmlcommon;
41: use Apache::lonspeller();
42: use HTML::Entities();
43: use Digest::MD5 qw(md5_hex);
44: use Storable qw(freeze thaw);
45:
46: # These are strings representing types of fields
47: # that will used to parse/display a field correctly
48: use constant {
49: TYPE_TEXT_HTML => 'html', #<-- default
50: TYPE_TEXT_PLAIN => 'text',
51: TYPE_URL_INCLUDE => 'include-url',
52: };
53:
54: sub handler {
55: my $r = shift;
56: &Apache::loncommon::content_type($r,'text/html');
57: $r->send_http_header;
58: return OK if $r->header_only;
59:
60: my $target=$env{'form.grade_target'};
61: # --------------------------------------------------- Get course info from URL
62: my (undef,undef,$cdom,$cnum)=split(/\//,$r->uri);
63: # ------------------------------------------------------------ Get query string
64: &Apache::loncommon::get_unprocessed_cgi
65: ($ENV{'QUERY_STRING'},['delete', 'field', 'forcestudent','register','forceedit','forceflush','wrapperdisplay']);
66: # ----------------------------------------------------- Is this even a course?
67: my $homeserver=&Apache::lonnet::homeserver($cnum,$cdom);
68: if ($homeserver eq 'no_host') {
69: &Apache::loncommon::content_type($r,'text/html');
70: $r->send_http_header;
71: &Apache::loncommon::simple_error_page($r,'No syllabus available',
72: 'No syllabus available');
73: return OK;
74: }
75: # ------------------------------------- There is such a course, get environment
76: my %courseenv=&Apache::lonnet::dump('environment',$cdom,$cnum);
77:
78: # ------------------------------------------------------------ Print the screen
79: if ($target eq 'tex') {
80: $r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'}));
81: }
82:
83: # -------------------------------------------------- Let's see who handles this
84: my $externalsyllabus=$courseenv{'externalsyllabus'};
85: if ($externalsyllabus=~/\w/) {
86: if ($env{'form.wrapperdisplay'} eq 'menu') {
87: $r->print(&Apache::lonwrapper::simple_menu());
88: } else {
89: $r->print(&Apache::lonwrapper::wrapper("/public/$cdom/$cnum/syllabus?wrapperdisplay=menu",
90: $externalsyllabus));
91: }
92: return OK;
93: }
94:
95: # --------------------------------------------------------- The old syllabus fields
96: my %syllabusfields=&Apache::lonlocal::texthash(
97: 'aaa_instructorinfo' => 'Instructor Information',
98: 'bbb_description' => 'Course Description',
99: 'ccc_prereq' => 'Prerequisites',
100: 'cdc_classhours' => 'Class Hours',
101: 'ddd_officehours' => 'Office Hours',
102: 'eee_helproom' => 'Helproom Hours',
103: 'efe_projectinfo' => 'Project Information',
104: 'fff_examinfo' => 'Exam Information',
105: 'fgf_deadlines' => 'Deadlines',
106: 'ggg_grading' => 'Grading Information',
107: 'hhh_readings' => 'Readings',
108: 'iii_coursepack' => 'Coursepack',
109: 'jjj_weblinks' => 'Web Links',
110: 'kkk_textbook' => 'Textbook',
111: 'lll_includeurl' => 'URLs To Include in Syllabus');
112:
113: # --------------------------------------------------------------- Force Student
114: my $forcestudent='';
115: if ($env{'form.forcestudent'}) { $forcestudent='student'; };
116: my $forceedit='';
117: if ($env{'form.forceedit'}) { $forceedit='edit'; }
118:
119: # ----------------------------------------------------------------- Make header
120: if ($target ne 'tex') {
121: my $rss_link = &Apache::lonrss::rss_link($cnum,$cdom);
122: my $js;
123: if ($env{'form.backto'} eq 'coursecatalog') {
124: $js .= <<"ENDSCRIPT";
125:
126: <script type="text/javascript">
127: function ToCatalog(caller) {
128: numidx = getIndexByName('coursenum');
129: if (numidx > -1) {
130: if (caller != 'details') {
131: document.backtocat.elements[numidx].value = '';
132: }
133: }
134: document.backtocat.submit();
135: }
136:
137: function getIndexByName(item) {
138: for (var i=0;i<document.backtocat.elements.length;i++) {
139: if (document.backtocat.elements[i].name == item) {
140: return i;
141: }
142: }
143: return -1;
144: }
145:
146: </script>
147:
148: ENDSCRIPT
149: }
150: my $start_page =
151: &Apache::loncommon::start_page("Syllabus", $rss_link.$js,
152: {'function' => undef,
153: 'domain' => $cdom,
154: 'force_register' =>
155: $env{'form.register'},});
156:
157: $r->print($start_page);
158: if ($env{'form.backto'} eq 'coursecatalog') {
159: &Apache::lonhtmlcommon::clear_breadcrumbs();
160: &Apache::lonhtmlcommon::add_breadcrumb
161: ({href=>"javascript:ToCatalog()",
162: text=>"Course/Community Catalog"});
163: if ($env{'form.coursenum'} ne '') {
164: &Apache::lonhtmlcommon::add_breadcrumb
165: ({href=>"javascript:ToCatalog('details')",
166: text=>"Course details"});
167: }
168: &Apache::lonhtmlcommon::add_breadcrumb
169: ({href=>$r->uri,
170: text=>"Course syllabus"});
171: $r->print(&Apache::lonhtmlcommon::breadcrumbs());
172: }
173: }
174: # ---------------------------------------------------------- Load syllabus info
175: my %syllabus=&Apache::lonnet::dump('syllabus',$cdom,$cnum); # load db
176: my $allowed=0; # can we edit this page?
177: my $privileged=0;
178: my %data;
179: if ($env{'form.forceflush'}) {
180: delete $syllabus{'data.fields'};
181: &Apache::lonnet::del('syllabus', ['data.fields'], $cdom, $cnum);
182: delete $syllabus{'properties.v2_conflict'};
183: &Apache::lonnet::del('syllabus', ['properties.v2_conflict'], $cdom, $cnum);
184: delete $syllabus{'properties.v2_conflict_fail'};
185: &Apache::lonnet::del('syllabus', ['properties.v2_conflict_fail'], $cdom, $cnum);
186: delete $syllabus{'properties.last_modified'};
187: &Apache::lonnet::del('syllabus', ['properties.last_modified'], $cdom, $cnum);
188: delete $syllabus{'properties.v2_converted'};
189: &Apache::lonnet::del('syllabus', ['properties.v2_converted'], $cdom, $cnum);
190: delete $syllabus{'data.old_new_map'};
191: &Apache::lonnet::del('syllabus', ['data.old_new_map'], $cdom, $cnum);
192: %syllabus=&Apache::lonnet::dump('syllabus',$cdom,$cnum); # load db
193: $r->print("Flushed syllabus DB file.<br />");
194: $r->print("Syllabus conflict: ".$syllabus{'properties.v2_conflict'}."<br />");
195: }
196: $r->print("Existing fields: ".$syllabus{'data.fields'}."<br />");
197: $r->print("Old-new map: ".$syllabus{'data.old_new_map'}."<br />");
198: if (!exists($syllabus{'data.fields'})) {
199: # convert existing 2.x data to new DB fields
200: # which become new primary data source for document
201: %data = %{convert_from_v2($r, \%syllabus, \%syllabusfields, 0)};
202: $r->print("New fields order: ".$data{'data.fields'}."<br />");
203: &Apache::lonnet::put('syllabus',\%data,$cdom,$cnum);
204: } elsif ( !exists($syllabus{'properties.v2_converted'}) &&
205: exists($syllabus{'uploaded.lastmodified'}) &&
206: exists($syllabus{'properties.last_modified'}) &&
207: ($syllabus{'uploaded.lastmodified'} !=
208: $syllabus{'properties.last_modified'})) {
209: # if the document has been saved in 3.x and later edited in
210: # 2.x, reconvert the existing document, with extra warning
211: %data = %{convert_from_v2($r, \%syllabus, \%syllabusfields, 1)};
212: delete $data{'properties.v2_converted'};
213: &Apache::lonnet::del('syllabus', ['properties.v2_converted'], $cdom, $cnum);
214: $data{'properties.v2_conflict'} = 1;
215: &Apache::lonnet::put('syllabus',\%data,$cdom,$cnum);
216: } else {
217: %data = %syllabus;
218:
219: }
220:
221: # ----------------------------------------------------- Only if not public call
222: if ($env{'user.environment'}) { # does this user have privileges to post, etc?
223: if ($env{'request.course.id'}
224: && $cdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}
225: && $cnum eq $env{'course.'.$env{'request.course.id'}.'.num'}) {
226: $allowed=&Apache::lonnet::allowed('mdc',$env{'request.course.id'});
227: $privileged=$allowed;
228: if (($data{'uploaded.lastmodified'}) && (!$forceedit)) {
229: $forcestudent='student';
230: }
231: if ($forcestudent or $target eq 'tex') { $allowed=0; }
232: }
233: #store what the user typed in
234: my @fields = @{thaw($data{'data.fields'})};
235: if (($allowed) && ($env{'form.delete'})) {
236: my $field = $env{'form.delete'};
237: chomp($field);
238: #allow only numbers, underscores
239: $field=~s/[^0-9_]//g;
240: #check if the field exists
241: #do not delete if file in v2 conversion mode
242: if (exists($data{'data.field.'.$field}) &&
243: !exists($data{'properties.v2_converted'})) {
244: $r->print("Field can be deleted.<br />");
245: }
246: }
247: if (($allowed) && ($env{'form.storesyl'})) {
248: foreach my $syl_field (@fields) {
249: my $field=$env{'form.'.$syl_field};
250: my $type;
251: my %field_hash;
252: # only update a field if it already exists!
253: if (exists($data{'data.field.'.$syl_field})) {
254: $r->print("Creating/updated field ".$syl_field."<br />");
255: %field_hash = exists($data{'data.field.'.$syl_field}) ?
256: %{thaw($data{'data.field.'.$syl_field})} :
257: ();
258: $type = exists($field_hash{type}) ? $field_hash{type} : TYPE_TEXT_HTML;
259: chomp($field);
260: $field=~s/\s+$//s;
261: $field=~s/^\s+//s;
262: $field=~s/\<br\s*\/*\>$//s;
263: $field=&Apache::lonfeedback::clear_out_html($field,1);
264: $field_hash{content}=$field;
265: if ($type eq TYPE_URL_INCLUDE) { # clean up included URLs
266: my $field='';
267: foreach my $value (split(/\n/,$field_hash{content})) {
268: my $url=$value;
269: # get rid of leading and trailing spaces
270: $url=~s/^\s+//;
271: $url=~s/\s+$//;
272: if ($url=~m|^https?\://([^/]+)/(.+)$|) {
273: my $host = $1;
274: my $remainder=$2;
275: # remove the hostname from internal URLs
276: my $hostname = &Apache::lonnet::hostname($host);
277: my %all_hostnames = &Apache::lonnet::all_hostnames();
278: foreach my $possible_host (keys(%all_hostnames)) {
279: if ($possible_host =~ /\Q$hostname\E/i) {
280: $url=$remainder;
281: }
282: }
283: }
284: # norm internal URLs
285: unless ($url=~/^https?\:/) {
286: $url=&Apache::lonnet::clutter($url);
287: }
288: # re-assemble field
289: if ($url) {
290: $field.=$url."\n";
291: }
292: }
293: $field_hash{content}=$field;
294: $field_hash{type}=TYPE_URL_INCLUDE;
295: }
296: $data{'data.field.'.$syl_field} = freeze(\%field_hash);
297: }
298: }
299: $data{'uploaded.domain'}=$env{'user.domain'};
300: $data{'uploaded.name'}=$env{'user.name'};
301: my $time = $^T;
302: $data{'uploaded.lastmodified'}=$time;
303: $data{'properties.last_modified'}=$time;
304: delete $data{'properties.v2_converted'};
305: delete $data{'properties.v2_conflict'};
306: delete $data{'properties.v2_conflict_fail'};
307: &Apache::lonnet::del('syllabus', ['properties.v2_converted',
308: 'properties.v2_conflict', 'properties.v2_conflict_fail'], $cdom, $cnum);
309:
310: #2.x compatibility: write to old fields with new mapped fields
311: my %old_new_map = %{thaw($data{'data.old_new_map'})};
312: foreach my $old_field (keys(%old_new_map)) {
313: $r->print("Looking for: ".$old_field." at ".$old_new_map{$old_field}."<br />");
314: if (exists($data{'data.field.'.$old_new_map{$old_field}})) {
315: $r->print("updating old field ".$old_field."<br />");
316: my %new_field = %{thaw($data{'data.field.'.$old_new_map{$old_field}})};
317: $data{$old_field} = $new_field{content};
318: }
319: }
320:
321: &Apache::lonnet::put('syllabus',\%data,$cdom,$cnum);
322: }
323: }
324:
325: #--------Functions
326: if( ($allowed || $privileged) && $target ne 'tex') {
327: my $functions=&Apache::lonhtmlcommon::start_funclist();
328: if ($allowed) {
329: #if you have the register flag, keep it
330: if($env{'form.register'} == 1) {
331: $functions.=&Apache::lonhtmlcommon::add_item_funclist(
332: '<a href="'.$r->uri.'?forcestudent=1&register=1">'
333: .&mt('Show Public View').'</a>'
334: .&Apache::loncommon::help_open_topic(
335: 'Uploaded_Templates_PublicView'));
336: } else {
337: $functions.=&Apache::lonhtmlcommon::add_item_funclist(
338: '<a href="'.$r->uri.'?forcestudent=1">'
339: .&mt('Show Public View').'</a>'
340: .&Apache::loncommon::help_open_topic(
341: 'Uploaded_Templates_PublicView'));
342: }
343: } elsif ($privileged) {
344: if($env{'form.register'} == 1) {
345: $functions.=&Apache::lonhtmlcommon::add_item_funclist(
346: '<a href="'.$r->uri.'?forceedit=1&register=1">'
347: .&mt('Edit').'</a>');
348: } else {
349: $functions.=&Apache::lonhtmlcommon::add_item_funclist(
350: '<a href="'.$r->uri.'?forceedit=1">'
351: .&mt('Edit').'</a>');
352: }
353: }
354:
355: $functions.=&Apache::lonhtmlcommon::end_funclist();
356: $r->print(&Apache::loncommon::head_subbox($functions));
357: }
358:
359: #---------------------Print External URL Syllabus Info and Help Text
360: if( ($allowed) && ($target ne 'tex') ) {
361: my $protocol = $Apache::lonnet::protocol{$homeserver};
362: $protocol = 'http' if ($protocol ne 'https');
363: $r->print('<p class="LC_info">'
364: .&mt('This syllabus can be publicly viewed at [_1]'
365: ,'<tt>'.$protocol.'://'.&Apache::lonnet::hostname($homeserver).$r->uri.'</tt>')
366: .' '.&Apache::loncommon::help_open_topic('Syllabus_ExtLink')
367: .'</p>'
368: .'<p class="LC_info">'
369: .&mt('Instead of using this template you can specify an external URL as Syllabus in the [_1]Course Configuration[_2].'
370: ,'<a href="/adm/courseprefs?actions=courseinfo&phase=display">','</a>')
371: .'</p>'
372: );
373: #-Print Help Text
374: $r->print(&Apache::loncommon::help_open_topic(
375: 'Uploaded_Templates_TextBoxes',
376: &mt('Help with filling in text boxes')));
377: }
378:
379: #----------Print last update
380: my $lastmod=$syllabus{'uploaded.lastmodified'};
381: $lastmod=($lastmod?&Apache::lonlocal::locallocaltime($lastmod):&mt('never'));
382: my $who = &Apache::loncommon::aboutmewrapper(
383: &Apache::loncommon::plainname($syllabus{'uploaded.name'},
384: $syllabus{'uploaded.domain'}),$syllabus{'uploaded.name'},
385: $syllabus{'uploaded.domain'});
386: if ($target ne 'tex') {
387: $r->print('<div class="LC_info">'.&mt('Last updated').': '.
388: $lastmod . ' '.
389: ($who ? &mt('by').' '.$who
390: : '' ) .
391: '</div>' );
392:
393: } else {
394: $r->print('\\\\ '.&mt('Last updated').': '.$lastmod.' '.
395: ($who? &mt('by').'\\\\ '.
396: &Apache::loncommon::plainname($syllabus{'uploaded.name'},$syllabus{'uploaded.domain'})
397: :'')
398: .'\\\\');
399: }
400: if ($allowed && $data{'properties.v2_converted'} == 1) {
401: $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/>");
402: }
403: if ($allowed && $data{'properties.v2_conflict'} == 1) {
404: $r->print("<em>This document was saved with LON-CAPA 3.x, then further edited in LON-CAPA 2.x.</em><br/>");
405: if ($data{'properties.v2_conflict_fail'} == 1) {
406: $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 />");
407: } else {
408: $r->print("<em>These changes were automatically transferred to LON-CAPA 3.x</em>");
409: }
410: }
411:
412: #----------------------------Print Headtitle
413: if ($target ne 'tex') {
414: $r->print('<h1>'.$courseenv{'description'}.'</h1>');
415: $r->print('<h3>'. &Apache::lonnet::domain($cdom,'description').'</h3>');
416: } else {
417: $r->print('\noindent{\large\textbf{'.$courseenv{'description'}.'}}\\\\\\\\\textbf{'.
418: &Apache::lonnet::domain($cdom,'description').'}\\\\');
419: }
420:
421: # -------------------------------------------------------- Get course personnel
422: my %coursepersonnel=&Apache::lonnet::get_course_adv_roles($cdom.'/'.$cnum);
423: if ($target ne 'tex') {
424: $r->print(&Apache::lonhtmlcommon::start_pick_box());
425: } else {
426: $r->print('\begin{tabular}{|p{0.45\textwidth}|p{0.45\textwidth}|}\hline');
427: }
428: my @personnel=sort(keys(%coursepersonnel));
429: my $lastpers=$personnel[$#personnel];
430: foreach my $element (@personnel) {
431: if ($target ne 'tex') {
432: $r->print(&Apache::lonhtmlcommon::row_title($element));
433: } else {
434: $r->print(' '.&Apache::lonxml::xmlparse($r,'tex',$element).' & ');
435: }
436: foreach (split(/\,/,$coursepersonnel{$element})) {
437: my ($puname,$pudom)=split(/\:/,$_);
438: if ($target ne 'tex') {
439: my $courseperson = &Apache::loncommon::plainname($puname,$pudom);
440: if (($env{'user.name'} eq '') || ($env{'user.name'} eq 'public') ||
441: ($env{'user.domain'} eq '') || ($env{'user.domain'} eq 'public')) {
442: $r->print(' '.$courseperson);
443: } else {
444: $r->print(' '.&Apache::loncommon::aboutmewrapper($courseperson,
445: $puname,$pudom));
446: }
447: } else {
448: $r->print(' '.&Apache::loncommon::plainname($puname,
449: $pudom).' ');
450: }
451: }
452: if ($target ne 'tex') {
453: my $lastclose=$element eq $lastpers?1:0;
454: $r->print(&Apache::lonhtmlcommon::row_closure($lastclose));
455: } else {
456: $r->print('\\\\ \hline');
457: }
458: }
459: if ($target ne 'tex') {
460: $r->print(&Apache::lonhtmlcommon::end_pick_box());
461: } else {
462: $r->print('\end{tabular}\\\\');
463: }
464: # -------------------------------------------------------------- Announcements?
465: my $day = &Apache::lonannounce::showday(time,2,
466: &Apache::lonannounce::readcalendar($cdom.'_'.$cnum));
467: if ($target ne 'tex') {
468: if ($allowed) {
469: &Apache::lontemplate::print_start_template($r,'RSS Feeds and Blogs','LC_Box');
470: $r->print(&Apache::lonrss::advertisefeeds($cnum,$cdom,$forceedit));
471: my $editurl= &Apache::lonnet::absolute_url().'/adm/'.$cdom.'/'.$cnum.'/_rss.html';
472: $r->print( '<a href="'.$editurl.'">'.&mt('New RSS Feed or Blog').'</a>');
473: &Apache::lontemplate::print_end_template($r);
474: } elsif (&Apache::lonrss::advertisefeeds($cnum,$cdom) ne '') {
475: &Apache::lontemplate::print_start_template($r,'RSS Feeds and Blogs','LC_Box');
476: $r->print(&Apache::lonrss::advertisefeeds($cnum,$cdom,$forceedit));
477: &Apache::lontemplate::print_end_template($r);
478: }
479:
480: } else {
481: $r->print(&Apache::lonxml::xmlparse($r,'tex',$day));
482: }
483:
484: # ---------------------------------------------------------------- Get syllabus
485: if (($syllabus{'uploaded.lastmodified'}) || ($allowed)) {
486: if ($allowed) {
487: $r->print('<form method="post" action="">'.
488: '<input type="hidden" name="forceedit" value="edit" />');
489: }
490: my @htmlids=();
491: my $url_include_handler = sub {
492: my ($r, $field, $json_ref, $group, $target, $allowed) = @_;
493: my $message = $json_ref->{items}{$field}{content};
494: my $title = $json_ref->{items}{$field}{title};
495: my $urls = $message;
496: foreach my $filelink (split(/\n/,$urls)) {
497: my $output='';
498: # embed style?
499: my ($curfext)=($filelink=~/\.([^\.]+)$/);
500: my $embstyle=&Apache::loncommon::fileembstyle($curfext);
501: if (($embstyle eq 'ssi') || ($curfext=~/\/$/)) {# make ssi call and remove everything but the body contents
502: $output=&Apache::lonnet::ssi_body($filelink);
503: } elsif ($embstyle eq 'img') {# embed as an image
504: $output='<img src="'.$filelink.'" />';
505: }
506: if ($output ne '') {
507: $message='';
508: if ($target ne 'tex') {
509: $message.='<p>'.$output.'</p>';
510: } else {
511: $message.=' '.&Apache::lonxml::xmlparse($r,'tex','<p>'.$output.'</p>').' ';
512: }
513: }
514: }
515: if ($allowed) {
516: &Apache::lonfeedback::newline_to_br(\$urls);
517: &Apache::lontemplate::print_start_template($r,$title.
518: &Apache::loncommon::help_open_topic('Syllabus_URLs'),'LC_Box');
519: $r->print($urls);
520: $r->print("<br /><div>");
521: &Apache::lontemplate::print_textarea_template($r, $message,
522: $field, Apache::lontemplate->RICH_TEXT_ALWAYS_OFF);
523: &Apache::lontemplate::print_saveall_template($r);
524: $r->print("</div>");
525: &Apache::lontemplate::print_end_template($r);
526:
527: } else {
528: $r->print($message);
529: }
530: };
531: my %custom_hash = ( TYPE_URL_INCLUDE() => $url_include_handler );
532: @htmlids = &print_template_new_fields($r, \%data,
533: $target, $allowed, Apache::lontemplate->RICH_TEXT_DETECT_HTML, \%custom_hash);
534: if ($allowed) {
535: $r->print('</form>'.
536: &Apache::lonhtmlcommon::htmlareaselectactive(@htmlids));
537: }
538: } else {
539: if ($target ne 'tex') {$r->print('<p>');} else {$r->print('\par ');}
540: $r->print(&mt('No syllabus information provided.'));
541: if ($target ne 'tex') {$r->print('</p>');}
542: }
543: if ($target ne 'tex') {
544: if ($env{'form.backto'} eq 'coursecatalog') {
545: $r->print('<form name="backtocat" method="post" action="/adm/coursecatalog">'.
546: &Apache::lonhtmlcommon::echo_form_input(['backto','courseid']).
547: '</form>');
548: }
549: $r->print(&Apache::loncommon::end_page());
550: } else {
551: $r->print('\end{document}');
552: }
553: return OK;
554: }
555:
556: sub print_template_new_fields {
557: my ($r, $data_ref, $target, $allowed, $default_rich_text, $custom_handlers_ref, $group) = @_;
558: my @html_ids = ();
559: my %data = %{$data_ref};
560: my @fields = @{thaw($data{'data.fields'})};
561: my %custom_handlers = %{$custom_handlers_ref};
562:
563: foreach my $key (@fields) {
564: my %field = %{thaw($data{'data.field.'.$key})};
565: my $title = $field{title};
566: my $raw_message = $field{content};
567: my $type = $field{type};
568: my $message = $raw_message if (($raw_message=~/\w/) || ($allowed));
569: if ((%custom_handlers) && exists($custom_handlers{$type})) {
570: #$custom_handlers{$type}->($r, $field, $json_ref, $group, $target, $allowed);
571: } else {
572: if (($raw_message=~/\w/) || ($allowed)) {
573: if (!&Apache::lonfeedback::contains_block_html($message)) {
574: &Apache::lonfeedback::newline_to_br(\$message);
575: } else {
576: $message = &Apache::lonfeedback::tidy_html($message);
577: }
578: $message=&Apache::lonhtmlcommon::raw_href_to_link($message);
579: if ($allowed) {
580: $message=&Apache::lonspeller::markeduptext($message);
581: }
582: $message=&Apache::lontexconvert::msgtexconverted($message);
583: if ($target ne 'tex') {
584: #output of syllabusfields will be generated here.
585: &Apache::lontemplate::print_start_template($r,$title,'LC_Box');
586: $r->print($message);
587: if ($allowed) {
588: $r->print("<br /><div>");
589: &Apache::lontemplate::print_textarea_template($r, $raw_message,
590: $key, $default_rich_text);
591: &Apache::lontemplate::print_saveall_template($r);
592: if (!exists($data{'properties.v2_converted'})) {
593: $r->print("<a href='?delete=$key&forceedit=1'>Delete</a>");
594: }
595: $r->print("</div>");
596: }
597: &Apache::lontemplate::print_end_template($r);
598: } else {
599: my $safeinit;
600: $r->print(&Apache::lonxml::xmlparse($r,'tex','<h3>'.$title.'</h3>'));
601: $r->print(&Apache::lonxml::xmlparse($r,'tex',$message));
602: }
603: push(@html_ids,"hello");
604: }
605: }
606: }
607:
608: return @html_ids;
609: }
610:
611: sub convert_from_v2 {
612: my ($r, $data_ref, $fields_ref, $conflict) = @_;
613: my %data = %{$data_ref};
614: my %fields = %{$fields_ref};
615: my @fields_order = (!$conflict) ? () : @{thaw($data{'data.fields'})};
616: my %old_new_map = (!$conflict) ? () : %{thaw($data{'data.old_new_map'})};
617: my $repeat_int = 0; #ensure fields with created timestamp are unique
618: foreach my $element (sort(keys(%fields))) {
619: my %new_element = ();
620: my $title = $fields{$element};
621: my $title_hash = time."_".$$;
622: if (exists($data{'data.field.'.$title_hash})) {
623: $title_hash .= "_".$repeat_int++;
624: }
625: my $content = $data{$element};
626: $new_element{title} = $title;
627: $new_element{content} = $content;
628: if ($element eq 'lll_includeurl') {
629: $new_element{type} = TYPE_URL_INCLUDE;
630: } else {
631: $new_element{type} = TYPE_TEXT_HTML;
632: }
633: if (!$conflict) {
634: $r->print("Creating new field with ID: ".$title_hash."<br />");
635: $data{'data.field.'.$title_hash} = freeze(\%new_element);
636: $old_new_map{$element} = $title_hash;
637: push(@fields_order, $title_hash);
638: } else {
639: if (exists($old_new_map{$element})) {
640: $r->print("Transferring old field ".$element." to new ID: ".$old_new_map{$element}."<br />");
641: if (exists($data{'data.field.'.$old_new_map{$element}})) {
642: my %new_field = %{thaw($data{'data.field.'.$old_new_map{$element}})};
643: $new_field{content} = $content;
644: $data{'data.field.'.$old_new_map{$element}} = freeze(\%new_field);
645: }
646: } else {
647: $data{'data.field.'.$title_hash} = freeze(\%new_element);
648: $old_new_map{$element} = $title_hash;
649: $data{'properties.v2_conflict_fail'} = 1;
650: push(@fields_order, $title_hash);
651: }
652: }
653: }
654: $data{'data.fields'} = freeze(\@fields_order);
655: $data{'data.old_new_map'} = freeze(\%old_new_map);
656: $data{'properties.last_modified'} = time;
657: $data{'properties.v2_converted'} = 1;
658: $data{'properties.type'} = 'syllabus';
659:
660: return \%data;
661: }
662:
663: 1;
664: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>