Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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