Commit 549cd6d6 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Send admin notifications to institutional admins too

parent 6b688bb5
......@@ -91,7 +91,6 @@ function handle_activity($activitytype, $data, $cron=false) {
$users = array();
if (!empty($activitytype->admin)) {
$users = activity_get_users($activitytype->name, null, null, true);
// validation stuff
switch ($activitytype->name) {
case 'contactus':
......@@ -104,6 +103,7 @@ function handle_activity($activitytype, $data, $cron=false) {
$data->subject = get_string('newcontactus', 'activity');
if (!empty($data->userfrom)) {
$data->url = get_config('wwwroot') . 'user/view.php?id=' . $data->userfrom;
$userid = $data->userfrom;
}
break;
case 'objectionable':
......@@ -113,13 +113,14 @@ function handle_activity($activitytype, $data, $cron=false) {
if (empty($data->message)) {
throw new InvalidArgumentException("Objectionable content requires a message");
}
if (!$viewtitle = get_field('view', 'title', 'id', $data->view)) {
if (!$view = get_record('view', 'id', $data->view, null, null, null, null, 'title,owner')) {
throw new InvalidArgumentException("Couldn't find view with id " . $data->view);
}
$userid = $view->owner;
if (empty($data->artefact)) {
$data->url = get_config('wwwroot') . 'view/view.php?id=' . $data->view;
$data->subject = get_string('objectionablecontentview', 'activity')
. ' ' . get_string('onview', 'activity') . ' ' . $viewtitle;
. ' ' . get_string('onview', 'activity') . ' ' . $view->title;
}
else {
$data->url = get_config('wwwroot') . 'view/view.php?artefact=' . $data->artefact . '&id=' . $data->view;
......@@ -134,10 +135,17 @@ function handle_activity($activitytype, $data, $cron=false) {
$userstring = $data->username . ' (' . $data->fullname . ') (userid:' . $data->userid . ')' ;
$data->subject = get_string('virusrepeatsubject', 'mahara', $userstring);
$data->message = get_string('virusrepeatmessage');
$userid = $data->userid;
break;
case 'virusrelease':
break;
}
if (empty($userid)) {
$users = activity_get_users($activitytype->name, null, null, true);
} else {
$userinstitutions = get_column('usr_institution', 'institution', 'usr', $userid);
$users = activity_get_users($activitytype->name, null, null, null, $userinstitutions);
}
}
else {
switch ($activitytype->name) {
......@@ -161,13 +169,8 @@ function handle_activity($activitytype, $data, $cron=false) {
$data->institution->displayname);
$data->message = get_string('institutionrequestmessage', 'activity');
$data->url = get_config('wwwroot') . 'admin/users/institutionusers.php';
$admins = get_column_sql('
SELECT u.id
FROM {usr} u
LEFT OUTER JOIN {usr_institution} i ON (u.id = i.usr AND i.institution = ?)
WHERE u.admin = 1 OR i.admin = 1',
array($data->institution->name));
$users = activity_get_users($activitytype->name, $admins);
$users = activity_get_users($activitytype->name, null, null, null,
array($data->institution->name));
} else if ($data->messagetype == 'invite') {
if (!is_array($data->users) || empty($data->users)) {
throw new InvalidArgumentException("Institution invitations expect an array of users");
......@@ -363,19 +366,22 @@ function handle_activity($activitytype, $data, $cron=false) {
* @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
* @param array $admininstitutions list of institution names to get admins for
* @return array of users
*/
function activity_get_users($activitytype, $userids=null, $userobjs=null, $adminonly=false) {
function activity_get_users($activitytype, $userids=null, $userobjs=null, $adminonly=false,
$admininstitutions = array()) {
$values = array($activitytype);
$sql = 'SELECT u.*, p.method
FROM {usr} u
LEFT JOIN {usr_activity_preference} p
ON p.usr = u.id
WHERE (p.activity = ? ' . (empty($adminonly) ? ' OR p.activity IS NULL' : '') . ')';
if (!empty($adminonly)) {
$sql .= ' AND u.admin = ? ';
$values[] = 1;
}
$sql = '
SELECT
u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff, p.method
FROM {usr} u
LEFT JOIN {usr_activity_preference} p
ON p.usr = u.id ' . (empty($admininstitutions) ? '' : '
LEFT OUTER JOIN {usr_institution} ui
ON (u.id = ui.usr
AND ui.institution IN ('.join(',',array_map('db_quote',$admininstitutions)).'))') . '
WHERE (p.activity = ? ' . ($adminonly ? '' : ' OR p.activity IS NULL') . ')';
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));
......@@ -384,6 +390,14 @@ function activity_get_users($activitytype, $userids=null, $userobjs=null, $admin
$sql .= ' AND u.id IN (' . implode(',',db_array_to_ph($userids)) . ')';
$values = array_merge($values, $userids);
}
if (!empty($admininstitutions)) {
$sql .= '
GROUP BY
u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff, p.method
HAVING (u.admin = 1 OR SUM(ui.admin) > 0)';
} else if ($adminonly) {
$sql .= ' AND u.admin = 1';
}
return get_records_sql_array($sql, $values);
}
......
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