Skip to content

Commit

Permalink
MDL-63280 core_message: Create group conversation from group edit page
Browse files Browse the repository at this point in the history
Allows linking of course groups with conversations when group messaging feature is enabled.
  • Loading branch information
cescobedo authored and mdjnelson committed Oct 25, 2018
1 parent 55fda00 commit e7f4671
Show file tree
Hide file tree
Showing 7 changed files with 392 additions and 3 deletions.
17 changes: 16 additions & 1 deletion group/group_form.php
Expand Up @@ -66,6 +66,12 @@ function definition () {
$mform->addHelpButton('enrolmentkey', 'enrolmentkey', 'group');
$mform->setType('enrolmentkey', PARAM_RAW);

// Group conversation messaging.
if (\core_message\api::can_create_group_conversation($USER->id, $coursecontext)) {
$mform->addElement('selectyesno', 'enablemessaging', get_string('enablemessaging', 'group'));
$mform->addHelpButton('enablemessaging', 'enablemessaging', 'group');
}

$mform->addElement('static', 'currentpicture', get_string('currentpicture'));

$mform->addElement('checkbox', 'deletepicture', get_string('delete'));
Expand Down Expand Up @@ -94,9 +100,18 @@ public function definition_after_data() {

$mform = $this->_form;
$groupid = $mform->getElementValue('id');
$coursecontext = context_course::instance($COURSE->id);

if ($group = $DB->get_record('groups', array('id' => $groupid))) {

// If can create group conversation then get if a conversation area exists and it is enabled.
if ($mform->elementExists('enablemessaging')) {
if (\core_message\helper::get_does_conversation_area_enabled('core_group',
'groups',
$groupid,
$coursecontext->id)) {
$mform->getElement('enablemessaging')->setSelected(1);
}
}
// Print picture.
if (!($pic = print_group_picture($group, $COURSE->id, true, true, false))) {
$pic = get_string('none');
Expand Down
41 changes: 39 additions & 2 deletions group/lib.php
Expand Up @@ -233,7 +233,7 @@ function groups_remove_member($grouporid, $userorid) {
* @return id of group or false if error
*/
function groups_create_group($data, $editform = false, $editoroptions = false) {
global $CFG, $DB;
global $CFG, $DB, $USER;

//check that courseid exists
$course = $DB->get_record('course', array('id' => $data->courseid), '*', MUST_EXIST);
Expand Down Expand Up @@ -275,6 +275,18 @@ function groups_create_group($data, $editform = false, $editoroptions = false) {
// Invalidate the grouping cache for the course
cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($course->id));

// Group conversation messaging.
if (\core_message\api::can_create_group_conversation($USER->id, $context)) {
if (!empty($data->enablemessaging)) {
\core_message\api::create_conversation_area('core_group',
'groups',
$group->id,
$context->id,
1,
$group->name);
}
}

// Trigger group event.
$params = array(
'context' => $context,
Expand Down Expand Up @@ -383,7 +395,7 @@ function groups_update_group_icon($group, $data, $editform) {
* @return bool true or exception
*/
function groups_update_group($data, $editform = false, $editoroptions = false) {
global $CFG, $DB;
global $CFG, $DB, $USER;

$context = context_course::instance($data->courseid);

Expand Down Expand Up @@ -413,6 +425,31 @@ function groups_update_group($data, $editform = false, $editoroptions = false) {
groups_update_group_icon($group, $data, $editform);
}

// Group conversation messaging.
if (\core_message\api::can_create_group_conversation($USER->id, $context)) {
if ($conversationarea = \core_message\api::get_conversation_area('core_group',
'groups',
$group->id,
$context->id)) {
if ($data->enablemessaging && $data->enablemessaging != $conversationarea->enabled) {
\core_message\api::enable_conversation_area($conversationarea->id);
}
if (!$data->enablemessaging && $data->enablemessaging != $conversationarea->enabled) {
\core_message\api::disable_conversation_area($conversationarea->id);
}
\core_message\api::update_conversation_name($conversationarea->conversationid, $group->name);
} else {
if (!empty($data->enablemessaging)) {
\core_message\api::create_conversation_area('core_group',
'groups',
$group->id,
$context->id,
1,
$group->name);
}
}
}

// Trigger group event.
$params = array(
'context' => $context,
Expand Down
128 changes: 128 additions & 0 deletions group/tests/lib_test.php
Expand Up @@ -528,4 +528,132 @@ public function test_groups_create_autogroups () {
}
$this->assertEquals(2, $DB->count_records('groups_members', array('groupid' => $group6->id)));
}

/**
* Test groups_create_group enabling a group of conversation.
*/
public function groups_create_group_with_conversation_area() {
global $DB;

$this->resetAfterTest();
$this->setAdminUser();
$course1 = $this->getDataGenerator()->create_course();
$coursecontext1 = context_course::instance($course1->id);
// Check not exists and conversation area created.
$this->assertEquals(
0,
$DB->count_records_sql("SELECT COUNT(ca.id)
FROM {message_conversation_area} ca
WHERE ca.contextid = ?
AND ca.component = 'core_group'
AND ca.itemtype = 'groups'", [$coursecontext1->id])
);
// Create two groups and only one group with enablemessaging = 1.
$group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
$group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0));
// Check exist only one row created in conversation area.
$this->assertEquals(
1,
$DB->count_records_sql("SELECT COUNT(ca.id)
FROM {message_conversation_area} ca
WHERE ca.contextid = ?
AND ca.component = 'core_group'
AND ca.itemtype = 'groups'
AND ca.enabled = 1", [$coursecontext1->id])
);
$conversation = $DB->get_record_sql("SELECT c.*
FROM {message_conversations} c
JOIN {message_conversation_area} ca ON c.id = ca.conversationid
WHERE ca.contextid = ?
AND ca.component = 'core_group'
AND ca.itemtype = 'groups'", [$coursecontext1->id]);
// Check group name and course fullname was stored in conversation.
$this->assertEquals($group1a->name, $conversation->name);
// Check groupid was stored in itemid on conversation area.
$this->assertEquals(
$group1a->id,
$DB->get_field_sql("SELECT ca.itemid
FROM {message_conversation_area} ca
WHERE ca.contextid = ?
AND ca.component = 'core_group'
AND ca.itemtype = 'groups'
AND ca.conversationid = ?", [$coursecontext1->id, $conversation->id])
);
}

/**
* Test groups_update_group enabling and disabling a group of conversation.
*/
public function test_groups_update_group_conversation_area() {
global $DB;

$this->resetAfterTest();
$this->setAdminUser();
$course1 = $this->getDataGenerator()->create_course();
$coursecontext1 = context_course::instance($course1->id);
// Check not exists and conversation area created.
$this->assertEquals(
0,
$DB->count_records_sql("SELECT COUNT(ca.id)
FROM {message_conversation_area} ca
WHERE ca.contextid = ?
AND ca.component = 'core_group'
AND ca.itemtype = 'groups'", [$coursecontext1->id])
);
// Create two groups and only one group with enablemessaging = 1.
$group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
$group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 0));
// Check exist only one row created in conversation area.
$this->assertEquals(
1,
$DB->count_records_sql("SELECT COUNT(ca.id)
FROM {message_conversation_area} ca
WHERE ca.contextid = ?
AND ca.component = 'core_group'
AND ca.itemtype = 'groups'
AND ca.enabled = 1", [$coursecontext1->id])
);
// Check that the conversation area is created when group messaging is enabled in the course group.
$group1b->enablemessaging = 1;
groups_update_group($group1b);
$this->assertEquals(
2,
$DB->count_records_sql("SELECT COUNT(ca.id)
FROM {message_conversation_area} ca
WHERE ca.contextid = ?
AND ca.component = 'core_group'
AND ca.itemtype = 'groups'
AND ca.enabled = 1", [$coursecontext1->id])
);
$conversation1b = $DB->get_record_sql("SELECT c.*
FROM {message_conversations} c
JOIN {message_conversation_area} ca ON c.id = ca.conversationid
WHERE ca.contextid = ?
AND ca.itemid = ?
AND ca.component = 'core_group'
AND ca.itemtype = 'groups'", [$coursecontext1->id, $group1b->id]);
// Check for group1b that group name was stored in conversation.
$this->assertEquals($group1b->name, $conversation1b->name);
// Check how to is disabled conversation area when group messaging is disabled in the course group.
$this->assertEquals(1, $DB->get_field("message_conversation_area", "enabled",
['itemid' => $group1b->id,
'contextid' => $coursecontext1->id]));
$group1b->enablemessaging = 0;
groups_update_group($group1b);
$this->assertEquals(0, $DB->get_field("message_conversation_area", "enabled",
['itemid' => $group1b->id,
'contextid' => $coursecontext1->id]));
// Check that the name of the conversation is changed when the name of the course group is updated.
$group1b->name = 'New group name';
$this->assertNotEquals($group1b->name, $conversation1b->name);
groups_update_group($group1b);
$conversation1b = $DB->get_record_sql("SELECT c.*
FROM {message_conversations} c
JOIN {message_conversation_area} ca ON c.id = ca.conversationid
WHERE ca.contextid = ?
AND ca.itemid = ?
AND ca.component = 'core_group'
AND ca.itemtype = 'groups'", [$coursecontext1->id, $group1b->id]);
$this->assertEquals($group1b->name, $conversation1b->name);
}
}
2 changes: 2 additions & 0 deletions lang/en/group.php
Expand Up @@ -57,6 +57,8 @@
$string['editgroupingsettings'] = 'Edit grouping settings';
$string['editgroupsettings'] = 'Edit group settings';
$string['editusersgroupsa'] = 'Edit groups for "{$a}"';
$string['enablemessaging'] = 'Group messaging';
$string['enablemessaging_help'] = 'If enabled, group members can send messages to the others in their group via the messaging drawer.';
$string['enrolmentkey'] = 'Enrolment key';
$string['enrolmentkey_help'] = 'An enrolment key enables access to the course to be restricted to only those who know the key. If a group enrolment key is specified, then not only will entering that key let the user into the course, but it will also automatically make them a member of this group.
Expand Down
71 changes: 71 additions & 0 deletions message/classes/api.php
Expand Up @@ -2011,4 +2011,75 @@ public static function create_conversation_area(string $component,

return $conversationarea;
}

/**
* Get 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.
* @return object message_conversation_area.
*/
public static function get_conversation_area(string $component,
string $itemtype,
int $itemid,
int $contextid) {
global $DB;

return $DB->get_record('message_conversation_area', ['itemid' => $itemid,
'contextid' => $contextid,
'component' => $component,
'itemtype' => $itemtype]);
}

/**
* Enable a conversation area.
*
* @param int $conversationareaid The id of a conversation area.
* @return void
*/
public static function enable_conversation_area(int $conversationareaid) {
global $DB;

$conversationarea = new \stdClass();
$conversationarea->id = $conversationareaid;
$conversationarea->enabled = 1;
$conversationarea->timeread = time();
$DB->update_record('message_conversation_area', $conversationarea);
}

/**
* Disable a conversation area.
*
* @param int $conversationareaid The id of a conversation area.
* @return void
*/
public static function disable_conversation_area(int $conversationareaid) {
global $DB;

$conversationarea = new \stdClass();
$conversationarea->id = $conversationareaid;
$conversationarea->enabled = 0;
$conversationarea->timeread = time();
$DB->update_record('message_conversation_area', $conversationarea);
}

/**
* Update the name of a conversation.
*
* @param int $conversationid The id of a conversation.
* @param string $name The main name of the area
* @return void
*/
public static function update_conversation_name(int $conversationid, string $name) {
global $DB;

if ($conversation = $DB->get_record('message_conversations', array('id' => $conversationid))) {
if ($name <> $conversation->name) {
$conversation->name = $name;
$DB->update_record('message_conversations', $conversation);
}
}
}
}
22 changes: 22 additions & 0 deletions message/classes/helper.php
Expand Up @@ -354,4 +354,26 @@ public static function legacy_messages_exist($userid) {

return $messageexists || $messagereadexists;
}

/**
* Get if exists a conversation area and is enabled.
*
* @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.
* @return bool Returns if a conversation area exists and is enabled, false otherwise
*/
public static function get_does_conversation_area_enabled(string $component,
string $itemtype,
int $itemid,
int $contextid) : bool {
global $DB;

return $DB->record_exists('message_conversation_area', ['itemid' => $itemid,
'contextid' => $contextid,
'component' => $component,
'itemtype' => $itemtype,
'enabled' => 1]);
}
}

0 comments on commit e7f4671

Please sign in to comment.