Moodle event observer not triggered in local plugin - moodle

I have created a plugin at this location moodle/local/redirectafterlogin with the following structure :
redirectafterlogin/
├── db
│   ├── classes
│   │   └── observer.php
│   └── events
│   └── events.php
└── version.php
version.php:
defined('MOODLE_INTERNAL') || die();
$plugin->version = 20170333;
$plugin->requires = 2015111000;
$plugin->component = 'local_redirectafterlogin';
events.php:
defined('MOODLE_INTERNAL') || die();
$observers = array(
array(
'eventname' => 'core\event\user_loggedin',
'callback' => 'local_redirectafterlogin_observer::user_loggedin',
),
array(
'eventname' => 'core\event\user_loggedout',
'callback' => 'local_redirectafterlogin_observer::user_loggedin',
),
);
observer.php:
class local_redirectafterlogin_observer
{
public static function user_loggedin(core\event\base $event)
{
$event_data = $event->get_data();
var_dump($event_data);
die();
}
}
Cached has been cleared a lot of time and version number has been bumped too but the callback is never called!
What's wrong, why does the callback is not triggered?
How can I debug events (is there a way in Moodle to see dispatched events)?

It was a structure mistake! I put the classes folder containing observer.php in the db folder!
After moving the classes folder at the root of the plugin as follow, this is ok, observer is triggered!
Structure that is ok:
redirectafterlogin/
├── classes
│   └── observer.php
├── db
│   └── events.php
└── version.php

I don't think the automatic class loading will be able to find your observer class.
Try adding to the top of the observer class file:
namespace local_redirectafterlogin;
Then change the events.php to:
'callback' => 'local_redirectafterlogin\local_redirectafterlogin_observer'
(You could also greatly shorten the class name, now it is namespaced). Make sure you bump the version number, to reload the events.php file.
You may want to reconsider the naming of your plugin, though, as redirection is not allowed from within an event handler, as that would cause a great many problems.

I made the experience that var_dump or output do stdout gets not trigggert. As quick "debug" i use file_put_contents to a temp logfile

Related

Links to json files

my directory structure is
├── xxx
│   ├── 01.md
| └── 02.md
├── auth
│   ├── j1.json
│   ├── j2.json
│   └── j3.json
└── default.template.html
And I link jsons from markdowns like Auth. It makes sense as we use there files as test scenarios and in json files we have credentials and roles. But if I try to generate html it fails on unresolved internal reference: ../auth/aspect_admin.json. I tried to exclude the link checking but without any help. The best would be to leave it as a link in md file but somehow follow the link and include the json as code block in generated html. Is it possible?
It was a bug and will be fixed in next version https://github.com/planet42/Laika/issues/148

importing style-sheet into app.scss to use throughout project

I want to use a style page across my app.
in the app.scss file I tried using:
#import "../global-scss/html-table-styles.scss";
this does not work.
Pasting the style directly into the app.scss file does work but I would rather not do that.
Is what I'm trying to accomplish possible and if so, what am I doing wrong?
If your file structure is like this:
├── src
│   ├── app
│   │   ├── app.component.ts
│   │   ├── app.module.ts
│   │   ├── app.scss
│   │   └── main.ts
// ... pages, components ...
| └── theme
│   ├── custom-styles.scss
...the import line should be:
#import "../theme/custom-styles.scss";
The problem was due to my poor understanding of style-sheets and because there is an answer already so I am unable to delete.
In the style-sheet I was trying to import I had followed the pattern used for styles local to a page and had a base style tag
eg:
html-table-styles{
//my styles
}
when i imported #import "../global-scss/html-table-styles.scss"; the styles were not being used, removing html-table-styles solved my issue

inheritance-diagrams in sphinx for matlab

I am documenting a matlab code that I have with sphinx. I am using the package sphinxcontrib-matlabdomain.
My directory tree is as follows:
me:~/.../doc$ tree ../
../
├── doc
│   ├── conf.py
│   ├── make.bat
│   ├── Makefile
│   ├── index.rst
│   ├── BaseClass.rst
│   └── DerivedClass.rst
├── LICENSE.md
├── README.md
└── src
├── BaseClass.m
└── DerivedClass.m
The problem comes when I want to show inheritance diagrams. I have added the necessary things in my config.py file:
matlab_src_dir = os.path.abspath('..')
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.inheritance_diagram',
'sphinx.ext.graphviz',
'sphinx.ext.viewcode',
'sphinxcontrib.matlab',
]
primary_domain = 'mat'
And I have the following in the index.rst file
Welcome to BGK's documentation!
===============================
I am trying to have a diagram here...
.. inheritance-diagram:: BaseClass DerivedClass
:parts:2
.. graphviz::
digraph {
"From here" -> "To" -> "Somewhere";
"From here" -> "To" -> "Somewhere else";
}
And in the output the directive inheritance-diagram is ignored, obtaining directly the next diagram that I am using to test that I can plot diagrams.
Is there any incompatibility to plot inheritance diagrams with sphinx for matlab classes? Is there any way to go around the problem? Thanks!
Sphinx does not support this. The built-in sphinx.ext.inheritance_diagram extension is for the Python domain only. It does not work for Matlab. If it did, I'm sure it would say so in the Sphinx documentation (and a glance at the source code in sphinx/ext/inheritance_diagram.py confirms that it is only for Python).
The only way inheritance diagrams for Matlab could work is if some other extension provided the functionality. The sphinxcontrib-matlabdomain extension that you use does not.

How do you deal with multiple PostgreSQL schemas and DBIx::Class?

In my database I have 5 separate schemas so that I can organize objects into logical groups. I used dbicdump to create my DBIx::Class schema, but noticed that it only loaded tables from the public schema. I figured out how to load all my schemas using dbicdump, but now I'm at a loss on where to go from here setting everything up and using it. I've looked online for any resources, and found very little documentation. The only thing I did find, I really wasn't too happy with the solution. I am wondering if anyone has encountered this before, and how they dealt with it.
You'll need a recent DBIx::Class I believe, but I use something like this:
#!/usr/bin/perl
use v5.14;
use DBIx::Class::Schema::Loader qw/ make_schema_at /;
use Path::Tiny;
my $dest_dir = '.';
my $pkg_base = 'Foo';
(my $pkg_dir = $pkg_base) =~ s/::/\//g;
# Delete any previous run
path($dest_dir.'/'.$pkg_base)->remove_tree;
make_schema_at(
$pkg_base.'::Schema',
{
debug => 0,
dump_directory => $dest_dir,
db_schema => '%',
moniker_parts => ['schema', 'name'],
#rescan => 1,
},
[ 'dbi:Pg:dbname=mydb port=5432', 'dbuser', 'dbpass' ],
);
exit;
That gives me something like:
Foo::Schema::Result::PublicTable1
Foo::Schema::Result::Schema2Table2
etc
There are details of the various options in the docs. I seem to remember I cross-referenced the docs with the source of dbicdump when I put it together.
HTH
I found this, and it was nearly what I needed. But I wanted traditional :: and separation of true classes (based on tables) with view classes that contain random bits of sql so, I have:
#!/usr/bin/perl
use DBIx::Class::Schema::Loader qw/ make_schema_at /;
use Path::Tiny;
my $dest_dir = '../sql' ;
my $pkg_base = 'Cclite2';
# Delete any previous run
path($dest_dir.'/'.$pkg_base)->remove_tree;
make_schema_at(
$pkg_base.'::Schema',
{
debug => 0,
dump_directory => $dest_dir,
db_schema => 'cclite2',
moniker_parts => ['schema', 'name'],
moniker_part_separator => '::',
naming => {
relationships => 'v8',
monikers => 'v8',
column_accessors => 'v8',
force_ascii => 1,
},
},
[ 'dbi:Pg:dbname=dbname', 'dbuser', 'dbpass' ],
);
exit;
This gives this:
├── Schema
│   │   └── Result
│   │   ├── Cclite2
│   │   │   ├── OmAuth.pm
│   │   │   ├── OmCategory.pm
etc.
│   │   │   └── OmYellowpage.pm
│   │   ├── OmAdminBalanceView.pm
│   │   ├── OmAdsRss.pm
etc.
│   │   ├── OmTradesByUserView.pm
│   │   └── OmVolumeView.pm
│   └── Schema.pm
This is not criticism of the first answer. It's just an alternative that suits me and may suit others.

Plone 4: restricting published content

On a Plone 4.x install, can either
published content be restricted to a certain user/group or
private content be made to show in menus and searches for not-logged-in users ?
I have a customer who wants to be able to have content that can only be viewed by a certain user/group, but will show up in a menu or search when not logged in.
What would be the best approach for achieving this functionality?
You'll have to customize your workflow as below:
go to Zope Management Interface-> portal_workflow
Create a new state, let's say "Trailer" (this is optional, you could customize an existing state instead...maybe the private state would be a good option for dealing with restrictions for specific users/groups)
Remove all permissions but "Access contents information" from the Anonymous user in that specific state
Push the "Update security settings" button
Done!
Now all contents in the "Trailer" state will be searchable but not viewable by anonymous users.
Note: if you choose to create a new state, as I'd suggest, be sure to add all needed transitions too.
Edit:
Unfortunately I wasn't aware that in recent Plone's versions, there's a new index in the portal_catalog (allowedRolesAndUsers) that prevents the process above to work as it used to. The process above is still correct, though you'll need to override the default indexer.
First create a new package with paster using the "plone" template. Then add in the main level of the package (e.g. my.package/my/package) a file called indexers.py with this:
from zope.interface import Interface
from plone.indexer.decorator import indexer
from AccessControl.PermissionRole import rolesForPermissionOn
from Products.CMFCore.utils import getToolByName
from Products.CMFCore.CatalogTool import _mergedLocalRoles
#indexer(Interface)
def allowedRolesAndUsers(obj):
"""Return a list of roles and users with View permission.
Used by PortalCatalog to filter out items you're not allowed to see.
"""
allowed = {}
for r in rolesForPermissionOn('Access contents information', obj):
allowed[r] = 1
# shortcut roles and only index the most basic system role if the object
# is viewable by either of those
if 'Anonymous' in allowed:
return ['Anonymous']
elif 'Authenticated' in allowed:
return ['Authenticated']
localroles = {}
try:
acl_users = getToolByName(obj, 'acl_users', None)
if acl_users is not None:
localroles = acl_users._getAllLocalRoles(obj)
except AttributeError:
localroles = _mergedLocalRoles(obj)
for user, roles in localroles.items():
for role in roles:
if role in allowed:
allowed['user:' + user] = 1
if 'Owner' in allowed:
del allowed['Owner']
return list(allowed.keys())
and then in the same level add a file overrides.zcml with this:
<configure xmlns="http://namespaces.zope.org/zope">
<adapter factory=".indexers.allowedRolesAndUsers" name="allowedRolesAndUsers" />
</configure>
In the end the tree of your product should look like this:
my.package/
├── my
│   ├── __init__.py
│   └── package
│   ├── configure.zcml
│ ├── overrides.zcml
│ ├── indexers.py
│   ├── __init__.py
│   ├── profiles
│   │   └── default
│   │   └── metadata.xml
│   └── tests.py
├── README.txt
├── setup.cfg
└── setup.py
Last thing, you need to include the newly created egg in your buildout.cfg:
eggs =
my.package
develop =
src/my.package
Rerun buildout. That's all.