/reportbuilder/classes/external/custom_report_conditions_exporter.php

https://github.com/markn86/moodle · PHP · 151 lines · 96 code · 14 blank · 41 comment · 3 complexity · 63e0c9516f0e8fc8a49866061c8b7bf1 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. declare(strict_types=1);
  17. namespace core_reportbuilder\external;
  18. use renderer_base;
  19. use core\external\exporter;
  20. use core_reportbuilder\form\condition;
  21. use core_reportbuilder\local\report\base;
  22. use core_reportbuilder\local\models\filter;
  23. /**
  24. * Custom report conditions exporter class
  25. *
  26. * @package core_reportbuilder
  27. * @copyright 2021 Paul Holden <paulh@moodle.com>
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29. */
  30. class custom_report_conditions_exporter extends exporter {
  31. /**
  32. * Return a list of objects that are related to the exporter
  33. *
  34. * @return array
  35. */
  36. protected static function define_related(): array {
  37. return [
  38. 'report' => base::class,
  39. ];
  40. }
  41. /**
  42. * Return the list of additional properties for read structure and export
  43. *
  44. * @return array[]
  45. */
  46. protected static function define_other_properties(): array {
  47. return [
  48. 'hasavailableconditions' => [
  49. 'type' => PARAM_BOOL,
  50. ],
  51. 'availableconditions' => [
  52. 'type' => [
  53. 'optiongroup' => [
  54. 'type' => [
  55. 'text' => ['type' => PARAM_TEXT],
  56. 'values' => [
  57. 'type' => [
  58. 'value' => ['type' => PARAM_TEXT],
  59. 'visiblename' => ['type' => PARAM_TEXT],
  60. ],
  61. 'multiple' => true,
  62. ],
  63. ],
  64. ],
  65. ],
  66. 'multiple' => true,
  67. 'optional' => true
  68. ],
  69. 'hasactiveconditions' => [
  70. 'type' => PARAM_BOOL,
  71. ],
  72. 'activeconditionsform' => [
  73. 'type' => PARAM_RAW,
  74. 'optional' => true,
  75. ],
  76. 'helpicon' => [
  77. 'type' => PARAM_RAW,
  78. 'optional' => true,
  79. ],
  80. 'javascript' => [
  81. 'type' => PARAM_RAW,
  82. 'optional' => true,
  83. ],
  84. ];
  85. }
  86. /**
  87. * Get the additional values to inject while exporting
  88. *
  89. * @param renderer_base $output
  90. * @return array
  91. */
  92. protected function get_other_values(renderer_base $output): array {
  93. /** @var base $report */
  94. $report = $this->related['report'];
  95. $reportid = $report->get_report_persistent()->get('id');
  96. // Current condition instances contained in the report.
  97. $conditioninstances = filter::get_condition_records($reportid, 'filterorder');
  98. $conditionidentifiers = array_map(static function(filter $condition): string {
  99. return $condition->get('uniqueidentifier');
  100. }, $conditioninstances);
  101. $availableconditions = [];
  102. // Populate available conditions.
  103. foreach ($report->get_conditions() as $condition) {
  104. if (in_array($condition->get_unique_identifier(), $conditionidentifiers)) {
  105. continue;
  106. }
  107. $entityname = $condition->get_entity_name();
  108. if (!array_key_exists($entityname, $availableconditions)) {
  109. $availableconditions[$entityname] = [
  110. 'optiongroup' => [
  111. 'text' => $report->get_entity_title($entityname)->out(),
  112. 'values' => [],
  113. ],
  114. ];
  115. }
  116. $availableconditions[$entityname]['optiongroup']['values'][] = [
  117. 'value' => $condition->get_unique_identifier(),
  118. 'visiblename' => $condition->get_header(),
  119. ];
  120. }
  121. // Generate filters form if report contains any filters.
  122. $conditionspresent = !empty($conditioninstances);
  123. if ($conditionspresent) {
  124. $conditionsform = new condition(null, null, 'post', '', [], true, [
  125. 'reportid' => $reportid,
  126. ]);
  127. $conditionsform->set_data_for_dynamic_submission();
  128. }
  129. return [
  130. 'hasavailableconditions' => !empty($availableconditions),
  131. 'availableconditions' => array_values($availableconditions),
  132. 'hasactiveconditions' => $conditionspresent,
  133. 'activeconditionsform' => $conditionspresent ? $conditionsform->render() : '',
  134. 'helpicon' => $output->help_icon('conditions', 'core_reportbuilder'),
  135. ];
  136. }
  137. }