fgcf.module 13 KB

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