MDL-66298 forumreport_summary: Added ability to bulk message users

Part of MDL-66076.
This commit is contained in:
Shamim Rezaie 2019-09-06 04:31:33 +10:00 committed by Jun Pataleta
parent cc97038258
commit 0942e58c91
5 changed files with 197 additions and 4 deletions

View File

@ -89,9 +89,10 @@ 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?
*/
public function __construct(int $courseid, array $filters) {
global $USER;
public function __construct(int $courseid, array $filters, bool $bulkoperations) {
global $USER, $OUTPUT;
$forumid = $filters['forums'][0];
@ -107,7 +108,21 @@ class summary_table extends table_sql {
$this->courseid = intval($courseid);
$columnheaders = [
$columnheaders = [];
if ($bulkoperations) {
$mastercheckbox = new \core\output\checkbox_toggleall('summaryreport-table', true, [
'id' => 'select-all-users',
'name' => 'select-all-users',
'label' => get_string('selectall'),
'labelclasses' => 'sr-only',
'classes' => 'm-1',
'checked' => false
]);
$columnheaders['select'] = $OUTPUT->render($mastercheckbox);
}
$columnheaders += [
'fullname' => get_string('fullnameuser'),
'postcount' => get_string('postcount', 'forumreport_summary'),
'replycount' => get_string('replycount', 'forumreport_summary'),
@ -153,6 +168,27 @@ class summary_table extends table_sql {
return $filternames[$filtertype];
}
/**
* Generate the select column.
*
* @param \stdClass $data
* @return string
*/
public function col_select($data) {
global $OUTPUT;
$checkbox = new \core\output\checkbox_toggleall('summaryreport-table', false, [
'classes' => 'usercheckbox m-1',
'id' => 'user' . $data->userid,
'name' => 'user' . $data->userid,
'checked' => false,
'label' => get_string('selectitem', 'moodle', fullname($data)),
'labelclasses' => 'accesshide',
]);
return $OUTPUT->render($checkbox);
}
/**
* Generate the fullname column.
*

View File

@ -84,8 +84,15 @@ $renderer = $PAGE->get_renderer('forumreport_summary');
echo $renderer->render_filters_form($cm, $url, $filters);
// Prepare and display the report.
$table = new \forumreport_summary\summary_table($courseid, $filters);
$bulkoperations = !empty($CFG->messaging) && has_capability('moodle/course:bulkmessaging', $context);
$table = new \forumreport_summary\summary_table($courseid, $filters, $bulkoperations);
$table->baseurl = $url;
echo $renderer->render_summary_table($table, $perpage);
if ($bulkoperations) {
echo $renderer->render_bulk_action_menu();
}
echo $OUTPUT->footer();

View File

@ -69,4 +69,39 @@ class forumreport_summary_renderer extends plugin_renderer_base {
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);
}
}

View File

@ -0,0 +1,31 @@
<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>
{{#js}}
require(['jquery', 'core_message/message_send_bulk'], function($, BulkSender) {
$('#{{id}}').on('change', function(e) {
var action = $(e.target).val();
if (action.indexOf('#') !== -1) {
e.preventDefault();
var ids = $('#summaryreport input.usercheckbox:checked').map(function(index, element) {
return element.name.replace('user', '');
}).get();
if (action == '#messageselect') {
BulkSender.showModal(ids, function() {
$('#{{id}}').focus();
});
}
$('#{{id}} option[value=""]').prop('selected', 'selected');
}
});
});
{{/js}}

View File

@ -0,0 +1,84 @@
@mod @mod_forum @forumreport @forumreport_summary
Feature: Message users in the summary report
In order to encourage users to participate
As a teacher
I should be able to send messages to those who are not taking part
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student1@example.com |
And the following "courses" exist:
| fullname | shortname |
| Course 1 | C1 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
| student2 | C1 | student |
And the following "activities" exist:
| activity | name | description | course | idnumber |
| forum | forum1 | C1 first forum | C1 | forum1 |
And the following forum discussions exist in course "Course 1":
| user | forum | name | message |
| teacher1 | forum1 | discussion1 | t1 earliest |
| teacher1 | forum1 | discussion2 | t1 between |
| student1 | forum1 | discussion3 | s1 latest |
And the following forum replies exist in course "Course 1":
| user | forum | discussion | message |
| teacher1 | forum1 | discussion1 | t1 between |
| teacher1 | forum1 | discussion2 | t1 latest |
| student1 | forum1 | discussion1 | s1 earliest |
@javascript
Scenario: Message some users
Given the following "users" exist:
| username | firstname | lastname | email |
| student3 | Student | 3 | student3@example.com |
And the following "course enrolments" exist:
| user | course | role |
| student3 | C1 | student |
When I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "forum1"
And I navigate to "Summary report" in current page administration
And I click on "Select 'Student 1'" "checkbox"
And I click on "Select 'Student 3'" "checkbox"
And I set the field "With selected users..." to "Send a message"
And I set the field "bulk-message" to "blah blah"
And I click on "Send message to 2 people" "button"
Then I should see "Message sent to 2 people"
And I log out
And I log in as "student1"
And I should see "1" in the "//*[@title='Toggle messaging drawer']/../*[@data-region='count-container']" "xpath_element"
And I log out
And I log in as "student3"
And I should see "1" in the "//*[@title='Toggle messaging drawer']/../*[@data-region='count-container']" "xpath_element"
And I log out
And I log in as "student2"
And I should not see "1" in the "//*[@title='Toggle messaging drawer']/../*[@data-region='count-container']" "xpath_element"
@javascript
Scenario: Message all users
When I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "forum1"
And I navigate to "Summary report" in current page administration
And I click on "Select all" "checkbox"
And I set the field "With selected users..." to "Send a message"
Then I should see "Send message to 3 people"
@javascript
Scenario: Ensure no message options when messaging is disabled
Given I log in as "admin"
And I set the following administration settings values:
| messaging | 0 |
And I log out
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "forum1"
And I navigate to "Summary report" in current page administration
Then I should not see "With selected users..."
And I should not see "Select all"