--- /dev/null
+<?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/>.
+
+/**
+ * Chat external API
+ *
+ * @package mod_chat
+ * @category external
+ * @copyright 2015 Juan Leyva <juan@moodle.com>
+ * @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 <juan@moodle.com>
+ * @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()
+ )
+ );
+ }
+
+}
--- /dev/null
+<?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/>.
+
+/**
+ * Chat external functions and service definitions.
+ *
+ * @package mod_chat
+ * @category external
+ * @copyright 2015 Juan Leyva <juan@moodle.com>
+ * @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'
+ ),
+
+);