fgcf.module 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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('filter_form' => array()),
  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. $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 support'.
  188. */
  189. function fgcf_page_support($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 Supports de cours');
  195. if (empty($format)) {
  196. $ret .= views_embed_view('fgcf_supports_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('Supports de formation'), 'fgcf/support');
  205. drupal_set_title(t('Support: @term', array('@term' => $term->name)));
  206. $values = _fgcf_filter_form_translate_values();
  207. }
  208. $ret .= views_embed_view('fgcf_formations_par_support', '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ème' vocab outside the view.
  235. $vid = _fgcf_get_vocabulary_by_name('FGCF Thème');
  236. if (empty($thematique)) {
  237. // Afficher liste des thèmes 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èmes'), 'fgcf/theme');
  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ème : @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/theme/'. $parent->tid);
  261. drupal_set_title(t('Thème : @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. $flood_limit = variable_get('fgcf_subscribe_flood', 10);
  323. if ($flood_limit && flood_is_allowed('fgcf_subscribe', $flood_limit)) {
  324. $to = variable_get('fgcf_mail_destination', variable_get('site_mail', ini_get('sendmail_from')));
  325. $params = array(
  326. 'account' => $user,
  327. 'node' => $form_state['values']['node'],
  328. );
  329. $message = drupal_mail('fgcf', 'subscribe', $to, language_default(), $params);
  330. if ($message['result']) {
  331. drupal_set_message(t("Votre demande a été transmise aux administrateurs du site."));
  332. flood_register_event('fgcf_subscribe');
  333. }
  334. }
  335. }
  336. /**
  337. * Implementation of hook_mail().
  338. */
  339. function fgcf_mail($key, &$message, $params) {
  340. $language = $message['language'];
  341. switch ($key) {
  342. case 'subscribe':
  343. // Generate base replacement strings.
  344. $variables = user_mail_tokens($params['account'], $language);
  345. // Add our own.
  346. $node = $params['node'];
  347. $variables['@node_title'] = $node->title;
  348. $variables['@node_reference'] = $node->field_fgcf_fiche_reference[0]['value'];
  349. $variables['!node_uri'] = url('node/' . $node->nid, array('absolute' => TRUE));
  350. $variables['!user_ip'] = ip_address();
  351. $message['subject'] = t('[France-galop] Inscription à une formation', $variables, $language->language);
  352. $message['body'][] = t("Formation: @node_reference - @node_title (!node_uri)\nUtilisateur: !username\nMail: !mailto\nIP de connection: !user_ip", $variables, $language->language);
  353. break;
  354. }
  355. }
  356. /**
  357. * Shared filter form for course lists.
  358. *
  359. * Filter values are taken from the form logic or, when absent, from the
  360. * session, in order for them to be persistent across non-form pages.
  361. *
  362. * @return array
  363. */
  364. function fgcf_filter_form($form_state) {
  365. $form['filtre'] = array(
  366. '#title' => t('Filtrer les formations'),
  367. '#attributes' => array('class' => 'fgcf-filtre'),
  368. '#type' => 'fieldset',
  369. '#collapsible' => TRUE,
  370. );
  371. $dif = isset($form_state['storage']['dif'])
  372. ? $form_state['storage']['dif']
  373. : isset($_SESSION['fgcf']['dif'])
  374. ? $_SESSION['fgcf']['dif']
  375. : FALSE;
  376. $form['filtre']['dif'] = array(
  377. '#type' => 'checkbox',
  378. '#default_value' => $dif,
  379. '#title' => t('Eligibles DIF'),
  380. );
  381. $modalite = isset($form_state['storage']['modalite'])
  382. ? $form_state['storage']['modalite']
  383. : isset($_SESSION['fgcf']['modalite'])
  384. ? $_SESSION['fgcf']['modalite']
  385. : array();
  386. $form['filtre']['modalite'] = array(
  387. '#title' => t('Modalité'),
  388. '#type' => 'checkboxes',
  389. '#options' => array(
  390. '1' => t('Inter-entreprises'),
  391. '2' => t('Intra-entreprise'),
  392. '999' => t('Autres'),
  393. ),
  394. '#default_value' => $modalite,
  395. );
  396. $form['filtre']['submit'] = array(
  397. '#type' => 'submit',
  398. '#value' => t('Appliquer le filtre'),
  399. );
  400. return $form;
  401. }
  402. /**
  403. * Submit handler for fgcf_filter_form().
  404. */
  405. function fgcf_filter_form_submit($form, &$form_state) {
  406. foreach (array('modalite', 'dif') as $key) {
  407. $value = $form_state['values'][$key];
  408. $form_state['storage'][$key] = $value;
  409. $_SESSION['fgcf'][$key] = $value;
  410. }
  411. }
  412. /**
  413. * Translate values in the filter form into arguments for the views.
  414. */
  415. function _fgcf_filter_form_translate_values() {
  416. // Default values.
  417. $values = array(
  418. 'dif' => 'all',
  419. 'modalites' => 'all',
  420. );
  421. if (isset($_SESSION['fgcf']['dif']) && $_SESSION['fgcf']['dif']) {
  422. $values['dif'] = 'o';
  423. }
  424. if (isset($_SESSION['fgcf']['modalite'])) {
  425. $modalites = array_filter($_SESSION['fgcf']['modalite']);
  426. if (!empty($modalites)) {
  427. // 'Autres' (999) means 'all other values than 1 and 2'.
  428. // Translate this into actual values.
  429. if (isset($modalites[999])) {
  430. // Get the actual possible values from the field definition.
  431. $field = content_fields('field_fgcf_fiche_modalite', 'fgcf_fiche');
  432. $allowed_values = content_allowed_values($field);
  433. // Remove the first 2 options.
  434. unset($allowed_values[1], $allowed_values[2]);
  435. // Remove the 'proxy' 'Autres' choice.
  436. unset($modalites[999]);
  437. // Add the actual values.
  438. $modalites += drupal_map_assoc(array_keys($allowed_values));
  439. }
  440. $values['modalites'] = implode('+', array_keys($modalites));
  441. }
  442. }
  443. return $values;
  444. }
  445. /**
  446. * Helper: get a vocabulary ID from its name.
  447. *
  448. * @todo : inefficient - replace with constants once the vocabs are deployed ?
  449. *
  450. * @param string $name
  451. */
  452. function _fgcf_get_vocabulary_by_name($name) {
  453. static $matches = array();
  454. if (!isset($matches[$name])) {
  455. $matches[$name] = 0;
  456. $vocabularies = taxonomy_get_vocabularies();
  457. foreach ($vocabularies as $vid => $vocabulary) {
  458. if (drupal_strtolower($vocabulary->name) == drupal_strtolower($name)) {
  459. $matches[$name] = $vid;
  460. break;
  461. }
  462. }
  463. }
  464. return $matches[$name];
  465. }