<?php
/***************************************************************************
- * Copyright (C) 2003-2010 Polytechnique.org *
+ * Copyright (C) 2003-2011 Polytechnique.org *
* http://opensource.polytechnique.org/ *
* *
* This program is free software; you can redistribute it and/or modify *
class ProfileSettingDeco implements ProfileSetting
{
- public function value(ProfilePage &$page, $field, $value, &$success)
+ private static function compareMedals(array $a, array $b)
+ {
+ if ($a['id'] == $b['id']) {
+ if ($a['grade'] == $b['grade']) {
+ return $a['level'] > $b['level'];
+ }
+ return $a['grade'] > $b['grade'];
+ }
+ return $a['id'] > $b['id'];
+ }
+
+ public function value(ProfilePage $page, $field, $value, &$success)
{
$success = true;
- if (is_null($value)) {
+ if (is_null($value) || !S::user()->isMyProfile($profile) &&
+ $page->values['medals_pub'] == 'private' && !S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
// Fetch already attributed medals
- $res = XDB::iterRow("SELECT m.id AS id, s.gid AS grade
- FROM profile_medals AS s
- INNER JOIN profile_medal_enum AS m ON ( s.mid = m.id )
- WHERE s.pid = {?}",
- $page->pid());
- $value = array();
- while (list($id, $grade) = $res->next()) {
- $value[$id] = array('grade' => $grade,
- 'valid' => '1');
- }
+ $value = XDB::fetchAllAssoc("SELECT m.mid AS id, m.gid AS grade, 1 AS valid, FIND_IN_SET('has_levels', e.flags) AS has_levels, m.level
+ FROM profile_medals AS m
+ INNER JOIN profile_medal_enum AS e ON (m.mid = e.id)
+ WHERE m.pid = {?}",
+ $page->pid());
// Fetch not yet validated medals
$medals = ProfileValidate::get_typed_requests($page->pid(), 'medal');
foreach ($medals as &$medal) {
- $value[$medal->mid] = array('grade' => $medal->gid,
- 'valid' => '0');
+ $value[] = array(
+ 'id' => $medal->mid,
+ 'grade' => $medal->gid,
+ 'level' => $medal->level,
+ 'has_levels' => $medal->has_levels,
+ 'valid' => '0'
+ );
}
- } else if (!is_array($value)) {
+ } elseif (!is_array($value)) {
$value = array();
}
- ksort($value);
+ usort($value, 'self::compareMedals');
return $value;
}
- public function save(ProfilePage &$page, $field, $value)
+ public function save(ProfilePage $page, $field, $value)
{
- $orig =& $page->orig[$field];
+ $original =& $page->orig[$field];
+
+ $i = $j = 0;
+ $total_original = count($original);
+ $total_value = count($value);
+
+ if ($total_original && !S::user()->isMyProfile($profile) &&
+ $page->values['medals_pub'] == 'private' && !S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
+ return;
+ }
- // Remove old ones
- foreach ($orig as $id=>&$val) {
- if (!isset($value[$id]) || $val['grade'] != $value[$id]['grade']) {
- if ($val['valid']) {
- XDB::execute("DELETE FROM profile_medals
- WHERE pid = {?} AND mid = {?}",
- $page->pid(), $id);
+ while ($i < $total_original || $j < $total_value) {
+ if (isset($value[$j]) && (!isset($original[$i]) || self::compareMedals($original[$i], $value[$j]))) {
+ $req = new MedalReq(S::user(), $page->profile, $value[$j]['id'], $value[$j]['grade'], $value[$j]['level'], $value[$j]['has_levels']);
+ $req->submit();
+ sleep(1);
+ ++$j;
+ } elseif (isset($original[$i]) && (!isset($value[$j]) || self::compareMedals($value[$j], $original[$i]))) {
+ if ($original[$i]['valid']) {
+ XDB::execute('DELETE FROM profile_medals
+ WHERE pid = {?} AND mid = {?} AND gid = {?}',
+ $page->pid(), $original[$i]['id'], $original[$i]['grade']);
} else {
- $req = MedalReq::get_request($page->pid(), $id);
+ $req = MedalReq::get_request($page->pid(), $original[$i]['id'], $original[$i]['grade'], $value[$j]['level']);
if ($req) {
$req->clean();
}
}
- }
- }
-
- // Add new ones
- foreach ($value as $id=>&$val) {
- if (!isset($orig[$id]) || $orig[$id]['grade'] != $val['grade']) {
- $req = new MedalReq(S::user(), $page->profile, $id, $val['grade']);
- $req->submit();
- sleep(1);
+ ++$i;
+ } else {
+ ++$i;
+ ++$j;
}
}
}
{
protected $pg_template = 'profile/deco.tpl';
- public function __construct(PlWizard &$wiz)
+ public function __construct(PlWizard $wiz)
{
parent::__construct($wiz);
- $this->settings['medals'] = new ProfileSettingDeco();
$this->settings['medals_pub'] = new ProfileSettingPub();
+ $this->settings['medals'] = new ProfileSettingDeco();
$this->watched['medals'] = true;
}
protected function _saveData()
{
- if ($this->changed['medals_pub']) {
+ if ($this->changed['medals_pub'] && (S::user()->isMyProfile($profile) || S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE))) {
XDB::execute("UPDATE profiles
SET medals_pub = {?}
WHERE pid = {?}",
}
}
- public function _prepare(PlPage &$page, $id)
+ public function _prepare(PlPage $page, $id)
{
$res = XDB::iterator('SELECT *, FIND_IN_SET(\'validation\', flags) AS validate
FROM profile_medal_enum