fgcf.module 15 KB

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