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 401 and 402]

   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_course\external;
  18  
  19  use context_module;
  20  use core_external\external_description;
  21  use core_external\external_files;
  22  use core_external\external_format_value;
  23  use core_external\util as external_util;
  24  use core_external\external_value;
  25  
  26  /**
  27   * This class helps implement the get_..._by_courses web service that every activity should have.
  28   *
  29   * It has helper methods to add the standard course-module fields to the results, and the declaration of the return value.
  30   *
  31   * @package    core_course
  32   * @copyright  2022 The Open University
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  abstract class helper_for_get_mods_by_courses {
  36  
  37      /**
  38       * Add the value of all the standard fields to the results to be returned by the service.
  39       * This is designed to be used in the implementation of the get_..._by_courses web service methods.
  40       *
  41       * Note that $modinstance is also updated in-place.
  42       *
  43       * @param \stdClass $modinstance one of the objects returned from a call to {@see get_all_instances_in_courses()}.
  44       * @param string $component the plugin name, e.g. 'mod_book'.
  45       * @param string $capabilityforgroups capability to check before including group/visible/section info in the results.
  46       * @param string|null $capabilityforintro capability to check before including intro info in the results.
  47       *      null means always include (the default).
  48       * @return array with the containing all the values declared in {@see standard_coursemodule_elements_returns()}.
  49       */
  50      public static function standard_coursemodule_element_values(\stdClass $modinstance, string $component,
  51              string $capabilityforgroups = 'moodle/course:manageactivities', string $capabilityforintro = null): array {
  52          self::format_name_and_intro($modinstance, $component);
  53          $context = context_module::instance($modinstance->coursemodule);
  54  
  55          // First, we return information that any user can see in the web interface.
  56          $moddetails['id'] = $modinstance->id;
  57          $moddetails['coursemodule'] = $modinstance->coursemodule;
  58          $moddetails['course'] = $modinstance->course;
  59          $moddetails['name'] = $modinstance->name;
  60          $moddetails['lang'] = clean_param($modinstance->lang, PARAM_LANG);
  61          if (!$capabilityforintro || has_capability($capabilityforintro, $context)) {
  62              $moddetails['intro'] = $modinstance->intro;
  63              $moddetails['introformat'] = $modinstance->introformat;
  64              $moddetails['introfiles'] = $modinstance->introfiles;
  65          }
  66  
  67          // Now add information only available to people who can edit.
  68          if (has_capability($capabilityforgroups, $context)) {
  69              $moddetails['section'] = $modinstance->section;
  70              $moddetails['visible'] = $modinstance->visible;
  71              $moddetails['groupmode'] = $modinstance->groupmode;
  72              $moddetails['groupingid'] = $modinstance->groupingid;
  73          }
  74  
  75          return $moddetails;
  76      }
  77  
  78      /**
  79       * Format the module name an introduction ready to be exported to a web service.
  80       *
  81       * Note that $modinstance is updated in-place.
  82       *
  83       * @param \stdClass $modinstance one of the objects returned from a call to {@see get_all_instances_in_courses()}.
  84       * @param string $component the plugin name, e.g. 'mod_book'.
  85       */
  86      public static function format_name_and_intro(\stdClass $modinstance, string $component) {
  87          $context = context_module::instance($modinstance->coursemodule);
  88  
  89          $modinstance->name = \core_external\util::format_string($modinstance->name, $context);
  90  
  91          [$modinstance->intro, $modinstance->introformat] = \core_external\util::format_text(
  92                  $modinstance->intro, $modinstance->introformat, $context,
  93                  $component, 'intro', null, ['noclean' => true]);
  94          $modinstance->introfiles = external_util::get_area_files($context->id, $component, 'intro', false, false);
  95      }
  96  
  97      /**
  98       * Get the list of standard fields, to add to the declaration of the return values.
  99       *
 100       * Example usage combine the fields returned here with any extra ones your activity uses:
 101       *
 102       *   public static function execute_returns() {
 103       *       return new external_single_structure([
 104       *               'bigbluebuttonbns' => new external_multiple_structure(
 105       *                   new external_single_structure(array_merge(
 106       *                       helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(),
 107       *                       [
 108       *                           'meetingid' => new external_value(PARAM_RAW, 'Meeting id'),
 109       *                           'timemodified' => new external_value(PARAM_INT, 'Last time the instance was modified'),
 110       *                       ]
 111       *                   ))
 112       *               ),
 113       *               'warnings' => new external_warnings(),
 114       *           ]
 115       *       );
 116       *   }
 117       *
 118       * @param bool $introoptional if true, the intro fields are marked as optional. Default false.
 119       * @return external_description[] array of standard fields, to which you can add your activity-specific ones.
 120       */
 121      public static function standard_coursemodule_elements_returns(bool $introoptional = false): array {
 122          return [
 123              'id' => new external_value(PARAM_INT, 'Activity instance id'),
 124              'coursemodule' => new external_value(PARAM_INT, 'Course module id'),
 125              'course' => new external_value(PARAM_INT, 'Course id'),
 126              'name' => new external_value(PARAM_RAW, 'Activity name'),
 127              'intro' => new external_value(PARAM_RAW, 'Activity introduction', $introoptional ? VALUE_OPTIONAL : VALUE_REQUIRED),
 128              'introformat' => new external_format_value('intro', $introoptional ? VALUE_OPTIONAL : VALUE_REQUIRED),
 129              'introfiles' => new external_files('Files in the introduction', VALUE_OPTIONAL),
 130              'section' => new external_value(PARAM_INT, 'Course section id', VALUE_OPTIONAL),
 131              'visible' => new external_value(PARAM_BOOL, 'Visible', VALUE_OPTIONAL),
 132              'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL),
 133              'groupingid' => new external_value(PARAM_INT, 'Group id', VALUE_OPTIONAL),
 134              'lang' => new external_value(PARAM_SAFEDIR, 'Forced activity language', VALUE_OPTIONAL),
 135          ];
 136      }
 137  }