-
Notifications
You must be signed in to change notification settings - Fork 0
/
eduerp.module
191 lines (168 loc) · 5.76 KB
/
eduerp.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
function eduerp_menu() {
$items['admin/settings/eduerp'] = array (
'title' => 'EduERP Configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array (
'eduerp_admin_settings'
),
'access arguments' => array (
'administer site configuration'
),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* implements the simpletest testing framework hook
* for more information see http://drupal.org/project/simpletest
*/
function eduerp_simpletest() {
$module_name = "eduerp";
$dir = drupal_get_path('module', $module_name) . "/tests";
$tests = file_scan_directory($dir, '\.test$');
return array_keys($tests);
}
function eduerp_lga() {
static $ritles;
if (!$titles) {
$titles[0] = '';
$r = db_query("select * from lga order by lga_name");
while ($f = db_fetch_object($r))
$titles[$f->lga_id] = $f->lga_name;
}
return $titles;
}
function eduerp_states($state_id = 0) {
static $ritles;
if (!$titles) {
$titles[0] = '';
$r = db_query("select * from state order by state_name");
while ($f = db_fetch_object($r))
$titles[$f->state_id] = $f->state_name;
}
return ($state_id) ? $titles[$state_id] : $titles;
}
function eduerp_admin_settings() {
$form['RegisterAllCoursesatStartofSession'] = array(
'#type' => 'checkbox',
'#title' => 'Register All Courses at Start of Session',
'#default_value' => variable_get('RegisterAllCoursesatStartofSession', 0),
'#description' => 'This switch determines whether students register courses for both semesters at the start of the first semester',
'#tree' => TRUE
);
$form['RegistrarApprovesGrades'] = array(
'#type' => 'checkbox',
'#title' => 'RegistrarApprovesGrades',
'#default_value' => variable_get('RegistrarApprovesGrades', 0),
'#description' => 'This switch determines whether student grades are approved by the Registrar (if not they go straight to the Vice Chancellor)',
'#tree' => TRUE
);
return system_settings_form($form);
}
/**
* loads a user's profile for reading and modification
* @param int $uid specifies the user id of the profile to load
* @param string $profile_type (optional) specifies the actual
* profile type to load
* @return object returns the profile node or false if there's none
*/
function eduerp_load_profile($uid, $profile_type = 'profile') {
if ($node = node_load(array('type' => $profile_type, 'uid' => $uid))) {
return $node;
} else {
return false;
}
}
/**
* saves a user profile node - nothing fanciful just a wrapper
*
* @param object $node
* @return void since the node_save api function doesn't return
* any status, we cannot return one.
*/
function eduerp_save_profile($node) {
node_save($node);
}
/**
* class definition for User profiles
* @var int $uid stores the userid that is being used to retrieve the profile nodes
* @var array $field_map field map is used to store the node/field mapping
* @var array $profile_nodes stores the node objects used as content profiles for the user
*/
class UserProfile {
var $uid;
var $field_map = array();
var $profile_nodes = array();
/**
* class constructor - instantiates the object
* and loads necessary nodes.
* @var int $uid User id for the user
* @return object
*/
function __construct($uid) {
$this->uid = $uid;
// obtain all content profile types and use that to map
// the fields to the object.
$content_profiles = content_profile_get_types('names');
// after obtainining the content profile types, we'll retrieve
// those that have been defined for this user.
foreach ($content_profiles as $content_profile => $content_profile_name) {
if ($node = eduerp_load_profile($this->uid, $content_profile)) {
$this->profile_nodes[$content_profile] = $node;
}
}
$this->_hydrate_fields();
}
/**
* _hydrate_fields - loads fields from the content profile nodes
* @return void
*/
private function _hydrate_fields() {
foreach ($this->profile_nodes as $node) {
// obtain visible properties for nodes and set them
// it sets $this->profile_first_name = $node->field_profile_first_name
// for instance (supports only native type and nodereference
// field types)
// TODO: add support for other node field types
foreach ($node as $key => $value) {
if (preg_match('/^field\_(.*)/', $key, $matches)) {
$node_value = $node->$key;
// check the existence of readable values
if (is_array($node_value) && is_array($node_value[0]) && array_key_exists('value', $node_value[0])) {
$this->$matches[1] = $node_value[0]['value'];
$this->field_map[$matches[1]] = $node->type;
} elseif (is_array($node_value) && is_array($node_value[0]) && array_key_exists('nid', $node_value[0])) {
$this->$matches[1] = $node_value[0]['nid'];
$this->field_map[$matches[1]] = $node->type;
}
}
}
}
}
/**
* this method saves all the attached content profile nodes
* @return void
*/
public function save() {
// iterate through all the profile fields and store
// their values in the respective nodes
foreach ($this as $key => $value) {
if (isset($this->field_map[$key])) {
$node_field_name = 'field_' . $key;
// must be returned as a reference if not
// it will not be saved.
$node_field =& $this->profile_nodes[$this->field_map[$key]]->$node_field_name;
if (array_key_exists('value', $node_field[0])) {
$node_field[0]['value'] = $value;
} elseif (array_key_exists('nid', $node_field[0])) {
$node_field[0]['nid'] = $value;
}
}
}
foreach ($this->profile_nodes as $node) {
node_save($node);
}
}
}
?>