Split lib.php into locallib.php and lib.php;
[moodle.git] / lib / blocklib.php
CommitLineData
0f3fe4b6 1<?PHP //$Id$
2
3//This library includes all the necessary stuff to use blocks in course pages
4
5define('BLOCK_LEFT', 11);
6define('BLOCK_RIGHT', 12);
7define('BLOCK_MOVE_LEFT', 0x01);
8define('BLOCK_MOVE_RIGHT', 0x02);
9define('BLOCK_MOVE_UP', 0x04);
10define('BLOCK_MOVE_DOWN', 0x08);
11
89adb174 12define('COURSE_FORMAT_SITE', 0x01);
13define('COURSE_FORMAT_WEEKS', 0x02);
14define('COURSE_FORMAT_TOPICS', 0x04);
15define('COURSE_FORMAT_SOCIAL', 0x08);
0f3fe4b6 16
17//This function retrieves a method-defined property of a class WITHOUT instantiating an object
18//It seems that the only way to use the :: operator with variable class names is eval() :(
19//For caveats with this technique, see the PHP docs on operator ::
20function block_method_result($blockname, $method) {
21 if(!block_load_class($blockname)) {
22 return NULL;
23 }
24 return eval('return CourseBlock_'.$blockname.'::'.$method.'();');
25}
26
27//This function creates a new object of the specified block class
28function block_instance($blockname, $argument) {
29 if(!block_load_class($blockname)) {
30 return false;
31 }
32 $classname = 'CourseBlock_'.$blockname;
33 return New $classname($argument);
34}
35
36//This function loads the necessary class files for a block
37//Whenever you want to load a block, use this first
38function block_load_class($blockname) {
39 global $CFG;
40
41 @include_once($CFG->dirroot.'/blocks/moodleblock.class.php');
42 $classname = 'CourseBlock_'.$blockname;
43 @include_once($CFG->dirroot.'/blocks/'.$blockname.'/block_'.$blockname.'.php');
44
45 // After all this, return value indicating success or failure
46 return class_exists($classname);
47}
48
49//This function determines if there is some active block in an array of blocks
50function block_have_active($array) {
51 foreach($array as $blockid) {
52 if($blockid > 0) {
53 return true;
54 }
55 }
56 return false;
57}
58
59//This function print the one side of blocks in course main page
60function print_course_blocks(&$course, $blocksarray, $side) {
61 global $CFG;
62
63 $isediting = isediting($course->id);
64 $ismoving = ismoving($course->id);
65 $isteacheredit = isteacheredit($course->id);
66
67 if(!empty($blocksarray)) {
68 // Include the base class
69 @include_once($CFG->dirroot.'/blocks/moodleblock.class.php');
70 if(!class_exists('moodleblock')) {
71 error('Class MoodleBlock is not defined or file not found for /course/blocks/moodleblock.class.php');
72 }
73
74 $blockdata = get_records('blocks', 'visible', 1);
75 if($blockdata !== false) {
76
77 $lastblock = end($blocksarray);
78 $firstblock = reset($blocksarray);
79
80 foreach($blocksarray as $blockid) {
81 if(!isset($blockdata[abs($blockid)])) {
82 // This block is hidden. Don't show it.
83 continue;
84 }
85
86 $blockname = $blockdata[abs($blockid)]->name;
87 $block = block_instance($blockname, $course);
88 if($block === false) {
89 // Something went wrong
90 continue;
91 }
92
93 // There are various sanity checks commented out below
94 // because the block detection code should have already done them long ago.
95
96 /*
97 if(!is_subclass_of($block, 'MoodleBlock')) {
98 // Error: you have to derive from MoodleBlock
99 continue;
100 }
101
102 if($content === NULL || $title === NULL) {
103 // Error: This shouldn't have happened
104 continue;
105 }
106 */
107 if ($isediting && !$ismoving && $isteacheredit) {
108 $options = 0;
109 $options |= BLOCK_MOVE_UP * ($blockid != $firstblock);
110 $options |= BLOCK_MOVE_DOWN * ($blockid != $lastblock);
111 $options |= ($side == BLOCK_LEFT) ? BLOCK_MOVE_RIGHT : BLOCK_MOVE_LEFT;
112 $block->add_edit_controls($options, $blockid);
113 }
114
115 if($blockid < 0) {
116 // We won't print this block...
117 if($isediting) {
118 // Unless we 're in editing mode, in which case we 'll print a 'shadow'
119 $block->print_shadow();
120 }
121 continue;
122 }
123 // So simple...
124 $block->print_block();
125 }
126 }
127 }
128}
129
130//This iterates over an array of blocks and calculates the preferred width
131function blocks_preferred_width($blockarray, $blockinfos) {
132 $width = 0;
133
134 if(!is_array($blockarray) || empty($blockarray)) {
135 return 0;
136 }
137 foreach($blockarray as $blockid) {
138 if(isset($blockinfos[$blockid])) {
139 $blockname = $blockinfos[$blockid]->name;
140 $pref = block_method_result($blockname, 'preferred_width');
141 if($pref === NULL) {
142 continue;
143 }
144 if($pref > $width) {
145 $width = $pref;
146 }
147 }
148 }
149 return $width;
150}
151
152
153// $course passed by reference for speed
154// $leftblocks, $rightblocks passed by reference because block_action() needs to
155// update the arrays so that the change can be shown immediately.
156
157function block_action(&$course, &$leftblocks, &$rightblocks, $blockaction, $blockid) {
158
159 $blockid = abs(intval($blockid)); // Just to make sure
160
161 switch($blockaction) {
162 case 'toggle':
163 $block = block_find($blockid, $leftblocks, $rightblocks);
164 if($block !== false) {
165 if($block->side == BLOCK_LEFT) {
166 $leftblocks[$block->position] = -$leftblocks[$block->position];
167 }
168 else {
169 $rightblocks[$block->position] = -$rightblocks[$block->position];
170 }
171 }
172 break;
173 case 'delete':
174 $block = block_find($blockid, $leftblocks, $rightblocks);
175 if($block !== false) {
176 if($block->side == BLOCK_LEFT) {
177 unset($leftblocks[$block->position]);
178 }
179 else {
180 unset($rightblocks[$block->position]);
181 }
182 }
183 break;
184 case 'add':
185 // Toggle to enabled, or add it if it doesn't exist at all
186 $block = block_find($blockid, $leftblocks, $rightblocks);
187 if($block === false) {
188 // It doesn't exist at all, so add it
189 $rightblocks[] = $blockid;
190 }
191 else if($block->enabled == false) {
192 // Enable it
193 if($block->side == BLOCK_LEFT) {
194 $leftblocks[$block->position] = -$leftblocks[$block->position];
195 }
196 else {
197 $rightblocks[$block->position] = -$rightblocks[$block->position];
198 }
199 }
200 break;
201 case 'moveup':
202 $block = block_find($blockid, $leftblocks, $rightblocks);
203 if($block !== false) {
204 if($block->side == BLOCK_LEFT) {
205 if(isset($leftblocks[$block->position - 1])) {
206 // We can move it upwards
207 $oldblock = $leftblocks[$block->position - 1];
208 $leftblocks[$block->position - 1] = $leftblocks[$block->position]; // not $blockid, as this loses the sign
209 $leftblocks[$block->position] = $oldblock;
210 }
211 }
212 else {
213 if(isset($rightblocks[$block->position - 1])) {
214 // We can move it upwards
215 $oldblock = $rightblocks[$block->position - 1];
216 $rightblocks[$block->position - 1] = $rightblocks[$block->position]; // not $blockid, as this loses the sign
217 $rightblocks[$block->position] = $oldblock;
218 }
219 }
220 }
221 break;
222 case 'movedown':
223 $block = block_find($blockid, $leftblocks, $rightblocks);
224 if($block !== false) {
225 if($block->side == BLOCK_LEFT) {
226 if(isset($leftblocks[$block->position + 1])) {
227 // We can move it downwards
228 $oldblock = $leftblocks[$block->position + 1];
229 $leftblocks[$block->position + 1] = $leftblocks[$block->position]; // not $blockid, as this loses the sign
230 $leftblocks[$block->position] = $oldblock;
231 }
232 }
233 else {
234 if(isset($rightblocks[$block->position + 1])) {
235 // We can move it downwards
236 $oldblock = $rightblocks[$block->position + 1];
237 $rightblocks[$block->position + 1] = $rightblocks[$block->position]; // not $blockid, as this loses the sign
238 $rightblocks[$block->position] = $oldblock;
239 }
240 }
241 }
242 break;
243 case 'moveside':
244 $block = block_find($blockid, $leftblocks, $rightblocks);
245 if($block !== false) {
246 if($block->side == BLOCK_LEFT) {
247 unset($leftblocks[$block->position]);
248 $rightblocks[] = $block->enabled ? $blockid : -$blockid;
249 }
250 else {
251 unset($rightblocks[$block->position]);
252 $leftblocks[] = $block->enabled ? $blockid : -$blockid;
253 }
254 }
255 break;
256 }
257
258 $course->blockinfo = implode(',', $leftblocks).':'.implode(',',$rightblocks);
259 set_field('course', 'blockinfo', $course->blockinfo, 'id', $course->id);
260
261}
262
263// Searches for the block with ID $blockid in one or more of the two
264// blocks arrays. If not found, returns boolean false. Otherwise,
265// returns an object $finding where:
266// $finding->side = BLOCK_LEFT or BLOCK_RIGHT
267// $finding->enabled = true or false
268// $finding->position = index of corresponding array where found
269
270function block_find($blockid, $leftblocks, $rightblocks) {
271
272 if(($blockid = abs($blockid)) == 0) {
273 return false;
274 }
275
276 $finding->side = BLOCK_LEFT;
277 $finding->enabled = true;
278 $finding->position = NULL;
279
280 // First, search for the "enabled" block, since that's what we
281 // will be doing most of the time.
282
283 $key = array_search($blockid, $leftblocks);
284 if($key !== false && $key !== NULL) {
285 $finding->position = $key;
286 return $finding;
287 }
288 $key = array_search($blockid, $rightblocks);
289 if($key !== false && $key !== NULL) {
290 $finding->position = $key;
291 $finding->side = BLOCK_RIGHT;
292 return $finding;
293 }
294
295 // "enabled" block not found. Now search for the disabled block.
296 $finding->enabled = false;
297 $blockid = -$blockid;
298
299 $key = array_search($blockid, $leftblocks);
300 if($key !== false && $key !== NULL) {
301 $finding->position = $key;
302 return $finding;
303 }
304 $key = array_search($blockid, $rightblocks);
305 if($key !== false && $key !== NULL) {
306 $finding->position = $key;
307 $finding->side = BLOCK_RIGHT;
308 return $finding;
309 }
310
311 // Nothing found :(
312
313 return false;
314}
315
c598d270 316//This function prints the block to admin blocks as necessary
317function block_print_blocks_admin($courseid, $missingblocks) {
0f3fe4b6 318 if (isediting($courseid)) {
c598d270 319 $strblocks = get_string('blocks');
320 $stradd = get_string('add');
321 if (!empty($missingblocks)) {
0f3fe4b6 322 $blockdata = get_records_list('blocks', 'id', implode(',', $missingblocks));
c598d270 323 if ($blockdata !== false) {
324 foreach ($blockdata as $block) {
0f3fe4b6 325 $blockobject = block_instance($block->name, NULL);
c598d270 326 if ($blockobject === false) {
0f3fe4b6 327 continue;
328 }
329 $menu[$block->id] = $blockobject->get_title();
330 }
89adb174 331
332 if($courseid == 1) {
333 $target = 'index.php';
334 }
335 else {
336 $target = 'view.php';
337 }
338 $content = popup_form($target.'?id='.$courseid.'&amp;blockaction=add&amp;blockid=',
c598d270 339 $menu, 'add_block', '', "$stradd...", '', '', true);
0f3fe4b6 340 $content = '<div align="center">'.$content.'</div>';
c598d270 341 print_side_block($strblocks, $content, NULL, NULL, NULL);
0f3fe4b6 342 }
343 }
344 }
345}
346
347function upgrade_blocks_db($continueto) {
348/// This function upgrades the blocks tables, if necessary
349/// It's called from admin/index.php
350
351 global $CFG, $db;
352
353 require_once ("$CFG->dirroot/blocks/version.php"); // Get code versions
354
355 if (empty($CFG->blocks_version)) { // Blocks have never been installed.
356 $strdatabaseupgrades = get_string("databaseupgrades");
357 print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades,
358 "", "", false, "&nbsp;", "&nbsp;");
359
360 $db->debug=true;
361 if (modify_database("$CFG->dirroot/blocks/db/$CFG->dbtype.sql")) {
362 $db->debug = false;
363 if (set_config("blocks_version", $blocks_version)) {
364 notify(get_string("databasesuccess"), "green");
365 notify(get_string("databaseupgradeblocks", "", $blocks_version));
366 print_continue($continueto);
367 exit;
368 } else {
369 error("Upgrade of blocks system failed! (Could not update version in config table)");
370 }
371 } else {
372 error("Blocks tables could NOT be set up successfully!");
373 }
374 }
375
376
377 if ($blocks_version > $CFG->blocks_version) { // Upgrade tables
378 $strdatabaseupgrades = get_string("databaseupgrades");
379 print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades);
380
381 require_once ("$CFG->dirroot/blocks/db/$CFG->dbtype.php");
382
383 $db->debug=true;
384 if (blocks_upgrade($CFG->blocks_version)) {
385 $db->debug=false;
386 if (set_config("blocks_version", $blocks_version)) {
387 notify(get_string("databasesuccess"), "green");
388 notify(get_string("databaseupgradeblocks", "", $blocks_version));
389 print_continue($continueto);
390 exit;
391 } else {
392 error("Upgrade of blocks system failed! (Could not update version in config table)");
393 }
394 } else {
395 $db->debug=false;
396 error("Upgrade failed! See blocks/version.php");
397 }
398
399 } else if ($blocks_version < $CFG->blocks_version) {
400 notify("WARNING!!! The code you are using is OLDER than the version that made these databases!");
401 }
402}
403
404//This function finds all available blocks and install them
405//into blocks table or do all the upgrade process if newer
406function upgrade_blocks_plugins($continueto) {
407
408 global $CFG;
409
410 $blocktitles = array();
411 $invalidblocks = array();
412 $validblocks = array();
413 $notices = array();
414
415 //Count the number of blocks in db
416 $blockcount = count_records("blocks");
417 //If there isn't records. This is the first install, so I remember it
418 if ($blockcount == 0) {
419 $first_install = true;
420 } else {
421 $first_install = false;
422 }
423
424 $site = get_site();
425
0cb77f5a 426 if (!$blocks = get_list_of_plugins("blocks", "db") ) {
0f3fe4b6 427 error("No blocks installed!");
428 }
429
828c4e09 430 include_once($CFG->dirroot."/blocks/moodleblock.class.php");
0f3fe4b6 431 if(!class_exists('moodleblock')) {
432 error('Class MoodleBlock is not defined or file not found for /blocks/moodleblock.class.php');
433 }
434
435 foreach ($blocks as $blockname) {
436
5216e2c6 437 if ($blockname == "NEWBLOCK") { // Someone has unzipped the template, ignore it
0f3fe4b6 438 continue;
439 }
440
441 $fullblock = "$CFG->dirroot/blocks/$blockname";
442
443 if ( is_readable($fullblock."/block_".$blockname.".php")) {
444 include_once($fullblock."/block_".$blockname.".php");
445 } else {
446 $notices[] = "Block $blockname: ".$fullblock."/block_".$blockname.".php was not readable";
447 continue;
448 }
449
828c4e09 450 if ( is_dir("$fullblock/db/")) {
451 if ( is_readable("$fullblock/db/$CFG->dbtype.php")) {
452 include_once("$fullblock/db/$CFG->dbtype.php"); # defines upgrading function
453 } else {
454 $notices[] ="Block $blockname: $fullblock/db/$CFG->dbtype.php was not readable";
455 continue;
456 }
0f3fe4b6 457 }
458
459 $classname = 'CourseBlock_'.$blockname;
460 if(!class_exists($classname)) {
461 $notices[] = "Block $blockname: $classname not implemented";
462 continue;
463 }
464
465 // Let's see if it supports some basic methods
466 $methods = get_class_methods($classname);
467 if(!in_array(strtolower($classname), $methods)) {
468 // No constructor
469 $notices[] = "Block $blockname: class does not have a constructor";
470 $invalidblocks[] = $blockname;
471 continue;
472 }
473
474 unset($block);
475
476 $blockobj = New $classname($site);
477
478 // Inherits from MoodleBlock?
479 if(!is_subclass_of($blockobj, "moodleblock")) {
480 $notices[] = "Block $blockname: class does not inherit from MoodleBlock";
481 continue;
482 }
483
484 // OK, it's as we all hoped. For further tests, the object will do them itself.
485 if(!$blockobj->_self_test()) {
486 $notices[] = "Block $blockname: self test failed";
487 continue;
488 }
489 $block->version = $blockobj->get_version();
490
491 if (!isset($block->version)) {
492 $notices[] = "Block $blockname: hasn't version support";
493 continue;
494 }
495
496 $block->name = $blockname; // The name MUST match the directory
497 $blocktitle = $blockobj->get_title();
498
499 if ($currblock = get_record("blocks", "name", $block->name)) {
500 if ($currblock->version == $block->version) {
501 // do nothing
502 } else if ($currblock->version < $block->version) {
503 if (empty($updated_blocks)) {
504 $strblocksetup = get_string("blocksetup");
505 print_header($strblocksetup, $strblocksetup, $strblocksetup, "", "", false, "&nbsp;", "&nbsp;");
506 }
b88f7004 507 print_heading('New version of '.$blocktitle.' ('.$block->name.') exists');
508 $upgrade_function = $block->name.'_upgrade';
0f3fe4b6 509 if (function_exists($upgrade_function)) {
510 $db->debug=true;
511 if ($upgrade_function($currblock->version, $block)) {
b88f7004 512
513 $upgradesuccess = true;
0f3fe4b6 514 } else {
b88f7004 515 $upgradesuccess = false;
516 }
517 $db->debug=false;
518 }
519 else {
520 $upgradesuccess = true;
521 }
522 if(!$upgradesuccess) {
523 notify("Upgrading block $block->name from $currblock->version to $block->version FAILED!");
524 }
525 else {
526 // OK so far, now update the blocks record
527 $block->id = $currblock->id;
528 if (! update_record('blocks', $block)) {
529 error("Could not update block $block->name record in blocks table!");
0f3fe4b6 530 }
b88f7004 531 notify(get_string('blocksuccess', '', $blocktitle), 'green');
532 echo '<hr />';
0f3fe4b6 533 }
534 $updated_blocks = true;
535 } else {
536 error("Version mismatch: block $block->name can't downgrade $currblock->version -> $block->version !");
537 }
538
539 } else { // block not installed yet, so install it
540
541 // [pj] Normally this would be inline in the if, but we need to
542 // check for NULL (necessary for 4.0.5 <= PHP < 4.2.0)
543 $conflictblock = array_search($blocktitle, $blocktitles);
544 if($conflictblock !== false && $conflictblock !== NULL) {
545
546 // Duplicate block titles are not allowed, they confuse people
547 // AND PHP's associative arrays ;)
548 error('<strong>Naming conflict</strong>: block <strong>'.$block->name.'</strong> has the same title with an existing block, <strong>'.$conflictblock.'</strong>!');
549 }
550 if (empty($updated_blocks)) {
551 $strblocksetup = get_string("blocksetup");
552 print_header($strblocksetup, $strblocksetup, $strblocksetup, "", "", false, "&nbsp;", "&nbsp;");
553 }
554 print_heading($block->name);
555 $updated_blocks = true;
556 $db->debug = true;
a71bfa1c 557 @set_time_limit(0); // To allow slow databases to complete the long SQL
828c4e09 558 if (!is_dir("$fullblock/db/") || modify_database("$fullblock/db/$CFG->dbtype.sql")) {
0f3fe4b6 559 $db->debug = false;
560 if ($block->id = insert_record('blocks', $block)) {
561 notify(get_string('blocksuccess', '', $blocktitle), 'green');
562 echo "<HR>";
563 } else {
564 error("$block->name block could not be added to the block list!");
565 }
566 } else {
567 error("Block $block->name tables could NOT be set up successfully!");
568 }
569 }
570
571 $blocktitles[$block->name] = $blocktitle;
572 }
573
574 if(!empty($notices)) {
575 foreach($notices as $notice) {
576 notify($notice);
577 }
578 }
579
580 //Finally, if we are in the first_install, update every course blockinfo field with
581 //default values.
582 if ($first_install) {
583 //Iterate over each course
584 if ($courses = get_records("course")) {
585 foreach ($courses as $course) {
586 //Dependig of the format, insert some values
587 if ($course->format == "social") {
588 $blockinfo = blocks_get_default_blocks ($course->id, "participants,search_forums,calendar_month,calendar_upcoming,social_activities,recent_activity,admin,course_list");
589 } else {
590 //For topics and weeks formats (default built in the function)
591 $blockinfo = blocks_get_default_blocks($course->id);
592 }
593 if ($CFG->debug) {
594 echo 'Updating blockinfo for course: '.$course->shortname.'('.$blockinfo.')<br>';
595 }
596 }
597 }
598 }
599
600 if (!empty($updated_blocks)) {
601 print_continue($continueto);
602 die;
603 }
604}
605
606//This function returns the number of courses currently using the block
607function blocks_get_courses_using_block_by_id($blockid) {
608
609 $num = 0;
610
611 if ($courses = get_records("course")) {
612 foreach($courses as $course) {
613 $blocks = str_replace(":",",",$course->blockinfo);
614 $blocksarr = explode(",",$blocks);
615 if (block_find($blockid,$blocksarr,array())) {
616 $num++;
617 }
618 }
619 }
620
621 return $num;
622}
623
624//This function hides a block in all courses using it
625function blocks_update_every_block_by_id($blockid,$action) {
626
627 if ($courses = get_records("course")) {
628 foreach($courses as $course) {
629 //Calculate left and right blocks
630 $blocks = $course->blockinfo;
631 $delimpos = strpos($blocks, ':');
632
633 if($delimpos === false) {
634 // No ':' found, we have all left blocks
635 $leftblocks = explode(',', $blocks);
636 $rightblocks = array();
637 } else if($delimpos === 0) {
638 // ':' at start of string, we have all right blocks
639 $blocks = substr($blocks, 1);
640 $leftblocks = array();
641 $rightblocks = explode(',', $blocks);
642 }
643 else {
644 // Both left and right blocks
645 $leftpart = substr($blocks, 0, $delimpos);
646 $rightpart = substr($blocks, $delimpos + 1);
647 $leftblocks = explode(',', $leftpart);
648 $rightblocks = explode(',', $rightpart);
649 }
650
651 switch($action) {
652 case 'show':
653 $block = block_find($blockid, $leftblocks, $rightblocks);
654 if($block !== false) {
655 if($block->side == BLOCK_LEFT) {
656 $leftblocks[$block->position] = abs($leftblocks[$block->position]);
657 }
658 else {
659 $rightblocks[$block->position] = abs($rightblocks[$block->position]);
660 }
661 }
662 break;
663 case 'hide':
664 $block = block_find($blockid, $leftblocks, $rightblocks);
665 if($block !== false) {
666 if($block->side == BLOCK_LEFT) {
667 $leftblocks[$block->position] = -abs($leftblocks[$block->position]);
668 }
669 else {
670 $rightblocks[$block->position] = -abs($rightblocks[$block->position]);
671 }
672 }
673 break;
674 case 'delete':
675 $block = block_find($blockid, $leftblocks, $rightblocks);
676 if($block !== false) {
677 if($block->side == BLOCK_LEFT) {
678 unset($leftblocks[$block->position]);
679 }
680 else {
681 unset($rightblocks[$block->position]);
682 }
683 }
684 break;
685 }
686 $course->blockinfo = implode(',', $leftblocks).':'.implode(',',$rightblocks);
687 set_field('course', 'blockinfo', $course->blockinfo, 'id', $course->id);
688 }
689 }
690}
691
692// [pj] I didn't like the block_get_X_by_Y() functions because
693// we should be able to do without them with clever coding,
694// so I set out to see if they could be removed somehow.
695// Only block_get_default_blocks() depends on them, and that
696// one is used nowhere at the moment. So I 'm commenting
697// them out until a use IS found.
698// [el] Uncommented to be used in the installation process, when
699// inserting new courses and when restoring courses. Perhaps
700// they can be modified, but previously related processes
701// will use them since now.
702
703//This function returns the id of the block, searching it by name
704function block_get_id_by_name ($blockname) {
705
706 if ($block = get_record("blocks","name",$blockname)) {
707 return $block->id;
708 } else {
709 return 0;
710 }
711}
712
713//This function returns the name of the block, searching it by id
714function block_get_name_by_id ($blockid) {
715
716 if ($block = get_record("blocks","id",$blockid)) {
717 return $block->name;
718 } else {
719 return NULL;
720 }
721}
722
723//This function return the necessary contents to update course->blockinfo
724//with default values. It accepts a list of block_names as parameter. They
725//will be converted to their blockids equivalent. If a course is specified
726//then the function will update the field too!
727
a7b44978 728function blocks_get_default_blocks ($courseid = NULL, $blocknames="") {
89adb174 729
a7b44978 730 global $CFG;
731
732 if (empty($blocknames)) {
733 if (!empty($CFG->defaultblocks)) {
734 $blocknames = $CFG->defaultblocks;
735 } else {
736 $blocknames = "participants,activity_modules,search_forums,admin,course_list:news_items,calendar_upcoming,recent_activity";
737 }
738 }
0f3fe4b6 739
740 //Calculate left and right blocks
741 $blocksn = $blocknames;
742 $delimpos = strpos($blocksn, ':');
743
744 if($delimpos === false) {
745 // No ':' found, we have all left blocks
746 $leftblocksn = explode(',', $blocksn);
747 $rightblocksn = array();
748 } else if($delimpos === 0) {
749 // ':' at start of string, we have all right blocks
750 $blocksn = substr($blocksn, 1);
751 $leftblocksn = array();
752 $rightblocksn = explode(',', $blocksn);
753 }
754 else {
755 // Both left and right blocks
756 $leftpartn = substr($blocksn, 0, $delimpos);
757 $rightpartn = substr($blocksn, $delimpos + 1);
758 $leftblocksn = explode(',', $leftpartn);
759 $rightblocksn = explode(',', $rightpartn);
760 }
761
762 //Now I have blocks separated
763
764 $leftblocks = array();
765 $rightblocks = array();
766
767 if ($leftblocksn) {
768 foreach($leftblocksn as $leftblockn) {
769 //Convert blockname to id
229f852a 770 $leftblock = block_get_id_by_name(str_replace("-","",$leftblockn));
0f3fe4b6 771 if ($leftblock) {
772 //Check it's visible
773 if($block = get_record("blocks","id",$leftblock,"visible","1")) {
229f852a 774 //Check if the module was hidden at course level
775 if (substr($leftblockn,0,1) == "-") {
776 $leftblocks[] = -$leftblock;
777 } else {
778 $leftblocks[] = $leftblock;
779 }
0f3fe4b6 780 }
781 }
782 }
783 }
784
785 if ($rightblocksn) {
786 foreach($rightblocksn as $rightblockn) {
787 //Convert blockname to id
229f852a 788 $rightblock = block_get_id_by_name(str_replace("-","",$rightblockn));
0f3fe4b6 789 if ($rightblock) {
790 //Check it's visible
791 if($block = get_record("blocks","id",$rightblock,"visible","1")) {
229f852a 792 //Check if the module was hidden at course level
793 if (substr($rightblockn,0,1) == "-") {
794 $rightblocks[] = -$rightblock;
795 } else {
796 $rightblocks[] = $rightblock;
797 }
0f3fe4b6 798 }
799 }
800 }
801 }
802
803 //Calculate the blockinfo field
804 if ($leftblocks || $rightblocks) {
805 $blockinfo = '';
806 if ($leftblocks) {
807 $blockinfo .= implode(",", $leftblocks);
808 }
809 if ($rightblocks) {
810 $blockinfo .= ':'.implode(",",$rightblocks);
811 }
812 } else {
813 $blockinfo = '';
814 }
815
816 //If a course has been specified, update it
817 if ($courseid) {
818 set_field('course', "blockinfo", $blockinfo, "id", $courseid);
819 }
820
821 //Returns the blockinfo
822 return $blockinfo;
823}
824
229f852a 825//This function will return the names representation of the blockinfo field.
826//It's used to include that info in backups. To restore we'll use the
827//blocks_get_block_ids() function. It makes the opposite conversion
828//(from names to ids)
829function blocks_get_block_names ($blockinfo) {
830
831 //Calculate left and right blocks
832 $blocksn = $blockinfo;
833 $delimpos = strpos($blocksn, ':');
834
835 if($delimpos === false) {
836 // No ':' found, we have all left blocks
837 $leftblocksn = explode(',', $blocksn);
838 $rightblocksn = array();
839 } else if($delimpos === 0) {
840 // ':' at start of string, we have all right blocks
841 $blocksn = substr($blocksn, 1);
842 $leftblocksn = array();
843 $rightblocksn = explode(',', $blocksn);
844 }
845 else {
846 // Both left and right blocks
847 $leftpartn = substr($blocksn, 0, $delimpos);
848 $rightpartn = substr($blocksn, $delimpos + 1);
849 $leftblocksn = explode(',', $leftpartn);
850 $rightblocksn = explode(',', $rightpartn);
851 }
852
853 //Now I have blocks separated
854
855 $leftblocks = array();
856 $rightblocks = array();
857
858 if ($leftblocksn) {
859 foreach($leftblocksn as $leftblockn) {
860 //Convert id to blockname
861 $leftblock = block_get_name_by_id(abs($leftblockn));
862 if ($leftblock) {
863 //Check it's visible
864 if($block = get_record("blocks","name",$leftblock,"visible","1")) {
865 //Check if it's hidden oe no in the course
866 if($leftblockn<0) {
867 $leftblocks[] = '-'.$leftblock;
868 } else {
869 $leftblocks[] = $leftblock;
870 }
871 }
872 }
873 }
874 }
875
876 if ($rightblocksn) {
877 foreach($rightblocksn as $rightblockn) {
878 //Convert id to blockname
879 $rightblock = block_get_name_by_id(abs($rightblockn));
880 if ($rightblock) {
881 //Check it's visible
882 if($block = get_record("blocks","name",$rightblock,"visible","1")) {
883 //Check if it's hidden oe no in the course
884 if($rightblockn<0) {
885 $rightblocks[] = '-'.$rightblock;
886 } else {
887 $rightblocks[] = $rightblock;
888 }
889 }
890 }
891 }
892 }
893
894 //Calculate the blockinfo field
895 if ($leftblocks || $rightblocks) {
896 $blockinfo = '';
897 if ($leftblocks) {
898 $blockinfo .= implode(",", $leftblocks);
899 }
900 if ($rightblocks) {
901 $blockinfo .= ':'.implode(",",$rightblocks);
902 }
903 } else {
904 $blockinfo = '';
905 }
906
907 //Returns the blockinfo
908 return $blockinfo;
909}
910
911//This function will return the ids representation of the blockinfo field.
912//It's used to load that info from backups. This function is the opposite
913//to the blocks_get_block_names() used in backup
914function blocks_get_block_ids ($blockinfo) {
915
916 //Just call this with the appropiate parammeters.
917 return blocks_get_default_blocks(NULL,$blockinfo);
918}
0784eb7e 919
920// This is used to register the blocks that are displayed in the course page.
921// Set in course/view.php, and read from any other place.
922function blocks_used($blocks = NULL, $records = NULL) {
923 static $used = NULL;
924
925 if(!empty($blocks) && !empty($records)) {
926 $used = array();
927 foreach($blocks as $val) {
928 if($val > 0 && isset($records[$val])) {
929 $used[] = $records[$val]->name;
930 }
931 }
932 }
933
934 return $used;
935}
936
0f3fe4b6 937?>