X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=modules%2Fprofile%2Fdecos.inc.php;h=3adbafaf7eec08d3bcc0ce83e147685ccfe88eec;hb=2df4879834d554904f1eccfd6efe5d906bdcea47;hp=fa40cf9ef06dc651607b9753493f4a76bad7cc4e;hpb=853ff307303faf861396c4e22dbaa56114366751;p=platal.git diff --git a/modules/profile/decos.inc.php b/modules/profile/decos.inc.php index fa40cf9..3adbafa 100644 --- a/modules/profile/decos.inc.php +++ b/modules/profile/decos.inc.php @@ -1,6 +1,6 @@ $b['grade']; + } + return $a['id'] > $b['id']; + } + + public function value(ProfilePage $page, $field, $value, &$success) { $success = true; if (is_null($value)) { // 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 mid AS id, gid AS grade, 1 AS valid + FROM profile_medals + WHERE pid = {?}', + $page->pid()); // Fetch not yet validated medals - require_once 'validations.inc.php'; $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, + '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) { - require_once 'validations.inc.php'; + $original =& $page->orig[$field]; - $orig =& $page->orig[$field]; + $i = $j = 0; + $total_original = count($original); + $total_value = count($value); - // 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']); + $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']); 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; } } } @@ -93,11 +98,11 @@ class ProfileSettingDeco implements ProfileSetting } } -class ProfileSettingDecos extends ProfilePage +class ProfilePageDecos extends ProfilePage { protected $pg_template = 'profile/deco.tpl'; - public function __construct(PlWizard &$wiz) + public function __construct(PlWizard $wiz) { parent::__construct($wiz); $this->settings['medals'] = new ProfileSettingDeco(); @@ -124,7 +129,7 @@ class ProfileSettingDecos extends ProfilePage } } - 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