index.php 16.6 KB
Newer Older
Penny Leach's avatar
Penny Leach committed
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
Penny Leach's avatar
Penny Leach committed
6
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * 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 3 of the License, or
 * (at your option) any later version.
Penny Leach's avatar
Penny Leach committed
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * 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.
Penny Leach's avatar
Penny Leach committed
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Penny Leach's avatar
Penny Leach committed
19
20
 *
 * @package    mahara
Penny Leach's avatar
Penny Leach committed
21
 * @subpackage artefact-resume
22
 * @author     Catalyst IT Ltd
Penny Leach's avatar
Penny Leach committed
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
Penny Leach's avatar
Penny Leach committed
25
26
27
28
 *
 */

define('INTERNAL', true);
29
define('MENUITEM', 'profile/myresume');
30
31
32
define('SECTION_PLUGINTYPE', 'artefact');
define('SECTION_PLUGINNAME', 'resume');
define('SECTION_PAGE', 'index');
Penny Leach's avatar
Penny Leach committed
33
34

require_once(dirname(dirname(dirname(__FILE__))) . '/init.php');
35
define('TITLE', get_string('myresume', 'artefact.resume'));
Penny Leach's avatar
Penny Leach committed
36
require_once('pieforms/pieform.php');
37
require_once(get_config('docroot') . 'artefact/lib.php');
Penny Leach's avatar
Penny Leach committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

// load up all the artefacts this user already has....
$coverletter = null;
$personalinformation = null;
$contactinformation = null;
$interest = null;
try {
    $coverletter = artefact_instance_from_type('coverletter');
}
catch (Exception $e) { }
try {
    $personalinformation = artefact_instance_from_type('personalinformation');
}
catch (Exception $e) { }
try {
    $contactinformation = artefact_instance_from_type('contactinformation');
}
catch (Exception $e) { 
    $contactinformation = ArtefactTypeContactinformation::setup_new($USER->get('id'));
}
try {
    $interest = artefact_instance_from_type('interest');
}
catch (Exception $e) { }

63
64
65
$contactinformation_value = $contactinformation->render_self(array('editing' => true));
$contactinformation_value = $contactinformation_value['html'];

Clare Lenihan's avatar
Clare Lenihan committed
66
$coverletterform = pieform(array(
67
    'name'        => 'coverletter',
Penny Leach's avatar
Penny Leach committed
68
69
70
71
72
73
    'jsform'      => true,
    'plugintype'  => 'artefact',
    'pluginname'  => 'resume',
    'jsform'      => true,
    'method'      => 'post',
    'elements'    => array(
Penny Leach's avatar
Penny Leach committed
74
75
76
77
78
79
80
81
        'coverletterfs' => array(
            'type' => 'fieldset',
            'legend' => get_string('coverletter', 'artefact.resume'),
            'elements' => array(
                'coverletter' => array(
                    'type'  => 'wysiwyg',
                    'cols'  => 70,
                    'rows'  => 10,
82
                    'defaultvalue' => ((!empty($coverletter)) ? $coverletter->get('description') : null),
Penny Leach's avatar
Penny Leach committed
83
                    'help' => true,
Penny Leach's avatar
Penny Leach committed
84
                ),
85
86
87
88
                'save' => array(
                    'type' => 'submit',
                    'value' => get_string('save'),
                ),
Penny Leach's avatar
Penny Leach committed
89
            )
90
91
        )
    )
Clare Lenihan's avatar
Clare Lenihan committed
92
));
93

Clare Lenihan's avatar
Clare Lenihan committed
94
$interestsform = pieform(array(
95
96
97
98
99
100
101
    'name'        => 'interests',
    'jsform'      => true,
    'plugintype'  => 'artefact',
    'pluginname'  => 'resume',
    'jsform'      => true,
    'method'      => 'post',
    'elements'    => array(
Penny Leach's avatar
Penny Leach committed
102
103
104
105
106
107
        'interestsfs' => array(
            'type' => 'fieldset',
            'legend' => get_string('interest', 'artefact.resume'),
            'elements' => array(
                'interest' => array(
                    'type' => 'wysiwyg',
108
                    'defaultvalue' => ((!empty($interest)) ? $interest->get('description') : null),
Penny Leach's avatar
Penny Leach committed
109
110
                    'cols'  => 70,
                    'rows'  => 10,
Penny Leach's avatar
Penny Leach committed
111
                    'help'  => true,
Penny Leach's avatar
Penny Leach committed
112
                ),
113
114
115
116
117
118
119
                'save' => array(
                    'type' => 'submit',
                    'value' => get_string('save'),
                ),
            )
        )
    )
Clare Lenihan's avatar
Clare Lenihan committed
120
));
121

Clare Lenihan's avatar
Clare Lenihan committed
122
$contactinformationform = pieform(array(
123
124
125
126
127
128
129
    'name'        => 'contactinformation',
    'jsform'      => true,
    'plugintype'  => 'artefact',
    'pluginname'  => 'resume',
    'jsform'      => true,
    'method'      => 'post',
    'elements'    => array(
Penny Leach's avatar
Penny Leach committed
130
131
132
133
134
        'contactinformationfs' => array(
            'type' => 'fieldset',
            'legend' => get_string('contactinformation', 'artefact.resume'),
            'elements' => array(
                'contactinformation' => array(
Penny Leach's avatar
Penny Leach committed
135
                    'type'  => 'html',
136
                    'value' => $contactinformation_value,
Penny Leach's avatar
Penny Leach committed
137
                    'help'  => true,
Penny Leach's avatar
Penny Leach committed
138
                ),
139
140
141
            )
        )
    )
Clare Lenihan's avatar
Clare Lenihan committed
142
));
143

Clare Lenihan's avatar
Clare Lenihan committed
144
$personalinformationform = pieform(array(
145
146
147
148
149
150
151
    'name'        => 'personalinformation',
    'jsform'      => true,
    'plugintype'  => 'artefact',
    'pluginname'  => 'resume',
    'jsform'      => true,
    'method'      => 'post',
    'elements'    => array(
Penny Leach's avatar
Penny Leach committed
152
153
154
155
156
157
158
159
160
161
162
        'personalinformation' => array(
            'type'        => 'fieldset',
            'legend'      => get_string('personalinformation', 'artefact.resume'),
            'elements'    => array(
               'dateofbirth' => array(
                    'type'       => 'calendar',
                    'caloptions' => array(
                        'showsTime'      => false,
                        'ifFormat'       => '%Y/%m/%d'
                    ),
                    'defaultvalue' => ((!empty($personalinformation)) 
163
                        ? $personalinformation->get_composite('dateofbirth') : null),
Penny Leach's avatar
Penny Leach committed
164
                    'title' => get_string('dateofbirth', 'artefact.resume'),
165
                    'description' => get_string('dateformatguide'),
Penny Leach's avatar
Penny Leach committed
166
                    'help'  => true,
Penny Leach's avatar
Penny Leach committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
                ),
                'placeofbirth' => array(
                    'type' => 'text',
                    'defaultvalue' => ((!empty($personalinformation)) 
                        ? $personalinformation->get_composite('placeofbirth') : null),
                    'title' => get_string('placeofbirth', 'artefact.resume'),
                ),  
                'citizenship' => array(
                    'type' => 'text',
                    'defaultvalue' => ((!empty($personalinformation))
                        ? $personalinformation->get_composite('citizenship') : null),
                    'title' => get_string('citizenship', 'artefact.resume'),
                ),
                'visastatus' => array(
                    'type' => 'text', 
                    'defaultvalue' => ((!empty($personalinformation))
                        ? $personalinformation->get_composite('visastatus') : null),
                    'title' => get_string('visastatus', 'artefact.resume'),
Penny Leach's avatar
Penny Leach committed
185
                    'help'  => true,
Penny Leach's avatar
Penny Leach committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
                ),
                'gender' => array(
                    'type' => 'radio', 
                    'defaultvalue' => ((!empty($personalinformation))
                        ? $personalinformation->get_composite('gender') : null),
                    'options' => array(
                        'female' => get_string('female', 'artefact.resume'),
                        'male'   => get_string('male', 'artefact.resume'),
                    ),
                    'title' => get_string('gender', 'artefact.resume'),
                ),
                'maritalstatus' => array(
                    'type' => 'text',
                    'defaultvalue' => ((!empty($personalinformation))
                        ? $personalinformation->get_composite('maritalstatus') :  null),
                    'title' => get_string('maritalstatus', 'artefact.resume'),
                ),
203
204
205
206
207
208
                'save' => array(
                    'type' => 'submit',
                    'value' => get_string('save'),
                ),
            )
        )
Penny Leach's avatar
Penny Leach committed
209
    )
Clare Lenihan's avatar
Clare Lenihan committed
210
));
Penny Leach's avatar
Penny Leach committed
211
212
213

$cancelstr = get_string('cancel');
$addstr = get_string('add');
214
215
216
$editstr = get_string('edit');
$delstr = get_string('delete');
$confirmdelstr = get_string('compositedeleteconfirm', 'artefact.resume');
217
218
$imagemoveblockup   = json_encode($THEME->get_url('images/move-block-up.png'));
$imagemoveblockdown = json_encode($THEME->get_url('images/move-block-down.png'));
219
220
$upstr = get_string('moveup', 'artefact.resume');
$downstr = get_string('movedown', 'artefact.resume');
221
222

$wwwroot = get_config('wwwroot');
Penny Leach's avatar
Penny Leach committed
223

Clare Lenihan's avatar
Clare Lenihan committed
224
$smarty = smarty(array('tablerenderer'));
225

Clare Lenihan's avatar
Clare Lenihan committed
226
227
228
229
$smarty->assign('coverletterform', $coverletterform);
$smarty->assign('interestsform', $interestsform);
$smarty->assign('contactinformationform', $contactinformationform);
$smarty->assign('personalinformationform',$personalinformationform);
Penny Leach's avatar
Penny Leach committed
230
231
232
233

$inlinejs = <<<EOF
var tableRenderers = {};

234
235
236
function toggleCompositeForm(type) {
    var elemName = ''; 
    elemName = type + 'form';
237
238
    if (hasElementClass(elemName, 'hidden')) {
        removeElementClass(elemName, 'hidden');
239
240
241
242
        $('add' + type + 'button').innerHTML = '{$cancelstr}';
    }
    else {
        $('add' + type + 'button').innerHTML = '{$addstr}';
243
        addElementClass(elemName, 'hidden'); 
244
245
246
    }
}

247
248
function compositeSaveCallback(form, data) {
    key = form.id.substr(3);
249
250
    tableRenderers[key].doupdate(); 
    toggleCompositeForm(key);
251
252
253
254
255
256
257
    // Can't reset() the form here, because its values are what were just submitted, 
    // thanks to pieforms
    forEach(form.elements, function(element) {
        if (hasElementClass(element, 'text') || hasElementClass(element, 'textarea')) {
            element.value = '';
        }
    });
258
259
}

260
function deleteComposite(type, id, artefact) {
261
262
    if (confirm('{$confirmdelstr}')) {
        sendjsonrequest('compositedelete.json.php',
263
            {'id': id, 'artefact': artefact},
264
265
266
267
268
269
270
271
272
            'GET', 
            function(data) {
                tableRenderers[type].doupdate();
            },
            function() {
                // @todo error
            }
        );
    }
273
274
275
    return false;
}

276
277
278
279
280
281
282
283
284
285
286
287
288
289
function moveComposite(type, id, artefact, direction) {
    sendjsonrequest('compositemove.json.php',
        {'id': id, 'artefact': artefact, 'direction':direction},
        'GET', 
        function(data) {
            tableRenderers[type].doupdate();
        },
        function() {
            // @todo error
        }
    );
    return false;
}

290
function editprofilebutton() {
291
    document.location='{$wwwroot}artefact/internal/index.php?fs=contact';
292
293
294
    return false;
}

Penny Leach's avatar
Penny Leach committed
295
EOF;
296
297
$inlinejs .= ArtefactTypeResumeComposite::get_showhide_composite_js();

Penny Leach's avatar
Penny Leach committed
298
$compositeforms = array();
Penny Leach's avatar
Penny Leach committed
299
foreach (ArtefactTypeResumeComposite::get_composite_artefact_types() as $compositetype) {
300
    $inlinejs .= call_static_method(generate_artefact_class_name($compositetype), 'get_composite_js');
Penny Leach's avatar
Penny Leach committed
301
    $inlinejs .= <<<EOF
Penny Leach's avatar
Penny Leach committed
302
tableRenderers.{$compositetype} = new TableRenderer(
Penny Leach's avatar
Penny Leach committed
303
304
305
306
    '{$compositetype}list',
    'composite.json.php',
    [
EOF;
Penny Leach's avatar
Penny Leach committed
307
    $inlinejs .= call_static_method(generate_artefact_class_name($compositetype), 'get_tablerenderer_js');
Penny Leach's avatar
Penny Leach committed
308
    $inlinejs .= <<<EOF
309
310

        function (r) {
311
            return TD(null, A({'href': 'editcomposite.php?id=' + r.id + '&artefact=' + r.artefact}, '{$editstr}'));
312
313
314
315
316
        },
        function (r, d) {
           var link = A({'href': ''}, '{$delstr}');
            connect(link, 'onclick', function (e) {
                e.stop();
317
                return deleteComposite(d.type, r.id, r.artefact);
318
319
            });
            return TD(null, link);
320
321
        },
        function (r, d) {
322
323
            var buttons = [];
            if (r._rownumber > 1) {
324
                var up = A({'href': ''}, IMG({'src': {$imagemoveblockup}, 'alt':'{$upstr}'}));
325
326
327
328
329
330
331
                connect(up, 'onclick', function (e) {
                    e.stop();
                    return moveComposite(d.type, r.id, r.artefact, 'up');
                });
                buttons.push(up);
            }
            if (!r._last) {
332
                var down = A({'href': ''}, IMG({'src': {$imagemoveblockdown}, 'alt':'{$downstr}'}));
333
334
335
336
337
338
339
340
                connect(down, 'onclick', function (e) {
                    e.stop();
                    return moveComposite(d.type, r.id, r.artefact, 'down');
                });
                buttons.push(' ');
                buttons.push(down);
            }
            return TD({'style':'text-align:center;'}, buttons);
341
        }
Penny Leach's avatar
Penny Leach committed
342
343
    ]
);
344

Penny Leach's avatar
Penny Leach committed
345
346
347
348
tableRenderers.{$compositetype}.type = '{$compositetype}';
tableRenderers.{$compositetype}.statevars.push('type');
tableRenderers.{$compositetype}.emptycontent = '';
tableRenderers.{$compositetype}.updateOnLoad();
Penny Leach's avatar
Penny Leach committed
349
350

EOF;
Penny Leach's avatar
Penny Leach committed
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
    $elements = call_static_method(generate_artefact_class_name($compositetype), 'get_addform_elements');
    $elements['submit'] = array(
        'type' => 'submit',
        'value' => get_string('save'),
    );
    $elements['compositetype'] = array(
        'type' => 'hidden',
        'value' => $compositetype,
    );
    $cform = array(
        'name' => 'add' . $compositetype,
        'plugintype' => 'artefact',
        'pluginname' => 'resume',
        'elements' => $elements, 
        'jsform' => true,
        'successcallback' => 'compositeform_submit',
        'jssuccesscallback' => 'compositeSaveCallback',
    );
    $compositeforms[$compositetype] = pieform($cform);
} // end composite loop

$smarty->assign('compositeforms', $compositeforms);
Penny Leach's avatar
Penny Leach committed
373
$smarty->assign('INLINEJAVASCRIPT', $inlinejs);
374
$smarty->assign('PAGEHEADING', hsc(get_string('myresume', 'artefact.resume')));
Penny Leach's avatar
Penny Leach committed
375
376
$smarty->display('artefact:resume:index.tpl');

377
function coverletter_submit(Pieform $form, $values) {
Penny Leach's avatar
Penny Leach committed
378
379
380
381
382
383
384
385
386
    global $coverletter, $personalinformation, $interest, $USER;

    $userid = $USER->get('id');
    $errors = array();

    try {
        if (empty($coverletter) && !empty($values['coverletter'])) {
            $coverletter = new ArtefactTypeCoverletter(0, array( 
                'owner' => $userid, 
387
                'description' => $values['coverletter']
Penny Leach's avatar
Penny Leach committed
388
389
390
            ));
            $coverletter->commit();
        }
391
        else if (!empty($coverletter) && !empty($values['coverletter'])) {
Penny Leach's avatar
Penny Leach committed
392
393
394
            $coverletter->set('description', $values['coverletter']);
            $coverletter->commit();
        }
395
396
397
        else if (!empty($coverletter) && empty($values['coverletter'])) {
            $coverletter->delete();
        }
Penny Leach's avatar
Penny Leach committed
398
399
400
401
    }
    catch (Exception $e) {
        $errors['coverletter'] = true;
    }
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
    if (empty($errors)) {
        $form->json_reply(PIEFORM_OK, get_string('resumesaved','artefact.resume'));
    }
    else {
        $message = '';
        foreach (array_keys($errors) as $key) {
            $message .= get_string('resumesavefailed', 'artefact.resume')."\n";
        }
        $form->json_reply(PIEFORM_ERR, $message);
    }
}

function interests_submit(Pieform $form, $values) {
    global $coverletter, $personalinformation, $interest, $USER;

    $userid = $USER->get('id');
    $errors = array();

420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
    try {
        if (empty($interest) && !empty($values['interest'])) {
            $interest = new ArtefactTypeInterest(0, array( 
                'owner' => $userid, 
                'description' => $values['interest']
            ));
            $interest->commit();
        }
        else if (!empty($interest) && !empty($values['interest'])) {
            $interest->set('description', $values['interest']);
            $interest->commit();
        }
        else if (!empty($interest) && empty($values['interest'])) {
            $interest->delete();
        }
    }
    catch (Exception $e) {
        $errors['interest'] = true;
    }   

440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
    if (empty($errors)) {
        $form->json_reply(PIEFORM_OK, get_string('resumesaved','artefact.resume'));
    }
    else {
        $message = '';
        foreach (array_keys($errors) as $key) {
            $message .= get_string('resumesavefailed', 'artefact.resume')."\n";
        }
        $form->json_reply(PIEFORM_ERR, $message);
    }
}

function personalinformation_submit(Pieform $form, $values) {
    global $personalinformation, $USER;
    $userid = $USER->get('id');
    $errors = array();

Penny Leach's avatar
Penny Leach committed
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
    try {
        if (empty($personalinformation)) {
            $personalinformation = new ArtefactTypePersonalinformation(0, array(
                'owner' => $userid,
                'title' => get_string('personalinformation', 'artefact.resume'),
            ));
        }
        foreach (array_keys(ArtefactTypePersonalInformation::get_composite_fields()) as $field) {
            $personalinformation->set_composite($field, $values[$field]);
        }
        $personalinformation->commit();
    }
    catch (Exception $e) {
        $errors['personalinformation'] = true;
    }

    if (empty($errors)) {
        $form->json_reply(PIEFORM_OK, get_string('resumesaved','artefact.resume'));
475
    }
Penny Leach's avatar
Penny Leach committed
476
477
478
    else {
        $message = '';
        foreach (array_keys($errors) as $key) {
Penny Leach's avatar
Penny Leach committed
479
            $message .= get_string('resumesavefailed', 'artefact.resume')."\n";
Penny Leach's avatar
Penny Leach committed
480
481
482
483
484
485
        }
        $form->json_reply(PIEFORM_ERR, $message);
    }
}

?>