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   * 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      case 'mycourses':
  64          $PAGE->set_blocks_editing_capability('moodle/my:manageblocks');
  65          break;
  66      case 'user':
  67          if ($pagetype[1] === 'profile' && $PAGE->context->contextlevel == CONTEXT_USER
  68                  && $PAGE->context->instanceid == $USER->id) {
  69              // A user can only move blocks on their own site profile.
  70              $PAGE->set_blocks_editing_capability('moodle/user:manageownblocks');
  71          } else {
  72              $PAGE->set_blocks_editing_capability('moodle/user:manageblocks');
  73          }
  74          break;
  75  }
  76  
  77  // Send headers.
  78  echo $OUTPUT->header();
  79  
  80  switch ($action) {
  81      case 'move':
  82          // Loading blocks and instances for the region.
  83          $PAGE->blocks->load_blocks();
  84          $instances = $PAGE->blocks->get_blocks_for_region($buinewregion);
  85  
  86          $buinewweight = null;
  87          if ($buibeforeid == 0) {
  88              if (count($instances) === 0) {
  89                  // Moving the block into an empty region. Give it the default weight.
  90                  $buinewweight = 0;
  91              } else {
  92                  // Moving to very bottom.
  93                  $last = end($instances);
  94                  $buinewweight = $last->instance->weight + 1;
  95              }
  96          } else {
  97              // Moving somewhere.
  98              $lastweight = 0;
  99              $lastblock = 0;
 100              $first = reset($instances);
 101              if ($first) {
 102                  $lastweight = $first->instance->weight - 2;
 103              }
 104  
 105              foreach ($instances as $instance) {
 106                  if ($instance->instance->id == $buibeforeid) {
 107                      // Location found, just calculate weight like in block_manager->create_block_contents() and quit the loop.
 108                      if ($lastblock == $buimoveid) {
 109                          // Same block, same place - nothing to move.
 110                          break;
 111                      }
 112                      $buinewweight = ($lastweight + $instance->instance->weight) / 2;
 113                      break;
 114                  }
 115                  $lastweight = $instance->instance->weight;
 116                  $lastblock = $instance->instance->id;
 117              }
 118          }
 119  
 120          // Move block if we need.
 121          if (isset($buinewweight)) {
 122              // Nasty hack.
 123              $_POST['bui_newweight'] = $buinewweight;
 124              $PAGE->blocks->process_url_move();
 125          }
 126          break;
 127  }