diff --git a/classes/datalynx.php b/classes/datalynx.php index 23309fd0..ec95b017 100644 --- a/classes/datalynx.php +++ b/classes/datalynx.php @@ -30,6 +30,7 @@ use datalynx_preset_manager; use datalynx_rule_manager; use html_writer; +use mod_datalynx\view\base; use moodle_page; use moodle_url; use stdClass; @@ -1382,6 +1383,27 @@ public function get_view_records(bool $forceget = false, string $sort = ''): arr return $this->views; } + /** + * Get view subclass objects editable by the user + * + * @param string $sort SQL ORDER BY clause + * @return array an array of datalynx_views subclass objects + */ + public function get_views_editable_by_user(string $sort = ''): array { + global $DB; + if (has_capability('mod/datalynx:managetemplates', $this->context)) { + $views = $DB->get_records('datalynx_views', array('dataid' => $this->id()), $sort); + $returnviews = []; + if (!empty($views)) { + foreach ($views as $viewid => $view) { + $returnviews[$viewid] = $this->get_view($view); + } + } + return $returnviews; + } + return []; + } + /** * Get view by name. Return the view record as stdClass or an empty stdClass if view was not found. * @@ -1460,7 +1482,8 @@ public function get_current_view_from_id(int $viewid = 0) { * @param int $viewid * @return bool|mixed */ - public function get_view_from_id($viewid = 0) { + public function get_view_from_id(int $viewid = 0) { + // Get views visible to user. $views = $this->get_view_records(); if (!empty($views)) { if ($viewid && isset($views[$viewid])) { @@ -1487,7 +1510,7 @@ public function get_view_from_id($viewid = 0) { * @param bool $active * @return mixed */ - public function get_view($viewortype, $active = false) { + public function get_view($viewortype, $active = false): base { global $CFG; if ($viewortype) { @@ -1534,9 +1557,9 @@ public function get_views_by_type($type, $forceget = false) { * @param string $sort * @return array of view objects indexed by view id or false if no views are found */ - public function get_views(array $exclude = [], bool $forceget = false, string $sort = '') { + public function get_views(array $exclude = [], bool $forceget = false, string $sort = ''): array { if (empty($this->get_view_records($forceget, $sort))) { - return false; + return []; } static $views = null; @@ -1578,10 +1601,8 @@ public function get_views_menu($exclude = null, $forceget = false, $sort = '') { * Set default view * * @param int $viewid - * @throws \dml_exception - * @throws moodle_exception */ - public function set_default_view($viewid = 0) { + public function set_default_view(int $viewid = 0): void { global $DB; $rec = new stdClass(); @@ -1597,10 +1618,8 @@ public function set_default_view($viewid = 0) { * Set default filter * * @param int $filterid - * @throws \dml_exception - * @throws moodle_exception */ - public function set_default_filter($filterid = 0) { + public function set_default_filter(int $filterid = 0): void { global $DB; $rec = new stdClass(); @@ -1616,10 +1635,8 @@ public function set_default_filter($filterid = 0) { * Set the default edit view * * @param int $viewid - * @throws \dml_exception - * @throws moodle_exception */ - public function set_single_edit_view($viewid = 0) { + public function set_single_edit_view(int $viewid = 0): void { global $DB; $rec = new stdClass(); @@ -1635,10 +1652,8 @@ public function set_single_edit_view($viewid = 0) { * Set view that is linked with the more-view patterns * * @param int $viewid - * @throws \dml_exception - * @throws moodle_exception */ - public function set_single_more_view($viewid = 0) { + public function set_single_more_view(int $viewid = 0): void { global $DB; $rec = new stdClass(); @@ -1662,8 +1677,9 @@ public function get_default_view_id() { * @param string $searchfieldname * @param string $newfieldname */ - public function replace_field_in_views($searchfieldname, $newfieldname) { - if ($views = $this->get_views()) { + public function replace_field_in_views(string $searchfieldname, string $newfieldname): void { + $views = $this->get_views(); + if (!empty($views)) { foreach ($views as $view) { $view->replace_field_in_view($searchfieldname, $newfieldname); } diff --git a/classes/shortcodes.php b/classes/shortcodes.php index eee2f77c..4d595125 100644 --- a/classes/shortcodes.php +++ b/classes/shortcodes.php @@ -78,7 +78,7 @@ public static function displayview($shortcode, $args, $content, $env, $next) { $options = ['tohtml' => true, 'skiplogincheck' => true]; return datalynx::get_content_inline($cm->instance, $view->id, null, $options); } else { - return "You must set arguments view and datalynx. Here is an example: [displayview view=\"My datalynx viewname\" cmid=5]"; + return "You must set arguments for view and datalynx. Here is an example: [displayview view=\"My datalynx viewname\" cmid=5]"; } } } diff --git a/view/index.php b/view/index.php index d9170e70..3ea45b12 100644 --- a/view/index.php +++ b/view/index.php @@ -105,12 +105,10 @@ // Any notifications? $dl->notifications['bad']['defaultview'] = ''; -$views = $dl->get_views([], true, - flexible_table::get_sort_for_table('datalynxviewsindex' . $dl->id())); -if (!$views) { - $dl->notifications['bad']['getstartedviews'] = get_string('viewnoneindatalynx', 'datalynx'); // Nothing. - // In. - // Database. +$views = $dl->get_views_editable_by_user(flexible_table::get_sort_for_table('datalynxviewsindex' . $dl->id())); +if (empty($views)) { + // Now views defined yet. + $dl->notifications['bad']['getstartedviews'] = get_string('viewnoneindatalynx', 'datalynx'); } else { if (empty($dl->data->defaultview)) { $dl->notifications['bad']['defaultview'] = get_string('viewnodefault', 'datalynx', ''); @@ -142,7 +140,7 @@ array('class' => 'viewadd mdl-align')); // If there are views print admin style list of them. -if ($views) { +if (!empty($views)) { $viewbaseurl = '/mod/datalynx/view.php'; $editbaseurl = '/mod/datalynx/view/view_edit.php'; diff --git a/view/view_edit.php b/view/view_edit.php index e65e6496..a4582f1d 100644 --- a/view/view_edit.php +++ b/view/view_edit.php @@ -73,13 +73,21 @@ require_capability('mod/datalynx:managetemplates', $dl->context); if ($urlparams->vedit) { - $view = $dl->get_view_from_id($urlparams->vedit); - if ($default = optional_param('resetdefault', 0, PARAM_INT)) { - $view->generate_default_view(); + $views = $dl->get_views_editable_by_user(''); + if (!empty($views) && array_key_exists($urlparams->vedit, $views)) { + $view = $views[$urlparams->vedit]; + if ($default = optional_param('resetdefault', 0, PARAM_INT)) { + $view->generate_default_view(); + } + } else { + throw new moodle_exception('The requested view does not exist or you do not have permission to edit it.'); } } else { if ($urlparams->type) { $view = $dl->get_view($urlparams->type); + if (!$view) { + throw new moodle_exception('The requested view type does not exist.'); + } $view->generate_default_view(); } } diff --git a/view/view_patterns.php b/view/view_patterns.php index 48989b49..170f3e0e 100644 --- a/view/view_patterns.php +++ b/view/view_patterns.php @@ -21,6 +21,9 @@ * @copyright based on the work by 2012 Itamar Tzadok * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ + +use mod_datalynx\view\base; + defined('MOODLE_INTERNAL') || die(); /** @@ -34,12 +37,13 @@ class datalynxview_patterns { /** * - * @var base + * @var ?base */ - protected $_view = null; + protected ?base $_view = null; /** * Constructor + * @param $view */ public function __construct(&$view) { $this->_view = $view; @@ -598,7 +602,8 @@ protected function get_viewurl_replacement($viewname = null) { static $views = null; if ($views === null) { $views = []; - if ($theviews = $df->get_views()) { + $theviews = $df->get_views(); + if (!empty($theviews)) { foreach ($theviews as $theview) { $views[$theview->name()] = $theview; } @@ -614,15 +619,16 @@ protected function get_viewurl_replacement($viewname = null) { /** * return HTML of the content of a given view $viewname * - * @param string $viewname + * @param ?string $viewname * @return string */ - protected function get_viewcontent_replacement($viewname = null) { + protected function get_viewcontent_replacement(?string $viewname = null): string { $df = $this->_view->get_dl(); static $views = null; if ($views === null) { $views = []; - if ($theviews = $df->get_views()) { + $theviews = $df->get_views(); + if (!empty($theviews)) { foreach ($theviews as $theview) { $views[$theview->name()] = $theview; }