fgcf.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. $bc = drupal_get_breadcrumb();
  148. $bc[] = l(t('Catalogue formations'), 'fgcf');
  149. $bc[] = l(t('Thématiques'), 'fgcf/thematique');
  150. $bc[] = l($cats[0]->description, 'fgcf/thematique/'. $cats[0]->tid);
  151. $bc[] = l($cats[1]->description, 'fgcf/thematique/'. $cats[1]->tid);
  152. drupal_set_breadcrumb($bc);
  153. unset($cats, $parents, $tid, $term);
  154. break;
  155. }
  156. }
  157. }
  158. /**
  159. * Page callback : 'Catalogue Formations'.
  160. */
  161. function fgcf_page_home() {
  162. drupal_add_css(drupal_get_path('module', 'fgcf') .'/theme/css/fgcf.css');
  163. drupal_set_title('Catalogue des formations France Galop');
  164. $filter_form = drupal_get_form('fgcf_filter_form');
  165. $ret = theme('fgcf_home', $filter_form);
  166. return $ret;
  167. }
  168. /**
  169. * Page callback : 'Formations par format'.
  170. */
  171. function fgcf_page_format($format = 0) {
  172. $bc = drupal_get_breadcrumb();
  173. $bc[] = l(t('Catalogue formations'), 'fgcf');
  174. $filter_form = drupal_get_form('fgcf_filter_form');
  175. $ret = $filter_form;
  176. $vid = _fgcf_get_vocabulary_by_name('FGCF Format de cours');
  177. if (empty($format)) {
  178. $ret .= views_embed_view('fgcf_formats_de_formations', 'default', $vid);
  179. }
  180. else {
  181. $term = taxonomy_get_term($format);
  182. if (!is_object($term) || !isset($term->vid) || $term->vid != $vid) {
  183. $format = 0;
  184. }
  185. else {
  186. $bc[] = l(t('Formats de formation'), 'fgcf/format');
  187. drupal_set_title(t('Format : @term', array('@term' => $term->name)));
  188. $values = _fgcf_filter_form_translate_values();
  189. }
  190. $ret .= views_embed_view('fgcf_formations_par_format', 'default', $format, $values['dif'], $values['modalites']);
  191. }
  192. drupal_set_breadcrumb($bc);
  193. return $ret;
  194. }
  195. /**
  196. * Page callback : 'Formations par organisme'.
  197. */
  198. function fgcf_page_organisme() {
  199. $bc = drupal_get_breadcrumb();
  200. $bc[] = l(t('Catalogue formations'), 'fgcf');
  201. drupal_set_breadcrumb($bc);
  202. $filter_form = drupal_get_form('fgcf_filter_form');
  203. $ret = $filter_form;
  204. $ret .= views_embed_view('fgcf_organismes_de_formation');
  205. return $ret;
  206. }
  207. /**
  208. * Page callback : 'Formations par thematique'.
  209. */
  210. function fgcf_page_thematique($thematique = 0) {
  211. $bc = drupal_get_breadcrumb();
  212. $bc[] = l(t('Catalogue formations'), 'fgcf');
  213. $filter_form = drupal_get_form('fgcf_filter_form');
  214. $ret = $filter_form;
  215. // The views itself doesn't mention vocab ids, which might be different in dev
  216. // and in production.
  217. // We limit to terms in the 'Thématique' vocab outside the view.
  218. $vid = _fgcf_get_vocabulary_by_name('FGCF Thématique');
  219. if (empty($thematique)) {
  220. // Afficher liste des thématiques de 1er niveau.
  221. $ret .= views_embed_view('fgcf_thematiques_premier_niveau', 'default', $vid);
  222. }
  223. else {
  224. $term = taxonomy_get_term($thematique);
  225. $parents = array();
  226. if (!is_object($term) || !isset($term->vid) || $term->vid != $vid) {
  227. $thematique = 0;
  228. $description = '';
  229. }
  230. else {
  231. $bc[] = l(t('Thématiques'), 'fgcf/thematique');
  232. $parents = taxonomy_get_parents($thematique);
  233. $description = $term->description;
  234. }
  235. if (empty($parents)) {
  236. // Top category: display list of sub-categories.
  237. drupal_set_title(t('Thématique : @term', array('@term' => $description)));
  238. $ret .= views_embed_view('fgcf_thematique_deuxieme_niveau', 'default', $thematique);
  239. }
  240. else {
  241. // Sub-category: display list of nodes.
  242. $parent = current($parents);
  243. $bc[] = l($parent->description, 'fgcf/thematique/'. $parent->tid);
  244. drupal_set_title(t('Thématique : @term', array('@term' => $description)));
  245. $values = _fgcf_filter_form_translate_values();
  246. $ret .= views_embed_view('fgcf_formations_par_thematique', 'default', $thematique, $values['dif'], $values['modalites']);
  247. }
  248. }
  249. drupal_set_breadcrumb($bc);
  250. return $ret;
  251. }
  252. /**
  253. * Page callback : 'ODT test'.
  254. */
  255. function fgcf_page_odt() {
  256. module_load_include('inc', 'fgcf', 'fgcf.odt');
  257. $template = dirname(__FILE__) ."/catalogue_template.odt";
  258. $odf = fgcf_odt_export_formations($template);
  259. $odf->exportAsAttachedFile('catalogue.odt');
  260. }
  261. /**
  262. * Theme function for course themes.
  263. */
  264. function theme_fgcf_thematiques($terms) {
  265. $items = array();
  266. foreach ($terms as $tid => $term) {
  267. $items[] = l($term->description, taxonomy_term_path($term));
  268. }
  269. // Build cat logo
  270. $cat_term = reset($terms);
  271. $nodes = nat_get_nids(array($cat_term->tid), TRUE);
  272. $cat_node = reset($nodes);
  273. $node_view = node_build_content($cat_node, FALSE, FALSE);
  274. $logo = drupal_render($node_view->content['field_fgcf_thematique_logo']);
  275. array_unshift($items, $logo);
  276. $ret = '<div class="node-thematique">'. theme('item_list', $items) .'</div>';
  277. return $ret;
  278. }
  279. /**
  280. * Shared filter form for course lists.
  281. *
  282. * Filter values are taken from the form logic or, when absent, from the
  283. * session, in order for them to be persistent across non-form pages.
  284. *
  285. * @return array
  286. */
  287. function fgcf_filter_form($form_state) {
  288. $form['filtre'] = array(
  289. '#title' => t('Filtrer les formations'),
  290. '#attributes' => array('class' => 'fgcf-filtre'),
  291. '#type' => 'fieldset',
  292. '#collapsible' => TRUE,
  293. );
  294. $dif = isset($form_state['storage']['dif'])
  295. ? $form_state['storage']['dif']
  296. : isset($_SESSION['fgcf']['dif'])
  297. ? $_SESSION['fgcf']['dif']
  298. : FALSE;
  299. $form['filtre']['dif'] = array(
  300. '#type' => 'checkbox',
  301. '#default_value' => $dif,
  302. '#title' => t('Eligibles DIF'),
  303. );
  304. $modalite = isset($form_state['storage']['modalite'])
  305. ? $form_state['storage']['modalite']
  306. : isset($_SESSION['fgcf']['modalite'])
  307. ? $_SESSION['fgcf']['modalite']
  308. : array();
  309. $form['filtre']['modalite'] = array(
  310. '#title' => t('Modalité'),
  311. '#type' => 'checkboxes',
  312. '#options' => array(
  313. '1' => t('Inter-entreprises'),
  314. '2' => t('Intra-entreprise'),
  315. '3' => t('Autres'),
  316. ),
  317. '#default_value' => $modalite,
  318. );
  319. $form['filtre']['submit'] = array(
  320. '#type' => 'submit',
  321. '#value' => t('Appliquer le filtre'),
  322. );
  323. return $form;
  324. }
  325. /**
  326. * Submit handler for fgcf_filter_form().
  327. */
  328. function fgcf_filter_form_submit($form, &$form_state) {
  329. foreach (array('modalite', 'dif') as $key) {
  330. $value = $form_state['values'][$key];
  331. //$form_state['storage'][$key] = $value;
  332. $_SESSION['fgcf'][$key] = $value;
  333. }
  334. }
  335. /**
  336. * Translate values in the filter form into arguments for the views.
  337. */
  338. function _fgcf_filter_form_translate_values() {
  339. // Default values.
  340. $values = array(
  341. 'dif' => 'all',
  342. 'modalites' => 'all',
  343. );
  344. if (isset($_SESSION['fgcf']['dif']) && $_SESSION['fgcf']['dif']) {
  345. $values['dif'] = 'o';
  346. }
  347. if (isset($_SESSION['fgcf']['modalite'])) {
  348. $modalites = array_filter($_SESSION['fgcf']['modalite']);
  349. if (!empty($modalites)) {
  350. $values['modalites'] = implode('+', array_keys($modalites));
  351. }
  352. }
  353. return $values;
  354. }
  355. /**
  356. * Helper: get a vocabulary ID from its name.
  357. *
  358. * @todo : inefficient - replace with constants once the vocabs are deployed ?
  359. *
  360. * @param string $name
  361. */
  362. function _fgcf_get_vocabulary_by_name($name) {
  363. static $matches = array();
  364. if (!isset($matches[$name])) {
  365. $matches[$name] = 0;
  366. $vocabularies = taxonomy_get_vocabularies();
  367. foreach ($vocabularies as $vid => $vocabulary) {
  368. if (drupal_strtolower($vocabulary->name) == drupal_strtolower($name)) {
  369. $matches[$name] = $vid;
  370. break;
  371. }
  372. }
  373. }
  374. return $matches[$name];
  375. }