Moodle email confirmation - moodle

When a new user register, moodle send confirmation email to user, from the language file lang/en/moodle.php with a string "emailconfirmation" by a moodle function:
function send_confirmation_email($user) {
global $CFG;
$site = get_site();
$supportuser = generate_email_supportuser();
$data = new stdClass();
$data->firstname = fullname($user);
$data->sitename = format_string($site->fullname);
$data->admin = generate_email_signoff();
$subject = get_string('emailconfirmationsubject', '', format_string($site->fullname));
$username = urlencode($user->username);
$username = str_replace('.', '%2E', $username); // prevent problems with trailing dots
$data->link = $CFG->wwwroot .'/login/confirm.php?data='. $user->secret .'/'. $username;
$message = get_string('emailconfirmation', '', $data);
$messagehtml = text_to_html(get_string('emailconfirmation', '', $data), false, false, true);
return email_to_user($user, $subject, $message, $messagehtml);
}
Moodle redirects to index.php after confirmation. How do I redirect $data->link to a custom page after confirmation:
if (send_confirmation_email($user)) {
if (AUTH_CONFIRM_OK = true) {
$urltogo = new moodle_url($CFG->wwwroot . "/coursestat/view.php", array('id' => $statid));
redirect($urltogo);
}
}
else
print_error('auth_noemail','auth_email');

Just use $data->link assuming its a string?
if (send_confirmation_email($user)) {
// Assuming $data->link is a string?
$urltogo = new moodle_url($data->link);
redirect($urltogo);
}
This line
if (AUTH_CONFIRM_OK = true) {
Doesn't make sense, AUTH_CONFIRM_OK is a constant. Also = is an assignment. I'm guessing you meant something like this:
if ($somevariable == AUTH_CONFIRM_OK) {

Related

Moodle File Manager - Crucial part

I am trying to show the file uploaded from File Manager mform element. I could store the file to mdl_files. To get the file saved is a bit hard to program. I tried implementing few options from Moodle Forums, but was stuck here. I really hope that someone can provide a solution for Moodle File manager (a crucial part). Could anyone guide me where I went wrong and suggest me to get the fileurl.
<?php
require('config.php');
require_once($CFG->libdir.'/formslib.php');
class active_form extends moodleform {
function definition() {
$mform = $this->_form;
$fileoptions = $this->_customdata['fileoptions'];
$mform->addElement('filemanager', 'video', get_string('video', 'moodle'), null,
$fileoptions);
$this->add_action_buttons();
}
function validation($data, $files) {
$errors = parent::validation($data, $files);
return $errors;
}
}
// Function for local_statistics plugin.
function local_statistics_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload,
array $options=array()) {
global $DB;
if ($context->contextlevel != CONTEXT_SYSTEM) {
return false;
}
$itemid = (int)array_shift($args);
if ($itemid != 0) {
return false;
}
$fs = get_file_storage();
$filename = array_pop($args);
if (empty($args)) {
$filepath = '/';
} else {
$filepath = '/'.implode('/', $args).'/';
}
$file = $fs->get_file($context->id, 'local_statistics', $filearea, $itemid, $filepath,$filename);
if (!$file) {
return false;
}
// finally send the file
send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
}
// Form Settings
$fileoptions = array('maxbytes' => 0, 'maxfiles' => 1, 'subdirs' => 0, 'context' =>
context_system::instance());
$data = new stdClass();
$data = file_prepare_standard_filemanager($data, 'video', $fileoptions, context_system::instance(),
'local_statistics', 'video', 0);
$mform = new active_form(null, array('fileoptions' => $fileoptions));
// Form Submission
if ($data = $mform->get_data()) {
$data = file_postupdate_standard_filemanager($data, 'video', $fileoptions,
context_system::instance(), 'local_statistics', 'video', 0);
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'local_statistics', 'video', '0', 'sortorder', false);
foreach ($files as $file) {
$fileurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(),
$file->get_filearea(), $file->get_itemid(), $file->get_filepath(),
$file->get_filename());
echo $fileurl;
}
}
?>
I've had a quick look through your code and it all looks reasonable, but you've not included the code of the local_statistics_pluginfile() function in local/statistics/lib.php - without that function, Moodle is unable to authenticate any requests from the browser to serve files, so all files will return a 'file not found' message.
Have a look at the documentation for details of what the x_pluginfile function should look like (or look for examples in any of the core plugins in Moodle): https://docs.moodle.org/dev/File_API#Serving_files_to_users

Retrieve Lead Ads Facebook API

I have problems retrieving Lead Ads.
I have the Ad-ID and the Page-ID. I haven't created them, but was added as a developer.
I was trying to use the PHP SDK and this https://developers.facebook.com/docs/marketing-api/guides/lead-ads/v2.9
Nothing is working. I cannot find a nice tutorial about that.
I just want to retrieve the leading Ads!
Anyone?
Assuming you have already installed FB API SDK and configured your FB app, you can use this to get all results from all LeadAds of your $page_id
use FacebookAds\Api;
use FacebookAds\Object\Page;
use FacebookAds\Object\Ad;
$access_token = 'YOUR TOKEN';
$app_id = 'YOUR APP ID';
$app_secret = 'YOUR APP SECRET';
$page_id = 'YOUR PAGE ID';
Api::init($app_id, $app_secret, $access_token);
$ads = getAllLeadsAds($page_id);
$result = array();
foreach ($ads->data as $item) {
$leads = getLeadAdInfo($item->id);
$i = 0;
foreach ($leads->data as $value) {
$result[$i]['ad_id'] = $item->id;
$result[$i]['lead_id'] = $value->id;
$result[$i]['form'] = $value->field_data;
$i++;
}
}
print_r($result);
function getAllLeadsAds($page)
{
$page = new Page($page);
return $page->getLeadgenForms()->getResponse()->getBody();
}
function getLeadAdInfo($ad)
{
$ad = new Ad($ad);
return $ad->getLeads()->getResponse()->getBody();
}
Its possible that you not calling all ads inside adset.
You can use facebook cursor.
$cursor->fetchAfter();
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Values\ArchivableCrudObjectEffectiveStatuses;
use FacebookAds\Object\Fields\CampaignFields;
use FacebookAds\Object\AdCampaign;
use FacebookAds\Object\Fields\AdSetFields;
use FacebookAds\Object\AdSet;
use FacebookAds\Object\Fields\AdFields;
use FacebookAds\Object\Ad;
use FacebookAds\Object\Campaign;
function getadsetname($adset_id,$acc_id){
$adset = new AdSet($adset_id, $acc_id);
$adset->read(array(
AdSetFields::NAME,
));
return $adset->name; // Outputs name of adset.
}
function get_campaignname($campaign_id){
$campaign = new Campaign($campaign_id);
$campaign->read(array(
CampaignFields::NAME,
));
return $campaign->name;
}
$account = new AdAccount($account_id);
echo $adset_name = getadsetname($adset_id,$account_id);
echo $campaign_name = get_campaignname($campaign_id);
echo "<hr>";
$adcampaign = new AdAccount($campaign_id);
$adset = new AdSet($adset_id);
$ads = $adset->getAds(array(
AdFields::NAME,
AdFields::CONFIGURED_STATUS,
AdFields::EFFECTIVE_STATUS,
AdFields::CREATIVE,
));
$ads->fetchAfter();
foreach ($ads as $ad) {
$ad_id = $ad->id;
$ad_name = $ad->name;
if($ad->configured_status=="ACTIVE"){
$ad1 = new Ad($ad_id);
$leads = $ad1->getLeads();
$leads->fetchAfter();
foreach ($leads as $lead) {
$fname = $lead->field_data;
//var_dump($fname);
$data = array();
$data['lead_id'] = $lead->id;
$data['created_time'] = $lead->created_time;
$data['ad_id'] = $ad_id;
$data['ad_name'] = $ad_name;
$data['adset_id'] = $adset_id;
$data['adset_name'] = $adset_name;//$adset_name;
$data['campaign_id'] = $campaign_id;
$data['campaign_name'] = $campaign_name;
$data['form_id'] = $lead->form_id;//$lead->id;
$data['is_organic'] = $lead->is_organic;//$lead->id;
}
}
}
$ads->fetchAfter(); will get the next list of ads and
$leads->fetchAfter(); will get the next list of leads

How to add variable to header.php controller and use it in header.tpl

I am creating a custom theme for OpenCart 2.3 and I need to show some additional information in page header (header.tpl). So I added some variable to catalog/controller/common/header.php:
$data['some_var'] = 'some_value';
And then I am trying to use this data in the header.tpl:
<?php echo $this->data['some_var']; ?>
But I am always getting this error:
Notice: Undefined index: some_var in /var/www/store_com/public_html/catalog/view/theme/mystore/template/common/header.tpl on line 133
If I try to use this code:
<?php echo $some_var; ?>
Then I am getting another error:
Notice: Undefined variable: some_var in /var/www/store_com/public_html/catalog/view/theme/mystore/template/common/header.tpl on line 133
And even when I do echo print_r($this->data) in header.tpl I don't even see this variable in $data array.
What am I doing wrong? Please help.
EDIT
Here is the full code of my header.php controller file. I added my custom variable at the very end of the file.
class ControllerCommonHeader extends Controller {
public function index() {
// Analytics
$this->load->model('extension/extension');
$data['analytics'] = array();
$analytics = $this->model_extension_extension->getExtensions('analytics');
foreach ($analytics as $analytic) {
if ($this->config->get($analytic['code'] . '_status')) {
$data['analytics'][] = $this->load->controller('extension/analytics/' . $analytic['code'], $this->config->get($analytic['code'] . '_status'));
}
}
if ($this->request->server['HTTPS']) {
$server = $this->config->get('config_ssl');
} else {
$server = $this->config->get('config_url');
}
if (is_file(DIR_IMAGE . $this->config->get('config_icon'))) {
$this->document->addLink($server . 'image/' . $this->config->get('config_icon'), 'icon');
}
$data['title'] = $this->document->getTitle();
$data['base'] = $server;
$data['description'] = $this->document->getDescription();
$data['keywords'] = $this->document->getKeywords();
$data['links'] = $this->document->getLinks();
$data['styles'] = $this->document->getStyles();
$data['scripts'] = $this->document->getScripts();
$data['lang'] = $this->language->get('code');
$data['direction'] = $this->language->get('direction');
$data['name'] = $this->config->get('config_name');
if (is_file(DIR_IMAGE . $this->config->get('config_logo'))) {
$data['logo'] = $server . 'image/' . $this->config->get('config_logo');
} else {
$data['logo'] = '';
}
$this->load->language('common/header');
$data['text_home'] = $this->language->get('text_home');
// Wishlist
if ($this->customer->isLogged()) {
$this->load->model('account/wishlist');
$data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), $this->model_account_wishlist->getTotalWishlist());
} else {
$data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));
}
$data['text_shopping_cart'] = $this->language->get('text_shopping_cart');
$data['text_logged'] = sprintf($this->language->get('text_logged'), $this->url->link('account/account', '', true), $this->customer->getFirstName(), $this->url->link('account/logout', '', true));
$data['text_account'] = $this->language->get('text_account');
$data['text_register'] = $this->language->get('text_register');
$data['text_login'] = $this->language->get('text_login');
$data['text_order'] = $this->language->get('text_order');
$data['text_transaction'] = $this->language->get('text_transaction');
$data['text_download'] = $this->language->get('text_download');
$data['text_logout'] = $this->language->get('text_logout');
$data['text_checkout'] = $this->language->get('text_checkout');
$data['text_category'] = $this->language->get('text_category');
$data['text_all'] = $this->language->get('text_all');
$data['home'] = $this->url->link('common/home');
$data['wishlist'] = $this->url->link('account/wishlist', '', true);
$data['logged'] = $this->customer->isLogged();
$data['account'] = $this->url->link('account/account', '', true);
$data['register'] = $this->url->link('account/register', '', true);
$data['login'] = $this->url->link('account/login', '', true);
$data['order'] = $this->url->link('account/order', '', true);
$data['transaction'] = $this->url->link('account/transaction', '', true);
$data['download'] = $this->url->link('account/download', '', true);
$data['logout'] = $this->url->link('account/logout', '', true);
$data['shopping_cart'] = $this->url->link('checkout/cart');
$data['checkout'] = $this->url->link('checkout/checkout', '', true);
$data['contact'] = $this->url->link('information/contact');
$data['telephone'] = $this->config->get('config_telephone');
// Menu
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
foreach ($categories as $category) {
if ($category['top']) {
// Level 2
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
$filter_data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);
$children_data[] = array(
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}
// Level 1
$data['categories'][] = array(
'name' => $category['name'],
'children' => $children_data,
'column' => $category['column'] ? $category['column'] : 1,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
}
$data['language'] = $this->load->controller('common/language');
$data['currency'] = $this->load->controller('common/currency');
$data['search'] = $this->load->controller('common/search');
$data['cart'] = $this->load->controller('common/cart');
// For page specific css
if (isset($this->request->get['route'])) {
if (isset($this->request->get['product_id'])) {
$class = '-' . $this->request->get['product_id'];
} elseif (isset($this->request->get['path'])) {
$class = '-' . $this->request->get['path'];
} elseif (isset($this->request->get['manufacturer_id'])) {
$class = '-' . $this->request->get['manufacturer_id'];
} elseif (isset($this->request->get['information_id'])) {
$class = '-' . $this->request->get['information_id'];
} else {
$class = '';
}
$data['class'] = str_replace('/', '-', $this->request->get['route']) . $class;
} else {
$data['class'] = 'common-home';
}
//CUSTOM THEME VARIABLES BEGIN
$data['some_var'] = 'some_value';
//CUSTOM THEME VARIABLES END
return $this->load->view('common/header', $data);
}
}
I need to see your controller to get the full picture and then i will give you the full answer, but take a look on your controller and make sure that you bind your data like the sample below:
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/header.tpl')) {
return $this->load->view($this->config->get('config_template') . '/template/common/header.tpl', $data);
} else {
return $this->load->view('default/template/common/header.tpl', $data);
}
Thanks
I finally found the solution of my problem, but I am not sure if it is the right way to do this. I found that I need to make changes in system/storage/modification/catalog/controller/common/header‌​.php. It seems like after installing some extension via Vqmod the controller file have been copied to this folder. If I add my variables there then I can access them without any issues.

Zend Mail and encodings, content-transfer etc. - Unified?

Is there any class in the Zend Framework that allows me to easily read emails?
The Zend_Mail class does allow me to easy get headers, subject and the content body. But transferring everything to UTF-8 and human-readable format is still a pain.
Or am I doing something wrong? As far as I can tell, Zend Framework does not allow me to easily get UTF-8 strings that I can just use, I still have to do some post-processing. Right?
The key thing is that you need to iterate over the parts within the Message and find the text. Once you have it, then you can use quoted_printable_decode to get the text itself in a useful way.
This is some rough and ready code that reads IMAP email boxes with Zend_Mail:
<?php
$mail = new Zend_Mail_Storage_Imap(array(
'host' => EMAIL_ACCOUNT_HOST,
'user' => EMAIL_ACCOUNT_USERNAME,
'password' => EMAIL_ACCOUNT_PASSWORD,
));
echo (int)$mail->countMessages() . " messages found\n";
foreach ($mail as $message) {
$from = $message->getHeader('from');
$subject = trim($message->subject);
$to = trim($message->to);
$body = getBody($message);
// do something with message here
}
function getBody(Zend_Mail_Message $message)
{
// find body
$part = $message;
$isText = true;
while ($part->isMultipart()) {
$foundPart = false;
$iterator = new RecursiveIteratorIterator($message);
foreach ($iterator as $part) {
// this detection code is a bit rough and ready!
if (!$foundPart) {
if (strtok($part->contentType, ';') == 'text/html') {
$foundPart = $part;
$isText = false;
break;
} else if (strtok($part->contentType, ';') == 'text/plain') {
$foundPart = $part;
$isText = true;
break;
}
}
}
if($foundPart) {
$part = $foundPart;
break;
}
}
$body = quoted_printable_decode($part->getContent());
}

Zend_Form_Element fails when i addElements

I have been having trouble adding a hidden zend form element.
when i invoke addElements the form fails and prints the following error to the page.
but only when i try and add $formContactID and $formCustomerID.
Fatal error: Call to a member function getOrder() on a non-object in /home/coder123/public_html/wms2/library/Zend/Form.php on line 3291
My code is as follows.
private function buildForm()
{
$Description = "";
$FirstName = "";
$LastName = "";
$ContactNumber = "";
$Fax = "";
$Position = "";
$Default = "";
$custAddressID = "";
$CustomerID = "";
$Email = "";
$ContactID = "";
if($this->contactDetails != null)
{
$Description = $this->contactDetails['Description'];
$CustomerID = $this->contactDetails['CustomerID'];
$FirstName = $this->contactDetails['FirstName'];
$LastName = $this->contactDetails['LastName'];
$ContactNumber = $this->contactDetails['ContactNumber'];
$Position = $this->contactDetails['Position'];
$Fax = $this->contactDetails['Fax'];
$Email = $this->contactDetails['Email'];
$Default = $this->contactDetails['Default'];
$custAddressID = $this->contactDetails['custAddressID'];
$ContactID = $this->contactDetails['custContactID'];
}
$formfirstname = new Zend_Form_Element_Text('FirstName');
$formfirstname->setValue($FirstName)->setLabel('First Name:')->setRequired();
$formlastname = new Zend_Form_Element_Text('LastName');
$formlastname->setLabel('Last Name:')->setValue($LastName)->setRequired();
$formPhone = new Zend_Form_Element_Text('ContactNumber');
$formPhone->setLabel('Phone Number:')->setValue($ContactNumber)->setRequired();
$formFax = new Zend_Form_Element_Text('FaxNumber');
$formFax->setLabel('Fax Number:')->setValue($Fax);
$FormPosition = new Zend_Form_Element_Text('Position');
$FormPosition->setLabel('Contacts Position:')->setValue($Position);
$FormDescription = new Zend_Form_Element_Text('Description');
$FormDescription->setLabel('Short Description:')->setValue($Description)->setRequired();
$formEmail = new Zend_Form_Element_Text('Email');
$formEmail->setLabel('Email Address:')->setValue($Email);
$FormDefault = new Zend_Form_Element_Checkbox('Default');
$FormDefault->setValue('Default')->setLabel('Set as defualt contact for this business:');
if($Default == 'Default')
{
$FormDefault->setChecked(true);
}
$formCustomerID = new Zend_Form_Element_Hidden('customerID');
$formCustomerID->setValue($customerID);
if($this->contactID != null)
{
$formContactID = new Zend_Form_Element_Hidden('ContactID');
$formContactID->setValue($this->contactID);
}
// FORM SELECT
$formSelectAddress = new Zend_Form_Element_Select('custAddress');
$pos = 0;
while($pos < count($this->customerAddressArray))
{
$formSelectAddress->addMultiOption($this->customerAddressArray[$pos]['custAddressID'], $this->customerAddressArray[$pos]['Description']);
$pos++;
}
$formSelectAddress->setValue(array($this->contactDetails['custAddressID']));
$formSelectAddress->setRequired()->setLabel('Default Address For this Contact:');
// END FORM SELECT
$this->setMethod('post');
$this->setName('FormCustomerEdit');
$formSubmit = new Zend_Form_Element_Submit('ContactSubmit');
$formSubmit->setLabel('Save Contact');
$this->setName('CustomerContactForm');
$this->setMethod('post');
$this->addElements(array($FormDescription, $formfirstname, $formlastname,
$FormPosition, $formPhone, $formFax, $FormDefault,
$formEmail, $formSelectAddress, $formContactID, $formCustomerID, $formSubmit));
$this->addElements(array($formSubmit));
}
Maybe you've already fixed, but just in case.
I was having the same issue and the problem was the name of certain attributes within the form. In your case you have:
if($this->contactID != null){
$formContactID = new Zend_Form_Element_Hidden('ContactID');
$formContactID->setValue($this->contactID);
}
In the moment that you have added $formContactID to the form a new internal attribute has been created for the form object, this being 'ContactID'. So now we have $this->ContactID and $this->contactID.
According to PHP standards this shouldn't be a problem because associative arrays keys and objects attribute names are case sensitive but I just wanted to use your code to illustrate the behaviour of Zend Form.
Revise the rest of the code in your form to see that you are not overriding any Zend Element. Sorry for the guess but since you didn't post all the code for the form file it's a bit more difficult to debug.
Thanks and I hope it helps.
I think the problem is on $this->addElements when $formContactID is missing because of if($this->contactID != null) rule.
You can update your code to fix the problem:
.....
$this->addElements(array($FormDescription, $formfirstname, $formlastname,
$FormPosition, $formPhone, $formFax, $FormDefault,
$formEmail, $formSelectAddress, $formCustomerID, $formSubmit));
if(isset($formContactID)) {
$this->addElements(array($formContactID));
}
......