fgcf.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. // @todo Quid de :
  88. // - FGCF Supports remis (affiché en lien)
  89. // - FGCF Sanction (affiché en lien)
  90. // - FGCF Public visé (affiché en lien)
  91. }
  92. }
  93. /**
  94. * Implementation of hook_views_api().
  95. */
  96. function fgcf_views_api() {
  97. $ret = array(
  98. 'api' => '2',
  99. 'path' => drupal_get_path('module', 'fgcf') . '/views',
  100. );
  101. return $ret;
  102. }
  103. /**
  104. * Implementation of hook_nodeapi().
  105. */
  106. function fgcf_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  107. switch ($op) {
  108. case 'view':
  109. // Only operate on 'page' view.
  110. if ($a3 /* !teaser */ || !$a4 /* page */) {
  111. break;
  112. }
  113. drupal_add_css(drupal_get_path('module', 'fgcf') .'/theme/css/fgcf.css');
  114. switch ($node->type) {
  115. case 'fgcf_prestataire':
  116. $bc = drupal_get_breadcrumb();
  117. $bc[] = l(t('Catalogue formations'), 'fgcf');
  118. $bc[] = l(t('Organismes'), 'fgcf/organisme');
  119. drupal_set_breadcrumb($bc);
  120. break;
  121. case 'fgcf_fiche':
  122. $nat_config = variable_get('nat_config', NULL);
  123. $vid = isset($nat_config['types']['fgcf_thematique'])
  124. ? reset($nat_config['types']['fgcf_thematique'])
  125. : 0;
  126. unset($nat_config);
  127. $taxo2 = array();
  128. foreach ($node->taxonomy as $tid => $term) {
  129. if ($term->vid != $vid) {
  130. $taxo2[$tid] = $term;
  131. }
  132. else {
  133. $node->fgcf_scat = $term; // Single by configuration
  134. }
  135. }
  136. $node->taxonomy = $taxo2;
  137. unset($taxo2, $tid, $term);
  138. // Build cat/scat[/scat]* path
  139. $cats = array();
  140. $parents = taxonomy_get_parents($node->fgcf_scat->tid);
  141. foreach ($parents as $tid => $term) {
  142. $cats[] = $term;
  143. }
  144. $cats[] = $node->fgcf_scat;
  145. $node->content['fgcf_catpath'] = array(
  146. '#value' => theme('fgcf_thematiques', $cats),
  147. );
  148. $bc = drupal_get_breadcrumb();
  149. $bc[] = l(t('Catalogue formations'), 'fgcf');
  150. $bc[] = l(t('Thématiques'), 'fgcf/thematique');
  151. $bc[] = l($cats[0]->description, 'fgcf/thematique/'. $cats[0]->tid);
  152. $bc[] = l($cats[1]->description, 'fgcf/thematique/'. $cats[1]->tid);
  153. drupal_set_breadcrumb($bc);
  154. unset($cats, $parents, $tid, $term);
  155. break;
  156. }
  157. }
  158. }
  159. /**
  160. * Page callback : 'Catalogue Formations'.
  161. */
  162. function fgcf_page_home() {
  163. drupal_add_css(drupal_get_path('module', 'fgcf') .'/theme/css/fgcf.css');
  164. drupal_set_title('Catalogue des formations France Galop');
  165. $filter_form = drupal_get_form('fgcf_filter_form');
  166. $ret = theme('fgcf_home', $filter_form);
  167. return $ret;
  168. }
  169. /**
  170. * Page callback : 'Formations par format'.
  171. */
  172. function fgcf_page_format($format = 0) {
  173. $bc = drupal_get_breadcrumb();
  174. $bc[] = l(t('Catalogue formations'), 'fgcf');
  175. $filter_form = drupal_get_form('fgcf_filter_form');
  176. $ret = $filter_form;
  177. $vid = _fgcf_get_vocabulary_by_name('FGCF Format de cours');
  178. if (empty($format)) {
  179. $ret .= views_embed_view('fgcf_formats_de_formations', 'default', $vid);
  180. }
  181. else {
  182. $term = taxonomy_get_term($format);
  183. if (!is_object($term) || !isset($term->vid) || $term->vid != $vid) {
  184. $format = 0;
  185. }
  186. else {
  187. $bc[] = l(t('Formats de formation'), 'fgcf/format');
  188. drupal_set_title(t('Format : @term', array('@term' => $term->name)));
  189. $values = _fgcf_filter_form_translate_values();
  190. }
  191. $ret .= views_embed_view('fgcf_formations_par_format', 'default', $format, $values['dif'], $values['modalites']);
  192. }
  193. drupal_set_breadcrumb($bc);
  194. return $ret;
  195. }
  196. /**
  197. * Page callback : 'Formations par organisme'.
  198. */
  199. function fgcf_page_organisme() {
  200. $bc = drupal_get_breadcrumb();
  201. $bc[] = l(t('Catalogue formations'), 'fgcf');
  202. drupal_set_breadcrumb($bc);
  203. $filter_form = drupal_get_form('fgcf_filter_form');
  204. $ret = $filter_form;
  205. $ret .= views_embed_view('fgcf_organismes_de_formation');
  206. return $ret;
  207. }
  208. /**
  209. * Page callback : 'Formations par thematique'.
  210. */
  211. function fgcf_page_thematique($thematique = 0) {
  212. $bc = drupal_get_breadcrumb();
  213. $bc[] = l(t('Catalogue formations'), 'fgcf');
  214. $filter_form = drupal_get_form('fgcf_filter_form');
  215. $ret = $filter_form;
  216. // The views itself doesn't mention vocab ids, which might be different in dev
  217. // and in production.
  218. // We limit to terms in the 'Thématique' vocab outside the view.
  219. $vid = _fgcf_get_vocabulary_by_name('FGCF Thématique');
  220. if (empty($thematique)) {
  221. // Afficher liste des thématiques de 1er niveau.
  222. $ret .= views_embed_view('fgcf_thematiques_premier_niveau', 'default', $vid);
  223. }
  224. else {
  225. $term = taxonomy_get_term($thematique);
  226. $parents = array();
  227. if (!is_object($term) || !isset($term->vid) || $term->vid != $vid) {
  228. $thematique = 0;
  229. $description = '';
  230. }
  231. else {
  232. $bc[] = l(t('Thématiques'), 'fgcf/thematique');
  233. $parents = taxonomy_get_parents($thematique);
  234. $description = $term->description;
  235. }
  236. if (empty($parents)) {
  237. // Top category: display list of sub-categories.
  238. drupal_set_title(t('Thématique : @term', array('@term' => $description)));
  239. $ret .= views_embed_view('fgcf_thematique_deuxieme_niveau', 'default', $thematique);
  240. }
  241. else {
  242. // Sub-category: display list of nodes.
  243. $parent = current($parents);
  244. $bc[] = l($parent->description, 'fgcf/thematique/'. $parent->tid);
  245. drupal_set_title(t('Thématique : @term', array('@term' => $description)));
  246. $values = _fgcf_filter_form_translate_values();
  247. $ret .= views_embed_view('fgcf_formations_par_thematique', 'default', $thematique, $values['dif'], $values['modalites']);
  248. }
  249. }
  250. drupal_set_breadcrumb($bc);
  251. return $ret;
  252. }
  253. /**
  254. * Page callback : 'ODT test'.
  255. */
  256. function fgcf_page_odt() {
  257. $odf = fgcf_new_odt(dirname(__FILE__) ."/odtphp/tests/tutoriel1.odt");
  258. $odf->setVars('titre', 'PHP');
  259. $message = "PHP est un langage de scripts libre ... hello";
  260. $odf->setVars('message', $message);
  261. //$odf->saveToDisk('foo.odt');
  262. $odf->exportAsAttachedFile();
  263. }
  264. function fgcf_new_odt($filename) {
  265. fgcf_load_odt();
  266. $options = array(
  267. 'PATH_TO_TMP' => file_directory_temp() .'/',
  268. // The PHP Zip extension in recent PHP versions produces corrupt ODT
  269. // archives. See http://www.odtphp.com/forum/viewtopic.php?f=4&t=43#p154.
  270. 'ZIP_PROXY' => 'PclZipProxy',
  271. );
  272. return new odf($filename, $options);
  273. }
  274. function fgcf_load_odt() {
  275. // Use correct temp folders - see http://www.odtphp.com/forum/viewtopic.php?f=5&t=20#p126
  276. // Temp folder used by PclZipProxy - see the beginning of PclZipProxy.php.
  277. // This folder is deleted and recreated each time, so we use a subfolder of
  278. // the tmp dir.
  279. define('PCL_ZIP_TMP', file_directory_temp() .'/phpodt/');
  280. // Temp folder used by the pclzip library - see the beginning of
  281. // pclzip.lib.php.
  282. define('PCLZIP_TEMPORARY_DIR', file_directory_temp() .'/');
  283. // Load the OdtPHP library.
  284. require_once(dirname(__FILE__) .'/odtphp/library/odf.php');
  285. }
  286. /**
  287. * Theme function for course themes.
  288. */
  289. function theme_fgcf_thematiques($terms) {
  290. $items = array();
  291. foreach ($terms as $tid => $term) {
  292. $items[] = l($term->description, taxonomy_term_path($term));
  293. }
  294. // Build cat logo
  295. $cat_term = reset($terms);
  296. $nodes = nat_get_nids(array($cat_term->tid), TRUE);
  297. $cat_node = reset($nodes);
  298. $node_view = node_build_content($cat_node, FALSE, FALSE);
  299. $logo = drupal_render($node_view->content['field_fgcf_thematique_logo']);
  300. array_unshift($items, $logo);
  301. $ret = '<div class="node-thematique">'. theme('item_list', $items) .'</div>';
  302. return $ret;
  303. }
  304. /**
  305. * Shared filter form for course lists.
  306. *
  307. * Filter values are taken from the form logic or, when absent, from the
  308. * session, in order for them to be persistent across non-form pages.
  309. *
  310. * @return array
  311. */
  312. function fgcf_filter_form($form_state) {
  313. $form['filtre'] = array(
  314. '#title' => t('Filtrer les formations'),
  315. '#attributes' => array('class' => 'fgcf-filtre'),
  316. '#type' => 'fieldset',
  317. '#collapsible' => TRUE,
  318. );
  319. $dif = isset($form_state['storage']['dif'])
  320. ? $form_state['storage']['dif']
  321. : isset($_SESSION['fgcf']['dif'])
  322. ? $_SESSION['fgcf']['dif']
  323. : FALSE;
  324. $form['filtre']['dif'] = array(
  325. '#type' => 'checkbox',
  326. '#default_value' => $dif,
  327. '#title' => t('Eligibles DIF'),
  328. );
  329. $modalite = isset($form_state['storage']['modalite'])
  330. ? $form_state['storage']['modalite']
  331. : isset($_SESSION['fgcf']['modalite'])
  332. ? $_SESSION['fgcf']['modalite']
  333. : array();
  334. $form['filtre']['modalite'] = array(
  335. '#title' => t('Modalité'),
  336. '#type' => 'checkboxes',
  337. '#options' => array(
  338. '1' => t('Inter-entreprises'),
  339. '2' => t('Intra-entreprise'),
  340. '3' => t('Autres'),
  341. ),
  342. '#default_value' => $modalite,
  343. );
  344. $form['filtre']['submit'] = array(
  345. '#type' => 'submit',
  346. '#value' => t('Appliquer le filtre'),
  347. );
  348. return $form;
  349. }
  350. /**
  351. * Submit handler for fgcf_filter_form().
  352. */
  353. function fgcf_filter_form_submit($form, &$form_state) {
  354. foreach (array('modalite', 'dif') as $key) {
  355. $value = $form_state['values'][$key];
  356. //$form_state['storage'][$key] = $value;
  357. $_SESSION['fgcf'][$key] = $value;
  358. }
  359. }
  360. /**
  361. * Translate values in the filter form into arguments for the views.
  362. */
  363. function _fgcf_filter_form_translate_values() {
  364. // Default values.
  365. $values = array(
  366. 'dif' => 'all',
  367. 'modalites' => 'all',
  368. );
  369. if (isset($_SESSION['fgcf']['dif']) && $_SESSION['fgcf']['dif']) {
  370. $values['dif'] = 'o';
  371. }
  372. if (isset($_SESSION['fgcf']['modalite'])) {
  373. $modalites = array_filter($_SESSION['fgcf']['modalite']);
  374. if (!empty($modalites)) {
  375. $values['modalites'] = implode('+', array_keys($modalites));
  376. }
  377. }
  378. return $values;
  379. }
  380. /**
  381. * Helper: get a vocabulary ID from its name.
  382. *
  383. * @todo : inefficient - replace with constants once the vocabs are deployed ?
  384. *
  385. * @param string $name
  386. */
  387. function _fgcf_get_vocabulary_by_name($name) {
  388. static $matches = array();
  389. if (!isset($matches[$name])) {
  390. $matches[$name] = 0;
  391. $vocabularies = taxonomy_get_vocabularies();
  392. foreach ($vocabularies as $vid => $vocabulary) {
  393. if (drupal_strtolower($vocabulary->name) == drupal_strtolower($name)) {
  394. $matches[$name] = $vid;
  395. break;
  396. }
  397. }
  398. }
  399. return $matches[$name];
  400. }