fgcf.module 13 KB

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