Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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   * Provide interface for blocks AJAX actions
  19   *
  20   * @copyright  2011 Lancaster University Network Services Limited
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   * @package core
  23   */
  24  
  25  define('AJAX_SCRIPT', true);
  26  require_once(__DIR__ . '/../../config.php');
  27  
  28  // Initialise ALL common incoming parameters here, up front.
  29  $courseid = required_param('courseid', PARAM_INT);
  30  $pagelayout = required_param('pagelayout', PARAM_ALPHAEXT);
  31  $pagetype = required_param('pagetype', PARAM_ALPHANUMEXT);
  32  $contextid = required_param('contextid', PARAM_INT);
  33  $subpage = optional_param('subpage', '', PARAM_ALPHANUMEXT);
  34  $cmid = optional_param('cmid', null, PARAM_INT);
  35  $action = optional_param('action', '', PARAM_ALPHA);
  36  // Params for blocks-move actions.
  37  $buimoveid = optional_param('bui_moveid', 0, PARAM_INT);
  38  $buinewregion = optional_param('bui_newregion', '', PARAM_ALPHAEXT);
  39  $buibeforeid = optional_param('bui_beforeid', 0, PARAM_INT);
  40  
  41  // Setting pagetype and URL.
  42  $PAGE->set_pagetype($pagetype);
  43  $PAGE->set_url('/lib/ajax/blocks.php', array('courseid' => $courseid, 'pagelayout' => $pagelayout, 'pagetype' => $pagetype));
  44  
  45  // Verifying login and session.
  46  $cm = null;
  47  if (!is_null($cmid)) {
  48      $cm = get_coursemodule_from_id(null, $cmid, $courseid, false, MUST_EXIST);
  49  }
  50  require_login($courseid, false, $cm);
  51  require_sesskey();
  52  
  53  // Set context from ID, so we don't have to guess it from other info.
  54  $PAGE->set_context(context::instance_by_id($contextid));
  55  
  56  // Setting layout to replicate blocks configuration for the page we edit.
  57  $PAGE->set_pagelayout($pagelayout);
  58  $PAGE->set_subpage($subpage);
  59  $PAGE->blocks->add_custom_regions_for_pagetype($pagetype);
  60  $pagetype = explode('-', $pagetype);
  61  switch ($pagetype[0]) {
  62      case 'my':
  63          $PAGE->set_blocks_editing_capability('moodle/my:manageblocks');
  64          break;
  65      case 'user':
  66          if ($pagetype[1] === 'profile' && $PAGE->context->contextlevel == CONTEXT_USER
  67                  && $PAGE->context->instanceid == $USER->id) {
  68              // A user can only move blocks on their own site profile.
  69              $PAGE->set_blocks_editing_capability('moodle/user:manageownblocks');
  70          } else {
  71              $PAGE->set_blocks_editing_capability('moodle/user:manageblocks');
  72          }
  73          break;
  74  }
  75  
  76  // Send headers.
  77  echo $OUTPUT->header();
  78  
  79  switch ($action) {
  80      case 'move':
  81          // Loading blocks and instances for the region.
  82          $PAGE->blocks->load_blocks();
  83          $instances = $PAGE->blocks->get_blocks_for_region($buinewregion);
  84  
  85          $buinewweight = null;
  86          if ($buibeforeid == 0) {
  87              if (count($instances) === 0) {
  88                  // Moving the block into an empty region. Give it the default weight.
  89                  $buinewweight = 0;
  90              } else {
  91                  // Moving to very bottom.
  92                  $last = end($instances);
  93                  $buinewweight = $last->instance->weight + 1;
  94              }
  95          } else {
  96              // Moving somewhere.
  97              $lastweight = 0;
  98              $lastblock = 0;
  99              $first = reset($instances);
 100              if ($first) {
 101                  $lastweight = $first->instance->weight - 2;
 102              }
 103  
 104              foreach ($instances as $instance) {
 105                  if ($instance->instance->id == $buibeforeid) {
 106                      // Location found, just calculate weight like in block_manager->create_block_contents() and quit the loop.
 107                      if ($lastblock == $buimoveid) {
 108                          // Same block, same place - nothing to move.
 109                          break;
 110                      }
 111                      $buinewweight = ($lastweight + $instance->instance->weight) / 2;
 112                      break;
 113                  }
 114                  $lastweight = $instance->instance->weight;
 115                  $lastblock = $instance->instance->id;
 116              }
 117          }
 118  
 119          // Move block if we need.
 120          if (isset($buinewweight)) {
 121              // Nasty hack.
 122              $_POST['bui_newweight'] = $buinewweight;
 123              $PAGE->blocks->process_url_move();
 124          }
 125          break;
 126  }