2004-09-12 15:06:29 +00:00
< ? php // $Id$
2003-07-07 06:44:16 +00:00
/// Library of functions and constants for module chat
2005-02-28 04:23:52 +00:00
require_once ( $CFG -> libdir . '/pagelib.php' );
2003-07-07 06:44:16 +00:00
// The HTML head for the message window to start with (<!-- nix --> is used to get some browsers starting with output
2008-07-24 03:15:03 +00:00
$CHAT_HTMLHEAD = " <!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.0 Transitional//EN \" \" http://www.w3.org/TR/REC-html40/loose.dtd \" ><html><head></head> \n <body bgcolor= \" #FFFFFF \" > \n \n " . padding ( 200 );
2003-07-07 06:44:16 +00:00
// The HTML head for the message window to start with (with js scrolling)
2008-07-24 03:15:03 +00:00
$CHAT_HTMLHEAD_JS = " <!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.0 Transitional//EN \" \" http://www.w3.org/TR/REC-html40/loose.dtd \" ><html><head><script type= \" text/javascript \" > \n //<![CDATA[ \n function move() \n { \n if (scroll_active) window.scroll(1,400000); \n window.setTimeout( \" move() \" ,100); \n } \n scroll_active = true; \n move(); \n //]]> \n </script> \n </head> \n <body bgcolor= \" #FFFFFF \" onBlur= \" scroll_active = true \" onFocus= \" scroll_active = false \" > \n \n " . padding ( 200 );
2003-07-07 06:44:16 +00:00
// The HTML code for standard empty pages (e.g. if a user was kicked out)
2005-02-10 18:22:31 +00:00
$CHAT_HTMLHEAD_OUT = " <!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.0 Transitional//EN \" \" http://www.w3.org/TR/REC-html40/loose.dtd \" ><html><head><title>You are out!</title></head><body bgcolor= \" #FFFFFF \" ></body></html> " ;
2003-07-07 06:44:16 +00:00
// The HTML head for the message input page
2005-02-10 18:22:31 +00:00
$CHAT_HTMLHEAD_MSGINPUT = " <!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.0 Transitional//EN \" \" http://www.w3.org/TR/REC-html40/loose.dtd \" ><html><head><title>Message Input</title></head><body bgcolor= \" #FFFFFF \" > " ;
2003-07-07 06:44:16 +00:00
// The HTML code for the message input page, with JavaScript
2007-01-03 19:33:44 +00:00
$CHAT_HTMLHEAD_MSGINPUT_JS = " <!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.0 Transitional//EN \" \" http://www.w3.org/TR/REC-html40/loose.dtd \" ><html><head><title>Message Input</title> \n <script type= \" text/javascript \" > \n //<![CDATA[ \n scroll_active = true; \n function empty_field_and_submit() \n { \n document.fdummy.arsc_message.value=document.f.arsc_message.value; \n document.fdummy.submit(); \n document.f.arsc_message.focus(); \n document.f.arsc_message.select(); \n return false; \n } \n //]]> \n </script> \n </head><body bgcolor= \" #FFFFFF \" OnLoad= \" document.f.arsc_message.focus();document.f.arsc_message.select(); \" > " ;
2003-07-07 06:44:16 +00:00
2004-08-23 14:40:53 +00:00
// Dummy data that gets output to the browser as needed, in order to make it show output
2008-07-24 03:15:03 +00:00
$CHAT_DUMMY_DATA = padding ( 200 );
2008-06-15 12:24:36 +00:00
2008-07-24 03:15:03 +00:00
function padding ( $n ){
2008-06-15 12:24:36 +00:00
$str = '' ;
2008-06-15 21:27:16 +00:00
for ( $i = 0 ; $i < $n ; $i ++ ){
2008-06-15 12:24:36 +00:00
$str .= '<!-- nix -->\n' ;
}
return $str ;
}
2003-07-07 06:44:16 +00:00
function chat_add_instance ( $chat ) {
2008-06-01 21:36:11 +00:00
global $DB ;
2004-08-10 13:50:58 +00:00
/// Given an object containing all the necessary data,
2008-06-01 19:09:13 +00:00
/// (defined by the form in mod_form.php) this function
2004-08-10 13:50:58 +00:00
/// will create a new instance and return the id number
2003-07-07 06:44:16 +00:00
/// of the new instance.
$chat -> timemodified = time ();
2008-06-01 21:36:11 +00:00
if ( $returnid = $DB -> insert_record ( " chat " , $chat )) {
2004-04-25 03:05:58 +00:00
$event = NULL ;
$event -> name = $chat -> name ;
$event -> description = $chat -> intro ;
$event -> courseid = $chat -> course ;
$event -> groupid = 0 ;
$event -> userid = 0 ;
$event -> modulename = 'chat' ;
$event -> instance = $returnid ;
$event -> eventtype = $chat -> schedule ;
$event -> timestart = $chat -> chattime ;
$event -> timeduration = 0 ;
add_event ( $event );
}
return $returnid ;
2003-07-07 06:44:16 +00:00
}
function chat_update_instance ( $chat ) {
2008-06-01 21:36:11 +00:00
global $DB ;
2004-08-10 13:50:58 +00:00
/// Given an object containing all the necessary data,
2008-06-01 19:09:13 +00:00
/// (defined by the form in mod_form.php) this function
2003-07-07 06:44:16 +00:00
/// will update an existing instance with new data.
$chat -> timemodified = time ();
$chat -> id = $chat -> instance ;
2008-06-01 21:36:11 +00:00
if ( $returnid = $DB -> update_record ( " chat " , $chat )) {
2004-04-25 03:05:58 +00:00
2006-11-09 18:25:58 +00:00
$event = new object ();
2004-04-25 03:05:58 +00:00
2008-06-01 21:36:11 +00:00
if ( $event -> id = $DB -> get_field ( 'event' , 'id' , array ( 'modulename' => 'chat' , 'instance' => $chat -> id ))) {
2004-04-25 03:05:58 +00:00
$event -> name = $chat -> name ;
$event -> description = $chat -> intro ;
$event -> timestart = $chat -> chattime ;
update_event ( $event );
}
}
return $returnid ;
2003-07-07 06:44:16 +00:00
}
function chat_delete_instance ( $id ) {
2008-06-01 21:36:11 +00:00
global $DB ;
2004-08-10 13:50:58 +00:00
/// Given an ID of an instance of this module,
/// this function will permanently delete the instance
/// and any data that depends on it.
2003-07-07 06:44:16 +00:00
2008-06-01 21:36:11 +00:00
if ( ! $chat = $DB -> get_record ( 'chat' , array ( 'id' => $id ))) {
2003-07-07 06:44:16 +00:00
return false ;
}
$result = true ;
# Delete any dependent records here #
2008-06-01 21:36:11 +00:00
if ( ! $DB -> delete_records ( 'chat' , array ( 'id' => $chat -> id ))) {
2005-03-27 05:56:23 +00:00
$result = false ;
}
2008-06-01 21:36:11 +00:00
if ( ! $DB -> delete_records ( 'chat_messages' , array ( 'chatid' => $chat -> id ))) {
2005-03-27 05:56:23 +00:00
$result = false ;
}
2008-07-24 03:15:03 +00:00
if ( ! $DB -> delete_records ( 'chat_messages_current' , array ( 'chatid' => $chat -> id ))) {
$result = false ;
}
2008-06-01 21:36:11 +00:00
if ( ! $DB -> delete_records ( 'chat_users' , array ( 'chatid' => $chat -> id ))) {
2003-07-07 06:44:16 +00:00
$result = false ;
}
2005-02-28 04:18:13 +00:00
$pagetypes = page_import_types ( 'mod/chat/' );
foreach ( $pagetypes as $pagetype ) {
2008-06-01 21:36:11 +00:00
if ( ! $DB -> delete_records ( 'block_instance' , array ( 'pageid' => $chat -> id , 'pagetype' => $pagetype ))) {
2005-02-28 04:18:13 +00:00
$result = false ;
}
}
2008-06-01 21:36:11 +00:00
if ( ! $DB -> delete_records ( 'event' , array ( 'modulename' => 'chat' , 'instance' => $chat -> id ))) {
2004-05-17 16:24:19 +00:00
$result = false ;
}
2003-07-07 06:44:16 +00:00
return $result ;
}
function chat_user_outline ( $course , $user , $mod , $chat ) {
2004-08-10 13:50:58 +00:00
/// Return a small object with summary information about what a
2003-07-07 06:44:16 +00:00
/// user has done with a given particular instance of this module
/// Used for user activity reports.
/// $return->time = the time they did it
/// $return->info = a short text description
2008-06-08 15:49:36 +00:00
return NULL ;
2003-07-07 06:44:16 +00:00
}
function chat_user_complete ( $course , $user , $mod , $chat ) {
2004-08-10 13:50:58 +00:00
/// Print a detailed representation of what a user has done with
2003-07-07 06:44:16 +00:00
/// a given particular instance of this module, for user activity reports.
return true ;
}
2008-01-24 20:33:50 +00:00
function chat_print_recent_activity ( $course , $viewfullnames , $timestart ) {
/// Given a course and a date, prints a summary of all chat rooms past and present
2003-07-07 06:44:16 +00:00
/// This function is called from course/lib.php: print_recent_activity()
2008-06-08 15:49:36 +00:00
global $CFG , $USER , $DB ;
2008-01-24 20:33:50 +00:00
// this is approximate only, but it is really fast ;-)
$timeout = $CFG -> chat_old_ping * 10 ;
2008-06-08 15:49:36 +00:00
if ( ! $mcms = $DB -> get_records_sql ( " SELECT cm.id, MAX(chm.timestamp) AS lasttime
FROM { course_modules } cm
JOIN { modules } md ON md . id = cm . module
JOIN { chat } ch ON ch . id = cm . instance
JOIN { chat_messages } chm ON chm . chatid = ch . id
WHERE chm . timestamp > ? AND ch . course = ? AND md . name = 'chat'
GROUP BY cm . id
ORDER BY lasttime ASC " , array( $timestart , $course->id ))) {
2008-01-24 20:33:50 +00:00
return false ;
}
$past = array ();
$current = array ();
$modinfo =& get_fast_modinfo ( $course ); // reference needed because we might load the groups
2008-02-14 16:12:53 +00:00
foreach ( $mcms as $cmid => $mcm ) {
if ( ! array_key_exists ( $cmid , $modinfo -> cms )) {
2008-01-24 20:33:50 +00:00
continue ;
}
2008-02-14 16:12:53 +00:00
$cm = $modinfo -> cms [ $cmid ];
$cm -> lasttime = $mcm -> lasttime ;
2008-01-24 20:33:50 +00:00
if ( ! $modinfo -> cms [ $cm -> id ] -> uservisible ) {
continue ;
}
2003-07-10 04:46:03 +00:00
2008-01-24 20:33:50 +00:00
if ( groups_get_activity_groupmode ( $cm ) != SEPARATEGROUPS
or has_capability ( 'moodle/site:accessallgroups' , get_context_instance ( CONTEXT_MODULE , $cm -> id ))) {
if ( $timeout > time () - $cm -> lasttime ) {
$current [] = $cm ;
} else {
$past [] = $cm ;
}
continue ;
}
if ( is_null ( $modinfo -> groups )) {
$modinfo -> groups = groups_get_user_groups ( $course -> id ); // load all my groups and cache it in modinfo
}
2004-01-15 08:44:08 +00:00
2008-01-24 20:33:50 +00:00
// verify groups in separate mode
if ( ! $mygroupids = $modinfo -> groups [ $cm -> groupingid ]) {
continue ;
}
2004-08-26 10:44:32 +00:00
2008-01-24 20:33:50 +00:00
// ok, last post was not for my group - we have to query db to get last message from one of my groups
// only minor problem is that the order will not be correct
$mygroupids = implode ( ',' , $mygroupids );
$cm -> mygroupids = $mygroupids ;
2008-06-08 15:49:36 +00:00
if ( ! $mcm = $DB -> get_record_sql ( " SELECT cm.id, MAX(chm.timestamp) AS lasttime
FROM { course_modules } cm
JOIN { chat } ch ON ch . id = cm . instance
2008-07-24 03:15:03 +00:00
JOIN { chat_messages_current } chm ON chm . chatid = ch . id
2008-06-08 15:49:36 +00:00
WHERE chm . timestamp > ? AND cm . id = ? AND
( chm . groupid IN ( $mygroupids ) OR chm . groupid = 0 )
GROUP BY cm . id " , array( $timestart , $cm->id ))) {
2008-01-24 20:33:50 +00:00
continue ;
}
2008-02-14 16:12:53 +00:00
$cm -> lasttime = $mcm -> lasttime ;
2008-01-24 20:33:50 +00:00
if ( $timeout > time () - $cm -> lasttime ) {
$current [] = $cm ;
} else {
$past [] = $cm ;
}
}
if ( ! $past and ! $current ) {
2003-07-10 04:46:03 +00:00
return false ;
}
2003-07-07 06:44:16 +00:00
2008-01-24 20:33:50 +00:00
$strftimerecent = get_string ( 'strftimerecent' );
if ( $past ) {
print_headline ( get_string ( 'pastchats' , 'chat' ) . ':' );
foreach ( $past as $cm ) {
$link = $CFG -> wwwroot . '/mod/chat/view.php?id=' . $cm -> id ;
$date = userdate ( $cm -> lasttime , $strftimerecent );
echo '<div class="head"><div class="date">' . $date . '</div></div>' ;
echo '<div class="info"><a href="' . $link . '">' . format_string ( $cm -> name , true ) . '</a></div>' ;
2003-07-10 04:46:03 +00:00
}
2005-03-10 13:30:57 +00:00
}
if ( $current ) {
2008-01-24 20:33:50 +00:00
print_headline ( get_string ( 'currentchats' , 'chat' ) . ':' );
$oldest = floor (( time () - $CFG -> chat_old_ping ) / 10 ) * 10 ; // better db caching
$timeold = time () - $CFG -> chat_old_ping ;
$timeold = floor ( $timeold / 10 ) * 10 ; // better db caching
$timeoldext = time () - ( $CFG -> chat_old_ping * 10 ); // JSless gui_basic needs much longer timeouts
$timeoldext = floor ( $timeoldext / 10 ) * 10 ; // better db caching
2008-06-08 15:49:36 +00:00
$params = array ( 'timeold' => $timeold , 'timeoldext' => $timeoldext , 'cmid' => $cm -> id );
$timeout = " AND (chu.version<>'basic' AND chu.lastping>:timeold) OR (chu.version='basic' AND chu.lastping>:timeoldext) " ;
2008-01-24 20:33:50 +00:00
foreach ( $current as $cm ) {
//count users first
if ( isset ( $cm -> mygroupids )) {
$groupselect = " AND (chu.groupid IN ( { $cm -> mygroupids } ) OR chu.groupid = 0) " ;
} else {
$groupselect = " " ;
}
2008-02-14 16:12:53 +00:00
2008-06-08 15:49:36 +00:00
if ( ! $users = $DB -> get_records_sql ( " SELECT u.id, u.firstname, u.lastname, u.email, u.picture
FROM { course_modules } cm
JOIN { chat } ch ON ch . id = cm . instance
JOIN { chat_users } chu ON chu . chatid = ch . id
JOIN { user } u ON u . id = chu . userid
WHERE cm . id = : cmid $timeout $groupselect
GROUP BY u . id , u . firstname , u . lastname , u . email , u . picture " , $params )) {
2008-01-24 20:33:50 +00:00
}
$link = $CFG -> wwwroot . '/mod/chat/view.php?id=' . $cm -> id ;
$date = userdate ( $cm -> lasttime , $strftimerecent );
echo '<div class="head"><div class="date">' . $date . '</div></div>' ;
echo '<div class="info"><a href="' . $link . '">' . format_string ( $cm -> name , true ) . '</a></div>' ;
echo '<div class="userlist">' ;
if ( $users ) {
echo '<ul>' ;
foreach ( $users as $user ) {
echo '<li>' . fullname ( $user , $viewfullnames ) . '</li>' ;
}
echo '</ul>' ;
}
echo '</div>' ;
}
2003-07-10 04:46:03 +00:00
}
return true ;
2003-07-07 06:44:16 +00:00
}
2005-03-10 13:30:57 +00:00
2003-07-07 06:44:16 +00:00
function chat_cron () {
/// Function to be run periodically according to the moodle cron
2004-08-10 13:50:58 +00:00
/// This function searches for things that need to be done, such
/// as sending out mail, toggling flags etc ...
2008-06-08 15:49:36 +00:00
global $DB ;
2003-07-07 06:44:16 +00:00
2003-07-25 06:47:06 +00:00
chat_update_chat_times ();
2003-07-09 12:53:26 +00:00
chat_delete_old_users ();
2008-01-08 20:58:24 +00:00
/// Delete old messages with a
/// single SQL query.
$subselect = " SELECT c.keepdays
2008-06-08 15:49:36 +00:00
FROM { chat } c
WHERE c . id = { chat_messages } . chatid " ;
2007-10-09 16:46:04 +00:00
2008-01-08 20:58:24 +00:00
$sql = " DELETE
2008-06-08 15:49:36 +00:00
FROM { chat_messages }
2008-07-24 03:15:03 +00:00
WHERE ( $subselect ) > 0 AND timestamp < ( " .time(). " - ( $subselect ) * 24 * 3600 ) " ;
$DB -> execute ( $sql );
$sql = " DELETE
FROM { chat_messages_current }
WHERE timestamp < ( " .time(). " - 8 * 3600 ) " ;
2007-10-09 16:46:04 +00:00
2008-06-08 15:49:36 +00:00
$DB -> execute ( $sql );
2003-07-21 03:16:19 +00:00
2003-07-07 06:44:16 +00:00
return true ;
}
2004-02-24 06:35:57 +00:00
function chat_get_participants ( $chatid , $groupid = 0 ) {
2003-09-29 15:27:30 +00:00
//Returns the users with data in one chat
//(users with records in chat_messages, students)
2008-06-08 15:49:36 +00:00
global $DB ;
2003-09-29 15:27:30 +00:00
2008-06-08 15:49:36 +00:00
$params = array ( 'groupid' => $groupid , 'chatid' => $chatid );
2003-09-29 15:27:30 +00:00
2004-02-24 06:35:57 +00:00
if ( $groupid ) {
2008-06-08 15:49:36 +00:00
$groupselect = " AND (c.groupid=:groupid OR c.groupid='0') " ;
2004-02-24 06:35:57 +00:00
} else {
$groupselect = " " ;
}
2003-09-29 15:27:30 +00:00
//Get students
2008-06-08 15:49:36 +00:00
$students = $DB -> get_records_sql ( " SELECT DISTINCT u.id, u.id
FROM { user } u , { chat_messages } c
WHERE c . chatid = : chatid $groupselect
AND u . id = c . userid " , $params );
2003-07-07 06:44:16 +00:00
2003-09-29 15:27:30 +00:00
//Return students array (it contains an array of unique users)
return ( $students );
}
2003-07-07 06:44:16 +00:00
2004-04-25 03:05:58 +00:00
function chat_refresh_events ( $courseid = 0 ) {
// This standard function will check all instances of this module
// and make sure there are up-to-date events created for each of them.
// If courseid = 0, then every chat event in the site is checked, else
// only chat events belonging to the course specified are checked.
// This function is used, in its new format, by restore_refresh_events()
2008-06-08 15:49:36 +00:00
global $DB ;
2004-04-25 03:05:58 +00:00
if ( $courseid ) {
2008-06-08 15:49:36 +00:00
if ( ! $chats = $DB -> get_records ( " chat " , array ( " course " => $courseid ))) {
2004-04-25 03:05:58 +00:00
return true ;
}
} else {
2008-06-08 15:49:36 +00:00
if ( ! $chats = $DB -> get_records ( " chat " )) {
2004-04-25 03:05:58 +00:00
return true ;
}
}
2008-06-08 15:49:36 +00:00
$moduleid = $DB -> get_field ( 'modules' , 'id' , array ( 'name' => 'chat' ));
2004-04-25 03:05:58 +00:00
foreach ( $chats as $chat ) {
$event = NULL ;
2008-06-08 15:49:36 +00:00
$event -> name = $chat -> name ;
$event -> description = $chat -> intro ;
2004-04-25 03:05:58 +00:00
$event -> timestart = $chat -> chattime ;
2008-06-08 15:49:36 +00:00
if ( $event -> id = $DB -> get_field ( 'event' , 'id' , array ( 'modulename' => 'chat' , 'instance' => $chat -> id ))) {
2004-04-25 03:05:58 +00:00
update_event ( $event );
} else {
$event -> courseid = $chat -> course ;
$event -> groupid = 0 ;
$event -> userid = 0 ;
$event -> modulename = 'chat' ;
$event -> instance = $chat -> id ;
$event -> eventtype = $chat -> schedule ;
$event -> timeduration = 0 ;
2008-06-08 15:49:36 +00:00
$event -> visible = $DB -> get_field ( 'course_modules' , 'visible' , array ( 'module' => $moduleid , 'instance' => $chat -> id ));
2004-08-10 13:50:58 +00:00
2004-04-25 03:05:58 +00:00
add_event ( $event );
}
}
return true ;
}
2005-02-03 20:39:27 +00:00
2003-07-07 06:44:16 +00:00
//////////////////////////////////////////////////////////////////////
/// Functions that require some SQL
2007-08-27 02:28:35 +00:00
function chat_get_users ( $chatid , $groupid = 0 , $groupingid = 0 ) {
2008-06-08 15:49:36 +00:00
global $DB ;
2003-07-07 06:44:16 +00:00
2008-06-08 15:49:36 +00:00
$params = array ( 'chatid' => $chatid , 'groupid' => $groupid , 'groupingid' => $groupingid );
2004-02-24 06:35:57 +00:00
if ( $groupid ) {
2008-06-08 15:49:36 +00:00
$groupselect = " AND (c.groupid=:groupid OR c.groupid='0') " ;
2004-02-24 06:35:57 +00:00
} else {
$groupselect = " " ;
}
2008-07-24 03:15:03 +00:00
2007-08-27 02:28:35 +00:00
if ( ! empty ( $CFG -> enablegroupings ) && ! ( empty ( $groupingid ))) {
2008-06-08 15:49:36 +00:00
$groupingjoin = " JOIN { groups_members} gm ON u.id = gm.userid
JOIN { groupings_groups } gg ON gm . groupid = gg . groupid AND gg . groupingid = : groupingid " ;
2008-07-24 03:15:03 +00:00
2007-08-27 02:28:35 +00:00
} else {
$groupingjoin = '' ;
}
2004-08-10 13:50:58 +00:00
2008-06-08 15:49:36 +00:00
return $DB -> get_records_sql ( " SELECT DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping, u.imagealt
FROM { chat_users } c
JOIN { user } u ON u . id = c . userid
$groupingjoin
WHERE c . chatid = : chatid
$groupselect
ORDER BY c . firstping ASC " , $params );
2003-07-07 06:44:16 +00:00
}
2004-02-24 06:35:57 +00:00
function chat_get_latest_message ( $chatid , $groupid = 0 ) {
2008-05-15 21:40:00 +00:00
global $DB ;
2003-07-07 06:44:16 +00:00
2008-06-08 15:49:36 +00:00
$params = array ( 'chatid' => $chatid , 'groupid' => $groupid );
2003-07-07 06:44:16 +00:00
2004-02-24 06:35:57 +00:00
if ( $groupid ) {
2008-06-08 15:49:36 +00:00
$groupselect = " AND (groupid=:groupid OR groupid=0) " ;
2004-02-24 06:35:57 +00:00
} else {
$groupselect = " " ;
}
2008-05-15 21:40:00 +00:00
$sql = " SELECT *
2008-07-24 03:15:03 +00:00
FROM { chat_messages_current }
2008-06-08 15:49:36 +00:00
WHERE chatid = : chatid
2008-05-15 21:40:00 +00:00
$groupselect
ORDER BY timestamp DESC " ;
2007-10-10 12:19:27 +00:00
2008-05-15 21:40:00 +00:00
return $DB -> get_record_sql ( $sql , $params , true );
2003-07-07 06:44:16 +00:00
}
2003-07-25 09:01:02 +00:00
2003-07-07 06:44:16 +00:00
//////////////////////////////////////////////////////////////////////
2005-02-03 20:39:27 +00:00
// login if not already logged in
2003-07-07 06:44:16 +00:00
2005-02-10 19:14:53 +00:00
function chat_login_user ( $chatid , $version , $groupid , $course ) {
2008-06-08 15:49:36 +00:00
global $USER , $DB ;
if (( $version != 'sockets' ) and $chatuser = $DB -> get_record ( 'chat_users' , array ( 'chatid' => $chatid , 'userid' => $USER -> id , 'groupid' => $groupid ))) {
2005-02-03 20:39:27 +00:00
$chatuser -> version = $version ;
2006-06-09 10:04:43 +00:00
$chatuser -> ip = $USER -> lastip ;
2005-02-03 20:39:27 +00:00
$chatuser -> lastping = time ();
$chatuser -> lang = current_language ();
2003-07-07 06:44:16 +00:00
2006-06-09 10:04:43 +00:00
// Sometimes $USER->lastip is not setup properly
2006-04-18 22:40:56 +00:00
// during login. Update with current value if possible
2006-04-19 21:22:36 +00:00
// or provide a dummy value for the db
2006-04-18 22:40:56 +00:00
if ( empty ( $chatuser -> ip )) {
$chatuser -> ip = getremoteaddr ();
if ( empty ( $chatuser -> ip )) {
2006-04-19 21:22:36 +00:00
$chatuser -> ip = '' ;
2006-04-18 22:40:56 +00:00
}
}
2005-02-03 20:39:27 +00:00
if (( $chatuser -> course != $course -> id )
or ( $chatuser -> userid != $USER -> id )) {
return false ;
}
2008-06-08 15:49:36 +00:00
if ( ! $DB -> update_record ( 'chat_users' , $chatuser )) {
2005-02-03 20:39:27 +00:00
return false ;
}
} else {
2006-12-29 18:33:41 +00:00
$chatuser = new object ();
2005-02-03 20:39:27 +00:00
$chatuser -> chatid = $chatid ;
$chatuser -> userid = $USER -> id ;
$chatuser -> groupid = $groupid ;
$chatuser -> version = $version ;
2006-06-09 10:04:43 +00:00
$chatuser -> ip = $USER -> lastip ;
2005-02-03 20:39:27 +00:00
$chatuser -> lastping = $chatuser -> firstping = $chatuser -> lastmessageping = time ();
$chatuser -> sid = random_string ( 32 );
2005-02-07 20:33:44 +00:00
$chatuser -> course = $course -> id ; //caching - needed for current_language too
$chatuser -> lang = current_language (); //caching - to resource intensive to find out later
2005-02-03 20:39:27 +00:00
2006-06-09 10:04:43 +00:00
// Sometimes $USER->lastip is not setup properly
2006-05-02 05:20:35 +00:00
// during login. Update with current value if possible
// or provide a dummy value for the db
if ( empty ( $chatuser -> ip )) {
$chatuser -> ip = getremoteaddr ();
if ( empty ( $chatuser -> ip )) {
$chatuser -> ip = '' ;
}
}
2008-06-08 15:49:36 +00:00
if ( ! $DB -> insert_record ( 'chat_users' , $chatuser )) {
2005-02-03 20:39:27 +00:00
return false ;
}
2005-02-10 19:14:53 +00:00
if ( $version == 'sockets' ) {
// do not send 'enter' message, chatd will do it
} else {
2006-12-29 18:33:41 +00:00
$message = new object ();
2005-02-04 21:14:19 +00:00
$message -> chatid = $chatuser -> chatid ;
$message -> userid = $chatuser -> userid ;
$message -> groupid = $groupid ;
$message -> message = 'enter' ;
$message -> system = 1 ;
$message -> timestamp = time ();
2008-07-24 03:15:03 +00:00
if ( ! $DB -> insert_record ( 'chat_messages' , $message ) || ! $DB -> insert_record ( 'chat_messages_current' , $message )) {
2008-06-06 08:46:48 +00:00
print_error ( 'cantinsert' , 'chat' );
2005-02-04 21:14:19 +00:00
}
2005-02-03 20:39:27 +00:00
}
2003-07-07 06:44:16 +00:00
}
return $chatuser -> sid ;
}
2003-07-09 12:53:26 +00:00
function chat_delete_old_users () {
// Delete the old and in the way
2008-06-08 15:49:36 +00:00
global $CFG , $DB ;
2003-08-23 14:03:14 +00:00
2005-02-12 10:54:25 +00:00
$timeold = time () - $CFG -> chat_old_ping ;
2006-12-30 19:45:24 +00:00
$timeoldext = time () - ( $CFG -> chat_old_ping * 10 ); // JSless gui_basic needs much longer timeouts
2005-02-10 19:14:53 +00:00
2008-06-08 15:49:36 +00:00
$query = " (version<>'basic' AND lastping<?) OR (version='basic' AND lastping<?) " ;
$params = array ( $timeold , $timeoldext );
2003-07-09 12:53:26 +00:00
2008-06-08 15:49:36 +00:00
if ( $oldusers = $DB -> get_records_select ( 'chat_users' , $query , $params ) ) {
$DB -> delete_records_select ( 'chat_users' , $query , $params );
2003-07-09 12:53:26 +00:00
foreach ( $oldusers as $olduser ) {
2006-12-29 18:33:41 +00:00
$message = new object ();
2005-02-03 20:39:27 +00:00
$message -> chatid = $olduser -> chatid ;
$message -> userid = $olduser -> userid ;
$message -> groupid = $olduser -> groupid ;
$message -> message = 'exit' ;
$message -> system = 1 ;
2003-07-09 12:53:26 +00:00
$message -> timestamp = time ();
2004-08-10 13:50:58 +00:00
2008-07-24 03:15:03 +00:00
if ( ! $DB -> insert_record ( 'chat_messages' , $message ) || ! $DB -> insert_record ( 'chat_messages_current' , $message ) ) {
2008-06-06 08:46:48 +00:00
print_error ( 'cantinsert' , 'chat' );
2003-07-09 12:53:26 +00:00
}
}
}
}
2003-07-07 06:44:16 +00:00
2003-07-21 03:16:19 +00:00
2003-07-25 06:47:06 +00:00
function chat_update_chat_times ( $chatid = 0 ) {
/// Updates chat records so that the next chat time is correct
2008-06-08 15:49:36 +00:00
global $DB ;
2003-07-25 06:47:06 +00:00
$timenow = time ();
2008-06-08 15:49:36 +00:00
$params = array ( 'timenow' => $timenow , 'chatid' => $chatid );
2003-07-25 06:47:06 +00:00
if ( $chatid ) {
2008-06-08 15:49:36 +00:00
if ( ! $chats [] = $DB -> get_record_select ( " chat " , " id = :chatid AND chattime <= :timenow AND schedule > 0 " , $params )) {
2003-07-25 06:47:06 +00:00
return ;
}
} else {
2008-06-08 15:49:36 +00:00
if ( ! $chats = $DB -> get_records_select ( " chat " , " chattime <= :timenow AND schedule > 0 " , $params )) {
2003-07-25 06:47:06 +00:00
return ;
}
}
foreach ( $chats as $chat ) {
2004-03-29 06:02:21 +00:00
unset ( $chat -> name );
unset ( $chat -> intro );
2003-07-25 06:47:06 +00:00
switch ( $chat -> schedule ) {
case 1 : // Single event - turn off schedule and disable
$chat -> chattime = 0 ;
$chat -> schedule = 0 ;
break ;
case 2 : // Repeat daily
2007-09-19 07:21:09 +00:00
while ( $chat -> chattime <= $timenow ) {
$chat -> chattime += 24 * 3600 ;
}
2003-07-25 06:47:06 +00:00
break ;
case 3 : // Repeat weekly
2007-09-19 07:21:09 +00:00
while ( $chat -> chattime <= $timenow ) {
$chat -> chattime += 7 * 24 * 3600 ;
}
2003-07-25 06:47:06 +00:00
break ;
}
2008-06-08 15:49:36 +00:00
$DB -> update_record ( " chat " , $chat );
$event = new object (); // Update calendar too
2004-04-25 03:05:58 +00:00
2008-06-08 15:49:36 +00:00
$cond = " modulename='chat' AND instance = :chatid AND timestart <> :chattime " ;
$params = array ( 'chattime' => $chat -> chattime , 'chatid' => $chatid );
if ( $event -> id = $DB -> get_field_select ( 'event' , 'id' , $cond , $params )) {
2004-04-25 03:05:58 +00:00
$event -> timestart = $chat -> chattime ;
update_event ( $event );
}
2003-07-25 06:47:06 +00:00
}
}
2005-09-10 22:57:21 +00:00
function chat_format_message_manually ( $message , $courseid , $sender , $currentuser , $chat_lastrow = NULL ) {
2005-10-24 17:30:56 +00:00
global $CFG , $USER ;
2003-07-07 06:44:16 +00:00
2006-12-29 18:33:41 +00:00
$output = new object ();
2005-02-03 20:39:27 +00:00
$output -> beep = false ; // by default
$output -> refreshusers = false ; // by default
2003-07-09 12:53:26 +00:00
2005-10-24 17:30:56 +00:00
// Use get_user_timezone() to find the correct timezone for displaying this message:
// It's either the current user's timezone or else decided by some Moodle config setting
2005-10-25 01:55:21 +00:00
// First, "reset" $USER->timezone (which could have been set by a previous call to here)
// because otherwise the value for the previous $currentuser will take precedence over $CFG->timezone
$USER -> timezone = 99 ;
2005-10-24 17:30:56 +00:00
$tz = get_user_timezone ( $currentuser -> timezone );
2004-08-10 13:50:58 +00:00
2005-10-24 17:30:56 +00:00
// Before formatting the message time string, set $USER->timezone to the above.
// This will allow dst_offset_on (called by userdate) to work correctly, otherwise the
// message times appear off because DST is not taken into account when it should be.
$USER -> timezone = $tz ;
2004-08-10 13:50:58 +00:00
$message -> strtime = userdate ( $message -> timestamp , get_string ( 'strftimemessage' , 'chat' ), $tz );
$message -> picture = print_user_picture ( $sender -> id , 0 , $sender -> picture , false , true , false );
2003-07-24 02:25:23 +00:00
if ( $courseid ) {
2007-01-09 04:50:07 +00:00
$message -> picture = " <a onclick= \" window.open(' $CFG->wwwroot /user/view.php?id= $sender->id &course= $courseid ') \" href= \" $CFG->wwwroot /user/view.php?id= $sender->id &course= $courseid\ " > $message -> picture </ a > " ;
2003-07-24 02:25:23 +00:00
}
2003-07-07 06:44:16 +00:00
2005-09-10 22:57:21 +00:00
//Calculate the row class
if ( $chat_lastrow !== NULL ) {
$rowclass = ' class="r' . $chat_lastrow . '" ' ;
} else {
$rowclass = '' ;
}
2004-08-10 13:50:58 +00:00
// Start processing the message
2003-07-07 06:44:16 +00:00
2004-08-10 13:50:58 +00:00
if ( ! empty ( $message -> system )) {
// System event
$output -> text = $message -> strtime . ': ' . get_string ( 'message' . $message -> message , 'chat' , fullname ( $sender ));
2005-09-10 22:57:21 +00:00
$output -> html = '<table class="chat-event"><tr' . $rowclass . '><td class="picture">' . $message -> picture . '</td><td class="text">' ;
$output -> html .= '<span class="event">' . $output -> text . '</span></td></tr></table>' ;
2008-07-24 03:15:03 +00:00
$output -> basic = '<dl><dt class="event">' . $message -> strtime . ': ' . get_string ( 'message' . $message -> message , 'chat' , fullname ( $sender )) . '</dt></dl>' ;
2003-07-09 12:53:26 +00:00
2005-02-03 20:39:27 +00:00
if ( $message -> message == 'exit' or $message -> message == 'enter' ) {
$output -> refreshusers = true ; //force user panel refresh ASAP
}
2003-07-07 06:44:16 +00:00
return $output ;
}
2004-08-11 14:45:40 +00:00
// It's not a system event
2004-08-10 13:50:58 +00:00
$text = $message -> message ;
2004-08-11 14:45:40 +00:00
/// Parse the text to clean and filter it
2006-12-29 18:33:41 +00:00
$options = new object ();
2004-08-11 14:45:40 +00:00
$options -> para = false ;
$text = format_text ( $text , FORMAT_MOODLE , $options , $courseid );
2007-03-01 07:19:57 +00:00
2004-08-10 13:50:58 +00:00
// And now check for special cases
2007-03-01 07:19:57 +00:00
$special = false ;
2004-08-10 13:50:58 +00:00
if ( substr ( $text , 0 , 5 ) == 'beep ' ) {
2008-07-24 03:15:03 +00:00
/// It's a beep!
2007-03-01 07:19:57 +00:00
$special = true ;
2003-07-09 12:53:26 +00:00
$beepwho = trim ( substr ( $text , 5 ));
2003-07-09 04:17:27 +00:00
2004-08-10 13:50:58 +00:00
if ( $beepwho == 'all' ) { // everyone
$outinfo = $message -> strtime . ': ' . get_string ( 'messagebeepseveryone' , 'chat' , fullname ( $sender ));
$outmain = '' ;
$output -> beep = true ; // (eventually this should be set to
2003-07-09 12:53:26 +00:00
// to a filename uploaded by the user)
2004-08-11 14:45:40 +00:00
} else if ( $beepwho == $currentuser -> id ) { // current user
2004-08-10 13:50:58 +00:00
$outinfo = $message -> strtime . ': ' . get_string ( 'messagebeepsyou' , 'chat' , fullname ( $sender ));
$outmain = '' ;
2003-07-09 12:53:26 +00:00
$output -> beep = true ;
2006-11-09 18:25:58 +00:00
2006-03-24 14:44:55 +00:00
} else { //something is not caught?
2003-07-09 12:53:26 +00:00
return false ;
}
2004-08-10 13:50:58 +00:00
} else if ( substr ( $text , 0 , 1 ) == '/' ) { /// It's a user command
2007-03-01 07:19:57 +00:00
if ( trim ( substr ( $text , 0 , 4 )) == '/me' ) {
$special = true ;
2004-08-10 13:50:58 +00:00
$outinfo = $message -> strtime ;
$outmain = $sender -> firstname . ' ' . substr ( $text , 4 );
2003-07-07 06:44:16 +00:00
}
2007-03-01 07:19:57 +00:00
}
2003-07-09 04:17:27 +00:00
2007-03-01 07:19:57 +00:00
if ( ! $special ) {
2004-08-10 13:50:58 +00:00
$outinfo = $message -> strtime . ' ' . $sender -> firstname ;
2003-07-09 12:53:26 +00:00
$outmain = $text ;
2003-07-07 06:44:16 +00:00
}
2006-11-09 18:25:58 +00:00
2003-07-09 04:17:27 +00:00
/// Format the message as a small table
2003-07-07 06:44:16 +00:00
2004-08-10 13:50:58 +00:00
$output -> text = strip_tags ( $outinfo . ': ' . $outmain );
2003-07-09 12:53:26 +00:00
2006-04-18 07:28:44 +00:00
$output -> html = " <table class= \" chat-message \" ><tr $rowclass ><td class= \" picture \" valign= \" top \" > $message->picture </td><td class= \" text \" > " ;
2005-09-10 22:57:21 +00:00
$output -> html .= " <span class= \" title \" > $outinfo </span> " ;
2003-07-09 12:53:26 +00:00
if ( $outmain ) {
$output -> html .= " : $outmain " ;
2008-07-24 03:15:03 +00:00
$output -> basic = '<dl><dt class="title">' . $outinfo . ':</dt><dd class="text">' . $outmain . '</dd></dl>' ;
2006-12-29 18:33:41 +00:00
} else {
2008-07-24 03:15:03 +00:00
$output -> basic = '<dl><dt class="title">' . $outinfo . '</dt></dl>' ;
2003-07-09 12:53:26 +00:00
}
2005-09-10 22:57:21 +00:00
$output -> html .= " </td></tr></table> " ;
2003-07-09 12:53:26 +00:00
return $output ;
2004-08-10 13:50:58 +00:00
}
2005-09-10 22:57:21 +00:00
function chat_format_message ( $message , $courseid , $currentuser , $chat_lastrow = NULL ) {
2004-08-10 13:50:58 +00:00
/// Given a message object full of information, this function
/// formats it appropriately into text and html, then
/// returns the formatted data.
2008-06-08 15:49:36 +00:00
global $DB ;
2004-08-10 13:50:58 +00:00
2006-04-18 07:14:21 +00:00
static $users ; // Cache user lookups
if ( isset ( $users [ $message -> userid ])) {
$user = $users [ $message -> userid ];
2008-06-08 15:49:36 +00:00
} else if ( $user = $DB -> get_record ( 'user' , array ( 'id' => $message -> userid ), 'id,picture,firstname,lastname' )) {
2006-04-18 07:14:21 +00:00
$users [ $message -> userid ] = $user ;
} else {
return NULL ;
2004-08-10 13:50:58 +00:00
}
2005-09-10 22:57:21 +00:00
return chat_format_message_manually ( $message , $courseid , $user , $currentuser , $chat_lastrow );
2003-07-07 06:44:16 +00:00
}
2005-09-01 04:14:31 +00:00
function chat_get_view_actions () {
return array ( 'view' , 'view all' , 'report' );
}
function chat_get_post_actions () {
return array ( 'talk' );
}
2006-03-14 22:28:28 +00:00
function chat_print_overview ( $courses , & $htmlarray ) {
global $USER , $CFG ;
if ( empty ( $courses ) || ! is_array ( $courses ) || count ( $courses ) == 0 ) {
return array ();
}
if ( ! $chats = get_all_instances_in_courses ( 'chat' , $courses )) {
return ;
}
$strchat = get_string ( 'modulename' , 'chat' );
$strnextsession = get_string ( 'nextsession' , 'chat' );
foreach ( $chats as $chat ) {
if ( $chat -> chattime and $chat -> schedule ) { // A chat is scheduled
2006-03-15 04:01:10 +00:00
$str = '<div class="chat overview"><div class="name">' .
$strchat . ': <a ' . ( $chat -> visible ? '' : ' class="dimmed"' ) .
' href="' . $CFG -> wwwroot . '/mod/chat/view.php?id=' . $chat -> coursemodule . '">' .
$chat -> name . '</a></div>' ;
$str .= '<div class="info">' . $strnextsession . ': ' . userdate ( $chat -> chattime ) . '</div></div>' ;
if ( empty ( $htmlarray [ $chat -> course ][ 'chat' ])) {
$htmlarray [ $chat -> course ][ 'chat' ] = $str ;
} else {
$htmlarray [ $chat -> course ][ 'chat' ] .= $str ;
}
2006-03-14 22:28:28 +00:00
}
}
}
2007-11-29 14:43:04 +00:00
/**
* Implementation of the function for printing the form elements that control
* whether the course reset functionality affects the chat .
* @ param $mform form passed by reference
*/
function chat_reset_course_form_definition ( & $mform ) {
$mform -> addElement ( 'header' , 'chatheader' , get_string ( 'modulenameplural' , 'chat' ));
$mform -> addElement ( 'advcheckbox' , 'reset_chat' , get_string ( 'removemessages' , 'chat' ));
}
/**
* Course reset form defaults .
*/
function chat_reset_course_form_defaults ( $course ) {
return array ( 'reset_chat' => 1 );
}
/**
* Actual implementation of the rest coures functionality , delete all the
* chat messages for course $data -> courseid .
* @ param $data the data submitted from the reset course .
* @ return array status array
*/
function chat_reset_userdata ( $data ) {
2008-06-08 15:49:36 +00:00
global $CFG , $DB ;
2007-11-29 14:43:04 +00:00
$componentstr = get_string ( 'modulenameplural' , 'chat' );
$status = array ();
if ( ! empty ( $data -> reset_chat )) {
$chatessql = " SELECT ch.id
2008-06-08 15:49:36 +00:00
FROM { chat } ch
WHERE ch . course = ? " ;
$params = array ( $data -> courseid );
2007-11-29 14:43:04 +00:00
2008-06-08 15:49:36 +00:00
$DB -> delete_records_select ( 'chat_messages' , " chatid IN ( $chatessql ) " , $params );
2008-07-24 03:15:03 +00:00
$DB -> delete_records_select ( 'chat_messages_current' , " chatid IN ( $chatessql ) " , $params );
2008-06-08 15:49:36 +00:00
$DB -> delete_records_select ( 'chat_users' , " chatid IN ( $chatessql ) " , $params );
2007-11-29 14:43:04 +00:00
$status [] = array ( 'component' => $componentstr , 'item' => get_string ( 'removemessages' , 'chat' ), 'error' => false );
}
/// updating dates - shift may be negative too
if ( $data -> timeshift ) {
shift_course_mod_dates ( 'chat' , array ( 'chattime' ), $data -> timeshift , $data -> courseid );
$status [] = array ( 'component' => $componentstr , 'item' => get_string ( 'datechanged' ), 'error' => false );
}
return $status ;
}
2008-07-24 21:59:13 +00:00
/**
* Returns all other caps used in module
*/
function chat_get_extra_capabilities () {
return array ( 'moodle/site:accessallgroups' , 'moodle/site:viewfullnames' );
}
2008-08-18 10:16:56 +00:00
require_once ( $CFG -> libdir . '/portfoliolib.php' );
class chat_portfolio_caller extends portfolio_module_caller_base {
private $chat ;
private $start ;
private $end ;
public function __construct ( $callbackargs ) {
global $DB , $USER ;
if ( ! $this -> cm = get_coursemodule_from_id ( 'chat' , $callbackargs [ 'id' ])) {
2008-08-19 14:20:32 +00:00
throw new portfolio_caller_exception ( 'invalidid' , 'chat' );
2008-08-18 10:16:56 +00:00
}
$this -> chat = $DB -> get_record ( 'chat' , array ( 'id' => $this -> cm -> instance ));
$select = 'chatid = ?' ;
$params = array ( $this -> chat -> id );
if ( array_key_exists ( 'start' , $callbackargs ) && array_key_exists ( 'end' , $callbackargs )
&& ! empty ( $callbackargs [ 'start' ]) && ! empty ( $callbackargs [ 'end' ])) {
$select .= ' AND timestamp >= ? AND timestamp <= ?' ;
$params [] = $callbackargs [ 'start' ];
$params [] = $callbackargs [ 'end' ];
$this -> start = $callbackargs [ 'start' ];
$this -> end = $callbackargs [ 'end' ];
}
$this -> messages = $DB -> get_records_select (
'chat_messages' ,
$select ,
$params ,
'timestamp ASC'
);
$select .= ' AND userid = ?' ;
$params [] = $USER -> id ;
$this -> participated = $DB -> record_exists_select (
'chat_messages' ,
$select ,
$params
);
2008-09-05 17:07:22 +00:00
$this -> supportedformats = array ( PORTFOLIO_FORMAT_HTML );
2008-08-18 10:16:56 +00:00
}
public function expected_time () {
return PORTFOLIO_TIME_LOW ;
}
public function get_sha1 () {
$str = '' ;
ksort ( $this -> messages );
foreach ( $this -> messages as $m ) {
$str .= implode ( '' , ( array ) $m );
}
return sha1 ( $str );
}
public function check_permissions () {
$context = get_context_instance ( CONTEXT_MODULE , $this -> cm -> id );
return has_capability ( 'mod/chat:exportsession' , $context )
|| ( $this -> participated
&& has_capability ( 'mod/chat:exportparticipatedsession' , $context ));
}
public function prepare_package () {
$content = '' ;
foreach ( $this -> messages as $message ) { // We are walking FORWARDS through messages
$m = clone $message ; // grrrrrr
$formatmessage = chat_format_message ( $m , null , $this -> user );
if ( ! isset ( $formatmessage -> html )) {
continue ;
}
$content .= $formatmessage -> html ;
}
$content = preg_replace ( '/\<img[^>]*\>/' , '' , $content );
return $this -> exporter -> write_new_file ( $content , clean_filename ( $this -> cm -> name . '-session.html' ));
}
public static function display_name () {
return get_string ( 'modulename' , 'chat' );
}
public function get_return_url () {
global $CFG ;
return $CFG -> wwwroot . '/mod/chat/report.php?id='
. $this -> cm -> id . (( isset ( $this -> start ))
? '&start=' . $this -> start . '&end=' . $this -> end
: '' );
}
}
2003-07-07 06:44:16 +00:00
?>