Send email to users that checked 'yes' - php and modx - php

I have a database of 'user comments' that holds the comment, the userId, true/false for if they want to be emailed.
My issue is that if a user comments on a post 5 times, they are in that database 5 times and thus, they get 5 of the same emails. Here is my setup:
Find users in the database that want to be emailed about this comment
$ab = $modx->newQuery('Comments');
$ab->leftJoin('modUserProfile','Profile',array('Profile.internalKey = Comments.author'));
$ab->leftJoin('Bulletin','Bulletin',array('Bulletin.id = Comments.bulletin_id'));
$ab->where(array('Comments.bulletin_id' => $bullId));
$ab->where(array('Comments.email' => 1));
$ab->select(array('Comments.*','Profile.fullname as fullname' , 'Profile.email as email' , 'Bulletin.title as title'));
$emailList = $modx->getCollection('Comments',$ab);
Now, email them
foreach ($emailList as $e){
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->address('to',$e->email);
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_SUBJECT,'A new comment has been posted!');}
What should I use to email the users so that they only get 1 email regardless of the amount of comments they have made? Please let me know if the above is not clear enough. Thank you!!

I think you can add a groupby....
$ab->groupby('email');
after your select

Related

Moodle Enrolment Email for Teachers - Add Student Email Address

I'm running Moodle, and have a teacher who receives a notification email anytime a student enrolls in a course (via PayPal enrollment).
The email contents come from lang/en/enrol.php:
$string['enrolmentnewuser'] = '{$a->user} has enrolled in course "{$a->course}";
What I'm trying to do is include the student's email address in this, for the teacher to reference.
Here's what I've tried:
$string['enrolmentnewuser'] = '{$a->user} has enrolled in course "{$a->course}". Student\'s email address: {$a->email}';
To make this $a->email variable available, I went into the function that gets the email contents: enrol/flatfile/lib.php::process_records()
Inside process_records(), I added the following:
$a->email = $user->email;
I put this directly after these lines:
$a = new stdClass();
$a->course = format_string($course->fullname, true, array('context' => $context));
$a->user = fullname($user);
and before this line, which gets the email contents:
$eventdata->fullmessage = get_string('enrolmentnewuser', 'enrol', $a);
I would think that the $a object would now contain my new 'email' property, which would be accessible in the 'enrolmentnewuser' string.
I've cleared the cache after my updates.
Unfortunately, this is the email the teacher is now receiving:
Student has enrolled in course "Test Course". Student's email address: {$a->email}
It looks like the $a->email is not swapping the actual property into the message. For the life of me, I haven't been able to figure out why it's not working. It's been difficult to debug, as I have to keep enrolling and un-enrolling in the test course which is frustrating.
Does anyone have any insight into why this is printing the $a->email property name literally, instead of the actual property?
In your case, you need to add the below line to enrol/paypal/ipn.php file also.
$a->email = $user->email;
put this directly after these lines:
$a->course = format_string($course->fullname, true, array('context' => $coursecontext));
$a->user = fullname($user);

Codeigniter Unilevel MLM earning Distribution

I am here to have some help from you.
I am making a Unilevel MLM using Codeigniter
and now I can sucessfully add new member
But the problem is I need to distribute the earnings to other level
after a new member is successfully Added
See pic below:
Distribution of earnings
I need to distribute like the image above.
I hope you can help me with this guys.
Okay, I have a solution for you. The process i used is based on my understanding of the question.
So this is it, first i checked for a registration post, if a post request is made, i use the referral id from the post to fetch the number of registrations tied to that referral id that has not been given awarded the 100 earning. If the count of the result of this query is equal to 4, i loop through all of them and give them the earning of 100 and update their paid status to reflect that they have been paid then i insert the record, else i just insert the record.
So too much text, lets see the code
//this is the controller code
//first check for post of registration
if($_POST){
//kindly do your form validation here
$register = array(
"name" => $this->input->post('name'),
"refid" => $this->input->post('refID')
);
//during post, get the referral id from the post
$refID = $this->input->post('refID');
//before registering, use referral id to get the referred that have not been given earnings yet
$thereffered = $this->referral_m->getReferred($refID);
//check for the number of registration
if(count($thereffered) == 4){
//then get their ids and give them their earnings and update them to paid
foreach($thereffered as $referred){
$earnings = array(
"userID" => $referred->id,
"amount" => 100
);
$paid = array(
"paid" => 1
);
//give earning
$this->referral_m->giveEarning($earnings); //this adds the user id to earning table and give it an amount of 100
$this->referral_m->updateUser($paid, $referred->id); //this updates the user with the paid status
}
//then proceed to register the new record
$this->referral_m->register($register);
}else{
//register the new record
$this->referral_m->register($register);
}
//redirect after registration
redirect();
}else{
//load view here
}
This is how the model looks like
function getReferred($refID){
return $this->db->get_where('referral', array("refid" => $refID, "paid" => '0'))->result();
}
function giveEarning($record){
$this->db->insert('earnings', $record);
}
function register($array){
$this->db->insert('referral', $array);
}
function updateUser($array, $id){
$this->db->where('id', $id);
$this->db->update('referral', $array);
}
From the model, you would discover that i created 2 database tables, I assume you already have those tables created, just use the logic to update your code. If you find any difficulty, kindly comment lets sort it out

how to validate email exists in database using luman while create new user?

how to validate email exists in database using luman while create new user?.
my registration controller code
$borrower = borrowerRegistration::create($request->all());
$last_borrower_id=DB::getPdo()->lastInsertId();
$store_borrower_array=array();
$store_borrower_array["borrower_id"]=$last_borrower_id;
$borrower_result = array('status' => 'true','message' =>'The First step borrower registration successfully.','content'=>array('registration'=>$store_borrower_array));
return json_encode($borrower_result);
please give a valuable suggestions.
You can try this way. here User is your Model (I am assuming)
if (User::where('email', '=', Input::get('email'))->exists()) {
// user found
}
Replace Input::get('email') to your email address from where you are getting and storing it.

Who Created Questionnaire (which User)?

I am new to moodle ,currently i am working on questionnaire module, in which we create questionnaire for courses(created by teacher , which are viewed by student on course detailed page there they will answer ) . now i want to know which user created that questionnaire (i.e userid ). I have been searching for while but didnt found any answer.
If the questionnaire table has a createdby field, then you can use that, otherwise, you'll have to do a search through the logs table like this:
// You'll need to get the right coursemoduleid first from the course_modules table
$cmid = 27;
$conditions = array('module' => 'questionnaire',
'coursemoduleid' => $cmid,
'action' => 'add');
$creationrecord = $DB->get_record('log', $conditions);
$creator = $creationrecord->userid;

send email on form submit only when a field has changed

I have a form to edit a job, a job has a status column which can be 1, 2 or 3
<?php echo $this->Form->input('status', array('label' => '', 'options' => $status)); ?>
When i submit the form i want to check if the value of status is equal to 3, if it is then i want to send an email. BUT i dont want to send the email if the value was 3 already.
Is there an easy way in cakephp to check the previous value to the new value etc?
Thanks.
No need to mess with sessions, or indeed set the value beforehand.
Basically when you edit the record, you get the current records status value from the table. If it's already 3, we do not want to send an email, so set a boolean.
Update the record as required.
If the status was not 3, and the new status is, send the email.
I haven't filled in the whole method; but you should get the idea:
$send_email = true;
$current_status = $this->Job->field('status');
if($current_status==3) {
$send_email = false;
}
// save the record
if($send_email==true && $this->data['Job']['status']==3) {
//send the email
}
Read the existing record from the database just before you save the new one. You'd then have something to compare the new data against.
Or, store the status in the session and compare the new data against it.
So when you read the record from the database, save the status in session:
$this->data = $this->Job->read(null, $id);
$this->Session->write('JobStatus.'.$this->data['Job']['id'], $this->data['Job']['status']);
When the Job is edited, you can check the new value against the old one:
if (!empty($this->data)) {
if ($this->data['Job']['status'] == 3 && $this->Session->read('JobStatus.'.$this->data['Job']['id']) != 3) {
/**
* Send email
*/
}
}
You can set a hidden field with the source value and check it's value against the submitted one.
<?php echo $this->Form->input('old_status', array('type' => 'hidden', 'default' => $old_status)); ?>

Categories