| 1 | 
           efrain | 
           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 | 
           namespace core_communication\task;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           use core\task\adhoc_task;
  | 
        
        
            | 
            | 
           20 | 
           use core_communication\processor;
  | 
        
        
            | 
            | 
           21 | 
              | 
        
        
            | 
            | 
           22 | 
           /**
  | 
        
        
            | 
            | 
           23 | 
            * Class create_and_configure_room_task to add a task to create a room and execute the task to action the creation.
  | 
        
        
            | 
            | 
           24 | 
            *
  | 
        
        
            | 
            | 
           25 | 
            * this task will be queued by the communication api and will use the communication handler api to action the creation.
  | 
        
        
            | 
            | 
           26 | 
            *
  | 
        
        
            | 
            | 
           27 | 
            * @package    core_communication
  | 
        
        
            | 
            | 
           28 | 
            * @copyright  2023 Safat Shahin <safat.shahin@moodle.com>
  | 
        
        
            | 
            | 
           29 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           30 | 
            */
  | 
        
        
            | 
            | 
           31 | 
           class create_and_configure_room_task extends adhoc_task {
  | 
        
        
            | 
            | 
           32 | 
               public function execute() {
  | 
        
        
            | 
            | 
           33 | 
                   $data = $this->get_custom_data();
  | 
        
        
            | 
            | 
           34 | 
              | 
        
        
            | 
            | 
           35 | 
                   // Call the communication api to action the operation.
  | 
        
        
            | 
            | 
           36 | 
                   $communication = processor::load_by_id($data->id);
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
                   if ($communication === null) {
  | 
        
        
            | 
            | 
           39 | 
                       mtrace("Skipping room creation because the instance does not exist");
  | 
        
        
            | 
            | 
           40 | 
                       return;
  | 
        
        
            | 
            | 
           41 | 
                   }
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
                   if (!$communication->is_instance_active()) {
  | 
        
        
            | 
            | 
           44 | 
                       mtrace("Skipping room creation because the instance is not active");
  | 
        
        
            | 
            | 
           45 | 
                       return;
  | 
        
        
            | 
            | 
           46 | 
                   }
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
                   // If the room is created successfully, add members to the room if supported by the provider.
  | 
        
        
            | 
            | 
           49 | 
                   if ($communication->get_room_provider()->create_chat_room() && $communication->supports_user_features()) {
  | 
        
        
            | 
            | 
           50 | 
                       add_members_to_room_task::queue(
  | 
        
        
            | 
            | 
           51 | 
                           $communication
  | 
        
        
            | 
            | 
           52 | 
                       );
  | 
        
        
            | 
            | 
           53 | 
                   }
  | 
        
        
            | 
            | 
           54 | 
               }
  | 
        
        
            | 
            | 
           55 | 
              | 
        
        
            | 
            | 
           56 | 
               /**
  | 
        
        
            | 
            | 
           57 | 
                * Queue the task for the next run.
  | 
        
        
            | 
            | 
           58 | 
                *
  | 
        
        
            | 
            | 
           59 | 
                * @param processor $communication The communication processor to perform the action on
  | 
        
        
            | 
            | 
           60 | 
                */
  | 
        
        
            | 
            | 
           61 | 
               public static function queue(
  | 
        
        
            | 
            | 
           62 | 
                   processor $communication,
  | 
        
        
            | 
            | 
           63 | 
               ): void {
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
            | 
            | 
           65 | 
                   // Add ad-hoc task to update the provider room.
  | 
        
        
            | 
            | 
           66 | 
                   $task = new self();
  | 
        
        
            | 
            | 
           67 | 
                   $task->set_custom_data([
  | 
        
        
            | 
            | 
           68 | 
                       'id' => $communication->get_id(),
  | 
        
        
            | 
            | 
           69 | 
                   ]);
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
                   // Queue the task for the next run.
  | 
        
        
            | 
            | 
           72 | 
                   \core\task\manager::queue_adhoc_task($task);
  | 
        
        
            | 
            | 
           73 | 
               }
  | 
        
        
            | 
            | 
           74 | 
           }
  |