1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
|
<?php //$Id: blocklib.php,v 1.86.2.2 2006/05/17 11:28:59 tjhunt Exp $
//This library includes all the necessary stuff to use blocks in course pages
define('BLOCK_MOVE_LEFT', 0x01);
define('BLOCK_MOVE_RIGHT', 0x02);
define('BLOCK_MOVE_UP', 0x04);
define('BLOCK_MOVE_DOWN', 0x08);
define('BLOCK_CONFIGURE', 0x10);
define('BLOCK_POS_LEFT', 'l');
define('BLOCK_POS_RIGHT', 'r');
define('BLOCKS_PINNED_TRUE',0);
define('BLOCKS_PINNED_FALSE',1);
define('BLOCKS_PINNED_BOTH',2);
require_once($CFG->libdir.'/pagelib.php');
// Returns false if this block is incompatible with the current version of Moodle.
function block_is_compatible($blockname) {
global $CFG;
$file = file($CFG->dirroot.'/blocks/'.$blockname.'/block_'.$blockname.'.php');
if(empty($file)) {
return NULL;
}
foreach($file as $line) {
// If you find MoodleBlock (appearing in the class declaration) it's not compatible
if(strpos($line, 'MoodleBlock')) {
return false;
}
// But if we find a { it means the class declaration is over, so it's compatible
else if(strpos($line, '{')) {
return true;
}
}
return NULL;
}
// Returns the case-sensitive name of the class' constructor function. This includes both
// PHP5- and PHP4-style constructors. If no appropriate constructor can be found, returns NULL.
// If there is no such class, returns boolean false.
function get_class_constructor($classname) {
// Caching
static $constructors = array();
if(!class_exists($classname)) {
return false;
}
// Tests indicate this doesn't hurt even in PHP5.
$classname = strtolower($classname);
// Return cached value, if exists
if(isset($constructors[$classname])) {
return $constructors[$classname];
}
// Get a list of methods. After examining several different ways of
// doing the check, (is_callable, method_exists, function_exists etc)
// it seems that this is the most reliable one.
$methods = get_class_methods($classname);
// PHP5 constructor?
if(phpversion() >= '5') {
if(in_array('__construct', $methods)) {
return $constructors[$classname] = '__construct';
}
}
// If we have PHP5 but no magic constructor, we have to lowercase the methods
$methods = array_map('strtolower', $methods);
if(in_array($classname, $methods)) {
return $constructors[$classname] = $classname;
}
return $constructors[$classname] = NULL;
}
//This function retrieves a method-defined property of a class WITHOUT instantiating an object
function block_method_result($blockname, $method, $param = NULL) {
if(!block_load_class($blockname)) {
return NULL;
}
return call_user_func(array('block_'.$blockname, $method), $param);
}
//This function creates a new object of the specified block class
function block_instance($blockname, $instance = NULL) {
if(!block_load_class($blockname)) {
return false;
}
$classname = 'block_'.$blockname;
$retval = new $classname;
if($instance !== NULL) {
$retval->_load_instance($instance);
}
return $retval;
}
//This function loads the necessary class files for a block
//Whenever you want to load a block, use this first
function block_load_class($blockname) {
global $CFG;
if(empty($blockname)) {
return false;
}
$classname = 'block_'.$blockname;
if(class_exists($classname)) {
return true;
}
require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
include_once($CFG->dirroot.'/blocks/'.$blockname.'/block_'.$blockname.'.php');
return class_exists($classname);
}
// This function returns an array with the IDs of any blocks that you can add to your page.
// Parameters are passed by reference for speed; they are not modified at all.
function blocks_get_missing(&$page, &$pageblocks) {
$missingblocks = array();
$allblocks = blocks_get_record();
$pageformat = $page->get_format_name();
if(!empty($allblocks)) {
foreach($allblocks as $block) {
if($block->visible && (!blocks_find_block($block->id, $pageblocks) || $block->multiple)) {
// And if it's applicable for display in this format...
if(blocks_name_allowed_in_format($block->name, $pageformat)) {
// ...add it to the missing blocks
$missingblocks[] = $block->id;
}
}
}
}
return $missingblocks;
}
function blocks_remove_inappropriate($page) {
$pageblocks = blocks_get_by_page($page);
if(empty($pageblocks)) {
return;
}
if(($pageformat = $page->get_format_name()) == NULL) {
return;
}
foreach($pageblocks as $position) {
foreach($position as $instance) {
$block = blocks_get_record($instance->blockid);
if(!blocks_name_allowed_in_format($block->name, $pageformat)) {
blocks_delete_instance($instance);
}
}
}
}
function blocks_name_allowed_in_format($name, $pageformat) {
$formats = block_method_result($name, 'applicable_formats');
$accept = NULL;
$depth = -1;
foreach($formats as $format => $allowed) {
$thisformat = '^'.str_replace('*', '[^-]*', $format).'.*$';
if(ereg($thisformat, $pageformat)) {
if(($scount = substr_count($format, '-')) > $depth) {
$depth = $scount;
$accept = $allowed;
}
}
}
if($accept === NULL) {
$accept = !empty($formats['all']);
}
return $accept;
}
function blocks_delete_instance($instance,$pinned=false) {
global $CFG;
// Get the block object and call instance_delete() first
if(!$record = blocks_get_record($instance->blockid)) {
return false;
}
if(!$obj = block_instance($record->name, $instance)) {
return false;
}
// Return value ignored
$obj->instance_delete();
if (!empty($pinned)) {
delete_records('block_pinned', 'id', $instance->id);
// And now, decrement the weight of all blocks after this one
execute_sql('UPDATE '.$CFG->prefix.'block_pinned SET weight = weight - 1 WHERE pagetype = \''.$instance->pagetype.
'\' AND position = \''.$instance->position.
'\' AND weight > '.$instance->weight, false);
} else {
// Now kill the db record;
delete_records('block_instance', 'id', $instance->id);
// And now, decrement the weight of all blocks after this one
execute_sql('UPDATE '.$CFG->prefix.'block_instance SET weight = weight - 1 WHERE pagetype = \''.$instance->pagetype.
'\' AND pageid = '.$instance->pageid.' AND position = \''.$instance->position.
'\' AND weight > '.$instance->weight, false);
}
return true;
}
// Accepts an array of block instances and checks to see if any of them have content to display
// (causing them to calculate their content in the process). Returns true or false. Parameter passed
// by reference for speed; the array is actually not modified.
function blocks_have_content(&$pageblocks, $position) {
if (empty($pageblocks) || !is_array($pageblocks) || !array_key_exists($position,$pageblocks)) {
return false;
}
// use a for() loop to get references to the array elements
// foreach() cannot fetch references in PHP v4.x
for ($n=0; $n<count($pageblocks[$position]);$n++) {
$instance = &$pageblocks[$position][$n];
if(!$instance->visible) {
continue;
}
if(!$record = blocks_get_record($instance->blockid)) {
continue;
}
if(!$obj = block_instance($record->name, $instance)) {
continue;
}
if(!$obj->is_empty()) {
// cache rec and obj
// for blocks_print_group()
$instance->rec = $record;
$instance->obj = $obj;
return true;
}
}
return false;
}
// This function prints one group of blocks in a page
// Parameters passed by reference for speed; they are not modified.
function blocks_print_group(&$page, &$pageblocks, $position) {
if(empty($pageblocks[$position])) {
$pageblocks[$position] = array();
$maxweight = 0;
}
else {
$maxweight = max(array_keys($pageblocks[$position]));
}
foreach ($pageblocks[$position] as $instance) {
if (!empty($instance->pinned)) {
$maxweight--;
}
}
$isediting = $page->user_is_editing();
foreach($pageblocks[$position] as $instance) {
// $instance may have ->rec and ->obj
// cached from when we walked $pageblocks
// in blocks_have_content()
if (empty($instance->rec)) {
$block = blocks_get_record($instance->blockid);
} else {
$block = $instance->rec;
}
if (empty($block)) {
// Block doesn't exist! We should delete this instance!
continue;
}
if(!$block->visible) {
// Disabled by the admin
continue;
}
if (empty($instance->obj)) {
if (!$obj = block_instance($block->name, $instance)) {
// Invalid block
continue;
}
} else {
$obj = $instance->obj;
}
$editalways = $page->edit_always();
if (($isediting && empty($instance->pinned)) || !empty($editalways)) {
$options = 0;
// The block can be moved up if it's NOT the first one in its position. If it is, we look at the OR clause:
// the first block might still be able to move up if the page says so (i.e., it will change position)
$options |= BLOCK_MOVE_UP * ($instance->weight != 0 || ($page->blocks_move_position($instance, BLOCK_MOVE_UP) != $instance->position));
// Same thing for downward movement
$options |= BLOCK_MOVE_DOWN * ($instance->weight != $maxweight || ($page->blocks_move_position($instance, BLOCK_MOVE_DOWN) != $instance->position));
// For left and right movements, it's up to the page to tell us whether they are allowed
$options |= BLOCK_MOVE_RIGHT * ($page->blocks_move_position($instance, BLOCK_MOVE_RIGHT) != $instance->position);
$options |= BLOCK_MOVE_LEFT * ($page->blocks_move_position($instance, BLOCK_MOVE_LEFT ) != $instance->position);
// Finally, the block can be configured if the block class either allows multiple instances, or if it specifically
// allows instance configuration (multiple instances override that one). It doesn't have anything to do with what the
// administrator has allowed for this block in the site admin options.
$options |= BLOCK_CONFIGURE * ( $obj->instance_allow_multiple() || $obj->instance_allow_config() );
$obj->_add_edit_controls($options);
}
if(!$instance->visible) {
if($isediting) {
$obj->_print_shadow();
}
}
else {
$obj->_print_block();
}
}
if($page->blocks_default_position() == $position && $page->user_is_editing()) {
blocks_print_adminblock($page, $pageblocks);
}
}
// This iterates over an array of blocks and calculates the preferred width
// Parameter passed by reference for speed; it's not modified.
function blocks_preferred_width(&$instances) {
$width = 0;
if(empty($instances) || !is_array($instances)) {
return 0;
}
$blocks = blocks_get_record();
foreach($instances as $instance) {
if(!$instance->visible) {
continue;
}
if (!array_key_exists($instance->blockid, $blocks)) {
// Block doesn't exist! We should delete this instance!
continue;
}
if(!$blocks[$instance->blockid]->visible) {
continue;
}
$pref = block_method_result($blocks[$instance->blockid]->name, 'preferred_width');
if($pref === NULL) {
continue;
}
if($pref > $width) {
$width = $pref;
}
}
return $width;
}
function blocks_get_record($blockid = NULL, $invalidate = false) {
static $cache = NULL;
if($invalidate || empty($cache)) {
$cache = get_records('block');
}
if($blockid === NULL) {
return $cache;
}
return (isset($cache[$blockid])? $cache[$blockid] : false);
}
function blocks_find_block($blockid, $blocksarray) {
if (empty($blocksarray)) {
return false;
}
foreach($blocksarray as $blockgroup) {
if (empty($blockgroup)) {
continue;
}
foreach($blockgroup as $instance) {
if($instance->blockid == $blockid) {
return $instance;
}
}
}
return false;
}
function blocks_find_instance($instanceid, $blocksarray) {
foreach($blocksarray as $subarray) {
foreach($subarray as $instance) {
if($instance->id == $instanceid) {
return $instance;
}
}
}
return false;
}
// Simple entry point for anyone that wants to use blocks
function blocks_setup(&$PAGE,$pinned=BLOCKS_PINNED_FALSE) {
switch ($pinned) {
case BLOCKS_PINNED_TRUE:
$pageblocks = blocks_get_pinned($PAGE);
break;
case BLOCKS_PINNED_BOTH:
$pageblocks = blocks_get_by_page_pinned($PAGE);
break;
case BLOCKS_PINNED_FALSE:
default:
$pageblocks = blocks_get_by_page($PAGE);
break;
}
blocks_execute_url_action($PAGE, $pageblocks,($pinned==BLOCKS_PINNED_TRUE));
return $pageblocks;
}
function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid, $pinned=false) {
global $CFG;
if (is_int($instanceorid)) {
$blockid = $instanceorid;
} else if (is_object($instanceorid)) {
$instance = $instanceorid;
}
switch($blockaction) {
case 'config':
global $USER;
$block = blocks_get_record($instance->blockid);
// Hacky hacky tricky stuff to get the original human readable block title,
// even if the block has configured its title to be something else.
// Create the object WITHOUT instance data.
$blockobject = block_instance($block->name);
if ($blockobject === false) {
break;
}
// First of all check to see if the block wants to be edited
if(!$blockobject->user_can_edit()) {
break;
}
// Now get the title and AFTER that load up the instance
$blocktitle = $blockobject->get_title();
$blockobject->_load_instance($instance);
optional_param('submitted', 0, PARAM_INT);
// Define the data we're going to silently include in the instance config form here,
// so we can strip them from the submitted data BEFORE serializing it.
$hiddendata = array(
'sesskey' => $USER->sesskey,
'instanceid' => $instance->id,
'blockaction' => 'config'
);
// To this data, add anything the page itself needs to display
$hiddendata = array_merge($hiddendata, $page->url_get_parameters());
if($data = data_submitted()) {
$remove = array_keys($hiddendata);
foreach($remove as $item) {
unset($data->$item);
}
if(!$blockobject->instance_config_save($data,$pinned)) {
error('Error saving block configuration');
}
// And nothing more, continue with displaying the page
}
else {
// We need to show the config screen, so we highjack the display logic and then die
$strheading = get_string('blockconfiga', 'moodle', $blocktitle);
$page->print_header(get_string('pageheaderconfigablock', 'moodle'), array($strheading => ''));
echo '<div class="block-config" id="'.$block->name.'">'; /// Make CSS easier
print_heading($strheading);
echo '<form method="post" action="'. $page->url_get_path() .'">';
echo '<p>';
foreach($hiddendata as $name => $val) {
echo '<input type="hidden" name="'. $name .'" value="'. $val .'" />';
}
echo '</p>';
$blockobject->instance_config_print();
echo '</form>';
echo '</div>';
$CFG->pagepath = 'blocks/' . $block->name;
print_footer();
die(); // Do not go on with the other page-related stuff
}
break;
case 'toggle':
if(empty($instance)) {
error('Invalid block instance for '.$blockaction);
}
$instance->visible = ($instance->visible) ? 0 : 1;
if (!empty($pinned)) {
update_record('block_pinned', $instance);
} else {
update_record('block_instance', $instance);
}
break;
case 'delete':
if(empty($instance)) {
error('Invalid block instance for '. $blockaction);
}
blocks_delete_instance($instance, $pinned);
break;
case 'moveup':
if(empty($instance)) {
error('Invalid block instance for '. $blockaction);
}
if($instance->weight == 0) {
// The block is the first one, so a move "up" probably means it changes position
// Where is the instance going to be moved?
$newpos = $page->blocks_move_position($instance, BLOCK_MOVE_UP);
$newweight = (empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1);
blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
}
else {
// The block is just moving upwards in the same position.
// This configuration will make sure that even if somehow the weights
// become not continuous, block move operations will eventually bring
// the situation back to normal without printing any warnings.
if(!empty($pageblocks[$instance->position][$instance->weight - 1])) {
$other = $pageblocks[$instance->position][$instance->weight - 1];
}
if(!empty($other)) {
++$other->weight;
if (!empty($pinned)) {
update_record('block_pinned', $other);
} else {
update_record('block_instance', $other);
}
}
--$instance->weight;
if (!empty($pinned)) {
update_record('block_pinned', $instance);
} else {
update_record('block_instance', $instance);
}
}
break;
case 'movedown':
if(empty($instance)) {
error('Invalid block instance for '. $blockaction);
}
if($instance->weight == max(array_keys($pageblocks[$instance->position]))) {
// The block is the last one, so a move "down" probably means it changes position
// Where is the instance going to be moved?
$newpos = $page->blocks_move_position($instance, BLOCK_MOVE_DOWN);
$newweight = (empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1);
blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
}
else {
// The block is just moving downwards in the same position.
// This configuration will make sure that even if somehow the weights
// become not continuous, block move operations will eventually bring
// the situation back to normal without printing any warnings.
if(!empty($pageblocks[$instance->position][$instance->weight + 1])) {
$other = $pageblocks[$instance->position][$instance->weight + 1];
}
if(!empty($other)) {
--$other->weight;
if (!empty($pinned)) {
update_record('block_pinned', $other);
} else {
update_record('block_instance', $other);
}
}
++$instance->weight;
if (!empty($pinned)) {
update_record('block_pinned', $instance);
} else {
update_record('block_instance', $instance);
}
}
break;
case 'moveleft':
if(empty($instance)) {
error('Invalid block instance for '. $blockaction);
}
// Where is the instance going to be moved?
$newpos = $page->blocks_move_position($instance, BLOCK_MOVE_LEFT);
$newweight = (empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1);
blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
break;
case 'moveright':
if(empty($instance)) {
error('Invalid block instance for '. $blockaction);
}
// Where is the instance going to be moved?
$newpos = $page->blocks_move_position($instance, BLOCK_MOVE_RIGHT);
$newweight = (empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1);
blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
break;
case 'add':
// Add a new instance of this block, if allowed
$block = blocks_get_record($blockid);
if(empty($block) || !$block->visible) {
// Only allow adding if the block exists and is enabled
break;
}
if(!$block->multiple && blocks_find_block($blockid, $pageblocks) !== false) {
// If no multiples are allowed and we already have one, return now
break;
}
if(!block_method_result($block->name, 'user_can_addto', $page)) {
// If the block doesn't want to be added...
break;
}
$newpos = $page->blocks_default_position();
if (!empty($pinned)) {
$sql = 'SELECT 1, max(weight) + 1 AS nextfree FROM '. $CFG->prefix .'block_pinned WHERE '
.' pagetype = \''. $page->get_type() .'\' AND position = \''. $newpos .'\'';
} else {
$sql = 'SELECT 1, max(weight) + 1 AS nextfree FROM '. $CFG->prefix .'block_instance WHERE pageid = '. $page->get_id()
.' AND pagetype = \''. $page->get_type() .'\' AND position = \''. $newpos .'\'';
}
$weight = get_record_sql($sql);
$newinstance = new stdClass;
$newinstance->blockid = $blockid;
if (empty($pinned)) {
$newinstance->pageid = $page->get_id();
}
$newinstance->pagetype = $page->get_type();
$newinstance->position = $newpos;
$newinstance->weight = empty($weight->nextfree) ? 0 : $weight->nextfree;
$newinstance->visible = 1;
$newinstance->configdata = '';
if (!empty($pinned)) {
$newinstance->id = insert_record('block_pinned', $newinstance);
} else {
$newinstance->id = insert_record('block_instance', $newinstance);
}
// If the new instance was created, allow it to do additional setup
if($newinstance && ($obj = block_instance($block->name, $newinstance))) {
// Return value ignored
$obj->instance_create();
}
break;
}
// In order to prevent accidental duplicate actions, redirect to a page with a clean url
redirect($page->url_get_full());
}
// You can use this to get the blocks to respond to URL actions without much hassle
function blocks_execute_url_action(&$PAGE, &$pageblocks,$pinned=false) {
$blockaction = optional_param('blockaction');
if (empty($blockaction) || !$PAGE->user_allowed_editing() || !confirm_sesskey()) {
return;
}
$instanceid = optional_param('instanceid', 0, PARAM_INT);
$blockid = optional_param('blockid', 0, PARAM_INT);
if (!empty($blockid)) {
blocks_execute_action($PAGE, $pageblocks, strtolower($blockaction), $blockid, $pinned);
}
else if (!empty($instanceid)) {
$instance = blocks_find_instance($instanceid, $pageblocks);
blocks_execute_action($PAGE, $pageblocks, strtolower($blockaction), $instance, $pinned);
}
}
// This shouldn't be used externally at all, it's here for use by blocks_execute_action()
// in order to reduce code repetition.
function blocks_execute_repositioning(&$instance, $newpos, $newweight, $pinned=false) {
global $CFG;
// If it's staying where it is, don't do anything
if($newpos == $instance->position) {
return;
}
// Close the weight gap we 'll leave behind
if (!empty($pinned)) {
$sql = 'UPDATE '. $CFG->prefix .'block_instance SET weight = weight - 1 WHERE pagetype = \''. $instance->pagetype.
'\' AND position = \'' .$instance->position.
'\' AND weight > '. $instance->weight;
} else {
$sql = 'UPDATE '. $CFG->prefix .'block_instance SET weight = weight - 1 WHERE pagetype = \''. $instance->pagetype.
'\' AND pageid = '. $instance->pageid .' AND position = \'' .$instance->position.
'\' AND weight > '. $instance->weight;
}
execute_sql($sql,false);
$instance->position = $newpos;
$instance->weight = $newweight;
if (!empty($pinned)) {
update_record('block_pinned', $instance);
} else {
update_record('block_instance', $instance);
}
}
function blocks_get_pinned($page) {
$visible = true;
if (method_exists($page,'edit_always')) {
if ($page->edit_always()) {
$visible = false;
}
}
$blocks = get_records_select('block_pinned', 'pagetype = \''. $page->get_type() .'\''.(($visible) ? 'AND visible = 1' : ''), 'position, weight');
$positions = $page->blocks_get_positions();
$arr = array();
foreach($positions as $key => $position) {
$arr[$position] = array();
}
if(empty($blocks)) {
return $arr;
}
foreach($blocks as $block) {
$block->pinned = true; // so we know we can't move it.
// make up an instanceid if we can..
$block->pageid = $page->get_id();
$arr[$block->position][$block->weight] = $block;
}
return $arr;
}
function blocks_get_by_page_pinned($page) {
$pinned = blocks_get_pinned($page);
$user = blocks_get_by_page($page);
$weights = array();
foreach ($pinned as $pos => $arr) {
$weights[$pos] = count($arr);
}
foreach ($user as $pos => $blocks) {
if (!array_key_exists($pos,$pinned)) {
$pinned[$pos] = array();
}
if (!array_key_exists($pos,$weights)) {
$weights[$pos] = 0;
}
foreach ($blocks as $block) {
$pinned[$pos][$weights[$pos]] = $block;
$weights[$pos]++;
}
}
return $pinned;
}
function blocks_get_by_page($page) {
$blocks = get_records_select('block_instance', "pageid = '". $page->get_id() ."' AND pagetype = '". $page->get_type() ."'", 'position, weight');
$positions = $page->blocks_get_positions();
$arr = array();
foreach($positions as $key => $position) {
$arr[$position] = array();
}
if(empty($blocks)) {
return $arr;
}
foreach($blocks as $block) {
$arr[$block->position][$block->weight] = $block;
}
return $arr;
}
//This function prints the block to admin blocks as necessary
function blocks_print_adminblock(&$page, &$pageblocks) {
global $USER;
$missingblocks = blocks_get_missing($page, $pageblocks);
if (!empty($missingblocks)) {
$strblocks = get_string('blocks');
$stradd = get_string('add');
foreach ($missingblocks as $blockid) {
$block = blocks_get_record($blockid);
$blockobject = block_instance($block->name);
if ($blockobject === false) {
continue;
}
if(!$blockobject->user_can_addto($page)) {
continue;
}
$menu[$block->id] = $blockobject->get_title();
}
asort($menu);
$target = $page->url_get_full(array('sesskey' => $USER->sesskey, 'blockaction' => 'add'));
$content = popup_form($target.'&blockid=', $menu, 'add_block', '', $stradd .'...', '', '', true);
print_side_block($strblocks, $content, NULL, NULL, NULL, array('class' => 'block_adminblock'));
}
}
function blocks_repopulate_page($page) {
global $CFG;
$allblocks = blocks_get_record();
if(empty($allblocks)) {
error('Could not retrieve blocks from the database');
}
// Assemble the information to correlate block names to ids
$idforname = array();
foreach($allblocks as $block) {
$idforname[$block->name] = $block->id;
}
/// If the site override has been defined, it is the only valid one.
if (!empty($CFG->defaultblocks_override)) {
$blocknames = $CFG->defaultblocks_override;
}
else {
$blocknames = $page->blocks_get_default();
}
$positions = $page->blocks_get_positions();
$posblocks = explode(':', $blocknames);
// Now one array holds the names of the positions, and the other one holds the blocks
// that are going to go in each position. Luckily for us, both arrays are numerically
// indexed and the indexes match, so we can work straight away... but CAREFULLY!
// Ready to start creating block instances, but first drop any existing ones
delete_records('block_instance', 'pageid', $page->get_id(), 'pagetype', $page->get_type());
// Here we slyly count $posblocks and NOT $positions. This can actually make a difference
// if the textual representation has undefined slots in the end. So we only work with as many
// positions were retrieved, not with all the page says it has available.
$numpositions = count($posblocks);
for($i = 0; $i < $numpositions; ++$i) {
$position = $positions[$i];
$blocknames = explode(',', $posblocks[$i]);
$weight = 0;
foreach($blocknames as $blockname) {
$newinstance = new stdClass;
$newinstance->blockid = $idforname[$blockname];
$newinstance->pageid = $page->get_id();
$newinstance->pagetype = $page->get_type();
$newinstance->position = $position;
$newinstance->weight = $weight;
$newinstance->visible = 1;
$newinstance->configdata = '';
if(!empty($newinstance->blockid)) {
// Only add block if it was recognized
insert_record('block_instance', $newinstance);
++$weight;
}
}
}
return true;
}
function upgrade_blocks_db($continueto) {
/// This function upgrades the blocks tables, if necessary
/// It's called from admin/index.php
global $CFG, $db;
require_once ($CFG->dirroot .'/blocks/version.php'); // Get code versions
if (empty($CFG->blocks_version)) { // Blocks have never been installed.
$strdatabaseupgrades = get_string('databaseupgrades');
print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades,
'', '', false, ' ', ' ');
$db->debug=true;
if (modify_database($CFG->dirroot .'/blocks/db/'. $CFG->dbtype .'.sql')) {
$db->debug = false;
if (set_config('blocks_version', $blocks_version)) {
notify(get_string('databasesuccess'), 'notifysuccess');
notify(get_string('databaseupgradeblocks', '', $blocks_version));
print_continue($continueto);
exit;
} else {
error('Upgrade of blocks system failed! (Could not update version in config table)');
}
} else {
error('Blocks tables could NOT be set up successfully!');
}
}
if ($blocks_version > $CFG->blocks_version) { // Upgrade tables
$strdatabaseupgrades = get_string('databaseupgrades');
print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades);
require_once ($CFG->dirroot .'/blocks/db/'. $CFG->dbtype .'.php');
$db->debug=true;
if (blocks_upgrade($CFG->blocks_version)) {
$db->debug=false;
if (set_config('blocks_version', $blocks_version)) {
notify(get_string('databasesuccess'), 'notifysuccess');
notify(get_string('databaseupgradeblocks', '', $blocks_version));
print_continue($continueto);
exit;
} else {
error('Upgrade of blocks system failed! (Could not update version in config table)');
}
} else {
$db->debug=false;
error('Upgrade failed! See blocks/version.php');
}
} else if ($blocks_version < $CFG->blocks_version) {
notify('WARNING!!! The Blocks version you are using is OLDER than the version that made these databases!');
}
}
//This function finds all available blocks and install them
//into blocks table or do all the upgrade process if newer
function upgrade_blocks_plugins($continueto) {
global $CFG;
$blocktitles = array();
$invalidblocks = array();
$validblocks = array();
$notices = array();
//Count the number of blocks in db
$blockcount = count_records('block');
//If there isn't records. This is the first install, so I remember it
if ($blockcount == 0) {
$first_install = true;
} else {
$first_install = false;
}
$site = get_site();
if (!$blocks = get_list_of_plugins('blocks', 'db') ) {
error('No blocks installed!');
}
include_once($CFG->dirroot .'/blocks/moodleblock.class.php');
if(!class_exists('block_base')) {
error('Class block_base is not defined or file not found for /blocks/moodleblock.class.php');
}
foreach ($blocks as $blockname) {
if ($blockname == 'NEWBLOCK') { // Someone has unzipped the template, ignore it
continue;
}
if(!block_is_compatible($blockname)) {
// This is an old-style block
//$notices[] = 'Block '. $blockname .' is not compatible with the current version of Mooodle and needs to be updated by a programmer.';
$invalidblocks[] = $blockname;
continue;
}
$fullblock = $CFG->dirroot .'/blocks/'. $blockname;
if ( is_readable($fullblock.'/block_'.$blockname.'.php')) {
include_once($fullblock.'/block_'.$blockname.'.php');
} else {
$notices[] = 'Block '. $blockname .': '. $fullblock .'/block_'. $blockname .'.php was not readable';
continue;
}
if ( @is_dir($fullblock .'/db/')) {
if ( @is_readable($fullblock .'/db/'. $CFG->dbtype .'.php')) {
include_once($fullblock .'/db/'. $CFG->dbtype .'.php'); // defines upgrading function
} else {
//$notices[] ='Block '. $blockname .': '. $fullblock .'/db/'. $CFG->dbtype .'.php was not readable';
continue;
}
}
$classname = 'block_'.$blockname;
if(!class_exists($classname)) {
$notices[] = 'Block '. $blockname .': '. $classname .' not implemented';
continue;
}
// Here is the place to see if the block implements a constructor (old style),
// an init() function (new style) or nothing at all (error time).
$constructor = get_class_constructor($classname);
if(empty($constructor)) {
// No constructor
$notices[] = 'Block '. $blockname .': class does not have a constructor';
$invalidblocks[] = $blockname;
continue;
}
$block = new stdClass; // This may be used to update the db below
$blockobj = new $classname; // This is what we 'll be testing
// Inherits from block_base?
if(!is_subclass_of($blockobj, 'block_base')) {
$notices[] = 'Block '. $blockname .': class does not inherit from block_base';
continue;
}
// OK, it's as we all hoped. For further tests, the object will do them itself.
if(!$blockobj->_self_test()) {
$notices[] = 'Block '. $blockname .': self test failed';
continue;
}
$block->version = $blockobj->get_version();
if (!isset($block->version)) {
$notices[] = 'Block '. $blockname .': has no version support. It must be updated by a programmer.';
continue;
}
$block->name = $blockname; // The name MUST match the directory
$blocktitle = $blockobj->get_title();
if ($currblock = get_record('block', 'name', $block->name)) {
if ($currblock->version == $block->version) {
// do nothing
} else if ($currblock->version < $block->version) {
if (empty($updated_blocks)) {
$strblocksetup = get_string('blocksetup');
print_header($strblocksetup, $strblocksetup, $strblocksetup, '', '', false, ' ', ' ');
}
print_heading('New version of '.$blocktitle.' ('.$block->name.') exists');
$upgrade_function = $block->name.'_upgrade';
if (function_exists($upgrade_function)) {
$db->debug=true;
if ($upgrade_function($currblock->version, $block)) {
$upgradesuccess = true;
} else {
$upgradesuccess = false;
}
$db->debug=false;
}
else {
$upgradesuccess = true;
}
if(!$upgradesuccess) {
notify('Upgrading block '. $block->name .' from '. $currblock->version .' to '. $block->version .' FAILED!');
}
else {
// OK so far, now update the block record
$block->id = $currblock->id;
if (! update_record('block', $block)) {
error('Could not update block '. $block->name .' record in block table!');
}
notify(get_string('blocksuccess', '', $blocktitle), 'notifysuccess');
echo '<hr />';
}
$updated_blocks = true;
} else {
error('Version mismatch: block '. $block->name .' can\'t downgrade '. $currblock->version .' -> '. $block->version .'!');
}
} else { // block not installed yet, so install it
// If it allows multiples, start with it enabled
$block->multiple = $blockobj->instance_allow_multiple();
if (!empty($blockobj->cron)) {
$block->cron = $blockobj->cron;
}
// [pj] Normally this would be inline in the if, but we need to
// check for NULL (necessary for 4.0.5 <= PHP < 4.2.0)
$conflictblock = array_search($blocktitle, $blocktitles);
if($conflictblock !== false && $conflictblock !== NULL) {
// Duplicate block titles are not allowed, they confuse people
// AND PHP's associative arrays ;)
error('<strong>Naming conflict</strong>: block <strong>'.$block->name.'</strong> has the same title with an existing block, <strong>'.$conflictblock.'</strong>!');
}
if (empty($updated_blocks)) {
$strblocksetup = get_string('blocksetup');
print_header($strblocksetup, $strblocksetup, $strblocksetup, '', '', false, ' ', ' ');
}
print_heading($block->name);
$updated_blocks = true;
$db->debug = true;
@set_time_limit(0); // To allow slow databases to complete the long SQL
if (!is_dir($fullblock .'/db/') || modify_database($fullblock .'/db/'. $CFG->dbtype .'.sql')) {
$db->debug = false;
if ($block->id = insert_record('block', $block)) {
notify(get_string('blocksuccess', '', $blocktitle), 'notifysuccess');
echo '<hr />';
} else {
error($block->name .' block could not be added to the block list!');
}
} else {
error('Block '. $block->name .' tables could NOT be set up successfully!');
}
}
$blocktitles[$block->name] = $blocktitle;
}
if(!empty($notices)) {
foreach($notices as $notice) {
notify($notice);
}
}
// Finally, if we are in the first_install of BLOCKS (this means that we are
// upgrading from Moodle < 1.3), put blocks in all existing courses.
if ($first_install) {
//Iterate over each course
if ($courses = get_records('course')) {
foreach ($courses as $course) {
$page = page_create_object(PAGE_COURSE_VIEW, $course->id);
blocks_repopulate_page($page);
}
}
}
if (!empty($CFG->siteblocksadded)) { /// This is a once-off hack to make a proper upgrade
$page = page_create_object(PAGE_COURSE_VIEW, SITEID);
blocks_repopulate_page($page);
delete_records('config', 'name', 'siteblocksadded');
}
if (!empty($updated_blocks)) {
print_continue($continueto);
die;
}
}
?>
|