f98e8280d4488df0e87cae03a3e60cf4a0f6faf9
[acontent.git] / docs / home / classes / ContentManager.class.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 if (!defined('TR_INCLUDE_PATH')) exit;
14
15 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
16 require_once(TR_INCLUDE_PATH. 'classes/DAO/ContentDAO.class.php');
17 require_once(TR_INCLUDE_PATH. 'classes/Utility.class.php');
18
19 class ContentManager
20 {
21         /* db handler   */
22         var $db;
23
24         /*      array           */
25         var $_menu;
26
27         /*      array           */
28         var $_menu_info;
29
30         /*      array           */
31         var $_menu_in_order;
32
33         /* int                  */
34         var $course_id;
35
36         // private
37         var $num_sections;
38         var $max_depth;
39         var $content_length;
40         var $dao;
41         var $contentDAO;
42
43         /* constructor  */
44         function ContentManager($course_id) {
45                 if (!($course_id > 0)) {
46                         return;
47                 }
48                 $this->course_id = $course_id;
49                 $this->dao = new DAO();
50                 $this->contentDAO = new ContentDAO();
51                 
52                 /* x could be the ordering or even the content_id       */
53                 /* don't really need the ordering anyway.                       */
54                 /* $_menu[content_parent_id][x] = array('content_id', 'ordering', 'title') */
55                 $_menu = array();
56
57                 /* number of content sections */
58                 $num_sections = 0;
59
60                 $max_depth = array();
61                 $_menu_info = array();
62
63 //              $sql = "SELECT content_id, content_parent_id, ordering, title, UNIX_TIMESTAMP(release_date) AS u_release_date, content_type 
64 //                        FROM ".TABLE_PREFIX."content 
65 //                       WHERE course_id=$this->course_id 
66 //                       ORDER BY content_parent_id, ordering";
67 //              $result = mysql_query($sql, $this->db);
68
69                 $rows = $this->contentDAO->getContentByCourseID($this->course_id);
70                 
71                 if (is_array($rows)) {
72                         foreach ($rows as $row) {
73                                 $num_sections++;
74                                 $_menu[$row['content_parent_id']][] = array('content_id'=> $row['content_id'],
75                                                                                                                         'ordering'      => $row['ordering'], 
76                                                                                                                         'title'         => htmlspecialchars($row['title']),
77                                                                                                                         'content_type' => $row['content_type']);
78         
79                                 $_menu_info[$row['content_id']] = array('content_parent_id' => $row['content_parent_id'],
80                                                                                                                 'title'                         => htmlspecialchars($row['title']),
81                                                                                                                 'ordering'                      => $row['ordering'],
82                                                                                                                 'content_type' => $row['content_type']);
83         
84                                 /* 
85                                  * add test content asscioations
86                                  * find associations per content page, and add it as a sublink.
87                                  * @author harris
88                                  */
89                                 $test_rows = $this->getContentTestsAssoc($row['content_id']);
90                                 if (is_array($test_rows)) {
91                                         foreach ($test_rows as $test_row){
92                                                 $_menu[$row['content_id']][] = array(   'test_id'       => $test_row['test_id'],
93                                                                                                                                 'title'         => htmlspecialchars($test_row['title']),
94                                                                                                                                 'content_type' => CONTENT_TYPE_CONTENT);
95                                         } // end of foreach
96                                 } // end of if
97                                 /* End of add test content asscioations */
98         
99                                 if ($row['content_parent_id'] == 0) {
100                                         $max_depth[$row['content_id']] = 1;
101                                 } else {
102                                         $max_depth[$row['content_id']] = $max_depth[$row['content_parent_id']]+1;
103                                 }
104                         } // end of foreach
105                 } // end of if
106
107                 $this->_menu = $_menu;
108
109                 $this->_menu_info =  $_menu_info;
110
111                 $this->num_sections = $num_sections;
112
113                 if (count($max_depth) > 1) {
114                         $this->max_depth = max($max_depth);
115                 } else {
116                         $this->max_depth = 0;
117                 }
118
119                 // generate array of all the content ids in the same order that they appear in "content navigation"
120                 if ($this->getNextContentID(0) > 0) {
121                         $this->_menu_in_order[] = $next_content_id = $this->getNextContentID(0);
122                 }
123                 while ($next_content_id > 0)
124                 {
125                         $next_content_id = $this->getNextContentID($next_content_id);
126                         
127                         if (in_array($next_content_id, $this->_menu_in_order)) break;
128                         else if ($next_content_id > 0) $this->_menu_in_order[] = $next_content_id;
129                 }
130                 
131                 $this->content_length = count($_menu[0]);
132         }
133
134         // This function is called by initContent to construct $this->_menu_in_order, an array to 
135         // holds all the content ids in the same order that they appear in "content navigation"
136         function getNextContentID($content_id, $order=0) {
137                 // return first root content when $content_id is not given
138                 if (!$content_id) {
139                         return $this->_menu[0][0]['content_id'];
140                 }
141                 
142                 $myParent = $this->_menu_info[$content_id]['content_parent_id'];
143                 $myOrder  = $this->_menu_info[$content_id]['ordering'];
144                 
145                 // calculate $myOrder, add in the number of tests in front of this content page
146                 if (is_array($this->_menu[$myParent])) {
147                         $num_of_tests = 0;
148                         foreach ($this->_menu[$myParent] as $menuContent) {
149                                 if ($menuContent['content_id'] == $content_id) break;
150                                 if (isset($menuContent['test_id'])) $num_of_tests++;
151                         }
152                 }
153                 $myOrder += $num_of_tests;
154                 // end of calculating $myOrder
155                 
156                 /* if this content has children, then take the first one. */
157                 if ( isset($this->_menu[$content_id]) && is_array($this->_menu[$content_id]) && ($order==0) ) {
158                         /* has children */
159                         // if the child is a test, keep searching for the content id
160                         foreach ($this->_menu[$content_id] as $menuID => $menuContent)
161                         {
162                                 if (!empty($menuContent['test_id'])) continue;
163                                 else 
164                                 {
165                                         $nextMenu = $this->_menu[$content_id][$menuID]['content_id'];
166                                         break;
167                                 }
168                         }
169                         
170                         // all children are tests
171                         if (!isset($nextMenu))
172                         {
173                                 if (isset($this->_menu[$myParent][$myOrder]['content_id'])) {
174                                         // has sibling
175                                         return $this->_menu[$myParent][$myOrder]['content_id'];
176                                 }
177                                 else { // no sibling
178                                         $nextMenu = $this->getNextContentID($myParent, 1);
179                                 }
180                         }
181                         return $nextMenu;
182                 } else {
183                         /* no children */
184                         if (isset($this->_menu[$myParent][$myOrder]) && $this->_menu[$myParent][$myOrder] != '') {
185                                 /* Has sibling */
186                                 return $this->_menu[$myParent][$myOrder]['content_id'];
187                         } else {
188                                 /* No more siblings */
189                                 if ($myParent != 0) {
190                                         return $this->getNextContentID($myParent, 1);
191                                 }
192                         }
193                 }
194         }
195         
196         function getContent($parent_id=-1, $length=-1) {
197                 if ($parent_id == -1) {
198                         $my_menu_copy = $this->_menu;
199                         if ($length != -1) {
200                                 $my_menu_copy[0] = array_slice($my_menu_copy[0], 0, $length);
201                         }
202                         return $my_menu_copy;
203                 }
204                 return $this->_menu[$parent_id];
205         }
206
207
208         function &getContentPath($content_id) {
209                 $path = array();
210
211                 $path[] = array('content_id' => $content_id, 'title' => $this->_menu_info[$content_id]['title']);
212
213                 $this->getContentPathRecursive($content_id, $path);
214
215                 $path = array_reverse($path);
216                 return $path;
217         }
218
219
220         function getContentPathRecursive($content_id, &$path) {
221                 $parent_id = $this->_menu_info[$content_id]['content_parent_id'];
222
223                 if ($parent_id > 0) {
224                         $path[] = array('content_id' => $parent_id, 'title' => $this->_menu_info[$parent_id]['title']);
225                         $this->getContentPathRecursive($parent_id, $path);
226                 }
227         }
228
229         function addContent($course_id, $content_parent_id, $ordering, $title, $text, $keywords, 
230                             $related, $formatting, $head = '', $use_customized_head = 0, 
231                             $test_message = '', $content_type = CONTENT_TYPE_CONTENT) {
232                 global $_current_user, $_course_id;
233                 
234             if (!isset($_current_user) || !$_current_user->isAuthor($this->course_id)) {
235                         return false;
236                 }
237
238                 // shift the new neighbouring content down
239                 $sql = "UPDATE ".TABLE_PREFIX."content SET ordering=ordering+1 
240                          WHERE ordering>=$ordering 
241                            AND content_parent_id=$content_parent_id 
242                            AND course_id=$_course_id";
243                 $this->contentDAO->execute($sql);
244
245                 /* main topics all have minor_num = 0 */
246                 $cid = $this->contentDAO->Create($_course_id, $content_parent_id, $ordering, 0, $formatting,
247                                           $keywords, '', $title, $text, $head, $use_customized_head,
248                                           $test_message, $content_type);
249                 return $cid;
250         }
251         
252         function editContent($content_id, $title, $text, $keywords, $formatting, 
253                              $head, $use_customized_head, $test_message) {
254             global $_current_user;
255             
256                 if (!isset($_current_user) || !$_current_user->isAuthor($this->course_id)) {
257                         return FALSE;
258                 }
259
260                 $this->contentDAO->Update($content_id, $title, $text, $keywords, $formatting, $head, $use_customized_head,
261                                           $test_message);
262         }
263
264         function moveContent($content_id, $new_content_parent_id, $new_content_ordering) {
265                 global $msg, $_current_user, $_course_id;
266                 
267             if (!isset($_current_user) || !$_current_user->isAuthor($this->course_id)) {
268                         return FALSE;
269                 }
270
271                 /* first get the content to make sure it exists */
272 //              $sql    = "SELECT ordering, content_parent_id FROM ".TABLE_PREFIX."content WHERE content_id=$content_id AND course_id=$_SESSION[course_id]";
273 //              $result = mysql_query($sql, $this->db);
274                 if (!($row = $this->getContentPage($content_id)) ) {
275                         return FALSE;
276                 }
277                 $old_ordering           = $row['ordering'];
278                 $old_content_parent_id  = $row['content_parent_id'];
279                 
280                 $sql    = "SELECT max(ordering) max_ordering FROM ".TABLE_PREFIX."content WHERE content_parent_id=$old_content_parent_id AND course_id=$_course_id";
281 //              $result = mysql_query($sql, $this->db);
282 //              $row = mysql_fetch_assoc($result);
283                 $row = $this->contentDAO->execute($sql);
284                 $max_ordering = $row[0]['max_ordering'];
285                 
286                 if ($content_id == $new_content_parent_id) {
287                         $msg->addError("NO_SELF_AS_PARENT");
288                         return;
289                 }
290                 
291                 if ($old_content_parent_id == $new_content_parent_id && $old_ordering == $new_content_ordering) {
292                         $msg->addError("SAME_LOCATION");
293                         return;
294                 }
295                 
296                 $content_path = $this->getContentPath($new_content_parent_id);
297                 foreach ($content_path as $parent){
298                         if ($parent['content_id'] == $content_id) {
299                                 $msg->addError("NO_CHILD_AS_PARENT");
300                                 return;
301                         }
302                 }
303                 
304                 // if the new_content_ordering is greater than the maximum ordering of the parent content, 
305                 // set the $new_content_ordering to the maximum ordering. This happens when move the content 
306                 // to the last element under the same parent content.
307                 if ($old_content_parent_id == $new_content_parent_id && $new_content_ordering > $max_ordering) 
308                         $new_content_ordering = $max_ordering;
309                 if (($old_content_parent_id != $new_content_parent_id) || ($old_ordering != $new_content_ordering)) {
310                         // remove the gap left by the moved content
311                         $sql = "UPDATE ".TABLE_PREFIX."content 
312                                    SET ordering=ordering-1 
313                                  WHERE ordering>$old_ordering 
314                                    AND content_parent_id=$old_content_parent_id 
315                                    AND content_id<>$content_id 
316                                    AND course_id=$_course_id";
317 //                      $result = mysql_query($sql, $this->db);
318                         $this->contentDAO->execute($sql);
319
320                         // shift the new neighbouring content down
321                         $sql = "UPDATE ".TABLE_PREFIX."content 
322                                    SET ordering=ordering+1 
323                                  WHERE ordering>=$new_content_ordering 
324                                    AND content_parent_id=$new_content_parent_id 
325                                    AND content_id<>$content_id 
326                                    AND course_id=$_course_id";
327 //                      $result = mysql_query($sql, $this->db);
328                         $this->contentDAO->execute($sql);
329
330                         $sql    = "UPDATE ".TABLE_PREFIX."content 
331                                       SET content_parent_id=$new_content_parent_id, ordering=$new_content_ordering 
332                                     WHERE content_id=$content_id AND course_id=$_course_id";
333 //                      $result = mysql_query($sql, $this->db);
334                         $this->contentDAO->execute($sql);
335                 }
336         }
337         
338         function deleteContent($content_id) {
339                 global $_current_user, $_course_id;
340                 
341                 if (!isset($_current_user) || !$_current_user->isAuthor($this->course_id)) {
342                         return false;
343                 }
344
345                 /* check if exists */
346 //              $sql    = "SELECT ordering, content_parent_id FROM ".TABLE_PREFIX."content WHERE content_id=$content_id AND course_id=$_SESSION[course_id]";
347 //              $result = mysql_query($sql, $this->db);
348 //              if (!($row = @mysql_fetch_assoc($result)) ) {
349                 if (!($row = $this->getContentPage($content_id)) ) {
350                         return false;
351                 }
352                 $ordering                       = $row['ordering'];
353                 $content_parent_id      = $row['content_parent_id'];
354
355                 /* check if this content has sub content        */
356                 $children = $this->_menu[$content_id];
357
358                 if (is_array($children) && (count($children)>0) ) {
359                         /* delete its children recursively first*/
360                         foreach ($children as $x => $info) {
361                                 if ($info['content_id'] > 0) {
362                                         $this->deleteContentRecursive($info['content_id']);
363                                 }
364                         }
365                 }
366
367                 $this->contentDAO->Delete($content_id);
368
369                 /* re-order the rest of the content */
370                 $sql = "UPDATE ".TABLE_PREFIX."content SET ordering=ordering-1 WHERE ordering>=$ordering AND content_parent_id=$content_parent_id AND course_id=$_course_id";
371                 $this->contentDAO->execute($sql);
372                 
373                 /* delete this content page                                     */
374 //              $sql    = "DELETE FROM ".TABLE_PREFIX."content WHERE content_id=$content_id AND course_id=$_SESSION[course_id]";
375 //              $result = mysql_query($sql, $this->db);
376
377                 /* delete this content from member tracking page        */
378 //              $sql    = "DELETE FROM ".TABLE_PREFIX."member_track WHERE content_id=$content_id AND course_id=$_SESSION[course_id]";
379 //              $result = mysql_query($sql, $this->db);
380
381 //              $sql    = "DELETE FROM ".TABLE_PREFIX."related_content WHERE content_id=$content_id OR related_content_id=$content_id";
382 //              $result = mysql_query($sql, $this->db);
383
384                 /* delete the content tests association */
385 //              $sql    = "DELETE FROM ".TABLE_PREFIX."content_tests_assoc WHERE content_id=$content_id";
386 //              $result = mysql_query($sql, $this->db);
387
388                 /* delete the content forum association */
389 //              $sql    = "DELETE FROM ".TABLE_PREFIX."content_forums_assoc WHERE content_id=$content_id";
390 //              $result = mysql_query($sql, $this->db);
391
392                 /* Delete all AccessForAll contents */
393 //              require_once(TR_INCLUDE_PATH.'classes/A4a/A4a.class.php');
394 //              $a4a = new A4a($content_id);
395 //              $a4a->deleteA4a();
396
397                 /* remove the "resume" to this page, b/c it was deleted */
398 //              $sql = "UPDATE ".TABLE_PREFIX."course_enrollment SET last_cid=0 WHERE course_id=$_SESSION[course_id] AND last_cid=$content_id";
399 //              $result = mysql_query($sql, $this->db);
400
401                 return true;
402         }
403
404
405         /* private. call from deleteContent only. */
406         function deleteContentRecursive($content_id) {
407                 /* check if this content has sub content        */
408                 $children = $this->_menu[$content_id];
409
410                 if (is_array($children) && (count($children)>0) ) {
411                         /* delete its children recursively first*/
412                         foreach ($children as $x => $info) {
413                                 if ($info['content_id'] > 0) {
414                                         $this->deleteContent($info['content_id']);
415                                 }
416                         }
417                 }
418
419                 // delete this content page
420                 $this->contentDAO->Delete($content_id);
421 //              $sql    = "DELETE FROM ".TABLE_PREFIX."content WHERE content_id=$content_id AND course_id=$_SESSION[course_id]";
422 //              $result = mysql_query($sql, $this->db);
423
424                 /* delete this content from member tracking page        */
425 //              $sql    = "DELETE FROM ".TABLE_PREFIX."member_track WHERE content_id=$content_id";
426 //              $result = mysql_query($sql, $this->db);
427
428                 /* delete the content tests association */
429 //              $sql    = "DELETE FROM ".TABLE_PREFIX."content_tests_assoc WHERE content_id=$content_id";
430 //              $result = mysql_query($sql, $this->db);
431         }
432
433         function getContentPage($content_id) {
434                 include_once(TR_INCLUDE_PATH.'classes/DAO/ContentDAO.class.php');
435                 $contentDAO = new ContentDAO();
436                 return $contentDAO->get($content_id);
437         }
438         
439         /** 
440          * Return a list of tests associated with the selected content
441          * @param       int             the content id that all tests are associated with it.
442          * @return      array   list of tests
443          * @date        Sep 10, 2008
444          * @author      Harris
445          */
446         function & getContentTestsAssoc($content_id){
447                 $sql    = "SELECT ct.test_id, t.title 
448                              FROM (SELECT * FROM ".TABLE_PREFIX."content_tests_assoc 
449                                     WHERE content_id=$content_id) AS ct 
450                              LEFT JOIN ".TABLE_PREFIX."tests t ON ct.test_id=t.test_id
451                             ORDER BY t.title";
452                 return $this->dao->execute($sql);
453         }
454
455         function & cleanOutput($value) {
456                 return stripslashes(htmlspecialchars($value));
457         }
458
459
460         /* @See include/html/editor_tabs/properties.inc.php */
461         /* Access: Public */
462         function getNumSections() {
463                 return $this->num_sections;
464         }
465
466         /* Access: Public */
467         function getMaxDepth() {
468                 return $this->max_depth;
469         }
470
471         /* Access: Public */
472         function getContentLength() {
473                 return $this->content_length;
474         }
475
476         /* @See include/html/dropdowns/local_menu.inc.php */
477         function getLocationPositions($parent_id, $content_id) {
478                 $siblings = $this->getContent($parent_id);
479                 for ($i=0;$i<count($siblings); $i++){
480                         if ($siblings[$i]['content_id'] == $content_id) {
481                                 return $i;
482                         }
483                 }
484                 return 0;       
485         }
486
487         /* Access: Private */
488         function getNumbering($content_id) {
489                 $path = $this->getContentPath($content_id);
490                 $parent = 0;
491                 $numbering = '';
492                 foreach ($path as $page) {
493                         $num = $this->getLocationPositions($parent, $page['content_id']) +1;
494                         $parent = $page['content_id'];
495                         $numbering .= $num.'.';
496                 }
497                 $numbering = substr($numbering, 0, -1);
498
499                 return $numbering;
500         }
501
502         function getPreviousContent($content_id) {
503                 if (is_array($this->_menu_in_order))
504                 {
505                         foreach ($this->_menu_in_order as $content_location => $this_content_id)
506                         {
507                                 if ($this_content_id == $content_id) break;
508                         }
509                         
510                         for ($i=$content_location-1; $i >= 0; $i--)
511                         {
512                                 $content_type = $this->_menu_info[$this->_menu_in_order[$i]]['content_type'];
513                                 
514                                 if ($content_type == CONTENT_TYPE_CONTENT || $content_type == CONTENT_TYPE_WEBLINK)
515                                         return array('content_id'       => $this->_menu_in_order[$i],
516                                                  'ordering'             => $this->_menu_info[$this->_menu_in_order[$i]]['ordering'],
517                                                      'title'            => $this->_menu_info[$this->_menu_in_order[$i]]['title']);
518                         }
519                 }
520                 return NULL;
521         }
522         
523         /**
524          * return the array of the next content node of the given $content_id
525          * when $content_id = 0 or is not set, return the first content node
526          * @param $content_id
527          * @return an array of the next content node
528          */
529         function getNextContent($content_id) {
530                 if (is_array($this->_menu_in_order))
531                 {
532                         // find out the location of the $content_id
533                         if (!$content_id) $content_location = 0; // the first content location when $content_id = 0 or is not set
534                         else
535                         {
536                                 foreach ($this->_menu_in_order as $content_location => $this_content_id)
537                                 {
538                                         if ($this_content_id == $content_id) 
539                                         {
540                                                 $content_location++;
541                                                 break;
542                                         }
543                                 }
544                         }
545                         
546                         // the next content node must be at or after the $content_location
547                         // and with the content type CONTENT or WEBLINK
548                         for ($i=$content_location; $i < count($this->_menu_in_order); $i++)
549                         {
550                                 $content_type = $this->_menu_info[$this->_menu_in_order[$i]]['content_type'];
551                                 
552                                 if ($content_type == CONTENT_TYPE_CONTENT || $content_type == CONTENT_TYPE_WEBLINK)
553                                         return(array('content_id'       => $this->_menu_in_order[$i],
554                                                  'ordering'             => $this->_menu_info[$this->_menu_in_order[$i]]['ordering'],
555                                                      'title'            => $this->_menu_info[$this->_menu_in_order[$i]]['title']));
556                         }
557                 }
558                 return NULL;
559         }
560         
561         /* @See include/header.inc.php */
562         function generateSequenceCrumbs($cid) {
563                 global $_base_path;
564
565                 $sequence_links = array();
566                 
567                 $first = $this->getNextContent(0); // get first
568                 
569                 if ($_SESSION['prefs']['PREF_NUMBERING'] && $first) {
570                         $first['title'] = $this->getNumbering($first['content_id']).' '.$first['title'];
571                 }
572                 if ($first) {
573                         $first['url'] = $_base_path.'home/course/content.php?_cid='.$first['content_id'];
574                         $sequence_links['first'] = $first;
575                 }
576
577                 if (!$cid && $_SESSION['s_cid']) {
578                         $resume['title'] = $this->_menu_info[$_SESSION['s_cid']]['title'];
579
580                         if ($_SESSION['prefs']['PREF_NUMBERING']) {
581                                 $resume['title'] = $this->getNumbering($_SESSION['s_cid']).' ' . $resume['title'];
582                         }
583
584                         $resume['url'] = $_base_path.'home/course/content.php?_cid='.$_SESSION['s_cid'];
585                         
586                         $sequence_links['resume'] = $resume;
587                 } else {
588                         if ($cid) {
589                                 $previous = $this->getPreviousContent($cid);
590                         }
591                         $next = $this->getNextContent($cid ? $cid : 0);
592
593                         if ($_SESSION['prefs']['PREF_NUMBERING']) {
594                                 $previous['title'] = $this->getNumbering($previous['content_id']).' '.$previous['title'];
595                                 $next['title'] = $this->getNumbering($next['content_id']).' '.$next['title'];
596                         }
597
598                         $next['url'] = $_base_path.'home/course/content.php?_cid='.$next['content_id'];
599                         if (isset($previous['content_id'])) {
600                                 $previous['url'] = $_base_path.'home/course/content.php?_cid='.$previous['content_id'];
601                         }
602                         
603                         if (isset($previous['content_id'])) {
604                                 $sequence_links['previous'] = $previous;
605                         } else if ($cid) {
606 //                              $previous['url']   = $_base_path . url_rewrite('index.php');
607 //                              $previous['url']   = $_base_path . 'home/course/index.php';
608 //                              $previous['title'] = _AT('home');
609 //                              $sequence_links['previous'] = $previous;
610                         }
611                         if (!empty($next['content_id'])) {
612                                 $sequence_links['next'] = $next;
613                         }
614                 }
615
616                 return $sequence_links;
617         }
618
619         /** Generate javascript to hide all root content folders, except the one with current content page
620          * access: private
621          * @return print out javascript function initContentMenu()
622          */
623         function initMenu(){
624                 global $_base_path, $_course_id;
625                 
626                 echo '
627 function initContentMenu() {
628   tree_collapse_icon = "'.$_base_path.'images/tree/tree_collapse.gif";
629   tree_expand_icon = "'.$_base_path.'images/tree/tree_expand.gif";
630                 
631 ';
632                 
633                 $sql = "SELECT content_id
634                           FROM ".TABLE_PREFIX."content 
635                          WHERE course_id=$this->course_id
636                            AND content_type = ".CONTENT_TYPE_FOLDER;
637                 $rows = $this->dao->execute($sql);
638
639                 // collapse all root content folders
640                 if (is_array($rows)) {
641                         foreach ($rows as $row) {
642                                 echo '
643   if (trans.utility.getcookie("t.c'.$_course_id.'_'.$row['content_id'].'") == "1")
644   {
645     jQuery("#folder"+'.$row['content_id'].').show();
646     jQuery("#tree_icon"+'.$row['content_id'].').attr("src", tree_collapse_icon);
647     jQuery("#tree_icon"+'.$row['content_id'].').attr("alt", "'._AT("collapse").'");
648     jQuery("#tree_icon"+'.$row['content_id'].').attr("title", "'._AT("collapse").'");
649   }
650   else
651   {
652     jQuery("#folder"+'.$row['content_id'].').hide();
653     jQuery("#tree_icon"+'.$row['content_id'].').attr("src", tree_expand_icon);
654     jQuery("#tree_icon"+'.$row['content_id'].').attr("alt", "'._AT("expand").'");
655     jQuery("#tree_icon"+'.$row['content_id'].').attr("title", "'._AT("expand").'");
656   }
657 ';
658                         }
659                 }
660                 
661                 // expand the content folder that has current content
662                 if (isset($_SESSION['s_cid']) && $_SESSION['s_cid'] > 0) {
663                         $current_content_path = $this->getContentPath($_SESSION['s_cid']);
664                         
665                         for ($i=0; $i < count($current_content_path)-1; $i++)
666                                 echo '
667   jQuery("#folder"+'.$current_content_path[$i]['content_id'].').show();
668   jQuery("#tree_icon"+'.$current_content_path[$i]['content_id'].').attr("src", tree_collapse_icon);
669   jQuery("#tree_icon"+'.$current_content_path[$i]['content_id'].').attr("alt", "'._AT("collapse").'");
670   trans.utility.setcookie("t.c'.$_course_id.'_'.$current_content_path[$i]['content_id'].'", "1", 1);
671 ';
672                 }
673                 echo '}'; // end of javascript function initContentMenu()
674         }
675         
676         /* @See include/html/dropdowns/menu_menu.inc.php */
677         function printMainMenu( ) {
678                 global $_current_user, $_course_id;
679                 
680                 if (!($this->course_id > 0)) {
681                         return;
682                 }
683                 
684                 global $_base_path;
685                 
686                 $parent_id    = 0;
687                 $depth        = 0;
688                 $path         = '';
689                 $children     = array();
690                 $truncate     = true;
691                 $ignore_state = true;
692
693                 $this->start = true;
694                 
695                 // if change the location of this line, change function switchEditMode(), else condition accordingly
696                 echo '<div id="editable_table">';
697                 
698                 if (isset($_current_user) && $_current_user->isAuthor($this->course_id) && !Utility::isMobileTheme())
699                 {
700                         echo "\n".'
701   <div class="menuedit">
702     <a href="'.$_base_path.'home/editor/edit_content_folder.php?_course_id='.$_course_id.'">
703       <img id="img_create_top_folder" src="'.$_base_path.'images/mfolder.gif" alt="'._AT("add_top_folder").'" title="'._AT("add_top_folder").'" style="border:0;height:1.2em" />
704     </a>'."\n".'
705     <a href="'.$_base_path.'home/editor/edit_content.php?_course_id='.$_course_id.'">
706       <img id="img_create_top_content" src="'.$_base_path.'images/mpage.gif" alt="'._AT("add_top_page").'" title="'._AT("add_top_page").'" style="border:0;height:1.2em" />
707     </a>'."\n".'
708     <a href="javascript:void(0)" onclick="javascript:switchEditMode();">
709       <img id="img_switch_edit_mode" src="'.$_base_path.'images/medit.gif" alt="'._AT("enter_edit_mode").'" title="'._AT("enter_edit_mode").'" style="border:0;height:1.2em" />
710     </a>
711   </div>'."\n";
712                 }
713                 $this->printMenu($parent_id, $depth, $path, $children, $truncate, $ignore_state);
714                 
715                 // javascript for inline editor
716                 echo '
717 <script type="text/javascript">
718 ';
719                 // only expand the content folder that has the current content page
720                 $this->initMenu();
721                 
722                 echo '
723 function switchEditMode() {
724   title_edit = "'._AT("enter_edit_mode").'";
725   img_edit = "'.$_base_path.'images/medit.gif";
726         
727   title_view = "'._AT("exit_edit_mode").'";
728   img_view = "'.$_base_path.'images/mlock.gif";
729         
730   if (jQuery("#img_switch_edit_mode").attr("src") == img_edit)
731   {
732     jQuery("#img_switch_edit_mode").attr("src", img_view);
733     jQuery("#img_switch_edit_mode").attr("alt", title_view);
734     jQuery("#img_switch_edit_mode").attr("title", title_view);
735     inlineEditsSetup();
736   }
737   else
738   { // refresh the content navigation to exit the edit mode
739     jQuery.post("'. TR_BASE_HREF. 'home/course/refresh_content_nav.php?_course_id='.$_course_id.'", {}, 
740                 function(data) {jQuery("#editable_table").replaceWith(data); initContentMenu();});
741   }
742 }
743
744 function inlineEditsSetup() {
745   jQuery("#editable_table").find(".inlineEdits").each(function() {
746     jQuery(this).text(jQuery(this).attr("title"));
747   });
748         
749   var tableEdit = fluid.inlineEdits("#editable_table", {
750     selectors : {
751       text : ".inlineEdits",
752       editables : "span:has(span.inlineEdits)"
753     },
754     defaultViewText: "",
755       applyEditPadding: false,
756       useTooltip: true,
757       listeners: {
758         afterFinishEdit : function (newValue, oldValue, editNode, viewNode) {
759           if (newValue != oldValue) 
760           {
761             rtn = jQuery.post("'. TR_BASE_HREF. 'home/course/content_nav_inline_editor_submit.php", { "field":viewNode.id, "value":newValue }, 
762                   function(data) {}, "json");
763           }
764         }
765       }
766    });
767
768   jQuery(".fl-inlineEdit-edit").css("width", "80px")
769 };
770
771 initContentMenu();
772 </script>
773 ';
774                 echo '</div>';
775         }
776
777         /* @See tools/sitemap/index.php */
778         function printSiteMapMenu() {
779                 $parent_id    = 0;
780                 $depth        = 1;
781                 $path         = '';
782                 $children     = array();
783                 $truncate     = false;
784                 $ignore_state = true;
785
786                 $this->start = true;
787                 $this->printMenu($parent_id, $depth, $path, $children, $truncate, $ignore_state, 'sitemap');
788         }
789
790         /* @See index.php */
791         function printTOCMenu($cid, $top_num) {
792                 $parent_id    = $cid;
793                 $depth        = 1;
794                 $path         = $top_num.'.';
795                 $children     = array();
796                 $truncate     = false;
797                 $ignore_state = false;
798
799                 $this->start = true;
800                 $this->printMenu($parent_id, $depth, $path, $children, $truncate, $ignore_state);
801         }
802
803         /* @See index.php include/html/dropdowns/local_menu.inc.php */
804         function printSubMenu($cid, $top_num) {
805                 $parent_id    = $cid;
806                 $depth        = 1;
807                 $path         = $top_num.'.';
808                 $children     = array();
809                 $truncate     = true;
810                 $ignore_state = false;
811         
812                 $this->start = true;
813                 $this->printMenu($parent_id, $depth, $path, $children, $truncate, $ignore_state);
814         }
815
816         /* @See include/html/menu_menu.inc.php  */
817         /* Access: PRIVATE */
818         function printMenu($parent_id, $depth, $path, $children, $truncate, $ignore_state, $from = '') {
819                 global $cid, $_my_uri, $_base_path, $rtl, $substr, $strlen, $_current_user;
820                 static $temp_path;
821
822                 if (!isset($temp_path)) {
823                         if ($cid) {
824                                 $temp_path      = $this->getContentPath($cid);
825                         } else {
826                                 $temp_path      = $this->getContentPath($_SESSION['s_cid']);
827                         }
828                 }
829
830                 $highlighted = array();
831                 if (is_array($temp_path)) {
832                         foreach ($temp_path as $temp_path_item) {
833                                 $_SESSION['menu'][$temp_path_item['content_id']] = 1;
834                                 $highlighted[$temp_path_item['content_id']] = true;
835                         }
836                 }
837
838                 if ($this->start) {
839                         reset($temp_path);
840                         $this->start = false;
841                 }
842
843                 if ( isset($this->_menu[$parent_id]) && is_array($this->_menu[$parent_id]) ) {
844                         $top_level = $this->_menu[$parent_id];
845                         $counter = 1;
846                         $num_items = count($top_level);
847                         
848 //                      if ($parent_id <> 0) echo '<li>';
849                         
850 //                      echo '<ul id="folder'.$parent_id.$from.'">'."\n";
851                         echo '<div id="folder'.$parent_id.$from.'">'."\n";
852                         
853                         foreach ($top_level as $garbage => $content) {
854                                 $link = '';
855                                 //tests do not have content id
856                                 $content['content_id'] = isset($content['content_id']) ? $content['content_id'] : '';
857
858                                 if (!$ignore_state) {
859                                         $link .= '<a name="menu'.$content['content_id'].'"></a>';
860                                 }
861
862                                 $on = false;
863
864                                 if ( (($_SESSION['s_cid'] != $content['content_id']) || ($_SESSION['s_cid'] != $cid)) && ($content['content_type'] == CONTENT_TYPE_CONTENT || $content['content_type'] == CONTENT_TYPE_WEBLINK)) 
865                                 { // non-current content nodes with content type "CONTENT_TYPE_CONTENT"
866                                         if (isset($highlighted[$content['content_id']])) {
867                                                 $link .= '<strong>';
868                                                 $on = true;
869                                         }
870
871                                         //content test extension  @harris
872                                         //if this is a test link.
873                                         if (isset($content['test_id'])){
874                                                 $title_n_alt =  $content['title'];
875                                                 $in_link = $_base_path.'tests/preview.php?tid='.$content['test_id'].'&_cid='.$parent_id;
876                                                 $img_link = ' <img src="'.$_base_path.'images/check.gif" title="'.$title_n_alt.'" alt="'.$title_n_alt.'" />';
877                                         } else {
878                                                 $in_link = $_base_path.'home/course/content.php?_cid='.$content['content_id'];
879                                                 $img_link = '';
880                                         }
881                                         
882                                         $full_title = $content['title'];
883 //                                      $link .= $img_link . ' <a href="'.$_base_path.url_rewrite($in_link).'" title="';
884                                         $link .= $img_link . ' <a href="'.$in_link.'" title="';
885                                         $base_title_length = 29;
886                                         if ($_SESSION['prefs']['PREF_NUMBERING']) {
887 //                                              $link .= $path.$counter.' ';
888                                                 $base_title_length = 24;
889                                         }
890
891                                         $link .= $content['title'].'">';
892
893                                         if ($truncate && ($strlen($content['title']) > ($base_title_length-$depth*4)) ) {
894                                                 $content['title'] = htmlspecialchars(rtrim($substr(htmlspecialchars_decode($content['title']), 0, ($base_title_length-$depth*4)-4))).'...';
895                                         }
896 //                                      $content['title'] = htmlspecialchars(rtrim($substr(htmlspecialchars_decode($content['title']), 0, $base_title_length-4))).'...';
897                                         
898                                         if (isset($content['test_id']))
899                                                 $link .= $content['title'];
900                                         else
901                                                 $link .= '<span class="inlineEdits" id="menu-'.$content['content_id'].'" title="'.$full_title.'">'.
902                                                          ($_SESSION['prefs']['PREF_NUMBERING'] ? $path.$counter.'&nbsp;' : '').
903                                                          $content['title'].'</span>';
904                                         
905                                         $link .= '</a>';
906                                         if ($on) {
907                                                 $link .= '</strong>';
908                                         }
909                                         
910                                         // instructors have privilege to delete content
911                                         if (isset($_current_user) && $_current_user->isAuthor($this->course_id) && !isset($content['test_id']) && !Utility::isMobileTheme()) {
912                                                 $link .= '<a href="'.$_base_path.'home/editor/delete_content.php?_cid='.$content['content_id'].'"><img src="'.TR_BASE_HREF.'images/x.gif" alt="'._AT("delete_content").'" title="'._AT("delete_content").'" style="border:0" height="10" /></a>';
913                                         }
914                                 } 
915                                 else 
916                                 { // current content page & nodes with content type "CONTENT_TYPE_FOLDER"
917                                         $base_title_length = 26;
918                                         if ($_SESSION['prefs']['PREF_NUMBERING']) {
919                                                 $base_title_length = 21;
920                                         }
921                                         
922                                         if (isset($highlighted[$content['content_id']])) {
923                                                 $link .= '<strong>';
924                                                 $on = true;
925                                         }
926
927                                         if ($content['content_type'] == CONTENT_TYPE_CONTENT || $content['content_type'] == CONTENT_TYPE_WEBLINK)
928                                         { // current content page
929                                                 $full_title = $content['title'];
930                                                 $link .= '<a href="'.$_my_uri.'"><img src="'.$_base_path.'images/clr.gif" alt="'._AT('you_are_here').': '.
931                                                          ($_SESSION['prefs']['PREF_NUMBERING'] ? $path.$counter : '').
932                                                          $content['title'].'" height="1" width="1" border="0" /></a><strong style="color:red" title="'.$content['title'].'">'."\n";
933                                                 
934                                                 if ($truncate && ($strlen($content['title']) > ($base_title_length-$depth*4)) ) {
935                                                         $content['title'] = htmlspecialchars(rtrim($substr(htmlspecialchars_decode($content['title']), 0, ($base_title_length-$depth*4)-4))).'...';
936                                                 }
937 //                                              $content['title'] = htmlspecialchars(rtrim($substr(htmlspecialchars_decode($content['title']), 0, $base_title_length-4))).'...';
938                                                 $link .= '<a name="menu'.$content['content_id'].'"></a><span class="inlineEdits" id="menu-'.$content['content_id'].'" title="'.$full_title.'">'.
939                                                          ($_SESSION['prefs']['PREF_NUMBERING'] ? $path.$counter.'&nbsp;' : '').
940                                                          $content['title'].'</span></strong>';
941                                                 
942                                                 // instructors have privilege to delete content
943                                                 if (isset($_current_user) && $_current_user->isAuthor($this->course_id) && !Utility::isMobileTheme()) {
944                                                         $link .= '<a href="'.$_base_path.'home/editor/delete_content.php?_cid='.$content['content_id'].'"><img src="'.TR_BASE_HREF.'images/x.gif" alt="'._AT("delete_content").'" title="'._AT("delete_content").'" style="border:0" height="10" /></a>';
945                                                 }
946                                         }
947                                         else
948                                         { // nodes with content type "CONTENT_TYPE_FOLDER"
949 //                                              $link .= '<a href="javascript:void(0)" onclick="javascript: trans.utility.toggleFolder(\''.$content['content_id'].$from.'\'); "><img src="'.$_base_path.'images/clr.gif" alt="'._AT('content_folder').': '.$content['title'].'" height="1" width="1" border="0" /></a>'."\n";
950                                                 
951                                                 $full_title = $content['title'];
952                                                 if (isset($_current_user) && $_current_user->isAuthor($this->course_id) && !Utility::isMobileTheme()) {
953 //                                                      $link .= '<a href="'.$_base_path.url_rewrite("editor/edit_content_folder.php?_cid=".$content['content_id']).'" title="'.$full_title. _AT('click_edit').'">'."\n";
954                                                         $link .= '<a href="'.$_base_path.'home/editor/edit_content_folder.php?_cid='.$content['content_id'].'" title="'.$full_title. _AT('click_edit').'">'."\n";
955                                                 }
956                                                 else {
957                                                         $link .= '<span style="cursor:pointer" onclick="javascript: trans.utility.toggleFolder(\''.$content['content_id'].$from.'\', \''._AT("expand").'\', \''._AT('collapse').'\', '.$this->course_id.'); ">'."\n";
958                                                 }
959                                                 
960                                                 if ($truncate && ($strlen($content['title']) > ($base_title_length-$depth*4)) ) {
961                                                         $content['title'] = htmlspecialchars(rtrim($substr(htmlspecialchars_decode($content['title']), 0, ($base_title_length-$depth*4)-4))).'...';
962                                                 }
963 //                                              $content['title'] = htmlspecialchars(rtrim($substr(htmlspecialchars_decode($content['title']), 0, $base_title_length-4))).'...';
964                                                 if (isset($content['test_id']))
965                                                         $link .= $content['title'];
966                                                 else
967                                                         $link .= '<span class="inlineEdits" id="menu-'.$content['content_id'].'" title="'.$full_title.'">'.
968                                                                  ($_SESSION['prefs']['PREF_NUMBERING'] ? $path.$counter.'&nbsp;' : '').
969                                                                  $content['title'].'</span>';
970                                                 
971                                                 if (isset($_current_user) && $_current_user->isAuthor($this->course_id) && !Utility::isMobileTheme()) {
972                                                         $link .= '</a>'."\n";
973                                                 }
974                                                 else {
975                                                         $link .= '</span>'."\n";
976                                                 }
977                                                 
978                                                 // instructors have privilege to delete content
979                                                 if (isset($_current_user) && $_current_user->isAuthor($this->course_id) && !Utility::isMobileTheme()) {
980                                                         $link .= '<a href="'.$_base_path.'home/editor/delete_content.php?_cid='.$content['content_id'].'"><img src="'.TR_BASE_HREF.'images/x.gif" alt="'._AT("delete_content").'" title="'._AT("delete_content").'" style="border:0" height="10" /></a>';
981                                                 }
982 //                                              echo '<div id="folder_content_'.$content['content_id'].'">';
983                                         }
984                                         
985                                         if ($on) {
986                                                 $link .= '</strong>';
987                                         }
988                                 }
989
990                                 if ($ignore_state) {
991                                         $on = true;
992                                 }
993
994 //                              echo '<li>'."\n";
995                                 echo '<span>'."\n";
996                                 
997                                 if ( isset($this->_menu[$content['content_id']]) && is_array($this->_menu[$content['content_id']]) ) {
998                                         /* has children */
999                                         for ($i=0; $i<$depth; $i++) {
1000                                                 if ($children[$i] == 1) {
1001                                                         echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_vertline.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />'."\n";
1002                                                 } else {
1003                                                         echo '<img src="'.$_base_path.'images/clr.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />'."\n";
1004                                                 }
1005                                         }
1006
1007                                         if (($counter == $num_items) && ($depth > 0)) {
1008                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_end.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />'."\n";
1009                                                 $children[$depth] = 0;
1010                                         } else if ($counter == $num_items) {
1011                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_end.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />'."\n";
1012                                                 $children[$depth] = 0;
1013                                         } else {
1014                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_split.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />'."\n";
1015                                                 $children[$depth] = 1;
1016                                         }
1017
1018                                         if ($_SESSION['s_cid'] == $content['content_id']) {
1019                                                 if (is_array($this->_menu[$content['content_id']])) {
1020                                                         $_SESSION['menu'][$content['content_id']] = 1;
1021                                                 }
1022                                         }
1023
1024                                         if (isset($_SESSION['menu'][$content['content_id']]) && $_SESSION['menu'][$content['content_id']] == 1) {
1025                                                 if ($on) {
1026 //                                                      echo '<img src="'.$_base_path.'images/tree/tree_collapse.gif" id="tree_icon'.$content['content_id'].$from.'" alt="'._AT('collapse').'" border="0" width="16" height="16" title="'._AT('collapse').'" class="img-size-tree" onclick="javascript: trans.utility.toggleFolder(\''.$content['content_id'].$from.'\'); " />'."\n";
1027                                                         echo '<a href="javascript:void(0)" onclick="javascript: trans.utility.toggleFolder(\''.$content['content_id'].$from.'\', \''._AT("expand").'\', \''._AT('collapse').'\', '.$this->course_id.'); "><img src="'.$_base_path.'images/tree/tree_collapse.gif" id="tree_icon'.$content['content_id'].$from.'" alt="'._AT('collapse').'" border="0" width="16" height="16" title="'._AT('collapse').'" class="img-size-tree" /></a>'."\n";
1028                                                         
1029                                                 } else {
1030                                                         echo '<a href="'.$_my_uri.'collapse='.$content['content_id'].'">'."\n";
1031                                                         echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_collapse.gif" id="tree_icon'.$content['content_id'].$from.'" alt="'._AT('collapse').'" border="0" width="16" height="16" title="'._AT('collapse').' '.$content['title'].'" class="img-size-tree" onclick="javascript: trans.utility.toggleFolder(\''.$content['content_id'].$from.'\', \''._AT("expand").'\', \''._AT('collapse').'\', '.$this->course_id.'); " />'."\n";
1032                                                         echo '</a>'."\n";
1033                                                 }
1034                                         } else {
1035                                                 if ($on) {
1036 //                                                      echo '<img src="'.$_base_path.'images/tree/tree_collapse.gif" id="tree_icon'.$content['content_id'].$from.'" alt="'._AT('collapse').'" border="0" width="16" height="16" title="'._AT('collapse').'" class="img-size-tree" />'."\n";
1037                                                         echo '<a href="javascript:void(0)" onclick="javascript: trans.utility.toggleFolder(\''.$content['content_id'].$from.'\', \''._AT("expand").'\', \''._AT('collapse').'\', '.$this->course_id.'); "><img src="'.$_base_path.'images/tree/tree_collapse.gif" id="tree_icon'.$content['content_id'].$from.'" alt="'._AT('collapse').'" border="0" width="16" height="16" title="'._AT('collapse').'" class="img-size-tree" /></a>'."\n";
1038                                                         
1039                                                 } else {
1040                                                         echo '<a href="'.$_my_uri.'expand='.$content['content_id'].'">'."\n";
1041                                                         echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_expand.gif" id="tree_icon'.$content['content_id'].$from.'" alt="'._AT('expand').'" border="0" width="16" height="16"    title="'._AT('expand').' '.$content['title'].'" class="img-size-tree" onclick="javascript: trans.utility.toggleFolder(\''.$content['content_id'].$from.'\', \''._AT("expand").'\', \''._AT('collapse').'\', '.$this->course_id.'); " />';
1042                                                         echo '</a>'."\n";
1043                                                 }
1044                                         }
1045
1046                                 } else {
1047                                         /* doesn't have children */
1048                                         if ($counter == $num_items) {
1049                                                 for ($i=0; $i<$depth; $i++) {
1050                                                         if ($children[$i] == 1) {
1051                                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_vertline.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />'."\n";
1052                                                         } else {
1053                                                                 echo '<img src="'.$_base_path.'images/clr.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />'."\n";
1054                                                         }
1055                                                 }
1056                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_end.gif" alt="" border="0" class="img-size-tree" />'."\n";
1057                                         } else {
1058                                                 for ($i=0; $i<$depth; $i++) {
1059                                                         if ($children[$i] == 1) {
1060                                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_vertline.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />'."\n";
1061                                                         } else {
1062                                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_space.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />'."\n";
1063                                                         }
1064                                                 }
1065                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_split.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />'."\n";
1066                                         }
1067                                         echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_horizontal.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />'."\n";
1068                                 }
1069
1070 //                              if ($_SESSION['prefs']['PREF_NUMBERING']) {
1071 //                                      echo $path.$counter;
1072 //                              }
1073                                 
1074                                 echo $link;
1075                                 
1076                                 echo "\n<br /></span>\n\n";
1077                                 
1078                                 if ( $ignore_state || (isset($_SESSION['menu'][$content['content_id']]) && $_SESSION['menu'][$content['content_id']] == 1)) {
1079
1080                                         $depth ++;
1081
1082                                         $this->printMenu($content['content_id'],
1083                                                                                 $depth, 
1084                                                                                 $path.$counter.'.', 
1085                                                                                 $children,
1086                                                                                 $truncate, 
1087                                                                                 $ignore_state,
1088                                                                                 $from);
1089
1090                                                                                 
1091                                         $depth--;
1092
1093                                 }
1094                                 $counter++;
1095                         } // end of foreach
1096 //                      echo "</ul>";
1097 //                      if ($parent_id <> 0) print "</li>\n\n";
1098                         print "</div>\n\n";
1099                 }
1100         }
1101
1102         /* @See include/html/editor_tabs/properties.inc.php
1103            @See editor/arrange_content.php
1104             $print_type: "movable" or "related_content"
1105          */
1106         function printActionMenu($menu, $parent_id, $depth, $path, $children, $print_type = 'movable') {
1107                 
1108                 global $cid, $_my_uri, $_base_path, $rtl;
1109
1110                 static $end;
1111
1112                 $top_level = $menu[$parent_id];
1113
1114                 if ( is_array($top_level) ) {
1115                         $counter = 1;
1116                         $num_items = count($top_level);
1117                         foreach ($top_level as $current_num => $content) {
1118                                 if (isset($content['test_id'])){
1119                                         continue;
1120                                 }
1121
1122                                 $link = $buttons = '';
1123
1124                                 echo '<tr>'."\n";
1125                                 
1126                                 if ($print_type == 'movable')
1127                                 {
1128                                         if ($content['content_id'] == $_POST['moved_cid']) {
1129                                                 $radio_selected = ' checked="checked" ';
1130                                         }
1131                                         else {
1132                                                 $radio_selected = '';
1133                                         }
1134                                 
1135                                         $buttons = '<td>'."\n".
1136                                                    '   <small>'."\n".
1137                                                    '      <input type="image" name="move['.$parent_id.'_'.$content['ordering'].']" src="'.$_base_path.'images/before.gif" alt="'._AT('before_topic', $content['title']).'" title="'._AT('before_topic', $content['title']).'" style="height:1.5em; width:1.9em;" />'."\n";
1138
1139                                         if ($current_num + 1 == count($top_level))
1140                                                 $buttons .= '      <input type="image" name="move['.$parent_id.'_'.($content['ordering']+1).']" src="'.$_base_path.'images/after.gif" alt="'._AT('after_topic', $content['title']).'" title="'._AT('after_topic', $content['title']).'" style="height:1.5em; width:1.9em;" />'."\n";
1141                                         
1142                                         $buttons .= '   </small>'."\n".
1143                                                    '</td>'."\n".
1144                                                    '<td>';
1145                                         
1146                                         if ($content['content_type'] == CONTENT_TYPE_FOLDER)
1147                                                 $buttons .= '<input type="image" name="move['.$content['content_id'].'_1]" src="'.$_base_path.'images/child_of.gif" style="height:1.25em; width:1.7em;" alt="'._AT('child_of', $content['title']).'" title="'._AT('child_of', $content['title']).'" />';
1148                                         else
1149                                                 $buttons .= '&nbsp;';
1150                                                 
1151                                         $buttons .= '</td>'."\n".
1152                                                    '<td><input name="moved_cid" value="'.$content['content_id'].'" type="radio" id="r'.$content['content_id'].'" '.$radio_selected .'/></td>'."\n";
1153                                 }
1154                                 
1155                                 $buttons .= '<td>'."\n";
1156                                 if ($print_type == "related_content")
1157                                 {
1158                                         if ($content['content_type'] == CONTENT_TYPE_CONTENT || $content['content_type'] == CONTENT_TYPE_WEBLINK)
1159                                         {
1160                                                 $link .= '<input type="checkbox" name="related[]" value="'.$content['content_id'].'" id="r'.$content['content_id'].'" ';
1161                                                 if (isset($_POST['related']) && in_array($content['content_id'], $_POST['related'])) {
1162                                                         $link .= ' checked="checked"';
1163                                                 }
1164                                                 $link .= ' />'."\n";
1165                                         }
1166                                 }       
1167                                 
1168                                 if ($content['content_type'] == CONTENT_TYPE_FOLDER)
1169                                 {
1170                                         $link .= '<img src="'.$_base_path.'images/folder.gif" />';
1171                                 }
1172                                 $link .= '&nbsp;<label for="r'.$content['content_id'].'">'.$content['title'].'</label>'."\n";
1173
1174                                 if ( is_array($menu[$content['content_id']]) && !empty($menu[$content['content_id']]) ) {
1175                                         /* has children */
1176
1177                                         for ($i=0; $i<$depth; $i++) {
1178                                                 if ($children[$i] == 1) {
1179                                                         echo $buttons;
1180                                                         unset($buttons);
1181                                                         if ($end && ($i==0)) {
1182                                                                 echo '<img src="'.$_base_path.'images/clr.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />';
1183                                                         } else {
1184                                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_vertline.gif" alt="" border="0" width="16" height="16" />';
1185                                                         }
1186                                                 } else {
1187                                                         echo '<img src="'.$_base_path.'images/clr.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />';
1188                                                 }
1189                                         }
1190
1191                                         if (($counter == $num_items) && ($depth > 0)) {
1192                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_end.gif" alt="" border="0" width="16" height="16" />';
1193                                                 $children[$depth] = 0;
1194                                         } else {
1195                                                 echo $buttons;
1196                                                 if (($num_items == $counter) && ($parent_id == 0)) {
1197                                                         echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_end.gif" alt="" border="0" width="16" height="16" />';
1198                                                         $end = true;
1199                                                 } else {
1200                                                         echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_split.gif" alt="" border="0" width="16" height="16" />';
1201                                                 }
1202                                                 $children[$depth] = 1;
1203                                         }
1204
1205                                         if ($_SESSION['s_cid'] == $content['content_id']) {
1206                                                 if (is_array($menu[$content['content_id']])) {
1207                                                         $_SESSION['menu'][$content['content_id']] = 1;
1208                                                 }
1209                                         }
1210
1211                                         if ($_SESSION['menu'][$content['content_id']] == 1) {
1212                                                 echo '<img src="'.$_base_path.'images/tree/tree_disabled.gif" alt="'._AT('toggle_disabled').'" border="0" width="16" height="16" title="'._AT('toggle_disabled').'" />';
1213
1214                                         } else {
1215                                                 echo '<img src="'.$_base_path.'images/tree/tree_disabled.gif" alt="'._AT('toggle_disabled').'" border="0" width="16" height="16" title="'._AT('toggle_disabled').'" />';
1216                                         }
1217
1218                                 } else {
1219                                         /* doesn't have children */
1220                                         if ($counter == $num_items) {
1221                                                 if ($depth) {
1222                                                         echo $buttons;
1223                                                         for ($i=0; $i<$depth; $i++) {
1224                                                                 if ($children[$i] == 1) {
1225                                                                         if ($end && ($i == 0)) {
1226                                                                                 echo '<img src="'.$_base_path.'images/clr.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />';
1227                                                                         } else {
1228                                                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_vertline.gif" alt="" border="0" width="16" height="16" />';
1229                                                                         }
1230                                                                 } else {
1231                                                                         echo '<img src="'.$_base_path.'images/clr.gif" alt="" border="0" width="16" height="16" class="img-size-tree" />';
1232                                                                 }
1233                                                         }
1234                                                 } else {
1235                                                         echo $buttons;
1236                                                 }
1237                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_end.gif" alt="" border="0" />';
1238                                         } else {
1239                                                 if ($depth) {
1240                                                         echo $buttons;
1241                                                         $print = false;
1242                                                         for ($i=0; $i<$depth; $i++) {
1243                                                                 if ($children[$i] == 1) {
1244                                                                         if ($end && !$print) {
1245                                                                                 $print = true;
1246                                                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_space.gif" alt="" border="0" width="16" height="16" />';
1247                                                                         } else {
1248                                                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_vertline.gif" alt="" border="0" width="16" height="16" />';
1249                                                                         }
1250                                                                 } else {
1251                                                                         echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_space.gif" alt="" border="0" width="16" height="16" />';
1252                                                                 }
1253                                                         }
1254                                                         $print = false;
1255                                                 } else {
1256                                                         echo $buttons;
1257                                                 }
1258                 
1259                                                 echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_split.gif" alt="" border="0" width="16" height="16" />';
1260                                         }
1261                                         echo '<img src="'.$_base_path.'images/'.$rtl.'tree/tree_horizontal.gif" alt="" border="0" width="16" height="16" />';
1262                                 }
1263
1264                                 echo '<small> '.($_SESSION['prefs']['PREF_NUMBERING'] ? $path.$counter : '');
1265                                 
1266                                 echo $link;
1267                                 
1268                                 echo '</small></td>'."\n".'</tr>'."\n";
1269
1270                                 $this->printActionMenu($menu,
1271                                                                         $content['content_id'],
1272                                                                         ++$depth, 
1273                                                                         $path.$counter.'.', 
1274                                                                         $children,
1275                                                                         $print_type);
1276                                 $depth--;
1277
1278                                 $counter++;
1279                         }
1280                 }
1281         }
1282 }
1283
1284 ?>