From a95b1422ca3b942d118cc83ccc5c9cbfdd75b181 Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Tue, 15 Dec 2015 12:03:05 +0100
Subject: [PATCH] dev #30641 add cas orthodidacte

---
 VERSIONS_WIP/30641                            |   1 +
 .../admin/controllers/IndexController.php     |   1 -
 .../opac/controllers/ModulesController.php    |   7 ++
 library/Class/AdminVar.php                    |   7 ++
 library/Class/Orthodidacte/Link.php           |  40 +++++++
 library/Class/Systeme/ModulesMenu.php         |   1 +
 .../Systeme/ModulesMenu/Orthodidacte.php      |  54 ++++++++++
 library/Class/UserGroup.php                   |   2 +
 library/Class/Users.php                       |   5 +
 public/opac/js/digital_connectors.js          |  10 ++
 .../controllers/ModulesControllerTest.php     | 101 ++++++++++++++++++
 .../fixtures/RessourcesNumeriquesFixtures.php |  12 ++-
 12 files changed, 239 insertions(+), 2 deletions(-)
 create mode 100644 VERSIONS_WIP/30641
 create mode 100644 library/Class/Orthodidacte/Link.php
 create mode 100644 library/Class/Systeme/ModulesMenu/Orthodidacte.php

diff --git a/VERSIONS_WIP/30641 b/VERSIONS_WIP/30641
new file mode 100644
index 00000000000..8adcca6998f
--- /dev/null
+++ b/VERSIONS_WIP/30641
@@ -0,0 +1 @@
+ - ticket #30641 : Ajout du connecteur CAS vers la plateform ORTHODIDACTE. 
\ No newline at end of file
diff --git a/application/modules/admin/controllers/IndexController.php b/application/modules/admin/controllers/IndexController.php
index c12ea844fab..2fdbe8da7df 100644
--- a/application/modules/admin/controllers/IndexController.php
+++ b/application/modules/admin/controllers/IndexController.php
@@ -70,7 +70,6 @@ class Admin_IndexController extends ZendAfi_Controller_Action {
                      $this->view->url(['action' => 'adminvar']));
 
     if ($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
-      xdebug_break();
       $new_valeur = $form->getAdminVarValue();
 
       $var->setValeur($new_valeur);
diff --git a/application/modules/opac/controllers/ModulesController.php b/application/modules/opac/controllers/ModulesController.php
index 6ad788e1d6e..72678e1fe2a 100644
--- a/application/modules/opac/controllers/ModulesController.php
+++ b/application/modules/opac/controllers/ModulesController.php
@@ -118,4 +118,11 @@ class ModulesController extends Zend_Controller_Action {
     $this->getResponse()->setHeader('Content-Type', 'text/html;charset=utf-8');
     $this->getResponse()->setBody('<script>window.location="'.$url.'";</script>');
   }
+
+
+  public function orthodidacteAction() {
+    $orthodidacte = new Class_Systeme_ModulesMenu_Orthodidacte();
+    $orthodidacte->afterLoginRedirectTo(Class_Url::absolute($this->view->url()));
+    $this->checkNotifyMessage($orthodidacte, $orthodidacte->getDynamiqueUrl());
+  }
 }
diff --git a/library/Class/AdminVar.php b/library/Class/AdminVar.php
index 6a23fd614dc..cdc86046280 100644
--- a/library/Class/AdminVar.php
+++ b/library/Class/AdminVar.php
@@ -266,6 +266,8 @@ class Class_AdminVarLoader extends Storm_Model_Loader {
 
                    'PLANETNEMO' => Class_AdminVar_Meta::newOnOff($this->_('Activation de la ressource numérique PlanetNemo'))->bePrivate(),
 
+                   'ORTHODIDACTE' => Class_AdminVar_Meta::newOnOff($this->_('Activation de la ressource numérique Orthodidacte'))->bePrivate(),
+
                    'LECTURA_DOMAIN' => Class_AdminVar_Meta::newDefault($this->_('Domaine utilisée par le serveur lectura pour authentification')),
 
                    'LEKIOSK_ID' => Class_AdminVar_Meta::newDefault($this->_('Identifiant fournit par lekiosk.com'))->bePrivate(),
@@ -482,6 +484,11 @@ class Class_AdminVarLoader extends Storm_Model_Loader {
   }
 
 
+  public function isOrthodidacteEnabled() {
+    return ('' != Class_AdminVar::get('ORTHODIDACTE'));
+  }
+
+
   public function isNumeriquePremiumEnabled() {
     return '' != Class_AdminVar::get('NUMERIQUE_PREMIUM_URL');
   }
diff --git a/library/Class/Orthodidacte/Link.php b/library/Class/Orthodidacte/Link.php
new file mode 100644
index 00000000000..73fdf45c791
--- /dev/null
+++ b/library/Class/Orthodidacte/Link.php
@@ -0,0 +1,40 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class Class_Orthodidacte_Link {
+  const ROOT_URL = 'https://plateforme.orthodidacte.com/authenticate?';
+
+  protected $_id, $_last_name, $_first_name, $_mail;
+
+  public static function newFor($user) {
+    return self::ROOT_URL
+      . http_build_query(
+                         [
+                          'cas_fournisseur' => Class_Url::absolute(['controller' => 'cas-server-v10'], null, true),
+                          'id' => $user->getIdabon(),
+                          'nom' => $user->getNom(),
+                          'prenom' => $user->getPrenom(),
+                          'mail' => ''
+                         ] );
+  }
+}
+?>
\ No newline at end of file
diff --git a/library/Class/Systeme/ModulesMenu.php b/library/Class/Systeme/ModulesMenu.php
index 3096d8616bb..f5f83eeb25d 100644
--- a/library/Class/Systeme/ModulesMenu.php
+++ b/library/Class/Systeme/ModulesMenu.php
@@ -120,6 +120,7 @@ class Class_Systeme_ModulesMenu extends Class_Systeme_ModulesAbstract {
                          "NUMILOG" => new Class_Systeme_ModulesMenu_Numilog(),
                          "ARTEVOD" => new Class_Systeme_ModulesMenu_ArteVod(),
                          "LEKIOSK" => new Class_Systeme_ModulesMenu_LeKiosk(),
+                         "ORTHODIDACTE" => new Class_Systeme_ModulesMenu_Orthodidacte(),
                          "MUSICME" => new Class_Systeme_ModulesMenu_MusicMe(),
                          "MYCOW" => new Class_Systeme_ModulesMenu_MyCow(),
                          "PLANETNEMO" => new Class_Systeme_ModulesMenu_PlanetNemo(),
diff --git a/library/Class/Systeme/ModulesMenu/Orthodidacte.php b/library/Class/Systeme/ModulesMenu/Orthodidacte.php
new file mode 100644
index 00000000000..d385efcb3b8
--- /dev/null
+++ b/library/Class/Systeme/ModulesMenu/Orthodidacte.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class Class_Systeme_ModulesMenu_Orthodidacte extends Class_Systeme_ModulesMenu_SSOAbstract {
+  /** @var string */
+  protected $_group = Class_Systeme_ModulesMenu::GROUP_MENU_ABONNES;
+
+  /** @var string */
+  protected $_type_module = 'ORTHODIDACTE';
+
+  /** @var string */
+  protected $_libelle = 'Lien vers Orthodidacte';
+
+  protected $_url = [
+                     'controller' => 'modules',
+                     'action' => 'orthodidacte'
+  ];
+
+
+  public function urlForUser($user) {
+    if (!$user->hasRightAccesOrthodidacte()) {
+      $this->setMessage($this->_('Votre abonnement ne permet pas d\'accéder aux ressources Orthodidacte. Merci de contacter la médiathèque'));
+      return '';
+    }
+
+    return Class_Orthodidacte_Link::newFor($user);
+  }
+
+
+  public function isVisibleForProfil($profil) {
+    return Class_AdminVar::isOrthodidacteEnabled();
+  }
+
+}
+?>
\ No newline at end of file
diff --git a/library/Class/UserGroup.php b/library/Class/UserGroup.php
index 88d9dad6abe..b2ce69a7e47 100644
--- a/library/Class/UserGroup.php
+++ b/library/Class/UserGroup.php
@@ -57,6 +57,7 @@ class Class_UserGroup extends Storm_Model_Abstract {
   const RIGHT_ACCES_KIDILANGUES = 25;
   const RIGHT_ACCES_PNB_DILICOM = 26;
   const RIGHT_ACCES_PLANETNEMO = 27;
+  const RIGHT_ACCESS_ORTHODIDACTE = 28;
 
   // droits moderateurs
   const RIGHT_USER_DOMAINES_SUPPRESSION_LIMIT = 8;
@@ -96,6 +97,7 @@ class Class_UserGroup extends Storm_Model_Abstract {
     self::RIGHT_ACCES_MYCOW => 'Bibliothèque numérique: accéder à MyCOW.EU',
     self::RIGHT_ACCES_PLANETNEMO => 'Bibliothèque numérique: accéder à planetnemo.fr',
     self::RIGHT_ACCES_KIDILANGUES => 'Bibliothèque numérique: accéder à Kidilangues',
+    self::RIGHT_ACCESS_ORTHODIDACTE => 'Bibliothèque numérique: accéder à Orthodidacte.com',
     self::RIGHT_ACCES_PNB_DILICOM => 'Bibliothèque numérique: autoriser le prêt numérique Dilicom',
     self::RIGHT_USER_DOMAINES_SUPPRESSION_LIMIT => 'Domaines: accès, modification et suppression limitée au créateur',
     self::RIGHT_USER_DOMAINES_TOTAL_ACCESS => 'Domaines: accès total en modification et suppression',
diff --git a/library/Class/Users.php b/library/Class/Users.php
index c029d689393..00ec6abc62e 100644
--- a/library/Class/Users.php
+++ b/library/Class/Users.php
@@ -813,6 +813,11 @@ class Class_Users extends Storm_Model_Abstract {
   }
 
 
+  public function hasRightAccesOrthodidacte() {
+    return $this->isAbonneAndHasRightToAccess(Class_UserGroup::RIGHT_ACCESS_ORTHODIDACTE);
+  }
+
+
   public function getFirstAvisByIdNotice($id_notice) {
     $notice = Class_Notice::getLoader()->find($id_notice);
     $avis = $notice->getAvisByUser($this);
diff --git a/public/opac/js/digital_connectors.js b/public/opac/js/digital_connectors.js
index 47204d961b5..a6e71ccbc5d 100644
--- a/public/opac/js/digital_connectors.js
+++ b/public/opac/js/digital_connectors.js
@@ -196,5 +196,15 @@
     "sales_contact": {"mail" : "",
 		      "name" : "",
 		      "url" : "http://www.vodeclic.com/fr/solutions/bibliotheques"}
+  },
+  "ORTHODIDACTE": {
+    "label" : "Orthodidacte", 
+    "url" : "http://www.orthodidacte.com/", 
+    "image_url" : "http://bokeh-library-portal.org/userfiles/media/ressources_numeriques/logo_orthodidacte_pastille.png", 
+    "desc": "Orthodidacte constitue une nouvelle approche pour apprendre l’orthographe et la grammaire de la langue française en ligne.", 
+    "features": ["CAS"], 
+    "sales_contact": {"mail" : "",
+		      "name" : "",
+		      "url" : "http://www.orthodidacte.com"}
   }
 }
diff --git a/tests/application/modules/opac/controllers/ModulesControllerTest.php b/tests/application/modules/opac/controllers/ModulesControllerTest.php
index 4cbf85d44d0..4027b3eb605 100644
--- a/tests/application/modules/opac/controllers/ModulesControllerTest.php
+++ b/tests/application/modules/opac/controllers/ModulesControllerTest.php
@@ -202,3 +202,104 @@ class ModulesControllerNumeriquepremiumTest extends AbstractControllerTestCase {
     $this->assertXPathContentContains('//script', 'http://foo/', $this->_response->getBody());
   }
 }
+
+
+
+class ModulesControllerLoginRequiredOrthodidacteTest extends AbstractControllerTestCase {
+  protected $_storm_default_to_volatile = true;
+
+  /** @test */
+  public function shouldRedirectToLogin() {
+    RessourcesNumeriquesFixtures::activateOrthodidacte();
+
+    $_SERVER['HTTP_REFERER'] = '/opac/cms/articleview/id/23';
+    ZendAfi_Auth::getInstance()->clearIdentity();
+
+    $this->dispatch('/opac/modules/orthodidacte');
+
+    $this->assertContains('/auth/login?redirect='.urlencode(ROOT_URL . BASE_URL . '/modules/orthodidacte'),
+                          $this->_response->getBody());
+  }
+}
+
+
+
+class ModulesControllerOrthodidacteUserWithoutGroupTest extends AbstractControllerTestCase {
+  public function setUp() {
+    parent::setUp();
+    RessourcesNumeriquesFixtures::activateOrthodidacte();
+    $this->dispatch('/opac/modules/orthodidacte', true);
+  }
+
+
+  /** @test */
+  public function shouldHaveAccessForbiddenMessage() {
+    $this->assertFlashMessengerContentContains('Votre abonnement ne permet pas');
+  }
+}
+
+
+
+class ModulesControllerOrthodidacteUserWithGroupWithoutRightTest extends AbstractControllerTestCase {
+  public function setUp() {
+    parent::setUp();
+    RessourcesNumeriquesFixtures::activateOrthodidacte();
+    $group = $this->fixture('Class_UserGroup', ['id' => 1]);
+    Class_Users::getIdentity()->setUserGroups([$group]);
+    $this->dispatch('/opac/modules/orthodidacte', true);
+  }
+
+
+  /** @test */
+  public function shouldHaveAccessForbiddenMessage() {
+    $this->assertFlashMessengerContentContains('Votre abonnement ne permet pas');
+  }
+}
+
+
+
+class ModulesControllerOrthodidacteUserWithGroupWithRightTest extends AbstractControllerTestCase {
+  protected $_storm_default_to_volatile = true;
+
+  public function setUp() {
+    parent::setUp();
+
+    RessourcesNumeriquesFixtures::activateOrthodidacte();
+    $group = $this->fixture('Class_UserGroup', ['id' => 1])
+                  ->addRight(Class_UserGroup::RIGHT_ACCESS_ORTHODIDACTE);
+    Class_Users::getIdentity()->setUserGroups([$group]);
+
+    $this->dispatch('/opac/modules/orthodidacte', true);
+  }
+
+
+  /** @test */
+  public function shouldHaveAccessToOrthodidacte() {
+    $this->assertContains('https://plateforme.orthodidacte.com', $this->_response->getBody());
+  }
+}
+
+
+
+class ModulesControllerOrthodidacteUserWithGroupWithRightCasTest extends AbstractControllerTestCase {
+  protected $_storm_default_to_volatile = true;
+
+  public function setUp() {
+    parent::setUp();
+
+    RessourcesNumeriquesFixtures::activateOrthodidacte();
+    $group = $this->fixture('Class_UserGroup', ['id' => 1])
+                  ->addRight(Class_UserGroup::RIGHT_ACCESS_ORTHODIDACTE);
+    Class_Users::getIdentity()->setUserGroups([$group]);
+    $_SERVER['HTTP_REFERER'] = 'http://localhost/view/notice';
+
+    $this->dispatch('/opac/modules/orthodidacte', true);
+  }
+
+
+  /** @test */
+  public function shouldHaveAccessToOrthodidacte() {
+    $this->assertContains('https://plateforme.orthodidacte.com/authenticate',
+                          $this->_response->getBody());
+  }
+}
diff --git a/tests/fixtures/RessourcesNumeriquesFixtures.php b/tests/fixtures/RessourcesNumeriquesFixtures.php
index e704ccbf19d..a22e9b48e89 100644
--- a/tests/fixtures/RessourcesNumeriquesFixtures.php
+++ b/tests/fixtures/RessourcesNumeriquesFixtures.php
@@ -37,6 +37,7 @@ class RessourcesNumeriquesFixtures {
     self::deactivateKidilangues();
     self::deactivateJamendo();
     self::deactivateDilicom();
+    self::deactiveteOthodidacte();
   }
 
 
@@ -58,6 +59,7 @@ class RessourcesNumeriquesFixtures {
     self::activateKidilangues();
     self::activateJamendo();
     self::activateDilicom();
+    self::activateOrthodidacte();
   }
 
 
@@ -276,6 +278,14 @@ class RessourcesNumeriquesFixtures {
     Class_AdminVar::newInstanceWithId('DILICOM_PNB_GLN_CONTRACTOR', ['valeur' => '']);
     Class_AdminVar::newInstanceWithId('DILICOM_PNB_IP_ADRESSES', ['valeur' => '']);
   }
-}
 
+
+  public static function activateOrthodidacte() {
+    Class_AdminVar::newInstanceWithId('OTHODIDACTE', ['valeur' => '1']);
+  }
+
+  public static function deactivateOrthodidacte() {
+    Class_AdminVar::newInstanceWithId('OTHODIDACTE', ['valeur' => '0']);
+  }
+}
 ?>
-- 
GitLab