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