Skip to content

Commit

Permalink
MDL-9469 mod_forum: add exportforum capability and checks
Browse files Browse the repository at this point in the history
Part of MDL-66075
  • Loading branch information
lameze committed Sep 20, 2019
1 parent f7b7f4e commit 75af28c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
10 changes: 10 additions & 0 deletions mod/forum/classes/local/managers/capability.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,14 @@ public function can_self_enrol(stdClass $user) : bool {

return $canstart;
}

/**
* Checks whether the user can export the whole forum (discussions and posts).
*
* @param stdClass $user The user object.
* @return bool True if the user can export the forum or false otherwise.
*/
public function can_export_forum(stdClass $user) : bool {
return has_capability('mod/forum:exportforum', $this->get_context(), $user);
}
}
11 changes: 11 additions & 0 deletions mod/forum/db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,17 @@
'manager' => CAP_ALLOW
)
),
'mod/forum:exportforum' => array(
'riskbitmask' => RISK_PERSONAL,

'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
)
),
'mod/forum:exportpost' => array(

'riskbitmask' => RISK_PERSONAL,
Expand Down
3 changes: 3 additions & 0 deletions mod/forum/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
}

$capabilitymanager = $managerfactory->get_capability_manager($forum);
if (!$capabilitymanager->can_export_forum($USER)) {
throw new moodle_exception('cannotexportforum', 'forum');
}

$course = $forum->get_course_record();
$coursemodule = $forum->get_course_module_record();
Expand Down
1 change: 1 addition & 0 deletions mod/forum/lang/en/forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
$string['cannotdeletepost'] = 'You can\'t delete this post!';
$string['cannotdeletediscussioninsinglediscussion'] = 'You cannot delete the first post in a single discussion';
$string['cannoteditposts'] = 'You can\'t edit other people\'s posts!';
$string['cannotexportforum'] = 'You cannot export this forum';
$string['cannotfinddiscussion'] = 'Could not find the discussion in this forum';
$string['cannotfindfirstpost'] = 'Could not find the first post in this forum';
$string['cannotfindorcreateforum'] = 'Could not find or create a main announcements forum for the site';
Expand Down
12 changes: 10 additions & 2 deletions mod/forum/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5245,6 +5245,11 @@ function forum_extend_settings_navigation(settings_navigation $settingsnav, navi
$PAGE->cm->context = context_module::instance($PAGE->cm->instance);
}

$vaultfactory = mod_forum\local\container::get_vault_factory();
$managerfactory = mod_forum\local\container::get_manager_factory();
$forumvault = $vaultfactory->get_forum_vault();
$forumentity = $forumvault->get_from_id($forumobject->id);

$params = $PAGE->url->params();
if (!empty($params['d'])) {
$discussionid = $params['d'];
Expand Down Expand Up @@ -5380,8 +5385,11 @@ function forum_extend_settings_navigation(settings_navigation $settingsnav, navi
$forumnode->add($string, $url, settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', ''));
}

$url = new moodle_url('/mod/forum/export.php', ['id' => $forumobject->id]);
$forumnode->add(get_string('export', 'mod_forum'), $url, navigation_node::TYPE_SETTING);
$capabilitymanager = $managerfactory->get_capability_manager($forumentity);
if ($capabilitymanager->can_export_forum($USER)) {
$url = new moodle_url('/mod/forum/export.php', ['id' => $forumobject->id]);
$forumnode->add(get_string('export', 'mod_forum'), $url, navigation_node::TYPE_SETTING);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion mod/forum/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2019052000; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2019052001; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2019051100; // Requires this Moodle version
$plugin->component = 'mod_forum'; // Full name of the plugin (used for diagnostics)

0 comments on commit 75af28c

Please sign in to comment.