Skip to content

Commit

Permalink
MDL-372 forum: Improve pinning and update for Moodle 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Kelada authored and ryanwyllie committed Jan 13, 2016
1 parent 87b007b commit 5f219cf
Show file tree
Hide file tree
Showing 22 changed files with 400 additions and 147 deletions.
3 changes: 2 additions & 1 deletion blocks/news_items/block_news_items.php
Expand Up @@ -93,7 +93,8 @@ function get_content() {
// descending order. The call to default sort order here will use
// that unless the discussion that post is in has a timestart set
// in the future.
$sort = forum_get_default_sort_order(true, 'p.modified');
// This sort will ignore pinned posts as we want the most recent.
$sort = forum_get_default_sort_order(true, 'p.modified', 'd', false);
if (! $discussions = forum_get_discussions($cm, $sort, false,
$currentgroup, $this->page->course->newsitems) ) {
$text .= '('.get_string('nonews', 'forum').')';
Expand Down
21 changes: 21 additions & 0 deletions mod/forum/classes/event/discussion_pinned.php
Expand Up @@ -99,4 +99,25 @@ protected function validate_data() {
throw new \coding_exception('objectid must be set to the discussionid.');
}
}

/**
* Forum discussion object id mappings.
*
* @return array
*/
public static function get_objectid_mapping() {
return array('db' => 'forum_discussions', 'restore' => 'forum_discussion');
}

/**
* Forum id mappings.
*
* @return array
*/
public static function get_other_mapping() {
$othermapped = array();
$othermapped['forumid'] = array('db' => 'forum', 'restore' => 'forum');

return $othermapped;
}
}
21 changes: 21 additions & 0 deletions mod/forum/classes/event/discussion_unpinned.php
Expand Up @@ -99,4 +99,25 @@ protected function validate_data() {
throw new \coding_exception('objectid must be set to the discussionid.');
}
}

/**
* Forum discussion object id mappings.
*
* @return array
*/
public static function get_objectid_mapping() {
return array('db' => 'forum_discussions', 'restore' => 'forum_discussion');
}

/**
* Forum id mappings.
*
* @return array
*/
public static function get_other_mapping() {
$othermapped = array();
$othermapped['forumid'] = array('db' => 'forum', 'restore' => 'forum');

return $othermapped;
}
}
5 changes: 5 additions & 0 deletions mod/forum/classes/post_form.php
Expand Up @@ -138,6 +138,11 @@ function definition() {
$mform->addHelpButton('attachments', 'attachment', 'forum');
}

if (!$post->parent && has_capability('mod/forum:pindiscussions', $modcontext)) {
$mform->addElement('checkbox', 'pinned', get_string('discussionpinned', 'forum'));
$mform->addHelpButton('pinned', 'discussionpinned', 'forum');
}

if (empty($post->id) && $manageactivities) {
$mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
}
Expand Down
2 changes: 1 addition & 1 deletion mod/forum/db/access.php
Expand Up @@ -226,7 +226,7 @@

'mod/forum:pindiscussions' => array(

'captype' => 'read',
'captype' => 'write',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'teacher' => CAP_ALLOW,
Expand Down
2 changes: 1 addition & 1 deletion mod/forum/db/install.xml 100644 → 100755
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/forum/db" VERSION="20141028" COMMENT="XMLDB file for Moodle mod/forum"
<XMLDB PATH="mod/forum/db" VERSION="20160113" COMMENT="XMLDB file for Moodle mod/forum"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
Expand Down
31 changes: 16 additions & 15 deletions mod/forum/db/log.php
Expand Up @@ -26,21 +26,22 @@

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

global $DB; // TODO: this is a hack, we should really do something with the SQL in SQL tables
global $DB; // TODO: this is a hack, we should really do something with the SQL in SQL tables.

$logs = array(
array('module'=>'forum', 'action'=>'add', 'mtable'=>'forum', 'field'=>'name'),
array('module'=>'forum', 'action'=>'update', 'mtable'=>'forum', 'field'=>'name'),
array('module'=>'forum', 'action'=>'add discussion', 'mtable'=>'forum_discussions', 'field'=>'name'),
array('module'=>'forum', 'action'=>'add post', 'mtable'=>'forum_posts', 'field'=>'subject'),
array('module'=>'forum', 'action'=>'update post', 'mtable'=>'forum_posts', 'field'=>'subject'),
array('module'=>'forum', 'action'=>'user report', 'mtable'=>'user', 'field'=>$DB->sql_concat('firstname', "' '" , 'lastname')),
array('module'=>'forum', 'action'=>'move discussion', 'mtable'=>'forum_discussions', 'field'=>'name'),
array('module'=>'forum', 'action'=>'view subscribers', 'mtable'=>'forum', 'field'=>'name'),
array('module'=>'forum', 'action'=>'view discussion', 'mtable'=>'forum_discussions', 'field'=>'name'),
array('module'=>'forum', 'action'=>'view forum', 'mtable'=>'forum', 'field'=>'name'),
array('module'=>'forum', 'action'=>'subscribe', 'mtable'=>'forum', 'field'=>'name'),
array('module'=>'forum', 'action'=>'unsubscribe', 'mtable'=>'forum', 'field'=>'name'),
array('module'=>'forum', 'action'=>'pin discussion', 'mtable'=>'forum_discussions', 'field'=>'name'),
array('module'=>'forum', 'action'=>'unpin discussion', 'mtable'=>'forum_discussions', 'field'=>'name'),
array('module' => 'forum', 'action' => 'add', 'mtable' => 'forum', 'field' => 'name'),
array('module' => 'forum', 'action' => 'update', 'mtable' => 'forum', 'field' => 'name'),
array('module' => 'forum', 'action' => 'add discussion', 'mtable' => 'forum_discussions', 'field' => 'name'),
array('module' => 'forum', 'action' => 'add post', 'mtable' => 'forum_posts', 'field' => 'subject'),
array('module' => 'forum', 'action' => 'update post', 'mtable' => 'forum_posts', 'field' => 'subject'),
array('module' => 'forum', 'action' => 'user report', 'mtable' => 'user',
'field' => $DB->sql_concat('firstname', "' '", 'lastname')),
array('module' => 'forum', 'action' => 'move discussion', 'mtable' => 'forum_discussions', 'field' => 'name'),
array('module' => 'forum', 'action' => 'view subscribers', 'mtable' => 'forum', 'field' => 'name'),
array('module' => 'forum', 'action' => 'view discussion', 'mtable' => 'forum_discussions', 'field' => 'name'),
array('module' => 'forum', 'action' => 'view forum', 'mtable' => 'forum', 'field' => 'name'),
array('module' => 'forum', 'action' => 'subscribe', 'mtable' => 'forum', 'field' => 'name'),
array('module' => 'forum', 'action' => 'unsubscribe', 'mtable' => 'forum', 'field' => 'name'),
array('module' => 'forum', 'action' => 'pin discussion', 'mtable' => 'forum_discussions', 'field' => 'name'),
array('module' => 'forum', 'action' => 'unpin discussion', 'mtable' => 'forum_discussions', 'field' => 'name'),
);
26 changes: 13 additions & 13 deletions mod/forum/db/upgrade.php
Expand Up @@ -239,19 +239,6 @@ function xmldb_forum_upgrade($oldversion) {

// Moodle v2.8.0 release upgrade line.
// Put any upgrade step following this.
if ($oldversion < 2014112400) {

// Add support for pinned discussions.
$table = new xmldb_table('forum_discussions');
$field = new xmldb_field('pinned', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'timeend');

if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Forum savepoint reached.
upgrade_mod_savepoint(true, 2014112400, 'forum');
}

// Moodle v2.9.0 release upgrade line.
// Put any upgrade step following this.
Expand All @@ -266,5 +253,18 @@ function xmldb_forum_upgrade($oldversion) {
// Moodle v3.0.0 release upgrade line.
// Put any upgrade step following this.

if ($oldversion < 2015120800) {

// Add support for pinned discussions.
$table = new xmldb_table('forum_discussions');
$field = new xmldb_field('pinned', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'timeend');

if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Forum savepoint reached.
upgrade_mod_savepoint(true, 2015120800, 'forum');
}
return true;
}
25 changes: 10 additions & 15 deletions mod/forum/discuss.php
Expand Up @@ -32,7 +32,7 @@
$move = optional_param('move', 0, PARAM_INT); // If set, moves this discussion to another forum
$mark = optional_param('mark', '', PARAM_ALPHA); // Used for tracking read posts if user initiated.
$postid = optional_param('postid', 0, PARAM_INT); // Used for tracking read posts if user initiated.
$pin = optional_param('pin', -1, PARAM_INT); // If set, pin or unpin this discussion.
$pin = optional_param('pin', -1, PARAM_INT); // If set, pin or unpin this discussion.

$url = new moodle_url('/mod/forum/discuss.php', array('d'=>$d));
if ($parent !== 0) {
Expand Down Expand Up @@ -172,7 +172,6 @@

redirect($return.'&move=-1&sesskey='.sesskey());
}

// Pin or unpin discussion if requested.
if ($pin !== -1 && confirm_sesskey()) {
require_capability('mod/forum:pindiscussions', $modcontext);
Expand All @@ -181,19 +180,15 @@

switch ($pin) {
case FORUM_DISCUSSION_PINNED:
$DB->set_field('forum_discussions', 'pinned', $pin, array('id' => $discussion->id));
$event = \mod_forum\event\discussion_pinned::create($params);
$event->add_record_snapshot('forum_discussions', $discussion);
$event->trigger();
// Pin the discussion and trigger discussion pinned event.
forum_discussion_pin($modcontext, $forum, $discussion);
break;
case FORUM_DISCUSSION_UNPINNED:
$DB->set_field('forum_discussions', 'pinned', $pin, array('id' => $discussion->id));
$event = \mod_forum\event\discussion_unpinned::create($params);
$event->add_record_snapshot('forum_discussions', $discussion);
$event->trigger();
// Unpin the discussion and trigger discussion unpinned event.
forum_discussion_unpin($modcontext, $forum, $discussion);
break;
default:
echo $OUTPUT->notfication("Invalid value when attempting to pin/unpin discussion");
echo $OUTPUT->notification("Invalid value when attempting to pin/unpin discussion");
break;
}

Expand Down Expand Up @@ -299,7 +294,7 @@
echo $neighbourlinks;

/// Print the controls across the top
echo '<div class="discussioncontrols clearfix">';
echo '<div class="discussioncontrols clearfix"><div class="controlscontainer">';

if (!empty($CFG->enableportfolios) && has_capability('mod/forum:exportdiscussion', $modcontext)) {
require_once($CFG->libdir.'/portfoliolib.php');
Expand Down Expand Up @@ -374,8 +369,8 @@
echo html_writer::tag('div', $OUTPUT->render($button), array('class' => 'discussioncontrol pindiscussion'));
}

echo '<div class="clearfloat">&nbsp;</div>';
echo "</div>";

echo "</div></div>";

if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
$a = new stdClass();
Expand All @@ -386,7 +381,7 @@

if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) &&
!forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
echo $OUTPUT->notification(get_string('qandanotify','forum'));
echo $OUTPUT->notification(get_string('qandanotify', 'forum'));
}

if ($move == -1 and confirm_sesskey()) {
Expand Down
17 changes: 14 additions & 3 deletions mod/forum/externallib.php
Expand Up @@ -649,7 +649,7 @@ public static function get_forum_discussions_paginated($forumid, $sortby = 'time
// Check they have the view forum capability.
require_capability('mod/forum:viewdiscussion', $modcontext, null, true, 'noviewdiscussionspermission', 'forum');

$sort = 'd.' . $sortby . ' ' . $sortdirection;
$sort = 'd.pinned DESC, d.' . $sortby . ' ' . $sortdirection;
$alldiscussions = forum_get_discussions($cm, $sort, true, -1, -1, true, $page, $perpage, FORUM_POSTS_ALL_USER_GROUPS);

if ($alldiscussions) {
Expand Down Expand Up @@ -801,7 +801,8 @@ public static function get_forum_discussions_paginated_returns() {
'userpictureurl' => new external_value(PARAM_URL, 'Post author picture.'),
'usermodifiedpictureurl' => new external_value(PARAM_URL, 'Post modifier picture.'),
'numreplies' => new external_value(PARAM_TEXT, 'The number of replies in the discussion'),
'numunread' => new external_value(PARAM_INT, 'The number of unread discussions.')
'numunread' => new external_value(PARAM_INT, 'The number of unread discussions.'),
'pinned' => new external_value(PARAM_BOOL, 'Is the discussion pinned')
), 'post'
)
),
Expand Down Expand Up @@ -1115,6 +1116,7 @@ public static function add_discussion_parameters() {
'name' => new external_value(PARAM_ALPHANUM,
'The allowed keys (value format) are:
discussionsubscribe (bool); subscribe to the discussion?, default to true
discussionpinned (bool); is the discussion pinned, default to false
'),
'value' => new external_value(PARAM_RAW, 'The value of the option,
This param is validated in the external function.'
Expand Down Expand Up @@ -1151,14 +1153,18 @@ public static function add_discussion($forumid, $subject, $message, $groupid = -
));
// Validate options.
$options = array(
'discussionsubscribe' => true
'discussionsubscribe' => true,
'discussionpinned' => false
);
foreach ($params['options'] as $option) {
$name = trim($option['name']);
switch ($name) {
case 'discussionsubscribe':
$value = clean_param($option['value'], PARAM_BOOL);
break;
case 'discussionpinned':
$value = clean_param($option['value'], PARAM_BOOL);
break;
default:
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
}
Expand Down Expand Up @@ -1210,6 +1216,11 @@ public static function add_discussion($forumid, $subject, $message, $groupid = -
$discussion->name = $discussion->subject;
$discussion->timestart = 0;
$discussion->timeend = 0;
if (has_capability('mod/forum:pindiscussions', $context) && $options['discussionpinned']) {
$discussion->pinned = FORUM_DISCUSSION_PINNED;
} else {
$discussion->pinned = FORUM_DISCUSSION_UNPINNED;
}

if ($discussionid = forum_add_discussion($discussion)) {

Expand Down
3 changes: 2 additions & 1 deletion mod/forum/lang/en/forum.php
Expand Up @@ -139,7 +139,8 @@
$string['discussionnownotsubscribed'] = '{$a->name} will NOT be notified of new posts in \'{$a->discussion}\' of \'{$a->forum}\'';
$string['discussionnowsubscribed'] = '{$a->name} will be notified of new posts in \'{$a->discussion}\' of \'{$a->forum}\'';
$string['discussionpin'] = 'Pin';
$string['discussionpinned'] = 'Pinned: {$a}';
$string['discussionpinned'] = 'Pinned';
$string['discussionpinned_help'] = 'Pinned discussions will appear at the top of a forum.';
$string['discussionsubscribestop'] = 'I don\'t want to be notified of new posts in this discussion';
$string['discussionsubscribestart'] = 'Send me notifications of new posts in this discussion';
$string['discussionsubscription'] = 'Discussion subscription';
Expand Down

0 comments on commit 5f219cf

Please sign in to comment.