renderer.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * Capability tool renderer.
  18. *
  19. * @package tool_capability
  20. * @copyright 2013 Sam Hemelryk
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. /**
  24. * The primary renderer for the capability tool.
  25. *
  26. * @copyright 2013 Sam Hemelryk
  27. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28. */
  29. class tool_capability_renderer extends plugin_renderer_base {
  30. /**
  31. * Returns an array of permission strings.
  32. *
  33. * @return lang_string[]
  34. */
  35. protected function get_permission_strings() {
  36. static $strpermissions;
  37. if (!$strpermissions) {
  38. $strpermissions = array(
  39. CAP_INHERIT => new lang_string('inherit', 'role'),
  40. CAP_ALLOW => new lang_string('allow', 'role'),
  41. CAP_PREVENT => new lang_string('prevent', 'role'),
  42. CAP_PROHIBIT => new lang_string('prohibit', 'role')
  43. );
  44. }
  45. return $strpermissions;
  46. }
  47. /**
  48. * Returns an array of permission CSS classes.
  49. *
  50. * @return string[]
  51. */
  52. protected function get_permission_classes() {
  53. static $permissionclasses;
  54. if (!$permissionclasses) {
  55. $permissionclasses = array(
  56. CAP_INHERIT => 'inherit',
  57. CAP_ALLOW => 'allow',
  58. CAP_PREVENT => 'prevent',
  59. CAP_PROHIBIT => 'prohibit',
  60. );
  61. }
  62. return $permissionclasses;
  63. }
  64. /**
  65. * Produces a table to visually compare roles and capabilities.
  66. *
  67. * @param array $capabilities An array of capabilities to show comparison for.
  68. * @param int $contextid The context we are displaying for.
  69. * @param array $roles An array of roles to show comparison for.
  70. * @return string
  71. */
  72. public function capability_comparison_table(array $capabilities, $contextid, array $roles) {
  73. $strpermissions = $this->get_permission_strings();
  74. $permissionclasses = $this->get_permission_classes();
  75. if ($contextid === context_system::instance()->id) {
  76. $strpermissions[CAP_INHERIT] = new lang_string('notset', 'role');
  77. }
  78. $table = new html_table();
  79. $table->attributes['class'] = 'comparisontable';
  80. $table->head = array('&nbsp;');
  81. foreach ($roles as $role) {
  82. $url = new moodle_url('/admin/roles/define.php', array('action' => 'view', 'roleid' => $role->id));
  83. $table->head[] = html_writer::div(html_writer::link($url, $role->localname));
  84. }
  85. $table->data = array();
  86. foreach ($capabilities as $capability) {
  87. $contexts = tool_capability_calculate_role_data($capability, $roles);
  88. $captitle = new html_table_cell(get_capability_string($capability) . html_writer::span($capability));
  89. $captitle->header = true;
  90. $row = new html_table_row(array($captitle));
  91. foreach ($roles as $role) {
  92. if (isset($contexts[$contextid]->rolecapabilities[$role->id])) {
  93. $permission = $contexts[$contextid]->rolecapabilities[$role->id];
  94. } else {
  95. $permission = CAP_INHERIT;
  96. }
  97. $cell = new html_table_cell($strpermissions[$permission]);
  98. $cell->attributes['class'] = $permissionclasses[$permission];
  99. $row->cells[] = $cell;
  100. }
  101. $table->data[] = $row;
  102. }
  103. // Start the list item, and print the context name as a link to the place to make changes.
  104. if ($contextid == context_system::instance()->id) {
  105. $url = new moodle_url('/admin/roles/manage.php');
  106. $title = get_string('changeroles', 'tool_capability');
  107. } else {
  108. $url = new moodle_url('/admin/roles/override.php', array('contextid' => $contextid));
  109. $title = get_string('changeoverrides', 'tool_capability');
  110. }
  111. $context = context::instance_by_id($contextid);
  112. $html = $this->output->heading(html_writer::link($url, $context->get_context_name(), array('title' => $title)), 3);
  113. $html .= html_writer::table($table);
  114. // If there are any child contexts, print them recursively.
  115. if (!empty($contexts[$contextid]->children)) {
  116. foreach ($contexts[$contextid]->children as $childcontextid) {
  117. $html .= $this->capability_comparison_table($capabilities, $childcontextid, $roles, true);
  118. }
  119. }
  120. return $html;
  121. }
  122. }