From 1ca4cdf3e983156608d28a5984bbdc1ad4d78e9e Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Thu, 16 Jul 2015 11:14:41 +0200 Subject: [PATCH] MDL-50853 mod_chat: New Web Service mod_chat_login_user --- lib/db/services.php | 1 + mod/chat/classes/external.php | 133 ++++++++++++++++++++++++++++++++++ mod/chat/db/services.php | 39 ++++++++++ mod/chat/version.php | 2 +- version.php | 2 +- 5 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 mod/chat/classes/external.php create mode 100644 mod/chat/db/services.php diff --git a/lib/db/services.php b/lib/db/services.php index 6afdfdd03a8..9ccdb84c2b4 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -1144,6 +1144,7 @@ $services = array( 'mod_page_view_page', 'mod_resource_view_resource', 'mod_folder_view_folder', + 'mod_chat_login_user', ), 'enabled' => 0, 'restrictedusers' => 0, diff --git a/mod/chat/classes/external.php b/mod/chat/classes/external.php new file mode 100644 index 00000000000..588d5eb25ec --- /dev/null +++ b/mod/chat/classes/external.php @@ -0,0 +1,133 @@ +. + +/** + * Chat external API + * + * @package mod_chat + * @category external + * @copyright 2015 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.0 + */ + +defined('MOODLE_INTERNAL') || die; + +require_once($CFG->libdir . '/externallib.php'); +require_once($CFG->dirroot . '/mod/chat/lib.php'); + +/** + * Chat external functions + * + * @package mod_chat + * @category external + * @copyright 2015 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.0 + */ +class mod_chat_external extends external_api { + + /** + * Returns description of method parameters + * + * @return external_function_parameters + * @since Moodle 3.0 + */ + public static function login_user_parameters() { + return new external_function_parameters( + array( + 'chatid' => new external_value(PARAM_INT, 'chat instance id'), + 'groupid' => new external_value(PARAM_INT, 'group id, 0 means that the function will determine the user group', + VALUE_DEFAULT, 0), + ) + ); + } + + /** + * Log the current user into a chat room in the given chat. + * + * @param int $chatid the chat instance id + * @param int $groupid the user group id + * @return array of warnings and the chat unique session id + * @since Moodle 3.0 + * @throws moodle_exception + */ + public static function login_user($chatid, $groupid = 0) { + global $DB; + + $params = self::validate_parameters(self::login_user_parameters(), + array( + 'chatid' => $chatid, + 'groupid' => $groupid + )); + $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); + + require_capability('mod/chat:chat', $context); + + 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; + } + } + + // Get the unique chat session id. + // Since we are going to use the chat via Web Service requests we set the ajax version (since it's the most similar). + if (!$chatsid = chat_login_user($chat->id, 'ajax', $groupid, $course)) { + throw moodle_exception('cantlogin', 'chat'); + } + + $result = array(); + $result['chatsid'] = $chatsid; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Returns description of method result value + * + * @return external_description + * @since Moodle 3.0 + */ + public static function login_user_returns() { + return new external_single_structure( + array( + 'chatsid' => new external_value(PARAM_ALPHANUM, 'unique chat session id'), + 'warnings' => new external_warnings() + ) + ); + } + +} diff --git a/mod/chat/db/services.php b/mod/chat/db/services.php new file mode 100644 index 00000000000..48dcba4c2ce --- /dev/null +++ b/mod/chat/db/services.php @@ -0,0 +1,39 @@ +. + +/** + * Chat external functions and service definitions. + * + * @package mod_chat + * @category external + * @copyright 2015 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.0 + */ + +defined('MOODLE_INTERNAL') || die; + +$functions = array( + + 'mod_chat_login_user' => array( + 'classname' => 'mod_chat_external', + 'methodname' => 'login_user', + 'description' => 'Log a user into a chat room in the given chat.', + 'type' => 'write', + 'capabilities' => 'mod/chat:chat' + ), + +); diff --git a/mod/chat/version.php b/mod/chat/version.php index 0d659b398ea..13c3965ef52 100644 --- a/mod/chat/version.php +++ b/mod/chat/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015051100; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2015051101; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2015050500; // Requires this Moodle version. $plugin->component = 'mod_chat'; // Full name of the plugin (used for diagnostics). $plugin->cron = 300; diff --git a/version.php b/version.php index 42e683c808d..faf17f0586e 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2015082800.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2015082800.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. -- 2.43.0