Commit a325ebdb authored by Penny Leach's avatar Penny Leach
Browse files

untested rough beginnings of activity lib

parent 292657e9
......@@ -76,6 +76,7 @@ require('dml.php');
require('ddl.php');
require('constants.php');
require('web.php');
require('activity.php');
// Database access functions
require('adodb/adodb-exceptions.inc.php');
......
<?php
/**
* This program is part of Mahara
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package mahara
* @subpackage core
* @author Penny Leach <penny@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
/**
* This is the function to call whenever anything happens
* that is going to end up on a user's activity page.
*
* @param string $activitytype type of activity
* @param mixed $data data
*/
function activity_occured($activitytype, $data) {
if (!$at = get_record('activity_type', 'name', $activitytype)) {
throw new Exception("Invalid activity type $activitytype");
}
if (!empty($at->delay)) {
$delayed = new StdClass;
$delayed->type = $activitytype;
$delayed->data = serialize($data);
$delayed->ctime = db_format_timestamp(time());
insert_record('activity_queue', $delayed);
}
else {
handle_activity($at, $data);
}
}
/**
* This function dispatches all the activity stuff
* to whatever notification plugin it needs to
* and figures out all the implications of
* activity and who needs to know about it.
*
* @param object $activitytype record from activity_type
* @param mixed $data
*/
function handle_activity($activitytype, $data) {
$users = array();
$prefix = get_config('dbprefix');
if (!empty($activitytype->admin)) {
$users = activity_get_users($activitytype->name, null, null, true);
}
else {
switch ($activitytype->name) {
// easy ones first :)
case 'maharamessage':
$users = activity_get_users($activitytype->name, null, $data->users);
break;
case 'usermessage':
$users = activity_get_users($activitytype->name, array($data->userto));
break;
case 'feedback':
if ($data->view) {
$userid = get_field('view', 'owner', 'id', $data->view);
}
else if ($data->artefact) {
$userid = get_field('artefact', 'owner', 'id', $data->artefact);
}
$users = activity_get_users($activitytype->name, array($userid));
break;
// and now the harder ones
case 'watchlist':
if ($data->view) {
$sql = 'SELECT u.*, p.method
FROM ' . $prefix . 'watchlist_view wv
JOIN ' . $prefix . 'usr u
ON wa.user = u.id
JOIN ' . $prefix . 'usr_preference p
ON p.user = u.id
WHERE pc.activity = ?
AND wv.view = ?
';
$users = get_records_sql($sql, array('watchlist', $data->view));
}
else if ($data->artefact) {
$sql = 'SELECT DISTINCT u.*, p.method
FROM ' . $prefix . 'watchlist_artefact wa
JOIN ' . $prefix . 'artefact_parent_cache pc
ON (pc.parent = wa.artefact OR pc.artefact = wa.artefact)
JOIN ' . $prefix . 'usr u
ON wa.user = u.id
JOIN ' . $prefix . 'usr_preference p
ON p.user = u.id
WHERE pc.activity = ?
AND (pc.parent = ? OR wa.artefact = ?)
';
$users = get_records_sql($sql, array('watchlist', $data->artefact));
}
else {
throw new InvalidArgumentException("Invalid watchlist type");
}
break;
case 'newview':
// add users on friendslist, userlist or grouplist...
$sql = 'SELECT userid, u.*, p.method
FROM (
SELECT (CASE WHEN usr1 = ? THEN usr2 ELSE usr1 END) AS userid
FROM ' . $prefix . 'usr_friend
WHERE (usr1 = ? OR usr2 = ?)
UNION SELECT member AS userid
FROM ' . $prefix . 'usr_group_member m
JOIN ' . $prefix . 'view_access_group g ON m.group = g.group
WHERE g.view = ?
UNION SELECT usr AS userid
FROM ' . $prefix . 'view_access_usr u
WHERE u.view = ?
) AS userlist
JOIN ' . $prefix . 'usr u ON u.id = userlist.userid
JOIN ' . $prefix . 'usr_activity_preference p ON p.usr = u.id';
$users = get_records_sql($sql, array($data->owner, $data->owner, $data->owner,
$data->view, $data->view));
break;
}
}
safe_require('notification', 'internal', 'lib.php', 'require_once');
foreach ($users as $user) {
if ($user->method != 'internal') {
safe_require('notification', $method, 'lib.php', 'require_once');
call_static_method(generate_class_name('notification', $method), 'notify_user', $user, $data);
}
// always do internal
call_static_method('PluginNotificationInternal', 'notify_user', $user, $data);
}
}
/**
* this function returns an array of users
* for a particular activitytype
* including the notification method.
*
* @param string $activitytype the name of the activity type
* @param array $userids an array of userids to filter by
* @param array $userobjs an array of user objects to filterby
* @param bool $adminonly whether to filter by admin flag
* @return array of users
*/
function activity_get_users($activitytype, $userids=null, $userobjs=null, $adminonly=false) {
$values = array($activitytype);
$sql = 'SELECT u.*, p.method
FROM ' . get_config('dbprefix') .'usr u
JOIN ' . get_config('dbprefix') . 'usr_activity_preference p
ON p.user = u.id
WHERE p.activity = ? ';
if (!empty($adminonly)) {
$sql .= ' AND u.admin = ? ';
$values[] = 1;
}
if (!empty($userobjs) && is_array($userobjs)) {
$sql .= ' AND u.id IN (' . implode(',',db_array_to_ph($userobjs)) . ')';
$values = array_merge($values, array_to_fields($userobjs));
}
else if (!empty($userids) && is_array($userids)) {
$sql .= ' AND u.id IN (' . implode(',',db_array_to_ph($userids)) . ')';
$values = array_merge($values, $userids);
}
}
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment