~oh-dev/openhealth/phit-tools

« back to all changes in this revision

Viewing changes to ihris-suite/lib/ihris-manage/lib/iHRIS_Position.php

  • Committer: litlfred at ibiblio
  • Date: 2009-10-23 12:59:28 UTC
  • Revision ID: litlfred@ibiblio.org-20091023125928-u5lkafz0urm9t8eq
updated ihris-suite to 4.0.1-prerelease -- not debugged

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * © Copyright 2007, 2008 IntraHealth International, Inc.
4
 
 * 
5
 
 * This File is part of iHRIS
6
 
 * 
7
 
 * iHRIS is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 3 of the License, or
10
 
 * (at your option) any later version.
11
 
 * 
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 * 
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
 */
20
 
/**
21
 
 * @package iHRIS
22
 
 * @subpackage Manage
23
 
 * @author Luke Duncan <lduncan@intrahealth.org>
24
 
 * @copyright Copyright &copy; 2007, 2008 IntraHealth International, Inc. 
25
 
 * @since v1.0.0
26
 
 * @version v2.0.0
27
 
 */
28
 
/**
29
 
 * Object for dealing with positions.
30
 
 * 
31
 
 * @package iHRIS
32
 
 * @subpackage Manage
33
 
 * @access public
34
 
 */
35
 
class iHRIS_Position extends I2CE_List {
36
 
        
37
 
    /**
38
 
     * The main field name used for display a description of a record.
39
 
     */
40
 
    const MAIN_FIELD = "code";
41
 
    /**
42
 
     * The secondary field name used for displaying a description of a record in combination with the MAIN_FIELD.
43
 
     */
44
 
    const SEC_FIELD = "title";
45
 
    /**
46
 
     * Constant value for open position status.
47
 
     */
48
 
    const STATUS_OPEN = 1;
49
 
    /**
50
 
     * Constant value for closed position status.
51
 
     */
52
 
    const STATUS_CLOSED = 2;
53
 
    /**
54
 
     * Constant value for discontinued position status.
55
 
     */
56
 
    const STATUS_DISCONTINUED = 3;
57
 
 
58
 
    /**
59
 
     * @var array The list of position status.
60
 
     */
61
 
    static private $status = array( self::STATUS_OPEN => 'Open', self::STATUS_CLOSED => 'Closed', 
62
 
                                    self::STATUS_DISCONTINUED => 'Discontinued' );
63
 
 
64
 
    /**
65
 
     * Create a new instance of a position.
66
 
     * 
67
 
     * @param string $form The name of this form
68
 
     * @param integer $id
69
 
     */
70
 
    public function __construct( $form, $id = 0 ) {
71
 
        parent::__construct( $form, $id );
72
 
 
73
 
        $this->fields['type']->setValue( $form );
74
 
        
75
 
    }
76
 
    
77
 
    /**
78
 
     * Remove all fields except for the status and type for when only dealing with status updates.
79
 
     */
80
 
    public function statusOnly() {
81
 
        $this->clearFields( array( 'status', 'type' ) );
82
 
    }
83
 
    
84
 
    /**
85
 
     * Lookup the given value from the status array.
86
 
     * @param integer $id
87
 
     * @param string $form Not used for this method.
88
 
     * @return string
89
 
     */
90
 
    static public function lookupStatus( $id, $form="" ) {
91
 
        return I2CE_Form::lookupArray( $id, self::$status );
92
 
    }
93
 
    /**
94
 
     * List all the options from the status array.
95
 
     * @param string $form The  form we wish to lookup by
96
 
     * @returns array
97
 
     */
98
 
    static public function listStatusOptions($form ) {
99
 
        return self::$status;
100
 
    }
101
 
    /**
102
 
     * List all options except closed for the status arra
103
 
     * @param string $form The  form we wish to lookup byy.
104
 
     * @returns array
105
 
     */
106
 
    static public function listDepartureOptions($form ) {
107
 
        $status = self::$status;
108
 
        unset( $status[self::STATUS_CLOSED] );
109
 
        return $status;
110
 
    }
111
 
 
112
 
    /**
113
 
     * List all positions by status (the linked form for this object).
114
 
     * 
115
 
     * This will return an array that includes all status codes with all positions that
116
 
     * are in that status.
117
 
     * @return array
118
 
     */
119
 
    static public function getOptionsByLink() {
120
 
        $factory = I2CE_FormFactory::instance();
121
 
        return parent::_getOptionsByLink( $factory->getFormName( get_class() ), self::MAIN_FIELD, "status", self::SEC_FIELD );
122
 
    }
123
 
    
124
 
    /**
125
 
     * List all open positions
126
 
     * @param string $form The class being listed.
127
 
     * @returns array
128
 
     */
129
 
    static public function listOpen (  $form="position" ) {
130
 
        return I2CE_List::listOptions( $form, "status", 1 );
131
 
    }
132
 
 
133
 
    /**
134
 
     * Update the status for this position.
135
 
     * @param integer $status
136
 
     */
137
 
    public function setStatus( $status ) {
138
 
        $this->fields['status']->setValue( $status );
139
 
    }
140
 
        
141
 
    /**
142
 
     * Mark this position as closed and remove it from any applications.
143
 
     * 
144
 
     * The person being assigned the position will have all application positions removed.
145
 
     * @param I2CE_User &$user The user performing this action.
146
 
     * @param integer $person_id The person being assigned this position.
147
 
     */
148
 
    public function closePosition( &$user, $person_id ) {
149
 
        $factory = I2CE_FormFactory::instance();
150
 
        $this->setStatus( self::STATUS_CLOSED );
151
 
        $this->save( $user );
152
 
        if ( I2CE_ModuleFactory::instance()->isEnabled( "ihris-manage-Application" ) ) {
153
 
            $apps = $factory->callStatic( "person", "findApplicants", array( $this->id ) );
154
 
            if ( count( $apps ) > 0 ) {
155
 
                foreach ( $apps as $per_id => $per_name ) {
156
 
                    $app_per = $factory->createForm( "person", $per_id );
157
 
                    $app_per->populateChildren( array( "application" ) );
158
 
                    if( array_key_exists( 'application', $app_per->children ) &&
159
 
                            count( $app_per->children['application'] ) > 0 ) {
160
 
                        foreach( $app_per->children['application'] as $app ) {
161
 
                            $app->removePosition( $this->getId() );
162
 
                            $app->save( $user );
163
 
                        }
164
 
                    }
165
 
                }
166
 
                /*
167
 
                $form_id = I2CE_Form::getFormId( "application" );
168
 
                $sth = $this->db->prepare( "SELECT id,parent FROM record WHERE parent IN ( " 
169
 
                                        . implode( ",", array_keys( $apps ) ) . " ) ", null, MDB2_PREPARE_RESULT );
170
 
                if ( !I2CE::pearError( $sth, "Error preparing statement to close a position: " ) ) {
171
 
                    $app_ids = $sth->execute();
172
 
                    if ( !I2CE::pearError( $app_ids, "Error executing statement to close a position: " ) ) {
173
 
                        while( $data = $app_ids->fetchRow() ) {
174
 
                            $app = $factory->createForm( "application", $data->id );
175
 
                            $app->populate();
176
 
                            $app->removePosition( $this->id );
177
 
                            $app->save( $user );
178
 
                        }
179
 
                    }
180
 
                }
181
 
                */
182
 
            }
183
 
        }
184
 
    }
185
 
        
186
 
    /**
187
 
     * Return the list of record ids that have this position as supervisor.
188
 
     * @return array
189
 
     */
190
 
    public function getSupervised() {
191
 
        $sup_det = I2CE_FormField::getFormFieldIdAndType( "position", "supervisor" );
192
 
        if ( !$sup_det ) return array();
193
 
        $supervised = array();
194
 
        
195
 
        $db = MDB2::singleton();
196
 
        if ( !I2CE::pearError( $db, "Error getting database connection: " ) ) {
197
 
        
198
 
            $sth = $db->prepare( "SELECT record FROM last_entry WHERE form_field = ? AND " . $sup_det['type'] . "_value = ?",
199
 
                                 array('integer', 'integer' ), MDB2_PREPARE_RESULT );
200
 
            if ( !I2CE::pearError( $sth, "Error preparing statement to get supervised positions: " ) ) {
201
 
                $results = $sth->execute( array( $sup_det['id'], $this->id ) );
202
 
                if ( !I2CE::pearError( $results, "Error executing statement to get supervised positions: " ) ) {
203
 
                    while( $row = $results->fetchRow() ) {
204
 
                        $supervised[] = $row->record;
205
 
                                        
206
 
                    }
207
 
                }
208
 
            }
209
 
                        
210
 
        }
211
 
                
212
 
        return $supervised;
213
 
        
214
 
    }
215
 
        
216
 
}
217
 
 
218
 
# Local Variables:
219
 
# mode: php
220
 
# c-default-style: "bsd"
221
 
# indent-tabs-mode: nil
222
 
# c-basic-offset: 4
223
 
# End: