/**
  * This is function sends mail using the parameters that are in the post request.
  *
  */
 private function send_mail()
 {
     $context = context_course::instance($this->courseid);
     if (!has_capability('block/course_message:sendmail', $context)) {
         echo json_encode(array('result' => false, 'text' => get_string('usercannotsendmail', BLOCK_CM_LANG_TABLE)));
         return;
     }
     // Grab the posted paramaters - slows things down, but allows for easier unit testing.
     $params = array("subject" => optional_param('subject', "no subject", PARAM_TEXT), "message" => required_param('message', PARAM_CLEANHTML), "parentmessage" => optional_param('parent', 0, PARAM_INT), "replytype" => optional_param('replytype', 'single', PARAM_TEXT), "draftid" => optional_param(BLOCK_CM_FILE_AREA_NAME, 0, PARAM_INT));
     /* If the optional param does not find mailTo (it is not sent on inline replies), then it will
      * already be setup as an array and not need to be JSON decoded. */
     $temp = optional_param('mailTo', array("1"), PARAM_TEXT);
     $mailto = !is_array($temp) ? json_decode($temp) : $temp;
     // The carboncopy field is actually optional: json decode it if it exists, set to null otherwise.
     $temp = optional_param('cc', null, PARAM_TEXT);
     $carboncopy = $temp === null ? $temp : json_decode($temp);
     $mailrecord = new mail_record(0);
     $mailrecord->set_send_params($params, $mailto, $carboncopy);
     $mailrecord->send_mail($this->courseid);
 }
 /**
  * This function creates the mail to be used for testing.  Only 5 mail messages
  * are created -> {one generic mail, one mail with an attachment, and three
  * mail as part of a thread}.
  *
  */
 protected function create_test_mail()
 {
     global $DB;
     ob_start();
     $this->setUser($this->craig);
     // Send first test mail -> generic.
     $params = array("subject" => 'Test Mail 1', "message" => 'Generic message goes here.', "parentmessage" => 0, "replytype" => 'single', "draftid" => 0);
     $mailto = array("{$this->friend->id}");
     $mailrecord = new mail_record(0, false);
     $mailrecord->set_send_params($params, $mailto);
     $mailrecord->send_mail($this->testcourseid);
     $this->mailids[] = $this->get_mail_id($params['message']);
     // Second test mail: used for deletion test.
     $params = array("subject" => 'Delete Mail 1', "message" => 'This mail will be deleted.', "parentmessage" => 0, "replytype" => 'single', "draftid" => 0);
     $mailrecord = new mail_record(0, false);
     $mailrecord->set_send_params($params, $mailto);
     $mailrecord->send_mail($this->testcourseid);
     $this->mailids[] = $this->get_mail_id($params['message']);
     // Here I'm spoofing adding an attachment afterwards: I think this could be condensed to 1 step.
     $query = "SELECT * FROM {course_message_mails} WHERE message = '{$params['message']}'";
     $record = $DB->get_record_sql($query);
     $record->attachment = self::FILEID;
     $DB->update_record('course_message_mails', $record);
     // Now create a thread of three.
     $params = array("subject" => 'Threaded Message Test', "message" => 'This is the parent for the thread.', "parentmessage" => 0, "replytype" => 'single', "draftid" => 0);
     $mailrecord = new mail_record(0, false);
     $mailrecord->set_send_params($params, $mailto);
     $mailrecord->send_mail($this->testcourseid);
     $this->mailids[] = $this->get_mail_id($params['message']);
     // Switch to friend and reply.
     $this->setUser($this->friend);
     $params = array("subject" => 'Threaded Message Test', "message" => 'This is the first reply.', "parentmessage" => $this->mailids[2], "replytype" => 'single', "draftid" => 0);
     $mailto = array("1");
     $mailrecord = new mail_record(0, false);
     $mailrecord->set_send_params($params, $mailto);
     $mailrecord->send_mail($this->testcourseid);
     $this->mailids[] = $this->get_mail_id($params['message']);
     // Now reply back.
     $this->setUser($this->craig);
     $params = array("subject" => 'Threaded Message Test', "message" => 'This is the second reply.', "parentmessage" => $this->mailids[3], "replytype" => 'single', "draftid" => 0);
     $mailto = array("1");
     $mailrecord = new mail_record(0, false);
     $mailrecord->set_send_params($params, $mailto);
     $mailrecord->send_mail($this->testcourseid);
     $this->mailids[] = $this->get_mail_id($params['message']);
     ob_end_clean();
 }