How to add cohort to a course programmatically? - moodle

I want to add addcohort to a course programmatically in moodle. I have gone through the documentation but did find away out this there. Is there any function like chort_add_course() with parameters as course id and chort id?

I'm guessing you mean add a cohort enrol method to a course? Then users in that cohort will be automatically enrolled onto the course.
The code is in /enrol/cohort/edit.php and /enrol/cohort/edit_form.php - but something like this will do it automatically.
require_once($CFG->dirroot . '/enrol/cohort/locallib.php');
function cohort_add_course($courseid, $cohortid) {
global $DB;
if (!enrol_is_enabled('cohort')) {
// Not enabled.
return false;
}
if ($DB->record_exists('enrol', array('courseid' => $courseid, 'enrol' => 'cohort'))) {
// The course already has a cohort enrol method.
return false;
}
// Get the cohort enrol plugin
$enrol = enrol_get_plugin('cohort');
// Get the course record.
$course = $DB->get_record('course', array('id' => $courseid));
// Add a cohort instance to the course.
$instance = array();
$instance['name'] = 'custom instance name - can be blank';
$instance['status'] = ENROL_INSTANCE_ENABLED; // Enable it.
$instance['customint1'] = $cohortid; // Used to store the cohort id.
$instance['roleid'] = $enrol->get_config('roleid'); // Default role for cohort enrol which is usually student.
$instance['customint2'] = 0; // Optional group id.
$enrol->add_instance($course, $instance);
// Sync the existing cohort members.
$trace = new null_progress_trace();
enrol_cohort_sync($trace, $course->id);
$trace->finished();
}

Take a look in enrol/cohort/ajax.php and scroll down to "case 'enrolcohort':". There isn't a single cohort enrolment function, but the few lines of code there should cover what you want.
Note - you could also use the 'enrolcohortusers' section just below this - the difference is the first will continue to update enrolments if new users are added to / removed from the cohort, the second adds the current users, but doesn't update in the future.

Related

Get Line Items in an Invoice logic hook in SuiteCRM

Via a logic hook I'm trying to update fields of my products, after an invoice has been saved.
What I understand so far is, that I need to get the invoice related AOS_Products_Quotes and from there I could get the products, update the required fields and save the products. Does that sound about right?
The logic hook is being triggered but relationships won't load.
function decrement_stocks ( $bean, $event, $arguments) {
//$bean->product_value_c = $bean->$product_unit_price * $bean->product_qty;
$file = 'custom/modules/AOS_Invoices/decrement.txt';
// Get the Invoice ID:
$sInvoiceID = $bean->id;
$oInvoice = new AOS_Invoices();
$oInvoice->retrieve($sInvoiceID);
$oInvoice->load_relationship('aos_invoices_aos_product_quotes');
$aProductQuotes = $oInvoice->aos_invoices_aos_product_quotes->getBeans();
/*
$aLineItemslist = array();
foreach ($oInvoice->aos_invoices_aos_product_quotes->getBeans() as $lineitem) {
$aLineItemslist[$lineitem->id] = $lineitem;
}
*/
$sBean = var_export($bean, true);
$sInvoice = var_export($oInvoice, true);
$sProductQuotes = var_export($aProductQuotes, true);
$current = $sProductQuotes . "\n\n\n------\n\n\n" . $sInvoice . "\n\n\n------\n\n\n" . $sBean;
file_put_contents($file, $current);
}
The invoice is being retrieved just fine. But either load_relationship isn't doing anything ($sInvoice isn't changing with or without it) and $aProductQuotes is Null.
I'm working on SuiteCRM 7.8.3 and tried it on 7.9.1 as well without success. What am I doing wrong?
I'm not familiar with SuiteCRM specifics, however I'd always suggest to check:
Return value of retrieve(): bean or null?
If null, then no bean with the given ID was found.
In such case $oInvoice would stay empty (Your comment suggests that's not the case here though)
Return value of load_relationship(): true (success) or false (failure, check logs)
And I do wonder, why don't you use $bean?
Instead you seem to receive another copy/reference of $bean (and calling it $oInvoice)? Why?
Or did you mean to receive a different type bean that is somehow connected to $bean?
Then its surely doesn't have the same id as $bean, unless you specifically coded it that way.

Trigger works but test doesn't cover 75% of the code

I have a trigger which works in the sandbox. The workflow checks the field in the campaign level and compares it with the custom setting. If it matches, then it returns the target to the DS Multiplier field. The trigger looks as follows
trigger PopulateTarget on Campaign (before insert, before update)
{
for(Campaign campaign : Trigger.new)
{
if (String.isNotBlank(campaign.Apex_Calculator__c) == true)
{
DSTargets__c targetInstance = DSTargets__c.getInstance(campaign.Apex_Calculator__c);
{
String target = targetInstance .Target__c;
campaign.DS_Target_Multiplier__c = Target;
}
}
}
}
However, I had problems to write a proper test to this and asked for the help on the internet. I received the test
#isTest
private class testPopulateTarget{
static testMethod void testMethod1(){
// Load the Custom Settings
DSTargets__c testSetting = new DSTargets__c(Name='Africa - 10 Weeks; CW 10',Target__c='0.1538', SetupOwnerId = apexCalculatorUserId);
insert testSetting;
// Create Campaign. Since it would execute trigger, put it in start and stoptests
Test.startTest();
Campaign testCamp = new Campaign();
// populate all reqd. fields.
testCamp.Name = 'test DS campaign';
testCamp.RecordTypeId = '012200000001b3v';
testCamp.Started_Campaign_weeks_before_Event__c = '12 Weeks';
testCamp.ParentId= '701g0000000EZRk';
insert testCamp;
Test.stopTest();
testCamp = [Select ID,Apex_Calculator__c,DS_Target_Multiplier__c from Campaign where Id = :testCamp.Id];
system.assertEquals(testCamp.DS_Target_Multiplier__c,testSetting.Target__c);// assert that target is populated right
}
}
Such test returns the error "Compile Error: Variable does not exist: apexCalculatorUserId at line 6 column 122". If I remove that ApexCalculator part System.assertEquals then the test passes. However it covers 4/6 part of the code (which is 66%)
Could anyone help me how should I amend the code to make the coverage of 75%?
Yes, apexCalculatorUserId has not been defined. The code you were given appears to be incomplete. You'll need to look at the constructor DSTargets__c and see what kind of ID it is expecting there.
At a guess, you could try UserInfo.getUserId() to get the ID of the current user, but that may not be the ID that's expected in the constructor. It would be worth trying it to see if the test coverage improves.
1) Replace apexCalculatorUserId with UserInfo.getUserId()
2) I'm not sure what kind of field is Apex_Calculator__c on campaign. If its not a formula you want to insert a new line before "insert testCamp". Something like:
testCamp.Apex_Calculator__c = UserInfo.getUserId();

Automatically set author and email in tt_news

I know I can configure tt_news to automatically set author and email to a given value, like this:
TCAdefaults.tt_news.author = full name
TCAdefaults.tt_news.author_email = name#domain.tld
But could I retrieve the name an email from the info of the currently logged BE user instead?
To meake it possible to fill the values on every change (if empty), you need to register a hook somewhere ie, in your own extension
typo3conf/ext/yourext/hooks/class.tx_ttnews_hooks.php
class tx_ttnews_hooks {
// hook for prefilling TCA values
function getSingleField_preProcess($table, $field, &$row, $altName, $palette, $extra, $pal, $pObj) {
switch($field) {
case 'author_email':
if($row[$field] == '') {
$row[$field] = $GLOBALS['BE_USER']->user['email'];
}
break;
case 'author':
if($row[$field] == '') {
$row[$field] = $GLOBALS['BE_USER']->user['realName'];
}
break;
}
}
}
and then add this into typo3conf/ext/yourext/ext_localconf.php:
$TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_tceforms.php']['getSingleFieldClass'][]
= 'EXT:yourext/hooks/class.tx_ttnews_hooks.php:tx_ttnews_hooks';
There is an extension that does what you want. I haven't used it myself, but the description sounds pretty promising.
As another solution, you may write a little bit of php code that adds a dynamic user ts. I only found an example in German, but maybe that's helpful anyway.

MVC 3 - Entity Framework & POCO - Many to Many REMOVAL on ActionLink()

I have Many to Many Relationships for:
Company -----> CompanyVenue <------
Venue
I can currently add a Venues to a Companies and Companies to Venues.
The Add Venue link takes the user to this view: (using this controller logic):
var venue = context.Companies.Select(x => x.Venues.Where(xy => xy.VenueId == id));
var company = venue.Select(x => x.);
ViewData["CompanyFor"] = company;
ViewData["VenueFor"] = venue;
Once the User Selects the Venue and Clicks the ADD VENUE Button, it passes the ViewModel back to Controller:
Company c = context.Companies.Single(x => x.CompanyId == model.CompanyId);
Venue v = context.Venues.Single(x => x.VenueId == model.VenueId);
c.Venues.Add(v);
context.SaveChanges();
So That is how I implimented the Many-Many Add. But, I Am looking for some help with Removal Controller Code / View (If Needed) / Possibly A View Model (but don't really see the need for what is essential a simple action)
In the Company Details View I am trying to get REMOVE Link underneath each Venue Item to Remove the [CompanyVenue] (many2many) Relationship.
If it is possible to pass 2 Values instead of one using this type of ActionLink():
#Html.ActionLink("Remove", "VenueRemove", new { Id = venue.VenueId }, new { Cid = model.CompanyId } )
it would be easy, but I have not figured out a way (load method) to pass both Values ( Id & Cid ) back to a VenueRemove() Controller Action, that would then return a RedirectToAction() back to the Company Details view.
If someone knows how to do that Please let me know. (Thank You Darin for the awnser to that)
I went ahead and got the Remove Working Using a View.
But is there anyway to get the Remove to work by just clicking the REMOVE Link/button and have it remove the venue with out going to the view?
Maybe an AJAX request or something.... ???
You can pass two parameters to the VenueRemove action like this:
#Html.ActionLink(
"Remove",
"VenueRemove",
new {
Id = venue.VenueId,
Cid = model.CompanyId
}
)

How to populate zend form field using session?

I am using sessions to populate a multi select box with options in my Zend application.
The user selects one or more options and fills in other fields on the form and then submits. If the user didn't select all of the options in the multi select then the form is displayed again but the multi select only has the options that the user did not select the last time. This process goes on until there are no more options from the multi select left to process.
Here is the code I use to get rid of the options that have already been processed so that they are not used to populate the multi select box:
if($form_successful){
// TODO remove $post['keyword_names'] (i.e. already processed) from $keyword_names (that come from $_SESSION)
$keyword_names = array_diff($keyword_names, $post['keyword_names']);
print_r($keyword_names);
if(is_array($keyword_names) && !empty($keyword_names)){
// save updated $keyword_names into $_SESSION['workflow1']
$session = new Zend_Session_Namespace('workflow1');
$session->keyword_names = $keyword_names;
// set flag to false so that we display form again
$form_successful = false;
}else{ // all keywords have been assigned
// go to next step
$this->_redirect('/workflow-1/step-'.($step+1).'/');
}
}
print_r($keyword_names); displays the correct options, however when the form is loaded when the user submits, the multi select displays the options that were there from the begining ie the options the user has just selected and submitted are not being taken out of the multi select, it is only when the user submits the form again then the multi select box updates.
Appreciate the help.
Solved the issue by making use of URL parameters. Here is the code (might differ a lot from what I posted first because some big changes were made):
// after successful form submission
if($form_successful){
// remove $post['keyword_names'] (i.e. already processed) from $keyword_names (that come from $_SESSION)
$keyword_names = array_diff($keyword_names, $post['keyword_names']);
// save remaining $keyword_names into $_SESSION['workflow1']
$session = new Zend_Session_Namespace('workflow1');
$session->keyword_names = $keyword_names;
if(is_array($keyword_names) && !empty($keyword_names)){
// redirect to the same step again - to ensure that the form will reflect (in select lists) newly created AdGroup and/or Campaign
// GET parameteres ($params_array) provide a way to remember user's choice
$params_array = array();
if(!empty($post['match_type_id'])){
$params_array['match_type_id'] = $post['match_type_id'];
}
if(!empty($post['with_permutations'])){
$params_array['with_permutations'] = $post['with_permutations'];
}
if(!empty($ad_group_id)){
$params_array['ad_group_id'] = $ad_group_id;
}
$this_step_url = UrlUtils::assemble('', $this->getRequest()->getActionName(), $this->getRequest()->getControllerName(), $this->getRequest()->getModuleName(), $params_array);
$this->_redirect($this_step_url);
}else{ // all keywords have been assigned
// go to next step
$this->_redirect('/workflow-1/step-'.($step+1).'/');
}
}
So you don't have any code about Zend_Form object here. How do you populate the form element? If you post your class code which extends Zend_Form (or any other code dials with your form) then I may help. But in any case you can populate your multiselectbox with setMultiOptions() method or addMultiOption() for each item in multiselectbox.