Merge branch 'xorg/maint'
[platal.git] / modules / profile / mentor.inc.php
CommitLineData
6457b5e4
FB
1<?php
2/***************************************************************************
c441aabe 3 * Copyright (C) 2003-2014 Polytechnique.org *
6457b5e4
FB
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
285e2fe7
SJ
22class ProfileSettingSkill implements ProfileSetting
23{
24 private $table;
25 private $id;
26 private $skill_field;
27 private $text_field;
28
29 public function __construct($table, $id, $skill, $text)
30 {
31 $this->table = $table;
32 $this->id = $id;
33 $this->skill_field = $skill;
34 $this->text_field = $text;
35 }
36
37 public function value(ProfilePage $page, $field, $value, &$success)
38 {
39 if (is_null($value)) {
40 $value = array();
41 $res = XDB::iterRow("SELECT s.{$this->id}, s.{$this->text_field}, i.level
42 FROM profile_{$this->table}_enum AS s
43 INNER JOIN profile_{$this->table}s AS i ON (s.{$this->id} = i.{$this->skill_field})
44 WHERE i.pid = {?}",
45 $page->pid());
46 while (list($sid, $text, $level) = $res->next()) {
47 $value[$sid] = array('text' => $text, 'level' => $level);
48 }
49 }
50 if (!is_array($value)) {
51 $value = array();
52 } else {
53 foreach ($value as $id=>&$skill) {
54 if (!isset($skill['text']) || empty($skill['text'])) {
55 $res = XDB::query("SELECT {$this->text_field}
56 FROM profile_{$this->table}_enum
57 WHERE {$this->id} = {?}", $id);
58 $skill['text'] = $res->fetchOneCell();
59 }
60 }
61 }
62 ksort($value);
63 $success = true;
64 return $value;
65 }
66
67 public function save(ProfilePage $page, $field, $value)
68 {
69 XDB::execute("DELETE FROM profile_{$this->table}s
70 WHERE pid = {?}",
71 $page->pid());
72 if (!count($value)) {
73 return;
74 }
75 foreach ($value as $id=>&$skill) {
76 XDB::execute("INSERT INTO profile_{$this->table}s (pid, {$this->skill_field}, level)
77 VALUES ({?}, {?}, {?})",
78 $page->pid(), $id, $skill['level']);
79 }
80 }
81
82 public function getText($value) {
83 $skills = array();
84
85 if ($this->table == 'langskill') {
86 static $levels = array(
87 1 => 'connaissance basique',
88 2 => 'maîtrise des bases',
89 3 => 'maîtrise limitée',
90 4 => 'maîtrise générale',
91 5 => 'bonne maîtrise',
92 6 => 'maîtrise complète'
93 );
94 foreach ($value as $skill) {
95 $skills[] = $skill['text'] . ' (' . $levels[$skill['level']] . ')';
96 }
97 } else {
98 foreach ($value as $skill) {
99 $skills[] = $skill['text'] . ' (' . $skill['level'] . ')';
100 }
101 }
102
103 return implode(', ' , $skills);
104 }
105}
106
3ac45f10
PC
107/** Terms associated to profile mentoring */
108class ProfileSettingTerms implements ProfileSetting
6457b5e4 109{
26ba053e 110 public function value(ProfilePage $page, $field, $value, &$success)
6457b5e4
FB
111 {
112 $success = true;
113 if (is_null($value)) {
114 $value = array();
3ac45f10
PC
115 $res = XDB::query('SELECT e.jtid, e.full_name
116 FROM profile_mentor_term AS m
117 INNER JOIN profile_job_term_enum AS e ON (m.jtid = e.jtid)
118 WHERE m.pid = {?}',
e5bcd851 119 $page->pid());
3ac45f10 120 $value = $res->fetchAllAssoc();
bdc3d2e9 121 } elseif (!is_array($value)) {
6457b5e4 122 $value = array();
3ac45f10
PC
123 } elseif (count($value) > 20) {
124 Platal::page()->trigError("Le nombre de mots clefs d'expertise est limité à 20.");
6457b5e4 125 $success = false;
3ac45f10
PC
126 } else {
127 $missing_full_names = array();
128 foreach ($value as &$term) if (empty($term['full_name'])) {
129 $missing_full_names[] = $term['jtid'];
130 }
131 if (count($missing_full_names)) {
132 $res = XDB::query('SELECT jtid, full_name
133 FROM profile_job_term_enum
134 WHERE jtid IN {?}',
135 $missing_full_names);
136 $term_id_to_name = $res->fetchAllAssoc('jtid', false);
137 foreach ($value as &$term) {
138 if (empty($term['full_name'])) {
139 $term['full_name'] = $term_id_to_name[$term['jtid']];
140 }
141 }
142 }
6457b5e4
FB
143 }
144 ksort($value);
145 foreach ($value as &$sss) {
146 ksort($sss);
147 }
148 return $value;
149 }
150
26ba053e 151 public function save(ProfilePage $page, $field, $value)
6457b5e4
FB
152 {
153
3ac45f10 154 XDB::execute("DELETE FROM profile_mentor_term
ce0b2c6f 155 WHERE pid = {?}",
e5bcd851 156 $page->pid());
6457b5e4
FB
157 if (!count($value)) {
158 return;
159 }
3ac45f10
PC
160 $mentor_term_values = array();
161 foreach ($value as &$term) {
162 $mentor_term_values[] = '('.XDB::escape($page->pid()).', '.XDB::escape($term['jtid']).')';
6457b5e4 163 }
3ac45f10
PC
164 XDB::execute('INSERT INTO profile_mentor_term (pid, jtid)
165 VALUES '.implode(',', $mentor_term_values));
166
6457b5e4 167 }
a0fce0c6
SJ
168
169 public function getText($value) {
3ac45f10
PC
170 $terms = array();
171 foreach ($value as &$term) {
172 $terms[] = $term['full_name'];
a0fce0c6 173 }
3ac45f10 174 return implode(', ', $terms);
a0fce0c6 175 }
6457b5e4
FB
176}
177
12bcf04b 178class ProfileSettingCountry implements ProfileSetting
6457b5e4 179{
26ba053e 180 public function value(ProfilePage $page, $field, $value, &$success)
6457b5e4
FB
181 {
182 $success = true;
183 if (is_null($value)) {
184 $value = array();
1c305d4c 185 $res = XDB::iterRow("SELECT m.country, gc.country
5fecdf6d 186 FROM profile_mentor_country AS m
e4cd7a1f 187 INNER JOIN geoloc_countries AS gc ON (m.country = gc.iso_3166_1_a2)
ce0b2c6f 188 WHERE m.pid = {?}",
e5bcd851 189 $page->pid());
6457b5e4
FB
190 while (list($id, $name) = $res->next()) {
191 $value[$id] = $name;
192 }
193 } else if (!is_array($value)) {
194 $value = array();
195 } else if (count($value) > 10) {
d7610c35 196 Platal::page()->trigError("Le nombre de secteurs d'expertise est limité à 10");
6457b5e4
FB
197 $success = false;
198 }
199 ksort($value);
200 return $value;
201 }
202
26ba053e 203 public function save(ProfilePage $page, $field, $value)
6457b5e4 204 {
5fecdf6d 205 XDB::execute("DELETE FROM profile_mentor_country
ce0b2c6f 206 WHERE pid = {?}",
e5bcd851 207 $page->pid());
6457b5e4 208 foreach ($value as $id=>&$name) {
ce0b2c6f 209 XDB::execute("INSERT INTO profile_mentor_country (pid, country)
6457b5e4 210 VALUES ({?}, {?})",
e5bcd851 211 $page->pid(), $id);
6457b5e4
FB
212 }
213 }
a0fce0c6
SJ
214
215 public function getText($value) {
216 return implode(', ', $value);
217 }
6457b5e4
FB
218}
219
220
66c4bdaf 221class ProfilePageMentor extends ProfilePage
6457b5e4
FB
222{
223 protected $pg_template = 'profile/mentor.tpl';
224
26ba053e 225 public function __construct(PlWizard $wiz)
6457b5e4
FB
226 {
227 parent::__construct($wiz);
228 $this->settings['expertise'] = null;
3ac45f10 229 $this->settings['terms'] = new ProfileSettingTerms();
12bcf04b 230 $this->settings['countries'] = new ProfileSettingCountry();
285e2fe7
SJ
231 $this->settings['competences'] = new ProfileSettingSkill('skill', 'id', 'cid', 'text_fr');
232 $this->settings['langues'] = new ProfileSettingSkill('langskill', 'iso_639_2b', 'lid', 'language');
6457b5e4
FB
233 }
234
7c2e0f0d 235 protected function _fetchData()
6457b5e4 236 {
6457b5e4 237 $res = XDB::query("SELECT expertise
bdc3d2e9 238 FROM profile_mentor
ce0b2c6f 239 WHERE pid = {?}",
e5bcd851 240 $this->pid());
e21e0b11 241 $this->values['expertise'] = $res->fetchOneCell();
6457b5e4
FB
242 }
243
7c2e0f0d 244 protected function _saveData()
6457b5e4 245 {
6457b5e4 246 if ($this->changed['expertise']) {
6dae45b3
FB
247 $expertise = trim($this->values['expertise']);
248 if (empty($expertise)) {
bdc3d2e9 249 XDB::execute("DELETE FROM profile_mentor
ce0b2c6f 250 WHERE pid = {?}",
e5bcd851 251 $this->pid());
6dae45b3
FB
252 $this->values['expertise'] = null;
253 } else {
00ba8a74
SJ
254 XDB::execute('INSERT INTO profile_mentor (pid, expertise)
255 VALUES ({?}, {?})
256 ON DUPLICATE KEY UPDATE expertise = VALUES(expertise)',
e5bcd851 257 $this->pid(), $expertise);
6dae45b3
FB
258 $this->values['expertise'] = $expertise;
259 }
6457b5e4
FB
260 }
261 }
262
26ba053e 263 public function _prepare(PlPage $page, $id)
6457b5e4 264 {
1c305d4c 265 $page->assign('countryList', XDB::iterator("SELECT iso_3166_1_a2, country
2aa2c77a 266 FROM geoloc_countries
1c305d4c 267 ORDER BY country"));
7a7167d3 268 $page->assign('hrpid', $this->profile->hrpid);
285e2fe7
SJ
269 $page->assign('comp_list', XDB::iterator("SELECT id, text_fr, FIND_IN_SET('titre',flags) AS title
270 FROM profile_skill_enum"));
271 $page->assign('comp_level', array('initié' => 'initié',
272 'bonne connaissance' => 'bonne connaissance',
273 'expert' => 'expert'));
274 $page->assign('lang_list', XDB::iterator('SELECT iso_639_2b, language
275 FROM profile_langskill_enum
276 ORDER BY language'));
277 $page->assign('lang_level', array(1 => 'connaissance basique',
278 2 => 'maîtrise des bases',
279 3 => 'maîtrise limitée',
280 4 => 'maîtrise générale',
281 5 => 'bonne maîtrise',
282 6 => 'maîtrise complète'));
6457b5e4
FB
283 }
284}
285
448c8cdc 286// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
6457b5e4 287?>