1 /*******************************************************************************
2 * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
3 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
4 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
6 * This file is part of the work and learning management system Pentila Nero.
8 * Pentila Nero is free software. You can redistribute it and/or modify since
9 * you respect the terms of either (at least one of the both license) :
10 * - under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
14 * License, or any later version
15 * - the GNU Lesser General Public License as published by the
16 * Free Software Foundation, either version 3 of the license,
17 * or (at your option) any later version.
19 * There are special exceptions to the terms and conditions of the
20 * licenses as they are applied to this software. View the full text of
21 * the exception in file LICENSE-PROJECT.txt in the directory of this software
24 * Pentila Nero is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * Licenses for more details.
29 * You should have received a copy of the GNU Affero General Public License
30 * and the CeCILL-C and the GNU Lesser General Public License along with
31 * Pentila Nero. If not, see :
32 * <http://www.gnu.org/licenses/> and
33 * <http://www.cecill.info/licences.fr.html>.
34 ******************************************************************************/
35 package com.pentila.entSavoie.cahierDeTexte.util;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.ResourceBundle;
41 import javax.portlet.PortletPreferences;
43 import com.liferay.contacts.util.OrganizationFilter;
44 import com.liferay.portal.kernel.exception.PortalException;
45 import com.liferay.portal.kernel.exception.SystemException;
46 import com.liferay.portal.kernel.json.JSONArray;
47 import com.liferay.portal.kernel.json.JSONFactoryUtil;
48 import com.liferay.portal.kernel.json.JSONObject;
49 import com.liferay.portal.model.Group;
50 import com.liferay.portal.model.Organization;
51 import com.liferay.portal.model.User;
52 import com.liferay.portal.service.RoleLocalServiceUtil;
53 import com.pentila.entSavoie.ENTDisplayUtil;
54 import com.pentila.entSavoie.ENTRolesConstants;
55 import com.pentila.entSavoie.cahierDeTexte.model.MatiereEtab;
56 import com.pentila.entSavoie.cahierDeTexte.service.MatiereEtabLocalServiceUtil;
57 import com.pentila.entSavoie.communityInfos.model.CommunityInfos;
58 import com.pentila.entSavoie.communityInfos.service.CommunityInfosLocalServiceUtil;
59 import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
61 public class CDTUserSettings {
63 public static String getUserName(User u) {
64 return u.getFirstName().charAt(0) + ". " + ENTDisplayUtil.capitalize(u.getLastName());
67 public static List<Organization> getOrganizationFiltered(User u) throws Exception {
69 List<Organization> orgsList = new ArrayList<Organization>();
70 List<Organization> tmpList = new ArrayList<Organization>();
71 // on ne fait pas de difference pour le moment
72 // --> tout le monde ne peut voir que les org de dernier niveau --> logiquement les classes
75 if (RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
76 ENTRolesConstants.NATIONAL_3, false) ||
77 RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
78 ENTRolesConstants.NATIONAL_1, false) ||
79 RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
80 ENTRolesConstants.NATIONAL_2, false)
82 tmpList = OrganizationFilter.getUserClassesAndGroups(u);
85 tmpList = OrganizationFilter.getUserClassesAndGroups(u);
88 // De plus on filtre pour enlever les organisations contenant les parents
89 // On essaye de garder uniquement les classes d'eleves
90 // on enleve aussi les archives
91 for (Organization o: tmpList) {
92 PortletPreferences prefs = o.getPreferences();
94 if (prefs.getValue("migrate", "none").equals("none") &&
95 !o.getName().contains("- Parent") &&
96 !o.getName().contains("- Enseignants") &&
97 !o.getName().contains("- Personnels")) {
105 public static JSONArray addUserGroups(User u, JSONArray jsonArray) {
106 List<Group> listUserGrp = new ArrayList<Group>();
108 listUserGrp = u.getGroups();
109 } catch (PortalException e1) {
110 // TODO Auto-generated catch block
111 e1.printStackTrace();
112 } catch (SystemException e1) {
113 // TODO Auto-generated catch block
114 e1.printStackTrace();
116 for (Group gTmp: listUserGrp) {
118 CommunityInfos ci = CommunityInfosLocalServiceUtil.getCommunityInfosByGroupId(gTmp.getGroupId());
119 if (ci.getStatus()==0) {
120 JSONObject orgJson = JSONFactoryUtil.createJSONObject();
121 orgJson.put("groupId", gTmp.getGroupId());
122 orgJson.put("name", gTmp.getName());
123 jsonArray.put(orgJson);
127 // aucune info pour ce groupe --> surement le groupe Guest
136 public static List<Group> getUserGroups(User u) {
137 List<Group> listUserGrp = new ArrayList<Group>();
139 listUserGrp = u.getGroups();
140 } catch (PortalException e1) {
141 // TODO Auto-generated catch block
142 e1.printStackTrace();
143 } catch (SystemException e1) {
144 // TODO Auto-generated catch block
145 e1.printStackTrace();
147 List<Group> listResult = new ArrayList<Group>();
149 for (Group gTmp: listUserGrp) {
151 CommunityInfos ci = CommunityInfosLocalServiceUtil.getCommunityInfosByGroupId(gTmp.getGroupId());
152 if (ci.getStatus()==0) {
153 listResult.add(gTmp);
157 // aucune info pour ce groupe --> surement le groupe Guest
158 //e.printStackTrace();
165 public static String getOrgAsJSON(User u) throws Exception {
166 JSONArray retJsonArray = JSONFactoryUtil.createJSONArray();
167 for (Organization org: getOrganizationFiltered(u)) {
168 JSONObject tmpMap = JSONFactoryUtil.createJSONObject();
170 tmpMap.put("groupId", String.valueOf(org.getGroup().getGroupId()));
171 tmpMap.put("name", org.getName());} catch (Exception e) {}
172 retJsonArray.put(tmpMap);
175 retJsonArray = addUserGroups(u, retJsonArray);
177 String result = retJsonArray.toString();
181 public static Boolean hasOrgCDT(User u) throws Exception {
182 if (getOrganizationFiltered(u).size()>0) {
190 public static long getDefaultEnseignement(User u) throws Exception {
191 // matiere par defaut associee a la personne (enseignant)
194 long etabId = OrganizationFinderServiceUtil.getEtabRatachement(u).getOrganizationId();
196 for (MatiereEtab matiereEtab : MatiereEtabLocalServiceUtil.getMatiereEtabByEtabIdState(etabId, true)) {
197 return matiereEtab.getMatiereId();
199 }catch (Exception e) {}
200 // this case does not have to appear
204 public static String listKindEvtCDT(User u, ResourceBundle messages) {
207 // On definit les type d evt possible --> note arrivera plus tard
208 JSONArray retJsonArray = JSONFactoryUtil.createJSONArray();
209 JSONObject tmpMap = JSONFactoryUtil.createJSONObject();
211 tmpMap.put("label", messages.getString("cours"));
212 retJsonArray.put(tmpMap);
213 tmpMap = JSONFactoryUtil.createJSONObject();
215 tmpMap.put("label", messages.getString("devoir"));
216 retJsonArray.put(tmpMap);
217 return retJsonArray.toString();
220 public static Boolean isCDTReadOnly(User u) throws Exception {
221 // Seul les enseignants et principaux peuvent ecrire
222 if ( RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
223 ENTRolesConstants.NATIONAL_3, false) ||
224 RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
225 ENTRolesConstants.NATIONAL_4, false)
234 public static Boolean hasGroupedCDT(User u) throws PortalException, SystemException {
235 // calendrier plutot destin� aux eleves --> regroupe l'ensemble de leur calendar
236 if ( RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
237 ENTRolesConstants.NATIONAL_1, false)
246 public static Boolean hasCreatorCDT(User u) throws PortalException, SystemException {
248 if ( RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
249 ENTRolesConstants.NATIONAL_3, false) ||
250 RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
251 ENTRolesConstants.NATIONAL_4, false) ||
252 RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
253 ENTRolesConstants.NATIONAL_5, false) ||
254 RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
255 ENTRolesConstants.NATIONAL_6, false)||
256 RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
257 ENTRolesConstants.NATIONAL_7, false)
266 public static Boolean hasPersonnalCDT(User u) {