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

/blocks/edit_form.php

https://bitbucket.org/moodle/moodle
PHP | 313 lines | 175 code | 38 blank | 100 comment | 42 complexity | 656c51b9b9929b021b26f500f623917f MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. /**
  49. * Defaults set in set_data() that need to be returned in get_data() if form elements were not created
  50. * @var array
  51. */
  52. protected $defaults = [];
  53. function __construct($actionurl, $block, $page) {
  54. global $CFG;
  55. $this->block = $block;
  56. $this->page = $page;
  57. parent::__construct($actionurl);
  58. }
  59. function definition() {
  60. $mform =& $this->_form;
  61. // First show fields specific to this type of block.
  62. $this->specific_definition($mform);
  63. // Then show the fields about where this block appears.
  64. $mform->addElement('header', 'whereheader', get_string('wherethisblockappears', 'block'));
  65. // If the current weight of the block is out-of-range, add that option in.
  66. $blockweight = $this->block->instance->weight;
  67. $weightoptions = array();
  68. if ($blockweight < -block_manager::MAX_WEIGHT) {
  69. $weightoptions[$blockweight] = $blockweight;
  70. }
  71. for ($i = -block_manager::MAX_WEIGHT; $i <= block_manager::MAX_WEIGHT; $i++) {
  72. $weightoptions[$i] = $i;
  73. }
  74. if ($blockweight > block_manager::MAX_WEIGHT) {
  75. $weightoptions[$blockweight] = $blockweight;
  76. }
  77. $first = reset($weightoptions);
  78. $weightoptions[$first] = get_string('bracketfirst', 'block', $first);
  79. $last = end($weightoptions);
  80. $weightoptions[$last] = get_string('bracketlast', 'block', $last);
  81. $regionoptions = $this->page->theme->get_all_block_regions();
  82. foreach ($this->page->blocks->get_regions() as $region) {
  83. // Make sure to add all custom regions of this particular page too.
  84. if (!isset($regionoptions[$region])) {
  85. $regionoptions[$region] = $region;
  86. }
  87. }
  88. $parentcontext = context::instance_by_id($this->block->instance->parentcontextid);
  89. $mform->addElement('static', 'bui_homecontext', get_string('createdat', 'block'), $parentcontext->get_context_name());
  90. $mform->addHelpButton('bui_homecontext', 'createdat', 'block');
  91. // For pre-calculated (fixed) pagetype lists
  92. $pagetypelist = array();
  93. // parse pagetype patterns
  94. $bits = explode('-', $this->page->pagetype);
  95. // First of all, check if we are editing blocks @ front-page or no and
  96. // make some dark magic if so (MDL-30340) because each page context
  97. // implies one (and only one) harcoded page-type that will be set later
  98. // when processing the form data at {@link block_manager::process_url_edit()}
  99. // Front page, show the page-contexts element and set $pagetypelist to 'any page' (*)
  100. // as unique option. Processign the form will do any change if needed
  101. if ($this->is_editing_the_frontpage()) {
  102. $contextoptions = array();
  103. $contextoptions[BUI_CONTEXTS_FRONTPAGE_ONLY] = get_string('showonfrontpageonly', 'block');
  104. $contextoptions[BUI_CONTEXTS_FRONTPAGE_SUBS] = get_string('showonfrontpageandsubs', 'block');
  105. $contextoptions[BUI_CONTEXTS_ENTIRE_SITE] = get_string('showonentiresite', 'block');
  106. $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
  107. $mform->addHelpButton('bui_contexts', 'contexts', 'block');
  108. $pagetypelist['*'] = '*'; // This is not going to be shown ever, it's an unique option
  109. // Any other system context block, hide the page-contexts element,
  110. // it's always system-wide BUI_CONTEXTS_ENTIRE_SITE
  111. } else if ($parentcontext->contextlevel == CONTEXT_SYSTEM) {
  112. } else if ($parentcontext->contextlevel == CONTEXT_COURSE) {
  113. // 0 means display on current context only, not child contexts
  114. // but if course managers select mod-* as pagetype patterns, block system will overwrite this option
  115. // to 1 (display on current context and child contexts)
  116. } else if ($parentcontext->contextlevel == CONTEXT_MODULE or $parentcontext->contextlevel == CONTEXT_USER) {
  117. // module context doesn't have child contexts, so display in current context only
  118. } else {
  119. $parentcontextname = $parentcontext->get_context_name();
  120. $contextoptions[BUI_CONTEXTS_CURRENT] = get_string('showoncontextonly', 'block', $parentcontextname);
  121. $contextoptions[BUI_CONTEXTS_CURRENT_SUBS] = get_string('showoncontextandsubs', 'block', $parentcontextname);
  122. $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions);
  123. }
  124. $mform->setType('bui_contexts', PARAM_INT);
  125. // Generate pagetype patterns by callbacks if necessary (has not been set specifically)
  126. if (empty($pagetypelist)) {
  127. $pagetypelist = generate_page_type_patterns($this->page->pagetype, $parentcontext, $this->page->context);
  128. $displaypagetypewarning = false;
  129. if (!array_key_exists($this->block->instance->pagetypepattern, $pagetypelist)) {
  130. // Pushing block's existing page type pattern
  131. $pagetypestringname = 'page-'.str_replace('*', 'x', $this->block->instance->pagetypepattern);
  132. if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) {
  133. $pagetypelist[$this->block->instance->pagetypepattern] = get_string($pagetypestringname, 'pagetype');
  134. } else {
  135. //as a last resort we could put the page type pattern in the select box
  136. //however this causes mod-data-view to be added if the only option available is mod-data-*
  137. // so we are just showing a warning to users about their prev setting being reset
  138. $displaypagetypewarning = true;
  139. }
  140. }
  141. }
  142. // hide page type pattern select box if there is only one choice
  143. if (count($pagetypelist) > 1) {
  144. if ($displaypagetypewarning) {
  145. $mform->addElement('static', 'pagetypewarning', '', get_string('pagetypewarning','block'));
  146. }
  147. $mform->addElement('select', 'bui_pagetypepattern', get_string('restrictpagetypes', 'block'), $pagetypelist);
  148. } else {
  149. $values = array_keys($pagetypelist);
  150. $value = array_pop($values);
  151. // Now we are really hiding a lot (both page-contexts and page-type-patterns),
  152. // specially in some systemcontext pages having only one option (my/user...)
  153. // so, until it's decided if we are going to add the 'bring-back' pattern to
  154. // all those pages or no (see MDL-30574), we are going to show the unique
  155. // element statically
  156. // TODO: Revisit this once MDL-30574 has been decided and implemented, although
  157. // perhaps it's not bad to always show this statically when only one pattern is
  158. // available.
  159. if (!$this->is_editing_the_frontpage()) {
  160. // Try to beautify it
  161. $strvalue = $value;
  162. $strkey = 'page-'.str_replace('*', 'x', $strvalue);
  163. if (get_string_manager()->string_exists($strkey, 'pagetype')) {
  164. $strvalue = get_string($strkey, 'pagetype');
  165. }
  166. // Show as static (hidden has been set already)
  167. $mform->addElement('static', 'bui_staticpagetypepattern',
  168. get_string('restrictpagetypes','block'), $strvalue);
  169. }
  170. }
  171. if ($this->page->subpage) {
  172. if ($parentcontext->contextlevel != CONTEXT_USER) {
  173. $subpageoptions = array(
  174. '%@NULL@%' => get_string('anypagematchingtheabove', 'block'),
  175. $this->page->subpage => get_string('thisspecificpage', 'block', $this->page->subpage),
  176. );
  177. $mform->addElement('select', 'bui_subpagepattern', get_string('subpages', 'block'), $subpageoptions);
  178. }
  179. }
  180. $defaultregionoptions = $regionoptions;
  181. $defaultregion = $this->block->instance->defaultregion;
  182. if (!array_key_exists($defaultregion, $defaultregionoptions)) {
  183. $defaultregionoptions[$defaultregion] = $defaultregion;
  184. }
  185. $mform->addElement('select', 'bui_defaultregion', get_string('defaultregion', 'block'), $defaultregionoptions);
  186. $mform->addHelpButton('bui_defaultregion', 'defaultregion', 'block');
  187. $mform->addElement('select', 'bui_defaultweight', get_string('defaultweight', 'block'), $weightoptions);
  188. $mform->addHelpButton('bui_defaultweight', 'defaultweight', 'block');
  189. // Where this block is positioned on this page.
  190. $mform->addElement('header', 'onthispage', get_string('onthispage', 'block'));
  191. $mform->addElement('selectyesno', 'bui_visible', get_string('visible', 'block'));
  192. $blockregion = $this->block->instance->region;
  193. if (!array_key_exists($blockregion, $regionoptions)) {
  194. $regionoptions[$blockregion] = $blockregion;
  195. }
  196. $mform->addElement('select', 'bui_region', get_string('region', 'block'), $regionoptions);
  197. $mform->addElement('select', 'bui_weight', get_string('weight', 'block'), $weightoptions);
  198. $pagefields = array('bui_visible', 'bui_region', 'bui_weight');
  199. if (!$this->block->user_can_edit()) {
  200. $mform->hardFreezeAllVisibleExcept($pagefields);
  201. }
  202. if (!$this->page->user_can_edit_blocks()) {
  203. $mform->hardFreeze($pagefields);
  204. }
  205. $this->add_action_buttons();
  206. }
  207. /**
  208. * Returns true if the user is editing a frontpage.
  209. * @return bool
  210. */
  211. public function is_editing_the_frontpage() {
  212. // There are some conditions to check related to contexts.
  213. $ctxconditions = $this->page->context->contextlevel == CONTEXT_COURSE &&
  214. $this->page->context->instanceid == get_site()->id;
  215. $issiteindex = (strpos($this->page->pagetype, 'site-index') === 0);
  216. // So now we can be 100% sure if edition is happening at frontpage.
  217. return ($ctxconditions && $issiteindex);
  218. }
  219. function set_data($defaults) {
  220. // Prefix bui_ on all the core field names.
  221. $blockfields = array('showinsubcontexts', 'pagetypepattern', 'subpagepattern', 'parentcontextid',
  222. 'defaultregion', 'defaultweight', 'visible', 'region', 'weight');
  223. foreach ($blockfields as $field) {
  224. $newname = 'bui_' . $field;
  225. $defaults->$newname = $defaults->$field;
  226. }
  227. // Copy block config into config_ fields.
  228. if (!empty($this->block->config)) {
  229. foreach ($this->block->config as $field => $value) {
  230. $configfield = 'config_' . $field;
  231. $defaults->$configfield = $value;
  232. }
  233. }
  234. // Munge ->subpagepattern becuase HTML selects don't play nicely with NULLs.
  235. if (empty($defaults->bui_subpagepattern)) {
  236. $defaults->bui_subpagepattern = '%@NULL@%';
  237. }
  238. $systemcontext = context_system::instance();
  239. if ($defaults->parentcontextid == $systemcontext->id) {
  240. $defaults->bui_contexts = BUI_CONTEXTS_ENTIRE_SITE; // System-wide and sticky
  241. } else {
  242. $defaults->bui_contexts = $defaults->bui_showinsubcontexts;
  243. }
  244. // Some fields may not be editable, remember the values here so we can return them in get_data().
  245. $this->defaults = [
  246. 'bui_parentcontextid' => $defaults->bui_parentcontextid,
  247. 'bui_contexts' => $defaults->bui_contexts,
  248. 'bui_pagetypepattern' => $defaults->bui_pagetypepattern,
  249. 'bui_subpagepattern' => $defaults->bui_subpagepattern,
  250. ];
  251. parent::set_data($defaults);
  252. }
  253. /**
  254. * Override this to create any form fields specific to this type of block.
  255. * @param object $mform the form being built.
  256. */
  257. protected function specific_definition($mform) {
  258. // By default, do nothing.
  259. }
  260. /**
  261. * Return submitted data if properly submitted or returns NULL if validation fails or
  262. * if there is no submitted data.
  263. *
  264. * @return object submitted data; NULL if not valid or not submitted or cancelled
  265. */
  266. public function get_data() {
  267. if ($data = parent::get_data()) {
  268. // Blocklib expects 'bui_editingatfrontpage' property to be returned from this form.
  269. $data->bui_editingatfrontpage = $this->is_editing_the_frontpage();
  270. // Some fields are non-editable and we need to populate them with the values from set_data().
  271. return (object)((array)$data + $this->defaults);
  272. }
  273. return $data;
  274. }
  275. }