Skip to content

Commit

Permalink
Refactor how to get views
Browse files Browse the repository at this point in the history
  • Loading branch information
dasistwas committed Sep 12, 2024
1 parent 9cc1b49 commit 61f6bdb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
63 changes: 32 additions & 31 deletions classes/datalynx.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ class datalynx {

protected $pagefile = 'view';

protected $fields = array();
protected $fields = [];

protected $views = array();
protected $views = [];

protected $_filtermanager = null;

Expand All @@ -112,9 +112,9 @@ class datalynx {

protected $_currentview = null;

protected $internalfields = array();
protected $internalfields = [];

protected $customfilterfields = array();
protected $customfilterfields = [];

protected $internalgroupmodes = array('separateparticipants' => -1);

Expand Down Expand Up @@ -387,7 +387,7 @@ public function set_page($page = 'view', $params = null, $skiplogincheck = false
$thisid = $this->id();

$params = (object) $params;
$urlparams = array();
$urlparams = [];
if (!empty($params->urlparams)) {
foreach ($params->urlparams as $param => $value) {
if ($value != 0 && $value != '') {
Expand Down Expand Up @@ -538,7 +538,7 @@ public function set_page($page = 'view', $params = null, $skiplogincheck = false
$output = '';

// CSS (cannot be required after head).
$cssurls = array();
$cssurls = [];
if (!empty($params->css)) {
// Js includes from the js template.
if ($this->data->cssincludes) {
Expand Down Expand Up @@ -578,7 +578,7 @@ public function set_page($page = 'view', $params = null, $skiplogincheck = false
}

// JS.
$jsurls = array();
$jsurls = [];
if (!empty($params->js)) {
// Js includes from the js template.
if ($this->data->jsincludes) {
Expand Down Expand Up @@ -872,7 +872,7 @@ public function get_internal_fields_names() {
global $CFG;

$fieldplugins = get_list_of_plugins('mod/datalynx/field/');
$internalfieldsnames = array();
$internalfieldsnames = [];
foreach ($fieldplugins as $fieldname) {
require_once("$CFG->dirroot/mod/datalynx/field/$fieldname/field_class.php");
$fieldclass = "datalynxfield_$fieldname";
Expand All @@ -897,7 +897,7 @@ public function get_customfilterfieldtypes() {
global $CFG;

if (!$this->customfilterfields) {
$this->customfilterfields = array();
$this->customfilterfields = [];
// Collate customfilter fields.
$fieldplugins = get_list_of_plugins('mod/datalynx/field/');
foreach ($fieldplugins as $fieldname) {
Expand Down Expand Up @@ -1035,7 +1035,7 @@ public function get_fields($exclude = null, $menu = false, $forceget = false, $s
global $DB;

if (!$this->fields || $forceget) {
$this->fields = array();
$this->fields = [];
// Collate user fields.
if ($fields = $DB->get_records('datalynx_fields', array('dataid' => $this->id()), $sort)) {
foreach ($fields as $fieldid => $field) {
Expand All @@ -1050,7 +1050,7 @@ public function get_fields($exclude = null, $menu = false, $forceget = false, $s
if (empty($exclude) && !$menu) {
return $fields;
} else {
$retfields = array();
$retfields = [];
foreach ($fields as $fieldid => $field) {
if (!empty($exclude) && in_array($fieldid, $exclude)) {
continue;
Expand All @@ -1073,7 +1073,7 @@ public function get_fields($exclude = null, $menu = false, $forceget = false, $s
public function get_fieldnames($sort = '') {
global $DB;

$fieldnames = array();
$fieldnames = [];
// Collate user fields.
if ($fields = $DB->get_records('datalynx_fields', array('dataid' => $this->id()), $sort)) {
foreach ($fields as $fieldid => $field) {
Expand Down Expand Up @@ -1140,7 +1140,7 @@ public function process_fields($action, $fids, $confirmed = false) {
}

$dffields = $this->get_fields();
$fields = array();
$fields = [];
// Collate the fields for processing.
if ($fieldids = explode(',', $fids)) {
foreach ($fieldids as $fieldid) {
Expand All @@ -1150,7 +1150,7 @@ public function process_fields($action, $fids, $confirmed = false) {
}
}

$processedfids = array();
$processedfids = [];
$strnotify = '';

if (empty($fields) && $action != 'add') {
Expand Down Expand Up @@ -1364,14 +1364,15 @@ function($list, $filter) {
* @param string $sort SQL ORDER BY clause
* @return array an array of datalynx_views entry objects
*/
public function get_view_records($forceget = false, $sort = ''): array {
public function get_view_records(bool $forceget = false, string $sort = ''): array {
global $DB;
if (empty($this->views) || $forceget) {
$views = array();
if (!$views = $DB->get_records('datalynx_views', array('dataid' => $this->id()), $sort)) {
return [];
$views = $DB->get_records('datalynx_views', array('dataid' => $this->id()), $sort);
// Reinitialise the views array.
$this->views = [];
if (empty($views)) {
return $views;
}
$this->views = array();
foreach ($views as $viewid => $view) {
if ($this->is_visible_to_user($view)) {
$this->views[$viewid] = $view;
Expand All @@ -1382,19 +1383,19 @@ public function get_view_records($forceget = false, $sort = ''): array {
}

/**
* Get view by name
* Get view by name. Return the view record as stdClass or an empty stdClass if view was not found.
*
* @param string $viewname
* @return stdClass
*/
public function get_view_by_name(string $viewname): ?stdClass {
public function get_view_by_name(string $viewname): stdClass {
$views = $this->get_view_records(true);
foreach ($views as $view) {
if($view->name === $viewname) {
return $view;
}
}
return null;
return new stdClass();
}

/**
Expand All @@ -1404,9 +1405,9 @@ public function get_view_by_name(string $viewname): ?stdClass {
*/
public function get_all_views() {
global $DB;
$views = array();
$views = [];
if (!$views = $DB->get_records('datalynx_views', array('dataid' => $this->id()))) {
return array();
return [];
} else {
return $views;
}
Expand Down Expand Up @@ -1516,7 +1517,7 @@ public function get_views_by_type($type, $forceget = false) {
return false;
}

$typeviews = array();
$typeviews = [];
foreach ($views as $viewid => $view) {
if ($view->type === $type) {
$typeviews[$viewid] = $this->get_view($view);
Expand All @@ -1540,7 +1541,7 @@ public function get_views(array $exclude = [], bool $forceget = false, string $s

static $views = null;
if ($views === null || $forceget) {
$views = array();
$views = [];
foreach ($this->views as $viewid => $view) {
if (!empty($exclude) && in_array($viewid, $exclude)) {
continue;
Expand All @@ -1560,7 +1561,7 @@ public function get_views(array $exclude = [], bool $forceget = false, string $s
* @return array $viewids[viewid]
*/
public function get_views_menu($exclude = null, $forceget = false, $sort = '') {
$views = array();
$views = [];

if (!empty($this->get_view_records($forceget, $sort))) {
foreach ($this->views as $viewid => $view) {
Expand Down Expand Up @@ -1691,7 +1692,7 @@ public function process_views($action, $vids, $confirmed = false) {
}

if ($vids) { // Some views are specified for action.
$views = array();
$views = [];
$viewobjs = $this->get_views([], true);
foreach (explode(',', $vids) as $vid) {
if (!empty($viewobjs[$vid])) {
Expand All @@ -1700,7 +1701,7 @@ public function process_views($action, $vids, $confirmed = false) {
}
}

$processedvids = array();
$processedvids = [];
$strnotify = '';

if (empty($views)) {
Expand Down Expand Up @@ -2174,7 +2175,7 @@ public function user_num_entries($perinterval = false) {
return $numentriesintervaled;
}

$params = array();
$params = [];
$params['dataid'] = $this->id();

$andwhereuserorgroup = '';
Expand Down Expand Up @@ -2459,7 +2460,7 @@ public function events_trigger($event, $data) {

public function get_baseurl() {
// Base url params.
$baseurlparams = array();
$baseurlparams = [];
$baseurlparams['d'] = $this->id();
return new moodle_url("/mod/datalynx/{$this->pagefile()}.php", $baseurlparams);
}
Expand Down
2 changes: 1 addition & 1 deletion classes/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function displayview($shortcode, $args, $content, $env, $next) {
$dl = new datalynx($cm->instance, $cmid);
$view = $dl->get_view_by_name($viewname);
// If view has not been found, the user has no right to view it. Return an empty string instead.
if (!has_capability('mod/datalynx:viewentry', $dl->context) || !$view) {
if (!has_capability('mod/datalynx:viewentry', $dl->context) || empty((array) $view)) {
// No right to view datalynx instance or view or entry. Return empty string.
return '';
}
Expand Down

0 comments on commit 61f6bdb

Please sign in to comment.