Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Contains class used to prepare the contacts for display.
  19   *
  20   * TODO: This file should be removed once the related web services go through final deprecation.
  21   * Followup: MDL-63261
  22   *
  23   * @package   core_message
  24   * @copyright 2016 Mark Nelson <markn@moodle.com>
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  namespace core_message\output\messagearea;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  use renderable;
  33  use templatable;
  34  
  35  /**
  36   * Class to prepare the contacts for display.
  37   *
  38   * @package   core_message
  39   * @copyright 2016 Mark Nelson <markn@moodle.com>
  40   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class contacts implements templatable, renderable {
  43  
  44      /**
  45       * @var int The id of the user that has been selected.
  46       */
  47      public $contactuserid;
  48  
  49      /**
  50       * @var array The contacts.
  51       */
  52      public $contacts;
  53  
  54      /**
  55       * Constructor.
  56       *
  57       * @param int|null $contactuserid The id of the user that has been selected
  58       * @param array $contacts
  59       */
  60      public function __construct($contactuserid, $contacts) {
  61          $this->contactuserid = $contactuserid;
  62          $this->contacts = $contacts;
  63      }
  64  
  65      public function export_for_template(\renderer_base $output) {
  66          $data = new \stdClass();
  67          $data->contacts = array();
  68          $userids = array();
  69          foreach ($this->contacts as $contact) {
  70              $contact = new contact($contact);
  71              $contactdata = $contact->export_for_template($output);
  72              $userids[$contactdata->userid] = $contactdata->userid;
  73              // Check if the contact was selected.
  74              if ($this->contactuserid == $contactdata->userid) {
  75                  $contactdata->selected = true;
  76              }
  77              $data->contacts[] = $contactdata;
  78          }
  79          // Check if the other user is not part of the contacts. We may be sending a message to someone
  80          // we have not had a conversation with, so we want to add a new item to the contacts array.
  81          if ($this->contactuserid && !isset($userids[$this->contactuserid])) {
  82              $user = \core_user::get_user($this->contactuserid);
  83              // Set an empty message so that we know we are messaging the user, and not viewing their profile.
  84              $user->smallmessage = '';
  85              $user->useridfrom = $user->id;
  86              $contact = \core_message\helper::create_contact($user);
  87              $contact = new contact($contact);
  88              $contactdata = $contact->export_for_template($output);
  89              $contactdata->selected = true;
  90              // Put the contact at the front.
  91              array_unshift($data->contacts, $contactdata);
  92          }
  93  
  94          return $data;
  95      }
  96  }