MDL-63280 core_message: Create conversation area
authorcescobedo <carlos.escobedo@gmail.com>
Mon, 22 Oct 2018 20:34:15 +0000 (22:34 +0200)
committerMark Nelson <markn@moodle.com>
Thu, 25 Oct 2018 04:20:00 +0000 (12:20 +0800)
lib/db/install.xml
lib/db/upgrade.php
message/classes/api.php
message/tests/api_test.php

index 360bf46..4e6290d 100644 (file)
         <INDEX NAME="convhash" UNIQUE="false" FIELDS="convhash"/>
       </INDEXES>
     </TABLE>
+    <TABLE NAME="message_conversation_area" COMMENT="Holds the information between Moodle components and conversations">
+      <FIELDS>
+        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
+        <FIELD NAME="conversationid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
+        <FIELD NAME="component" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false" COMMENT="Defines the Moodle component which the area was added to"/>
+        <FIELD NAME="itemtype" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false"/>
+        <FIELD NAME="itemid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
+        <FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The context id of the itemid or course of the itemtype was added"/>
+        <FIELD NAME="enabled" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
+        <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
+        <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="timemodified"/>
+      </FIELDS>
+      <KEYS>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
+        <KEY NAME="conversationid" TYPE="foreign" FIELDS="conversationid" REFTABLE="message_conversations" REFFIELDS="id"/>
+        <KEY NAME="contextid" TYPE="foreign" FIELDS="contextid" REFTABLE="context" REFFIELDS="id"/>
+      </KEYS>
+      <INDEXES>
+        <INDEX NAME="component-itemtype-contextid-itemid" UNIQUE="true" FIELDS="component, itemtype, contextid, itemid"/>
+      </INDEXES>
+    </TABLE>
     <TABLE NAME="message_conversation_members" COMMENT="Stores all members in a conversations">
       <FIELDS>
         <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
index d60b286..68b92a3 100644 (file)
@@ -2589,7 +2589,7 @@ function xmldb_main_upgrade($oldversion) {
         $table = new xmldb_table('message_conversations');
 
         // Remove the unique 'convhash' index, change to null and add a new non unique index.
-        $index = new xmldb_index('convhash', XMLDB_INDEX_UNIQUE, ['convhash']);
+        $index = new xmldb_index('convhash', XMLDB_INDEX_UNIQUE, array('convhash'));
         if ($dbman->index_exists($table, $index)) {
             $dbman->drop_index($table, $index);
         }
@@ -2597,7 +2597,7 @@ function xmldb_main_upgrade($oldversion) {
         $field = new xmldb_field('convhash', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'name');
         $dbman->change_field_notnull($table, $field);
 
-        $index = new xmldb_index('convhash', XMLDB_INDEX_NOTUNIQUE, ['convhash']);
+        $index = new xmldb_index('convhash', XMLDB_INDEX_NOTUNIQUE, array('convhash'));
         if (!$dbman->index_exists($table, $index)) {
             $dbman->add_index($table, $index);
         }
@@ -2605,5 +2605,31 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2018102200.00);
     }
 
+    if ($oldversion < 2018101900.03) {
+        // Create new 'message_conversation_area' table.
+        $table = new xmldb_table('message_conversation_area');
+        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
+        $table->add_field('conversationid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id');
+        $table->add_field('component',  XMLDB_TYPE_CHAR, '100', null, null, null, null, 'conversationid');
+        $table->add_field('itemtype',  XMLDB_TYPE_CHAR, '100', null, null, null, null, 'component');
+        $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'itemtype');
+        $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'itemid');
+        $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, 0, 'contextid');
+        $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'enable');
+        $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'timecreated');
+        $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id'], null, null);
+        $table->add_key('conversationid', XMLDB_KEY_FOREIGN, ['conversationid'], 'message_conversations', ['id']);
+        $table->add_key('contextid', XMLDB_KEY_FOREIGN, ['contextid'], 'context', ['id']);
+        $table->add_index('component-itemtype-contextid-itemid', XMLDB_INDEX_UNIQUE, ['component',
+                                                                                      'itemtype',
+                                                                                      'itemid',
+                                                                                      'contextid']);
+        if (!$dbman->table_exists($table)) {
+            $dbman->create_table($table);
+        }
+
+        upgrade_main_savepoint(true, 2018101900.03);
+    }
+
     return true;
 }
index 216050c..06c5d9c 100644 (file)
@@ -1975,4 +1975,40 @@ class api {
 
         return $DB->count_records('message_conversation_members', ['conversationid' => $convid]);
     }
+
+    /**
+     * Create message conversation area.
+     *
+     * @param string $component Defines the Moodle component which the area was added to
+     * @param string $itemtype Defines the type of the component
+     * @param int    $itemid The id of the component
+     * @param int    $contextid The id of the context
+     * @param int    $enabled Allow enabled or disabled the conversation area
+     * @param string $name The main name of the area
+     * @return \stdClass
+     */
+    public static function create_conversation_area(string $component,
+                                                    string $itemtype,
+                                                    int $itemid,
+                                                    int $contextid,
+                                                    int $enabled = 0,
+                                                    string $name) {
+        global $DB;
+
+        // Create a conversation.
+        $conversation = self::create_conversation(self::MESSAGE_CONVERSATION_TYPE_GROUP, array(), $name);
+        // Create a conversation area.
+        $conversationarea = new \stdClass;
+        $conversationarea->conversationid = $conversation->id;
+        $conversationarea->component      = !empty($component) ? $component : '';
+        $conversationarea->itemtype       = $itemtype;
+        $conversationarea->itemid         = $itemid;
+        $conversationarea->contextid      = $contextid;
+        $conversationarea->enabled        = $enabled;
+        $conversationarea->timecreated    = time();
+        $conversationarea->timemodified   = $conversationarea->timecreated;
+        $conversationarea->id = $DB->insert_record('message_conversation_area', $conversationarea);
+
+        return $conversationarea;
+    }
 }
index 15fba07..c196693 100644 (file)
@@ -3273,4 +3273,32 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
     protected static function sort_contacts($a, $b) {
         return $a->userid > $b->userid;
     }
+
+    /**
+     * Test create message conversation area.
+     */
+    public function test_create_conversation_area() {
+        global $DB;
+
+        $this->resetAfterTest();
+        $contextid = 111;
+        $itemid = 222;
+        $name = 'Name of conversation';
+        \core_message\api::create_conversation_area('core_group',
+                                                    'groups',
+                                                    $itemid,
+                                                    $contextid,
+                                                    1,
+                                                    $name);
+        $conversationarea = $DB->get_record('message_conversation_area', ['itemid'    => $itemid,
+                                                                          'contextid' => $contextid,
+                                                                          'component' => 'core_group',
+                                                                          'itemtype'  => 'groups']);
+        // Check if exist the new conversation area and if the conversation has been created with the name.
+        $this->assertEquals($itemid, $conversationarea->itemid);
+        $this->assertEquals(
+                $name,
+                $DB->get_field('message_conversations', 'name', ['id' => $conversationarea->conversationid])
+        );
+    }
 }