MDL-50853 mod_chat: New Web Service mod_chat_login_user

This commit is contained in:
Juan Leyva 2015-07-16 11:14:41 +02:00
parent e28004e614
commit 1ca4cdf3e9
5 changed files with 175 additions and 2 deletions

View File

@ -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,

View File

@ -0,0 +1,133 @@
<?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()
)
);
}
}

39
mod/chat/db/services.php Normal file
View File

@ -0,0 +1,39 @@
<?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'
),
);

View File

@ -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;

View File

@ -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.