0f3fe4b6 |
1 | <?PHP //$Id$ |
2 | |
3 | //This library includes all the necessary stuff to use blocks in course pages |
4 | |
5 | define('BLOCK_LEFT', 11); |
6 | define('BLOCK_RIGHT', 12); |
7 | define('BLOCK_MOVE_LEFT', 0x01); |
8 | define('BLOCK_MOVE_RIGHT', 0x02); |
9 | define('BLOCK_MOVE_UP', 0x04); |
10 | define('BLOCK_MOVE_DOWN', 0x08); |
11 | |
12 | define('COURSE_FORMAT_WEEKS', 0x01); |
13 | define('COURSE_FORMAT_TOPICS', 0x02); |
14 | define('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 :: |
19 | function 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 |
27 | function 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 |
37 | function 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 |
49 | function 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 |
59 | function 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 |
130 | function 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 | |
156 | function 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 | |
269 | function 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 | |
315 | //This function prints the add_block popup as necessary |
316 | function block_print_add_block($courseid, $missingblocks) { |
317 | if (isediting($courseid)) { |
318 | $title = get_string('add'); |
319 | if(!empty($missingblocks)) { |
320 | $blockdata = get_records_list('blocks', 'id', implode(',', $missingblocks)); |
321 | if($blockdata !== false) { |
322 | foreach($blockdata as $block) { |
323 | $blockobject = block_instance($block->name, NULL); |
324 | if($blockobject === false) { |
325 | continue; |
326 | } |
327 | $menu[$block->id] = $blockobject->get_title(); |
328 | } |
329 | $content = popup_form('view.php?id='.$courseid.'&blockaction=add&blockid=',$menu,'add_block','','choose','','',true); |
330 | $content = '<div align="center">'.$content.'</div>'; |
331 | print_side_block($title, $content, NULL, NULL, NULL); |
332 | } |
333 | } |
334 | } |
335 | } |
336 | |
337 | function upgrade_blocks_db($continueto) { |
338 | /// This function upgrades the blocks tables, if necessary |
339 | /// It's called from admin/index.php |
340 | |
341 | global $CFG, $db; |
342 | |
343 | require_once ("$CFG->dirroot/blocks/version.php"); // Get code versions |
344 | |
345 | if (empty($CFG->blocks_version)) { // Blocks have never been installed. |
346 | $strdatabaseupgrades = get_string("databaseupgrades"); |
347 | print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades, |
348 | "", "", false, " ", " "); |
349 | |
350 | $db->debug=true; |
351 | if (modify_database("$CFG->dirroot/blocks/db/$CFG->dbtype.sql")) { |
352 | $db->debug = false; |
353 | if (set_config("blocks_version", $blocks_version)) { |
354 | notify(get_string("databasesuccess"), "green"); |
355 | notify(get_string("databaseupgradeblocks", "", $blocks_version)); |
356 | print_continue($continueto); |
357 | exit; |
358 | } else { |
359 | error("Upgrade of blocks system failed! (Could not update version in config table)"); |
360 | } |
361 | } else { |
362 | error("Blocks tables could NOT be set up successfully!"); |
363 | } |
364 | } |
365 | |
366 | |
367 | if ($blocks_version > $CFG->blocks_version) { // Upgrade tables |
368 | $strdatabaseupgrades = get_string("databaseupgrades"); |
369 | print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades); |
370 | |
371 | require_once ("$CFG->dirroot/blocks/db/$CFG->dbtype.php"); |
372 | |
373 | $db->debug=true; |
374 | if (blocks_upgrade($CFG->blocks_version)) { |
375 | $db->debug=false; |
376 | if (set_config("blocks_version", $blocks_version)) { |
377 | notify(get_string("databasesuccess"), "green"); |
378 | notify(get_string("databaseupgradeblocks", "", $blocks_version)); |
379 | print_continue($continueto); |
380 | exit; |
381 | } else { |
382 | error("Upgrade of blocks system failed! (Could not update version in config table)"); |
383 | } |
384 | } else { |
385 | $db->debug=false; |
386 | error("Upgrade failed! See blocks/version.php"); |
387 | } |
388 | |
389 | } else if ($blocks_version < $CFG->blocks_version) { |
390 | notify("WARNING!!! The code you are using is OLDER than the version that made these databases!"); |
391 | } |
392 | } |
393 | |
394 | //This function finds all available blocks and install them |
395 | //into blocks table or do all the upgrade process if newer |
396 | function upgrade_blocks_plugins($continueto) { |
397 | |
398 | global $CFG; |
399 | |
400 | $blocktitles = array(); |
401 | $invalidblocks = array(); |
402 | $validblocks = array(); |
403 | $notices = array(); |
404 | |
405 | //Count the number of blocks in db |
406 | $blockcount = count_records("blocks"); |
407 | //If there isn't records. This is the first install, so I remember it |
408 | if ($blockcount == 0) { |
409 | $first_install = true; |
410 | } else { |
411 | $first_install = false; |
412 | } |
413 | |
414 | $site = get_site(); |
415 | |
416 | if (!$blocks = get_list_of_plugins("blocks") ) { |
417 | error("No blocks installed!"); |
418 | } |
419 | |
420 | @include_once($CFG->dirroot."/blocks/moodleblock.class.php"); |
421 | if(!class_exists('moodleblock')) { |
422 | error('Class MoodleBlock is not defined or file not found for /blocks/moodleblock.class.php'); |
423 | } |
424 | |
425 | foreach ($blocks as $blockname) { |
426 | |
427 | if ($blockname == "NEWBLOCK" || $blockname == 'db') { // Someone has unzipped the template, ignore it |
428 | continue; |
429 | } |
430 | |
431 | $fullblock = "$CFG->dirroot/blocks/$blockname"; |
432 | |
433 | if ( is_readable($fullblock."/block_".$blockname.".php")) { |
434 | include_once($fullblock."/block_".$blockname.".php"); |
435 | } else { |
436 | $notices[] = "Block $blockname: ".$fullblock."/block_".$blockname.".php was not readable"; |
437 | continue; |
438 | } |
439 | |
440 | if ( is_readable("$fullblock/db/$CFG->dbtype.php")) { |
441 | include_once("$fullblock/db/$CFG->dbtype.php"); # defines upgrading function |
442 | } else { |
443 | $notices[] ="Block $blockname: $fullblock/db/$CFG->dbtype.php was not readable"; |
444 | continue; |
445 | } |
446 | |
447 | $classname = 'CourseBlock_'.$blockname; |
448 | if(!class_exists($classname)) { |
449 | $notices[] = "Block $blockname: $classname not implemented"; |
450 | continue; |
451 | } |
452 | |
453 | // Let's see if it supports some basic methods |
454 | $methods = get_class_methods($classname); |
455 | if(!in_array(strtolower($classname), $methods)) { |
456 | // No constructor |
457 | $notices[] = "Block $blockname: class does not have a constructor"; |
458 | $invalidblocks[] = $blockname; |
459 | continue; |
460 | } |
461 | |
462 | unset($block); |
463 | |
464 | $blockobj = New $classname($site); |
465 | |
466 | // Inherits from MoodleBlock? |
467 | if(!is_subclass_of($blockobj, "moodleblock")) { |
468 | $notices[] = "Block $blockname: class does not inherit from MoodleBlock"; |
469 | continue; |
470 | } |
471 | |
472 | // OK, it's as we all hoped. For further tests, the object will do them itself. |
473 | if(!$blockobj->_self_test()) { |
474 | $notices[] = "Block $blockname: self test failed"; |
475 | continue; |
476 | } |
477 | $block->version = $blockobj->get_version(); |
478 | |
479 | if (!isset($block->version)) { |
480 | $notices[] = "Block $blockname: hasn't version support"; |
481 | continue; |
482 | } |
483 | |
484 | $block->name = $blockname; // The name MUST match the directory |
485 | $blocktitle = $blockobj->get_title(); |
486 | |
487 | if ($currblock = get_record("blocks", "name", $block->name)) { |
488 | if ($currblock->version == $block->version) { |
489 | // do nothing |
490 | } else if ($currblock->version < $block->version) { |
491 | if (empty($updated_blocks)) { |
492 | $strblocksetup = get_string("blocksetup"); |
493 | print_header($strblocksetup, $strblocksetup, $strblocksetup, "", "", false, " ", " "); |
494 | } |
495 | print_heading("$block->name block needs upgrading"); |
496 | $upgrade_function = $block->name."_upgrade"; |
497 | if (function_exists($upgrade_function)) { |
498 | $db->debug=true; |
499 | if ($upgrade_function($currblock->version, $block)) { |
500 | $db->debug=false; |
501 | // OK so far, now update the blocks record |
502 | $block->id = $currblock->id; |
503 | if (! update_record("blocks", $block)) { |
504 | error("Could not update block $block->name record in blocks table!"); |
505 | } |
506 | notify(get_string('blocksuccess', '', $blocktitle), 'green'); |
507 | echo "<HR>"; |
508 | } else { |
509 | $db->debug=false; |
510 | notify("Upgrading block $block->name from $currblock->version to $block->version FAILED!"); |
511 | } |
512 | } |
513 | $updated_blocks = true; |
514 | } else { |
515 | error("Version mismatch: block $block->name can't downgrade $currblock->version -> $block->version !"); |
516 | } |
517 | |
518 | } else { // block not installed yet, so install it |
519 | |
520 | // [pj] Normally this would be inline in the if, but we need to |
521 | // check for NULL (necessary for 4.0.5 <= PHP < 4.2.0) |
522 | $conflictblock = array_search($blocktitle, $blocktitles); |
523 | if($conflictblock !== false && $conflictblock !== NULL) { |
524 | |
525 | // Duplicate block titles are not allowed, they confuse people |
526 | // AND PHP's associative arrays ;) |
527 | error('<strong>Naming conflict</strong>: block <strong>'.$block->name.'</strong> has the same title with an existing block, <strong>'.$conflictblock.'</strong>!'); |
528 | } |
529 | if (empty($updated_blocks)) { |
530 | $strblocksetup = get_string("blocksetup"); |
531 | print_header($strblocksetup, $strblocksetup, $strblocksetup, "", "", false, " ", " "); |
532 | } |
533 | print_heading($block->name); |
534 | $updated_blocks = true; |
535 | $db->debug = true; |
536 | set_time_limit(0); // To allow slow databases to complete the long SQL |
537 | if (modify_database("$fullblock/db/$CFG->dbtype.sql")) { |
538 | $db->debug = false; |
539 | if ($block->id = insert_record('blocks', $block)) { |
540 | notify(get_string('blocksuccess', '', $blocktitle), 'green'); |
541 | echo "<HR>"; |
542 | } else { |
543 | error("$block->name block could not be added to the block list!"); |
544 | } |
545 | } else { |
546 | error("Block $block->name tables could NOT be set up successfully!"); |
547 | } |
548 | } |
549 | |
550 | $blocktitles[$block->name] = $blocktitle; |
551 | } |
552 | |
553 | if(!empty($notices)) { |
554 | foreach($notices as $notice) { |
555 | notify($notice); |
556 | } |
557 | } |
558 | |
559 | //Finally, if we are in the first_install, update every course blockinfo field with |
560 | //default values. |
561 | if ($first_install) { |
562 | //Iterate over each course |
563 | if ($courses = get_records("course")) { |
564 | foreach ($courses as $course) { |
565 | //Dependig of the format, insert some values |
566 | if ($course->format == "social") { |
567 | $blockinfo = blocks_get_default_blocks ($course->id, "participants,search_forums,calendar_month,calendar_upcoming,social_activities,recent_activity,admin,course_list"); |
568 | } else { |
569 | //For topics and weeks formats (default built in the function) |
570 | $blockinfo = blocks_get_default_blocks($course->id); |
571 | } |
572 | if ($CFG->debug) { |
573 | echo 'Updating blockinfo for course: '.$course->shortname.'('.$blockinfo.')<br>'; |
574 | } |
575 | } |
576 | } |
577 | } |
578 | |
579 | if (!empty($updated_blocks)) { |
580 | print_continue($continueto); |
581 | die; |
582 | } |
583 | } |
584 | |
585 | //This function returns the number of courses currently using the block |
586 | function blocks_get_courses_using_block_by_id($blockid) { |
587 | |
588 | $num = 0; |
589 | |
590 | if ($courses = get_records("course")) { |
591 | foreach($courses as $course) { |
592 | $blocks = str_replace(":",",",$course->blockinfo); |
593 | $blocksarr = explode(",",$blocks); |
594 | if (block_find($blockid,$blocksarr,array())) { |
595 | $num++; |
596 | } |
597 | } |
598 | } |
599 | |
600 | return $num; |
601 | } |
602 | |
603 | //This function hides a block in all courses using it |
604 | function blocks_update_every_block_by_id($blockid,$action) { |
605 | |
606 | if ($courses = get_records("course")) { |
607 | foreach($courses as $course) { |
608 | //Calculate left and right blocks |
609 | $blocks = $course->blockinfo; |
610 | $delimpos = strpos($blocks, ':'); |
611 | |
612 | if($delimpos === false) { |
613 | // No ':' found, we have all left blocks |
614 | $leftblocks = explode(',', $blocks); |
615 | $rightblocks = array(); |
616 | } else if($delimpos === 0) { |
617 | // ':' at start of string, we have all right blocks |
618 | $blocks = substr($blocks, 1); |
619 | $leftblocks = array(); |
620 | $rightblocks = explode(',', $blocks); |
621 | } |
622 | else { |
623 | // Both left and right blocks |
624 | $leftpart = substr($blocks, 0, $delimpos); |
625 | $rightpart = substr($blocks, $delimpos + 1); |
626 | $leftblocks = explode(',', $leftpart); |
627 | $rightblocks = explode(',', $rightpart); |
628 | } |
629 | |
630 | switch($action) { |
631 | case 'show': |
632 | $block = block_find($blockid, $leftblocks, $rightblocks); |
633 | if($block !== false) { |
634 | if($block->side == BLOCK_LEFT) { |
635 | $leftblocks[$block->position] = abs($leftblocks[$block->position]); |
636 | } |
637 | else { |
638 | $rightblocks[$block->position] = abs($rightblocks[$block->position]); |
639 | } |
640 | } |
641 | break; |
642 | case 'hide': |
643 | $block = block_find($blockid, $leftblocks, $rightblocks); |
644 | if($block !== false) { |
645 | if($block->side == BLOCK_LEFT) { |
646 | $leftblocks[$block->position] = -abs($leftblocks[$block->position]); |
647 | } |
648 | else { |
649 | $rightblocks[$block->position] = -abs($rightblocks[$block->position]); |
650 | } |
651 | } |
652 | break; |
653 | case 'delete': |
654 | $block = block_find($blockid, $leftblocks, $rightblocks); |
655 | if($block !== false) { |
656 | if($block->side == BLOCK_LEFT) { |
657 | unset($leftblocks[$block->position]); |
658 | } |
659 | else { |
660 | unset($rightblocks[$block->position]); |
661 | } |
662 | } |
663 | break; |
664 | } |
665 | $course->blockinfo = implode(',', $leftblocks).':'.implode(',',$rightblocks); |
666 | set_field('course', 'blockinfo', $course->blockinfo, 'id', $course->id); |
667 | } |
668 | } |
669 | } |
670 | |
671 | // [pj] I didn't like the block_get_X_by_Y() functions because |
672 | // we should be able to do without them with clever coding, |
673 | // so I set out to see if they could be removed somehow. |
674 | // Only block_get_default_blocks() depends on them, and that |
675 | // one is used nowhere at the moment. So I 'm commenting |
676 | // them out until a use IS found. |
677 | // [el] Uncommented to be used in the installation process, when |
678 | // inserting new courses and when restoring courses. Perhaps |
679 | // they can be modified, but previously related processes |
680 | // will use them since now. |
681 | |
682 | //This function returns the id of the block, searching it by name |
683 | function block_get_id_by_name ($blockname) { |
684 | |
685 | if ($block = get_record("blocks","name",$blockname)) { |
686 | return $block->id; |
687 | } else { |
688 | return 0; |
689 | } |
690 | } |
691 | |
692 | //This function returns the name of the block, searching it by id |
693 | function block_get_name_by_id ($blockid) { |
694 | |
695 | if ($block = get_record("blocks","id",$blockid)) { |
696 | return $block->name; |
697 | } else { |
698 | return NULL; |
699 | } |
700 | } |
701 | |
702 | //This function return the necessary contents to update course->blockinfo |
703 | //with default values. It accepts a list of block_names as parameter. They |
704 | //will be converted to their blockids equivalent. If a course is specified |
705 | //then the function will update the field too! |
706 | |
707 | function blocks_get_default_blocks ($courseid = NULL, $blocknames="participants,activity_modules,calendar_month,calendar_upcoming,search_forums,admin,course_list:news_items,recent_activity") { |
708 | |
709 | //Calculate left and right blocks |
710 | $blocksn = $blocknames; |
711 | $delimpos = strpos($blocksn, ':'); |
712 | |
713 | if($delimpos === false) { |
714 | // No ':' found, we have all left blocks |
715 | $leftblocksn = explode(',', $blocksn); |
716 | $rightblocksn = array(); |
717 | } else if($delimpos === 0) { |
718 | // ':' at start of string, we have all right blocks |
719 | $blocksn = substr($blocksn, 1); |
720 | $leftblocksn = array(); |
721 | $rightblocksn = explode(',', $blocksn); |
722 | } |
723 | else { |
724 | // Both left and right blocks |
725 | $leftpartn = substr($blocksn, 0, $delimpos); |
726 | $rightpartn = substr($blocksn, $delimpos + 1); |
727 | $leftblocksn = explode(',', $leftpartn); |
728 | $rightblocksn = explode(',', $rightpartn); |
729 | } |
730 | |
731 | //Now I have blocks separated |
732 | |
733 | $leftblocks = array(); |
734 | $rightblocks = array(); |
735 | |
736 | if ($leftblocksn) { |
737 | foreach($leftblocksn as $leftblockn) { |
738 | //Convert blockname to id |
739 | $leftblock = block_get_id_by_name($leftblockn); |
740 | if ($leftblock) { |
741 | //Check it's visible |
742 | if($block = get_record("blocks","id",$leftblock,"visible","1")) { |
743 | $leftblocks[] = $leftblock; |
744 | } |
745 | } |
746 | } |
747 | } |
748 | |
749 | if ($rightblocksn) { |
750 | foreach($rightblocksn as $rightblockn) { |
751 | //Convert blockname to id |
752 | $rightblock = block_get_id_by_name($rightblockn); |
753 | if ($rightblock) { |
754 | //Check it's visible |
755 | if($block = get_record("blocks","id",$rightblock,"visible","1")) { |
756 | $rightblocks[] = $rightblock; |
757 | } |
758 | } |
759 | } |
760 | } |
761 | |
762 | //Calculate the blockinfo field |
763 | if ($leftblocks || $rightblocks) { |
764 | $blockinfo = ''; |
765 | if ($leftblocks) { |
766 | $blockinfo .= implode(",", $leftblocks); |
767 | } |
768 | if ($rightblocks) { |
769 | $blockinfo .= ':'.implode(",",$rightblocks); |
770 | } |
771 | } else { |
772 | $blockinfo = ''; |
773 | } |
774 | |
775 | //If a course has been specified, update it |
776 | if ($courseid) { |
777 | set_field('course', "blockinfo", $blockinfo, "id", $courseid); |
778 | } |
779 | |
780 | //Returns the blockinfo |
781 | return $blockinfo; |
782 | } |
783 | |
784 | ?> |