Loading

Send email to admin once task is completed - code inside :)

General Discussion around Collabtive

Send email to admin once task is completed - code inside :)

Postby BaneD » 14.01.2016, 11:15

Hi everyone, :)

I noticed that someone could close a task without any notice to the administrator, making it very easy to miss something being done and creating new tasks, or following back with checks and similar.

In my case, I like to see what was done, when and if needed check how it was done - or if there are any issues with the same.

Now, since roles can be named in different ways and still some people be admins looking for "admin" as a role is not something I went with, instead, I went with the option in the roles to delete the task. In my opinion no matter how you name it, the admin of any kind is the only person that should be able to do that and as such they get the email (in my code) of any closed task.

This is the code that I have added to my managetask.php file:

Code: Select all
//Requires:
// taskcompletedsubject = "One of the tasks has been marked as closed"
// taskcompletedtext = "One of the tasks has been marked as closed, and you can review the same on the following link:"
// To be added to line 307 in default managetask.php right after "if ($task->close($tid)) {" so that we run this if the task is actually closed and before all else
//  this can be found within elseif ($action == "close") section


//Lets check if emails are turned on or not
if ($settings["mailnotify"])
{
  $currentTask = $task->getTask($tid);
  $currentProject = new project();

  // We need the list of all of the members of the current project
  $users = $currentProject->getProjectMembers($id, $currentProject->countMembers($id));

  //Lets strole through them..
  foreach($users as $member)
  {

    //Lets check if this is admin account or not and continue further only if it is
    // Admin account - in my opinion - is each account that is given delete privilege of a task, since I do not see it as a good practice that anyone could delete the same
    if ( $member['role']['tasks']['del'] == 1 )
    {
      //Admins should get the email, and in my opinion admins would need to have their emails set, but just in case, we check this and don't send it to the one that closed it (if also admin)
      if (!empty($member["email"]) && $userid != $member["ID"])
      {

        //We will need user language
        $userlang = readLangfile($member['locale']);

        //now adding a temp fix without modifying language files:
        $userlang["taskcompletedsubject"] = "One of the tasks has been marked as closed";
        $userlang["taskcompletedtext"] = "One of the tasks has been marked as closed:";

        //Lets add the title and the text of the task
        $title = $currentTask['title'];
        $text = $currentTask['text'];

        // send email
        $subject = $userlang["taskcompletedsubject"] . ' (' . $userlang['by'] . ' ' . $username . ')';

        $mailcontent = $userlang["hello"] . ",<br /><br/>" .
                                $userlang["taskcompletedtext"] . ' (' . $userlang['by'] . ' ' . $username . ')' .
                                "<h3><a href = \"" . $url . "managetask.php?action=showtask&id=$id&tid=$tid\">$title</a></h3>".
                                $text;

        $themail = new emailer($settings);

        $themail->send_mail($member["email"], $subject , $mailcontent);
      }
    }
  }
}


As mentioned at the beginning of the code, there are few requirements to this:
1.taskcompletedsubject and taskcompletedtext do not exist at this time in language files, so my mod creates these within the code (I know that it is not the prettiest option, but it allows for a quick check of how it works). You can always create the same within your own language file.
2. the code above should be copy -> pasted into the managetask.php by adding a new line after line 307. If you have any plugins or custom code applied, your exact lines could be moved a bit, in which case you should find this code: if ($task->close($tid)) { in the elseif ($action == "close") section and apply the change right after it (without removing any other code in there).

Well I hope that this helps someone and I will post few other mods I made shortly. :)
BaneD
 
Posts: 16
Joined: 13.01.2016, 20:26

Re: Send email to admin once task is completed - code inside

Postby GordonPremap » 26.01.2016, 11:30

Hi,

Thanks for the code working perfectly.

I would like to include the project title to the email that gets sent to the admin

How would I include it into the email

Thank you
GordonPremap
 
Posts: 2
Joined: 26.01.2016, 11:26

Re: Send email to admin once task is completed - code inside

Postby BaneD » 27.01.2016, 01:48

Thanks, glad to hear that Gordon :)

To get project name, you can use "pname" key

Code: Select all
$projectName = $currentTask['pname'];


If you want to get the current list you can use "list" in the same manner:
Code: Select all
$taskList = $currentTask['list'];


For me another interesting detail is also how many days the task had on it (have left or overdue for):

Code: Select all
$daysLeft = $currentTask['daysleft'];


Then you just need to modify mailcontent:

Code: Select all
$mailcontent = $userlang["hello"] . ",<br /><br/>" .
                                $userlang["taskcompletedtext"] . ' (' . $userlang['by'] . ' ' . $username . ')' .
                                "<h3><a href = \"" . $url . "managetask.php?action=showtask&id=$id&tid=$tid\">$title</a></h3>".
                                $text;


to include the $projectName in the place you prefer.

Hope this helps :)
BaneD
 
Posts: 16
Joined: 13.01.2016, 20:26

Re: Send email to admin once task is completed - code inside

Postby GordonPremap » 28.01.2016, 08:56

Thanks very much

Exactly the variables I was looking for.

Appreciate it,

Can I buy you a coffee or beer ? :D
GordonPremap
 
Posts: 2
Joined: 26.01.2016, 11:26

Re: Send email to admin once task is completed - code inside

Postby BaneD » 28.01.2016, 14:16

You are welcome :)

Never heard of anyone refusing a free beer :D

Cheers,
Bane
BaneD
 
Posts: 16
Joined: 13.01.2016, 20:26


Return to General

Who is online

Users browsing this forum: No registered users

cron