lib.php 26.7 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 29 30 31 32 33 34 35 36 37 38 39
<?php
/**
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2011 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
 *
 * 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.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * @package    mahara
 * @subpackage blocktype-openbadgedisplayer
 * @author     Discendum Oy
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2012 Discedum Oy http://discendum.com
 * @copyright  (C) 2011 Catalyst IT Ltd http://catalyst.net.nz
 *
 */


defined('INTERNAL') || die();

class PluginBlocktypeOpenbadgedisplayer extends SystemBlocktype {

    private static $source = null;

    public static function single_only() {
        return false;
    }

Lisa Seeto's avatar
Lisa Seeto committed
40 41 42 43
    public static function single_artefact_per_block() {
        return false;
    }

44 45 46 47 48 49 50 51 52 53 54 55
    public static function get_title() {
        return get_string('title', 'blocktype.openbadgedisplayer');
    }

    public static function get_description() {
        return get_string('description', 'blocktype.openbadgedisplayer');
    }

    public static function get_categories() {
        return array('external');
    }

56
    public static function get_css_icon($blocktypename) {
57
        return 'shield-alt';
58 59
    }

60 61 62 63 64 65 66 67
    public static function get_viewtypes() {
        return array('portfolio', 'profile');
    }

    public static function get_backpack_source() {
        if (is_null(self::$source)) {
            $source = get_config('openbadgedisplayer_source');

68 69 70 71 72
            if (!empty($source)) {
                $source = (array) $source;
            }
            else {
                return false;
73 74 75 76 77 78 79 80
            }

            self::$source = $source;
        }

        return self::$source;
    }

81 82 83 84
    public static function get_blocktype_type_content_types() {
        return array('openbadgedisplayer' => array('media'));
    }

85
    public static function app_tabs() {
86
        return array(
87
            'badgr' => array(
88 89 90
                'path' => 'settings/badgr',
                'url' => 'blocktype/openbadgedisplayer/badgrtoken.php',
                'title' => get_string('badgrtokentitle', 'blocktype.openbadgedisplayer'),
91
                'weight' => 20,
92 93
                'iconclass' => 'flag'
            ),
94 95 96 97 98 99 100
            'apps' => array (
                'path' => 'settings/apps',
                'url' => '/account/apps.php',
                'title' => get_string('overview'),
                'weight' => 5,
                'iconclass' => 'flag'
            )
101 102 103
        );
    }

104
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
105
        $configdata = $instance->get('configdata');
106
        if (empty($configdata) || !isset($configdata['badgegroup']) || !get_config('openbadgedisplayer_source')) {
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
            return;
        }

        $host = 'backpack';
        $badgegroups = $configdata['badgegroup'];
        $html = '';

        // Support the legacy format (a string, not an array).
        if (is_string($badgegroups)) {
            $badgegroups = array($badgegroups);
        }

        if ($editing) {
            $items = array();

122 123 124 125 126 127 128 129
            foreach ($badgegroups as $selectedbadgegroup) {
                list($host, $uid, $selectedgroupid) = explode(':', $selectedbadgegroup);

                $allbadgegroups = self::get_badgegroupnames($host, $uid);
                if (!empty($allbadgegroups)) {
                    foreach ($allbadgegroups as $badgegroupid => $name) {
                        if ((int) $selectedgroupid === (int) $badgegroupid) {
                            $items[] = $name;
130 131 132 133 134 135 136 137
                        }
                    }
                }
            }

            if (count($items) > 0) {
                $html .= '<ul>' . implode('', array_map(function ($item) { return "<li>{$item}</li>"; }, $items)) . '</ul>';
            }
138
            else {
139
                $html .= '<p class="editor-description">' . get_string('nobadgesselectone', 'blocktype.openbadgedisplayer') . '</p>';
140
            }
141 142 143 144 145

            return $html;
        }
        else {
            $smarty = smarty_core();
146 147
            $smarty->left_delimiter = '{{';
            $smarty->right_delimiter = '}}';
148
            $smarty->assign('id', $instance->get('id'));
149
            $smarty->assign('badgehtml', self::get_badges_html($badgegroups));
150 151 152 153 154 155 156

            $html = $smarty->fetch('blocktype:openbadgedisplayer:openbadgedisplayer.tpl');
        }

        return $html;
    }

157 158 159 160 161 162 163 164 165 166
    /**
     * Returns html code for badge in a group
     * @param string $group in format <host>:<uid>:<badgegroupid>
     * @param bool $fromcache if true the info will be fetched from database first
     * @return string HTML code
     */
    private static function get_badge_html($group, $fromcache=false) {
        if (!isset($group) && !is_string($group)) {
            return '';
        }
167

168
        $parts = explode(':', $group);
169

170 171 172
        if (count($parts) < 3) {
            return '';
        }
173

174 175 176 177 178 179 180 181 182 183 184 185
        $host = $parts[0];
        $uid = $parts[1];
        $badgegroupid = $parts[2];

        // Try to get the badge html from database first
        // Get badge group html using uid (backpackid)
        if ($fromcache && $badgegroup = get_record_select('blocktype_openbadgedisplayer_data',
                'host = ? AND uid = ? AND badgegroupid = ? AND lastupdate > ?',
                array($host, $uid, $badgegroupid, db_format_timestamp(strtotime('-1 day'))),
                'html')) {
            if (isset($badgegroup->html)) {
                return $badgegroup->html;
186
            }
187
        }
188

189 190 191
        $html = '';
        $existing = array();
        $backpack_url = self::get_backpack_url($host);
192 193 194 195 196 197 198 199 200 201 202 203 204
        if ($host == 'badgr') {
            $url = $backpack_url . 'v2/backpack/collections/' . $badgegroupid;
            $res = mahara_http_request(
                    array(
                        CURLOPT_URL        => $url,
                        CURLOPT_HTTPHEADER => array('Authorization: Token ' . $uid),
                    )
            );
        }
        else {
            $url = $backpack_url . 'displayer/' . $uid . '/group/' . $badgegroupid . '.json';
            $res = mahara_http_request(array(CURLOPT_URL => $url));
        }
205

206 207 208
        if ($res->info['http_code'] != 200) {
            return '';
        }
209

210
        $json = json_decode($res->data);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        if (isset($json->status) && $json->status->success) {
            foreach ($json->result as $collection) {
                foreach ($collection->assertions as $assertion) {
                    // Currently I can't see a way to fetch the badge/assertion/issuer info
                    // as one json blob/one curl request
                    $url = $backpack_url . 'v2/backpack/assertions/' . $assertion;
                    $res2 = mahara_http_request(
                        array(
                            CURLOPT_URL        => $url,
                            CURLOPT_HTTPHEADER => array('Authorization: Token ' . $uid),
                        )
                    );
                    $badge = json_decode($res2->data);
                    $res3 = mahara_http_request(
                        array(
                            CURLOPT_URL        => $badge->result[0]->badgeclassOpenBadgeId,
                            CURLOPT_HTTPHEADER => array('accept: application/json'),
                        )
                    );
                    $badgeinfo = json_decode($res3->data);
                    $res4 = mahara_http_request(
                        array(
                            CURLOPT_URL        => $badgeinfo->issuer,
                            CURLOPT_HTTPHEADER => array('accept: application/json'),
                        )
                    );
                    if (!empty($badgeinfo->id)) {
                        $criteria = $badgeinfo->id;
                    }
                    else if (is_array($badgeinfo->criteria)) {
                        $criteria = $badgeinfo->criteria->id;
                    }
                    else if (is_string($badgeinfo->criteria)) {
                        $criteria = $badgeinfo->criteria;
                    }
                    else {
                        $criteria = '';
                    }
                    $issuer = json_decode($res4->data);
                    $data_assertion = $badge->result[0];
                    $data_assertion->issued_on = strtotime($data_assertion->issuedOn);
                    $data_assertion->expires = strtotime($data_assertion->expires);
                    $data_assertion->badge = new stdClass();
                    $data_assertion->badge->name = hsc($badgeinfo->name);
                    $data_assertion->badge->description = hsc($badgeinfo->description);
                    $data_assertion->badge->criteria = $criteria;
                    $data_assertion->badge->_location = $data_assertion->openBadgeId;
                    $data_assertion->badge->image = $data_assertion->image;
                    $data_assertion->badge->issuer = new stdClass();
                    $data_assertion->badge->issuer->origin = $issuer->url;
                    $data_assertion->badge->issuer->name = $issuer->name;
                    $data_assertion->badge->issuer->org = $issuer->email;

                    $html .= '<img tabindex="0" id="' . (preg_replace('/\:/', '_', $group)) . '" '
                          . 'src="' . $badge->result[0]->image . '" '
                          . 'title="' . $badge->result[0]->entityId . '" '
                          . 'data-assertion="' . htmlentities(json_encode($data_assertion)) . '" />';
                }
            }
        }
        else if (isset($json->badges) && is_array($json->badges)) {
272 273 274

            foreach ($json->badges as $badge) {
                $b = $badge->assertion->badge;
275

276 277 278 279
                // TODO: Simple check for unique badges, improve me!
                if (array_key_exists($b->name, $existing) && strcmp($existing[$b->name], $b->description) === 0) {
                    continue;
                }
280

281 282
                if (self::assertion_has_expired($badge->assertion)) {
                    continue;
283
                }
284

285
                $html .= '<img tabindex="0" id="' . (preg_replace('/\:/', '_', $group)) . '" '
286 287 288
                            . 'src="' . $b->image . '" '
                            . 'title="' . $b->name . '" '
                            . 'data-assertion="' . htmlentities(json_encode($badge->assertion)) . '" />';
289 290

                $existing[$b->name] = $b->description;
291
            }
292
        }
293

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
        // Caching badge info into database for better performance
        if ($fromcache) {
            ensure_record_exists('blocktype_openbadgedisplayer_data',
                (object) array(
                    'host' => $host,
                    'uid' => $uid,
                    'badgegroupid' => $badgegroupid,
                ),
                (object) array(
                    'host' => $host,
                    'uid' => $uid,
                    'badgegroupid' => $badgegroupid,
                    'html' => $html,
                    'lastupdate' => db_format_timestamp(time())
                )
            );
        }

        return $html;
    }

    private static function get_badges_html($groups) {
        $html = '';

        foreach ($groups as $group) {
            $html .= self::get_badge_html($group);
320 321
        }

322 323 324
        if (empty($html)) {
            $html = get_string('nobadgegroups', 'blocktype.openbadgedisplayer');
        }
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
        return $html;
    }

    private static function assertion_has_expired($assertion) {
        if (!isset($assertion->expires)) {
            return false;
        }

        // Unix timestamp
        if (preg_match('/^[0-9]+$/', $assertion->expires)) {
            return ($assertion->expires * 1000) < time();
        }

        // Formatted date
        return strtotime($assertion->expires) < time();
    }

    public static function has_instance_config() {
        return true;
    }

346
    public static function instance_config_form(BlockInstance $instance) {
347
        global $USER;
348
        $owner = $instance->get_view()->get('owner');
349
        $sources = self::get_backpack_source();
350 351 352 353 354
        if ($sources === false) {
            $fields = array(
                'message' => array(
                    'type' => 'html',
                    'class' => '',
Evonne's avatar
Evonne committed
355
                    'value' => '<div class="alert alert-warning" role="alert">' . get_string('missingbadgesources', 'blocktype.openbadgedisplayer') . '</div>'
356 357 358 359 360
                ),
            );
            return $fields;
        }

361
        $configdata = $instance->get('configdata');
362 363 364 365
        $addresses = array();
        if ($owner) {
            $addresses = get_column('artefact_internal_profile_email', 'email', 'owner', $USER->id, 'verified', 1);
        }
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
        $current_values = array();

        if (isset($configdata['badgegroup'])) {
            $current_values = $configdata['badgegroup'];

            // Support the legacy format (a string, not an array).
            if (is_string($current_values)) {
                $current_values = array($current_values);
            }

            foreach ($current_values as &$current_value) {
                if (substr_count($current_value, ':') == 1) {
                    // Legacy value, prepend host
                    $current_value = 'backpack:' . $current_value;
                }
            }
        }

        $sourcelinks = array();

        foreach ($sources as $source => $url) {
            if (!empty($url)) {
                $title = get_string('title_' . $source, 'blocktype.openbadgedisplayer');
389
                $sourcelinks[] = '<a href="' . $url . '">' . $title . '</a>';
390 391
            }
        }
392 393 394 395 396 397 398
        $fields['message'] = array(
            'type' => 'html',
            'class' => '',
            'value' => '<p class="message">'. get_string('confighelp', 'blocktype.openbadgedisplayer', implode(', ', $sourcelinks)) .'</p>'
        );
        if ($owner) {
            $fields['badgegroups'] = array(
399 400
                'type' => 'container',
                'class' => '',
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
                'elements' => array(
                    'loadinginfo' => array(
                        'type' => 'html',
                        'class' => '',
                        'value' => '<p class="loading-box alert alert-info">'. get_string('fetchingbadges', 'blocktype.openbadgedisplayer') .'</p>' .
                                    '<div></div>',
                    ),
                    'hosts' => array(
                        'type' => 'hidden',
                        'value' => json_encode(array_keys($sources)),
                    ),
                    'emails' => array(
                        'type' => 'hidden',
                        'value' => json_encode($addresses),
                    ),
                    'selectedbadgegroups' => array(
                        'type' => 'hidden',
                        'value' => json_encode($current_values),
                    ),
                )
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
            );
        }
        else {
            $fields['blocktemplatehtml'] = array(
                'type' => 'html',
                'value' => get_string('blockinstanceconfigownerchange', 'mahara'),
            );
            $fields['blocktemplate'] = array(
                'type'    => 'hidden',
                'value'   => 1,
            );
        }
        $fields['tags'] = array(
            'type'         => 'tags',
            'title'        => get_string('tags'),
            'description'  => get_string('tagsdescblock'),
            'defaultvalue' => $instance->get('tags'),
            'help'         => false,
439 440 441 442 443
        );

        return $fields;
    }

444 445 446 447
    public static function get_instance_config_javascript(BlockInstance $instance) {
        return array(
            'js/configform.js',
        );
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    }

    private static function get_form_fields($host, $addresses) {
        if ( ! $host) {
            return array();
        }

        $backpackid = array();
        foreach ($addresses AS $email) {
            $backpackid[] = self::get_backpack_id($host, $email);
        }
        $backpackid = array_filter($backpackid);

        $opt = array();
        foreach ($backpackid AS $uid) {
            $opt += self::get_group_opt($host, $uid);
        }

        return $opt;

    }


471 472
    public static function get_backpack_id($host, $email) {
        static $backpackids = array();
473 474
        $backpack_url = self::get_backpack_url($host);

475 476 477 478
        if (isset($backpackids[$host][$email])) {
            return $backpackids[$host][$email];
        }

479
        if ($backpack_url !== false) {
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
            if ($backpack_url == 'https://api.badgr.io/') {
                $userid = get_field('artefact_internal_profile_email', 'owner', 'email', $email);
                $token = get_field('usr_account_preference', 'value', 'field', 'badgr_token', 'usr', $userid);
                if ($token) {
                    $res = mahara_http_request(
                        array(
                            CURLOPT_URL        => $backpack_url . 'v2/users/self',
                            CURLOPT_HTTPHEADER => array('Authorization: Token ' . $token),
                        )
                    );
                    $res = json_decode($res->data);
                    if (isset($res->status) && $res->status->success) {
                        $backpackids[$host][$email] = $token;
                        return $token;
                    }
                }
            }
            else {
                $res = mahara_http_request(
                    array(
                        CURLOPT_URL        => $backpack_url . 'displayer/convert/email',
                        CURLOPT_POST       => 1,
                        CURLOPT_POSTFIELDS => 'email=' . urlencode($email)
                    )
                );
                $res = json_decode($res->data);
                if (isset($res->userId)) {
                    $backpackids[$host][$email] = $res->userId;
                    return $res->userId;
                }
510 511 512 513 514
            }
        }
        return null;
    }

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
    /**
     * Returns all backpack IDs of current logged-in user
     *
     * @return array of backpack IDs:
     *      array(
     *          <host> => array (
     *              <email> => <backpackid>
     *          )
     *      )
     */
    public static function get_user_backpack_ids() {
        global $USER;

        if (!$USER->is_logged_in()) {
            return array();
        }

        $sources = self::get_backpack_source();
        $addresses = get_column('artefact_internal_profile_email', 'email', 'owner', $USER->get('id'), 'verified', 1);
        $userbackpackids = array();

        if (!empty($sources) && !empty($addresses)) {
            foreach ($sources as $h => $url) {
                $userbackpackids[$h] = array();
                foreach ($addresses as $e) {
                    $userbackpackids[$h][$e] = self::get_backpack_id($h, $e);
                }
            }
        }

        return $userbackpackids;
    }

    /**
     * Return name of badge groups for a given host and backpackid
     * @param $host
     * @param $uid backpack ID attached to an email on the host
     * @param $usedbcache if true, the badge groups will be fetched from database first
     * @return array
     *      <badgegroupid> => <badgegroupname>
     * )
     */
    public static function get_badgegroupnames($host, $uid, $usedbcache=false) {
        static $badgegroupnames = array();

        if (!isset($host) || !isset($uid)) {
            return array();
        }

        if (isset($badgegroupnames[$host][$uid])) {
            return $badgegroupnames[$host][$uid];
        }

        // Get badge group names using uid (backpackid) from database
        if ($usedbcache && $badgegroups = get_records_select_array('blocktype_openbadgedisplayer_data',
            'host = ? AND uid = ? AND lastupdate > ?', array($host, $uid, db_format_timestamp(strtotime('-1 day'))),
            '', 'badgegroupid, name')) {
            foreach ($badgegroups as $badgegroup) {
                $badgegroupnames[$host][$uid][$badgegroup->badgegroupid] = $badgegroup->name;
            }
            return $badgegroupnames[$host][$uid];
        }

        $badgegroupnames[$host][$uid] = array();
        $backpack_url = self::get_backpack_url($host);
580 581 582 583 584 585 586 587
        if ($backpack_url == 'https://api.badgr.io/') {
            $res = mahara_http_request(array(CURLOPT_URL => $backpack_url . "v2/backpack/collections",
                                             CURLOPT_HTTPHEADER => array('Authorization: Token ' . $uid,
                                                                         'accept: application/json')));
        }
        else {
            $res = mahara_http_request(array(CURLOPT_URL => $backpack_url . "displayer/{$uid}/groups.json"));
        }
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
        $res = json_decode($res->data);

        if (!empty($res->groups)) {
            foreach ($res->groups AS $g) {
                if ($g->name == 'Public badges' && $g->groupId == 0) {
                    $name = get_string('obppublicbadges', 'blocktype.openbadgedisplayer');
                }
                else {
                    $name = hsc($g->name);
                }

                $name .= ' (' . get_string('nbadges', 'blocktype.openbadgedisplayer', $g->badges) . ')';
                $badgegroupnames[$host][$uid][$g->groupId] = $name;

                // Caching badge info into database for better performance
                ensure_record_exists('blocktype_openbadgedisplayer_data',
                    (object) array(
                        'host' => $host,
                        'uid' => $uid,
                        'badgegroupid' => $g->groupId,
                    ),
                    (object) array(
                        'host' => $host,
                        'uid' => $uid,
                        'badgegroupid' => $g->groupId,
                        'name' => $name,
                        'lastupdate' => db_format_timestamp(time())
                    )
                );
            }
        }
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
        else if (isset($res->status) && isset($res->status->success) && $res->status->success) {
            foreach ($res->result as $g) {
                if (!$g->published) {
                    continue;
                }
                $name = hsc($g->name);

                $name .= ' (' . get_string('nbadges', 'blocktype.openbadgedisplayer', count($g->assertions)) . ')';
                $badgegroupnames[$host][$uid][$g->entityId] = $name;
                // Caching badge info into database for better performance
                ensure_record_exists('blocktype_openbadgedisplayer_data',
                    (object) array(
                        'host' => $host,
                        'uid' => $uid,
                        'badgegroupid' => $g->entityId,
                    ),
                    (object) array(
                        'host' => $host,
                        'uid' => $uid,
                        'badgegroupid' => $g->entityId,
                        'name' => $name,
                        'lastupdate' => db_format_timestamp(time())
                    )
                );
            }
        }
645 646
        return $badgegroupnames[$host][$uid];
    }
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681

    private static function get_group_opt($host, $uid) {
        $opt = array();
        $backpack_url = self::get_backpack_url($host);
        $res = mahara_http_request(array(CURLOPT_URL => $backpack_url . "displayer/{$uid}/groups.json"));
        $res = json_decode($res->data);

        if (!empty($res->groups)) {
            foreach ($res->groups AS $g) {
                if ($g->name == 'Public badges' && $g->groupId == 0) {
                    $name = get_string('obppublicbadges', 'blocktype.openbadgedisplayer');
                }
                else {
                    $name = hsc($g->name);
                }

                $name .= ' (' . get_string('nbadges', 'blocktype.openbadgedisplayer', $g->badges) . ')';
                $cb_id = $host . ':' . $uid . ':' . $g->groupId;
                $cb_name = self::_sanitize_name($cb_id);
                $opt[$cb_name] = array(
                    'type' => 'checkbox',
                    'title' => $name,
                    'value' => $cb_id
                );
            }
        }
        return $opt;
    }

    public static function get_backpack_url($host) {
        $sources = self::get_backpack_source();

        return isset($sources[$host]) ? $sources[$host] : false;
    }

682
    public static function _sanitize_name($name) {
683 684 685 686 687 688 689 690
        return preg_replace('/[^a-zA-Z0-9_]/', '_', $name);
    }

    public static function instance_config_save($values) {
        unset($values['message']);
        // Support old save format.
        $sources = array_keys(self::get_backpack_source());
        $values['badgegroup'] = array();
691
        $validbackpackids = self::get_user_backpack_ids();
692 693 694 695 696 697

        foreach ($sources as $source) {
            if (isset($values[$source])) {
                $values['badgegroup'] = array_merge($values['badgegroup'], $values[$source]);
                unset($values[$source]);
            }
698 699
            else if (param_exists($source)) {
                $values['badgegroup'] = array_merge($values['badgegroup'], param_variable($source));
700 701 702 703 704 705 706 707 708 709
            }
        }
        // check that what has been entered is allowed
        if (!empty($values['badgegroup'])) {
            foreach ($values['badgegroup'] as $key => $badgegroup) {
                list($host, $uid, $group) = explode(':', $badgegroup);
                if (!isset($uid) || !in_array($uid, array_values($validbackpackids[$host]))) {
                    unset($values['badgegroup'][$key]);
                }
            }
710 711 712 713 714 715 716 717 718
        }
        return $values;
    }

    public static function default_copy_type() {
        return 'shallow';
    }

    public static function allowed_in_view(View $view) {
719 720 721
        if ($view->get('type') == 'portfolio') {
            return true;
        }
722 723 724
        return $view->get('owner') != null;
    }

725
    public static function get_instance_javascript(BlockInstance $bi) {
726
        return array('js/showdetails.js');
727 728
    }

729 730 731 732
    public static function should_ajaxify() {
        return true;
    }

733 734 735 736 737 738 739
    public static function rewrite_blockinstance_config(View $view, $configdata) {
        if ($view->get('owner') !== null && !empty($configdata['blocktemplate'])) {
            unset($configdata['blocktemplatehtml']);
            unset($configdata['blocktemplate']);
        }
        return $configdata;
    }
740
}