Skip to content

Commit

Permalink
Finish shortcode integration vor displaying a dl view via sc
Browse files Browse the repository at this point in the history
  • Loading branch information
dasistwas committed Jan 25, 2024
1 parent dc5e218 commit 42b9cfb
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 20 deletions.
41 changes: 30 additions & 11 deletions classes/datalynx.php
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ public function display() {
* @param int $datalynxid The id of the datalynx whose content should be displayed.
* @param int $viewid The id of the datalynx's view whose content should be displayed.
* @param int $eids The id of the datalynx entrie that should be displayed.
* @param array $options Pass options what should be displayed, esp. to hide control interfaces.
* @param array $options bool skiplogincheck, bool tohtml, string pagelayout.
* @return string
*/
public static function get_content_inline(int $datalynxid, int $viewid = 0, ?int $eids = null, array $options = array('tohtml' => true)) {
Expand All @@ -796,6 +796,7 @@ public static function get_content_inline(int $datalynxid, int $viewid = 0, ?int
$datalynx = new datalynx($datalynxid, null);
$urlparams->d = $datalynxid;
$urlparams->view = $viewid;
$skiplogincheck = $options['skiplogincheck'] ?? false;

// It's used nowhere but keeping for backwards compatibility.
$urlparams->pagelayout = 'external';
Expand All @@ -814,7 +815,7 @@ public static function get_content_inline(int $datalynxid, int $viewid = 0, ?int

$pageparams = array('js' => true, 'css' => true, 'rss' => true, 'modjs' => true,
'completion' => true, 'comments' => true, 'urlparams' => $urlparams);
$datalynx->set_page('external', $pageparams);
$datalynx->set_page('external', $pageparams, $skiplogincheck);
$type = $datalynx->views[$viewid]->type;
require_once($CFG->dirroot . "/mod/datalynx/view/$type/view_class.php");
$viewclass = "datalynxview_$type";
Expand Down Expand Up @@ -1361,12 +1362,12 @@ 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 = '') {
public function get_view_records($forceget = false, $sort = ''): array {
global $DB;
if (empty($this->views) || $forceget) {
$views = array();
if (!$views = $DB->get_records('datalynx_views', array('dataid' => $this->id()), $sort)) {
return false;
return [];
}
$this->views = array();
foreach ($views as $viewid => $view) {
Expand All @@ -1378,6 +1379,22 @@ public function get_view_records($forceget = false, $sort = '') {
return $this->views;
}

/**
* Get view by name
*
* @param string $viewname
* @return stdClass
*/
public function get_view_by_name(string $viewname): ?stdClass {
$views = $this->get_view_records();
foreach ($views as $view) {
if($view->name === $viewname) {
return $view;
}
}
return null;
}

/**
* Get all views of a datalynx instance
*
Expand Down Expand Up @@ -1442,7 +1459,8 @@ public function get_current_view_from_id(int $viewid = 0) {
* @return bool|mixed
*/
public function get_view_from_id($viewid = 0) {
if ($views = $this->get_view_records()) {
$views = $this->get_view_records();
if (!empty($views)) {
if ($viewid && isset($views[$viewid])) {
$view = $views[$viewid];
// If can't find the requested, try the default.
Expand Down Expand Up @@ -1492,7 +1510,8 @@ public function get_view($viewortype, $active = false) {
* @return array|bool
*/
public function get_views_by_type($type, $forceget = false) {
if (!$views = $this->get_view_records($forceget)) {
$views = $this->get_view_records($forceget);
if (!empty($views)) {
return false;
}

Expand All @@ -1513,8 +1532,8 @@ 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($exclude = null, $forceget = false, $sort = '') {
if (!$this->get_view_records($forceget, $sort)) {
public function get_views(array $exclude = [], bool $forceget = false, string $sort = '') {
if (!empty($this->get_view_records($forceget, $sort))) {
return false;
}

Expand All @@ -1532,17 +1551,17 @@ public function get_views($exclude = null, $forceget = false, $sort = '') {
}

/**
* Get all views visible to the user of a datalynx instance as an array indexed by viewid
* Get all viewnames visible to the user of a datalynx instance as an array indexed by viewid
*
* @param string $exclude
* @param boolean $forceget
* @param string $sort
* @return string[viewid]
* @return array $viewids[viewid]
*/
public function get_views_menu($exclude = null, $forceget = false, $sort = '') {
$views = array();

if ($this->get_view_records($forceget, $sort)) {
if (!empty($this->get_view_records($forceget, $sort))) {
foreach ($this->views as $viewid => $view) {
if (!empty($exclude) && in_array($viewid, $exclude)) {
continue;
Expand Down
43 changes: 38 additions & 5 deletions classes/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

namespace mod_datalynx;

use moodle_url;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/mod/booking/lib.php');
Expand All @@ -36,9 +38,8 @@
class shortcodes {

/**
* This shortcode shows a list of booking options, which have a booking customfield...
* ... with the shortname "recommendedin" and the value set to the shortname of the course...
* ... in which they should appear.
* This shortcode shows a view of a datalynx instance.
* Arguments: view="My view name" datalynx=5 (cmid)
*
* @param string $shortcode
* @param array $args
Expand All @@ -48,7 +49,39 @@ class shortcodes {
* @return string
*/
public static function displayview($shortcode, $args, $content, $env, $next) {
TODO:
return "x";
global $DB, $CFG, $PAGE;
require_once("{$CFG->dirroot}/mod/datalynx/locallib.php");
if (isset($args['view']) && isset($args['cmid'])) {
$viewname = $args['view'];
$cmid = $args['cmid'];
$cm = get_coursemodule_from_id('datalynx', $cmid);
if (!$cm) {
return get_string('invalidcoursemodule', 'error');
}
// Sanity check in case the designated datalynx has been deleted or does not exist.
if (!$DB->record_exists('datalynx', array('id' => $cm->instance))) {
return get_string('datalynxinstance_deleted', 'mod_datalynxcoursepage');
}

// Sanity check in case the designated view has been deleted.
if (!$DB->record_exists('datalynx_views',
array('dataid' => $cm->instance, 'name' => $viewname))) {
return get_string('datalynxview_deleted', 'mod_datalynxcoursepage');
}
$dl = new datalynx($cm->instance, $cmid);
$view = $dl->get_view_by_name($viewname);
if (!has_capability('mod/datalynx:viewentry', $dl->context)) {
// No right to view datalynx instance. Return empty string.
return '';
}

$jsurl = new moodle_url('/mod/datalynxcoursepage/js.php', array('id' => $cmid));
$PAGE->requires->js($jsurl);
$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]";
}
}
}
2 changes: 1 addition & 1 deletion db/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
defined('MOODLE_INTERNAL') || die();

$shortcodes = [
'recommendedin' => [
'displayview' => [
'callback' => 'mod_datalynx\shortcodes::displayview',
'description' => 'Displays a datalynx view',
],
Expand Down
3 changes: 2 additions & 1 deletion lang/de/datalynx.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
$string['convert'] = 'Konvertieren';
$string['converttoeditor'] = 'Zu Editorfeld konvertieren';
$string['correct'] = 'Richtig';
$string['coursevisible'] = 'Der Kurs wurde freigeschaltet und ist für Studierende verfügbar';
$string['csscode'] = 'CSS Code';
$string['cssinclude'] = 'CSS';
$string['cssincludes'] = 'Externes CSS einbinden';
Expand Down Expand Up @@ -106,8 +107,8 @@
$string['datalynx:viewentry'] = 'Einträge anzeigen';
$string['datalynx:viewindex'] = 'Index anzeigen';
$string['datalynx:writeentry'] = 'Einträge schreiben';
$string['displayview'] = 'Eine Datalynx Ansicht via Shortcodes anzeigen';
// End Capability strings.
$string['coursevisible'] = 'Der Kurs wurde freigeschaltet und ist für Studierende verfügbar';
$string['defaultview'] = 'D';
$string['deletenotenrolled'] = 'Einträge von nicht eingeschriebenen UserInnen löschen.';
$string['descending'] = 'Absteigend';
Expand Down
1 change: 1 addition & 0 deletions lang/en/datalynx.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
$string['dftimeinterval_help'] = 'Select a time interval until next entry is unlocked for the user';
$string['dfupdatefailed'] = 'Failed to update datalynx!';
$string['disapproved'] = 'Not approved';
$string['displayview'] = 'Display a datalynx view via shortcodes';
$string['documenttype'] = 'Document type';
$string['dots'] = '...';
$string['download'] = 'Download';
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'mod_datalynx';
$plugin->version = 2023121903;
$plugin->release = 'v3.2-DataIntelligence'; // Data words like data science, data mining.
$plugin->version = 2024012501;
$plugin->release = 'v3.3-DataIntelligence'; // Data words like data science, data mining.
$plugin->requires = 2022112800;
$plugin->maturity = MATURITY_STABLE;
$plugin->dependencies = array(
Expand Down

0 comments on commit 42b9cfb

Please sign in to comment.