Skip to content

Commit

Permalink
MDL-27548 comments: component now recorded
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Nov 26, 2014
1 parent ca0e301 commit 686d056
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 14 deletions.
82 changes: 72 additions & 10 deletions comment/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ class comment {
private $courseid;
/** @var stdClass course module object, only be used to help find pluginname automatically */
private $cm;
/** @var string The component that this comment is for. It is STRONGLY recommended to set this. */
/**
* The component that this comment is for.
*
* It is STRONGLY recommended to set this.
* Added as a database field in 2.9, old comments will have a null component.
*
* @var string
*/
private $component;
/** @var string This is calculated by normalising the component */
private $pluginname;
Expand Down Expand Up @@ -241,10 +248,11 @@ public static function init(moodle_page $page = null) {
}
// setup variables for non-js interface
self::$nonjs = optional_param('nonjscomment', '', PARAM_ALPHANUM);
self::$comment_itemid = optional_param('comment_itemid', '', PARAM_INT);
self::$comment_itemid = optional_param('comment_itemid', '', PARAM_INT);
self::$comment_component = optional_param('comment_component', '', PARAM_COMPONENT);
self::$comment_context = optional_param('comment_context', '', PARAM_INT);
self::$comment_page = optional_param('comment_page', '', PARAM_INT);
self::$comment_area = optional_param('comment_area', '', PARAM_AREA);
self::$comment_page = optional_param('comment_page', '', PARAM_INT);
self::$comment_area = optional_param('comment_area', '', PARAM_AREA);

$page->requires->string_for_js('addcomment', 'moodle');
$page->requires->string_for_js('deletecomment', 'moodle');
Expand All @@ -259,6 +267,7 @@ public static function init(moodle_page $page = null) {
* invalidates permission checks.
* A coding_error is now thrown if code attempts to change the component.
*
* @throws coding_exception if you try to change the component after it has been set.
* @param string $component
*/
public function set_component($component) {
Expand Down Expand Up @@ -320,6 +329,7 @@ public function get_nojslink(moodle_page $page = null) {
'nonjscomment' => true,
'comment_itemid' => $this->itemid,
'comment_context' => $this->context->id,
'comment_component' => $this->get_component(),
'comment_area' => $this->commentarea,
));
$link->remove_params(array('comment_page'));
Expand Down Expand Up @@ -525,10 +535,19 @@ public function get_comments($page = '') {
$perpage = (!empty($CFG->commentsperpage))?$CFG->commentsperpage:15;
$start = $page * $perpage;
$ufields = user_picture::fields('u');

list($componentwhere, $component) = $this->get_component_select_sql('c');
if ($component) {
$params['component'] = $component;
}

$sql = "SELECT $ufields, c.id AS cid, c.content AS ccontent, c.format AS cformat, c.timecreated AS ctimecreated
FROM {comments} c
JOIN {user} u ON u.id = c.userid
WHERE c.contextid = :contextid AND c.commentarea = :commentarea AND c.itemid = :itemid
WHERE c.contextid = :contextid AND
c.commentarea = :commentarea AND
c.itemid = :itemid AND
$componentwhere
ORDER BY c.timecreated DESC";
$params['contextid'] = $this->contextid;
$params['commentarea'] = $this->commentarea;
Expand Down Expand Up @@ -568,6 +587,25 @@ public function get_comments($page = '') {
return $comments;
}

/**
* Returns an SQL fragment and param for selecting on component.
* @param string $alias
* @return array
*/
protected function get_component_select_sql($alias = '') {
$component = $this->get_component();
if ($alias) {
$alias = $alias.'.';
}
if (empty($component)) {
$componentwhere = "{$alias}component IS NULL";
$component = null;
} else {
$componentwhere = "({$alias}component IS NULL OR {$alias}component = :component)";
}
return array($componentwhere, $component);
}

/**
* Returns the number of comments associated with the details of this object
*
Expand All @@ -577,7 +615,18 @@ public function get_comments($page = '') {
public function count() {
global $DB;
if ($this->totalcommentcount === null) {
$this->totalcommentcount = $DB->count_records('comments', array('itemid' => $this->itemid, 'commentarea' => $this->commentarea, 'contextid' => $this->context->id));
list($where, $component) = $this->get_component_select_sql();
$where .= ' AND itemid = :itemid AND commentarea = :commentarea AND contextid = :contextid';
$params = array(
'itemid' => $this->itemid,
'commentarea' => $this->commentarea,
'contextid' => $this->context->id,
);
if ($component) {
$params['component'] = $component;
}

$this->totalcommentcount = $DB->count_records_select('comments', $where, $params);
}
return $this->totalcommentcount;
}
Expand Down Expand Up @@ -636,6 +685,7 @@ public function add($content, $format = FORMAT_MOODLE) {
$newcmt->contextid = $this->contextid;
$newcmt->commentarea = $this->commentarea;
$newcmt->itemid = $this->itemid;
$newcmt->component = !empty($this->component) ? $this->component : null;
$newcmt->content = $content;
$newcmt->format = $format;
$newcmt->userid = $USER->id;
Expand Down Expand Up @@ -780,10 +830,11 @@ public function print_comments($page = 0, $return = true, $nonjs = true) {
return '';
}

$html = '';
if (!(self::$comment_itemid == $this->itemid &&
self::$comment_context == $this->context->id &&
self::$comment_area == $this->commentarea)) {
self::$comment_area == $this->commentarea &&
self::$comment_component == $this->component
)) {
$page = 0;
}
$comments = $this->get_comments($page);
Expand Down Expand Up @@ -913,13 +964,24 @@ public function can_delete($commentid) {
}

/**
* Returns the component associated with the comment
* Returns the component associated with the comment.
*
* @return string
*/
public function get_compontent() {
public function get_component() {
return $this->component;
}

/**
* Do not call! I am a deprecated method because of the typo in my name.
* @deprecated since 2.9
* @see comment::get_component()
* @return string
*/
public function get_compontent() {
return $this->get_component();
}

/**
* Returns the context associated with the comment
* @return stdClass
Expand Down
2 changes: 1 addition & 1 deletion comment/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function get_comments($page) {
$comments = array();

$usernamefields = get_all_user_name_fields(true, 'u');
$sql = "SELECT c.id, c.contextid, c.itemid, c.commentarea, c.userid, c.content, $usernamefields, c.timecreated
$sql = "SELECT c.id, c.contextid, c.itemid, c.component, c.commentarea, c.userid, c.content, $usernamefields, c.timecreated
FROM {comments} c
JOIN {user} u
ON u.id=c.userid
Expand Down
5 changes: 3 additions & 2 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20141017" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20141113" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -2479,6 +2479,7 @@
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="component" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="The plugin this comment belongs to."/>
<FIELD NAME="commentarea" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="itemid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="content" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
Expand Down Expand Up @@ -3081,4 +3082,4 @@
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>
15 changes: 15 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4058,5 +4058,20 @@ function xmldb_main_upgrade($oldversion) {
// Moodle v2.8.0 release upgrade line.
// Put any upgrade step following this.

if ($oldversion < 2014112800.00) {

// Define field component to be added to comments.
$table = new xmldb_table('comments');
$field = new xmldb_field('component', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'contextid');

// Conditionally launch add field component.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2014112800.00);
}

return true;
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

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

$version = 2014112000.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2014112800.00; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit 686d056

Please sign in to comment.