PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/blocks/edit_form.php

https://github.com/pauln/moodle
PHP | 294 lines | 171 code | 38 blank | 85 comment | 47 complexity | 844e2d2cc3099a8b3799e7648d86e3c6 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Defines the base class form used by blocks/edit.php to edit block instance configuration.
  18. *
  19. * It works with the {@link block_edit_form} class, or rather the particular
  20. * subclass defined by this block, to do the editing.
  21. *
  22. * @package core_block
  23. * @copyright 2009 Tim Hunt
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. if (!defined('MOODLE_INTERNAL')) {
  27. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  28. }
  29. require_once($CFG->libdir . '/formslib.php');
  30. require_once($CFG->libdir . '/blocklib.php');
  31. /**
  32. * The base class form used by blocks/edit.php to edit block instance configuration.
  33. *
  34. * @copyright 2009 Tim Hunt
  35. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36. */
  37. class block_edit_form extends moodleform {
  38. /**
  39. * The block instance we are editing.
  40. * @var block_base
  41. */
  42. public $block;
  43. /**
  44. * The page we are editing this block in association with.
  45. * @var moodle_page
  46. */
  47. public $page;
  48. function __construct($actionurl, $block, $page) {
  49. global $CFG;
  50. $this->block = $block;
  51. $this->page = $page;
  52. parent::__construct($actionurl);
  53. }
  54. function definition() {
  55. $mform =& $this->_form;
  56. // First show fields specific to this type of block.
  57. $this->specific_definition($mform);
  58. // Then show the fields about where this block appears.
  59. $mform->addElement('header', 'whereheader', get_string('wherethisblockappears', 'block'));
  60. // If the current weight of the block is out-of-range, add that option in.
  61. $blockweight = $this->block->instance->weight;
  62. $weightoptions = array();
  63. if ($blockweight < -block_manager::MAX_WEIGHT) {
  64. $weightoptions[$blockweight] = $blockweight;
  65. }
  66. for ($i = -block_manager::MAX_WEIGHT; $i <= block_manager::MAX_WEIGHT; $i++) {
  67. $weightoptions[$i] = $i;
  68. }
  69. if ($blockweight > block_manager::MAX_WEIGHT) {
  70. $weightoptions[$blockweight] = $blockweight;
  71. }
  72. $first = reset($weightoptions);
  73. $weightoptions[$first] = get_string('bracketfirst', 'block', $first);
  74. $last = end($weightoptions);
  75. $weightoptions[$last] = get_string('bracketlast', 'block', $last);
  76. $regionoptions = $this->page->theme->get_all_block_regions();
  77. foreach ($this->page->blocks->get_regions() as $region) {
  78. // Make sure to add all custom regions of this particular page too.
  79. if (!isset($regionoptions[$region])) {
  80. $regionoptions[$region] = $region;
  81. }
  82. }
  83. $parentcontext = context::instance_by_id($this->block->instance->parentcontextid);
  84. $mform->addElement('hidden', 'bui_parentcontextid', $parentcontext->id);
  85. $mform->setType('bui_parentcontextid', PARAM_INT);
  86. $mform->addElement('static', 'bui_homecontext', get_string('createdat', 'block'), $parentcontext->get_context_name());
  87. $mform->addHelpButton('bui_homecontext', 'createdat', 'block');
  88. // For pre-calculated (fixed) pagetype lists
  89. $pagetypelist = array();
  90. // parse pagetype patterns
  91. $bits = explode('-', $this->page->pagetype);
  92. // First of all, check if we are editing blocks @ front-page or no and
  93. // make some dark magic if so (MDL-30340) because each page context
  94. // implies one (and only one) harcoded page-type that will be set later
  95. // when processing the form data at {@link block_manager::process_url_edit()}
  96. // There are some conditions to check related to contexts
  97. $ctxconditions = $this->page->context->contextlevel == CONTEXT_COURSE &&
  98. $this->page->context->instanceid == get_site()->id;
  99. // And also some pagetype conditions
  100. $pageconditions = isset($bits[0]) && isset($bits[1]) && $bits[0] == 'site' && $bits[1] == 'index';
  101. // So now we can be 100% sure if edition is happening at frontpage
  102. $editingatfrontpage = $ctxconditions && $pageconditions;
  103. // Let the form to know about that, can be useful later
  104. $mform->addElement('hidden', 'bui_editingatfrontpage', (int)$editingatfrontpage);
  105. $mform->setType('bui_editingatfrontpage', PARAM_INT);
  106. // Front page, show the page-contexts element and set $pagetypelist to 'any page' (*)
  107. // as unique option. Processign the form will do any change if needed
  108. if ($editingatfrontpage) {
  109. $contextoptions = array();
  110. $contextoptions[BUI_CONTEXTS_FRONTPAGE_ONLY] = get_string('showonfrontpageonly', 'block');
  111. $contextoptions[BUI_CONTEXTS_FRONTPAGE_SUBS] = get_string('showonfrontpageandsubs', 'block');
  112. $contextoptions[BUI_CONTEXTS_ENTIRE_SITE] = get_string('showonentiresite', 'block');
  113. $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
  114. $mform->addHelpButton('bui_contexts', 'contexts', 'block');
  115. $pagetypelist['*'] = '*'; // This is not going to be shown ever, it's an unique option
  116. // Any other system context block, hide the page-contexts element,
  117. // it's always system-wide BUI_CONTEXTS_ENTIRE_SITE
  118. } else if ($parentcontext->contextlevel == CONTEXT_SYSTEM) {
  119. $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_ENTIRE_SITE);
  120. } else if ($parentcontext->contextlevel == CONTEXT_COURSE) {
  121. // 0 means display on current context only, not child contexts
  122. // but if course managers select mod-* as pagetype patterns, block system will overwrite this option
  123. // to 1 (display on current context and child contexts)
  124. $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT);
  125. } else if ($parentcontext->contextlevel == CONTEXT_MODULE or $parentcontext->contextlevel == CONTEXT_USER) {
  126. // module context doesn't have child contexts, so display in current context only
  127. $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_CURRENT);
  128. } else {
  129. $parentcontextname = $parentcontext->get_context_name();
  130. $contextoptions[BUI_CONTEXTS_CURRENT] = get_string('showoncontextonly', 'block', $parentcontextname);
  131. $contextoptions[BUI_CONTEXTS_CURRENT_SUBS] = get_string('showoncontextandsubs', 'block', $parentcontextname);
  132. $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
  133. }
  134. $mform->setType('bui_contexts', PARAM_INT);
  135. // Generate pagetype patterns by callbacks if necessary (has not been set specifically)
  136. if (empty($pagetypelist)) {
  137. $pagetypelist = generate_page_type_patterns($this->page->pagetype, $parentcontext, $this->page->context);
  138. $displaypagetypewarning = false;
  139. if (!array_key_exists($this->block->instance->pagetypepattern, $pagetypelist)) {
  140. // Pushing block's existing page type pattern
  141. $pagetypestringname = 'page-'.str_replace('*', 'x', $this->block->instance->pagetypepattern);
  142. if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) {
  143. $pagetypelist[$this->block->instance->pagetypepattern] = get_string($pagetypestringname, 'pagetype');
  144. } else {
  145. //as a last resort we could put the page type pattern in the select box
  146. //however this causes mod-data-view to be added if the only option available is mod-data-*
  147. // so we are just showing a warning to users about their prev setting being reset
  148. $displaypagetypewarning = true;
  149. }
  150. }
  151. }
  152. // hide page type pattern select box if there is only one choice
  153. if (count($pagetypelist) > 1) {
  154. if ($displaypagetypewarning) {
  155. $mform->addElement('static', 'pagetypewarning', '', get_string('pagetypewarning','block'));
  156. }
  157. $mform->addElement('select', 'bui_pagetypepattern', get_string('restrictpagetypes', 'block'), $pagetypelist);
  158. } else {
  159. $values = array_keys($pagetypelist);
  160. $value = array_pop($values);
  161. $mform->addElement('hidden', 'bui_pagetypepattern', $value);
  162. $mform->setType('bui_pagetypepattern', PARAM_RAW);
  163. // Now we are really hiding a lot (both page-contexts and page-type-patterns),
  164. // specially in some systemcontext pages having only one option (my/user...)
  165. // so, until it's decided if we are going to add the 'bring-back' pattern to
  166. // all those pages or no (see MDL-30574), we are going to show the unique
  167. // element statically
  168. // TODO: Revisit this once MDL-30574 has been decided and implemented, although
  169. // perhaps it's not bad to always show this statically when only one pattern is
  170. // available.
  171. if (!$editingatfrontpage) {
  172. // Try to beautify it
  173. $strvalue = $value;
  174. $strkey = 'page-'.str_replace('*', 'x', $strvalue);
  175. if (get_string_manager()->string_exists($strkey, 'pagetype')) {
  176. $strvalue = get_string($strkey, 'pagetype');
  177. }
  178. // Show as static (hidden has been set already)
  179. $mform->addElement('static', 'bui_staticpagetypepattern',
  180. get_string('restrictpagetypes','block'), $strvalue);
  181. }
  182. }
  183. if ($this->page->subpage) {
  184. if ($parentcontext->contextlevel == CONTEXT_USER) {
  185. $mform->addElement('hidden', 'bui_subpagepattern', '%@NULL@%');
  186. $mform->setType('bui_subpagepattern', PARAM_RAW);
  187. } else {
  188. $subpageoptions = array(
  189. '%@NULL@%' => get_string('anypagematchingtheabove', 'block'),
  190. $this->page->subpage => get_string('thisspecificpage', 'block', $this->page->subpage),
  191. );
  192. $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions);
  193. }
  194. }
  195. $defaultregionoptions = $regionoptions;
  196. $defaultregion = $this->block->instance->defaultregion;
  197. if (!array_key_exists($defaultregion, $defaultregionoptions)) {
  198. $defaultregionoptions[$defaultregion] = $defaultregion;
  199. }
  200. $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions);
  201. $mform->addHelpButton('bui_defaultregion', 'defaultregion', 'block');
  202. $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions);
  203. $mform->addHelpButton('bui_defaultweight', 'defaultweight', 'block');
  204. // Where this block is positioned on this page.
  205. $mform->addElement('header', 'onthispage', get_string('onthispage', 'block'));
  206. $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block'));
  207. $blockregion = $this->block->instance->region;
  208. if (!array_key_exists($blockregion, $regionoptions)) {
  209. $regionoptions[$blockregion] = $blockregion;
  210. }
  211. $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions);
  212. $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions);
  213. $pagefields = array('bui_visible', 'bui_region', 'bui_weight');
  214. if (!$this->block->user_can_edit()) {
  215. $mform->hardFreezeAllVisibleExcept($pagefields);
  216. }
  217. if (!$this->page->user_can_edit_blocks()) {
  218. $mform->hardFreeze($pagefields);
  219. }
  220. $this->add_action_buttons();
  221. }
  222. function set_data($defaults) {
  223. // Prefix bui_ on all the core field names.
  224. $blockfields = array('showinsubcontexts', 'pagetypepattern', 'subpagepattern', 'parentcontextid',
  225. 'defaultregion', 'defaultweight', 'visible', 'region', 'weight');
  226. foreach ($blockfields as $field) {
  227. $newname = 'bui_' . $field;
  228. $defaults->$newname = $defaults->$field;
  229. }
  230. // Copy block config into config_ fields.
  231. if (!empty($this->block->config)) {
  232. foreach ($this->block->config as $field => $value) {
  233. $configfield = 'config_' . $field;
  234. $defaults->$configfield = $value;
  235. }
  236. }
  237. // Munge ->subpagepattern becuase HTML selects don't play nicely with NULLs.
  238. if (empty($defaults->bui_subpagepattern)) {
  239. $defaults->bui_subpagepattern = '%@NULL@%';
  240. }
  241. $systemcontext = context_system::instance();
  242. if ($defaults->parentcontextid == $systemcontext->id) {
  243. $defaults->bui_contexts = BUI_CONTEXTS_ENTIRE_SITE; // System-wide and sticky
  244. } else {
  245. $defaults->bui_contexts = $defaults->bui_showinsubcontexts;
  246. }
  247. parent::set_data($defaults);
  248. }
  249. /**
  250. * Override this to create any form fields specific to this type of block.
  251. * @param object $mform the form being built.
  252. */
  253. protected function specific_definition($mform) {
  254. // By default, do nothing.
  255. }
  256. }