Simple status box for the sidebar.
[elgg_plugins.git] / presentation / lib.php
blob8c431f9d1f5b69fa9e3b542e3a9a3b57d34b7e3a
1 <?php
2 /**
3 * Elgg Presentation tool
4 *
5 * @author Sven Edge
6 * @package mod_presentation
7 */
9 /*
10 TODO?:
11 export presentation to zip?
12 The ability to search for blog posts and files via tag
13 The ability to choose files uploaded to a community
14 Auto table of contents
21 /**
22 * Standard page setup function
24 * @param string $varname
25 * @param int $options
26 * @return mixed
28 function presentation_pagesetup() {
30 global $PAGE, $CFG, $USER, $page_owner, $function, $metatags, $template;
32 require_once($CFG->dirroot.'lib/filelib.php'); // to ensure file_get_contents()
33 $css = file_get_contents($CFG->dirroot . "mod/presentation/presentation.css");
34 $css = str_replace("{{url}}", $CFG->wwwroot, $css);
35 $metatags .= '<style type="text/css">' . $css . '</style>';
36 $metatags .= '<script type="text/javascript" src="' . $CFG->wwwroot . 'mod/presentation/presentation.js"></script>' . "\n";
38 $page_username = user_info("username",$page_owner);
40 if (isloggedin() && user_info("user_type", $USER->ident) != "external") {
41 if (defined("context") && context == "presentation" && $page_owner == $USER->ident) {
42 $PAGE->menu[] = array(
43 'name' => 'presentation',
44 'html' => '<li><a href="' . $CFG->wwwroot . $USER->username . '/presentations/" class="selected">' . __gettext("Your Presentations") . '</a></li>'
46 } else {
47 $PAGE->menu[] = array(
48 'name' => 'presentation',
49 'html' => '<li><a href="' . $CFG->wwwroot . $USER->username . '/presentations/">' . __gettext("Your Presentations") . '</a></li>'
54 if (defined("context") && context == "presentation") {
56 $PAGE->menu_sub[] = array(
57 'name' => 'presentation:index',
58 'html' => a_href(
59 $CFG->wwwroot . $page_username . '/presentations/',
60 __gettext("Index")
64 if (isloggedin() && run("permissions:check", "profile") && $page_owner == $USER->ident) {
65 $PAGE->menu_sub[] = array(
66 'name' => 'presentation:create',
67 'html' => a_href(
68 $CFG->wwwroot . 'mod/presentation/new.php?owner=' . $USER->ident,
69 __gettext("New")
77 $function['display:sidebar'][] = $CFG->dirroot . "mod/presentation/user_info_menu.php";
79 $postedby = '';
80 $keywords = __gettext("Keywords: ");
81 $template['presentation_weblogpost'] = <<< END
82 <div class="weblog-post"><!-- Holds all aspects of a blog post -->
83 <div class="user"><!-- Open class user -->
84 <img alt="" src="{{url}}_icon/user/{{usericon}}"/><br />{{fullname}}
85 </div><!-- Close class user -->
86 <div class="weblog-title"><h3>{{title}}</h3></div>
87 <div class="post"><!-- Open class post -->
88 {{body}}
89 </div><!-- Close class post -->
90 <div class="info"><!-- Open class info -->
91 <p>
92 $postedby {{fullname}}
93 {{links}}
94 {{commentslink}}
95 </p>
96 </div><!-- Close class info -->
97 <p>
98 $keywords {{tags}}
99 </p>
100 {{comments}}
101 </div><!-- Close weblog_post -->
102 <div class="clearing"></div>
103 END;
105 $comments = __gettext("Comments (+/-)");
106 $template['presentation_weblogcomments'] = <<< END
107 <div class="presentation-importedcomments"><!-- start comments div -->
108 <h4><a href="javascript:toggleVisibility('{{blockid}}')">{{numcomments}} $comments</a></h4>
109 <ol id="{{blockid}}">
110 {{comments}}
111 </ol>
112 <script type="text/javascript">
113 <!--
114 toggleVisibility('{{blockid}}');
115 // -->
116 </script>
117 </div><!-- end comments div -->
118 END;
120 $template['presentation_weblogcomment'] = <<< END
121 <li>
122 {{body}}
123 <div class="comment-owner">
125 {{usericon}}{{postedname}} on {{posted}}
126 </p>
127 </div>
128 </li>
129 END;
131 $template['presentation_comment'] = <<< END
132 <li>
133 {{body}}
134 <div class="comment-owner">
136 {{usericon}}{{postedname}} on {{posted}} | {{links}}
137 </p>
138 </div>
139 </li>
140 END;
142 $comments = __gettext("Comments");
143 //template for overall presentation comments
144 $template['presentation_comments'] = <<< END
145 <div class="presentation-comments"><!-- start comments div -->
146 <ol id="{{blockid}}">
147 {{comments}}
148 </ol>
149 </div><!-- end comments div -->
150 END;
151 //template for per-section comments, with integrated comment adding form
152 $template['presentation_sectioncomments'] = <<< END
153 <div class="presentation-sectioncomments" id="{{blockid}}">
154 <ol>
155 {{comments}}
156 </ol>
157 {{addform}}
158 </div>
159 <script type="text/javascript">
160 <!--
161 toggleVisibility('{{blockid}}');
162 // -->
163 </script>
164 <!-- -->
165 END;
171 } // end function
177 * Initialisation function
180 function presentation_init() {
182 global $CFG, $function, $db, $METATABLES;
184 $CFG->presentation->types = array('text', 'blogpost', 'file');
186 if (!array_intersect(array(
187 $CFG->prefix . "presentations",
188 $CFG->prefix . "presentation_sections",
189 $CFG->prefix . "presentation_sections_data",
190 $CFG->prefix . "presentation_comments"
191 ), $METATABLES)) {
192 if (file_exists($CFG->dirroot . "mod/presentation/$CFG->dbtype.sql")) {
193 modify_database($CFG->dirroot . "mod/presentation/$CFG->dbtype.sql");
194 } else {
195 error("Error: Your database ($CFG->dbtype) is not yet fully supported by the Elgg Presentation tool. See the mod/presentation directory.");
197 print_continue("index.php");
198 exit;
201 $function['search:all:tagtypes'][] = $CFG->dirroot . "mod/presentation/function_search_all_tagtypes.php";
202 $function['search:display_results'][] = $CFG->dirroot . "mod/presentation/function_search.php";
203 $function['groups:delete'][] = $CFG->dirroot . "mod/presentation/groups_delete.php";
205 $action = trim(optional_param('action'));
206 if ($action) {
207 presentation_actions($action);
210 listen_for_event("user", "delete", "presentation_user_delete");
212 } // end function
218 * Get presentations of a particular user visible to the logged on user
220 * @param int $user_id userid to get presentations for
221 * @return array
223 function presentation_get_presentations_visible($owner_id) {
225 global $CFG, $USER;
227 // Get access list for this user
228 $where = run("users:access_level_sql_where",$USER->ident);
230 // Get a list of presentations
231 $sql = "SELECT * FROM ".$CFG->prefix."presentations WHERE ($where) AND owner = $owner_id ORDER BY name";
232 $presentations = get_records_sql($sql);
234 // Return them
235 return $presentations;
237 } // end function
243 * Get presentations sidebar content
245 * @return string html to add on to sidebar
247 function presentation_sidebar() {
249 global $page_owner, $CFG;
250 $run_result = '';
252 if ($page_owner > 0) { // TODO: a better way to do this - Sven
253 $title = __gettext("Presentations");
254 $body = "<ul><li>";
255 $ownername = user_info("username",$page_owner);
256 $body .= '<a href="' . $CFG->wwwroot . $ownername . '/presentations/">' . __gettext("Presentations List") . '</a></li></ul>';
258 $run_result .= "<li id=\"sidebar_presentations\">";
259 $run_result .= templates_draw(array(
260 'context' => 'sidebarholder',
261 'title' => $title,
262 'body' => $body,
265 $run_result .= "</li>";
267 return $run_result;
268 } // end function
274 * Do updatey things for presentations
276 * @param string $action the action to trigger
279 function presentation_actions($action) {
281 global $CFG, $USER, $page_owner, $messages;
283 // logged on-only actions
284 if (logged_on) {
285 switch ($action) {
287 case "presentation:create":
288 // this always creates as the logged in user, not the page owner
289 $new_presentation = new StdClass;
290 $new_presentation->name = trim(optional_param('new_presentation_title'));
291 $new_presentation->access = trim(optional_param('new_presentation_access'));
292 $new_presentation->owner = $USER->ident;
293 $new_presentation_keywords = trim(optional_param('new_presentation_keywords'));
295 // If we've been asked to add an item, do that
296 if (!empty($new_presentation->name)) {
298 $new_presentation = plugin_hook("presentation","create",$new_presentation);
299 if (!empty($new_presentation)) {
300 $insert_id = insert_record('presentations',$new_presentation);
301 $new_presentation->ident = $insert_id;
303 insert_tags_from_string ($new_presentation_keywords, 'presentation', $insert_id, $new_presentation->access, $new_presentation->owner);
305 $new_presentation = plugin_hook("presentation","publish",$new_presentation);
306 $messages[] = __gettext("Your presentation has been created.");
307 define('redirect_url',$CFG->wwwroot . user_info("username",$USER->ident) . "/presentations/");
309 } else {
310 $messages[] = __gettext("Your presentation has to have a name.");
312 break;
316 case "presentation:editsave":
318 $edit_presentation = new StdClass;
319 $edit_presentation->ident = optional_param('edit_presentation_ident',0,PARAM_INT);
320 $edit_presentation->name = trim(optional_param('edit_presentation_title'));
321 $edit_presentation->access = trim(optional_param('edit_presentation_access'));
322 $edit_presentation_keywords = trim(optional_param('edit_presentation_keywords'));
324 $presrec = get_record("presentations", "ident", $edit_presentation->ident);
326 // presentation exists? right owner?
327 if ($presrec && presentation_permissions_check($presrec->ident, $USER->ident)) {
329 if ($edit_presentation->name) {
331 $edit_presentation = plugin_hook("presentation","update",$edit_presentation);
332 if (!empty($edit_presentation)) {
333 $insert_id = update_record('presentations',$edit_presentation);
335 delete_records('tags','tagtype','presentation','ref',$edit_presentation->ident);
336 insert_tags_from_string ($edit_presentation_keywords, 'presentation', $edit_presentation->ident, $edit_presentation->access, $USER->ident);
338 $edit_presentation = plugin_hook("presentation","republish",$edit_presentation);
339 $messages[] = __gettext("Your changes have been saved.");
340 $page_username = user_info("username",$USER->ident);
341 define('redirect_url',$CFG->wwwroot . $page_username . "/presentations/" . $edit_presentation->ident . '/edit');
342 //define('redirect_url',$CFG->wwwroot . "mod/presentation/edit.php?id=" . $edit_presentation->ident);
344 } else {
345 $messages[] = __gettext("Your changes could not be saved.");
348 break;
352 case "presentation:delete":
354 $deleteident = optional_param('delete_presentation_ident',0,PARAM_INT);
356 $presrec = get_record("presentations", "ident", $deleteident);
358 // presentation exists? right owner?
359 if ($presrec && presentation_permissions_check($presrec->ident, $USER->ident)) {
361 //$edit_presentation = plugin_hook("presentation","delete",$edit_presentation);
363 presentation_delete($deleteident);
365 //$edit_presentation = plugin_hook("presentation","republish",$edit_presentation);
366 $messages[] = __gettext("Your presentation was deleted.");
368 $page_username = user_info("username",$USER->ident);
369 define('redirect_url',$CFG->wwwroot . $page_username . "/presentations/");
370 } else {
371 $messages[] = __gettext("Your presentation could not be deleted.");
373 break;
377 case "presentation:movesec":
378 $presentation = optional_param('id',0,PARAM_INT);
379 $section = optional_param('sec',0,PARAM_INT);
380 $direction = optional_param('dir');
382 $presrec = get_record("presentations", "ident", $presentation);
384 // presentation exists? right owner?
385 if ($presrec && presentation_permissions_check($presrec->ident, $USER->ident)) {
386 if ($secrec = get_record("presentation_sections", "ident", $section, "presentation", $presentation)) {
387 if ($direction == "up") {
388 presentation_section_moveup($secrec);
389 } elseif ($direction == "down") {
390 presentation_section_movedown($secrec);
393 $page_username = user_info("username",$USER->ident);
394 define('redirect_url',$CFG->wwwroot . $page_username . "/presentations/" . $presentation . '/edit');
395 //define('redirect_url',$CFG->wwwroot . "mod/presentation/edit.php?id=" . $presentation);
398 break;
402 case "presentation:delsec":
403 $presentation = optional_param('id',0,PARAM_INT);
404 $section = optional_param('sec',0,PARAM_INT);
406 $presrec = get_record("presentations", "ident", $presentation);
408 // presentation exists? right owner?
409 if ($presrec && presentation_permissions_check($presrec->ident, $USER->ident)) {
410 if ($secrec = get_record("presentation_sections", "ident", $section, "presentation", $presentation)) {
411 presentation_section_delete($presentation, $section);
412 $messages[] = __gettext("Section deleted.");
414 $page_username = user_info("username",$USER->ident);
415 define('redirect_url',$CFG->wwwroot . $page_username . "/presentations/" . $presentation . '/edit');
416 //define('redirect_url',$CFG->wwwroot . "mod/presentation/edit.php?id=" . $presentation);
419 break;
423 case "presentation:savesec":
424 $presentation = optional_param('id',0,PARAM_INT);
425 $section = optional_param('sec',0,PARAM_INT);
426 $sectiontype = optional_param('sectype');
427 $dataarray = optional_param('pres_sec_data');
429 $presrec = get_record("presentations", "ident", $presentation);
431 $time = time();
433 // presentation exists? right owner?
434 if ($presrec && presentation_permissions_check($presrec->ident, $USER->ident)) {
436 if ($section == -1) {
437 //new section
438 $newsec = new stdClass;
439 $newsec->presentation = $presrec->ident;
440 $newsec->section_type = $sectiontype;
441 $newsec->updated = $newsec->created = $time;
442 $newsec->display_order = 10001; //yeah, i know. i can't be arsed. - sven
444 if ($insert = insert_record('presentation_sections', $newsec)) {
445 $func = "presentation_section_save_" . $sectiontype;
446 if (is_callable($func)) {
447 $func($presrec->ident, $insert, $dataarray);
449 presentation_sections_reorder($presrec->ident);
450 $messages[] = __gettext("Added section.");
452 } else {
453 $messages[] = __gettext("Error: Could not create section.");
456 } else {
457 //existing section
459 if ($secrec = get_record("presentation_sections", "ident", $section, "presentation", $presrec->ident)) {
460 $func = "presentation_section_save_" . $sectiontype;
461 if (is_callable($func)) {
462 $func($presrec->ident, $section, $dataarray);
464 set_field('presentation_sections', 'updated', $time, 'ident', $section);
465 } else {
466 $messages[] = __gettext("Error: Could not find section to update.");
472 $page_username = user_info("username",$USER->ident);
473 define('redirect_url',$CFG->wwwroot . $page_username . "/presentations/" . $presentation . '/edit');
474 //define('redirect_url',$CFG->wwwroot . "mod/presentation/edit.php?id=" . $presentation);
477 break;
479 case "presentation:delcomment":
481 $presentation = optional_param('id',0,PARAM_INT);
482 $section = optional_param('sec',0,PARAM_INT);
483 $deleteident = optional_param('commentid',0,PARAM_INT);
485 $presrec = get_record("presentations", "ident", $presentation);
486 $commentrec = get_record("presentation_comments", "ident", $deleteident);
488 // presentation + comment exist? presentation or comment owner?
489 if (
490 $presrec &&
491 $commentrec &&
493 presentation_permissions_check($presrec->ident, $USER->ident)
494 || $commentrec->owner == $USER->ident
498 delete_records('presentation_comments', 'ident', $deleteident, 'presentation', $presentation, 'section', $section);
500 $messages[] = __gettext("Comment deleted.");
502 $page_username = user_info("username",$USER->ident);
503 define('redirect_url',$CFG->wwwroot . $page_username . "/presentations/" . $presentation);
504 } else {
505 $messages[] = __gettext("Comment could not be deleted.");
507 break;
511 } // end switch
513 } // end if logged_on
515 // logged-out actions
516 switch ($action) {
518 case "presentation:addcomment":
520 $presentation = optional_param('id',0,PARAM_INT);
521 $section = optional_param('sec',0,PARAM_INT);
522 $presrec = get_record("presentations", "ident", $presentation);
524 // presentation exists? comment poster is logged on or public comments allowed?
525 if ($presrec && (logged_on || (!$CFG->disable_publiccomments && user_flag_get("publiccomments",$presrec->owner)) )) {
527 if ($section && !record_exists("presentation_sections", "ident", $section, "presentation", $presentation)) {
528 $section = 0;
531 $newcomment = new stdClass;
532 $newcomment->presentation = $presrec->ident;
533 $newcomment->section = $section;
534 $newcomment->created = time();
535 if (logged_on) {
536 $newcomment->owner = $USER->ident;
537 } else {
538 $newcomment->owner = -1;
540 $newcomment->postedname = trim(optional_param('postedname'));
541 $newcomment->body = trim(optional_param('commentbody'));
543 if ($newcomment->body) {
544 if ($insert = insert_record('presentation_comments', $newcomment)) {
545 $messages[] = __gettext("Comment added.");
546 } else {
547 $messages[] = __gettext("Error: Failed to add comment.");
549 } else {
550 $messages[] = __gettext("Error: Not adding blank comment.");
553 //stay on the same page
554 define('redirect_url', $_SERVER['REQUEST_URI']);
557 break;
559 } // end switch
565 if (defined('redirect_url')) {
566 $_SESSION['messages'] = $messages;
567 header("Location: " . redirect_url);
568 exit;
571 } // end function
577 * Reorders sections for a particular presentation
579 * @param int $presentationid ID of the presentation to reorder
582 function presentation_sections_reorder($presentationid) {
584 $sections = presentation_get_sections($presentationid);
585 if (!empty($sections) && is_array($sections)) {
586 $order = array();
587 $i = 1;
588 foreach($sections as $section) {
589 $order[$section->ident] = $i * 10;
590 $i++;
592 foreach($order as $ident => $display_order) {
593 // no point in updating non-changing values
594 if ($sections[$ident]->display_order != $display_order) {
595 $section = new StdClass;
596 $section->display_order = $display_order;
597 $section->ident = $ident;
598 update_record('presentation_sections',$section);
603 } // end function
606 * Move a section up
608 * @param object $section section row to move
611 function presentation_section_moveup($section) {
612 $section->display_order = $section->display_order - 11;
613 update_record('presentation_sections',$section);
614 presentation_sections_reorder($section->presentation);
615 } // end function
618 * Move a section down
620 * @param object $section section row to move
623 function presentation_section_movedown($section) {
624 $section->display_order = $section->display_order + 11;
625 update_record('presentation_sections',$section);
626 presentation_sections_reorder($section->presentation);
627 } // end function
632 * Get section rows for a presentation
634 * @param int $presentation presentation id
635 * @return mixed false, or an array of sections
637 function presentation_get_sections($presentation) {
639 return get_records('presentation_sections', 'presentation', $presentation, 'display_order');
641 } // end function
645 * Purge a section from a presentation
647 * @param int $presentation presentation id
648 * @param int $section section id
650 function presentation_section_delete($presentation, $sectionid) {
652 $secrec = get_record("presentation_sections", "ident", $sectionid, "presentation", $presentation);
653 $func = "presentation_section_delete_" . $secrec->section_type;
654 if (is_callable($func)) {
655 $func($presentation, $sectionid);
658 delete_records('presentation_comments', 'section', $sectionid, 'presentation', $presentation);
659 delete_records('presentation_sections_data', 'section', $sectionid, 'presentation', $presentation);
660 delete_records('presentation_sections', 'ident', $sectionid, 'presentation', $presentation);
662 } // end function
667 * Purge a presentation
669 * @param int $presentation ID of presentation to delete
671 function presentation_delete($presentation) {
673 if ($secrecs = get_records("presentation_sections", "presentation", $presentation)) {
674 foreach ($secrecs as $secrec) {
675 presentation_section_delete($presentation, $secrec->ident);
679 delete_records('tags','tagtype','presentation','ref',$presentation);
680 delete_records('presentations', 'ident', $presentation);
682 } // end function
687 * Check whether a user id has write permission for a presentation
688 * Currently just checks owner, but could be so much more!
690 * @param int $presentation presentation id
691 * @param int $userid user id
692 * @return boolean true for has write permission
694 function presentation_permissions_check($presentation, $userid) {
696 $presrec = get_record("presentations", "ident", $presentation);
698 // presentation exists? right owner?
699 if ($presrec && $presrec->owner == $userid) {
700 return true;
701 } else {
702 return false;
704 } // end function
710 * Returns HTML for a particular section
712 * @param object $section section row
713 * @return string HTML output
715 function presentation_section_display($section, $menuedit = false, $menucomments = true, $forcenoaddcomments = false) {
717 global $CFG, $PAGE, $USER;
719 $body = "";
720 if (is_object($section)) {
722 // If the handler for displaying this particular section type exists,
723 // run it - otherwise display nothing
724 $func = "presentation_section_display_" . $section->section_type;
725 if (is_callable($func)) {
726 $body = $func($section);
728 $body = "<div class=\"presentation-widget-content\">$body</div>\n";
730 $menu = array();
731 if ($menuedit && isloggedin() && presentation_permissions_check($section->presentation, $USER->ident)) {
732 $menu[] = '<a href="' . $CFG->wwwroot . 'mod/presentation/editsection.php?id=' . $section->presentation . '&amp;sec=' . $section->ident . '">' . __gettext("Edit section") . "</a>";
733 $menu[] = '<a href="' . $CFG->wwwroot . 'mod/presentation/edit.php?action=presentation:delsec&amp;id=' . $section->presentation . '&amp;sec=' . $section->ident . '" onclick="return confirm(\'' . __gettext("Are you sure you want to delete this section?") . '\')">' . __gettext("Delete section") . "</a>";
734 $menu[] = '<a href="' . $CFG->wwwroot . 'mod/presentation/edit.php?action=presentation:movesec&amp;id=' . $section->presentation . '&amp;sec=' . $section->ident . '&amp;dir=up">' . __gettext("Move up") . "</a>";
735 $menu[] = '<a href="' . $CFG->wwwroot . 'mod/presentation/edit.php?action=presentation:movesec&amp;id=' . $section->presentation . '&amp;sec=' . $section->ident . '&amp;dir=down">' . __gettext("Move down") . "</a>";
740 //TODO comment link/code here?
743 $commentstext = '';
744 if ($menucomments) {
745 $numcomments = 0;
746 if ($comments = presentation_get_comments($section->presentation, $section->ident)) {
747 $numcomments = count($comments);
750 $blockid = "cmtp" . $section->presentation . "s" . $section->ident;
752 $presrec = get_record("presentations", "ident", $section->presentation);
753 $addform = false;
754 if (!$forcenoaddcomments) {
755 if (logged_on || (!$CFG->disable_publiccomments && user_flag_get("publiccomments",$presrec->owner)) ) {
756 $addform = true;
760 $menutext = '';
761 if ($numcomments || $addform) {
762 $menutext .= '<a class="presentation-seclinkcom" href="javascript:toggleVisibility(\'' . $blockid . '\')">';
763 $menutext .= $numcomments . __gettext(' Section Comments');
764 $menutext .= '</a>';
766 if ($menutext) {
767 $menu[] = $menutext;
770 $commentstext = presentation_comments_display($section->presentation, $section->ident, $comments, $addform);
773 if ($menuedit || $menucomments) {
774 $menu[] = '<a class="presentation-seclinktop" href="#container" title="' . __gettext('Jump to the top of the page') . '">' . __gettext('Top') . '</a>';
775 $body .= '<p class="presentation-widget-menu">' . implode(' | ', $menu) . '</p>';
776 $body .= $commentstext;
779 $body = "<div class=\"presentation-widget\">$body</div>\n";
783 // Return HTML
784 return $body;
786 } // end function
790 * Return title for section
792 * @param object $section section row
793 * @return string HTML output
795 function presentation_section_gettitle($section) {
797 $body = '';
798 if (is_object($section)) {
800 // If the handler for displaying this particular section type exists,
801 // run it - otherwise display nothing
802 $func = "presentation_section_gettitle_" . $section->section_type;
803 if (is_callable($func)) {
804 $body = $func($section);
809 // Return HTML
810 return $body;
815 * Returns HTML for editing a particular section
817 * @param object $section section row object
818 * @return string HTML output
820 function presentation_section_edit($section) {
822 global $CFG, $PAGE;
824 $body = "something went wrong";
825 $showform = true;
827 if (is_object($section)) {
828 // If the handler for displaying this particular section type exists,
829 // run it - otherwise display nothing
830 $func = "presentation_section_edit_" . $section->section_type;
831 if (is_callable($func)) {
832 $obj = $func($section);
833 $body = $obj->body;
834 $showform = $obj->showform;
837 if ($section->ident > 0) {
838 $submitstring = __gettext("Update section");
839 } else {
840 $submitstring = __gettext("Add section");
843 if ($showform) {
844 // Stick it in an appropriate form for saving
845 $body = "<form action=\"" . $CFG->wwwroot . "mod/presentation/edit.php\" method=\"post\">\n" . $body;
847 $body .= '
849 <input type="hidden" name="action" value="presentation:savesec" />
850 <input type="hidden" name="id" value="' . $section->presentation . '" />
851 <input type="hidden" name="sec" value="' . $section->ident . '" />
852 <input type="hidden" name="sectype" value="' . $section->section_type . '" />
853 <input type="submit" value="' . $submitstring . '" />
854 </p>
855 </form>
861 return $body;
863 } // end function
872 * Display plain text section
874 * @param object $section section row
875 * @return string HTML output
877 function presentation_section_display_text($section) {
879 global $CFG;
881 $title = presentation_section_get_data($section->ident,"title");
882 $body = presentation_section_get_data($section->ident,"body");
883 $return = '';
885 if ($title->value) {
886 $return .= "<h2>" . $title->value . "</h2>\n";
888 if ($body->value) {
889 $return .= "<div>" . nl2br($body->value) . "</div>\n";
892 return $return;
894 } // end function
898 * Return title for plain text section
900 * @param object $section section row
901 * @return string HTML output
903 function presentation_section_gettitle_text($section) {
905 global $CFG;
907 $title = presentation_section_get_data($section->ident,"title");
908 if (empty($title->value)) {
909 $ret = 'Untitled';
910 } else {
911 $ret = htmlspecialchars($title->value);
914 return $ret;
916 } // end function
920 * Display edit form for plain text section
922 * @param object $section section row
923 * @return object body=HTML output, showform=whether to surround output with a form
925 function presentation_section_edit_text($section) {
927 global $CFG, $page_owner;
928 $ret = new stdClass;
929 $ret->body = "";
930 $ret->showform = true;
932 if (is_object($section)) {
933 if ($section->ident > 0) {
934 $sectitle = presentation_section_get_data($section->ident,"title");
935 $sectitle = $sectitle->value;
936 $secbody = presentation_section_get_data($section->ident,"body");
937 $secbody = $secbody->value;
938 } else {
939 $sectitle = '';
940 $secbody = '';
943 $ret->body .= "<h2>" . __gettext("Text box") . "</h2>\n";
944 $ret->body .= "<p>" . __gettext("This displays the text content of your choice. All you need to do is enter the title and body below:") . "</p>\n";
946 if ($section->ident > 0) {
947 // presentation edit page runs its own
948 run('tinymce:include', array(array("pres_sec_data__body__")) );
951 $ret->body .= "<p>" . display_input_field(array("pres_sec_data[title]",$sectitle,"text")) . "</p>\n";
952 $ret->body .= "<p>" . display_input_field(array("pres_sec_data[body]",$secbody,"longtext")) . "</p>\n";
956 return $ret;
958 } // end function
962 * Handle saving data for a plain text section
964 * @param int $presentation presentation id
965 * @param int $section section id
966 * @param array $dataarray extra data
967 * @return boolean
969 function presentation_section_save_text($presentation, $section, $dataarray) {
971 global $CFG, $page_owner;
972 $return = true;
974 if (is_array($dataarray)) {
975 foreach ($dataarray as $name => $value) {
976 presentation_section_set_data($presentation, $section, $name, $value);
980 return $return;
981 } // end function
990 * Display blog post section
992 * @param object $section section row
993 * @return string HTML output
995 function presentation_section_display_blogpost($section) {
997 global $CFG, $template;
998 $body = "";
1000 if (is_object($section)) {
1002 $data = presentation_section_get_all_data($section->ident);
1003 //var_dump($data);
1004 $sectitle = isset($data['title']) ? $data['title']->value : 'Untitled';
1005 $secbody = isset($data['body']) ? $data['body']->value : '';
1006 $secposted = isset($data['posted']) ? strftime("%d %B %Y", $data['posted']->value) : '';
1007 $secicon = isset($data['icon']) ? $data['icon']->value : -1;
1008 $secblogid = isset($data['weblog']) ? $data['weblog']->value : '';
1009 $secowner = isset($data['owner']) ? $data['owner']->value : '';
1010 $commentsbody = isset($data['comments']) ? $data['comments']->value : '';
1011 $numcomments = isset($data['numcomments']) ? $data['numcomments']->value : 0;
1012 $tags = isset($data['tags']) ? $data['tags']->value : '';
1013 $narrative = isset($data['narrative']) ? $data['narrative']->value : '';
1015 $sourceid = presentation_section_get_data($section->ident, "sourceid");
1017 $body .= '<h2>Blog post: ' . $secposted . '</h2>';
1018 //$body .= '<div>' . $secbody . '</div>';
1019 if ($narrative) {
1020 $body .= '<div class="presentation-narrative">' . $narrative . '</div>';
1023 if ($numcomments && $commentsbody) {
1024 $blockid = "cmtp" . $section->presentation . "s" . $section->ident . "content";
1025 $commentsbody = templates_draw(
1026 array(
1027 'context' => 'presentation_weblogcomments',
1028 'comments' => $commentsbody,
1029 'numcomments' => $numcomments,
1030 'blockid' => $blockid,
1035 if ($secicon == -1) {
1036 if ($newicon = user_info("icon", $secowner)) {
1037 $secicon = $newicon;
1041 $fullname = '';
1042 $username = '';
1043 $links = '';
1045 $body .= templates_draw(
1046 array(
1047 'context' => 'presentation_weblogpost',
1048 'date' => $secposted,
1049 'username' => $username,
1050 'usericon' => $secicon,
1051 'body' => $secbody,
1052 'fullname' => $fullname,
1053 'title' => $sectitle,
1054 'comments' => $commentsbody,
1055 'tags' => $tags,
1056 'links' => $links
1060 if (!empty($sourceid)) {
1061 $body .= '<div class="presentation-imported-guff">';
1062 if ($sourceid->created) {
1063 $body .= __gettext('Imported at: ') . strftime("%d/%m/%Y %H:%M %Z", $sourceid->created) . '<br />';
1066 if ($sourcepost = get_record("weblog_posts", "ident", $sourceid->value)) {
1067 $ownerusername = user_info("username", $sourcepost->weblog);
1068 $url = $CFG->wwwroot . $ownerusername . '/weblog/' . $sourceid->value . '.html';
1069 $body .= sprintf(__gettext('The original blog post this was imported from is <a href="%s">here</a>.'), $url) . '<br />';
1071 $body .= '</div>';
1076 return $body;
1081 * Return title for blog post section
1083 * @param object $section section row
1084 * @return string HTML output
1086 function presentation_section_gettitle_blogpost($section) {
1088 global $CFG;
1090 $title = presentation_section_get_data($section->ident,"title");
1091 $posted = presentation_section_get_data($section->ident,"posted");
1093 $ret = 'Blog post: ' . strftime("%d %B %Y", $posted->value) . ' -- ';
1095 if (empty($title->value)) {
1096 $ret .= 'Untitled';
1097 } else {
1098 $ret .= htmlspecialchars($title->value);
1101 return $ret;
1103 } // end function
1107 * Display edit form for blog post section
1109 * @param object $section section row
1110 * @return object body=HTML output, showform=whether to surround output with a form
1112 function presentation_section_edit_blogpost($section) {
1114 global $CFG, $USER, $page_owner;
1115 $ret = new stdClass;
1116 $ret->body = "";
1117 $ret->showform = true;
1119 if (is_object($section)) {
1121 $presrec = get_record("presentations", "ident", $section->presentation);
1123 $ret->body .= "<h2>" . __gettext("Blog post") . "</h2>\n";
1124 $url = $CFG->wwwroot . $USER->username . '/weblog/';;
1126 $secnarrative = presentation_section_get_data($section->ident,"narrative");
1127 $secnarrative = $secnarrative->value;
1128 if ($section->ident > 0) {
1129 // presentation edit page runs its own
1130 run('tinymce:include', array(array("pres_sec_data__blognarrative__")) );
1132 $ret->body .= "<p>" . __gettext('Description:') . "</p>\n";
1133 $ret->body .= "<p>" . display_input_field(array("pres_sec_data[blognarrative]",$secnarrative,"longtext")) . "</p>\n";
1135 if ($section->ident > 0) {
1136 //editing
1138 $ownerusername = user_info("username", $presrec->owner);
1139 $url = $CFG->wwwroot . $ownerusername . '/weblog/';
1141 $sourceid = presentation_section_get_data($section->ident, "sourceid");
1142 if ($sourcepost = get_record("weblog_posts", "ident", $sourceid->value)) {
1144 $url .= $sourceid->value . '.html';
1145 $ret->body .= "<p>" . sprintf(__gettext('You can reimport <a href="%s">this post</a> from your blog, optionally with the comments it currently has:'), $url) . "</p>\n";
1147 $ret->body .= '<p>'
1148 . '<label for="pres_sec_data__postid__"><input type="checkbox" id="pres_sec_data__postid__" name="pres_sec_data[postid]" value="' . $sourcepost->ident . '" /> ' . __gettext('Reimport blog post.') . '</label>'
1149 . '</p>'
1150 . "\n";
1151 $ret->body .= '<p>'
1152 . '<label for="pres_sec_data__withcomments__"><input type="checkbox" id="pres_sec_data__withcomments__" name="pres_sec_data[withcomments]" value="yes" /> ' . __gettext('Import comments too.') . '</label>'
1153 . '</p>'
1154 . "\n";
1156 } else {
1158 $ret->body .= "<p>" . sprintf(__gettext('You can not reimport this post, as it no longer exists on <a href="%s">your blog</a>.'), $url) . "</p>\n";
1159 $ret->showform = false;
1162 } else {
1163 //adding
1165 $ret->body .= "<p>" . sprintf(__gettext('You can import a post from <a href="%s">your blog</a> into a new section, optionally with the comments it currently has:'), $url) . "</p>\n";
1167 if ($posts = get_records_select('weblog_posts', 'owner = '.$presrec->owner, null, 'posted ASC')) {
1168 $ret->body .= '<p><select name="pres_sec_data[postid]">' . "\n";
1169 $lasttime = '';
1170 foreach ($posts as $apost) {
1171 $time = strftime("%d %B %Y", $apost->posted);
1172 if ($time != $lasttime) {
1173 if ($lasttime) {
1174 $ret->body .= '</optgroup>' . "\n";
1176 $ret->body .= '<optgroup label="' . $time . '">' . "\n";
1178 $title = strip_tags(get_access_description($apost->access)) . (($apost->title) ? htmlspecialchars($apost->title) : "Untitled");
1179 if ($apost->weblog != $apost->owner) {
1180 $title = '@' . run("profile:display:name", $apost->weblog) . ': ' . $title;
1182 $text = htmlspecialchars(substr($apost->body, 0, 50));
1183 $ret->body .= "\t" . '<option value="' . $apost->ident . '" title="' . $text . '">' . $title . '</option>' . "\n";
1184 $lasttime = $time;
1186 if ($lasttime) {
1187 $ret->body .= '</optgroup>' . "\n";
1189 $ret->body .= '</select></p>' . "\n";
1190 $ret->body .= '<p><label for="pres_sec_data__withcomments__"><input type="checkbox" id="pres_sec_data__withcomments__" name="pres_sec_data[withcomments]" value="yes" /> ' . __gettext('Import blog post comments too.') . '</label></p>' . "\n";
1192 } else {
1193 // no posts
1194 $ret->showform = false;
1195 $ret->body .= "<p>" . __gettext("You don't have any blog posts yet.") . "</p>\n";
1203 return $ret;
1207 * Handle saving data for a blog post section
1209 * @param int $presentation presentation id
1210 * @param int $section section id
1211 * @param array $dataarray extra data
1212 * @return boolean
1214 function presentation_section_save_blogpost($presentation, $section, $dataarray) {
1216 global $CFG, $USER, $page_owner;
1217 $return = true;
1219 if (!empty($dataarray['blognarrative'])) {
1220 presentation_section_set_data($presentation, $section, "narrative", $dataarray['blognarrative']);
1223 if (!empty($dataarray['postid'])) {
1224 if ($post = get_record("weblog_posts", "ident", $dataarray['postid'], "owner", $USER->ident)) {
1226 presentation_section_set_data($presentation, $section, "sourceid", $post->ident);
1227 $postbody = run("weblogs:text:process", $post->body);
1228 presentation_section_set_data($presentation, $section, "body", $postbody);
1229 presentation_section_set_data($presentation, $section, "title", $post->title);
1230 presentation_section_set_data($presentation, $section, "icon", $post->icon);
1231 presentation_section_set_data($presentation, $section, "owner", $post->owner);
1232 presentation_section_set_data($presentation, $section, "weblog", $post->weblog);
1233 presentation_section_set_data($presentation, $section, "posted", $post->posted);
1235 $keywords = display_output_field(array("", "keywords", "weblog", "weblog", $post->ident, $post->owner));
1236 presentation_section_set_data($presentation, $section, "tags", $keywords);
1238 if (!empty($dataarray['withcomments'])) {
1239 //adding comments too
1241 $commentsbody = '';
1242 $numcomments = 0;
1243 if ($commentarray = get_records('weblog_comments','post_id',$post->ident,'posted ASC')) {
1245 $numcomments = count($commentarray);
1247 templates_page_setup();
1249 foreach ($commentarray as $comment) {
1250 // turn commentor name into a link if they're a registered user
1251 // add rel="nofollow" to comment links if they're not
1252 if ($comment->owner > 0) {
1253 $commentownerusername = user_info('username', $comment->owner);
1254 $comment->postedname = '<a href="' . $CFG->wwwroot . $commentownerusername . '/">' . $comment->postedname . '</a>';
1255 $comment->icon = '<a href="' . $CFG->wwwroot . $commentownerusername . '/"><img src="' . $CFG->wwwroot . '_icon/user/' . run("icons:get",$comment->owner) . '/w/50/h/50" border="0" align="left" alt="" /></a>';
1256 $comment->body = run("weblogs:text:process", array($comment->body, false));
1257 } else {
1258 $comment->icon = '<img src="' . $CFG->wwwroot . '_icons/data/default.png" width="50" height="50" align="left" alt="" />';
1259 $comment->body = run("weblogs:text:process", array($comment->body, true));
1261 $commentsbody .= templates_draw(
1262 array(
1263 'context' => 'presentation_weblogcomment',
1264 'postedname' => $comment->postedname,
1265 'body' => '' . $comment->body,
1266 'posted' => strftime("%A, %d %B %Y, %H:%M %Z",$comment->posted),
1267 'usericon' => $comment->icon,
1274 presentation_section_set_data($presentation, $section, "comments", $commentsbody);
1275 presentation_section_set_data($presentation, $section, "numcomments", $numcomments);
1276 } else {
1277 presentation_section_set_data($presentation, $section, "comments", '');
1278 presentation_section_set_data($presentation, $section, "numcomments", 0);
1284 return $return;
1297 * Display file section
1299 * @param object $section section row
1300 * @return string HTML output
1302 function presentation_section_display_file($section) {
1304 global $CFG;
1305 $body = '';
1306 if (is_object($section)) {
1308 $data = presentation_section_get_all_data($section->ident);
1310 $title = isset($data['title']) ? $data['title']->value : 'Untitled';
1311 $description = isset($data['description']) ? $data['description']->value : '';
1312 $size = isset($data['size']) ? $data['size']->value : 0;
1313 $originalname = isset($data['originalname']) ? $data['originalname']->value : '';
1314 $location = isset($data['location']) ? $data['location']->value : '';
1315 $tags = isset($data['tags']) ? $data['tags']->value : '';
1316 $narrative = isset($data['narrative']) ? $data['narrative']->value : '';
1318 $sourceid = presentation_section_get_data($section->ident,"sourceid");
1320 $body .= "<h2>File: " . htmlspecialchars($title) . "</h2>\n";
1321 if ($narrative) {
1322 $body .= '<div class="presentation-narrative">' . $narrative . '</div>';
1324 $url = $CFG->wwwroot . 'mod/presentation/file.php?pres=' . $section->presentation . '&amp;sec=' . $section->ident;
1326 $body .= '<div class="presentation-file">';
1327 $body .= '<a href="' . $url . '&amp;get=file"><img align="left" border="0" src="' . $url . '&amp;get=icon" alt="' . __gettext('Thumbnail') . '" /></a>';
1328 $body .= '<div style="margin-left: 100px;">';
1329 $body .= __gettext('Title: ') . htmlspecialchars($title) . '<br />';
1330 $body .= __gettext('Description: ') . $description . '<br />';
1331 $body .= __gettext('Keywords: ') . $tags . '<br />';
1332 $body .= __gettext('Size: ') . $size . ' ' . __gettext('bytes') . '<br />';
1333 $body .= __gettext('Original filename: ') . $originalname . '<br />';
1334 //$body .= 'location: ' . $location->value . '<br />';
1335 //$body .= 'Source: ' . $sourceid->value . '<br />';
1336 $body .= '</div>';
1338 $body .= '<div class="presentation-imported-guff">';
1339 $body .= __gettext('Imported at: ') . strftime("%d/%m/%Y %H:%M %Z", $sourceid->created) . '<br />';
1341 if ($sourcefile = get_record("files", "ident", $sourceid->value)) {
1342 $ownerusername = user_info("username", $sourcefile->files_owner);
1343 $url = $CFG->wwwroot . $ownerusername . '/files/';
1344 if ($sourcefile->folder > 0) {
1345 $url .= $sourcefile->folder . '/';
1347 $body .= sprintf(__gettext('The original file this was imported from is <a href="%s">here</a>.'), $url) . '<br />';
1350 $body .= '</div>';
1351 $body .= '</div>';
1354 return $body;
1359 * Return title for file section
1361 * @param object $section section row
1362 * @return string HTML output
1364 function presentation_section_gettitle_file($section) {
1366 $title = presentation_section_get_data($section->ident,"title");
1368 $ret = 'File: ';
1370 if (empty($title->value)) {
1371 $ret .= 'Untitled';
1372 } else {
1373 $ret .= htmlspecialchars($title->value);
1376 return $ret;
1378 } // end function
1382 * Display edit form for file section
1384 * @param object $section section row
1385 * @return object body=HTML output, showform=whether to surround output with a form
1387 function presentation_section_edit_file($section) {
1389 global $CFG, $USER, $page_owner;
1390 $ret = new stdClass;
1391 $ret->body = "";
1392 $ret->showform = true;
1394 //TODO - better file selector
1396 if (is_object($section)) {
1398 $presrec = get_record("presentations", "ident", $section->presentation);
1400 $ret->body .= "<h2>" . __gettext("File") . "</h2>\n";
1401 $fileurl = $CFG->wwwroot . $USER->username . '/files/';
1403 $secnarrative = presentation_section_get_data($section->ident,"narrative");
1404 $secnarrative = $secnarrative->value;
1405 if ($section->ident > 0) {
1406 // presentation edit page runs its own
1407 run('tinymce:include', array(array("pres_sec_data__filenarrative__")) );
1409 $ret->body .= "<p>" . __gettext('Description:') . "</p>\n";
1410 $ret->body .= "<p>" . display_input_field(array("pres_sec_data[filenarrative]",$secnarrative,"longtext")) . "</p>\n";
1412 if ($section->ident > 0) {
1413 //editing
1415 $ownerusername = user_info("username", $presrec->owner);
1417 $sourceid = presentation_section_get_data($section->ident, "sourceid");
1418 if ($sourcefile = get_record("files", "ident", $sourceid->value)) {
1420 $url = $CFG->wwwroot . '_files/edit_file.php?edit_file_id=' . $sourcefile->ident . '&amp;owner=' . $presrec->owner;
1421 $ret->body .= "<p>" . sprintf(__gettext('You can reimport this file, to update its title/description/keywords from the <a href="%s">source file</a>.'), $url) . '</p>';
1422 $ret->body .= '<p><label for="pres_sec_data__fileid__"><input type="checkbox" id="pres_sec_data__fileid__" name="pres_sec_data[fileid]" value="' . $sourcefile->ident . '" /> ' . __gettext('Reimport file.') . '</label></p>' . "\n";
1424 } else {
1426 $url = $CFG->wwwroot . $ownerusername . '/files/';
1427 $ret->body .= "<p>" . sprintf(__gettext('You can not reimport this file, as it no longer exists in <a href="%s">your files</a>.'), $url) . "</p>\n";
1428 $ret->showform = false;
1432 } else {
1433 //adding
1435 $ret->body .= "<p>" . sprintf(__gettext('You can copy one of <a href="%s">your files</a> into a new section.'), $fileurl) . "</p>\n";
1436 if ($files = get_records_select('files',"files_owner = ?",array($USER->ident))) {
1438 $ret->body .= '<p><select name="pres_sec_data[fileid]">' . "\n";
1439 $ret->body .= presentation_viewfolder(-1, $USER->ident, 0);
1440 $ret->body .= "</select></p>\n";
1442 } else {
1444 // no files
1445 $ret->showform = false;
1446 $ret->body .= "<p>" . __gettext("You don't have any files yet.") . "</p>\n";
1453 return $ret;
1457 * Handle saving data for a file section
1459 * @param int $presentation presentation id
1460 * @param int $section section id
1461 * @param array $dataarray extra data
1462 * @return boolean
1464 function presentation_section_save_file($presentation, $section, $dataarray) {
1466 global $CFG, $USER, $page_owner;
1467 $return = false;
1469 require_once($CFG->dirroot . 'lib/filelib.php');
1471 $initial = substr($presentation, -1, 1);
1472 require_once($CFG->dirroot . 'lib/uploadlib.php');
1473 make_upload_directory('presentation');
1474 make_upload_directory('presentation/' . $initial);
1475 make_upload_directory('presentation/' . $initial . '/' . $presentation);
1477 if (!empty($dataarray['filenarrative'])) {
1478 presentation_section_set_data($presentation, $section, "narrative", $dataarray['filenarrative']);
1481 if (!empty($dataarray['fileid'])) {
1482 if ($file = get_record("files", "ident", $dataarray['fileid'], "owner", $USER->ident)) {
1483 if ($filelocation = file_cache($file)) {
1484 $filename = basename($filelocation);
1485 $location = 'presentation/' . $initial . '/' . $presentation . '/' . time() . $filename;
1486 $destdir = $CFG->dataroot . $location;
1487 // var_dump($filelocation);
1488 // var_dump($destdir);
1489 if (copy($filelocation, $destdir)) {
1491 presentation_section_set_data($presentation, $section, "sourceid", $file->ident);
1492 presentation_section_set_data($presentation, $section, "originalname", $file->originalname);
1493 presentation_section_set_data($presentation, $section, "location", $location);
1494 presentation_section_set_data($presentation, $section, "title", $file->title);
1495 presentation_section_set_data($presentation, $section, "description", $file->description);
1496 presentation_section_set_data($presentation, $section, "size", $file->size);
1498 $keywords = display_output_field(array("", "keywords", "file", "file", $file->ident, $file->owner));
1499 presentation_section_set_data($presentation, $section, "tags", $keywords);
1501 $return = true;
1508 return $return;
1513 * Handle deleting data for a file section
1515 * @param int $presentation presentation id
1516 * @param int $section section id
1517 * @return boolean
1519 function presentation_section_delete_file($presentation, $section) {
1521 global $CFG, $USER, $page_owner;
1522 $return = true;
1524 $location = presentation_section_get_data($section, "location");
1525 $fullocation = $CFG->dataroot . $location->value;
1526 if (is_file($fullocation)) {
1527 unlink($fullocation);
1530 return $return;
1542 * Get a data row for a section
1544 * @param int $sectionid section id
1545 * @param string $field "name"-field value to search for
1546 * @return object presentation_sections_data table row
1548 function presentation_section_get_data($sectionid, $field) {
1550 global $db, $CFG;
1552 $value = get_record_sql("SELECT * FROM ".$CFG->prefix."presentation_sections_data where section=? AND name=?", array($sectionid, $field));
1554 if (empty($value)) {
1555 $value = new stdClass;
1556 $value->name = $field;
1557 $value->value = "";
1560 return $value;
1562 } // end function
1567 * Get a data array for a section
1569 * @param int $sectionid section id
1570 * @param string $field "name"-field value to search for
1571 * @return mixed false or an array of allpresentation_sections_data
1573 function presentation_section_get_all_data($sectionid) {
1575 return get_records('presentation_sections_data', 'section', $sectionid, $sort='', $fields='name,value');
1577 } // end function
1582 * Sets data with a particular name for a particular section ID
1584 * @param int $presentation presentation id
1585 * @param int $section section id
1586 * @param string $name field name
1587 * @param string $value value to set
1588 * @return mixed something
1590 function presentation_section_set_data($presentation, $section, $name, $value) {
1591 $data = new stdClass;
1592 $data->presentation = $presentation;
1593 $data->section = $section;
1594 $data->name = $name;
1595 $data->value = $value;
1596 $data->created = time();
1597 $existing = presentation_section_get_data($section, $name);
1598 if (!empty($existing->ident)) {
1599 $data->ident = $existing->ident;
1600 return update_record('presentation_sections_data',$data);
1601 } else {
1602 return insert_record('presentation_sections_data',$data);
1604 } // end function
1619 * copied from units/files/weblogs_posts_add_fields.php and modified
1621 * @param int $folderid folder id
1622 * @param int $userid user id whose files we're browsing
1623 * @param int $level how many levels down the recursion we are
1624 * @return string HTML of a list of <option> tags
1627 function presentation_viewfolder($folderid, $userid, $level) {
1629 $prefix = "";
1630 for ($i = 0; $i < $level; $i++) {
1631 $prefix .= "&gt;";
1633 $fileprefix = $prefix . "&gt;";
1635 if ($folderid == -1) {
1636 $body = <<< END
1637 <option value="" title="Folder" disabled="disabled">ROOT</option>
1638 END;
1639 } else {
1640 $current_folder = get_record('file_folders','files_owner',$userid,'ident',$folderid);
1641 $textlib = textlib_get_instance();
1642 $name = htmlspecialchars($textlib->strtoupper($current_folder->name));
1643 $body = <<< END
1644 <option value="" title="Folder" disabled="disabled">{$prefix} {$name}</option>
1645 END;
1647 if ($files = get_records_select('files',"files_owner = ? AND folder = ?",array($userid,$folderid))) {
1648 foreach($files as $file) {
1649 $filetitle = strip_tags(get_access_description($file->access)) . htmlspecialchars($file->title);
1650 $filename = htmlspecialchars($file->originalname);
1652 $body .= <<< END
1653 <option value="{$file->ident}" title="{$filename}">{$fileprefix} {$filetitle}</option>
1654 END;
1658 if ($folders = get_records_select('file_folders',"files_owner = ? AND parent = ? ",array($userid,$folderid))) {
1659 foreach($folders as $folder) {
1660 $body .= presentation_viewfolder($folder->ident, $userid, $level + 1);
1663 return $body;
1664 } // end function
1669 * get the comments for a given presentation, overall or a section
1671 * @param int $presentation presentation id
1672 * @param int $section section id, 0 for presentation comments
1673 * @return mixed array list of comments, or false
1676 function presentation_get_comments($presentation, $section) {
1678 return get_records_select('presentation_comments', 'presentation = ? AND section = ?', array($presentation,$section), 'created ASC');
1680 } // end function
1688 * get the comment adding form
1690 * @param int $presentation presentation id
1691 * @param int $section section id, 0 for presentation comments
1692 * @param string $posturl URL the form should submit to
1693 * @return string HTML
1695 function presentation_comments_add_form($presentation, $section, $posturl) {
1697 global $CFG, $USER;
1698 $run_result = '';
1700 $posturl = htmlspecialchars($posturl);
1701 if ($section > 0) {
1702 $addComment = __gettext("Add a section comment"); // gettext variable
1703 } else {
1704 $addComment = __gettext("Add a comment"); // gettext variable
1706 $run_result .= <<< END
1707 <form action="$posturl" method="post" class="presentation-commentaddform">
1708 <h2>$addComment</h2>
1709 END;
1711 $id = 'commentbodyp' . $presentation . 's' . $section;
1712 $field = '<textarea name="commentbody" id="' . $id . '" style="width: 95%; height: 200px"></textarea>';
1714 $field .= <<< END
1715 <input type="hidden" name="action" value="presentation:addcomment" />
1716 <input type="hidden" name="id" value="{$presentation}" />
1717 <input type="hidden" name="sec" value="{$section}" />
1718 END;
1720 $run_result .= templates_draw(array(
1721 'context' => 'databox1',
1722 'name' => __gettext("Your comment text"),
1723 'column1' => $field
1727 if (logged_on) {
1728 $comment_name = $USER->name;
1729 } else {
1730 $comment_name = __gettext("Guest");
1733 $run_result .= templates_draw(array(
1734 'context' => 'databox1',
1735 'name' => __gettext("Your name"),
1736 'column1' => "<input type=\"text\" name=\"postedname\" value=\"".htmlspecialchars($comment_name, ENT_COMPAT, 'utf-8')."\" />"
1740 $run_result .= templates_draw(array(
1741 'context' => 'databox1',
1742 'name' => '&nbsp;',
1743 'column1' => "<input type=\"submit\" value=\"".__gettext("Add comment")."\" />"
1747 $run_result .= "</form>\n";
1749 return $run_result;
1751 } // end function
1755 * get HTML for a presentation or section's comments
1757 * @param int $presentation presentation id
1758 * @param int $section section id, 0 for presentation comments
1759 * @param array $commentarray array of comment db rows
1760 * @param boolean $showaddform whether to show the add comment form
1761 * @return string HTML
1763 function presentation_comments_display($presentation, $section, $commentarray, $showaddform = false) {
1765 global $CFG, $USER, $PAGE;
1766 $body = '';
1767 $commentsbody = '';
1769 if ($section > 0) {
1770 $commentcontext = 'presentation_comment';
1771 $commentscontext = 'presentation_sectioncomments';
1772 $blockid = "cmtp" . $presentation . "s" . $section;
1773 } else {
1774 $commentcontext = 'presentation_comment';
1775 $commentscontext = 'presentation_comments';
1776 $blockid = "cmtp" . $presentation . "s0";
1779 $numcomments = 0;
1780 if (is_array($commentarray)) {
1781 $numcomments = count($commentarray);
1782 foreach ($commentarray as $comment) {
1784 // turn commentor name into a link if they're a registered user
1785 // add rel="nofollow" to comment links if they're not
1786 if ($comment->owner > 0) {
1787 $commentownerusername = user_info('username', $comment->owner);
1788 $comment->postedname = '<a href="' . $CFG->wwwroot . $commentownerusername . '/">' . $comment->postedname . '</a>';
1789 $comment->icon = '<a href="' . $CFG->wwwroot . $commentownerusername . '/"><img src="' . $CFG->wwwroot . '_icon/user/' . run("icons:get",$comment->owner) . '/w/50/h/50" border="0" align="left" alt="" /></a>';
1790 $comment->body = run("weblogs:text:process", array($comment->body, false));
1791 } else {
1792 $comment->icon = '<img src="' . $CFG->wwwroot . '_icons/data/default.png" width="50" height="50" align="left" alt="" />';
1793 $comment->body = run("weblogs:text:process", array($comment->body, true));
1796 $commentmenu = '';
1797 if (logged_on && ($comment->owner == $USER->ident || presentation_permissions_check($presentation, $USER->ident) )) {
1798 $returnConfirm = __gettext("Are you sure you want to permanently delete this comment?");
1799 $url = $CFG->wwwroot . 'mod/presentation/index.php?action=presentation:delcomment&amp;id=' . $presentation . '&amp;sec=' . $section . '&amp;commentid=' . $comment->ident;
1800 $commentmenu = '<a href="' . $url . '" onclick="return confirm(\'' . $returnConfirm . '\')">' . __gettext("Delete") . '</a>';
1804 $commentsbody .= templates_draw(
1805 array(
1806 'context' => $commentcontext,
1807 'postedname' => $comment->postedname,
1808 'body' => '' . $comment->body,
1809 'posted' => strftime("%A, %d %B %Y, %H:%M %Z",$comment->created),
1810 'usericon' => $comment->icon,
1811 'links' => $commentmenu,
1818 $addform = '';
1819 if ($showaddform) {
1820 $addform = presentation_comments_add_form($presentation, $section, $_SERVER['REQUEST_URI']);
1823 if ($commentsbody || $addform) {
1824 $body .= templates_draw(
1825 array(
1826 'context' => $commentscontext,
1827 'comments' => $commentsbody,
1828 'addform' => $addform,
1829 'numcomments' => $numcomments,
1830 'blockid' => $blockid,
1835 return $body;
1836 } // end function
1840 * hook into user deletion
1842 * @param string $object_type
1843 * @param string $event
1844 * @param object $object user to delete
1845 * @return object user to delete
1847 function presentation_user_delete($object_type, $event, $object) {
1849 if ($presentations = get_records('presentations', 'owner', $object->ident, $sort='', $fields='ident')) {
1850 foreach ($presentations as $apresentation) {
1851 presentation_delete($apresentation->ident);
1855 // You must pass $object back unless something goes wrong!
1856 return $object;
1857 } // end function