| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | class ProfileSettingSkill implements ProfileSetting |
| 23 | { |
| 24 | private $table; |
| 25 | private $skill_field; |
| 26 | private $text_field; |
| 27 | |
| 28 | public function __construct($table, $skill, $text) |
| 29 | { |
| 30 | $this->table = $table; |
| 31 | $this->skill_field = $skill; |
| 32 | $this->text_field = $text; |
| 33 | } |
| 34 | |
| 35 | public function value(ProfilePage &$page, $field, $value, &$success) |
| 36 | { |
| 37 | if (is_null($value)) { |
| 38 | $value = array(); |
| 39 | $res = XDB::iterRow("SELECT s.id, s.{$this->text_field}, i.level |
| 40 | FROM profile_{$this->table}_enum AS s |
| 41 | INNER JOIN profile_{$this->table}s AS i ON(s.id = i.{$this->skill_field}) |
| 42 | WHERE i.pid = {?}", |
| 43 | $page->pid()); |
| 44 | while (list($sid, $text, $level) = $res->next()) { |
| 45 | $value[$sid] = array('text' => $text, 'level' => $level); |
| 46 | } |
| 47 | } |
| 48 | if (!is_array($value)) { |
| 49 | $value = array(); |
| 50 | } else { |
| 51 | foreach ($value as $id=>&$skill) { |
| 52 | if (!isset($skill['text']) || empty($skill['text'])) { |
| 53 | $res = XDB::query("SELECT {$this->text_field} |
| 54 | FROM profile_{$this->table}_enum |
| 55 | WHERE id = {?}", $id); |
| 56 | $skill['text'] = $res->fetchOneCell(); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | ksort($value); |
| 61 | $success = true; |
| 62 | return $value; |
| 63 | } |
| 64 | |
| 65 | public function save(ProfilePage &$page, $field, $value) |
| 66 | { |
| 67 | XDB::execute("DELETE FROM profile_{$this->table}s |
| 68 | WHERE pid = {?}", |
| 69 | $page->pid()); |
| 70 | if (!count($value)) { |
| 71 | return; |
| 72 | } |
| 73 | foreach ($value as $id=>&$skill) { |
| 74 | XDB::execute("INSERT INTO profile_{$this->table}s (pid, {$this->skill_field}, level) |
| 75 | VALUES ({?}, {?}, {?})", |
| 76 | $page->pid(), $id, $skill['level']); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | public function getText($value) { |
| 81 | $skills = array(); |
| 82 | |
| 83 | if ($this->table == 'langskill') { |
| 84 | static $levels = array( |
| 85 | 1 => 'connaissance basique', |
| 86 | 2 => 'maîtrise des bases', |
| 87 | 3 => 'maîtrise limitée', |
| 88 | 4 => 'maîtrise générale', |
| 89 | 5 => 'bonne maîtrise', |
| 90 | 6 => 'maîtrise complète' |
| 91 | ); |
| 92 | foreach ($value as $skill) { |
| 93 | $skills[] = $skill['text'] . ' (' . $levels[$skill['level']] . ')'; |
| 94 | } |
| 95 | } else { |
| 96 | foreach ($value as $skill) { |
| 97 | $skills[] = $skill['text'] . ' (' . $skill['level'] . ')'; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return implode(', ' , $skills); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | class ProfilePageSkills extends ProfilePage |
| 106 | { |
| 107 | protected $pg_template = 'profile/skill.tpl'; |
| 108 | |
| 109 | public function __construct(PlWizard &$wiz) |
| 110 | { |
| 111 | parent::__construct($wiz); |
| 112 | $this->settings['competences'] = new ProfileSettingSkill('skill', 'cid', 'text_fr'); |
| 113 | $this->settings['langues'] = new ProfileSettingSkill('langskill', 'lid', 'langue_fr'); |
| 114 | } |
| 115 | |
| 116 | public function _prepare(PlPage &$page, $id) |
| 117 | { |
| 118 | $page->assign('comp_list', XDB::iterator("SELECT id, text_fr, FIND_IN_SET('titre',flags) AS title |
| 119 | FROM profile_skill_enum")); |
| 120 | $page->assign('comp_level', array('initié' => 'initié', |
| 121 | 'bonne connaissance' => 'bonne connaissance', |
| 122 | 'expert' => 'expert')); |
| 123 | $page->assign('lang_list', XDB::iterator("SELECT id, langue_fr |
| 124 | FROM profile_langskill_enum |
| 125 | ORDER BY langue_fr")); |
| 126 | $page->assign('lang_level', array(1 => 'connaissance basique', |
| 127 | 2 => 'maîtrise des bases', |
| 128 | 3 => 'maîtrise limitée', |
| 129 | 4 => 'maîtrise générale', |
| 130 | 5 => 'bonne maîtrise', |
| 131 | 6 => 'maîtrise complète')); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 136 | ?> |