MDL-57394 mod_chat: New WS mod_chat_get_session_messages

This commit is contained in:
Juan Leyva
2018-01-30 14:30:51 +01:00
parent 2971d8421c
commit bf7a643f8a
5 changed files with 277 additions and 2 deletions
+106
View File
@@ -29,6 +29,8 @@ defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->dirroot . '/mod/chat/lib.php');
use mod_chat\external\chat_message_exporter;
/**
* Chat external functions
*
@@ -735,4 +737,108 @@ class mod_chat_external extends external_api {
)
);
}
/**
* 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()
)
);
}
}