fgcf.module 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Non-features part of FGCF
  6. */
  7. include_once('fgcf.features.inc');
  8. /**
  9. * Implementation of hook_init().
  10. */
  11. function fgcf_init() {
  12. if (arg(0) == 'fgcf') {
  13. drupal_add_css(drupal_get_path('module', 'fgcf') .'/theme/css/fgcf.css');
  14. }
  15. }
  16. /**
  17. * Implementation of hook_menu().
  18. */
  19. function fgcf_menu() {
  20. $read_access = array('access course catalog');
  21. $items = array();
  22. // 'admin/settings/fg' est défini dans fgrhm, qui est une dépendance de ce module.
  23. $items['admin/settings/fg/fgcf'] = array(
  24. 'title' => 'Catalogue formations',
  25. 'description' => 'Réglages du catalogue de formations',
  26. 'type' => MENU_LOCAL_TASK,
  27. 'access arguments' => array('administer site configuration'),
  28. 'page callback' => 'drupal_get_form',
  29. 'page arguments' => array('fgcf_admin_settings'),
  30. 'file' => 'fgcf.admin.inc',
  31. 'weight' => 0,
  32. );
  33. $items['fgcf'] = array(
  34. 'title' => 'Catalogue formations',
  35. 'page callback' => 'fgcf_page_home',
  36. 'access arguments' => $read_access,
  37. 'menu_name' => 'primary-links',
  38. );
  39. $items['fgcf/format'] = array(
  40. 'type' => MENU_CALLBACK,
  41. 'title' => 'Formations par format',
  42. 'page callback' => 'fgcf_page_format',
  43. 'access arguments' => $read_access,
  44. );
  45. $items['fgcf/organisme'] = array(
  46. 'type' => MENU_CALLBACK,
  47. 'title' => 'Formations par organisme',
  48. 'page callback' => 'fgcf_page_organisme',
  49. 'access arguments' => $read_access,
  50. );
  51. $items['fgcf/thematique'] = array(
  52. 'type' => MENU_CALLBACK,
  53. 'title' => 'Formations par thématique',
  54. 'page callback' => 'fgcf_page_thematique',
  55. 'access arguments' => $read_access,
  56. );
  57. $items['fgcf/odt'] = array(
  58. 'title' => 'ODT Test',
  59. 'page callback' => 'fgcf_page_odt',
  60. 'access arguments' => array('create fgcf_fiche content'),
  61. );
  62. return $items;
  63. }
  64. /**
  65. * Implementation of hook_perm().
  66. */
  67. function fgcf_perm() {
  68. return array('access course catalog');
  69. }
  70. /**
  71. * Implementation of hook_theme().
  72. */
  73. function fgcf_theme($existing, $type, $theme, $path) {
  74. $ret = array(
  75. 'fgcf_home' => array(
  76. 'arguments' => array('filter_form' => array()),
  77. 'template' => 'fgcf-home',
  78. 'path' => $path .'/theme',
  79. ),
  80. 'fgcf_thematiques' => array(
  81. 'arguments' => array('terms' => array()),
  82. ),
  83. );
  84. return $ret;
  85. }
  86. /**
  87. * Implementation of hook_term_path().
  88. */
  89. function fgcf_term_path($term) {
  90. $vocabulary = taxonomy_vocabulary_load($term->vid);
  91. switch ($vocabulary->vid) {
  92. case _fgcf_get_vocabulary_by_name('FGCF Thématique'):
  93. return 'fgcf/thematique/'. $term->tid;
  94. break;
  95. case _fgcf_get_vocabulary_by_name('FGCF Format de cours'):
  96. return 'fgcf/format/'. $term->tid;
  97. break;
  98. default:
  99. // Terms in other vocabs should be set to display 'as text' (no link)
  100. break;
  101. }
  102. }
  103. /**
  104. * Implementation of hook_views_api().
  105. */
  106. function fgcf_views_api() {
  107. $ret = array(
  108. 'api' => '2',
  109. 'path' => drupal_get_path('module', 'fgcf') . '/views',
  110. );
  111. return $ret;
  112. }
  113. /**
  114. * Implementation of hook_nodeapi().
  115. */
  116. function fgcf_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  117. switch ($op) {
  118. case 'view':
  119. // Only operate on 'page' view.
  120. if ($a3 /* !teaser */ || !$a4 /* page */) {
  121. break;
  122. }
  123. drupal_add_css(drupal_get_path('module', 'fgcf') .'/theme/css/fgcf.css');
  124. switch ($node->type) {
  125. case 'fgcf_prestataire':
  126. $bc = drupal_get_breadcrumb();
  127. $bc[] = l(t('Catalogue formations'), 'fgcf');
  128. $bc[] = l(t('Organismes'), 'fgcf/organisme');
  129. drupal_set_breadcrumb($bc);
  130. break;
  131. case 'fgcf_fiche':
  132. $nat_config = variable_get('nat_config', NULL);
  133. $vid = isset($nat_config['types']['fgcf_thematique'])
  134. ? reset($nat_config['types']['fgcf_thematique'])
  135. : 0;
  136. unset($nat_config);
  137. $taxo2 = array();
  138. foreach ($node->taxonomy as $tid => $term) {
  139. if ($term->vid != $vid) {
  140. $taxo2[$tid] = $term;
  141. }
  142. else {
  143. $node->fgcf_scat = $term; // Single by configuration
  144. }
  145. }
  146. $node->taxonomy = $taxo2;
  147. unset($taxo2, $tid, $term);
  148. // Build cat/scat[/scat]* path
  149. $cats = array();
  150. $parents = taxonomy_get_parents($node->fgcf_scat->tid);
  151. foreach ($parents as $tid => $term) {
  152. $cats[] = $term;
  153. }
  154. $cats[] = $node->fgcf_scat;
  155. $node->content['fgcf_catpath'] = array(
  156. '#value' => theme('fgcf_thematiques', $cats),
  157. );
  158. // Add 'Subscribe' button.
  159. $node->content['fgcf_subscribe'] = array(
  160. '#value' => drupal_get_form('fgcf_subscribe_form', $node),
  161. '#weight' => -100,
  162. );
  163. $bc = drupal_get_breadcrumb();
  164. $bc[] = l(t('Catalogue formations'), 'fgcf');
  165. $bc[] = l(t('Thématiques'), 'fgcf/thematique');
  166. $bc[] = l($cats[0]->description, 'fgcf/thematique/'. $cats[0]->tid);
  167. $bc[] = l($cats[1]->description, 'fgcf/thematique/'. $cats[1]->tid);
  168. drupal_set_breadcrumb($bc);
  169. unset($cats, $parents, $tid, $term);
  170. break;
  171. }
  172. }
  173. }
  174. /**
  175. * Page callback : 'Catalogue Formations'.
  176. */
  177. function fgcf_page_home() {
  178. drupal_add_css(drupal_get_path('module', 'fgcf') .'/theme/css/fgcf.css');
  179. drupal_set_title('Catalogue des formations France Galop');
  180. $filter_form = drupal_get_form('fgcf_filter_form');
  181. $ret = theme('fgcf_home', $filter_form);
  182. return $ret;
  183. }
  184. /**
  185. * Page callback : 'Formations par format'.
  186. */
  187. function fgcf_page_format($format = 0) {
  188. $bc = drupal_get_breadcrumb();
  189. $bc[] = l(t('Catalogue formations'), 'fgcf');
  190. $filter_form = drupal_get_form('fgcf_filter_form');
  191. $ret = $filter_form;
  192. $vid = _fgcf_get_vocabulary_by_name('FGCF Format de cours');
  193. if (empty($format)) {
  194. $ret .= views_embed_view('fgcf_formats_de_formations', 'default', $vid);
  195. }
  196. else {
  197. $term = taxonomy_get_term($format);
  198. if (!is_object($term) || !isset($term->vid) || $term->vid != $vid) {
  199. $format = 0;
  200. }
  201. else {
  202. $bc[] = l(t('Formats de formation'), 'fgcf/format');
  203. drupal_set_title(t('Format : @term', array('@term' => $term->name)));
  204. $values = _fgcf_filter_form_translate_values();
  205. }
  206. $ret .= views_embed_view('fgcf_formations_par_format', 'default', $format, $values['dif'], $values['modalites']);
  207. }
  208. drupal_set_breadcrumb($bc);
  209. return $ret;
  210. }
  211. /**
  212. * Page callback : 'Formations par organisme'.
  213. */
  214. function fgcf_page_organisme() {
  215. $bc = drupal_get_breadcrumb();
  216. $bc[] = l(t('Catalogue formations'), 'fgcf');
  217. drupal_set_breadcrumb($bc);
  218. $filter_form = drupal_get_form('fgcf_filter_form');
  219. $ret = views_embed_view('fgcf_organismes_de_formation');
  220. return $ret;
  221. }
  222. /**
  223. * Page callback : 'Formations par thematique'.
  224. */
  225. function fgcf_page_thematique($thematique = 0) {
  226. $bc = drupal_get_breadcrumb();
  227. $bc[] = l(t('Catalogue formations'), 'fgcf');
  228. $filter_form = drupal_get_form('fgcf_filter_form');
  229. $ret = $filter_form;
  230. // The views itself doesn't mention vocab ids, which might be different in dev
  231. // and in production.
  232. // We limit to terms in the 'Thématique' vocab outside the view.
  233. $vid = _fgcf_get_vocabulary_by_name('FGCF Thématique');
  234. if (empty($thematique)) {
  235. // Afficher liste des thématiques de 1er niveau.
  236. $ret .= views_embed_view('fgcf_thematiques_premier_niveau', 'default', $vid);
  237. }
  238. else {
  239. $term = taxonomy_get_term($thematique);
  240. $parents = array();
  241. if (!is_object($term) || !isset($term->vid) || $term->vid != $vid) {
  242. $thematique = 0;
  243. $description = '';
  244. }
  245. else {
  246. $bc[] = l(t('Thématiques'), 'fgcf/thematique');
  247. $parents = taxonomy_get_parents($thematique);
  248. $description = $term->description;
  249. }
  250. if (empty($parents)) {
  251. // Top category: display list of sub-categories.
  252. drupal_set_title(t('Thématique : @term', array('@term' => $description)));
  253. $ret .= views_embed_view('fgcf_thematique_deuxieme_niveau', 'default', $thematique);
  254. }
  255. else {
  256. // Sub-category: display list of nodes.
  257. $parent = current($parents);
  258. $bc[] = l($parent->description, 'fgcf/thematique/'. $parent->tid);
  259. drupal_set_title(t('Thématique : @term', array('@term' => $description)));
  260. $values = _fgcf_filter_form_translate_values();
  261. $ret .= views_embed_view('fgcf_formations_par_thematique', 'default', $thematique, $values['dif'], $values['modalites']);
  262. }
  263. }
  264. drupal_set_breadcrumb($bc);
  265. return $ret;
  266. }
  267. /**
  268. * Page callback : 'ODT test'.
  269. */
  270. function fgcf_page_odt() {
  271. module_load_include('inc', 'fgcf', 'fgcf.odt');
  272. $template = dirname(__FILE__) ."/catalogue_template.odt";
  273. $template = "catalogue_template.odt";
  274. $odf = fgcf_odt_export_formations($template);
  275. $odf->exportAsAttachedFile('catalogue.odt');
  276. }
  277. /**
  278. * Theme function for course themes.
  279. */
  280. function theme_fgcf_thematiques($terms) {
  281. $items = array();
  282. foreach ($terms as $tid => $term) {
  283. $items[] = l($term->description, taxonomy_term_path($term));
  284. }
  285. // Build cat logo
  286. $cat_term = reset($terms);
  287. $nodes = nat_get_nids(array($cat_term->tid), TRUE);
  288. $cat_node = reset($nodes);
  289. $node_view = node_build_content($cat_node, FALSE, FALSE);
  290. $logo = drupal_render($node_view->content['field_fgcf_thematique_logo']);
  291. array_unshift($items, $logo);
  292. $ret = '<div class="node-thematique">'. theme('item_list', $items) .'</div>';
  293. return $ret;
  294. }
  295. /**
  296. * Subscribe form displayed on 'formations' nodes.
  297. */
  298. function fgcf_subscribe_form($form_state, $node) {
  299. global $user;
  300. $form['node'] = array(
  301. '#type' => 'value',
  302. '#value' => $node,
  303. );
  304. if ($user->uid) {
  305. $form['subscribe'] = array(
  306. '#type' => 'submit',
  307. '#value' => t('Inscrivez-moi'),
  308. );
  309. }
  310. return $form;
  311. }
  312. /**
  313. * Submit handler for the subscribe form.
  314. *
  315. * Send an email to site administrators.
  316. */
  317. function fgcf_subscribe_form_submit($form, &$form_state) {
  318. global $user;
  319. // Prevent flooding.
  320. $flood_limit = variable_get('fgcf_subscribe_flood', 10);
  321. if ($flood_limit && flood_is_allowed('fgcf_subscribe', $flood_limit)) {
  322. $to = variable_get('fgcf_mail_destination', variable_get('site_mail', ini_get('sendmail_from')));
  323. $params = array(
  324. 'account' => $user,
  325. 'node' => $form_state['values']['node'],
  326. );
  327. $message = drupal_mail('fgcf', 'subscribe', $to, language_default(), $params);
  328. if ($message['result']) {
  329. drupal_set_message(t("Votre demande a été transmise aux administrateurs du site."));
  330. flood_register_event('fgcf_subscribe');
  331. }
  332. }
  333. }
  334. /**
  335. * Implementation of hook_mail().
  336. */
  337. function fgcf_mail($key, &$message, $params) {
  338. $language = $message['language'];
  339. switch ($key) {
  340. case 'subscribe':
  341. // Generate base replacement strings.
  342. $variables = user_mail_tokens($params['account'], $language);
  343. // Add our own.
  344. $node = $params['node'];
  345. $variables['@node_title'] = $node->title;
  346. $variables['@node_reference'] = $node->field_fgcf_fiche_reference[0]['value'];
  347. $variables['!node_uri'] = url('node/' . $node->nid, array('absolute' => TRUE));
  348. $variables['!user_ip'] = ip_address();
  349. $message['subject'] = t('[France-galop] Inscription à une formation', $variables, $language->language);
  350. $message['body'][] = t("Formation: @node_reference - @node_title (!node_uri)\nUtilisateur: !username\nMail: !mailto\nIP de connection: !user_ip", $variables, $language->language);
  351. break;
  352. }
  353. }
  354. /**
  355. * Shared filter form for course lists.
  356. *
  357. * Filter values are taken from the form logic or, when absent, from the
  358. * session, in order for them to be persistent across non-form pages.
  359. *
  360. * @return array
  361. */
  362. function fgcf_filter_form($form_state) {
  363. $form['filtre'] = array(
  364. '#title' => t('Filtrer les formations'),
  365. '#attributes' => array('class' => 'fgcf-filtre'),
  366. '#type' => 'fieldset',
  367. '#collapsible' => TRUE,
  368. );
  369. $dif = isset($form_state['storage']['dif'])
  370. ? $form_state['storage']['dif']
  371. : isset($_SESSION['fgcf']['dif'])
  372. ? $_SESSION['fgcf']['dif']
  373. : FALSE;
  374. $form['filtre']['dif'] = array(
  375. '#type' => 'checkbox',
  376. '#default_value' => $dif,
  377. '#title' => t('Eligibles DIF'),
  378. );
  379. $modalite = isset($form_state['storage']['modalite'])
  380. ? $form_state['storage']['modalite']
  381. : isset($_SESSION['fgcf']['modalite'])
  382. ? $_SESSION['fgcf']['modalite']
  383. : array();
  384. $form['filtre']['modalite'] = array(
  385. '#title' => t('Modalité'),
  386. '#type' => 'checkboxes',
  387. '#options' => array(
  388. '1' => t('Inter-entreprises'),
  389. '2' => t('Intra-entreprise'),
  390. '999' => t('Autres'),
  391. ),
  392. '#default_value' => $modalite,
  393. );
  394. $form['filtre']['submit'] = array(
  395. '#type' => 'submit',
  396. '#value' => t('Appliquer le filtre'),
  397. );
  398. return $form;
  399. }
  400. /**
  401. * Submit handler for fgcf_filter_form().
  402. */
  403. function fgcf_filter_form_submit($form, &$form_state) {
  404. foreach (array('modalite', 'dif') as $key) {
  405. $value = $form_state['values'][$key];
  406. $form_state['storage'][$key] = $value;
  407. $_SESSION['fgcf'][$key] = $value;
  408. }
  409. }
  410. /**
  411. * Translate values in the filter form into arguments for the views.
  412. */
  413. function _fgcf_filter_form_translate_values() {
  414. // Default values.
  415. $values = array(
  416. 'dif' => 'all',
  417. 'modalites' => 'all',
  418. );
  419. if (isset($_SESSION['fgcf']['dif']) && $_SESSION['fgcf']['dif']) {
  420. $values['dif'] = 'o';
  421. }
  422. if (isset($_SESSION['fgcf']['modalite'])) {
  423. $modalites = array_filter($_SESSION['fgcf']['modalite']);
  424. if (!empty($modalites)) {
  425. // 'Autres' (999) means 'all other values than 1 and 2'.
  426. // Translate this into actual values.
  427. if (isset($modalites[999])) {
  428. // Get the actual possible values from the field definition.
  429. $field = content_fields('field_fgcf_fiche_modalite', 'fgcf_fiche');
  430. $allowed_values = content_allowed_values($field);
  431. // Remove the first 2 options.
  432. unset($allowed_values[1], $allowed_values[2]);
  433. // Remove the 'proxy' 'Autres' choice.
  434. unset($modalites[999]);
  435. // Add the actual values.
  436. $modalites += drupal_map_assoc(array_keys($allowed_values));
  437. }
  438. $values['modalites'] = implode('+', array_keys($modalites));
  439. }
  440. }
  441. return $values;
  442. }
  443. /**
  444. * Helper: get a vocabulary ID from its name.
  445. *
  446. * @todo : inefficient - replace with constants once the vocabs are deployed ?
  447. *
  448. * @param string $name
  449. */
  450. function _fgcf_get_vocabulary_by_name($name) {
  451. static $matches = array();
  452. if (!isset($matches[$name])) {
  453. $matches[$name] = 0;
  454. $vocabularies = taxonomy_get_vocabularies();
  455. foreach ($vocabularies as $vid => $vocabulary) {
  456. if (drupal_strtolower($vocabulary->name) == drupal_strtolower($name)) {
  457. $matches[$name] = $vid;
  458. break;
  459. }
  460. }
  461. }
  462. return $matches[$name];
  463. }