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