/** * Given a list of tags, finds blocks by the current user that contain those tags * (Used to determine which ones to check for the view_artefact table) * @param array $tags * @return array */ public static function find_matching_blocks(array $tags) { global $USER; $taggedblockids = (array) get_column_sql('SELECT bi.id as block FROM {blocktype_taggedposts_tags} btt INNER JOIN {block_instance} bi ON btt.block_instance = bi.id INNER JOIN {view} v ON bi.view = v.id WHERE v.owner = ? AND btt.tagtype = ? AND btt.tag IN (' . implode(',', db_array_to_ph($tags)) . ') ', array_merge(array($USER->id, PluginBlocktypeTaggedposts::TAGTYPE_INCLUDE), $tags)); if ($taggedblockids) { return $taggedblockids; } else { return array(); } }
/** * Makes a literal copy of a list of collections for this user. * * @param array $templateids A list of collectionids to copy. */ public function copy_collections($templateids, $checkviewaccess = true) { if (!$templateids) { // Nothing to do return; } if (!is_array($templateids)) { throw new SystemException('User->copy_collections: templateids must be a list of templates to copy for the user'); } require_once get_config('libroot') . 'collection.php'; $collections = array(); $results = get_records_select_array('collection', 'id IN (' . implode(', ', db_array_to_ph($templateids)) . ')', $templateids, '', 'id, name'); foreach ($results as $result) { $collections[$result->id] = $result; } db_begin(); foreach ($templateids as $tid) { Collection::create_from_template(array('owner' => $this->get('id'), 'title' => $collections[$tid]->name), $tid, $this->get('id'), $checkviewaccess); } db_commit(); }
/** * Returns search results for users in a particular group * * The search term is applied against first and last names of the users in the group * * @param int $group The group to build results for * @param string $query A search string to filter by * @param int $offset What result to start showing paginated results from * @param int $limit How many results to show * @param array $membershiptype User membershiptype * @param bool $random Set to true if you want the result to be ordered by random, default false * @param int $friendof Only return friends of this user * */ function get_group_user_search_results($group, $query, $offset, $limit, $membershiptype, $order = null, $friendof = null, $sortoptionidx = null) { $plugin = get_config('searchplugin'); safe_require('search', $plugin); $searchclass = generate_class_name('search', $plugin); $constraints = array(); if (call_static_method($searchclass, 'can_process_raw_group_search_user_queries')) { // Pass the raw query string through to group_search_user; parsing of the // query depends on the plugin configuration. $queries = $query; } else { $queries = array(); if (!empty($query)) { list($words, $fullnames) = parse_name_query($query); foreach ($words as $word) { $queries[] = array('field' => 'firstname', 'type' => 'contains', 'string' => $word); $queries[] = array('field' => 'lastname', 'type' => 'contains', 'string' => $word); } foreach ($fullnames as $n) { $constraints[] = array('field' => 'firstname', 'type' => 'contains', 'string' => $n[0]); $constraints[] = array('field' => 'lastname', 'type' => 'contains', 'string' => $n[1]); } } } $results = call_static_method($searchclass, 'group_search_user', $group, $queries, $constraints, $offset, $limit, $membershiptype, $order, $friendof, $sortoptionidx); if ($results['count']) { $userids = array_map(create_function('$a', 'return $a["id"];'), $results['data']); $introductions = get_records_sql_assoc("SELECT \"owner\", title\n FROM {artefact}\n WHERE artefacttype = 'introduction'\n AND \"owner\" IN (" . implode(',', db_array_to_ph($userids)) . ')', $userids); foreach ($results['data'] as &$result) { $result['name'] = display_name($result); $result['introduction'] = isset($introductions[$result['id']]) ? $introductions[$result['id']]->title : ''; if (isset($result['jointime'])) { $result['jointime'] = format_date($result['jointime'], 'strftimedate'); } } } return $results; }
/** * this function returns an array of users who subsribe to a particular activitytype * including the notification method they are using to subscribe to it. * * @param int $activitytype the id 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 * @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, $admininstitutions = array()) { $values = array($activitytype); $sql = ' SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff, p.method, ap.value AS lang, apm.value AS maildisabled, aic.value AS mnethostwwwroot, h.appname AS mnethostapp FROM {usr} u LEFT JOIN {usr_activity_preference} p ON (p.usr = u.id AND p.activity = ?)' . (empty($admininstitutions) ? '' : ' LEFT OUTER JOIN {usr_institution} ui ON (u.id = ui.usr AND ui.institution IN (' . join(',', array_map('db_quote', $admininstitutions)) . '))') . ' LEFT OUTER JOIN {usr_account_preference} ap ON (ap.usr = u.id AND ap.field = \'lang\') LEFT OUTER JOIN {usr_account_preference} apm ON (apm.usr = u.id AND apm.field = \'maildisabled\') LEFT OUTER JOIN {auth_instance} ai ON (ai.id = u.authinstance AND ai.authname = \'xmlrpc\') LEFT OUTER JOIN {auth_instance_config} aic ON (aic.instance = ai.id AND aic.field = \'wwwroot\') LEFT OUTER JOIN {host} h ON aic.value = h.wwwroot WHERE u.deleted = 0'; 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); } } if (!empty($admininstitutions)) { $sql .= ' GROUP BY u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff, p.method, ap.value, apm.value, aic.value, h.appname HAVING (u.admin = 1 OR SUM(ui.admin) > 0)'; } else { if ($adminonly) { $sql .= ' AND u.admin = 1'; } } return get_records_sql_array($sql, $values); }
function group_get_admins($groupids) { $groupids = array_map('intval', $groupids); if (empty($groupids)) { return array(); } $groupadmins = get_records_sql_array(' SELECT m.group, m.member, u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.profileicon, u.urlid FROM {group_member} m JOIN {usr} u ON u.id = m.member WHERE m.group IN (' . implode(',', db_array_to_ph($groupids)) . ")\n AND m.role = 'admin'", $groupids); if (!$groupadmins) { $groupadmins = array(); } return $groupadmins; }
public static function send_digest() { $users = array(); $sitename = get_config('sitename'); $types = get_records_assoc('activity_type', 'admin', 0, 'plugintype,pluginname,name', 'id,name,plugintype,pluginname'); foreach ($types as &$type) { if (!empty($type->plugintype)) { $type->section = "{$type->plugintype}.{$type->pluginname}"; } else { $type->section = "activity"; } } $sql = 'SELECT q.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff, p.value AS lang, q.*,' . db_format_tsfield('ctime') . ' FROM {usr} u JOIN {notification_emaildigest_queue} q ON q.usr = u.id LEFT OUTER JOIN {usr_account_preference} p ON (p.usr = u.id AND p.field = \'lang\') ORDER BY usr,type,q.ctime'; if ($tosend = get_records_sql_array($sql, array())) { foreach ($tosend as $queue) { if (!isset($users[$queue->usr])) { $users[$queue->usr] = new StdClass(); $users[$queue->usr]->user = new StdClass(); $users[$queue->usr]->user->username = $queue->username; $users[$queue->usr]->user->firstname = $queue->firstname; $users[$queue->usr]->user->lastname = $queue->lastname; $users[$queue->usr]->user->preferredname = $queue->preferredname; $users[$queue->usr]->user->email = $queue->email; $users[$queue->usr]->user->admin = $queue->admin; $users[$queue->usr]->user->staff = $queue->staff; $users[$queue->usr]->user->id = $queue->usr; $users[$queue->usr]->user->lang = empty($queue->lang) || $queue->lang == 'default' ? get_config('lang') : $queue->lang; $users[$queue->usr]->entries = array(); } $queue->nicetype = get_string_from_language($users[$queue->usr]->user->lang, 'type' . $types[$queue->type]->name, $types[$queue->type]->section); $users[$queue->usr]->entries[$queue->id] = $queue; } } foreach ($users as $user) { $lang = $user->user->lang; $subject = get_string_from_language($lang, 'emailsubject', 'notification.emaildigest', $sitename); $body = get_string_from_language($lang, 'emailbodynoreply', 'notification.emaildigest', $sitename); foreach ($user->entries as $entry) { $body .= get_string_from_language($lang, 'type', 'activity') . ': ' . $entry->nicetype . ' ' . get_string_from_language($lang, 'attime', 'activity') . ' ' . format_date($entry->ctime) . "\n"; if (!empty($entry->subject)) { $body .= get_string_from_language($lang, 'subject') . $entry->subject . "\n"; } if (!empty($entry->message)) { $body .= "\n" . $entry->message; } if (!empty($entry->url)) { $body .= "\n" . $entry->url; } $body .= "\n\n"; } $prefurl = get_config('wwwroot') . 'account/activity/preferences/'; $body .= "\n\n" . get_string_from_language($lang, 'emailbodyending', 'notification.emaildigest', $prefurl); try { email_user($user->user, null, $subject, $body); //only delete them if the email succeeded! $in = db_array_to_ph($user->entries); delete_records_select('notification_emaildigest_queue', 'id IN (' . implode(', ', $in) . ')', array_keys($user->entries)); } catch (Exception $e) { // @todo } } }
/** * cron job to process the queue and wake up and finish imports */ function import_process_queue() { if (!($ready = get_records_select_array('import_queue', 'ready = ? OR expirytime < ?', array(1, db_format_timestamp(time())), '', '*,' . db_format_tsfield('expirytime', 'ex')))) { return true; } $now = time(); $processed = array(); foreach ($ready as $item) { if ($item->ex < $now) { log_debug('deleting expired import record', $item); $processed[] = $item->id; continue; } $tr = null; if (!empty($item->host)) { $tr = new MnetImporterTransport($item); } else { $tr = new LocalImporterTransport($item); } $importer = PluginImport::create_importer($item->id, $tr, $item); try { $importer->prepare(); $importer->process(); $importer->cleanup(); $processed[] = $item->id; } catch (Exception $e) { log_debug('an error occurred on import: ' . $e->getMessage()); $importer->get('importertransport')->cleanup(); } } if (empty($processed)) { return true; } delete_records_select('import_queue', 'id IN ( ' . implode(',', db_array_to_ph($processed)) . ')', $processed); }
/** * Returns search results for users in a particular group * * The search term is applied against first and last names of the users in the group * * @param int $group The group to build results for * @param string $query A search string to filter by * @param int $offset What result to start showing paginated results from * @param int $limit How many results to show * @param array $membershiptype User membershiptype * @param bool $random Set to true if you want the result to be ordered by random, default false * */ function get_group_user_search_results($group, $query, $offset, $limit, $membershiptype, $order = null) { $queries = array(); $constraints = array(); if (!empty($query)) { list($words, $fullnames) = parse_name_query($query); foreach ($words as $word) { $queries[] = array('field' => 'firstname', 'type' => 'contains', 'string' => $word); $queries[] = array('field' => 'lastname', 'type' => 'contains', 'string' => $word); } foreach ($fullnames as $n) { $constraints[] = array('field' => 'firstname', 'type' => 'contains', 'string' => $n[0]); $constraints[] = array('field' => 'lastname', 'type' => 'contains', 'string' => $n[1]); } } $results = group_user_search($group, $queries, $constraints, $offset, $limit, $membershiptype, $order); if ($results['count']) { $userids = array_map(create_function('$a', 'return $a["id"];'), $results['data']); $introductions = get_records_sql_assoc("SELECT \"owner\", title\n FROM {artefact}\n WHERE artefacttype = 'introduction'\n AND \"owner\" IN (" . implode(',', db_array_to_ph($userids)) . ')', $userids); foreach ($results['data'] as &$result) { $result['name'] = display_name($result); $result['introduction'] = isset($introductions[$result['id']]) ? $introductions[$result['id']]->title : ''; if (isset($result['jointime'])) { $result['jointime'] = strftime(get_string('strftimedate'), $result['jointime']); } } } return $results; }
/** * Makes a literal copy of a list of views for this user. * * @param array $templateids A list of viewids to copy. */ public function copy_views($templateids, $checkviewaccess = true) { if (!$templateids) { // Nothing to do return; } if (!is_array($templateids)) { throw new SystemException('User->copy_views: templateids must be a list of templates to copy for the user'); } require_once get_config('libroot') . 'view.php'; $views = array(); foreach (get_records_select_array('view', 'id IN (' . implode(', ', db_array_to_ph($templateids)) . ')', $templateids, '', 'id, title, description') as $result) { $views[$result->id] = $result; } db_begin(); foreach ($templateids as $tid) { View::create_from_template(array('owner' => $this->get('id'), 'title' => $views[$tid]->title, 'description' => $views[$tid]->description), $tid, $this->get('id'), $checkviewaccess); } db_commit(); }
/** * Sets up groups for display in mygroups.php and find.php * * @param array $groups Initial group data, including the current user's * membership type in each group. See mygroups.php for * the query to build this information. * @param string $returnto Where forms generated for display should be told to return to */ function group_prepare_usergroups_for_display($groups, $returnto = 'mygroups') { if (!$groups) { return; } // Retrieve a list of all the group admins, for placing in each $group object $groupadmins = array(); $groupids = array_map(create_function('$a', 'return $a->id;'), $groups); if ($groupids) { $groupadmins = get_records_sql_array('SELECT "group", member FROM {group_member} WHERE "group" IN (' . implode(',', db_array_to_ph($groupids)) . ")\n AND role = 'admin'", $groupids); if (!$groupadmins) { $groupadmins = array(); } } $i = 0; foreach ($groups as $group) { $group->admins = array(); foreach ($groupadmins as $admin) { if ($admin->group == $group->id) { $group->admins[] = $admin->member; } } $group->description = str_shorten_html($group->description, 100, true); if ($group->membershiptype == 'member') { $group->canleave = group_user_can_leave($group->id); } else { if ($group->jointype == 'open') { $group->groupjoin = group_get_join_form('joingroup' . $i++, $group->id); } else { if ($group->membershiptype == 'invite') { $group->invite = group_get_accept_form('invite' . $i++, $group->id, $returnto); } else { if ($group->membershiptype == 'admin' && $group->requests > 1) { $group->requests = array($group->requests); } } } } } }
/** * this function returns an array of users who subsribe to a particular activitytype * including the notification method they are using to subscribe to it. * * @param int $activitytype the id 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 * @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, $admininstitutions = array()) { $values = array($activitytype); $sql = ' SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff, p.method, ap.value AS lang FROM {usr} u LEFT JOIN {usr_activity_preference} p ON (p.usr = u.id AND p.activity = ?)' . (empty($admininstitutions) ? '' : ' LEFT OUTER JOIN {usr_institution} ui ON (u.id = ui.usr AND ui.institution IN (' . join(',', array_map('db_quote', $admininstitutions)) . '))') . ' LEFT OUTER JOIN {usr_account_preference} ap ON (ap.usr = u.id AND ap.field = \'lang\') WHERE TRUE'; 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); } } if (!empty($admininstitutions)) { $sql .= ' GROUP BY u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff, p.method, ap.value HAVING (u.admin = 1 OR SUM(ui.admin) > 0)'; } else { if ($adminonly) { $sql .= ' AND u.admin = 1'; } } return get_records_sql_array($sql, $values); }