MDL-81601 core_course: Add course index completion status behats
[moodle.git] / reportbuilder / lib.php
blob08c888b0195706a1b03dce5f38ff949a60d969e4
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.
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/>.
17 /**
18 * Callback methods for reportbuilder component
20 * @package core_reportbuilder
21 * @copyright 2021 Paul Holden <paulh@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 declare(strict_types=1);
27 use core\output\inplace_editable;
28 use core_reportbuilder\form\audience;
29 use core_reportbuilder\form\filter;
30 use core_reportbuilder\local\helpers\audience as audience_helper;
31 use core_reportbuilder\local\models\report;
32 use core_tag\output\{tagfeed, tagindex};
34 /**
35 * Return the filters form fragment
37 * @param array $params
38 * @return string
40 function core_reportbuilder_output_fragment_filters_form(array $params): string {
41 $filtersform = new filter(null, null, 'post', '', [], true, [
42 'reportid' => $params['reportid'],
43 'parameters' => $params['parameters'],
44 ]);
46 $filtersform->set_data_for_dynamic_submission();
48 return $filtersform->render();
51 /**
52 * Return the audience form fragment
54 * @param array $params
55 * @return string
57 function core_reportbuilder_output_fragment_audience_form(array $params): string {
58 global $PAGE;
60 $audienceform = new audience(null, null, 'post', '', [], true, [
61 'reportid' => $params['reportid'],
62 'classname' => $params['classname'],
63 ]);
64 $audienceform->set_data_for_dynamic_submission();
66 $context = [
67 'instanceid' => 0,
68 'heading' => $params['title'],
69 'headingeditable' => $params['title'],
70 'form' => $audienceform->render(),
71 'canedit' => true,
72 'candelete' => true,
73 'showormessage' => $params['showormessage'],
76 $renderer = $PAGE->get_renderer('core_reportbuilder');
77 return $renderer->render_from_template('core_reportbuilder/local/audience/form', $context);
80 /**
81 * Callback to return tagged reports
83 * @param core_tag_tag $tag
84 * @param bool $exclusivemode
85 * @param int|null $fromcontextid
86 * @param int|null $contextid
87 * @param bool $recurse
88 * @param int $page
89 * @return tagindex
91 function core_reportbuilder_get_tagged_reports(
92 core_tag_tag $tag,
93 bool $exclusivemode = false,
94 ?int $fromcontextid = 0,
95 ?int $contextid = 0,
96 bool $recurse = true,
97 int $page = 0,
98 ): tagindex {
99 global $OUTPUT;
101 // Limit the returned list to those reports the current user can access.
102 [$where, $params] = audience_helper::user_reports_list_access_sql('it');
104 $tagcount = $tag->count_tagged_items('core_reportbuilder', 'reportbuilder_report', $where, $params);
105 $perpage = $exclusivemode ? 20 : 5;
106 $pagecount = ceil($tagcount / $perpage);
108 $content = '';
110 if ($tagcount > 0) {
111 $tagfeed = new tagfeed();
113 $pixicon = new pix_icon('i/report', new lang_string('customreport', 'core_reportbuilder'));
115 $reports = $tag->get_tagged_items('core_reportbuilder', 'reportbuilder_report', $page * $perpage, $perpage,
116 $where, $params);
117 foreach ($reports as $report) {
118 $tagfeed->add(
119 $OUTPUT->render($pixicon),
120 html_writer::link(
121 new moodle_url('/reportbuilder/view.php', ['id' => $report->id]),
122 (new report(0, $report))->get_formatted_name(),
127 $content = $OUTPUT->render_from_template('core_tag/tagfeed', $tagfeed->export_for_template($OUTPUT));
130 return new tagindex($tag, 'core_reportbuilder', 'reportbuilder_report', $content, $exclusivemode, $fromcontextid,
131 $contextid, $recurse, $page, $pagecount);
135 * Plugin inplace editable implementation
137 * @param string $itemtype
138 * @param int $itemid
139 * @param string $newvalue
140 * @return inplace_editable|null
142 function core_reportbuilder_inplace_editable(string $itemtype, int $itemid, string $newvalue): ?inplace_editable {
143 switch ($itemtype) {
144 case 'reportname':
145 return \core_reportbuilder\output\report_name_editable::update($itemid, $newvalue);
147 case 'columnheading':
148 return \core_reportbuilder\output\column_heading_editable::update($itemid, $newvalue);
150 case 'columnaggregation':
151 return \core_reportbuilder\output\column_aggregation_editable::update($itemid, $newvalue);
153 case 'filterheading':
154 return \core_reportbuilder\output\filter_heading_editable::update($itemid, $newvalue);
156 case 'audienceheading':
157 return \core_reportbuilder\output\audience_heading_editable::update($itemid, $newvalue);
159 case 'schedulename':
160 return \core_reportbuilder\output\schedule_name_editable::update($itemid, $newvalue);
163 return null;