Happy New Year!
[platal.git] / modules / profile / mentor.inc.php
CommitLineData
6457b5e4
FB
1<?php
2/***************************************************************************
12262f13 3 * Copyright (C) 2003-2011 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
3ac45f10
PC
22/** Terms associated to profile mentoring */
23class ProfileSettingTerms implements ProfileSetting
6457b5e4
FB
24{
25 public function value(ProfilePage &$page, $field, $value, &$success)
26 {
27 $success = true;
28 if (is_null($value)) {
29 $value = array();
3ac45f10
PC
30 $res = XDB::query('SELECT e.jtid, e.full_name
31 FROM profile_mentor_term AS m
32 INNER JOIN profile_job_term_enum AS e ON (m.jtid = e.jtid)
33 WHERE m.pid = {?}',
e5bcd851 34 $page->pid());
3ac45f10 35 $value = $res->fetchAllAssoc();
bdc3d2e9 36 } elseif (!is_array($value)) {
6457b5e4 37 $value = array();
3ac45f10
PC
38 } elseif (count($value) > 20) {
39 Platal::page()->trigError("Le nombre de mots clefs d'expertise est limité à 20.");
6457b5e4 40 $success = false;
3ac45f10
PC
41 } else {
42 $missing_full_names = array();
43 foreach ($value as &$term) if (empty($term['full_name'])) {
44 $missing_full_names[] = $term['jtid'];
45 }
46 if (count($missing_full_names)) {
47 $res = XDB::query('SELECT jtid, full_name
48 FROM profile_job_term_enum
49 WHERE jtid IN {?}',
50 $missing_full_names);
51 $term_id_to_name = $res->fetchAllAssoc('jtid', false);
52 foreach ($value as &$term) {
53 if (empty($term['full_name'])) {
54 $term['full_name'] = $term_id_to_name[$term['jtid']];
55 }
56 }
57 }
6457b5e4
FB
58 }
59 ksort($value);
60 foreach ($value as &$sss) {
61 ksort($sss);
62 }
63 return $value;
64 }
65
66 public function save(ProfilePage &$page, $field, $value)
67 {
68
3ac45f10 69 XDB::execute("DELETE FROM profile_mentor_term
ce0b2c6f 70 WHERE pid = {?}",
e5bcd851 71 $page->pid());
6457b5e4
FB
72 if (!count($value)) {
73 return;
74 }
3ac45f10
PC
75 $mentor_term_values = array();
76 foreach ($value as &$term) {
77 $mentor_term_values[] = '('.XDB::escape($page->pid()).', '.XDB::escape($term['jtid']).')';
6457b5e4 78 }
3ac45f10
PC
79 XDB::execute('INSERT INTO profile_mentor_term (pid, jtid)
80 VALUES '.implode(',', $mentor_term_values));
81
6457b5e4 82 }
a0fce0c6
SJ
83
84 public function getText($value) {
3ac45f10
PC
85 $terms = array();
86 foreach ($value as &$term) {
87 $terms[] = $term['full_name'];
a0fce0c6 88 }
3ac45f10 89 return implode(', ', $terms);
a0fce0c6 90 }
6457b5e4
FB
91}
92
12bcf04b 93class ProfileSettingCountry implements ProfileSetting
6457b5e4
FB
94{
95 public function value(ProfilePage &$page, $field, $value, &$success)
96 {
97 $success = true;
98 if (is_null($value)) {
99 $value = array();
1c305d4c 100 $res = XDB::iterRow("SELECT m.country, gc.country
5fecdf6d 101 FROM profile_mentor_country AS m
e4cd7a1f 102 INNER JOIN geoloc_countries AS gc ON (m.country = gc.iso_3166_1_a2)
ce0b2c6f 103 WHERE m.pid = {?}",
e5bcd851 104 $page->pid());
6457b5e4
FB
105 while (list($id, $name) = $res->next()) {
106 $value[$id] = $name;
107 }
108 } else if (!is_array($value)) {
109 $value = array();
110 } else if (count($value) > 10) {
d7610c35 111 Platal::page()->trigError("Le nombre de secteurs d'expertise est limité à 10");
6457b5e4
FB
112 $success = false;
113 }
114 ksort($value);
115 return $value;
116 }
117
118 public function save(ProfilePage &$page, $field, $value)
119 {
5fecdf6d 120 XDB::execute("DELETE FROM profile_mentor_country
ce0b2c6f 121 WHERE pid = {?}",
e5bcd851 122 $page->pid());
6457b5e4 123 foreach ($value as $id=>&$name) {
ce0b2c6f 124 XDB::execute("INSERT INTO profile_mentor_country (pid, country)
6457b5e4 125 VALUES ({?}, {?})",
e5bcd851 126 $page->pid(), $id);
6457b5e4
FB
127 }
128 }
a0fce0c6
SJ
129
130 public function getText($value) {
131 return implode(', ', $value);
132 }
6457b5e4
FB
133}
134
135
66c4bdaf 136class ProfilePageMentor extends ProfilePage
6457b5e4
FB
137{
138 protected $pg_template = 'profile/mentor.tpl';
139
140 public function __construct(PlWizard &$wiz)
141 {
142 parent::__construct($wiz);
143 $this->settings['expertise'] = null;
3ac45f10 144 $this->settings['terms'] = new ProfileSettingTerms();
12bcf04b 145 $this->settings['countries'] = new ProfileSettingCountry();
6457b5e4
FB
146 }
147
7c2e0f0d 148 protected function _fetchData()
6457b5e4 149 {
6457b5e4 150 $res = XDB::query("SELECT expertise
bdc3d2e9 151 FROM profile_mentor
ce0b2c6f 152 WHERE pid = {?}",
e5bcd851 153 $this->pid());
e21e0b11 154 $this->values['expertise'] = $res->fetchOneCell();
6457b5e4
FB
155 }
156
7c2e0f0d 157 protected function _saveData()
6457b5e4 158 {
6457b5e4 159 if ($this->changed['expertise']) {
6dae45b3
FB
160 $expertise = trim($this->values['expertise']);
161 if (empty($expertise)) {
bdc3d2e9 162 XDB::execute("DELETE FROM profile_mentor
ce0b2c6f 163 WHERE pid = {?}",
e5bcd851 164 $this->pid());
6dae45b3
FB
165 $this->values['expertise'] = null;
166 } else {
00ba8a74
SJ
167 XDB::execute('INSERT INTO profile_mentor (pid, expertise)
168 VALUES ({?}, {?})
169 ON DUPLICATE KEY UPDATE expertise = VALUES(expertise)',
e5bcd851 170 $this->pid(), $expertise);
6dae45b3
FB
171 $this->values['expertise'] = $expertise;
172 }
6457b5e4
FB
173 }
174 }
175
04334c61 176 public function _prepare(PlPage &$page, $id)
6457b5e4 177 {
1c305d4c 178 $page->assign('countryList', XDB::iterator("SELECT iso_3166_1_a2, country
2aa2c77a 179 FROM geoloc_countries
1c305d4c 180 ORDER BY country"));
7a7167d3 181 $page->assign('hrpid', $this->profile->hrpid);
6457b5e4
FB
182 }
183}
184
185// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
186?>