MDL-50853 mod_chat: New Web Service mod_chat_view_chat

This commit is contained in:
Juan Leyva 2015-07-27 14:00:11 +02:00
parent 8380bc7fc0
commit 5841b9d5d3
5 changed files with 103 additions and 13 deletions

View File

@ -1148,6 +1148,7 @@ $services = array(
'mod_chat_get_chat_users',
'mod_chat_send_chat_message',
'mod_chat_get_chat_latest_messages',
'mod_chat_view_chat',
),
'enabled' => 0,
'restrictedusers' => 0,

View File

@ -429,4 +429,68 @@ class mod_chat_external extends external_api {
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function view_chat_parameters() {
return new external_function_parameters(
array(
'chatid' => new external_value(PARAM_INT, 'chat instance id')
)
);
}
/**
* Trigger the course module viewed event and update the module completion status.
*
* @param int $chatid the chat instance id
* @return array of warnings and status result
* @since Moodle 3.0
* @throws moodle_exception
*/
public static function view_chat($chatid) {
global $DB, $CFG;
$params = self::validate_parameters(self::view_chat_parameters(),
array(
'chatid' => $chatid
));
$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);
// Call the url/lib API.
chat_view($chat, $course, $cm, $context);
$result = array();
$result['status'] = true;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 3.0
*/
public static function view_chat_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
'warnings' => new external_warnings()
)
);
}
}

View File

@ -60,4 +60,12 @@ $functions = array(
'capabilities' => 'mod/chat:chat'
),
'mod_chat_view_chat' => array(
'classname' => 'mod_chat_external',
'methodname' => 'view_chat',
'description' => 'Trigger the course module viewed event and update the module completion status.',
'type' => 'write',
'capabilities' => 'mod/chat:chat'
),
);

View File

@ -1323,3 +1323,31 @@ function chat_get_latest_messages($chatuser, $chatlasttime) {
return $DB->get_records_select('chat_messages_current', 'chatid = :chatid AND timestamp > :lasttime ' . $groupselect,
$params, 'timestamp ASC');
}
/**
* Mark the activity completed (if required) and trigger the course_module_viewed event.
*
* @param stdClass $chat chat object
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @since Moodle 3.0
*/
function chat_view($chat, $course, $cm, $context) {
// Trigger course_module_viewed event.
$params = array(
'context' => $context,
'objectid' => $chat->id
);
$event = \mod_chat\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('chat', $chat);
$event->trigger();
// Completion.
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
}

View File

@ -69,15 +69,8 @@ if (isguestuser()) {
exit;
}
// Log this request - the problem here is that the view page
// does not display the chat content which is actually in a new window.
$params = array(
'objectid' => $chat->id,
'context' => $context
);
$event = \mod_chat\event\course_module_viewed::create($params);
$event->add_record_snapshot('chat', $chat);
$event->trigger();
// Completion and trigger events.
chat_view($chat, $course, $cm, $context);
$strenterchat = get_string('enterchat', 'chat');
$stridle = get_string('idle', 'chat');
@ -87,10 +80,6 @@ $strnextsession = get_string('nextsession', 'chat');
$courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
$title = $courseshortname . ': ' . format_string($chat->name);
// Mark viewed by user (if required).
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
// Initialize $PAGE.
$PAGE->set_url('/mod/chat/view.php', array('id' => $cm->id));
$PAGE->set_title($title);