This commit is contained in:
Jun Pataleta 2019-11-07 13:22:32 +08:00
commit ae05752048
8 changed files with 168 additions and 86 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -43,35 +43,77 @@ export const init = (root) => {
// Generic filter handlers.
// Called to override click event to trigger a proper generate request with filtering.
const generateWithFilters = (event) => {
let newLink = $('#filtersform').attr('action');
const generateWithFilters = (event, getparams) => {
let currentLink = document.forms.filtersform.action,
newLink;
if (event) {
event.preventDefault();
let filterParams = event.target.search.substr(1);
newLink += '&' + filterParams;
let currentSplit = currentLink.split('?'),
currentstring = currentSplit[1],
newparamsarray = getparams.split('&'),
paramsstring = '',
paramkeys = [],
paramvalues = [];
// Separate out the existing action GET param string.
currentstring.split('&').forEach(function(param) {
let splitparam = param.split('=');
paramkeys.push(splitparam[0]);
paramvalues.push(splitparam[1]);
});
newparamsarray.forEach(function(paramstring) {
let newparam = paramstring.split('='),
existingkey = paramkeys.indexOf(newparam[0]);
// Overwrite value if existing, otherwise add new param.
if (existingkey > -1) {
paramvalues[existingkey] = newparam[1];
} else {
paramkeys.push(newparam[0]);
paramvalues.push(newparam[1]);
}
});
// Build URL.
paramkeys.forEach(function(name, key) {
paramsstring += `&${name}=${paramvalues[key]}`;
});
newLink = currentSplit[0] + '?' + paramsstring.substr(1);
} else {
newLink = currentLink;
}
$('#filtersform').attr('action', newLink);
$('#filtersform').submit();
document.forms.filtersform.action = newLink;
document.forms.filtersform.submit();
};
// Override 'reset table preferences' so it generates with filters.
$('.resettable').on("click", "a", function(event) {
generateWithFilters(event);
generateWithFilters(event, event.target.search.substr(1));
});
// Override table heading sort links so they generate with filters.
$('thead').on("click", "a", function(event) {
generateWithFilters(event);
generateWithFilters(event, event.target.search.substr(1));
});
// Override pagination page links so they generate with filters.
$('.pagination').on("click", "a", function(event) {
generateWithFilters(event);
generateWithFilters(event, event.target.search.substr(1));
});
// Override rows per page submission so it generates with filters.
if (document.forms.selectperpage) {
document.forms.selectperpage.onsubmit = (event) => {
let getparam = 'perpage=' + document.forms.selectperpage.elements.perpage.value;
generateWithFilters(event, getparam);
};
}
// Submit report via filter
const submitWithFilter = (containerelement) => {
// Disable the dates filter mform checker to prevent any changes triggering a warning to the user.

View File

@ -50,11 +50,17 @@ class summary_table extends table_sql {
/** Table to store summary data extracted from the log table */
const LOG_SUMMARY_TEMP_TABLE = 'forum_report_summary_counts';
/** Default number of rows to display per page */
const DEFAULT_PER_PAGE = 50;
/** @var \stdClass The various SQL segments that will be combined to form queries to fetch various information. */
public $sql;
/** @var int The number of rows to be displayed per page. */
protected $perpage = 25;
protected $perpage = self::DEFAULT_PER_PAGE;
/** @var array The values available for pagination size per page. */
protected $perpageoptions = [50, 100, 200];
/** @var \stdClass The course module object of the forum being reported on. */
protected $cm;
@ -75,6 +81,9 @@ class summary_table extends table_sql {
*/
protected $context = null;
/** @var bool Whether the user has the capability/capabilities to perform bulk operations. */
protected $allowbulkoperations = false;
/**
* @var bool
*/
@ -90,10 +99,11 @@ class summary_table extends table_sql {
*
* @param int $courseid The ID of the course the forum(s) exist within.
* @param array $filters Report filters in the format 'type' => [values].
* @param bool $bulkoperations Is the user allowed to perform bulk operations?
* @param bool $allowbulkoperations Is the user allowed to perform bulk operations?
* @param bool $canseeprivatereplies Whether the user can see all private replies or not.
* @param int $perpage The number of rows to display per page.
*/
public function __construct(int $courseid, array $filters, bool $bulkoperations, bool $canseeprivatereplies) {
public function __construct(int $courseid, array $filters, bool $allowbulkoperations, bool $canseeprivatereplies, int $perpage) {
global $USER, $OUTPUT;
$forumid = $filters['forums'][0];
@ -102,7 +112,9 @@ class summary_table extends table_sql {
$this->cm = get_coursemodule_from_instance('forum', $forumid, $courseid);
$this->context = \context_module::instance($this->cm->id);
$this->allowbulkoperations = $allowbulkoperations;
$this->canseeprivatereplies = $canseeprivatereplies;
$this->perpage = $perpage;
// Only show their own summary unless they have permission to view all.
if (!has_capability('forumreport/summary:viewall', $this->context)) {
@ -111,7 +123,7 @@ class summary_table extends table_sql {
$columnheaders = [];
if ($bulkoperations) {
if ($allowbulkoperations) {
$mastercheckbox = new \core\output\checkbox_toggleall('summaryreport-table', true, [
'id' => 'select-all-users',
'name' => 'select-all-users',
@ -794,4 +806,64 @@ class summary_table extends table_sql {
return $this->showwordcharcounts;
}
/**
* Fetch the number of items to be displayed per page.
*
* @return int
*/
public function get_perpage(): int {
return $this->perpage;
}
/**
* Overriding method to render the bulk actions and items per page pagination options directly below the table.
*
* @return void
*/
public function wrap_html_finish(): void {
global $OUTPUT;
$data = new \stdClass();
$data->showbulkactions = $this->allowbulkoperations;
if ($data->showbulkactions) {
$data->id = 'formactionid';
$data->attributes = [
[
'name' => 'data-action',
'value' => 'toggle'
],
[
'name' => 'data-togglegroup',
'value' => 'summaryreport-table'
],
[
'name' => 'data-toggle',
'value' => 'action'
],
[
'name' => 'disabled',
'value' => true
]
];
$data->actions = [
[
'value' => '#messageselect',
'name' => get_string('messageselectadd')
]
];
}
// Include the pagination size selector.
$perpageoptions = array_combine($this->perpageoptions, $this->perpageoptions);
$selected = in_array($this->perpage, $this->perpageoptions) ? $this->perpage : $this->perpageoptions[0];
$perpageselect = new \single_select(new \moodle_url(''), 'perpage',
$perpageoptions, $selected, null, 'selectperpage');
$perpageselect->set_label(get_string('perpage', 'moodle'));
$data->perpage = $perpageselect->export_for_template($OUTPUT);
echo $OUTPUT->render_from_template('forumreport_summary/bulk_action_menu', $data);
}
}

View File

@ -30,7 +30,7 @@ if (isguestuser()) {
$courseid = required_param('courseid', PARAM_INT);
$forumid = required_param('forumid', PARAM_INT);
$perpage = optional_param('perpage', 25, PARAM_INT);
$perpage = optional_param('perpage', \forumreport_summary\summary_table::DEFAULT_PER_PAGE, PARAM_INT);
$filters = [];
// Establish filter values.
@ -78,10 +78,10 @@ $PAGE->set_heading($course->fullname);
$PAGE->navbar->add(get_string('nodetitle', "forumreport_summary"));
// Prepare and display the report.
$bulkoperations = !$download && !empty($CFG->messaging) && has_capability('moodle/course:bulkmessaging', $context);
$allowbulkoperations = !$download && !empty($CFG->messaging) && has_capability('moodle/course:bulkmessaging', $context);
$canseeprivatereplies = has_capability('mod/forum:readprivatereplies', $context);
$table = new \forumreport_summary\summary_table($courseid, $filters, $bulkoperations, $canseeprivatereplies);
$table = new \forumreport_summary\summary_table($courseid, $filters, $allowbulkoperations, $canseeprivatereplies, $perpage);
$table->baseurl = $url;
if ($download) {
@ -99,11 +99,6 @@ if ($download) {
echo $renderer->render_filters_form($cm, $url, $filters);
$table->show_download_buttons_at(array(TABLE_P_BOTTOM));
echo $renderer->render_summary_table($table, $perpage);
if ($bulkoperations) {
echo $renderer->render_bulk_action_menu();
}
echo $renderer->render_summary_table($table);
echo $OUTPUT->footer();
}

View File

@ -53,55 +53,19 @@ class forumreport_summary_renderer extends plugin_renderer_base {
* Render the summary report table.
*
* @param summary_table $table The summary table to be rendered.
* @param int $perpage Number of results to render per page.
* @return string The report table HTML.
*/
public function render_summary_table(summary_table $table, int $perpage): string {
public function render_summary_table(summary_table $table): string {
// Buffer so calling script can output the report as required.
ob_start();
// Render table.
$table->out($perpage, false);
$table->out($table->get_perpage(), false);
$tablehtml = ob_get_contents();
ob_end_clean();
return $this->render_from_template('forumreport_summary/report', ['tablehtml' => $tablehtml, 'placeholdertext' => false]);
}
/**
* Render the bulk action menu for the forum summary report.
* @return string
*/
public function render_bulk_action_menu(): string {
$data = new stdClass();
$data->id = 'formactionid';
$data->attributes = [
[
'name' => 'data-action',
'value' => 'toggle'
],
[
'name' => 'data-togglegroup',
'value' => 'summaryreport-table'
],
[
'name' => 'data-toggle',
'value' => 'action'
],
[
'name' => 'disabled',
'value' => true
]
];
$data->actions = [
[
'value' => '#messageselect',
'name' => get_string('messageselectadd')
]
];
return $this->render_from_template('forumreport_summary/bulk_action_menu', $data);
return $this->render_from_template('forumreport_summary/report', ['tablehtml' => $tablehtml]);
}
}

View File

@ -21,6 +21,7 @@
Example context (json):
{
"showbulkactions": true,
"id": "formactionid",
"attributes": [
{
@ -45,19 +46,33 @@
"name": "Send a message",
"value": "#messageselect"
}
]
],
"perpage" : "<div class='singleselect'></div>"
}
}}
<br /><div class="buttons"><div class="form-inline">
<label for="{{id}}">{{#str}}withselectedusers{{/str}}</label>
<select id="{{id}}" class="select custom-select ml-2" {{#attributes}}{{name}}="{{value}}" {{/attributes}}>
<option value="">{{#str}}choosedots{{/str}}</option>
{{#actions}}
<option value="{{value}}">{{name}}</option>
{{/actions}}
</select>
</div></div>
<br />
<div class="d-inline-block w-100">
{{#showbulkactions}}
<div class="buttons float-left">
<div class="form-inline pl-1">
<label for="{{id}}">{{#str}}withselectedusers{{/str}}</label>
<select id="{{id}}" class="select custom-select ml-4" {{#attributes}}{{name}}="{{value}}" {{/attributes}}>
<option value="">{{#str}}choosedots{{/str}}</option>
{{#actions}}
<option value="{{value}}">{{name}}</option>
{{/actions}}
</select>
</div>
</div>
{{/showbulkactions}}
<div class="float-right">
{{#perpage}}
{{> core/single_select}}
{{/perpage}}
</div>
</div>
{{#js}}
require(['jquery', 'core_message/message_send_bulk'], function($, BulkSender) {
$('#{{id}}').on('change', function(e) {

View File

@ -21,19 +21,13 @@
Example context (json):
{
"placeholdertext": "To generate the summary report, set any filter values required, then select \"Generate report\".",
"tablehtml": false
"tablehtml": "TableSQL format HTML for the table"
}
}}
{{! The placeholder text, used before the report has been generated }}
{{^tablehtml}}<div class='d-block p-b-10'><h3>{{placeholdertext}}</h3></div>{{/tablehtml}}
{{#tablehtml}}
<div class="text-center">
{{> core/loading }}
</div>
<div id="summaryreport" class="hidden">
{{{tablehtml}}}
</div>
{{/tablehtml}}
<div class="text-center">
{{> core/loading }}
</div>
<div id="summaryreport" class="hidden">
{{{tablehtml}}}
</div>