fgcf.module 15 KB

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