first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,111 @@
<?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/>.
/**
* Activity base class.
*
* @package mod_chat
* @copyright 2017 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\analytics\indicator;
defined('MOODLE_INTERNAL') || die();
/**
* Activity base class.
*
* @package mod_chat
* @copyright 2017 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class activity_base extends \core_analytics\local\indicator\community_of_inquiry_activity {
/**
* feedback_viewed_events
*
* @return string[]
*/
protected function feedback_viewed_events() {
return array('\mod_chat\event\course_module_viewed', '\mod_chat\event\message_sent',
'\mod_chat\event\sessions_viewed');
}
/**
* feedback_replied_events
*
* @return string[]
*/
protected function feedback_replied_events() {
return array('\mod_chat\event\message_sent');
}
/**
* feedback_post_action
*
* @param \cm_info $cm
* @param int $contextid
* @param int $userid
* @param string[] $eventnames
* @param int $after
* @return bool
*/
protected function feedback_post_action(\cm_info $cm, $contextid, $userid, $eventnames, $after = false) {
if (empty($this->activitylogs[$contextid][$userid])) {
return false;
}
$logs = $this->activitylogs[$contextid][$userid];
if (empty($logs['\mod_chat\event\message_sent'])) {
// No feedback viewed if there is no submission.
return false;
}
// First user message time.
$firstmessage = $logs['\mod_chat\event\message_sent']->timecreated[0];
// We consider feedback another user messages.
foreach ($this->activitylogs[$contextid] as $anotheruserid => $logs) {
if ($anotheruserid == $userid) {
continue;
}
if (empty($logs['\mod_chat\event\message_sent'])) {
continue;
}
$firstmessagesenttime = $logs['\mod_chat\event\message_sent']->timecreated[0];
if (parent::feedback_post_action($cm, $contextid, $userid, $eventnames, $firstmessagesenttime)) {
return true;
}
// Continue with the next user.
}
return false;
}
/**
* feedback_check_grades
*
* @return bool
*/
protected function feedback_check_grades() {
// Chat's feedback is not contained in grades.
return false;
}
}
@@ -0,0 +1,56 @@
<?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/>.
/**
* Cognitive depth indicator - chat.
*
* @package mod_chat
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\analytics\indicator;
defined('MOODLE_INTERNAL') || die();
/**
* Cognitive depth indicator - chat.
*
* @package mod_chat
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cognitive_depth extends activity_base {
/**
* Returns the name.
*
* If there is a corresponding '_help' string this will be shown as well.
*
* @return \lang_string
*/
public static function get_name(): \lang_string {
return new \lang_string('indicator:cognitivedepth', 'mod_chat');
}
public function get_indicator_type() {
return self::INDICATOR_COGNITIVE;
}
public function get_cognitive_depth_level(\cm_info $cm) {
return self::COGNITIVE_LEVEL_4;
}
}
@@ -0,0 +1,56 @@
<?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/>.
/**
* Social breadth indicator - chat.
*
* @package mod_chat
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\analytics\indicator;
defined('MOODLE_INTERNAL') || die();
/**
* Social breadth indicator - chat.
*
* @package mod_chat
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class social_breadth extends activity_base {
/**
* Returns the name.
*
* If there is a corresponding '_help' string this will be shown as well.
*
* @return \lang_string
*/
public static function get_name(): \lang_string {
return new \lang_string('indicator:socialbreadth', 'mod_chat');
}
public function get_indicator_type() {
return self::INDICATOR_SOCIAL;
}
public function get_social_breadth_level(\cm_info $cm) {
return self::SOCIAL_LEVEL_2;
}
}
+58
View File
@@ -0,0 +1,58 @@
<?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/>.
/**
* Contains the class for fetching the important dates in mod_chat for a given module instance and a user.
*
* @package mod_chat
* @copyright 2021 Dongsheng Cai
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types=1);
namespace mod_chat;
use core\activity_dates;
/**
* Class for fetching the important dates in mod_chat for a given module instance and a user.
*
*/
class dates extends activity_dates {
/**
* Returns a list of important dates in mod_chat.
*
* @return array
*/
protected function get_dates(): array {
$customdata = $this->cm->customdata;
$chat = (object) $customdata;
$chattime = $chat->chattime ?? 0;
$now = time();
if (!empty($chat->schedule) && $chattime > $now) {
return [
[
'dataid' => 'chattime',
'label' => get_string('nextchattime', 'mod_chat'),
'timestamp' => (int) $chattime
]
];
}
return [];
}
}
@@ -0,0 +1,38 @@
<?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/>.
/**
* The mod_chat instance list viewed event.
*
* @package mod_chat
* @copyright 2013 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_chat instance list viewed event class.
*
* @package mod_chat
* @since Moodle 2.7
* @copyright 2013 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_module_instance_list_viewed extends \core\event\course_module_instance_list_viewed {
}
@@ -0,0 +1,47 @@
<?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/>.
/**
* The mod_chat course module viewed event.
*
* @package mod_chat
* @copyright 2014 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_chat course module viewed event class.
*
* @package mod_chat
* @since Moodle 2.7
* @copyright 2014 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_module_viewed extends \core\event\course_module_viewed {
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'chat';
}
public static function get_objectid_mapping() {
return array('db' => 'chat', 'restore' => 'chat');
}
}
+93
View File
@@ -0,0 +1,93 @@
<?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/>.
/**
* The mod_chat message sent event.
*
* @package mod_chat
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_chat message sent event class.
*
* @package mod_chat
* @since Moodle 2.6
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class message_sent extends \core\event\base {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->relateduserid' has sent a message in the chat with course module id
'$this->contextinstanceid'.";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventmessagesent', 'mod_chat');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/chat/view.php', array('id' => $this->contextinstanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'chat_messages';
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if (!isset($this->relateduserid)) {
throw new \coding_exception('The \'relateduserid\' must be set.');
}
}
public static function get_objectid_mapping() {
return array('db' => 'chat_messages', 'restore' => 'chat_message');
}
}
+108
View File
@@ -0,0 +1,108 @@
<?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/>.
/**
* The mod_chat sessions viewed event.
*
* @package mod_chat
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_chat sessions viewed event class.
*
* @property-read array $other {
* Extra information about the event.
*
* - int start: start of period.
* - int end: end of period.
* }
*
* @package mod_chat
* @since Moodle 2.6
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class sessions_viewed extends \core\event\base {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' has viewed the sessions of the chat with course module id
'$this->contextinstanceid'.";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventsessionsviewed', 'mod_chat');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/chat/report.php', array('id' => $this->contextinstanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'chat';
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if (!isset($this->other['start'])) {
throw new \coding_exception('The \'start\' value must be set in other.');
}
if (!isset($this->other['end'])) {
throw new \coding_exception('The \'end\' value must be set in other.');
}
}
public static function get_objectid_mapping() {
return array('db' => 'chat', 'restore' => 'chat');
}
public static function get_other_mapping() {
// Nothing to map.
return false;
}
}
+833
View File
@@ -0,0 +1,833 @@
<?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->dirroot . '/mod/chat/lib.php');
use core_course\external\helper_for_get_mods_by_courses;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use core_external\util;
use mod_chat\external\chat_message_exporter;
/**
* 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 new moodle_exception('cantlogin', 'chat');
}
$result = array();
$result['chatsid'] = $chatsid;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns description of method result value
*
* @return \core_external\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()
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function get_chat_users_parameters() {
return new external_function_parameters(
array(
'chatsid' => new external_value(PARAM_ALPHANUM, 'chat session id (obtained via mod_chat_login_user)')
)
);
}
/**
* Get the list of users in the given chat session.
*
* @param int $chatsid the chat session id
* @return array of warnings and the user lists
* @since Moodle 3.0
* @throws moodle_exception
*/
public static function get_chat_users($chatsid) {
global $DB, $PAGE;
$params = self::validate_parameters(self::get_chat_users_parameters(),
array(
'chatsid' => $chatsid
));
$warnings = array();
// Request and permission validation.
if (!$chatuser = $DB->get_record('chat_users', array('sid' => $params['chatsid']))) {
throw new moodle_exception('notlogged', 'chat');
}
$chat = $DB->get_record('chat', array('id' => $chatuser->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);
// First, delete old users from the chats.
chat_delete_old_users();
$users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid);
$returnedusers = array();
foreach ($users as $user) {
$userpicture = new user_picture($user);
$userpicture->size = 1; // Size f1.
$profileimageurl = $userpicture->get_url($PAGE)->out(false);
$returnedusers[] = array(
'id' => $user->id,
'fullname' => fullname($user),
'profileimageurl' => $profileimageurl
);
}
$result = array();
$result['users'] = $returnedusers;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.0
*/
public static function get_chat_users_returns() {
return new external_single_structure(
array(
'users' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'user id'),
'fullname' => new external_value(PARAM_NOTAGS, 'user full name'),
'profileimageurl' => new external_value(PARAM_URL, 'user picture URL'),
)
),
'list of users'
),
'warnings' => new external_warnings()
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function send_chat_message_parameters() {
return new external_function_parameters(
array(
'chatsid' => new external_value(PARAM_ALPHANUM, 'chat session id (obtained via mod_chat_login_user)'),
'messagetext' => new external_value(PARAM_RAW, 'the message text'),
'beepid' => new external_value(PARAM_RAW, 'the beep id', VALUE_DEFAULT, ''),
)
);
}
/**
* Send a message on the given chat session.
*
* @param int $chatsid the chat session id
* @param string $messagetext the message text
* @param string $beepid the beep message id
* @return array of warnings and the new message id (0 if the message was empty)
* @since Moodle 3.0
* @throws moodle_exception
*/
public static function send_chat_message($chatsid, $messagetext, $beepid = '') {
global $DB;
$params = self::validate_parameters(self::send_chat_message_parameters(),
array(
'chatsid' => $chatsid,
'messagetext' => $messagetext,
'beepid' => $beepid
));
$warnings = array();
// Request and permission validation.
if (!$chatuser = $DB->get_record('chat_users', array('sid' => $params['chatsid']))) {
throw new moodle_exception('notlogged', 'chat');
}
$chat = $DB->get_record('chat', array('id' => $chatuser->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);
$chatmessage = clean_text($params['messagetext'], FORMAT_MOODLE);
if (!empty($params['beepid'])) {
$chatmessage = 'beep ' . $params['beepid'];
}
if (!empty($chatmessage)) {
// Send the message.
$messageid = chat_send_chatmessage($chatuser, $chatmessage, 0, $cm);
// Update ping time.
$chatuser->lastmessageping = time() - 2;
$DB->update_record('chat_users', $chatuser);
} else {
$messageid = 0;
}
$result = array();
$result['messageid'] = $messageid;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.0
*/
public static function send_chat_message_returns() {
return new external_single_structure(
array(
'messageid' => new external_value(PARAM_INT, 'message sent id'),
'warnings' => new external_warnings()
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function get_chat_latest_messages_parameters() {
return new external_function_parameters(
array(
'chatsid' => new external_value(PARAM_ALPHANUM, 'chat session id (obtained via mod_chat_login_user)'),
'chatlasttime' => new external_value(PARAM_INT, 'last time messages were retrieved (epoch time)', VALUE_DEFAULT, 0)
)
);
}
/**
* Get the latest messages from the given chat session.
*
* @param int $chatsid the chat session id
* @param int $chatlasttime last time messages were retrieved (epoch time)
* @return array of warnings and the new message id (0 if the message was empty)
* @since Moodle 3.0
* @throws moodle_exception
*/
public static function get_chat_latest_messages($chatsid, $chatlasttime = 0) {
global $DB, $CFG;
$params = self::validate_parameters(self::get_chat_latest_messages_parameters(),
array(
'chatsid' => $chatsid,
'chatlasttime' => $chatlasttime
));
$warnings = array();
// Request and permission validation.
if (!$chatuser = $DB->get_record('chat_users', array('sid' => $params['chatsid']))) {
throw new moodle_exception('notlogged', 'chat');
}
$chat = $DB->get_record('chat', array('id' => $chatuser->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);
$chatlasttime = $params['chatlasttime'];
if ((time() - $chatlasttime) > $CFG->chat_old_ping) {
chat_delete_old_users();
}
// Set default chat last time (to not retrieve all the conversations).
if ($chatlasttime == 0) {
$chatlasttime = time() - $CFG->chat_old_ping;
}
if ($latestmessage = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
$chatnewlasttime = $latestmessage->timestamp;
} else {
$chatnewlasttime = 0;
}
$messages = chat_get_latest_messages($chatuser, $chatlasttime);
$returnedmessages = array();
foreach ($messages as $message) {
// FORMAT_MOODLE is mandatory in the chat plugin.
[$messageformatted] = \core_external\util::format_text(
$message->message,
FORMAT_MOODLE,
$context->id,
'mod_chat',
'',
0
);
$returnedmessages[] = array(
'id' => $message->id,
'userid' => $message->userid,
'system' => (bool) $message->issystem,
'message' => $messageformatted,
'timestamp' => $message->timestamp,
);
}
// Update our status since we are active in the chat.
$DB->set_field('chat_users', 'lastping', time(), array('id' => $chatuser->id));
$result = array();
$result['messages'] = $returnedmessages;
$result['chatnewlasttime'] = $chatnewlasttime;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.0
*/
public static function get_chat_latest_messages_returns() {
return new external_single_structure(
array(
'messages' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'message id'),
'userid' => new external_value(PARAM_INT, 'user id'),
'system' => new external_value(PARAM_BOOL, 'true if is a system message (like user joined)'),
'message' => new external_value(PARAM_RAW, 'message text'),
'timestamp' => new external_value(PARAM_INT, 'timestamp for the message'),
)
),
'list of users'
),
'chatnewlasttime' => new external_value(PARAM_INT, 'new last time'),
'warnings' => new external_warnings()
)
);
}
/**
* 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 \core_external\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()
)
);
}
/**
* Describes the parameters for get_chats_by_courses.
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function get_chats_by_courses_parameters() {
return new external_function_parameters (
array(
'courseids' => new external_multiple_structure(
new external_value(PARAM_INT, 'course id'), 'Array of course ids', VALUE_DEFAULT, array()
),
)
);
}
/**
* Returns a list of chats in a provided list of courses,
* if no list is provided all chats that the user can view will be returned.
*
* @param array $courseids the course ids
* @return array of chats details
* @since Moodle 3.0
*/
public static function get_chats_by_courses($courseids = array()) {
global $CFG;
$returnedchats = array();
$warnings = array();
$params = self::validate_parameters(self::get_chats_by_courses_parameters(), array('courseids' => $courseids));
$courses = array();
if (empty($params['courseids'])) {
$courses = enrol_get_my_courses();
$params['courseids'] = array_keys($courses);
}
// Ensure there are courseids to loop through.
if (!empty($params['courseids'])) {
list($courses, $warnings) = util::validate_courses($params['courseids'], $courses);
// Get the chats in this course, this function checks users visibility permissions.
// We can avoid then additional validate_context calls.
$chats = get_all_instances_in_courses("chat", $courses);
foreach ($chats as $chat) {
$chatcontext = context_module::instance($chat->coursemodule);
$chatdetails = helper_for_get_mods_by_courses::standard_coursemodule_element_values($chat, 'mod_chat');
if (has_capability('mod/chat:chat', $chatcontext)) {
$chatdetails['chatmethod'] = $CFG->chat_method;
$chatdetails['keepdays'] = $chat->keepdays;
$chatdetails['studentlogs'] = $chat->studentlogs;
$chatdetails['chattime'] = $chat->chattime;
$chatdetails['schedule'] = $chat->schedule;
}
if (has_capability('moodle/course:manageactivities', $chatcontext)) {
$chatdetails['timemodified'] = $chat->timemodified;
}
$returnedchats[] = $chatdetails;
}
}
$result = array();
$result['chats'] = $returnedchats;
$result['warnings'] = $warnings;
return $result;
}
/**
* Describes the get_chats_by_courses return value.
*
* @return external_single_structure
* @since Moodle 3.0
*/
public static function get_chats_by_courses_returns() {
return new external_single_structure(
array(
'chats' => new external_multiple_structure(
new external_single_structure(array_merge(
helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(),
[
'chatmethod' => new external_value(PARAM_PLUGIN, 'chat method (sockets, ajax, header_js)',
VALUE_OPTIONAL),
'keepdays' => new external_value(PARAM_INT, 'keep days', VALUE_OPTIONAL),
'studentlogs' => new external_value(PARAM_INT, 'student logs visible to everyone', VALUE_OPTIONAL),
'chattime' => new external_value(PARAM_INT, 'chat time', VALUE_OPTIONAL),
'schedule' => new external_value(PARAM_INT, 'schedule type', VALUE_OPTIONAL),
'timemodified' => new external_value(PARAM_INT, 'time of last modification', VALUE_OPTIONAL),
]
), 'Chats')
),
'warnings' => new external_warnings(),
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.4
*/
public static function get_sessions_parameters() {
return new external_function_parameters(
array(
'chatid' => new external_value(PARAM_INT, 'Chat instance id.'),
'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),
'showall' => new external_value(PARAM_BOOL, 'Whether to show completed sessions or not.', VALUE_DEFAULT, false),
)
);
}
/**
* Retrieves chat sessions for a given chat.
*
* @param int $chatid the chat instance id
* @param int $groupid filter messages by this group. 0 to determine the group.
* @param bool $showall whether to include incomplete sessions or not
* @return array of warnings and the sessions
* @since Moodle 3.4
* @throws moodle_exception
*/
public static function get_sessions($chatid, $groupid = 0, $showall = false) {
global $DB;
$params = self::validate_parameters(self::get_sessions_parameters(),
array(
'chatid' => $chatid,
'groupid' => $groupid,
'showall' => $showall,
));
$sessions = $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, 0, 0, 'timestamp DESC');
if ($messages) {
$chatsessions = chat_get_sessions($messages, $params['showall']);
// Format sessions for external.
foreach ($chatsessions as $session) {
$sessionusers = array();
foreach ($session->sessionusers as $sessionuser => $usermessagecount) {
$sessionusers[] = array(
'userid' => $sessionuser,
'messagecount' => $usermessagecount
);
}
$session->sessionusers = $sessionusers;
$sessions[] = $session;
}
}
$result = array();
$result['sessions'] = $sessions;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.4
*/
public static function get_sessions_returns() {
return new external_single_structure(
array(
'sessions' => new external_multiple_structure(
new external_single_structure(
array(
'sessionstart' => new external_value(PARAM_INT, 'Session start time.'),
'sessionend' => new external_value(PARAM_INT, 'Session end time.'),
'sessionusers' => new external_multiple_structure(
new external_single_structure(
array(
'userid' => new external_value(PARAM_INT, 'User id.'),
'messagecount' => new external_value(PARAM_INT, 'Number of messages in the session.'),
)
), 'Session users.'
),
'iscomplete' => new external_value(PARAM_BOOL, 'Whether the session is completed or not.'),
)
),
'list of users'
),
'warnings' => new external_warnings()
)
);
}
/**
* 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 \core_external\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()
)
);
}
}
+101
View File
@@ -0,0 +1,101 @@
<?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/>.
/**
* Class for exporting a chat message.
*
* @package mod_chat
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\external;
defined('MOODLE_INTERNAL') || die();
use core\external\exporter;
/**
* Class for exporting a chat message.
*
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class chat_message_exporter extends exporter {
/**
* Defines exporter properties.
*
* @return array
*/
protected static function define_properties() {
return array(
'id' => array(
'type' => PARAM_INT,
'description' => 'The message record id.',
),
'chatid' => array(
'type' => PARAM_INT,
'description' => 'The chat id.',
'default' => 0,
),
'userid' => array(
'type' => PARAM_INT,
'description' => 'The user who wrote the message.',
'default' => 0,
),
'groupid' => array(
'type' => PARAM_INT,
'description' => 'The group this message belongs to.',
'default' => 0,
),
'issystem' => array(
'type' => PARAM_BOOL,
'description' => 'Whether is a system message or not.',
'default' => false,
),
'message' => array(
'type' => PARAM_RAW,
'description' => 'The message text.',
),
'timestamp' => array(
'type' => PARAM_INT,
'description' => 'The message timestamp (indicates when the message was sent).',
'default' => 0,
),
);
}
/**
* Defines related information.
*
* @return array
*/
protected static function define_related() {
return array(
'context' => 'context',
);
}
/**
* Get the formatting parameters for the name.
*
* @return array
*/
protected function get_format_parameters_for_message() {
return [
'component' => 'mod_chat',
];
}
}
+121
View File
@@ -0,0 +1,121 @@
<?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/>.
namespace mod_chat\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
/**
* External service to log viewed previous chat sessions.
*
* @package mod_chat
* @category external
* @copyright 2023 Rodrigo Mady <rodrigo.mady@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 4.3
*/
class view_sessions extends external_api {
/**
* Returns description of method parameters
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'cmid' => new external_value(PARAM_INT, 'Course module id', VALUE_REQUIRED),
'start' => new external_value(PARAM_INT, 'Session start time', VALUE_DEFAULT, 0),
'end' => new external_value(PARAM_INT, 'Session end time', VALUE_DEFAULT, 0),
]);
}
/**
* Execute the chat view sessions event.
*
* @param int $cmid the chat course module id
* @param null|int $start
* @param null|int $end
* @return array
* @throws \restricted_context_exception
*/
public static function execute(int $cmid, ?int $start = 0, ?int $end = 0): array {
global $DB;
$warnings = [];
$status = false;
// Validate the cmid ID.
[
'cmid' => $cmid,
'start' => $start,
'end' => $end,
] = self::validate_parameters(self::execute_parameters(), [
'cmid' => $cmid,
'start' => $start,
'end' => $end,
]);
if (!$cm = get_coursemodule_from_id('chat', $cmid)) {
throw new \moodle_exception('invalidcoursemodule', 'error');
}
if (!$chat = $DB->get_record('chat', ['id' => $cm->instance])) {
throw new \moodle_exception('invalidcoursemodule', 'error');
}
$context = \context_module::instance($cm->id);
self::validate_context($context);
// Check capability.
if (has_capability('mod/chat:readlog', $context)) {
$params = [
'context' => $context,
'objectid' => $chat->id,
'other' => [
'start' => $start,
'end' => $end
]
];
$event = \mod_chat\event\sessions_viewed::create($params);
$status = true;
$event->add_record_snapshot('chat', $chat);
$event->trigger();
} else {
$warnings[] = [
'item' => $cm->id,
'warningcode' => 'nopermissiontoseethechatlog',
'message' => get_string('nopermissiontoseethechatlog', 'chat')
];
}
$result = [
'status' => $status,
'warnings' => $warnings
];
return $result;
}
/**
* Describe the return structure of the external service.
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
'warnings' => new external_warnings()
]);
}
}
+333
View File
@@ -0,0 +1,333 @@
<?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/>.
/**
* Data provider.
*
* @package mod_chat
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\privacy;
defined('MOODLE_INTERNAL') || die();
use context;
use context_helper;
use context_module;
use moodle_recordset;
use stdClass;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\approved_userlist;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\helper;
use core_privacy\local\request\transform;
use core_privacy\local\request\userlist;
use core_privacy\local\request\writer;
/**
* Data provider class.
*
* @package mod_chat
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\core_userlist_provider,
\core_privacy\local\request\plugin\provider {
/**
* Returns metadata.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection): collection {
$collection->add_database_table('chat_messages', [
'userid' => 'privacy:metadata:messages:userid',
'message' => 'privacy:metadata:messages:message',
'issystem' => 'privacy:metadata:messages:issystem',
'timestamp' => 'privacy:metadata:messages:timestamp',
], 'privacy:metadata:messages');
// The tables chat_messages_current and chat_users are not exported/deleted
// because they are considered as short-lived data and are deleted on a
// regular basis by cron, or during normal requests. TODO MDL-62006.
$collection->add_database_table('chat_messages_current', [
'userid' => 'privacy:metadata:messages:userid',
'message' => 'privacy:metadata:messages:message',
'issystem' => 'privacy:metadata:messages:issystem',
'timestamp' => 'privacy:metadata:messages:timestamp'
], 'privacy:metadata:chat_messages_current');
$collection->add_database_table('chat_users', [
'userid' => 'privacy:metadata:chat_users:userid',
'version' => 'privacy:metadata:chat_users:version',
'ip' => 'privacy:metadata:chat_users:ip',
'firstping' => 'privacy:metadata:chat_users:firstping',
'lastping' => 'privacy:metadata:chat_users:lastping',
'lastmessageping' => 'privacy:metadata:chat_users:lastmessageping',
'lang' => 'privacy:metadata:chat_users:lang'
], 'privacy:metadata:chat_users');
return $collection;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid): \core_privacy\local\request\contextlist {
$contextlist = new \core_privacy\local\request\contextlist();
$sql = "
SELECT DISTINCT ctx.id
FROM {chat} c
JOIN {modules} m
ON m.name = :chat
JOIN {course_modules} cm
ON cm.instance = c.id
AND cm.module = m.id
JOIN {context} ctx
ON ctx.instanceid = cm.id
AND ctx.contextlevel = :modulelevel
JOIN {chat_messages} chm
ON chm.chatid = c.id
WHERE chm.userid = :userid";
$params = [
'chat' => 'chat',
'modulelevel' => CONTEXT_MODULE,
'userid' => $userid,
];
$contextlist->add_from_sql($sql, $params);
return $contextlist;
}
/**
* Get the list of users who have data within a context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
$context = $userlist->get_context();
if (!is_a($context, \context_module::class)) {
return;
}
$params = [
'instanceid' => $context->instanceid,
'modulename' => 'chat',
];
$sql = "SELECT chm.userid
FROM {course_modules} cm
JOIN {modules} m ON m.id = cm.module AND m.name = :modulename
JOIN {chat} c ON c.id = cm.instance
JOIN {chat_messages} chm ON chm.chatid = c.id
WHERE cm.id = :instanceid";
$userlist->add_from_sql('userid', $sql, $params);
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
global $DB;
$user = $contextlist->get_user();
$userid = $user->id;
$cmids = array_reduce($contextlist->get_contexts(), function($carry, $context) {
if ($context->contextlevel == CONTEXT_MODULE) {
$carry[] = $context->instanceid;
}
return $carry;
}, []);
if (empty($cmids)) {
return;
}
$chatidstocmids = static::get_chat_ids_to_cmids_from_cmids($cmids);
$chatids = array_keys($chatidstocmids);
// Export the messages.
list($insql, $inparams) = $DB->get_in_or_equal($chatids, SQL_PARAMS_NAMED);
$params = array_merge($inparams, ['userid' => $userid]);
$recordset = $DB->get_recordset_select('chat_messages', "chatid $insql AND userid = :userid", $params, 'timestamp, id');
static::recordset_loop_and_export($recordset, 'chatid', [], function($carry, $record) use ($user, $chatidstocmids) {
$message = $record->message;
if ($record->issystem) {
$message = get_string('message' . $record->message, 'mod_chat', fullname($user));
}
$carry[] = [
'message' => $message,
'sent_at' => transform::datetime($record->timestamp),
'is_system_generated' => transform::yesno($record->issystem),
];
return $carry;
}, function($chatid, $data) use ($user, $chatidstocmids) {
$context = context_module::instance($chatidstocmids[$chatid]);
$contextdata = helper::get_context_data($context, $user);
$finaldata = (object) array_merge((array) $contextdata, ['messages' => $data]);
helper::export_context_files($context, $user);
writer::with_context($context)->export_data([], $finaldata);
});
}
/**
* Delete all data for all users in the specified context.
*
* @param context $context The specific context to delete data for.
*/
public static function delete_data_for_all_users_in_context(context $context) {
global $DB;
if ($context->contextlevel != CONTEXT_MODULE) {
return;
}
$cm = get_coursemodule_from_id('chat', $context->instanceid);
if (!$cm) {
return;
}
$chatid = $cm->instance;
$DB->delete_records_select('chat_messages', 'chatid = :chatid', ['chatid' => $chatid]);
$DB->delete_records_select('chat_messages_current', 'chatid = :chatid', ['chatid' => $chatid]);
$DB->delete_records_select('chat_users', 'chatid = :chatid', ['chatid' => $chatid]);
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
global $DB;
$userid = $contextlist->get_user()->id;
$cmids = array_reduce($contextlist->get_contexts(), function($carry, $context) {
if ($context->contextlevel == CONTEXT_MODULE) {
$carry[] = $context->instanceid;
}
return $carry;
}, []);
if (empty($cmids)) {
return;
}
$chatidstocmids = static::get_chat_ids_to_cmids_from_cmids($cmids);
$chatids = array_keys($chatidstocmids);
list($insql, $inparams) = $DB->get_in_or_equal($chatids, SQL_PARAMS_NAMED);
$sql = "chatid $insql AND userid = :userid";
$params = array_merge($inparams, ['userid' => $userid]);
$DB->delete_records_select('chat_messages', $sql, $params);
$DB->delete_records_select('chat_messages_current', $sql, $params);
$DB->delete_records_select('chat_users', $sql, $params);
}
/**
* Delete multiple users within a single context.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function delete_data_for_users(approved_userlist $userlist) {
global $DB;
$context = $userlist->get_context();
$cm = $DB->get_record('course_modules', ['id' => $context->instanceid]);
$chat = $DB->get_record('chat', ['id' => $cm->instance]);
list($userinsql, $userinparams) = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);
$params = array_merge(['chatid' => $chat->id], $userinparams);
$sql = "chatid = :chatid AND userid {$userinsql}";
$DB->delete_records_select('chat_messages', $sql, $params);
$DB->delete_records_select('chat_messages_current', $sql, $params);
$DB->delete_records_select('chat_users', $sql, $params);
}
/**
* Return a dict of chat IDs mapped to their course module ID.
*
* @param array $cmids The course module IDs.
* @return array In the form of [$chatid => $cmid].
*/
protected static function get_chat_ids_to_cmids_from_cmids(array $cmids) {
global $DB;
list($insql, $inparams) = $DB->get_in_or_equal($cmids, SQL_PARAMS_NAMED);
$sql = "
SELECT c.id, cm.id AS cmid
FROM {chat} c
JOIN {modules} m
ON m.name = :chat
JOIN {course_modules} cm
ON cm.instance = c.id
AND cm.module = m.id
WHERE cm.id $insql";
$params = array_merge($inparams, ['chat' => 'chat']);
return $DB->get_records_sql_menu($sql, $params);
}
/**
* Loop and export from a recordset.
*
* @param moodle_recordset $recordset The recordset.
* @param string $splitkey The record key to determine when to export.
* @param mixed $initial The initial data to reduce from.
* @param callable $reducer The function to return the dataset, receives current dataset, and the current record.
* @param callable $export The function to export the dataset, receives the last value from $splitkey and the dataset.
* @return void
*/
protected static function recordset_loop_and_export(moodle_recordset $recordset, $splitkey, $initial,
callable $reducer, callable $export) {
$data = $initial;
$lastid = null;
foreach ($recordset as $record) {
if ($lastid && $record->{$splitkey} != $lastid) {
$export($lastid, $data);
$data = $initial;
}
$data = $reducer($data, $record);
$lastid = $record->{$splitkey};
}
$recordset->close();
if (!empty($lastid)) {
$export($lastid, $data);
}
}
}
+46
View File
@@ -0,0 +1,46 @@
<?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/>.
/**
* Search area for mod_chat activities.
*
* @package mod_chat
* @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\search;
defined('MOODLE_INTERNAL') || die();
/**
* Search area for mod_chat activities.
*
* @package mod_chat
* @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activity extends \core_search\base_activity {
/**
* Returns true if this area uses file indexing.
*
* @return bool
*/
public function uses_file_indexing() {
return true;
}
}
+65
View File
@@ -0,0 +1,65 @@
<?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/>.
/**
* A scheduled task for chat cron.
*
* @package mod_chat
* @copyright 2019 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\task;
defined('MOODLE_INTERNAL') || die();
/**
* The main schedule task for the chat module.
*
* @package mod_chat
* @copyright 2019 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cron_task extends \core\task\scheduled_task {
/**
* Get a descriptive name for this task (shown to admins).
*
* @return string
*/
public function get_name() {
return get_string('crontask', 'mod_chat');
}
/**
* Run chat cron.
*/
public function execute() {
global $CFG, $DB;
require_once($CFG->dirroot . '/mod/chat/lib.php');
chat_update_chat_times();
chat_delete_old_users();
$timenow = time();
$subselect = "SELECT c.keepdays
FROM {chat} c
WHERE c.id = {chat_messages}.chatid";
$DB->delete_records_select('chat_messages', "($subselect) > 0 AND timestamp < (? - ($subselect) * ?)",
[$timenow, DAYSECS]);
$DB->delete_records_select('chat_messages_current', "timestamp < ?", [$timenow - 8 * HOURSECS]);
}
}