Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 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  /**
  18   * Label external API
  19   *
  20   * @package    mod_label
  21   * @category   external
  22   * @copyright  2017 Juan Leyva <juan@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      Moodle 3.3
  25   */
  26  
  27  use core_course\external\helper_for_get_mods_by_courses;
  28  use core_external\external_api;
  29  use core_external\external_function_parameters;
  30  use core_external\external_multiple_structure;
  31  use core_external\external_single_structure;
  32  use core_external\external_value;
  33  use core_external\external_warnings;
  34  use core_external\util;
  35  
  36  /**
  37   * Label external functions
  38   *
  39   * @package    mod_label
  40   * @category   external
  41   * @copyright  2017 Juan Leyva <juan@moodle.com>
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   * @since      Moodle 3.3
  44   */
  45  class mod_label_external extends external_api {
  46  
  47      /**
  48       * Describes the parameters for get_labels_by_courses.
  49       *
  50       * @return external_function_parameters
  51       * @since Moodle 3.3
  52       */
  53      public static function get_labels_by_courses_parameters() {
  54          return new external_function_parameters (
  55              array(
  56                  'courseids' => new external_multiple_structure(
  57                      new external_value(PARAM_INT, 'Course id'), 'Array of course ids', VALUE_DEFAULT, array()
  58                  ),
  59              )
  60          );
  61      }
  62  
  63      /**
  64       * Returns a list of labels in a provided list of courses.
  65       * If no list is provided all labels that the user can view will be returned.
  66       *
  67       * @param array $courseids course ids
  68       * @return array of warnings and labels
  69       * @since Moodle 3.3
  70       */
  71      public static function get_labels_by_courses($courseids = array()) {
  72  
  73          $warnings = array();
  74          $returnedlabels = array();
  75  
  76          $params = array(
  77              'courseids' => $courseids,
  78          );
  79          $params = self::validate_parameters(self::get_labels_by_courses_parameters(), $params);
  80  
  81          $mycourses = array();
  82          if (empty($params['courseids'])) {
  83              $mycourses = enrol_get_my_courses();
  84              $params['courseids'] = array_keys($mycourses);
  85          }
  86  
  87          // Ensure there are courseids to loop through.
  88          if (!empty($params['courseids'])) {
  89  
  90              list($courses, $warnings) = util::validate_courses($params['courseids'], $mycourses);
  91  
  92              // Get the labels in this course, this function checks users visibility permissions.
  93              // We can avoid then additional validate_context calls.
  94              $labels = get_all_instances_in_courses("label", $courses);
  95              foreach ($labels as $label) {
  96                  helper_for_get_mods_by_courses::format_name_and_intro($label, 'mod_label');
  97                  $returnedlabels[] = $label;
  98              }
  99          }
 100  
 101          $result = array(
 102              'labels' => $returnedlabels,
 103              'warnings' => $warnings
 104          );
 105          return $result;
 106      }
 107  
 108      /**
 109       * Describes the get_labels_by_courses return value.
 110       *
 111       * @return external_single_structure
 112       * @since Moodle 3.3
 113       */
 114      public static function get_labels_by_courses_returns() {
 115          return new external_single_structure(
 116              array(
 117                  'labels' => new external_multiple_structure(
 118                      new external_single_structure(array_merge(
 119                          helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(),
 120                          [
 121                              'timemodified' => new external_value(PARAM_INT, 'Last time the label was modified'),
 122                          ]
 123                      ))
 124                  ),
 125                  'warnings' => new external_warnings(),
 126              )
 127          );
 128      }
 129  }