Skip to content

Commit

Permalink
MDL-66631 mod_forum: add date filters to export
Browse files Browse the repository at this point in the history
  • Loading branch information
lameze authored and rezaies committed Oct 18, 2019
1 parent c77052f commit 718d7a4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
6 changes: 6 additions & 0 deletions mod/forum/classes/form/export_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public function definition() {
];
$mform->addElement('autocomplete', 'discussionids', get_string('discussions', 'mod_forum'), $discussions, $options);

// Date fields.
$mform->addElement('date_time_selector', 'from', get_string('postsfrom', 'mod_forum'),
['optional' => true]);
$mform->addElement('date_time_selector', 'to', get_string('poststo', 'mod_forum'),
['optional' => true]);

// Export formats.
$formats = \core_plugin_manager::instance()->get_plugins_of_type('dataformat');
$options = [];
Expand Down
61 changes: 46 additions & 15 deletions mod/forum/classes/local/vaults/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,39 +157,70 @@ public function get_from_discussion_ids(
}

/**
* The method returns posts made by the supplied users in the supplied discussions.
* The method returns posts based on a set of filters.
*
* @param stdClass $user Only used when restricting private replies
* @param int[] $discussionids The list of discussion ids to load posts for
* @param int[] $userids Only return posts made by these users
* @param array $filters Export filters, valid filters are:
*
* 'discussionids' => array of discussion ids eg [1,2,3]
* 'userids' => array of user ids eg [1,2,3]
* 'from' => timestamp to filter posts from this date.
* 'to' => timestamp to filter posts till this date.
*
* @param bool $canseeprivatereplies Whether this user can see all private replies or not
* @param string $orderby Order the results
* @return post_entity[]
*/
public function get_from_discussion_ids_and_user_ids(
public function get_from_filters(
stdClass $user,
array $discussionids,
array $userids,
array $filters,
bool $canseeprivatereplies,
string $orderby = ''
): array {
if (empty($discussionids) || empty($userids)) {
if (count($filters) == 0) {
return [];
}

$wheresql = [];
$params = [];
$alias = $this->get_table_alias();

list($indiscussionssql, $indiscussionsparams) = $this->get_db()->get_in_or_equal($discussionids, SQL_PARAMS_NAMED);
list($inuserssql, $inusersparams) = $this->get_db()->get_in_or_equal($userids, SQL_PARAMS_NAMED);
// Filter by discussion ids.
if (!empty($filters['discussionids'])) {
list($indiscussionssql, $indiscussionsparams) = $this->get_db()->get_in_or_equal($filters['discussionids'],
SQL_PARAMS_NAMED);
$wheresql[] = "{$alias}.discussion {$indiscussionssql}";
$params += $indiscussionsparams;
}

// Filter by user ids.
if (!empty($filters['userids'])) {
list($inuserssql, $inusersparams) = $this->get_db()->get_in_or_equal($filters['userids'],
SQL_PARAMS_NAMED);
$wheresql[] = "{$alias}.userid {$inuserssql}";
$params += $inusersparams;
}

// Filter posts by from and to dates.
if (isset($filters['from'])) {
$wheresql[] = "{$alias}.created >= :from";
$params['from'] = $filters['from'];
}

if (isset($filters['to'])) {
$wheresql[] = "{$alias}.created < :to";
$params['to'] = $filters['to'];
}

// We need to build the WHERE here, because get_private_reply_sql returns the query with the AND clause.
$wheresql = implode(' AND ', $wheresql);

// Build private replies sql.
[
'where' => $privatewhere,
'params' => $privateparams,
] = $this->get_private_reply_sql($user, $canseeprivatereplies);

$wheresql = "{$alias}.discussion {$indiscussionssql}
AND {$alias}.userid {$inuserssql}
{$privatewhere}";
$wheresql .= "{$privatewhere}";
$params += $privateparams;

if ($orderby) {
$orderbysql = $alias . '.' . $orderby;
Expand All @@ -198,7 +229,7 @@ public function get_from_discussion_ids_and_user_ids(
}

$sql = $this->generate_get_records_sql($wheresql, $orderbysql);
$records = $this->get_db()->get_records_sql($sql, array_merge($indiscussionsparams, $inusersparams, $privateparams));
$records = $this->get_db()->get_records_sql($sql, $params);

return $this->transform_db_records_to_entities($records);
}
Expand Down
19 changes: 11 additions & 8 deletions mod/forum/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,19 @@
}, $discussions);
}

$filters = ['discussionids' => $discussionids];
if ($data->userids) {
$posts = $postvault->get_from_discussion_ids_and_user_ids($USER,
$discussionids,
$data->userids,
$capabilitymanager->can_view_any_private_reply($USER));
} else {
$posts = $postvault->get_from_discussion_ids($USER,
$discussionids,
$capabilitymanager->can_view_any_private_reply($USER));
$filters['userids'] = $data->userids;
}
if ($data->from) {
$filters['from'] = $data->from;
}
if ($data->to) {
$filters['to'] = $data->to;
}

// Retrieve posts based on the selected filters.
$posts = $postvault->get_from_filters($USER, $filters, $capabilitymanager->can_view_any_private_reply($USER));

$fields = ['id', 'discussion', 'parent', 'userid', 'created', 'modified', 'mailed', 'subject', 'message',
'messageformat', 'messagetrust', 'attachment', 'totalscore', 'mailnow', 'deleted', 'privatereplyto'];
Expand Down
2 changes: 2 additions & 0 deletions mod/forum/lang/en/forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,10 @@
$string['postrating2'] = 'Separate and connected';
$string['postrating3'] = 'Mostly connected knowing';
$string['posts'] = 'Posts';
$string['postsfrom'] = 'Posts from';
$string['postsmadebyuser'] = 'Posts made by {$a}';
$string['postsmadebyuserincourse'] = 'Posts made by {$a->fullname} in {$a->coursename}';
$string['poststo'] = 'Posts to';
$string['posttoforum'] = 'Post to forum';
$string['postupdated'] = 'Your post was updated';
$string['potentialsubscribers'] = 'Potential subscribers';
Expand Down

0 comments on commit 718d7a4

Please sign in to comment.