PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/blocks/cmsnavigation/block_cmsnavigation.php

https://github.com/nadavkav/MoodleTAO
PHP | 263 lines | 183 code | 52 blank | 28 comment | 48 complexity | 1c164a29d286e5f2c0a0225ff2aeea32 MD5 | raw file
  1. <?php // $Id: block_cmsnavigation.php,v 1.7.10.1 2008/03/23 09:36:05 julmis Exp $
  2. class block_cmsnavigation extends block_base {
  3. function init() {
  4. $this->title = get_string('cmsnavigation','cms');
  5. $this->content_type = BLOCK_TYPE_TEXT;
  6. $this->version = 2005020301;
  7. $this->navidata = NULL;
  8. }
  9. function specialization() {
  10. // We allow empty titles
  11. $this->title = isset($this->config->title) ? $this->config->title : '';
  12. }
  13. function instance_allow_multiple() {
  14. return true;
  15. }
  16. function is_login_required ( $menuid ) {
  17. return get_field("cmsnavi", "requirelogin", "id", $menuid);
  18. }
  19. function is_guest_allowed ( $menuid ) {
  20. return get_field("cmsnavi", "allowguest", "id", $menuid);
  21. }
  22. function preferred_width() {
  23. // The preferred value is in pixels
  24. return 240;
  25. }
  26. function hide_header() {
  27. return empty($this->title);
  28. }
  29. function get_css_classname (&$path, &$navi) {
  30. global $CFG;
  31. $frontpagelayout = (isloggedin() && !empty($CFG->frontpageloggedin))
  32. ? $CFG->frontpageloggedin
  33. : $CFG->frontpage;
  34. $frontpagecheck = false;
  35. if ( $frontpagelayout == FRONTPAGECMS ) {
  36. if ( end($path) == 0 && $navi->isfp != 0 && empty($GLOBALS['pagename']) ) {
  37. $frontpagecheck = true;
  38. }
  39. }
  40. if ( ($navi->pageid == end($path)) or $frontpagecheck ) {
  41. $class = 'class="cms-active-1';
  42. } else if ( in_array($navi->pageid, $path) ) {
  43. $class = 'class="cms-active-2';
  44. } else {
  45. $class = 'class="cms-inactive';
  46. }
  47. return $class;
  48. }
  49. function construct_tree_menu ($parentid, $path, $menuid, $level=1) {
  50. global $CFG, $USER, $SITE;
  51. static $top;
  52. if ( empty($top) ) {
  53. $top = array();
  54. }
  55. if (! empty($this->navidata) ) {
  56. $this->content->text .= '<ul class="cms-navi-list">' . "\n";
  57. foreach($this->navidata as $key => $navi) {
  58. if ( $navi->parentid == $parentid ) {
  59. array_push($top, $navi->pageid);
  60. $class = $this->get_css_classname($path, $navi);
  61. $class .= ' level'.$level.'on"';
  62. if ( empty($navi->url) ) {
  63. // If the admin has hacked the core Moodle code then he will want the links
  64. // to point to the site index.php page for site pages
  65. if (empty($navi->pagename)) {
  66. $baseurl = $CFG->wwwroot .'/?pid='. (empty($navi->parentid)? '' : $navi->parentid . ',') . $navi->pageid;
  67. } else {
  68. $tempurl = (!$CFG->slasharguments)
  69. ? $CFG->wwwroot.'/index.php?page='.$navi->pagename
  70. : $CFG->wwwroot.'/index.php/'. $navi->pagename;
  71. $baseurl = ($navi->course == SITEID)
  72. ? ( $tempurl )
  73. // TODO: should the URL below be rendered with slasharguments?
  74. : $CFG->wwwroot.'/course/view.php?id='. $navi->course .'&amp;page='.$navi->pagename;
  75. }
  76. } else {
  77. $baseurl = $navi->url;
  78. }
  79. $target = (!empty($navi->url) && !empty($navi->target)) ? ' target="'. $navi->target .'"' : '';
  80. $this->content->text .= '<li '. $class .'><a '.
  81. ' href="'. $baseurl . '"'. $target .
  82. '>' . stripslashes($navi->title) .
  83. '</a>' . "\n";
  84. if ( in_array($navi->pageid, $path) or (empty($path) && $navi->isfp) ) {
  85. $this->construct_tree_menu($navi->pageid, $path, $menuid, $level+1);
  86. }
  87. $this->content->text .= '</li>' . "\n";
  88. array_pop($top);
  89. }
  90. }
  91. $this->content->text .= '</ul>' . "\n";
  92. }
  93. }
  94. /**
  95. * Check if page have parent id.
  96. *
  97. * As navidata array already exists we can use it as searcable
  98. * index because of its structure ( array of objects where pageid
  99. * is also an array key ).
  100. * @param int $pageid
  101. * @param bool $returnid
  102. * @return mixed
  103. */
  104. function has_parent ( $pageid, $returnid=FALSE) {
  105. $pageid = intval($pageid);
  106. if ( !empty($this->navidata[$pageid]) ) {
  107. $page = $this->navidata[$pageid];
  108. if ( $page->parentid != 0 ) {
  109. if ( !$returnid ) {
  110. return true;
  111. } else {
  112. // return first item.
  113. return (int) $page->parentid;
  114. }
  115. }
  116. }
  117. return false;
  118. }
  119. /**
  120. * Create a id path from requested page all away to root level.
  121. *
  122. * @param int $pageid;
  123. * @return array
  124. */
  125. function get_path($pageid) {
  126. $pagearray = array();
  127. array_push($pagearray, $pageid);
  128. while ( $pageid = $this->has_parent($pageid, true) ) {
  129. $pagearray[$pageid] = $pageid;
  130. }
  131. return array_reverse($pagearray);
  132. }
  133. function get_content() {
  134. global $CFG, $USER, $SITE, $COURSE;
  135. if ($this->content !== NULL) {
  136. return $this->content;
  137. }
  138. $pagename = optional_param('page', '', PARAM_FILE);
  139. $pageid = optional_param('pid', 0, PARAM_INT);
  140. if ( defined('SITEID') && SITEID == $this->instance->pageid && $CFG->slasharguments ) {
  141. // Support sitelevel slasharguments
  142. // in form /index.php/<pagename>
  143. $relativepath = get_file_argument(basename($_SERVER['SCRIPT_FILENAME']));
  144. if ( preg_match("/^(\/[a-z0-9\_\-]+)/i", $relativepath) ) {
  145. $args = explode("/", $relativepath);
  146. $pagename = clean_param($args[1], PARAM_FILE);
  147. }
  148. unset($args, $relativepath);
  149. }
  150. // set menuid according to block configuration or if no menu has been configured yet use
  151. // the first available menu if it exists
  152. if (!empty($this->config->menu)) {
  153. $menuid = intval($this->config->menu);
  154. } else if ($menus = get_records('cmsnavi', 'course', $this->instance->pageid, 'id ASC')) {
  155. $menu = array_pop($menus);
  156. $menuid = $menu->id;
  157. $this->config->menu = $menuid;
  158. $this->instance_config_commit();
  159. } else {
  160. $menuid = 0;
  161. }
  162. $this->content = new stdClass;
  163. $this->content->text = '';
  164. $this->content->footer = '';
  165. $menurequirelogin = $this->is_login_required($menuid);
  166. $menuallowguest = $this->is_guest_allowed($menuid);
  167. if (($menurequirelogin && empty($USER->loggedin)) or
  168. ($menurequirelogin && isguest() && !$menuallowguest)) {
  169. return $this->content;
  170. }
  171. $this->navidata = get_records_sql("SELECT n.pageid, n.parentid, n.title, n.isfp, n.pagename,
  172. n.url, n.target, p.publish, cn.requirelogin, cn.course
  173. FROM {$CFG->prefix}cmsnavi_data n,
  174. {$CFG->prefix}cmspages p,
  175. {$CFG->prefix}cmsnavi cn
  176. WHERE n.pageid = p.id AND p.publish = 1
  177. AND n.naviid = '$menuid'
  178. AND (cn.id = '$menuid')
  179. AND n.showinmenu = '1'
  180. ORDER BY sortorder");
  181. if ( empty($pageid) && !empty($pagename) ) {
  182. $pageid = get_field('cmsnavi_data', 'pageid', 'pagename', $pagename, 'naviid', $menuid);
  183. }
  184. $path = $this->get_path($pageid);
  185. // Wrap it inside div element which width you can control
  186. // with CSS styles in styles.php file.
  187. $this->content->text .= "\n" . '<div class="cms-navi">' . "\n";
  188. $this->construct_tree_menu(0, $path, $menuid);
  189. $this->content->text .= '</div>'."\n";
  190. if (!empty($USER->editing) and !empty($pageid)) {
  191. $toolbar = '';
  192. $stradd = get_string('add');
  193. $addlink = $CFG->wwwroot .'/cms/pageadd.php?id='. $pageid .'&amp;'.
  194. 'sesskey='. $USER->sesskey .'&amp;parentid=0&amp;course=' . $COURSE->id .'';
  195. $addicon = $CFG->wwwroot .'/cms/pix/add.gif';
  196. $toolbar .= '<a href="'. $addlink .'" target="reorder"><img src="'. $addicon .'"'
  197. . ' width="11" height="11" alt="'. $stradd .'"'
  198. . ' title="'. $stradd .'" /></a>';
  199. $strreorder = get_string('reorder', 'cms');
  200. $reorderlink = $CFG->wwwroot .'/cms/reorder.php?source='. $pageid .
  201. '&amp;sesskey='. $USER->sesskey;
  202. $reordericon = $CFG->wwwroot .'/pix/t/move.gif';
  203. $toolbar .= ' <a href="'. $reorderlink .'" target="reorder"><img src="'. $reordericon .'"'
  204. . ' width="11" height="11" alt="'. $strreorder .'"'
  205. . ' title="'. $strreorder .'" /></a>';
  206. $this->content->footer = $toolbar;
  207. }
  208. return $this->content;
  209. }
  210. }
  211. ?>