PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/blocks/edit_form.php

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