public static function render_instance(BlockInstance $instance, $editing = false) { global $USER; $configdata = $instance->get('configdata'); $limit = isset($configdata['count']) ? (int) $configdata['count'] : 10; $userid = $USER->get('id'); $smarty = smarty_core(); $sql = ' SELECT v.id, v.title, v.owner, v.group, v.institution, v.ownerformat, v.urlid, v.ctime, v.mtime FROM {view} v JOIN {usr_watchlist_view} wv ON wv.view = v.id WHERE wv.usr = ? ORDER BY v.title LIMIT ?'; $results = get_records_sql_assoc($sql, array($userid, $limit)); // if there are no watched views, notify the user if (!$results) { $smarty->assign('watchlistempty', true); return $smarty->fetch('blocktype:watchlist:watchlist.tpl'); } View::get_extra_view_info($results, false, false); foreach ($results as &$r) { $r = (object) $r; } $smarty->assign('loggedin', $USER->is_logged_in()); $smarty->assign('views', array_values($results)); return $smarty->fetch('blocktype:watchlist:watchlist.tpl'); }
/** * Get views which have been explicitly shared to a group and are * not owned by the group excluding the view in collections * * @param int $limit * @param int $offset * @param int $groupid * @param boolean $membersonly Only return pages owned by members of the group * @param string $orderby Columns to sort by (defaults to (title, id) if empty) * @throws AccessDeniedException */ public static function get_sharedviews_data($limit = 10, $offset = 0, $groupid, $membersonly = false, $orderby = null) { global $USER; $userid = $USER->get('id'); require_once get_config('libroot') . 'group.php'; if (!group_user_access($groupid)) { throw new AccessDeniedException(get_string('accessdenied', 'error')); } $from = ' FROM {view} v INNER JOIN {view_access} a ON (a.view = v.id) INNER JOIN {group_member} m ON (a.group = m.group AND (a.role = m.role OR a.role IS NULL)) '; $where = 'WHERE a.group = ? AND m.member = ? AND (v.group IS NULL OR v.group != ?) AND (a.startdate <= CURRENT_TIMESTAMP OR a.startdate IS NULL) AND (a.stopdate > CURRENT_TIMESTAMP OR a.stopdate IS NULL) AND (v.startdate <= CURRENT_TIMESTAMP OR v.startdate IS NULL) AND (v.stopdate > CURRENT_TIMESTAMP OR v.stopdate IS NULL) AND NOT EXISTS (SELECT 1 FROM {collection_view} cv WHERE cv.view = v.id)'; $ph = array($groupid, $userid, $groupid); if ($membersonly) { $from .= ' INNER JOIN {group_member} m2 ON m2.member = v.owner '; $where .= ' AND m2.group = ? '; $ph[] = $groupid; } $count = count_records_sql('SELECT COUNT(DISTINCT(v.id)) ' . $from . $where, $ph); if ($orderby === null) { $ordersql = ' ORDER BY v.title, v.id'; } else { $ordersql = ' ORDER BY ' . $orderby . ', v.id'; } $viewdata = get_records_sql_assoc(' SELECT DISTINCT v.id, v.title, v.startdate, v.stopdate, v.description, v.group, v.owner, v.ownerformat, v.institution, v.urlid, v.ctime, v.mtime ' . $from . $where . $ordersql, $ph, $offset, $limit); if ($viewdata) { View::get_extra_view_info($viewdata, false); } else { $viewdata = array(); } return (object) array('data' => array_values($viewdata), 'count' => $count, 'limit' => $limit, 'offset' => $offset); }
/** * get the views that a user can see belonging * to the given users * * @param array $users users to fetch views owned by * @param int $userlooking (optional, defaults to logged in user) * @param int $limit grab this many views. (setting this null means get all) * * @return array Associative array keyed by userid, of arrays of view ids */ function get_views($users, $userlooking = null, $limit = 5, $type = null) { $userlooking = optional_userid($userlooking); if (is_int($users)) { $users = array($users); } $list = array(); if (count($users) == 0) { return $list; } $users = array_flip($users); $dbnow = db_format_timestamp(time()); if ($friends = get_records_sql_array('SELECT CASE WHEN usr1=? THEN usr2 ELSE usr1 END AS id FROM {usr_friend} f WHERE ( usr1=? AND usr2 IN (' . join(',', array_map('db_quote', array_keys($users))) . ') ) OR ( usr2=? AND usr1 IN (' . join(',', array_map('db_quote', array_keys($users))) . ') ) ', array($userlooking, $userlooking, $userlooking))) { foreach ($friends as $user_id) { $users[$user_id->id] = 'friend'; } } if (is_null($type)) { $typesql = "AND v.type != 'profile'"; } else { $typesql = 'AND v.type = ' . db_quote($type); } $data = array(); $done = false; // public, logged in, or friends' views if ($results = get_records_sql_assoc('SELECT v.*, ' . db_format_tsfield('atime') . ', ' . db_format_tsfield('mtime') . ', ' . db_format_tsfield('v.ctime', 'ctime') . ' FROM {view} v INNER JOIN {view_access} a ON v.id=a.view AND ( accesstype IN (\'public\',\'loggedin\') ' . (count(preg_grep('/^friend$/', $users)) > 0 ? 'OR ( accesstype = \'friends\' AND v.owner IN (' . join(',', array_map('db_quote', array_keys(preg_grep('/^friend$/', $users)))) . ') )' : '') . ' ) WHERE v.owner IN (' . join(',', array_map('db_quote', array_keys($users))) . ') AND ( v.startdate IS NULL OR v.startdate < ? ) AND ( v.stopdate IS NULL OR v.stopdate > ? ) ' . $typesql, array($dbnow, $dbnow))) { foreach ($results as $row) { $list[$row->owner][$row->id] = $row->id; } $data = $results; // bail if we've filled all users to the limit $done = _get_views_trim_list($list, $users, $limit, $data); } // check individual user access if (!$done && ($results = get_records_sql_assoc('SELECT v.*, ' . db_format_tsfield('atime') . ', ' . db_format_tsfield('mtime') . ', ' . db_format_tsfield('v.ctime', 'ctime') . ' FROM {view} v INNER JOIN {view_access} a ON v.id=a.view AND a.usr=? WHERE v.owner IN (' . join(',', array_map('db_quote', array_keys($users))) . ') AND ( v.startdate IS NULL OR v.startdate < ? ) AND ( v.stopdate IS NULL OR v.stopdate > ? ) ' . $typesql, array($userlooking, $dbnow, $dbnow)))) { foreach ($results as &$row) { $list[$row->owner][$row->id] = $row->id; } $data = array_merge($data, $results); // bail if we've filled all users to the limit $done = $done && _get_views_trim_list($list, $users, $limit, $data); } // check group access if (!$done && ($results = get_records_sql_assoc('SELECT v.*, ' . db_format_tsfield('v.atime', 'atime') . ', ' . db_format_tsfield('v.mtime', 'mtime') . ', ' . db_format_tsfield('v.ctime', 'ctime') . ' FROM {view} v INNER JOIN {view_access} a ON v.id=a.view INNER JOIN {group_member} m ON m.group=a.group AND m.member=? INNER JOIN {group} g ON (g.id = a.group AND g.deleted = ?) WHERE v.owner IN (' . join(',', array_map('db_quote', array_keys($users))) . ') AND ( v.startdate IS NULL OR v.startdate < ? ) AND ( v.stopdate IS NULL OR v.stopdate > ? ) ' . $typesql, array($userlooking, 0, $dbnow, $dbnow)))) { foreach ($results as &$row) { $list[$row->owner][$row->id] = $row->id; } $data = array_merge($data, $results); // bail if we've filled all users to the limit $done = $done && _get_views_trim_list($list, $users, $limit, $data); } require_once 'view.php'; View::get_extra_view_info($data, false, false); $list = array(); foreach ($data as $d) { $list[$d['owner']][$d['id']] = (object) $d; } return $list; }
/** * Get views submitted to a group */ public static function get_submitted_views($groupid, $userid = null) { $values = array($groupid); $where = 'submittedgroup = ?'; if (!empty($userid)) { // Filter by view owner $values[] = (int) $userid; $where .= ' AND owner = ?'; } $viewdata = get_records_sql_assoc(' SELECT id, title, description, "owner", ownerformat, "group", institution, ' . db_format_tsfield('submittedtime') . ' FROM {view} WHERE ' . $where . ' ORDER BY title, id', $values); if ($viewdata) { View::get_extra_view_info($viewdata, false); return array_values($viewdata); } return false; }
/** * Returns array of views in the current collection * * @return array views */ public function views() { if (!isset($this->views)) { $sql = "SELECT v.id, cv.*, v.title, v.owner, v.group, v.institution, v.ownerformat, v.urlid\n FROM {collection_view} cv JOIN {view} v ON cv.view = v.id\n WHERE cv.collection = ?\n ORDER BY cv.displayorder, v.title, v.ctime ASC"; $result = get_records_sql_assoc($sql, array($this->get('id'))); if (!empty($result)) { require_once 'view.php'; View::get_extra_view_info($result, false, false); $result = array_values($result); $max = $min = $result[0]['displayorder']; foreach ($result as &$r) { $max = max($max, $r['displayorder']); $min = min($min, $r['displayorder']); $r = (object) $r; } $this->views = array('views' => array_values($result), 'count' => count($result), 'max' => $max, 'min' => $min); } else { $this->views = array(); } } return $this->views; }
/** * Get views submitted to a group */ public static function get_submitted_views($groupid) { $viewdata = get_records_sql_assoc(' SELECT id, title, description, owner, ownerformat FROM {view} WHERE submittedgroup = ? ORDER BY title, id', array($groupid)); if ($viewdata) { View::get_extra_view_info($viewdata); return array_values($viewdata); } return false; }
function institution_view_stats_table($limit, $offset, &$institutiondata) { global $USER; if ($institutiondata['views'] != 0) { $count = count_records_select('view', 'id IN (' . join(',', array_fill(0, $institutiondata['views'], '?')) . ') AND type != ?', array_merge($institutiondata['viewids'], array('dashboard'))); } else { $count = 0; } $pagination = build_pagination(array('id' => 'stats_pagination', 'url' => get_config('wwwroot') . 'admin/users/statistics.php?institution=' . $institutiondata['name'] . '&type=views', 'jsonscript' => 'admin/users/statistics.json.php', 'datatable' => 'statistics_table', 'count' => $count, 'limit' => $limit, 'offset' => $offset, 'extradata' => array('institution' => $institutiondata['name']), 'setlimit' => true)); $result = array('count' => $count, 'tablerows' => '', 'pagination' => $pagination['html'], 'pagination_js' => $pagination['javascript']); if ($count < 1) { return $result; } $viewdata = get_records_sql_assoc("SELECT\n v.id, v.title, v.owner, v.group, v.institution, v.visits, v.type, v.ownerformat, v.urlid\n FROM {view} v\n WHERE v.id IN (" . join(',', array_fill(0, $institutiondata['views'], '?')) . ") AND v.type != ?\n ORDER BY v.visits DESC, v.title, v.id", array_merge($institutiondata['viewids'], array('dashboard')), $offset, $limit); require_once 'view.php'; require_once 'group.php'; View::get_extra_view_info($viewdata, false, false); safe_require('artefact', 'comment'); $comments = ArtefactTypeComment::count_comments(array_keys($viewdata)); foreach ($viewdata as &$v) { $v = (object) $v; if ($v->owner) { $v->ownername = display_name($v->user); $v->ownerurl = profile_url($v->user); } else { $v->ownername = $v->sharedby; if ($v->group) { $v->ownerurl = group_homepage_url($v->groupdata); } else { if ($v->institution && $v->institution != 'mahara') { $v->ownerurl = get_config('wwwroot') . 'institution/index.php?institution=' . $v->institution; } } } $v->comments = isset($comments[$v->id]) ? (int) $comments[$v->id]->comments : 0; } $csvfields = array('title', 'displaytitle', 'fullurl', 'owner', 'group', 'institution', 'ownername', 'ownerurl', 'visits', 'type', 'comments'); $USER->set_download_file(generate_csv($viewdata, $csvfields), 'viewstatistics.csv', 'text/csv'); $result['csv'] = true; $smarty = smarty_core(); $smarty->assign('data', $viewdata); $smarty->assign('offset', $offset); $result['tablerows'] = $smarty->fetch('admin/viewstats.tpl'); return $result; }
va.view, v.title AS viewtitle, v.owner, v.group, v.institution, v.ownerformat, v.urlid FROM {block_instance} bi JOIN {view_artefact} va ON bi.id = va.block JOIN {view} v ON va.view = v.id WHERE va.artefact IN (' . join(',', array_fill(0, count($data), '?')) . ') ORDER BY va.view, bi.title', array_keys($data)); if ($blocks) { $viewdata = array(); foreach ($blocks as $b) { if (!isset($viewdata[$b->view])) { $viewdata[$b->view] = (object) array('id' => $b->view, 'title' => $b->viewtitle, 'owner' => $b->owner, 'group' => $b->group, 'institution' => $b->institution, 'ownerformat' => $b->ownerformat, 'urlid' => $b->urlid); } } View::get_extra_view_info($viewdata, false, false); foreach ($blocks as $b) { if (!isset($data[$b->artefact]->views)) { $data[$b->artefact]->views = array(); } if (!isset($data[$b->artefact]->views[$b->view])) { $data[$b->artefact]->views[$b->view] = array('view' => $b->view, 'viewtitle' => $b->viewtitle, 'fullurl' => $viewdata[$b->view]['fullurl']); // Add the view owner's name if it's not the same as the note owner. This will either // be a group artefact inside an individual's view, or it's an institution/site artefact. if (!empty($params['group']) && $b->owner || !empty($params['institution']) && $params['institution'] != $b->institution) { if ($b->owner) { $ownername = display_default_name($viewdata[$b->view]['user']); $ownerurl = profile_url($viewdata[$b->view]['user']); } else { if ($b->group) { $ownername = $viewdata[$b->view]['groupdata']['name'];