- $page->changeTpl('profile/nomusage.tpl');
-
- require_once 'validations.inc.php';
-
- $res = XDB::query(
- "SELECT u.nom, u.nom_usage, u.flags, e.alias
- FROM auth_user_md5 AS u
- LEFT JOIN aliases AS e ON(u.user_id = e.id
- AND FIND_IN_SET('usage', e.flags))
- WHERE user_id={?}", S::v('uid'));
-
- list($nom, $usage_old, $flags, $alias_old) = $res->fetchOneRow();
- $flags = new PlFlagSet($flags);
- $page->assign('usage_old', $usage_old);
- $page->assign('alias_old', $alias_old);
-
- $nom_usage = replace_accent(trim(Env::v('nom_usage')));
- $nom_usage = strtoupper($nom_usage);
- $page->assign('usage_req', $nom_usage);
-
- if (Env::has('submit') && ($nom_usage != $usage_old)) {
- S::assert_xsrf_token();
-
- // on vient de recevoir une requete, differente de l'ancien nom d'usage
- if ($nom_usage == $nom) {
- $page->trigWarning('Le nom d\'usage que tu demandes est identique à ton nom à l\'X, '
- . 'aucune modification n\'a donc été effectuée.');
- $page->assign('same', true);
- } else { // le nom de mariage est distinct du nom à l'X
- // on calcule l'alias pour l'afficher
- $reason = Env::v('reason');
- if ($reason == 'other') {
- $reason = Env::v('other_reason');
+ $this->handler_jobterms($page, 'mentor');
+ }
+
+ /**
+ * Page for url "profile/jobterms" (function also used for "referent/autocomplete" @see
+ * handler_ref_autocomplete). Displays an "autocomplete" page (plain text with values
+ * separated by "|" chars) for jobterms to add in profile.
+ * @param $page the Platal page
+ * @param $type set to 'mentor' to display the number of mentors for each term and order
+ * by descending number of mentors.
+ *
+ * @param Env::v('q') the text that has been typed and to complete automatically
+ */
+ function handler_jobterms($page, $type = 'nomentor')
+ {
+ pl_content_headers("text/plain");
+
+ $q = Env::v('term') . '%';
+ $tokens = JobTerms::tokenize($q);
+ if (count($tokens) == 0) {
+ exit;
+ }
+ sort($tokens);
+ $q_normalized = implode(' ', $tokens);
+
+ // Try to look in cached results.
+ $cached = false;
+ $cache = XDB::query('SELECT result
+ FROM search_autocomplete
+ WHERE name = {?} AND query = {?} AND generated > NOW() - INTERVAL 1 DAY',
+ $type, $q_normalized);
+
+ if ($cache->numRows() > 0) {
+ $cached = true;
+ $data = explode("\n", $cache->fetchOneCell());
+ $list = array();
+ foreach ($data as $line) {
+ if ($line != '') {
+ $aux = explode("\t", $line);
+ if ($type == 'mentor') {
+ $item = array(
+ 'field' => $aux[0],
+ 'nb' => $aux[1],
+ 'id' => $aux[2]
+ );
+ $item['value'] = SearchModule::format_autocomplete($item);
+ } else {
+ $item = array(
+ 'value' => $aux[0],
+ 'id' => $aux[1]
+ );
+ }
+ array_push($list, $item);
+ }
+ }
+ } else {
+ $joins = JobTerms::token_join_query($tokens, 'e');
+ if ($type == 'mentor') {
+ $count = ', COUNT(DISTINCT pid) AS nb';
+ $countjoin = ' INNER JOIN profile_job_term_relation AS r ON(r.jtid_1 = e.jtid) INNER JOIN profile_mentor_term AS m ON(r.jtid_2 = m.jtid)';
+ $countorder = 'nb DESC, ';
+ } else {
+ $count = $countjoin = $countorder = '';
+ }
+ $list = XDB::fetchAllAssoc('SELECT e.jtid AS id, e.full_name AS field' . $count . '
+ FROM profile_job_term_enum AS e ' . $joins . $countjoin . '
+ GROUP BY e.jtid
+ ORDER BY ' . $countorder . 'field
+ LIMIT ' . DirEnumeration::AUTOCOMPLETE_LIMIT);
+ $to_cache = '';
+ if ($type == 'mentor') {
+ foreach ($list as &$item) {
+ $to_cache .= $item['field'] . "\t" . $item['nb'] . "\t" . $item['id'] . "\n";
+ $item['value'] = SearchModule::format_autocomplete($item);
+ }
+ } else {
+ foreach ($list as &$item) {
+ $to_cache .= $item['field'] . "\t" . $item['id'] . "\n";
+ $item['value'] = $item['field'];