New enrolment method is not working properly in Totara (Moodle) - moodle

I am trying to add a new custom enrolment method to a base Totara (Moodle) installation. I decided to copy the existing "Self enrolment" enrolment method, because it is relatively simple and therefore seemed to be a good starting point for my custom method.
I changed the name and all of the references to the "Self enrolment" method to my new method name. This seemed to have worked because when I logged into the Totara dashboard I got the popup for a new plugin installation.
I went through the installation process and didn't run into any errors. Next I activated my plugin in the "Enrolment plugin" menu. Everything seemed fine until I tried to add my new method to a course.
Firstly the new method should be displayed bij default on the "Enrolment methods" page of the course but it isn't, however I can select it with the dropdown located below. When I do I am redirected to the config page of the enrolment method, so far so good. But when I click the button on the bottom of the page to add the method to the course it still isn't visible on the "Enrolment methods" page of the course.
When I log in as a regular user I can't access the course through my new method, so the method and the course haven't been properly linked. However when I look in the database I do see that my new method has been added to the course in the enrol table.
The problem doesn't seem to be caching related because I have purged it a couple of times already but the method still won't show up.
I am working with Totara version 16.2 (Moodle 3.4.9)

I found the problem, it seems that the "Self enrolment" plugin uses the get_name() function of the /server/lib/enrollib.php by default. However this function is written in a way to avoid "fancy" plugin names according to a comment in the function. The function explodes your plugin name on "_" and only uses the first part of your name, which means the records in enrol won't be recognised as records of your plugin because the names don't match.
I solved this by adding my own get_name() to my plugin, which returns the whole name.
The function:
/**
* Returns name of this enrol plugin
* #return string
*/
public function get_name() {
// second word in class is always enrol name, sorry, no fancy plugin names with _
$words = explode('_', get_class($this));
return $words[1];
}

Related

Object reference exception when importing content in Episerver

We are using Optimizely/Episerver CMS 11.20. When trying to export a page hierarchy from our production environment and then import the resulting ExportedFile.episerverdata file to our acceptance test environment I get the following error:
[Importing content 70725_133679] Exception: Object reference not set to an instance of an object.
I have no idea what 70725_133679 is referring to. Is there a way to look this up, e.g. via an SQL query against the "EPi" database?
It refers to a specific version of some content (which could be just about anything).
You could try to browse to https://yoursite/EPiServer/CMS/#context=epi.cms.contentdata:///70725_133679 (note the ID at the end) to see which content it is.
Got another answer on a Optimizely forum (thanks to Surjit Bharath):
The following SQL against the EPi database gives information about the referenced content:
select * from tblContent where pkID = 70725 
select * from tblWorkContent where pkID = 133679
This too points to a submit button. I have yet to understand why that block would cause an exception during import, but now I at least have a place to start digging.

How to trigger a function when a custom field is edited/created in redmine

I have been researching about this term for so many hours, but I found anything useful yet.
Hope you can help us building this redmine plugin, or provide us some research links to help us find the correct key.
What we want to build
The thing is we want to update one custom field in redmine (let's name it 'Target_CF') whenever another one is created or updated.
We are looking for incrementing possible values of Target_CF, so we can have all custom field's names available to select.
Of course, we want to achieve this without directly editing Redmine's Core, so we thought developing a plugin would be the best approach.
Our plugin also creates and configures a new custom field (the one mentioned above), but I will let this out of the question, because I think it is not relevant for this.
Where we are right now
We have identified some hooks that could be useful for us, as the following:
:controller_custom_fields_new_after_save
:controller_custom_fields_edit_after_save
We have the following directories/files structure so far:
plugins/
custom_plugin/
init.rb
lib/
hooks.rb
The code we have written
init.rb
require_dependency 'hooks'
Redmine::Plugin.register :custom_plugin do
name 'custom_plugin'
author 'author name'
description 'description text'
version '1.0.0'
end
hooks.rb
class Hooks < Redmine::Hook::ViewListener
def controller_custom_fields_edit_after_save(context={ })
#target_custom_field_name = "Target_CF"
CustomField.find_by_name(#target_custom_field_name).possible_values.push(context[:custom_field].name)
end
end
The result of this code is none. I mean, no erros, no updates, nothing at all. There is no change in our possible values after editing/creating another custom field. We are sure there is something we don't know, some concept or workflow, and due to this we are doing something so badly.
Please, help us understeand what we are missing.
Previously we have succesfully developed another plugin that overwrites certain views. So we have kind of little skills in views related plugins, but zero experience at all at controllers ones.
We are using a Redmine 3.2.0 stack by Bitnami and a mysql database.
Well, finally we found out how to extend base controller's methods. I will post it here so hopefully this can be useful to anyone who finds the same doubts we had.
After some more researching, we conclude that it is mandatory to extend base controllers, in order to not directly modify core methods.
This is our final directories/files structures:
plugins/
custom_plugin/
init.rb
lib/
custom_plugin/
issue_custom_field_patch.rb
We previously stated that we could use some hooks to inject our desired functionality, but it seems not to work that way with controllers. On the other hand, we built a patch which will extend target class functionality.
Our final working code
Init.rb
require 'redmine'
ActionDispatch::Callbacks.to_prepare do
require_dependency 'issue_custom_field'
unless IssueCustomField.included_modules.include? CustomPlugin::IssueCustomFieldPatch
IssueCustomField.send(:include, CustomPlugin::IssueCustomFieldPatch)
end
end
Redmine::Plugin.register :custom_plugin do
name 'custom_plugin'
author 'author name'
description 'description text'
version '1.0.0'
end
issue_custom_field_patch.rb
module CustomPlugin
module IssueCustomFieldPatch
def self.included(base) # :nodoc:
base.extend(ClassMethods)
base.send(:include, InstanceMethods)
base.class_eval do
unloadable
after_save :update_possible_values
end
end
end
module ClassMethods
end
module InstanceMethods
def update_possible_values
self.reload
updatedPossibleValues unless self.name == "Target_CF"
end
private
def updatedPossibleValues
#safe_attrs = ['project', 'description', 'due_date', 'category', 'status', 'assigned_to', 'priority', 'fixed_version', 'author', 'lock_version', 'created_on', 'updated_on', 'start_date', 'done_ratio', 'estimated_hours', 'is_private', 'closed_on']
#custom_fields = IssueCustomField.all.select {|cf| !cf[:position].nil?}.collect {|cf| cf.name}
#possible_values = #safe_attrs + #custom_fields
CustomField.find_by_name("Target_CF").update possible_values: #possible_values
end
end
CustomField.send(:include, IssueCustomFieldPatch)
end
Functionality explained
As we stated in the question, we needed to update Target_CF possible values each time the users create/modify/removes a custom field from Redmine.
We extended IssueCustomField's class's instance methods, triggering our new function 'updatedPossibleValues' after each save. This includes creating new custom fields, and of course, updating existing ones and removing them. Because we reload our list of possible values each time, we had to control if its position were nil. If it is, this means that custom field has been removed.
Because of the ultimate action of this patch, which is the updating of another custom field, this also triggered our function, causing an infinite loop. To prevent this, we linked our functionality to every other custom field which name was not 'Target_CF'. A bit rusty fix, but we couldn't find a better approach.
I hope this can be useful to somebody in the future, as they could invest a fraction of time that we spent on this.
Comments, fixes and improvements are very welcome.
Based on: https://www.redmine.org/projects/redmine/wiki/Plugin_Internals which is a bit outdated, but finally could complete the code with help of another resources and forums.

Automatically send email about editing google spreadsheet

I'm working on a rather simple script which should handle new values in the spreadsheet and then send emails to specified addresses. And I faced with the problem. My code is listed below:
function onEdit(e) {
//part of the code for checking e.range to process only updated values
sendEmail();
}
function sendEmail() {
// arguments are missed only for demo
GmailApp.sendEmail();
}
While I'm using "simple trigger", my function "sendEmail()" works only if I start it from script editor. I allowed sending emails on behalf of my at first time and then function works fine. But if I'm changing the value in the spreadsheet - function "onEdit(e)" processes new data but function "sendEmail()" does nothing.
I partly solved this problem by using project's triggers from "current project's triggers" menu. In that case, function "sendEmail()" works properly, but I have no access to the information about update.
For my purposes I could use just second way and find new values "manually" every time, but I wish to optimize this work.
So, my questions are:
Is the process I described above proper or I made a mistake
anywhere?
If process proper, is where a way to combine both cases?
Thanks!
You correctly understood that (as the docs say) simple triggers cannot send an email, because they run without authorization. An installable trigger, created via Resources menu, can: it has the same rights as the user who created the trigger. If it is set to fire on edit, it will get the same type of event object as a simple trigger does.
So, a minimal example would be like this, set to run "on edit":
function sendMail(e) {
MailApp.sendEmail('user#gmail.com', 'sheet edited', JSON.stringify(e));
}
It emails the whole event object in JSON format.
Aside: if your script only needs to send email but not read it, use MailApp instead of GmailApp to keep the scope of permissions more narrow.

Refresh or Clear GUI (Apps Script Gadget Embedded in Google Site)

I have a small form that my colleagues and I often fill out that can be seen here, with source code (view-only) here. The spreadsheet that this form posts to can be seen (view-only) here.
Basically, the apps script is solely for preventing my coworkers from having to scroll through thousands of rows of a spreadsheet (this is also used for our film collection) to check in/out inventory. It will also, soon, post detailed history of the checkout in an audit spreadsheet.
I have attempted, with no success, to clear the values of the text fields after the data has been posted to the spreadsheet. What I essentially want to do is restart the GUI so that the fields are once again blank.
I have tried accessing the text fields by id but the return type is a Generic widget, which I of course can't do anything with. And since control is passed to the handler functions, the text fields can't be accessed, or at least I can't figure out how (and I looked through the documentation and online solutions for hours last night).
So the question: how can I erase/clear the values of the text fields in the GUI after values have been posted to the spreadsheet? (return from handler function to access text fields again, or restart the GUI?
I can accomplish restarting the script runs on a spreadsheet, but I have not been able to do this when it is embedded in a Google site.
The getElementById('xxxxx').setText('') approach was the right way to do it. but the way you tried to test it doesn't work... the new value would only be available in the next handler call.
/*
This function only has the desired effect if it is called within the handler
function (not within a nested function in the handler function).
*/
function restartGUI() {
var gui = UiApp.getActiveApplication();
gui.getElementById('equipmentIDField1').setText('');
gui.getElementById('equipmentIDField2').setText('');
gui.getElementById('equipmentIDField3').setText('');
gui.getElementById('studentLastNameField').setText('');
gui.getElementById('labasstLastNameField').setText('');
return gui;
}

Zend Lucene with symfony and i18n

I've went through the Jobeet Tutorial for integrating Zend Lucene into a symfony (1.4.8) project in order to add search capabilities into my frontend of my site (through indexing). Among others, the key concept is to use updateLuceneIndex during model's save action (needs to be overridden) in order to create/update the index of the specific entry.
My model has i18n fields, some of which (i,e name, title) I want to be inserted in the index. Everything works as expected but when it comes to save the i18n fields into the index all I get is blank values ($this->getName() returns empty string). I'm inspecting the created index with the Luke.
I ended up that this has nothing to do with the Zend Lucene but with symfony. It seems that during save the information for i18n fields isn't available (or is it?). I've also tried hook up the update during preSave(), postSave() but no avail.
So I want to ask how am I supposed to get my model's i18n field values during the save action in order to update the index accordingly?
Important note: This happens only during doctrine:data-load task. If I manually insert or update a record the index gets updated accordingly.
One last related question. It would be nice if I could save different keywords for each of the languages of the field of the model. How can I get the different values for each field's language inside the model?
The reason of this strange behaviour of Symfony is that when you are loading fixtures via cli, it has no context loaded (for instance when you try to get context instance sfContext::getInstance(), youll get "context instance does not exists" error exception).
With no context instance available, there is no "current culture" and with no current culture, there is no value of i18n fields.
The symfony context actualy supports all I18N functionalities with current User culture ($currentUserCulture = sfContext::getInstance()->getUser->getCulture()).
This all means 2 things:
You cant use symfony "current user culture" capabilities while you are
in cli session
If you needs to have sfContext::getInstance() somewhere in your
code (especialy in the models), you have to close it into condition to avoid any troubles with unexpected and hard to find exceptions while in cli
Example of getting current culture in model class (it will not pass condition while in cli):
if (sfContext::hasInstance()) {
sfContext::getInstance()->getUser()->getCulture();
}
So when you cant use Symfony i18n shortcuts (like $record->getName()), you have to work around it.
In Your symfony1-doctrine models you always have $this->Translation object available.
So you can access your translation values object via something like $this->Translation[$culture].
Its up to you to work with that, you can use your default culture $this->Translation[sfConfig::get('sf_default_culture')], or interate trough all your supported cultures from some global configuration (i recommends you to set it in one of your configuration files globaly accross of all apps - maybe /config/app.yml).
Example of getting $record Translation object in any situations:
if (sfContext::hasInstance()) {
$translation = $this->Translation[sfContext::getInstance()->getUser()->getCulture()];
}
else {
$translation = $this->Translation->getFirst();
// or: $translation = $this->Translation[$yourPreferedCulture];
}
// you can access to modified fields of translation object
$translationModified = $translation->getModified();