category.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 is used to display a categories sub categories, external pages, and settings.
  18. *
  19. * @since Moodle 2.3
  20. * @package admin
  21. * @copyright 2011 Sam Hemelryk
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. require_once('../config.php');
  25. require_once($CFG->libdir.'/adminlib.php');
  26. $category = required_param('category', PARAM_SAFEDIR);
  27. $return = optional_param('return','', PARAM_ALPHA);
  28. $adminediting = optional_param('adminedit', -1, PARAM_BOOL);
  29. /// no guest autologin
  30. require_login(0, false);
  31. $PAGE->set_context(context_system::instance());
  32. $PAGE->set_url('/admin/category.php', array('category' => $category));
  33. $PAGE->set_pagetype('admin-setting-' . $category);
  34. $PAGE->set_pagelayout('admin');
  35. $PAGE->navigation->clear_cache();
  36. $adminroot = admin_get_root(); // need all settings
  37. $settingspage = $adminroot->locate($category, true);
  38. if (empty($settingspage) or !($settingspage instanceof admin_category)) {
  39. print_error('categoryerror', 'admin', "$CFG->wwwroot/$CFG->admin/");
  40. }
  41. if (!($settingspage->check_access())) {
  42. print_error('accessdenied', 'admin');
  43. }
  44. $statusmsg = '';
  45. $errormsg = '';
  46. if ($data = data_submitted() and confirm_sesskey()) {
  47. if (admin_write_settings($data)) {
  48. $statusmsg = get_string('changessaved');
  49. }
  50. if (empty($adminroot->errors)) {
  51. switch ($return) {
  52. case 'site': redirect("$CFG->wwwroot/");
  53. case 'admin': redirect("$CFG->wwwroot/$CFG->admin/");
  54. }
  55. } else {
  56. $errormsg = get_string('errorwithsettings', 'admin');
  57. $firsterror = reset($adminroot->errors);
  58. }
  59. $settingspage = $adminroot->locate($category, true);
  60. }
  61. if ($PAGE->user_allowed_editing() && $adminediting != -1) {
  62. $USER->editing = $adminediting;
  63. }
  64. if ($PAGE->user_allowed_editing()) {
  65. $url = clone($PAGE->url);
  66. if ($PAGE->user_is_editing()) {
  67. $caption = get_string('blockseditoff');
  68. $url->param('adminedit', 'off');
  69. } else {
  70. $caption = get_string('blocksediton');
  71. $url->param('adminedit', 'on');
  72. }
  73. $buttons = $OUTPUT->single_button($url, $caption, 'get');
  74. }
  75. $savebutton = false;
  76. $outputhtml = '';
  77. foreach ($settingspage->children as $childpage) {
  78. if ($childpage->is_hidden()) {
  79. continue;
  80. }
  81. if ($childpage instanceof admin_externalpage) {
  82. $outputhtml .= $OUTPUT->heading(html_writer::link($childpage->url, $childpage->visiblename), 3);
  83. } else if ($childpage instanceof admin_settingpage) {
  84. $outputhtml .= $OUTPUT->heading(html_writer::link(new moodle_url('/'.$CFG->admin.'/settings.php', array('section' => $childpage->name)), $childpage->visiblename), 3);
  85. // If its a settings page and has settings lets display them.
  86. if (!empty($childpage->settings)) {
  87. $outputhtml .= html_writer::start_tag('fieldset', array('class' => 'adminsettings'));
  88. foreach ($childpage->settings as $setting) {
  89. if (empty($setting->nosave)) {
  90. $savebutton = true;
  91. }
  92. $fullname = $setting->get_full_name();
  93. if (array_key_exists($fullname, $adminroot->errors)) {
  94. $data = $adminroot->errors[$fullname]->data;
  95. } else {
  96. $data = $setting->get_setting();
  97. }
  98. $outputhtml .= html_writer::tag('div', '<!-- -->', array('class' => 'clearer'));
  99. $outputhtml .= $setting->output_html($data);
  100. }
  101. $outputhtml .= html_writer::end_tag('fieldset');
  102. }
  103. } else if ($childpage instanceof admin_category) {
  104. $outputhtml .= $OUTPUT->heading(html_writer::link(new moodle_url('/'.$CFG->admin.'/category.php', array('category' => $childpage->name)), get_string('admincategory', 'admin', $childpage->visiblename)), 3);
  105. }
  106. }
  107. if ($savebutton) {
  108. $outputhtml .= html_writer::start_tag('div', array('class' => 'form-buttons'));
  109. $outputhtml .= html_writer::empty_tag('input', array('class' => 'form-submit', 'type' => 'submit', 'value' => get_string('savechanges','admin')));
  110. $outputhtml .= html_writer::end_tag('div');
  111. }
  112. $visiblepathtosection = array_reverse($settingspage->visiblepath);
  113. $PAGE->set_title("$SITE->shortname: " . implode(": ",$visiblepathtosection));
  114. $PAGE->set_heading($SITE->fullname);
  115. $PAGE->set_button($buttons);
  116. echo $OUTPUT->header();
  117. if ($errormsg !== '') {
  118. echo $OUTPUT->notification($errormsg);
  119. } else if ($statusmsg !== '') {
  120. echo $OUTPUT->notification($statusmsg, 'notifysuccess');
  121. }
  122. $path = array_reverse($settingspage->visiblepath);
  123. if (is_array($path)) {
  124. $visiblename = join(' / ', $path);
  125. } else {
  126. $visiblename = $path;
  127. }
  128. echo $OUTPUT->heading(get_string('admincategory', 'admin', $visiblename), 2);
  129. echo html_writer::start_tag('form', array('action' => '', 'method' => 'post', 'id' => 'adminsettings'));
  130. echo html_writer::start_tag('div');
  131. echo html_writer::input_hidden_params(new moodle_url($PAGE->url, array('sesskey' => sesskey(), 'return' => $return)));
  132. echo html_writer::end_tag('div');
  133. echo html_writer::start_tag('fieldset');
  134. echo html_writer::tag('div', '<!-- -->', array('class' => 'clearer'));
  135. echo $outputhtml;
  136. echo html_writer::end_tag('fieldset');
  137. echo html_writer::end_tag('form');
  138. echo $OUTPUT->footer();