Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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