--- /dev/null
+/*******************************************************************************
+ * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
+ *
+ * This file is part of the work and learning management system Pentila Nero.
+ *
+ * Pentila Nero is free software. You can redistribute it and/or modify since
+ * you respect the terms of either (at least one of the both license) :
+ * - under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ * - the CeCILL-C as published by CeCILL-C; either version 1 of the
+ * License, or any later version
+ * - the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the license,
+ * or (at your option) any later version.
+ *
+ * There are special exceptions to the terms and conditions of the
+ * licenses as they are applied to this software. View the full text of
+ * the exception in file LICENSE-PROJECT.txt in the directory of this software
+ * distribution.
+ *
+ * Pentila Nero 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
+ * Licenses for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * and the CeCILL-C and the GNU Lesser General Public License along with
+ * Pentila Nero. If not, see :
+ * <http://www.gnu.org/licenses/> and
+ * <http://www.cecill.info/licences.fr.html>.
+ ******************************************************************************/
+package com.pentila.entSavoie.cahierDeTexte.util;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.ResourceBundle;
+
+import javax.portlet.PortletPreferences;
+
+import com.liferay.contacts.util.OrganizationFilter;
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.json.JSONArray;
+import com.liferay.portal.kernel.json.JSONFactoryUtil;
+import com.liferay.portal.kernel.json.JSONObject;
+import com.liferay.portal.model.Group;
+import com.liferay.portal.model.Organization;
+import com.liferay.portal.model.User;
+import com.liferay.portal.service.RoleLocalServiceUtil;
+import com.pentila.entSavoie.ENTDisplayUtil;
+import com.pentila.entSavoie.ENTRolesConstants;
+import com.pentila.entSavoie.cahierDeTexte.model.MatiereEtab;
+import com.pentila.entSavoie.cahierDeTexte.service.MatiereEtabLocalServiceUtil;
+import com.pentila.entSavoie.communityInfos.model.CommunityInfos;
+import com.pentila.entSavoie.communityInfos.service.CommunityInfosLocalServiceUtil;
+import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
+
+public class CDTUserSettings {
+
+ public static String getUserName(User u) {
+ return u.getFirstName().charAt(0) + ". " + ENTDisplayUtil.capitalize(u.getLastName());
+ }
+
+ public static List<Organization> getOrganizationFiltered(User u) throws Exception {
+
+ List<Organization> orgsList = new ArrayList<Organization>();
+ List<Organization> tmpList = new ArrayList<Organization>();
+ // on ne fait pas de difference pour le moment
+ // --> tout le monde ne peut voir que les org de dernier niveau --> logiquement les classes
+
+
+ if (RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ ENTRolesConstants.NATIONAL_3, false) ||
+ RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ ENTRolesConstants.NATIONAL_1, false) ||
+ RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ ENTRolesConstants.NATIONAL_2, false)
+ ) {
+ tmpList = OrganizationFilter.getUserClassesAndGroups(u);
+ }
+ else {
+ tmpList = OrganizationFilter.getUserClassesAndGroups(u);
+ }
+
+ // De plus on filtre pour enlever les organisations contenant les parents
+ // On essaye de garder uniquement les classes d'eleves
+ // on enleve aussi les archives
+ for (Organization o: tmpList) {
+ PortletPreferences prefs = o.getPreferences();
+
+ if (prefs.getValue("migrate", "none").equals("none") &&
+ !o.getName().contains("- Parent") &&
+ !o.getName().contains("- Enseignants") &&
+ !o.getName().contains("- Personnels")) {
+ orgsList.add(o);
+ }
+ }
+
+ return orgsList;
+ }
+
+ public static JSONArray addUserGroups(User u, JSONArray jsonArray) {
+ List<Group> listUserGrp = new ArrayList<Group>();
+ try {
+ listUserGrp = u.getGroups();
+ } catch (PortalException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ } catch (SystemException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ for (Group gTmp: listUserGrp) {
+ try {
+ CommunityInfos ci = CommunityInfosLocalServiceUtil.getCommunityInfosByGroupId(gTmp.getGroupId());
+ if (ci.getStatus()==0) {
+ JSONObject orgJson = JSONFactoryUtil.createJSONObject();
+ orgJson.put("groupId", gTmp.getGroupId());
+ orgJson.put("name", gTmp.getName());
+ jsonArray.put(orgJson);
+ }
+ }
+ catch(Exception e) {
+ // aucune info pour ce groupe --> surement le groupe Guest
+ e.printStackTrace();
+ }
+ }
+
+ return jsonArray;
+
+ }
+
+ public static List<Group> getUserGroups(User u) {
+ List<Group> listUserGrp = new ArrayList<Group>();
+ try {
+ listUserGrp = u.getGroups();
+ } catch (PortalException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ } catch (SystemException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ List<Group> listResult = new ArrayList<Group>();
+
+ for (Group gTmp: listUserGrp) {
+ try {
+ CommunityInfos ci = CommunityInfosLocalServiceUtil.getCommunityInfosByGroupId(gTmp.getGroupId());
+ if (ci.getStatus()==0) {
+ listResult.add(gTmp);
+ }
+ }
+ catch(Exception e) {
+ // aucune info pour ce groupe --> surement le groupe Guest
+ //e.printStackTrace();
+ }
+ }
+
+ return listResult;
+ }
+
+ public static String getOrgAsJSON(User u) throws Exception {
+ JSONArray retJsonArray = JSONFactoryUtil.createJSONArray();
+ for (Organization org: getOrganizationFiltered(u)) {
+ JSONObject tmpMap = JSONFactoryUtil.createJSONObject();
+ try {
+ tmpMap.put("groupId", String.valueOf(org.getGroup().getGroupId()));
+ tmpMap.put("name", org.getName());} catch (Exception e) {}
+ retJsonArray.put(tmpMap);
+ }
+
+ retJsonArray = addUserGroups(u, retJsonArray);
+
+ String result = retJsonArray.toString();
+ return result;
+ }
+
+ public static Boolean hasOrgCDT(User u) throws Exception {
+ if (getOrganizationFiltered(u).size()>0) {
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+
+ public static long getDefaultEnseignement(User u) throws Exception {
+ // matiere par defaut associee a la personne (enseignant)
+
+ try{
+ long etabId = OrganizationFinderServiceUtil.getEtabRatachement(u).getOrganizationId();
+
+ for (MatiereEtab matiereEtab : MatiereEtabLocalServiceUtil.getMatiereEtabByEtabIdState(etabId, true)) {
+ return matiereEtab.getMatiereId();
+ }
+ }catch (Exception e) {}
+ // this case does not have to appear
+ return new Long(0);
+ }
+
+ public static String listKindEvtCDT(User u, ResourceBundle messages) {
+
+
+ // On definit les type d evt possible --> note arrivera plus tard
+ JSONArray retJsonArray = JSONFactoryUtil.createJSONArray();
+ JSONObject tmpMap = JSONFactoryUtil.createJSONObject();
+ tmpMap.put("id", 1);
+ tmpMap.put("label", messages.getString("cours"));
+ retJsonArray.put(tmpMap);
+ tmpMap = JSONFactoryUtil.createJSONObject();
+ tmpMap.put("id", 2);
+ tmpMap.put("label", messages.getString("devoir"));
+ retJsonArray.put(tmpMap);
+ return retJsonArray.toString();
+ }
+
+ public static Boolean isCDTReadOnly(User u) throws Exception {
+ // Seul les enseignants et principaux peuvent ecrire
+ if ( RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ ENTRolesConstants.NATIONAL_3, false) ||
+ RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ ENTRolesConstants.NATIONAL_4, false)
+ ) {
+ return false;
+ }
+ else {
+ return true;
+ }
+ }
+
+ public static Boolean hasGroupedCDT(User u) throws PortalException, SystemException {
+ // calendrier plutot destin� aux eleves --> regroupe l'ensemble de leur calendar
+ if ( RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ ENTRolesConstants.NATIONAL_1, false)
+ ) {
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+
+ public static Boolean hasCreatorCDT(User u) throws PortalException, SystemException {
+
+ if ( RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ ENTRolesConstants.NATIONAL_3, false) ||
+ RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ ENTRolesConstants.NATIONAL_4, false) ||
+ RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ ENTRolesConstants.NATIONAL_5, false) ||
+ RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ ENTRolesConstants.NATIONAL_6, false)||
+ RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ ENTRolesConstants.NATIONAL_7, false)
+ ) {
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+
+ public static Boolean hasPersonnalCDT(User u) {
+ return false;
+ }
+}