fgcf.module 14 KB

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