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 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   * Contains class core_tag\output\course_module_name
  19   *
  20   * @package   core_course
  21   * @copyright 2016 Marina Glancy
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_course\output;
  26  
  27  use context_module;
  28  use lang_string;
  29  use cm_info;
  30  
  31  /**
  32   * Class to prepare a course module name for display and in-place editing
  33   *
  34   * @deprecated since Moodle 4.0 MDL-72656 - please do not use this class any more.
  35   * @package   core_course
  36   * @copyright 2016 Marina Glancy
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class course_module_name extends \core\output\inplace_editable {
  40  
  41      /** @var cm_info */
  42      protected $cm;
  43  
  44      /** @var array */
  45      protected $displayoptions;
  46  
  47      /**
  48       * Constructor.
  49       *
  50       * @param cm_info $cm
  51       * @param bool $editable
  52       * @param array $displayoptions
  53       */
  54      public function __construct(cm_info $cm, $editable, $displayoptions = array()) {
  55          debugging(
  56              'course_section_cm_list is deprecated. Use core_courseformat\\output\\local\\cm\\cmname instead',
  57              DEBUG_DEVELOPER
  58          );
  59          $this->cm = $cm;
  60          $this->displayoptions = $displayoptions;
  61          $value = $cm->name;
  62          $edithint = new lang_string('edittitle');
  63          $editlabel = new lang_string('newactivityname', '', $cm->get_formatted_name());
  64          $editable = $editable && has_capability('moodle/course:manageactivities',
  65                      context_module::instance($cm->id));
  66          parent::__construct(
  67              'core_course', 'activityname', $cm->id, $editable, $value, $value, $edithint, $editlabel);
  68      }
  69  
  70      /**
  71       * Export this data so it can be used as the context for a mustache template (core/inplace_editable).
  72       *
  73       * @param renderer_base $output typically, the renderer that's calling this function
  74       * @return array data context for a mustache template
  75       */
  76      public function export_for_template(\renderer_base $output) {
  77          global $PAGE;
  78          $courserenderer = $PAGE->get_renderer('core', 'course');
  79          $this->displayvalue = $courserenderer->course_section_cm_name_title($this->cm, $this->displayoptions);
  80          if (strval($this->displayvalue) === '') {
  81              $this->editable = false;
  82          }
  83          return parent::export_for_template($output);
  84      }
  85  
  86      /**
  87       * Updates course module name
  88       *
  89       * @param int $itemid course module id
  90       * @param string $newvalue new name
  91       * @return static
  92       */
  93      public static function update($itemid, $newvalue) {
  94          $context = context_module::instance($itemid);
  95          // Check access.
  96          \external_api::validate_context($context);
  97          require_capability('moodle/course:manageactivities', $context);
  98  
  99          // Trim module name and Update value.
 100          set_coursemodule_name($itemid, trim($newvalue));
 101          $coursemodulerecord = get_coursemodule_from_id('', $itemid, 0, false, MUST_EXIST);
 102          // Return instance.
 103          $cm = get_fast_modinfo($coursemodulerecord->course)->get_cm($itemid);
 104          return new static($cm, true);
 105      }
 106  }