fgcf.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. return $items;
  39. }
  40. /**
  41. * Implementation of hook_init().
  42. */
  43. function fgcf_init() {
  44. if (arg(0) == 'fgcf') {
  45. drupal_add_css(drupal_get_path('module', 'fgcf') .'/theme/css/fgcf.css');
  46. }
  47. }
  48. /**
  49. * Implementation of hook_perm().
  50. */
  51. function fgcf_perm() {
  52. return array('access course catalog');
  53. }
  54. /**
  55. * Implementation of hook_theme().
  56. */
  57. function fgcf_theme($existing, $type, $theme, $path) {
  58. $ret = array(
  59. 'fgcf_home' => array(
  60. 'arguments' => array('filter_form' => array()),
  61. 'template' => 'fgcf-home',
  62. 'path' => $path .'/theme',
  63. ),
  64. 'fgcf_thematiques' => array(
  65. 'arguments' => array('terms' => array()),
  66. ),
  67. );
  68. return $ret;
  69. }
  70. /**
  71. * Implementation of hook_term_path().
  72. */
  73. function fgcf_term_path($term) {
  74. $vocabulary = taxonomy_vocabulary_load($term->vid);
  75. switch ($vocabulary->vid) {
  76. case _fgcf_get_vocabulary_by_name('FGCF Thématique'):
  77. return 'fgcf/thematique/'. $term->tid;
  78. break;
  79. case _fgcf_get_vocabulary_by_name('FGCF Format de cours'):
  80. return 'fgcf/format/'. $term->tid;
  81. break;
  82. // @todo Quid de :
  83. // - FGCF Supports remis (affiché en lien)
  84. // - FGCF Sanction (affiché en lien)
  85. // - FGCF Public visé (affiché en lien)
  86. }
  87. }
  88. /**
  89. * Implementation of hook_views_api().
  90. */
  91. function fgcf_views_api() {
  92. $ret = array(
  93. 'api' => '2',
  94. 'path' => drupal_get_path('module', 'fgcf') . '/views',
  95. );
  96. return $ret;
  97. }
  98. /**
  99. * Implementation of hook_nodeapi().
  100. */
  101. function fgcf_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  102. switch ($op) {
  103. case 'view':
  104. // Only operate on 'page' view.
  105. if ($a3 /* !teaser */ || !$a4 /* page */) {
  106. break;
  107. }
  108. drupal_add_css(drupal_get_path('module', 'fgcf') .'/theme/css/fgcf.css');
  109. switch ($node->type) {
  110. case 'fgcf_prestataire':
  111. $bc = drupal_get_breadcrumb();
  112. $bc[] = l(t('Catalogue formations'), 'fgcf');
  113. $bc[] = l(t('Organismes'), 'fgcf/organisme');
  114. drupal_set_breadcrumb($bc);
  115. break;
  116. case 'fgcf_fiche':
  117. $nat_config = variable_get('nat_config', NULL);
  118. $vid = isset($nat_config['types']['fgcf_thematique'])
  119. ? reset($nat_config['types']['fgcf_thematique'])
  120. : 0;
  121. unset($nat_config);
  122. $taxo2 = array();
  123. foreach ($node->taxonomy as $tid => $term) {
  124. if ($term->vid != $vid) {
  125. $taxo2[$tid] = $term;
  126. }
  127. else {
  128. $node->fgcf_scat = $term; // Single by configuration
  129. }
  130. }
  131. $node->taxonomy = $taxo2;
  132. unset($taxo2, $tid, $term);
  133. // Build cat/scat[/scat]* path
  134. $cats = array();
  135. $parents = taxonomy_get_parents($node->fgcf_scat->tid);
  136. foreach ($parents as $tid => $term) {
  137. $cats[] = $term;
  138. }
  139. $cats[] = $node->fgcf_scat;
  140. $node->content['fgcf_catpath'] = array(
  141. '#value' => theme('fgcf_thematiques', $cats),
  142. );
  143. $bc = drupal_get_breadcrumb();
  144. $bc[] = l(t('Catalogue formations'), 'fgcf');
  145. $bc[] = l(t('Thématiques'), 'fgcf/thematique');
  146. $bc[] = l($cats[0]->description, 'fgcf/thematique/'. $cats[0]->tid);
  147. $bc[] = l($cats[1]->description, 'fgcf/thematique/'. $cats[1]->tid);
  148. drupal_set_breadcrumb($bc);
  149. unset($cats, $parents, $tid, $term);
  150. break;
  151. }
  152. }
  153. }
  154. /**
  155. * Page callback : 'Catalogue Formations'.
  156. */
  157. function fgcf_page_home() {
  158. drupal_add_css(drupal_get_path('module', 'fgcf') .'/theme/css/fgcf.css');
  159. drupal_set_title('Catalogue des formations France Galop');
  160. $filter_form = drupal_get_form('fgcf_filter_form');
  161. $ret = theme('fgcf_home', $filter_form);
  162. return $ret;
  163. }
  164. /**
  165. * Page callback : 'Formations par format'.
  166. */
  167. function fgcf_page_format($format = 0) {
  168. $bc = drupal_get_breadcrumb();
  169. $bc[] = l(t('Catalogue formations'), 'fgcf');
  170. $filter_form = drupal_get_form('fgcf_filter_form');
  171. $ret = $filter_form;
  172. $vid = _fgcf_get_vocabulary_by_name('FGCF Format de cours');
  173. if (empty($format)) {
  174. $ret .= views_embed_view('fgcf_formats_de_formations', 'default', $vid);
  175. }
  176. else {
  177. $term = taxonomy_get_term($format);
  178. if (!is_object($term) || !isset($term->vid) || $term->vid != $vid) {
  179. $format = 0;
  180. }
  181. else {
  182. $bc[] = l(t('Formats de formation'), 'fgcf/format');
  183. drupal_set_title(t('Format : @term', array('@term' => $term->name)));
  184. $values = _fgcf_filter_form_translate_values();
  185. }
  186. $ret .= views_embed_view('fgcf_formations_par_format', 'default', $format, $values['dif'], $values['modalites']);
  187. }
  188. drupal_set_breadcrumb($bc);
  189. return $ret;
  190. }
  191. /**
  192. * Page callback : 'Formations par organisme'.
  193. */
  194. function fgcf_page_organisme() {
  195. $bc = drupal_get_breadcrumb();
  196. $bc[] = l(t('Catalogue formations'), 'fgcf');
  197. drupal_set_breadcrumb($bc);
  198. $filter_form = drupal_get_form('fgcf_filter_form');
  199. $ret = $filter_form;
  200. $ret .= views_embed_view('fgcf_organismes_de_formation');
  201. return $ret;
  202. }
  203. /**
  204. * Page callback : 'Formations par thematique'.
  205. */
  206. function fgcf_page_thematique($thematique = 0) {
  207. $bc = drupal_get_breadcrumb();
  208. $bc[] = l(t('Catalogue formations'), 'fgcf');
  209. $filter_form = drupal_get_form('fgcf_filter_form');
  210. $ret = $filter_form;
  211. // The views itself doesn't mention vocab ids, which might be different in dev
  212. // and in production.
  213. // We limit to terms in the 'Thématique' vocab outside the view.
  214. $vid = _fgcf_get_vocabulary_by_name('FGCF Thématique');
  215. if (empty($thematique)) {
  216. // Afficher liste des thématiques de 1er niveau.
  217. $ret .= views_embed_view('fgcf_thematiques_premier_niveau', 'default', $vid);
  218. }
  219. else {
  220. $term = taxonomy_get_term($thematique);
  221. $parents = array();
  222. if (!is_object($term) || !isset($term->vid) || $term->vid != $vid) {
  223. $thematique = 0;
  224. $description = '';
  225. }
  226. else {
  227. $bc[] = l(t('Thématiques'), 'fgcf/thematique');
  228. $parents = taxonomy_get_parents($thematique);
  229. $description = $term->description;
  230. }
  231. if (empty($parents)) {
  232. // Top category: display list of sub-categories.
  233. drupal_set_title(t('Thématique : @term', array('@term' => $description)));
  234. $ret .= views_embed_view('fgcf_thematique_deuxieme_niveau', 'default', $thematique);
  235. }
  236. else {
  237. // Sub-category: display list of nodes.
  238. $parent = current($parents);
  239. $bc[] = l($parent->description, 'fgcf/thematique/'. $parent->tid);
  240. drupal_set_title(t('Thématique : @term', array('@term' => $description)));
  241. $values = _fgcf_filter_form_translate_values();
  242. $ret .= views_embed_view('fgcf_formations_par_thematique', 'default', $thematique, $values['dif'], $values['modalites']);
  243. }
  244. }
  245. drupal_set_breadcrumb($bc);
  246. return $ret;
  247. }
  248. /**
  249. * Theme function for course themes.
  250. */
  251. function theme_fgcf_thematiques($terms) {
  252. $items = array();
  253. foreach ($terms as $tid => $term) {
  254. $items[] = l($term->description, taxonomy_term_path($term));
  255. }
  256. // Build cat logo
  257. $cat_term = reset($terms);
  258. $nodes = nat_get_nids(array($cat_term->tid), TRUE);
  259. $cat_node = reset($nodes);
  260. $node_view = node_build_content($cat_node, FALSE, FALSE);
  261. $logo = drupal_render($node_view->content['field_fgcf_thematique_logo']);
  262. array_unshift($items, $logo);
  263. $ret = '<div class="node-thematique">'. theme('item_list', $items) .'</div>';
  264. return $ret;
  265. }
  266. /**
  267. * Shared filter form for course lists.
  268. *
  269. * Filter values are taken from the form logic or, when absent, from the
  270. * session, in order for them to be persistent across non-form pages.
  271. *
  272. * @return array
  273. */
  274. function fgcf_filter_form($form_state) {
  275. $form['filtre'] = array(
  276. '#title' => t('Filtrer les formations'),
  277. '#attributes' => array('class' => 'fgcf-filtre'),
  278. '#type' => 'fieldset',
  279. '#collapsible' => TRUE,
  280. );
  281. $dif = isset($form_state['storage']['dif'])
  282. ? $form_state['storage']['dif']
  283. : isset($_SESSION['fgcf']['dif'])
  284. ? $_SESSION['fgcf']['dif']
  285. : FALSE;
  286. $form['filtre']['dif'] = array(
  287. '#type' => 'checkbox',
  288. '#default_value' => $dif,
  289. '#title' => t('Eligibles DIF'),
  290. );
  291. $modalite = isset($form_state['storage']['modalite'])
  292. ? $form_state['storage']['modalite']
  293. : isset($_SESSION['fgcf']['modalite'])
  294. ? $_SESSION['fgcf']['modalite']
  295. : array();
  296. $form['filtre']['modalite'] = array(
  297. '#title' => t('Modalité'),
  298. '#type' => 'checkboxes',
  299. '#options' => array(
  300. '1' => t('Inter-entreprises'),
  301. '2' => t('Intra-entreprise'),
  302. '3' => t('Autres'),
  303. ),
  304. '#default_value' => $modalite,
  305. );
  306. $form['filtre']['submit'] = array(
  307. '#type' => 'submit',
  308. '#value' => t('Appliquer le filtre'),
  309. );
  310. return $form;
  311. }
  312. /**
  313. * Submit handler for fgcf_filter_form().
  314. */
  315. function fgcf_filter_form_submit($form, &$form_state) {
  316. foreach (array('modalite', 'dif') as $key) {
  317. $value = $form_state['values'][$key];
  318. //$form_state['storage'][$key] = $value;
  319. $_SESSION['fgcf'][$key] = $value;
  320. }
  321. }
  322. /**
  323. * Translate values in the filter form into arguments for the views.
  324. */
  325. function _fgcf_filter_form_translate_values() {
  326. // Default values.
  327. $values = array(
  328. 'dif' => 'all',
  329. 'modalites' => 'all',
  330. );
  331. if (isset($_SESSION['fgcf']['dif']) && $_SESSION['fgcf']['dif']) {
  332. $values['dif'] = 'o';
  333. }
  334. if (isset($_SESSION['fgcf']['modalite'])) {
  335. $modalites = array_filter($_SESSION['fgcf']['modalite']);
  336. if (!empty($modalites)) {
  337. $values['modalites'] = implode('+', array_keys($modalites));
  338. }
  339. }
  340. return $values;
  341. }
  342. /**
  343. * Helper: get a vocabulary ID from its name.
  344. *
  345. * @todo : inefficient - replace with constants once the vocabs are deployed ?
  346. *
  347. * @param string $name
  348. */
  349. function _fgcf_get_vocabulary_by_name($name) {
  350. static $matches = array();
  351. if (!isset($matches[$name])) {
  352. $matches[$name] = 0;
  353. $vocabularies = taxonomy_get_vocabularies();
  354. foreach ($vocabularies as $vid => $vocabulary) {
  355. if (drupal_strtolower($vocabulary->name) == drupal_strtolower($name)) {
  356. $matches[$name] = $vid;
  357. break;
  358. }
  359. }
  360. }
  361. return $matches[$name];
  362. }