Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 402 and 403]

   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\external;
  18  
  19  use context_course;
  20  use core\moodlenet\utilities;
  21  use core\oauth2\api;
  22  use core_external\external_api;
  23  use core_external\external_function_parameters;
  24  use core_external\external_single_structure;
  25  use core_external\external_value;
  26  use core_external\external_warnings;
  27  
  28  /**
  29   * The external API to het the activity information for MoodleNet sharing.
  30   *
  31   * @package    core
  32   * @copyright  2023 Huong Nguyen <huongnv13@gmail.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class moodlenet_get_share_info_activity extends external_api {
  36  
  37      /**
  38       * Returns description of parameters.
  39       *
  40       * @return external_function_parameters
  41       * @since Moodle 4.2
  42       */
  43      public static function execute_parameters(): external_function_parameters {
  44          return new external_function_parameters([
  45              'cmid' => new external_value(PARAM_INT, 'The cmid of the activity', VALUE_REQUIRED),
  46          ]);
  47      }
  48  
  49      /**
  50       * External function to get the activity information.
  51       *
  52       * @param int $cmid The course module id.
  53       * @return array
  54       * @since Moodle 4.2
  55       */
  56      public static function execute(int $cmid): array {
  57          global $CFG, $USER;
  58  
  59          [
  60              'cmid' => $cmid
  61          ] = self::validate_parameters(self::execute_parameters(), [
  62              'cmid' => $cmid
  63          ]);
  64  
  65          // Get course module.
  66          $coursemodule = get_coursemodule_from_id(false, $cmid);
  67          if (!$coursemodule) {
  68              return self::return_errors($cmid, 'errorgettingactivityinformation', get_string('invalidcoursemodule', 'error'));
  69          }
  70  
  71          // Get course.
  72          $course = get_course($coursemodule->course);
  73  
  74          // Check capability.
  75          $coursecontext = context_course::instance($course->id);
  76          $usercanshare = utilities::can_user_share($coursecontext, $USER->id);
  77          if (!$usercanshare) {
  78              return self::return_errors($cmid, 'errorpermission',
  79                  get_string('nopermissions', 'error', get_string('moodlenet:sharetomoodlenet', 'moodle')));
  80          }
  81  
  82          $warnings = [];
  83          $supporturl = '';
  84          $issuerid = get_config('moodlenet', 'oauthservice');
  85  
  86          if (empty($issuerid)) {
  87              return self::return_errors(0, 'errorissuernotset', get_string('moodlenet:issuerisnotset', 'moodle'));
  88          }
  89  
  90          if ($CFG->supportavailability && $CFG->supportavailability != CONTACT_SUPPORT_DISABLED) {
  91              if (!empty($CFG->supportpage)) {
  92                  $supporturl = $CFG->supportpage;
  93              } else {
  94                  $supporturl = $CFG->wwwroot . '/user/contactsitesupport.php';
  95              }
  96          }
  97  
  98          // Get the issuer.
  99          $issuer = api::get_issuer($issuerid);
 100          // Validate the issuer and check if it is enabled or not.
 101          if (!utilities::is_valid_instance($issuer)) {
 102              return self::return_errors($issuerid, 'errorissuernotenabled', get_string('moodlenet:issuerisnotenabled', 'moodle'));
 103          }
 104  
 105          return [
 106              'status' => true,
 107              'name' => $coursemodule->name,
 108              'type' => get_string('modulename', $coursemodule->modname),
 109              'server' => $issuer->get_display_name(),
 110              'supportpageurl' => $supporturl,
 111              'issuerid' => $issuerid,
 112              'warnings' => $warnings
 113          ];
 114      }
 115  
 116      /**
 117       * Describes the data returned from the external function.
 118       *
 119       * @return external_single_structure
 120       * @since Moodle 4.2
 121       */
 122      public static function execute_returns(): external_single_structure {
 123          return new external_single_structure([
 124              'name' => new external_value(PARAM_TEXT, 'Activity name'),
 125              'type' => new external_value(PARAM_TEXT, 'Activity type'),
 126              'server' => new external_value(PARAM_TEXT, 'MoodleNet server'),
 127              'supportpageurl' => new external_value(PARAM_URL, 'Support page URL'),
 128              'issuerid' => new external_value(PARAM_INT, 'MoodleNet issuer id'),
 129              'status' => new external_value(PARAM_BOOL, 'status: true if success'),
 130              'warnings' => new external_warnings()
 131          ]);
 132      }
 133  
 134      /**
 135       * Handle return error.
 136       *
 137       * @param int $itemid Item id.
 138       * @param string $warningcode Warning code.
 139       * @param string $message Message.
 140       * @param int $issuerid Issuer id.
 141       * @return array
 142       */
 143      protected static function return_errors(int $itemid, string $warningcode, string $message, int $issuerid = 0): array {
 144          $warnings[] = [
 145              'item' => $itemid,
 146              'warningcode' => $warningcode,
 147              'message' => $message
 148          ];
 149  
 150          return [
 151              'status' => false,
 152              'name' => '',
 153              'type' => '',
 154              'server' => '',
 155              'supportpageurl' => '',
 156              'issuerid' => $issuerid,
 157              'warnings' => $warnings
 158          ];
 159      }
 160  }