source: sipes/0.3-modules/ente_planificador_sipp_mcti/ente_planificador_sipp_mcti.module @ eb502b4

stableversion-3.0
Last change on this file since eb502b4 was b907fa2, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se modifico el ambito del usuario

  • Propiedad mode establecida a 100755
File size: 15.7 KB
Línea 
1<?php
2  /**
3  * Sistema de Planificación y Presupuesto - SIPP
4  * @file ente_planificador_sipp_mcti.module
5  * Drupal part Module to code ente planificador module
6  * Copyright 2013 Sistema de Planificación y Presupuesto - SIPP
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  * 
22  * @author Cenditel Merida - Ing. Jose Puentes
23  * @date 2014-12-08 // (a&#241;o-mes-dia)
24  * @version 0.1 // (0.1)
25  *
26  */
27
28/*
29 * hook_form_alter
30 */
31function ente_planificador_sipp_mcti_form_alter(&$form, $form_state, $form_id) {
32  /*
33   * Configuración del ente planificador SIPP
34   * Creación del Rol para el Actor Enlace
35   */ 
36  if ($form_id == 'ente_planificador_admin_settings') {
37    $roles = user_roles();
38    $roles[0] = t('No aplica');
39    unset($roles[1]); //se destruye el anonimo
40    $form['coordinador_ambito_user'] = array(
41      '#type' => 'fieldset',
42      '#title' => t('Coordinador de Ambito y Supervisor de Organismo Publicos'),
43      '#collapsible' => TRUE,
44      '#collapsed' => FALSE,
45    );
46    //rol enlace nivel 1
47    $form['coordinador_ambito_user']['ActorPlanificadorcoordinador1_text'] = array(
48      '#type' => 'textfield',
49      '#title' => t('Nombre del tipo de Rol para el Actor Enlace Revisor de Ambito'),
50      '#default_value' => variable_get('ActorPlanificadorcoordinador1_text', t('Ente tipo 1 (Enlace Revisor)')),
51      '#size' => 60,
52      '#maxlength' => 244,
53      '#description' => t('Introduzca el nombre del tipo de rol que ha de tener el usuario para ser coordinador de ambito'),
54      '#required' => TRUE,
55    );
56    $form['coordinador_ambito_user']['ActorPlanificadorcoordinador1'] = array(
57      '#type' => 'select',
58      '#title' => t('Rol para el Actor Enlace Revisor de Ambito'),
59      '#default_value' => variable_get('ActorPlanificadorcoordinador1', 0),
60      '#options' => $roles,
61    );
62    //rol formulador nivel 1
63    $form['coordinador_ambito_user']['ActorPlanificadorcoordinador2_text'] = array(
64      '#type' => 'textfield',
65      '#title' => t('Nombre del tipo de Rol para el Actor Supervisor de Ambito'),
66      '#default_value' => variable_get('ActorPlanificadorcoordinador2_text', t('Ente tipo 1 (Supervisor)')),
67      '#size' => 60,
68      '#maxlength' => 244,
69      '#description' => t('Introduzca el nombre del tipo de rol que ha de tener el usuario para ser coordinador de ambito'),
70      '#required' => TRUE,
71      '#prefix' => '<hr>',
72    );
73    $form['coordinador_ambito_user']['ActorPlanificadorcoordinador2'] = array(
74      '#type' => 'select',
75      '#title' => t('Rol para el Actor Supervisor de Ambito'),
76      '#default_value' => variable_get('ActorPlanificadorcoordinador2', 0),
77      '#options' => $roles,
78    );
79    //rol enlace nivel 2
80    $form['coordinador_ambito_user']['ActorPlanificadorevisor2_text'] = array(
81      '#type' => 'textfield',
82      '#title' => t('Nombre del tipo de Rol para el Actor Enlace Revisor de segundo nivel'),
83      '#default_value' => variable_get('ActorPlanificadorevisor2_text', t('Ente tipo 2 (Enlace Revisor)')),
84      '#size' => 60,
85      '#maxlength' => 244,
86      '#description' => t('Introduzca el nombre del tipo de rol que ha de tener el usuario para ser revisor del segundo nivel'),
87      '#required' => TRUE,
88      '#prefix' => '<hr>',
89    );
90    $form['coordinador_ambito_user']['ActorPlanificadorevisor2'] = array(
91      '#type' => 'select',
92      '#title' => t('Rol para el Actor Enlace Revisor del nivel 2'),
93      '#default_value' => variable_get('ActorPlanificadorevisor2', 0),
94      '#options' => $roles,
95    );
96    //rol supervisor segundo nivel
97    $form['coordinador_ambito_user']['ActorPlanificadorsupervisor2_text'] = array(
98      '#type' => 'textfield',
99      '#title' => t('Nombre del tipo de Rol para el Actor Supervisor de Organismos Publicos'),
100      '#default_value' => variable_get('ActorPlanificadorsupervisor2_text', t('Ente tipo 2 (Supervisor)')),
101      '#size' => 60,
102      '#maxlength' => 244,
103      '#description' => t('Introduzca el nombre del tipo de rol que ha de tener el usuario para ser Supervisor de los Organismo Públicos.'),
104      '#required' => TRUE,
105      '#prefix' => '<hr>',
106    );
107    $form['coordinador_ambito_user']['ActorPlanificadorsupervisor2'] = array(
108      '#type' => 'select',
109      '#title' => t('Rol para el Actor Supervisor de Organismo Públicos'),
110      '#default_value' => variable_get('ActorPlanificadorsupervisor2', 0),
111      '#options' => $roles,
112    );
113    //rol supervisor tercer nivel
114    $form['coordinador_ambito_user']['ActorPlanificadorsupervisor3_text'] = array(
115      '#type' => 'textfield',
116      '#title' => t('Nombre del tipo de Rol para el Actor Supervisor de los Entes adscritos'),
117      '#default_value' => variable_get('ActorPlanificadorsupervisor3_text', t('Ente tipo 3 (Supervisor)')),
118      '#size' => 60,
119      '#maxlength' => 244,
120      '#description' => t('Introduzca el nombre del tipo de rol que ha de tener el usuario para ser Supervisor de los Entes Adscritos.'),
121      '#required' => TRUE,
122      '#prefix' => '<hr>',
123    );
124    $form['coordinador_ambito_user']['ActorPlanificadorsupervisor3'] = array(
125      '#type' => 'select',
126      '#title' => t('Rol para el Actor Supervisor de los Entes Adscritos'),
127      '#default_value' => variable_get('ActorPlanificadorsupervisor3', 0),
128      '#options' => $roles,
129    );
130    //revisor Tercer nivel
131    $form['coordinador_ambito_user']['ActorPlanificadorevisor3'] = array(
132      '#type' => 'select',
133      '#title' => t('Rol para el Actor Enlace Revisor del nivel 3'),
134      '#default_value' => variable_get('ActorPlanificadorevisor3', 0),
135      '#options' => $roles,
136    );
137    //rol supervisor cuarto nivel
138    $form['coordinador_ambito_user']['ActorPlanificadorsupervisor4_text'] = array(
139      '#type' => 'textfield',
140      '#title' => t('Nombre del tipo de Rol para el Actor Supervisor de los Entes adscritos a Otros Entes'),
141      '#default_value' => variable_get('ActorPlanificadorsupervisor4_text', t('Ente tipo 4 (Supervisor)')),
142      '#size' => 60,
143      '#maxlength' => 244,
144      '#description' => t('Introduzca el nombre del tipo de rol que ha de tener el usuario para ser Supervisor de los Sub-Entes Adscritos.'),
145      '#required' => TRUE,
146      '#prefix' => '<hr>',
147    );
148    $form['coordinador_ambito_user']['ActorPlanificadorsupervisor4'] = array(
149      '#type' => 'select',
150      '#title' => t('Rol para el Actor Supervisor de los Sub-Entes Adscritos'),
151      '#default_value' => variable_get('ActorPlanificadorsupervisor4', 0),
152      '#options' => $roles,
153    );
154    //revisor segundo nivel
155    $form['coordinador_ambito_user']['ActorPlanificadorevisor4'] = array(
156      '#type' => 'select',
157      '#title' => t('Rol para el Actor Enlace Revisor del nivel 4'),
158      '#default_value' => variable_get('ActorPlanificadorevisor4', 0),
159      '#options' => $roles,
160    );
161    //rol supervisor quinto nivel
162    $form['coordinador_ambito_user']['ActorPlanificadorsupervisor5_text'] = array(
163      '#type' => 'textfield',
164      '#title' => t('Nombre del tipo de Rol para el Actor Supervisor de los Entes adscritos a Otros Entes'),
165      '#default_value' => variable_get('ActorPlanificadorsupervisor5_text', t('Ente tipo 5 (Supervisor)')),
166      '#size' => 60,
167      '#maxlength' => 244,
168      '#description' => t('Introduzca el nombre del tipo de rol que ha de tener el usuario para ser Supervisor de los Sub-Entes Adscritos.'),
169      '#required' => TRUE,
170      '#prefix' => '<hr>',
171    );
172    $form['coordinador_ambito_user']['ActorPlanificadorsupervisor5'] = array(
173      '#type' => 'select',
174      '#title' => t('Rol para el Actor Supervisor de los Sub-Entes Adscritos'),
175      '#default_value' => variable_get('ActorPlanificadorsupervisor5', 0),
176      '#options' => $roles,
177    );
178    $form['buttons']['#weight'] = 10;
179  }
180  if (($form_id == 'user_register') || ($form_id == 'user_profile_form' && arg(2) != 'chgpwd' && user_access('administer users'))) {
181    global $user;
182    $ente_planificador = FALSE;
183    //get ente planificador       
184    if (arg(4) && is_numeric(arg(4))) {
185      $ente_planificador = node_load(arg(4));
186    }
187    if (isset($form['_account']['#value']->entes) && count($form['_account']['#value']->entes)) {
188      $ente_planificador = node_load($form['_account']['#value']->entes[0]['nid']);
189    }
190    //ambito
191    $tid = variable_get('ente_planificador_sector_Ambitos', 0);
192    $terms = taxonomy_get_tree($tid);
193    foreach ($terms as $term) {
194      if ($term->depth == 0) { //si el termino es padre
195        $ambito[$term->tid] = $term->name;
196      }
197      if ($term->depth == 1) {
198        $organismo[$term->tid] = $term->name;
199      }
200      if ($term->depth == 2) {
201        $ente_adscrito[$term->tid] = $term->name;
202      }
203      if ($term->depth == 3) {
204        $sub_ente[$term->tid] = $term->name;
205      }
206    }
207    $roles = user_roles();
208    if ($ente_planificador && ($ente_planificador->tipo == 1)) { // ente nivel 1
209      $coordinador1 = variable_get('ActorPlanificadorcoordinador1', 0);
210      $coordinador2 = variable_get('ActorPlanificadorcoordinador2', 0);
211      $count_coordinador1 = ente_planificador_sipp_mcti_count_user_by_role($coordinador1, $ente_planificador->nid);
212      $count_coordinador2 = ente_planificador_sipp_mcti_count_user_by_role($coordinador2, $ente_planificador->nid);
213       
214      $text[$coordinador1] = variable_get('ActorPlanificadorcoordinador1_text','');
215      $text[$coordinador2] = variable_get('ActorPlanificadorcoordinador2_text','');
216      if ($coordinador1 && isset($roles[$coordinador1])) {
217        $form['account']['roles']['#options'][$coordinador1] = $text[$coordinador1];
218      }
219      if ($coordinador2 && isset($roles[$coordinador2])) {
220        $form['account']['roles']['#options'][$coordinador2] = $text[$coordinador2];
221      }
222     
223      //solo se permite enlaces formuladores por el numero de ambitos existentes.
224      if ($count_coordinador1 > count($ambito)) {
225        unset($form['account']['roles']['#options'][$coordinador1]);
226      }
227      //solo se permite enlaces revisores por el numero de ambitos existentes.
228      if ($count_coordinador2 > count($ambito)) {
229        unset($form['account']['roles']['#options'][$coordinador2]);
230      }
231      $ambito[0] = t("Seleccione");
232      $ambito_user = db_result(db_query("SELECT ambito FROM {ente_user_planificador} WHERE usuario = %d", $form['#uid']));
233      $form['account']['roles']['#prefix'] = '<div id = "roles_user">';
234      $form['account']['roles']['#suffix'] = '</div>';
235      $ambito[0] = t("Seleccione");
236      $form['usuario_planificacion']['ambito_user'] = array(     
237        '#title' => t('Ambito del Usuario'),
238        '#type' =>'select',
239        '#default_value' => $ambito_user,
240        '#options' => $ambito,
241        '#prefix' => '<div id = "ambito_user">',
242        '#suffix' => '</div>',
243        '#weight' => 3,
244      );
245    }
246    if ($ente_planificador && ($ente_planificador->tipo == 2)) {
247      //solo se permite un supervisor por cada organismo
248      $supervisor2 = variable_get('ActorPlanificadorsupervisor2', 0);
249      $text[$supervisor2] = variable_get('ActorPlanificadorsupervisor2_text','');
250      $count_supervisor2 = ente_planificador_sipp_mcti_count_user_by_role($supervisor2, $ente_planificador->nid);
251      if ($supervisor2 && isset($roles[$supervisor2])) {
252        $form['account']['roles']['#options'][$supervisor2] = $text[$supervisor2];
253      }
254      if ($count_supervisor2 > count($organismo)) {
255        unset($form['account']['roles']['#options'][$supervisor2]);
256      }
257    }
258    if ($ente_planificador && ($ente_planificador->tipo == 3)) {
259      //solo se permite un supervisor por cada ente adscrito a un organismo
260      $supervisor3 = variable_get('ActorPlanificadorsupervisor3', 0);
261      $text[$supervisor3] = variable_get('ActorPlanificadorsupervisor3_text','');
262      $count_supervisor3 = ente_planificador_sipp_mcti_count_user_by_role($supervisor3, $ente_planificador->nid);
263      if ($supervisor3 && isset($roles[$supervisor3])) {
264        $form['account']['roles']['#options'][$supervisor3] = $text[$supervisor3];
265      }
266      if ($count_supervisor3 > count($ente_adscrito)) {
267        unset($form['account']['roles']['#options'][$supervisor3]);
268      }
269    }
270    if ($ente_planificador && ($ente_planificador->tipo == 4)) {
271      //solo se permite un supervisor por cada sub-ente adscrito
272      $supervisor4 = variable_get('ActorPlanificadorsupervisor4', 0);
273      $text[$supervisor4] = variable_get('ActorPlanificadorsupervisor4_text','');
274      $count_supervisor4 = ente_planificador_sipp_mcti_count_user_by_role($supervisor4, $ente_planificador->nid);
275      if ($supervisor4 && isset($roles[$supervisor4])) {
276        $form['account']['roles']['#options'][$supervisor4] = $text[$supervisor4];
277      }
278      if ($count_supervisor4 > count($sub_ente)) {
279        unset($form['account']['roles']['#options'][$supervisor4]);
280      }
281    }
282    $path_module = drupal_get_path('module', 'ente_planificador_sipp_mcti');
283    drupal_add_js($path_module . '/js/ente_planificador_sipp_mcti.js');
284    $usuarios['roles_user'] = array($coordinador1, $coordinador2);
285    drupal_add_js($usuarios, 'setting');
286  }
287}
288
289/**
290 * Obtiene el numero de usuarios que poseen un determinado rol
291 */
292function ente_planificador_sipp_mcti_count_user_by_role($rol, $ente) {
293  $result = 0;
294  if ($rol && $ente){
295    $result = db_result(db_query("SELECT count(n.usuario) FROM {users_roles} as u INNER JOIN {ente_user_planificador} as n ON n.usuario = u.uid INNER JOIN {ente_planificador} as e ON e.nid = n.nid where u.rid = %d AND e.nid = %d", $rol, $ente));
296  }
297  return $result;
298}
299
300/*
301 * hook_user
302 * Verificando que se inserte el Rol seleccionado en el Registro del usuario
303 * Si el Rol es Enlace se elimina el Rol Supervisor(Micro) que viene por
304 * defecto del modulo ente_planificador en la función: ente_planificador_user
305*/
306function ente_planificador_sipp_mcti_user($op, &$edit, &$user, $category = NULL) {
307  switch ($op) {
308    case 'update':
309      if (isset($edit['ambito_user'])) {
310        db_query("UPDATE {ente_user_planificador} SET ambito = %d WHERE usuario = %d",  $edit['ambito_user'], $user->uid); 
311      }
312    break;
313    case 'insert':
314      if (isset($edit['ambito_user'])) {
315        db_query("UPDATE {ente_user_planificador} SET ambito = %d WHERE usuario = %d",  $edit['ambito_user'], $user->uid); 
316      }
317      if(array_key_exists('entes', $edit) && $edit['entes']) {
318        $roles = user_roles();
319        $ente_planificador = node_load($edit['entes']);       
320        $rolenlace = variable_get('ActorPlanificadorEnlace', 2);
321        $rolsupervisor = variable_get('ActorPlanificadorMicro', 2);
322        //Si se han creado los roles
323        if(isset($roles[$rolenlace]) && isset($roles[$rolsupervisor])){         
324          if ($ente_planificador->tipo) {
325            if($ente_planificador->tipo && $ente_planificador->tipo == 2) {       
326              $edit['roles'][2] = $roles[2];
327              // Si se selecciona el rol Enlace
328              if(isset($edit['roles'][$rolenlace])){
329                //Elimina el rol supervisor
330                unset($edit['roles'][$rolsupervisor]);
331              }
332              /*
333              if(isset($edit['roles'][$rolsupervisor])){             
334                unset($edit['roles'][$rolenlace]);
335              */
336            }
337          }
338        }
339      }     
340    break;   
341  }
342}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.