templatemigration.php 27 KB
Newer Older
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
<?php
/**
 * This program is part of Mahara
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
 * @subpackage core
 * @author     Penny Leach <penny@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

defined('INTERNAL') || die();
require_once('view.php');
29
safe_require('blocktype', 'textbox'); // need this for labels, always
30
31
32
safe_require('artefact', 'internal'); // needed for blocktype creation
safe_require('artefact', 'resume');   // ditto

33

34
35
36
37
/**
 * Performs the template migration, from the old view template style of 0.8 to 
 * the new views format for 0.9
 */
38
function upgrade_template_migration() {
39
    log_debug('upgrade_template_migration()');
40

41
42
    if (!$views = get_records_array('view', '', '', 'id')) {
        log_debug('no views to migrate');
43
44
        return;
    }
45
46
    log_debug('we have ' . count($views) . ' views to migrate...');

47

48
49
50
    //
    // Static data needed by various templates when they are migrated
    //
51

52
53
54
55
56
    // blogreflection
    $blogreflection_numbers = array(1 => 'One', 2 => 'One', 3 => 'Two', 4 => 'Two', 5 => 'Three', 6 => 'Three');

    // ppae
    $ppae_text = array(
57
58
59
60
61
62
63
64
        '<h4>Group Name</h4>', 
        '<h4>Student Names</h4>', 
        '<h4>Mission and Vision Statement (Concept and Concept Outline)</h4>', 
        '<h4>Physical Design of the space</h4><p>Explain, using theorists, how you created ambeince and support of holistic learning for each child</p>', 
        '<h4>Schedule or timetable of tasks and events prior to opening</h4>', 
        '<h4>Curriculum Matrix for first six weeks, specific activities for beginners, intermediates, end-game</h4><p>provide NZCF AO/level cross-reference</p>', 
        '<h4>Yearlong Teaching and Learning Topics plan for beginners, intermediates and end-game.</h4><p>Justify your choices by citing educationalists, psychologists and other theorists</p>', 
        '<h4>Priorised table of equipment, materials and supplies</h4>'
65
66
    );

67
68
69

    // The main view migration loop
    $count = 0;
70
    foreach ($views as $view) {
71
72
73
74
75
76
77
78
79
80
81
82
        $count++;
        log_debug("Migrating view $view->id ($count)");
        if ($view->template == 'test') {
            // We are NOT migrating test templates. There's not much point. The 
            // only real data loss from this is the labels, which are only one 
            // line text inputs. If this annoys someone they could always have 
            // a go at writing a migration. There's no technical reason why one 
            // couldn't be done.
            log_debug("View $view->id is a test view. Deleting instead");
            $view = new View($view->id);
            delete_records('view_content', 'view', $view->get('id'));
            $view->delete();
83
84
85
            continue;
        }

86
87
88
89
90
91
92
93
94
95
96
97
98
99
        //
        // $viewcolumns is a datastructure representing the new structure of a 
        // view. It's an array of columns, which are arrays of label titles etc.
        // All the values are null.
        //
        // We are trying to turn the nulls into blockinstances
        //
        if (!$viewcolumns = upgrade_template_get_structure($view->template)) {
            log_warn("Unsupported custom template $view->template! Deleting!");
            $view = new View($view->id);
            delete_records('view_content', 'view', $view->get('id'));
            $view->delete();
            continue;
        }
100
101
        $numcolumns = count($viewcolumns);

102
103
104
105
        // Handle all label blocks, by converting them to WYSIWYG blocks
        if ($lblocks = get_records_array('view_content', 'view', $view->id)) {
            foreach ($lblocks as $block) {
                upgrade_template_insert_block($viewcolumns, $block->block, upgrade_template_create_wysiwyg($block->content, $view->id));
106
            }
107
108
109
110
111
112
113
114
115
116
117
        }

        // Get all artefact blocks in the view
        if ($ablocks = get_records_sql_array('
            SELECT va.*, a.artefacttype, a.title
            FROM {view_artefact} va
            INNER JOIN {artefact} a ON (va.artefact = a.id)
            WHERE "view" = ?', array($view->id))) {
            foreach ($ablocks as $block) {
                if (!upgrade_template_block_exists($viewcolumns, $block->oldblock)) {
                    // There's no block here. We can make one and insert it
118
                    $bi = upgrade_template_convert_block_to_blockinstance($block, $view);
119
120
121
122
123
124
125
                    // note: the location to insert the block is known as 
                    // 'oldblock', as the column as been renamed in the database 
                    // in preparation for its removal
                    upgrade_template_insert_block($viewcolumns, $block->oldblock, $bi);
                }
                else {
                    // We're dealing with multiple artefacts in the same block... just need to append
126
                    upgrade_template_update_block($viewcolumns, $block, $view);
127
                }
128
129
130
            }
        }

131
132
133
134
        //
        // Special case code for each template, to make sure the labels and 
        // hard coded text are migrated nicely
        //
135
        if ($view->template == 'blogreflection') {
136
137
            // There are six labels in this view - the three reflection titles 
            // and three 'last date available' fields
138
            for ($i = 1; $i <= 6; $i++) {
139
140
                // The labels were badly numbered, 2, 4 and 5 are the right 
                // hand side (and thus the 'last date available' ones)
141
                if (in_array($i, array(2, 4, 5))) {
142
143
                    $column = 1;
                    $text = '<h4>Reflection ' . $blogreflection_numbers[$i] . ': Last Date Available</h4>';
144
145
                }
                else {
146
147
                    $column = 0;
                    $text = '<h3>Reflection ' . $blogreflection_numbers[$i] . ' Title</h3>'; 
148
149
                }

150
151
152
                // The label migration would have put WYSIWYG blocks in where 
                // there was content. If there is none, let's ignore this block
                if (empty($viewcolumns[$column]['tpl_label' . $i])) {
153
154
155
                    continue;
                }

156
                $labelkey = 'tpl_label' . $i;
157
158
159
160
161
                $replace = $text . '<p>' . upgrade_template_get_wysiwyg_content($viewcolumns, $column, 'tpl_label' . $i) . '</p>';

                // Update the WYSIWYG blocks to have the titles they need
                if (in_array($i, array(2, 4, 5))) {
                    upgrade_template_update_wysiwyg($viewcolumns, $column, $labelkey, null, $replace);
162
163
                }
                else {
164
                    upgrade_template_update_wysiwyg($viewcolumns, $column, 'tpl_label' . $i, null, $replace);
165
166
167
168
169
                }
            }
        }
        else if ($view->template == 'PPAE') {
            if (!empty($viewcolumns[0]['tpl_label1'])) {
170
171
                log_debug('tpl_label1 is not empty, assuming it is a wysiwyg and updating its content');
                upgrade_template_update_wysiwyg($viewcolumns, 0, 'tpl_label1', null, '<h4>' . $ppae_text[0] . '</h4>' . upgrade_template_get_wysiwyg_content($viewcolumns, 0, 'tpl_label1'));
172
173
            }
            if (!empty($viewcolumns[0]['tpl_label2']) || !empty($viewcolumns[0]['tpl_label3']) || !empty($viewcolumns[0]['tpl_label4']) || !empty($viewcolumns[0]['tpl_label5'])) {
174
                // mash it all into the first one and unset the rest 
175
176
177
                log_debug('assuming tpl_label2 is a wysiwyg');
                $label2_text = '<h4>' . $ppae_text[1] . '</h4>'
                    . upgrade_template_get_wysiwyg_content($viewcolumns, 0, 'tpl_label2') . '<br>' 
178
179
                    . upgrade_template_get_wysiwyg_content($viewcolumns, 0, 'tpl_label3') . '<br>' 
                    . upgrade_template_get_wysiwyg_content($viewcolumns, 0, 'tpl_label4') . '<br>'
180
181
182
183
184
185
186
187
188
189
                    . upgrade_template_get_wysiwyg_content($viewcolumns, 0, 'tpl_label5');

                $label2_text = preg_replace('#(<br>)+$#', '', $label2_text);

                if (upgrade_template_block_exists($viewcolumns, 'tpl_label2')) {
                    upgrade_template_update_wysiwyg($viewcolumns, 0, 'tpl_label2', null, $label2_text);
                }
                else {
                    upgrate_template_insert_block($viewcolumns, 'tpl_label2', upgrade_template_create_wysiwyg($label2_text, $view->id));
                }
190
191
192
193
194
                unset($viewcolumns[0]['tpl_label3']);
                unset($viewcolumns[0]['tpl_label4']);
                unset($viewcolumns[0]['tpl_label5']);
            }
            if (!empty($viewcolumns[0]['tpl_blog1'])) {
195
                $viewcolumns[0]['tpl_converted1'] = upgrade_template_create_wysiwyg($ppae_text[2], $view->id);
196
197
            }
            if (!empty($viewcolumns[0]['tpl_files1']) || !empty($viewcolumns[0]['tpl_blog2'])) {
198
                $viewcolumns[0]['tpl_converted2'] = upgrade_template_create_wysiwyg($ppae_text[3], $view->id);
199
200
            }
            if (!empty($viewcolumns[0]['tpl_files2']) || !empty($viewcolumns[0]['tpl_blog3'])) {
201
                $viewcolumns[0]['tpl_converted3'] = upgrade_template_create_wysiwyg($ppae_text[4], $view->id);
202
203
            }
            if (!empty($viewcolumns[0]['tpl_files3']) || !empty($viewcolumns[0]['tpl_blog4'])) {
204
                $viewcolumns[0]['tpl_converted4'] = upgrade_template_create_wysiwyg($ppae_text[5], $view->id);
205
206
            }
            if (!empty($viewcolumns[0]['tpl_files4']) || !empty($viewcolumns[0]['tpl_blog5'])) {
207
                $viewcolumns[0]['tpl_converted5'] = upgrade_template_create_wysiwyg($ppae_text[6], $view->id);
208
209
            }
            if (!empty($viewcolumns[0]['tpl_files5']) || !empty($viewcolumns[0]['tpl_blog6'])) {
210
                $viewcolumns[0]['tpl_converted6'] = upgrade_template_create_wysiwyg($ppae_text[7], $view->id);
211
212
213
            }
        }
        
214
215
216
217
218
219
220
221
222
223
224
225
226
        // Clean up empty columns, although the gallery should always have its 
        // five columns
        if ($view->template != 'gallery') {
            foreach ($viewcolumns as $c => $col) {
                $empty = true;
                foreach ($col as $key => $guff) {
                    if (!empty($guff)) {
                        $empty = false;
                    }
                }
                if ($empty) {
                    $numcolumns--;
                    unset($viewcolumns[$c]);
227
228
229
                }
            }
        }
230
231

        // Make all the block instances have the correct column and order 
232
        foreach ($viewcolumns as $c => $col) {
233
234
            $order = 1;
            foreach ($col as $key => $block) {
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
                if (is_array($block)) {
                    foreach ($block as $bi) {
                        $bi->set('column', ($c + 1));
                        $bi->set('order', $order);
                        $bi->commit();
                        $order++;
                    }
                }
                else {
                    if ($block instanceof BlockInstance) {
                        $block->set('column', ($c + 1));
                        $block->set('order', $order);
                        $block->commit();
                        $order++;
                    }
250
                }
251
252
253
            }
        }

254
255
256
257
        // Work out what layout to set the view to
        $layout = upgrade_template_get_view_layout($view->template);

        // Commit the view!
258
259
        $view = new View($view->id);
        $view->set('numcolumns', $numcolumns);
260
261
262
        if ($layout) {
            $view->set('layout', $layout);
        }
263
        $view->commit();
264
    } // End of the view loop
265
266
}

267
/**
268
* Puts blockinstances into the appropriate place in the deeply nested array
269
270
*
* @param array (reference) $columns column structure
271
272
* @param string            $key     key to insert blockinstance at
* @param BlockInstance     $bi      blockinstance to insert
273
*/
274
function upgrade_template_insert_block(&$columns, $key, BlockInstance $bi) {
275
276
277
278
279
280
281
    foreach ($columns as &$c) {
        if (array_key_exists($key, $c)) {
            if (!empty($c[$key])) {
                $e = new TemplateBlockExistsAlreadyException();
                $e->set_block_data($c[$key]);
                throw $e;
            }
282
            $c[$key] = $bi;
283
284
285
286
287
            return;
        }
    }
}

288
289
290
291
292
293
294
295
296
/**
 * Takes a block that, according to its oldblock setting, wants to be inserted 
 * somewhere where there is an existing blockinstance.  With this information, 
 * establishes how to change the existing blockinstance, or what to replace it 
 * with, so that both artefacts are in the same blockinstance.
 *
 * @param array (reference) $columns column structure
 * @param stdClass          $block   the new block data
 */
297
function upgrade_template_update_block(&$columns, $block, $view) {
298
299
300
    // $block->oldblock is where the existing blockinstance is
    // Then we need to establish what to put in $columns, or in the blockinstance
    $bi = null;
301
    $column = null;
302
303
    foreach ($columns as &$c) {
        if (array_key_exists($block->oldblock, $c)) {
304
            $column =& $c;
305
306
            if (!empty($c[$block->oldblock])) {
                $bi = $c[$block->oldblock];
307
308
309
310
311
                // Will be an array if there's more than one blockinstance here. 
                // See the 'else' clause below
                if (is_array($bi)) {
                    $bi = $bi[0];
                }
312
            }
313
            break;
314
315
316
317
318
319
320
321
322
323
324
325
        }
    }

    if (empty($bi)) {
        log_debug("WTF: tried to update a block when there was nothing there to update");
        return;
    }

    // If the blockinstance is a filedownload block and we have a file or image 
    // to add, add it directly to the blockinstance
    if ($bi->get('blocktype') == 'filedownload') {
        if ($block->artefacttype == 'file' || $block->artefacttype == 'image') {
326
327
            upgrade_template_add_artefact_to_blockinstance($bi, $block->artefact);
        }
328
329
        // More than one artefact in this filedownload block, so remove the title
        $bi->set('title', '');
330
331
332
333
334
335
336
337
338
339
    }
    // If the blockinstance is profileinfo, we can keep adding profile fields to it
    else if ($bi->get('blocktype') == 'profileinfo') {
        if (in_array($block->artefacttype, PluginArtefactInternal::get_artefact_types())) {
            if ($block->artefacttype != 'profileicon') {
                upgrade_template_add_artefact_to_blockinstance($bi, $block->artefact);
            }
            else {
                // The profileicon is stored in a dedicated field, not 'artefactids'
                $configdata = $bi->get('configdata');
340
                $configdata['profileicon'] = $block->artefact;
341
342
                $bi->set('configdata', $configdata);
            }
343
344
        }
    }
345
346
347
348
349
    else if ($bi->get('blocktype') == 'recentposts') {
        if ($block->artefacttype == 'blog') {
            upgrade_template_add_artefact_to_blockinstance($bi, $block->artefact);
        }
    }
350
351
352
353
354
355
356
357
    else {
        // Make a new blockinstance and insert it below
        $bi = upgrade_template_convert_block_to_blockinstance($block, $view);
        if (!is_array($column[$block->oldblock])) {
            $column[$block->oldblock] = array($column[$block->oldblock]);
        }
        $column[$block->oldblock] = array_merge(array($bi), $column[$block->oldblock]);
    }
358
}
359

360
361
362
363
364
365
366
367
368
/**
 * Given a blockinstance that is assumed to have an 'artefactids' config field 
 * and an artefact, ensures that the blockinstance config includes the given 
 * artefact ID
 */
function upgrade_template_add_artefact_to_blockinstance(BlockInstance $bi, $artefact) {
    $configdata = $bi->get('configdata');
    $configdata['artefactids'][] = $artefact;
    $bi->set('configdata', $configdata);
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
}

/**
 * Determines whether a block already exists at the given location
 *
 * @param array (reference) $columns column structure
 * @param string            $key     key to check for existance
 */
function upgrade_template_block_exists(&$columns, $key) {
    foreach ($columns as &$c) {
        if (array_key_exists($key, $c)) {
            if (!empty($c[$key])) {
                return true;
            }
        }
    }
    return false;
}

/**
 * Creates a WYSIWYG blockinstance
 *
 * @param string $content The content for the wysiwyg block
 * @param int $view The view the new block will be in
 */
394
395
function upgrade_template_create_wysiwyg($content, $view) {
    $b = new BlockInstance(0, array(
396
397
        'title'      => '',
        'blocktype'  => 'textbox',
398
        'configdata' => serialize(array('text' => $content)),
399
        'view'       => $view,
400
401
402
403
404
    ));

    return $b;
}

405
406
407
/**
 * Given a location with a WYSIWYG blockinstance, either appends or replaces its content
 */
408
function upgrade_template_update_wysiwyg(&$columns, $column, $key, $appendcontent=null, $replacecontent=null) {
409
    $block = $columns[$column][$key];
410
411
412
413
414
415
416
417
418
419
    $data = $block->get('configdata');
    if (!empty($appendcontent)) {
        $data['text'] .= $appendcontent;
    }
    else {
        $data['text'] = $replacecontent;
    }
    $block->set('configdata', $data);
}

420
/**
421
422
 * Gets content of an existing WYSIWYG blockinstance, or an empty string if the 
 * block is empty
423
 */
424
425
function upgrade_template_get_wysiwyg_content($columns, $column, $key) {
    $block = $columns[$column][$key];
426
427
428
    if (empty($block)) {
        return '';
    }
429
430
    $data = $block->get('configdata');
    return $data['text'];
431
}
432

433
434
435
436
437
438
/**
 * Get the new view structure for the given template.
 *
 * @param string $template The template to get the structure for
 * @return array
 */
439
function upgrade_template_get_structure($template) {
440
441
442
443
444
445

    static $columnstructure;
    if (empty($columnstructure)) {

        $columnstructure = array(
            'blogandprofile' => array(
446
                // First column
447
                array(
448
                    // Each thing in the column, from top to bottom
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
                    'tpl_blogslabel' => null,
                    'tpl_blog1'      => null,
                    'tpl_blog2'      => null
                ),
                array(
                    'tpl_profilelabel' => null,
                    'tpl_profile'      => null,
                ),
            ),
            'blogreflection' => array( 
                array(
                    'tpl_label1' => null,
                    'tpl_blog1'  => null,
                    'tpl_label3' => null, 
                    'tpl_blog2'  => null,
                    'tpl_label6' => null,
465
                    'tpl_blog3'  => null,
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
                ), 
                array(
                    'tpl_label2' => null,
                    'tpl_label4' => null,
                    'tpl_label5' => null,
                ), // I know it looks like 5 and 6 are backwards, it's in the template html
            ),
            'filelist' => array(
                array(
                    'tpl_fileslabel1' => null,
                    'tpl_files1'      => null,
                    'tpl_fileslabel2' => null,
                    'tpl_files2'      => null,
                    'tpl_fileslabel3' => null,
                    'tpl_files3'      => null,
                ),
                array(
                    'tpl_freelabel' => null
                ),
            ),
            'gallery' => array(
                array(
                    'tpl_image1' => null,
                    'tpl_label1' => null,
                    'tpl_image6' => null,
                    'tpl_label6' => null,
                ),
                array(
                    'tpl_image2' => null,
                    'tpl_label2' => null, 
                    'tpl_image7' => null,
                    'tpl_label7' => null,
                ),
                array(
                    'tpl_image3' => null,
                    'tpl_label3' => null,
                    'tpl_image8' => null,
                    'tpl_label8' => null,
                ),
                array(
                    'tpl_image4' => null,
                    'tpl_label4' => null,
                    'tpl_image9' => null,
                    'tpl_label9' => null,
                ),
                array(
                    'tpl_image5'  => null,
                    'tpl_label5'  => null,
                    'tpl_image10' => null,
                    'tpl_label10' => null,
                ),
            ), 
            'generaltemplate' => array(
                array(
                    'tpl_label'    => null,
                    'tpl_generic1' => null,
                    'tpl_generic2' => null,
                    'tpl_generic3' => null,
                    'tpl_generic4' => null,
                ),
            ),
            'professionalprofile' => array( 
                array(
                    'tpl_firstname' => null,
                    'tpl_generic4'  => null,
                    'tpl_generic1'  => null,
                    'tpl_label1'    => null,
                    'tpl_generic2'  => null,
                ),
                array(
                    'tpl_lastname' => null,
                    'tpl_image'    => null,
                    'tpl_label2'   => null,
                    'tpl_generic3' => null,
                ),
            ),
            'PPAE' => array( 
                array(
                    // Free text here (Group Name)
                    'tpl_label1' => null,
                    // Free text here (Student Names)
                    'tpl_label2' => null,
                    'tpl_label3' => null,
                    'tpl_label4' => null, 
                    'tpl_label5' => null,
                    // Free text here (Mission.. )
                    'tpl_converted1' => null,
                    'tpl_blog1'  => null,
                    // Free text here (Physical Design.. )
                    'tpl_converted2' => null,
                    'tpl_files1' => null,
                    'tpl_blog2'  => null, 
                    // Free text here (Schedule or timetable.. )
                    'tpl_converted3' => null,
                    'tpl_files2' => null,
                    'tpl_blog3'  => null, 
                    // Free text here (Curriculum Matrix.. )
                    'tpl_converted4' => null,
                    'tpl_files3' => null,
                    'tpl_blog4'  => null,
                    // Free text here (Yearlong teaching.. )
                    'tpl_converted5' => null,
                    'tpl_files4' => null,
                    'tpl_blog5'  => null,
                    // Free text here (Prioritised table)
                    'tpl_converted6' => null,
                    'tpl_files5' => null,
                    'tpl_blog6'  => null
                ),
            ),
        );
    }

    if (!array_key_exists($template, $columnstructure)) {
        return false;
    }
    return $columnstructure[$template];
}

585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/**
 * Select what view layout the template should be migrated to
 */
function upgrade_template_get_view_layout($template) {
    if ($template == 'blogreflection') {
        return 2; // 67/33
    }
    return null;
}

/**
 * Given a record from the view_artefact table (otherwise known as a "block"), 
 * try and establish a blockinstance that it could be under the new system and 
 * return it
 */
function upgrade_template_convert_block_to_blockinstance($block, $view) {
    if ($block->artefacttype == 'blogpost') {
        $bi = new BlockInstance(0, array(
            'title' => $block->title,
            'blocktype' => 'blogpost',
            'configdata' => serialize(array('artefactid' => $block->artefact)),
606
            'view' => $view->id,
607
608
609
610
        ));
        return $bi;
    }
    else if ($block->artefacttype == 'blog') {
611
612
613
614
615
616
617
618
619
        if ($block->format == 'listself') {
            $blocktype = 'recentposts';
            $configdata = array('artefactids' => array($block->artefact));
        }
        else {
            $blocktype = 'blog';
            $configdata = array('artefactid' => $block->artefact);
        }

620
        $bi = new BlockInstance(0, array(
621
622
623
            'title' => '',
            'blocktype' => $blocktype,
            'configdata' => serialize($configdata),
624
            'view' => $view->id,
625
626
627
628
        ));
        return $bi;
    }
    else if ($block->artefacttype == 'image') {
629
630
631
632
633
634
        if ($block->format == 'listself') {
            $blocktype = 'filedownload';
            $configdata = array('artefactids' => array($block->artefact));
        }
        else {
            $blocktype = 'image';
635
            $configdata = array('artefactid' => $block->artefact, 'width' => ($view->template == 'gallery' ? 175 : 350));
636
        }
637
        $bi = new BlockInstance(0, array(
638
            'title' => '',
639
640
            'blocktype' => $blocktype,
            'configdata' => serialize($configdata),
641
            'view' => $view->id,
642
643
644
645
646
647
648
649
        ));
        return $bi;
    }
    else if ($block->artefacttype == 'file') {
        $bi = new BlockInstance(0, array(
            'title' => $block->title,
            'blocktype' => 'filedownload',
            'configdata' => serialize(array('artefactids' => array($block->artefact))),
650
            'view' => $view->id,
651
652
653
        ));
        return $bi;
    }
654
655
656
657
658
    else if (in_array($block->artefacttype, PluginArtefactResume::get_artefact_types())) {
        $bi = new BlockInstance(0, array(
            'title' => $block->title,
            'blocktype' => 'resumefield',
            'configdata' => serialize(array('artefactid' => $block->artefact)),
659
            'view' => $view->id,
660
661
662
        ));
        return $bi;
    }
663
664
665
666
667
668
669
670
671
672
    else if (in_array($block->artefacttype, PluginArtefactInternal::get_artefact_types())) {
        // This happens in the blogandprofile template, there's a 'listself' internal artefact thing
        $bi = new BlockInstance(0, array(
            'title' => '',
            'blocktype' => 'profileinfo',
            'configdata' => serialize(array('artefactids' => array($block->artefact))),
            'view' => $view->id,
        ));
        return $bi;
    }
673
674
    else if ($block->artefacttype == 'folder') {
        $bi = new BlockInstance(0, array(
675
            'title' => $block->title,
676
677
678
679
680
681
            'blocktype' => 'folder',
            'configdata' => serialize(array('artefactid' => $block->artefact)),
            'view' => $view->id,
        ));
        return $bi;
    }
682

683
684
    log_info("Created a placeholder blockinstance for:");
    log_info($block);
685
686
687
688
    $bi = new BlockInstance(0, array(
        'title' => 'TODO - correct blocktype',
        'blocktype' => 'textbox',
        'configdata' => serialize(array('text' => 'TODO - correct blocktype')),
689
        'view' => $view->id,
690
691
692
693
    ));
    return $bi;
}

694
695
696
697
698
699
700
701
702
703
704
705
class TemplateBlockExistsAlreadyException extends MaharaException {
    
    private $blockdata;

    public function set_block_data($data) {
        $this->blockdata = $data;
    }

    public function get_block_data() {
        return $this->blockdata;
    }
}
706

707
?>