Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'MDL-57394-master' of git://github.com/jleyva/moodle
  • Loading branch information
David Monllao authored and snake committed Feb 1, 2018
2 parents cdef2e7 + bf7a643 commit e57392f
Show file tree
Hide file tree
Showing 8 changed files with 686 additions and 94 deletions.
229 changes: 229 additions & 0 deletions mod/chat/classes/external.php
Expand Up @@ -29,6 +29,8 @@
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->dirroot . '/mod/chat/lib.php');

use mod_chat\external\chat_message_exporter;

/**
* Chat external functions
*
Expand Down Expand Up @@ -612,4 +614,231 @@ public static function get_chats_by_courses_returns() {
)
);
}

/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.4
*/
public static function get_sessions_parameters() {
return new external_function_parameters(
array(
'chatid' => new external_value(PARAM_INT, 'Chat instance id.'),
'groupid' => new external_value(PARAM_INT, 'Get messages from users in this group.
0 means that the function will determine the user group', VALUE_DEFAULT, 0),
'showall' => new external_value(PARAM_BOOL, 'Whether to show completed sessions or not.', VALUE_DEFAULT, false),
)
);
}

/**
* Retrieves chat sessions for a given chat.
*
* @param int $chatid the chat instance id
* @param int $groupid filter messages by this group. 0 to determine the group.
* @param bool $showall whether to include incomplete sessions or not
* @return array of warnings and the sessions
* @since Moodle 3.4
* @throws moodle_exception
*/
public static function get_sessions($chatid, $groupid = 0, $showall = false) {
global $DB;

$params = self::validate_parameters(self::get_sessions_parameters(),
array(
'chatid' => $chatid,
'groupid' => $groupid,
'showall' => $showall,
));
$sessions = $warnings = array();

// Request and permission validation.
$chat = $DB->get_record('chat', array('id' => $params['chatid']), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($chat, 'chat');

$context = context_module::instance($cm->id);
self::validate_context($context);

if (empty($chat->studentlogs) && !has_capability('mod/chat:readlog', $context)) {
throw new moodle_exception('nopermissiontoseethechatlog', 'chat');
}

if (!empty($params['groupid'])) {
$groupid = $params['groupid'];
// Determine is the group is visible to user.
if (!groups_group_visible($groupid, $course, $cm)) {
throw new moodle_exception('notingroup');
}
} else {
// Check to see if groups are being used here.
if ($groupmode = groups_get_activity_groupmode($cm)) {
$groupid = groups_get_activity_group($cm);
// Determine is the group is visible to user (this is particullary for the group 0).
if (!groups_group_visible($groupid, $course, $cm)) {
throw new moodle_exception('notingroup');
}
} else {
$groupid = 0;
}
}

$messages = chat_get_session_messages($chat->id, $groupid, 0, 0, 'timestamp DESC');
if ($messages) {
$chatsessions = chat_get_sessions($messages, $params['showall']);
// Format sessions for external.
foreach ($chatsessions as $session) {
$sessionusers = array();
foreach ($session->sessionusers as $sessionuser => $usermessagecount) {
$sessionusers[] = array(
'userid' => $sessionuser,
'messagecount' => $usermessagecount
);
}
$session->sessionusers = $sessionusers;
$sessions[] = $session;
}
}

$result = array();
$result['sessions'] = $sessions;
$result['warnings'] = $warnings;
return $result;
}

/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 3.4
*/
public static function get_sessions_returns() {
return new external_single_structure(
array(
'sessions' => new external_multiple_structure(
new external_single_structure(
array(
'sessionstart' => new external_value(PARAM_INT, 'Session start time.'),
'sessionend' => new external_value(PARAM_INT, 'Session end time.'),
'sessionusers' => new external_multiple_structure(
new external_single_structure(
array(
'userid' => new external_value(PARAM_INT, 'User id.'),
'messagecount' => new external_value(PARAM_INT, 'Number of messages in the session.'),
)
), 'Session users.'
),
'iscomplete' => new external_value(PARAM_BOOL, 'Whether the session is completed or not.'),
)
),
'list of users'
),
'warnings' => new external_warnings()
)
);
}

/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.4
*/
public static function get_session_messages_parameters() {
return new external_function_parameters(
array(
'chatid' => new external_value(PARAM_INT, 'Chat instance id.'),
'sessionstart' => new external_value(PARAM_INT, 'The session start time (timestamp).'),
'sessionend' => new external_value(PARAM_INT, 'The session end time (timestamp).'),
'groupid' => new external_value(PARAM_INT, 'Get messages from users in this group.
0 means that the function will determine the user group', VALUE_DEFAULT, 0),
)
);
}

/**
* Retrieves messages of the given chat session.
*
* @param int $chatid the chat instance id
* @param int $sessionstart the session start time (timestamp)
* @param int $sessionend the session end time (timestamp)
* @param int $groupid filter messages by this group. 0 to determine the group.
* @return array of warnings and the messages
* @since Moodle 3.4
* @throws moodle_exception
*/
public static function get_session_messages($chatid, $sessionstart, $sessionend, $groupid = 0) {
global $DB, $PAGE;

$params = self::validate_parameters(self::get_session_messages_parameters(),
array(
'chatid' => $chatid,
'sessionstart' => $sessionstart,
'sessionend' => $sessionend,
'groupid' => $groupid,
));
$messages = $warnings = array();

// Request and permission validation.
$chat = $DB->get_record('chat', array('id' => $params['chatid']), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($chat, 'chat');

$context = context_module::instance($cm->id);
self::validate_context($context);

if (empty($chat->studentlogs) && !has_capability('mod/chat:readlog', $context)) {
throw new moodle_exception('nopermissiontoseethechatlog', 'chat');
}

if (!empty($params['groupid'])) {
$groupid = $params['groupid'];
// Determine is the group is visible to user.
if (!groups_group_visible($groupid, $course, $cm)) {
throw new moodle_exception('notingroup');
}
} else {
// Check to see if groups are being used here.
if ($groupmode = groups_get_activity_groupmode($cm)) {
$groupid = groups_get_activity_group($cm);
// Determine is the group is visible to user (this is particullary for the group 0).
if (!groups_group_visible($groupid, $course, $cm)) {
throw new moodle_exception('notingroup');
}
} else {
$groupid = 0;
}
}

$messages = chat_get_session_messages($chat->id, $groupid, $params['sessionstart'], $params['sessionend'],
'timestamp ASC');
if ($messages) {
foreach ($messages as $message) {
$exporter = new chat_message_exporter($message, array('context' => $context));
$returneditems[] = $exporter->export($PAGE->get_renderer('core'));
}
}

$result = array(
'messages' => $messages,
'warnings' => $warnings,
);
return $result;
}

/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 3.4
*/
public static function get_session_messages_returns() {
return new external_single_structure(
array(
'messages' => new external_multiple_structure(
chat_message_exporter::get_read_structure()
),
'warnings' => new external_warnings()
)
);
}
}
101 changes: 101 additions & 0 deletions mod/chat/classes/external/chat_message_exporter.php
@@ -0,0 +1,101 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Class for exporting a chat message.
*
* @package mod_chat
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\external;
defined('MOODLE_INTERNAL') || die();

use core\external\exporter;

/**
* Class for exporting a chat message.
*
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class chat_message_exporter extends exporter {

/**
* Defines exporter properties.
*
* @return array
*/
protected static function define_properties() {
return array(
'id' => array(
'type' => PARAM_INT,
'description' => 'The message record id.',
),
'chatid' => array(
'type' => PARAM_INT,
'description' => 'The chat id.',
'default' => 0,
),
'userid' => array(
'type' => PARAM_INT,
'description' => 'The user who wrote the message.',
'default' => 0,
),
'groupid' => array(
'type' => PARAM_INT,
'description' => 'The group this message belongs to.',
'default' => 0,
),
'issystem' => array(
'type' => PARAM_BOOL,
'description' => 'Whether is a system message or not.',
'default' => false,
),
'message' => array(
'type' => PARAM_RAW,
'description' => 'The message text.',
),
'timestamp' => array(
'type' => PARAM_INT,
'description' => 'The message timestamp (indicates when the message was sent).',
'default' => 0,
),
);
}

/**
* Defines related information.
*
* @return array
*/
protected static function define_related() {
return array(
'context' => 'context',
);
}

/**
* Get the formatting parameters for the name.
*
* @return array
*/
protected function get_format_parameters_for_message() {
return [
'component' => 'mod_chat',
];
}
}
16 changes: 15 additions & 1 deletion mod/chat/db/services.php
Expand Up @@ -81,5 +81,19 @@
'type' => 'read',
'capabilities' => '',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
)
),
'mod_chat_get_sessions' => array(
'classname' => 'mod_chat_external',
'methodname' => 'get_sessions',
'description' => 'Retrieves chat sessions for a given chat.',
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
'mod_chat_get_session_messages' => array(
'classname' => 'mod_chat_external',
'methodname' => 'get_session_messages',
'description' => 'Retrieves messages of the given chat session.',
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
);

0 comments on commit e57392f

Please sign in to comment.