Skip to content

Commit

Permalink
Improve view handling when editing views
Browse files Browse the repository at this point in the history
  • Loading branch information
dasistwas committed Sep 16, 2024
1 parent 61f6bdb commit 7e9fb55
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 35 deletions.
52 changes: 34 additions & 18 deletions classes/datalynx.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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])) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion classes/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]";
}
}
}
12 changes: 5 additions & 7 deletions view/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '');
Expand Down Expand Up @@ -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';
Expand Down
14 changes: 11 additions & 3 deletions view/view_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
18 changes: 12 additions & 6 deletions view/view_patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 7e9fb55

Please sign in to comment.