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

/backup/util/ui/base_moodleform.class.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 372 lines | 222 code | 18 blank | 132 comment | 39 complexity | 55412acaf0851014ad8d08ea4c3af77c MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-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. * This file contains the generic moodleform bridge for the backup user interface
  18. * as well as the individual forms that relate to the different stages the user
  19. * interface can exist within.
  20. *
  21. * @package moodlecore
  22. * @copyright 2010 Sam Hemelryk
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. require_once($CFG->libdir . '/formslib.php');
  27. /**
  28. * Backup moodleform bridge
  29. *
  30. * Ahhh the mighty moodleform bridge! Strong enough to take the weight of 682 full
  31. * grown african swallows all of whom have been carring coconuts for several days.
  32. * EWWWWW!!!!!!!!!!!!!!!!!!!!!!!!
  33. *
  34. * @copyright 2010 Sam Hemelryk
  35. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36. */
  37. abstract class base_moodleform extends moodleform {
  38. /**
  39. * The stage this form belongs to
  40. * @var base_ui_stage
  41. */
  42. protected $uistage = null;
  43. /**
  44. * True if we have a course div open, false otherwise
  45. * @var bool
  46. */
  47. protected $coursediv = false;
  48. /**
  49. * True if we have a section div open, false otherwise
  50. * @var bool
  51. */
  52. protected $sectiondiv = false;
  53. /**
  54. * True if we have an activity div open, false otherwise
  55. * @var bool
  56. */
  57. protected $activitydiv = false;
  58. /**
  59. * Creates the form
  60. *
  61. * @param backup_ui_stage $uistage
  62. * @param moodle_url|string $action
  63. * @param mixed $customdata
  64. * @param string $method get|post
  65. * @param string $target
  66. * @param array $attributes
  67. * @param bool $editable
  68. */
  69. function __construct(base_ui_stage $uistage, $action=null, $customdata=null, $method='post', $target='', $attributes=null, $editable=true) {
  70. $this->uistage = $uistage;
  71. // Add a class to the attributes to prevent the default collapsible behaviour.
  72. if (!$attributes) {
  73. $attributes = array();
  74. }
  75. $attributes['class'] = 'unresponsive';
  76. if (!isset($attributes['enctype'])) {
  77. $attributes['enctype'] = 'application/x-www-form-urlencoded'; // Enforce compatibility with our max_input_vars hack.
  78. }
  79. parent::__construct($action, $customdata, $method, $target, $attributes, $editable);
  80. }
  81. /**
  82. * The standard form definition... obviously not much here
  83. */
  84. function definition() {
  85. $ui = $this->uistage->get_ui();
  86. $mform = $this->_form;
  87. $mform->setDisableShortforms();
  88. $stage = $mform->addElement('hidden', 'stage', $this->uistage->get_stage());
  89. $mform->setType('stage', PARAM_INT);
  90. $stage = $mform->addElement('hidden', $ui->get_name(), $ui->get_uniqueid());
  91. $mform->setType($ui->get_name(), PARAM_ALPHANUM);
  92. $params = $this->uistage->get_params();
  93. if (is_array($params) && count($params) > 0) {
  94. foreach ($params as $name=>$value) {
  95. // TODO: Horrible hack, but current backup ui structure does not allow
  96. // to make this easy (only changing params to objects that would be
  97. // possible. MDL-38735.
  98. $intparams = array(
  99. 'contextid', 'importid', 'target');
  100. $stage = $mform->addElement('hidden', $name, $value);
  101. if (in_array($name, $intparams)) {
  102. $mform->setType($name, PARAM_INT);
  103. } else {
  104. // Adding setType() to avoid missing setType() warnings.
  105. // MDL-39126: support $mform->setType() for additional backup parameters.
  106. $mform->setType($name, PARAM_RAW);
  107. }
  108. }
  109. }
  110. }
  111. /**
  112. * Definition applied after the data is organised.. why's it here? because I want
  113. * to add elements on the fly.
  114. * @global moodle_page $PAGE
  115. */
  116. function definition_after_data() {
  117. $buttonarray=array();
  118. $buttonarray[] = $this->_form->createElement('submit', 'submitbutton', get_string($this->uistage->get_ui()->get_name().'stage'.$this->uistage->get_stage().'action', 'backup'), array('class'=>'proceedbutton'));
  119. if (!$this->uistage->is_first_stage()) {
  120. $buttonarray[] = $this->_form->createElement('submit', 'previous', get_string('previousstage','backup'));
  121. }
  122. $buttonarray[] = $this->_form->createElement('cancel', 'cancel', get_string('cancel'), array('class'=>'confirmcancel'));
  123. $this->_form->addGroup($buttonarray, 'buttonar', '', array(' '), false);
  124. $this->_form->closeHeaderBefore('buttonar');
  125. $this->_definition_finalized = true;
  126. }
  127. /**
  128. * Closes any open divs
  129. */
  130. function close_task_divs() {
  131. if ($this->activitydiv) {
  132. $this->_form->addElement('html', html_writer::end_tag('div'));
  133. $this->activitydiv = false;
  134. }
  135. if ($this->sectiondiv) {
  136. $this->_form->addElement('html', html_writer::end_tag('div'));
  137. $this->sectiondiv = false;
  138. }
  139. if ($this->coursediv) {
  140. $this->_form->addElement('html', html_writer::end_tag('div'));
  141. $this->coursediv = false;
  142. }
  143. }
  144. /**
  145. * Adds the backup_setting as a element to the form
  146. * @param backup_setting $setting
  147. * @return bool
  148. */
  149. function add_setting(backup_setting $setting, base_task $task=null) {
  150. return $this->add_settings(array(array($setting, $task)));
  151. }
  152. /**
  153. * Adds multiple backup_settings as elements to the form
  154. * @param array $settingstasks Consists of array($setting, $task) elements
  155. * @return bool
  156. */
  157. public function add_settings(array $settingstasks) {
  158. global $OUTPUT;
  159. $defaults = array();
  160. foreach ($settingstasks as $st) {
  161. list($setting, $task) = $st;
  162. // If the setting cant be changed or isn't visible then add it as a fixed setting.
  163. if (!$setting->get_ui()->is_changeable() || $setting->get_visibility() != backup_setting::VISIBLE) {
  164. $this->add_fixed_setting($setting, $task);
  165. continue;
  166. }
  167. // First add the formatting for this setting
  168. $this->add_html_formatting($setting);
  169. // Then call the add method with the get_element_properties array
  170. call_user_func_array(array($this->_form, 'addElement'), $setting->get_ui()->get_element_properties($task, $OUTPUT));
  171. $this->_form->setType($setting->get_ui_name(), $setting->get_param_validation());
  172. $defaults[$setting->get_ui_name()] = $setting->get_value();
  173. if ($setting->has_help()) {
  174. list($identifier, $component) = $setting->get_help();
  175. $this->_form->addHelpButton($setting->get_ui_name(), $identifier, $component);
  176. }
  177. $this->_form->addElement('html', html_writer::end_tag('div'));
  178. }
  179. $this->_form->setDefaults($defaults);
  180. return true;
  181. }
  182. /**
  183. * Adds a heading to the form
  184. * @param string $name
  185. * @param string $text
  186. */
  187. function add_heading($name , $text) {
  188. $this->_form->addElement('header', $name, $text);
  189. }
  190. /**
  191. * Adds HTML formatting for the given backup setting, needed to group/segment
  192. * correctly.
  193. * @param backup_setting $setting
  194. */
  195. protected function add_html_formatting(backup_setting $setting) {
  196. $mform = $this->_form;
  197. $isincludesetting = (strpos($setting->get_name(), '_include')!==false);
  198. if ($isincludesetting && $setting->get_level() != backup_setting::ROOT_LEVEL) {
  199. switch ($setting->get_level()) {
  200. case backup_setting::COURSE_LEVEL:
  201. if ($this->activitydiv) {
  202. $this->_form->addElement('html', html_writer::end_tag('div'));
  203. $this->activitydiv = false;
  204. }
  205. if ($this->sectiondiv) {
  206. $this->_form->addElement('html', html_writer::end_tag('div'));
  207. $this->sectiondiv = false;
  208. }
  209. if ($this->coursediv) {
  210. $this->_form->addElement('html', html_writer::end_tag('div'));
  211. }
  212. $mform->addElement('html', html_writer::start_tag('div', array('class'=>'grouped_settings course_level')));
  213. $mform->addElement('html', html_writer::start_tag('div', array('class'=>'include_setting course_level')));
  214. $this->coursediv = true;
  215. break;
  216. case backup_setting::SECTION_LEVEL:
  217. if ($this->activitydiv) {
  218. $this->_form->addElement('html', html_writer::end_tag('div'));
  219. $this->activitydiv = false;
  220. }
  221. if ($this->sectiondiv) {
  222. $this->_form->addElement('html', html_writer::end_tag('div'));
  223. }
  224. $mform->addElement('html', html_writer::start_tag('div', array('class'=>'grouped_settings section_level')));
  225. $mform->addElement('html', html_writer::start_tag('div', array('class'=>'include_setting section_level')));
  226. $this->sectiondiv = true;
  227. break;
  228. case backup_setting::ACTIVITY_LEVEL:
  229. if ($this->activitydiv) {
  230. $this->_form->addElement('html', html_writer::end_tag('div'));
  231. }
  232. $mform->addElement('html', html_writer::start_tag('div', array('class'=>'grouped_settings activity_level')));
  233. $mform->addElement('html', html_writer::start_tag('div', array('class'=>'include_setting activity_level')));
  234. $this->activitydiv = true;
  235. break;
  236. default:
  237. $mform->addElement('html', html_writer::start_tag('div', array('class'=>'normal_setting')));
  238. break;
  239. }
  240. } else if ($setting->get_level() == backup_setting::ROOT_LEVEL) {
  241. $mform->addElement('html', html_writer::start_tag('div', array('class'=>'root_setting')));
  242. } else {
  243. $mform->addElement('html', html_writer::start_tag('div', array('class'=>'normal_setting')));
  244. }
  245. }
  246. /**
  247. * Adds a fixed or static setting to the form
  248. * @param backup_setting $setting
  249. */
  250. function add_fixed_setting(backup_setting $setting, base_task $task) {
  251. global $OUTPUT;
  252. $settingui = $setting->get_ui();
  253. if ($setting->get_visibility() == backup_setting::VISIBLE) {
  254. $this->add_html_formatting($setting);
  255. switch ($setting->get_status()) {
  256. case backup_setting::LOCKED_BY_PERMISSION:
  257. $icon = ' '.$OUTPUT->pix_icon('i/permissionlock', get_string('lockedbypermission', 'backup'), 'moodle', array('class'=>'smallicon lockedicon permissionlock'));
  258. break;
  259. case backup_setting::LOCKED_BY_CONFIG:
  260. $icon = ' '.$OUTPUT->pix_icon('i/configlock', get_string('lockedbyconfig', 'backup'), 'moodle', array('class'=>'smallicon lockedicon configlock'));
  261. break;
  262. case backup_setting::LOCKED_BY_HIERARCHY:
  263. $icon = ' '.$OUTPUT->pix_icon('i/hierarchylock', get_string('lockedbyhierarchy', 'backup'), 'moodle', array('class'=>'smallicon lockedicon configlock'));
  264. break;
  265. default:
  266. $icon = '';
  267. break;
  268. }
  269. $label = $settingui->get_label($task);
  270. $labelicon = $settingui->get_icon();
  271. if (!empty($labelicon)) {
  272. $label .= '&nbsp;'.$OUTPUT->render($labelicon);
  273. }
  274. $this->_form->addElement('static', 'static_'.$settingui->get_name(), $label, $settingui->get_static_value().$icon);
  275. $this->_form->addElement('html', html_writer::end_tag('div'));
  276. }
  277. $this->_form->addElement('hidden', $settingui->get_name(), $settingui->get_value());
  278. $this->_form->setType($settingui->get_name(), $settingui->get_param_validation());
  279. }
  280. /**
  281. * Adds dependencies to the form recursively
  282. *
  283. * @param backup_setting $setting
  284. */
  285. function add_dependencies(backup_setting $setting) {
  286. $mform = $this->_form;
  287. // Apply all dependencies for backup
  288. foreach ($setting->get_my_dependency_properties() as $key=>$dependency) {
  289. call_user_func_array(array($this->_form, 'disabledIf'), $dependency);
  290. }
  291. }
  292. /**
  293. * Returns true if the form was cancelled, false otherwise
  294. * @return bool
  295. */
  296. public function is_cancelled() {
  297. return (optional_param('cancel', false, PARAM_BOOL) || parent::is_cancelled());
  298. }
  299. /**
  300. * Removes an element from the form if it exists
  301. * @param string $elementname
  302. * @return bool
  303. */
  304. public function remove_element($elementname) {
  305. if ($this->_form->elementExists($elementname)) {
  306. return $this->_form->removeElement($elementname);
  307. } else {
  308. return false;
  309. }
  310. }
  311. /**
  312. * Gets an element from the form if it exists
  313. *
  314. * @param string $elementname
  315. * @return HTML_QuickForm_input|MoodleQuickForm_group
  316. */
  317. public function get_element($elementname) {
  318. if ($this->_form->elementExists($elementname)) {
  319. return $this->_form->getElement($elementname);
  320. } else {
  321. return false;
  322. }
  323. }
  324. /**
  325. * Displays the form
  326. */
  327. public function display() {
  328. global $PAGE, $COURSE;
  329. $this->require_definition_after_data();
  330. $config = new stdClass;
  331. $config->title = get_string('confirmcancel', 'backup');
  332. $config->question = get_string('confirmcancelquestion', 'backup');
  333. $config->yesLabel = get_string('confirmcancelyes', 'backup');
  334. $config->noLabel = get_string('confirmcancelno', 'backup');
  335. $config->closeButtonTitle = get_string('close', 'editor');
  336. $PAGE->requires->yui_module('moodle-backup-confirmcancel', 'M.core_backup.watch_cancel_buttons', array($config));
  337. // Get list of module types on course.
  338. $modinfo = get_fast_modinfo($COURSE);
  339. $modnames = $modinfo->get_used_module_names(true);
  340. $PAGE->requires->yui_module('moodle-backup-backupselectall', 'M.core_backup.select_all_init',
  341. array($modnames));
  342. $PAGE->requires->strings_for_js(array('select', 'all', 'none'), 'moodle');
  343. $PAGE->requires->strings_for_js(array('showtypes', 'hidetypes'), 'backup');
  344. parent::display();
  345. }
  346. /**
  347. * Ensures the the definition after data is loaded
  348. */
  349. public function require_definition_after_data() {
  350. if (!$this->_definition_finalized) {
  351. $this->definition_after_data();
  352. }
  353. }
  354. }