summaryrefslogtreecommitdiff
path: root/code_approval.inc
blob: 14b333e2fdbc57a159bd2a49fe88914d7221ffe0 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
// $Id$
function lab_migration_code_approval()
{
    /* get a list of unapproved solutions */
    //$pending_solution_q = db_query("SELECT * FROM {lab_migration_solution} WHERE approval_status = 0");
    $query = db_select('lab_migration_solution');
    $query->fields('lab_migration_solution');
    $query->condition('approval_status', 0);
    $pending_solution_q = $query->execute();
    if (!$pending_solution_q) {
        drupal_set_message(t('There are no pending code approvals.'), 'status');
        return '';
    }
    $pending_solution_rows = array();
    while ($pending_solution_data = $pending_solution_q->fetchObject()) {
        /* get experiment data */
        //$experiment_q = db_query("SELECT * FROM {lab_migration_experiment} WHERE id = %d", $pending_solution_data->experiment_id);
        $query = db_select('lab_migration_experiment');
        $query->fields('lab_migration_experiment');
        $query->condition('id', $pending_solution_data->experiment_id);
        $experiment_q = $query->execute();
        $experiment_data = $experiment_q->fetchObject();
        /* get proposal data */
        // $proposal_q = db_query("SELECT * FROM {lab_migration_proposal} WHERE id = %d", $experiment_data->proposal_id);
        $query = db_select('lab_migration_proposal');
        $query->fields('lab_migration_proposal');
        $query->condition('id', $experiment_data->proposal_id);
        $proposal_q = $query->execute();
        $proposal_data = $proposal_q->fetchObject();
        /* get solution provider details */
        $solution_provider_user_name = '';
        $user_data = user_load($proposal_data->solution_provider_uid);
        if ($user_data) {
            $solution_provider_user_name = $user_data->name;
        } else {
            $solution_provider_user_name = '';
        }
        /* setting table row information */
        $pending_solution_rows[] = array(
            $proposal_data->lab_title,
            $experiment_data->title,
            $proposal_data->name,
            $solution_provider_user_name,
            l('Edit', 'lab-migration/code-approval/approve/' . $pending_solution_data->id)
        );
    }
    /* check if there are any pending solutions */
    if (!$pending_solution_rows) {
        drupal_set_message(t('There are no pending solutions'), 'status');
        return '';
    }
    $header = array(
        'Title of the Lab',
        'Experiment',
        'Proposer',
        'Solution Provider',
        'Actions'
    );
    //$output = theme_table($header, $pending_solution_rows);
    $output = theme('table', array(
        'header' => $header,
        'rows' => $pending_solution_rows
    ));
    return $output;
}
function lab_migration_code_approval_form($form, $form_state)
{
    $solution_id = (int) arg(3);
    /* get solution details */
    //$solution_q = db_query("SELECT * FROM {lab_migration_solution} WHERE id = %d", $solution_id);
    $query = db_select('lab_migration_solution');
    $query->fields('lab_migration_solution');
    $query->condition('id', $solution_id);
    $solution_q = $query->execute();
    $solution_data = $solution_q->fetchObject();
    if (!$solution_data) {
        drupal_set_message(t('Invalid solution selected.'), 'status');
        drupal_goto('lab-migration/code-approval');
    }
    if ($solution_data->approval_status == 1) {
        drupal_set_message(t('This solution has already been approved. Are you sure you want to change the approval status?'), 'error');
    }
    if ($solution_data->approval_status == 2) {
        drupal_set_message(t('This solution has already been dis-approved. Are you sure you want to change the approval status?'), 'error');
    }
    /* get experiment data */
    //xperiment_q = db_query("SELECT * FROM {lab_migration_experiment} WHERE id = %d", $solution_data->experiment_id);
    $query = db_select('lab_migration_experiment');
    $query->fields('lab_migration_experiment');
    $query->condition('id', $solution_data->experiment_id);
    $experiment_q = $query->execute();
    $experiment_data = $experiment_q->fetchObject();
    /* get proposal data */
    //$proposal_q = db_query("SELECT * FROM {lab_migration_proposal} WHERE id = %d", $experiment_data->proposal_id);
    $query = db_select('lab_migration_proposal');
    $query->fields('lab_migration_proposal');
    $query->condition('id', $experiment_data->proposal_id);
    $proposal_q = $query->execute();
    $proposal_data = $proposal_q->fetchObject();
    /* get solution provider details */
    $solution_provider_user_name = '';
    $user_data = user_load($proposal_data->solution_provider_uid);
    if ($user_data) {
        $solution_provider_user_name = $user_data->name;
    } else {
        $solution_provider_user_name = '';
    }
    $form['#tree'] = TRUE;
    $form['lab_title'] = array(
        '#type' => 'item',
        '#markup' => $proposal_data->lab_title,
        '#title' => t('Title of the Lab')
    );
    $form['name'] = array(
        '#type' => 'item',
        '#markup' => $proposal_data->name,
        '#title' => t('Contributor Name')
    );
    $form['experiment']['number'] = array(
        '#type' => 'item',
        '#markup' => $experiment_data->number,
        '#title' => t('Experiment Number')
    );
    $form['experiment']['title'] = array(
        '#type' => 'item',
        '#markup' => $experiment_data->title,
        '#title' => t('Title of the Experiment')
    );
    $form['back_to_list'] = array(
        '#type' => 'item',
        '#markup' => l('Back to Code Approval List', 'lab-migration/code-approval')
    );
    $form['code_number'] = array(
        '#type' => 'item',
        '#markup' => $solution_data->code_number,
        '#title' => t('Code No')
    );
    $form['code_caption'] = array(
        '#type' => 'item',
        '#markup' => $solution_data->caption,
        '#title' => t('Caption')
    );
    /* get solution files */
    $solution_files_html = '';
    //$solution_files_q = db_query("SELECT * FROM {lab_migration_solution_files} WHERE solution_id = %d ORDER BY id ASC", $solution_id);
    $query = db_select('lab_migration_solution_files');
    $query->fields('lab_migration_solution_files');
    $query->condition('solution_id', $solution_id);
    $query->orderBy('id', 'ASC');
    $solution_files_q = $query->execute();
    if ($solution_files_q) {
        while ($solution_files_data = $solution_files_q->fetchObject()) {
            $code_file_type = '';
            switch ($solution_files_data->filetype) {
                case 'S':
                    $code_file_type = 'Source';
                    break;
                case 'R':
                    $code_file_type = 'Result';
                    break;
                case 'X':
                    $code_file_type = 'Xcox';
                    break;
                case 'U':
                    $code_file_type = 'Unknown';
                    break;
                default:
                    $code_file_type = 'Unknown';
                    break;
            }
            $solution_files_html .= l($solution_files_data->filename, 'lab-migration/download/file/' . $solution_files_data->id) . ' (' . $code_file_type . ')' . '<br/>';
        }
    }
    /* get dependencies files */
    //$dependency_q = db_query("SELECT * FROM {lab_migration_solution_dependency} WHERE solution_id = %d ORDER BY id ASC", $solution_id);
    $query = db_select('lab_migration_solution_dependency');
    $query->fields('lab_migration_solution_dependency');
    $query->condition('solution_id', $solution_id);
    $query->orderBy('id', 'ASC');
    $dependency_q = $query->execute();
    while ($dependency_data = $dependency_q->fetchObject()) {
        //$dependency_files_q = db_query("SELECT * FROM {lab_migration_dependency_files} WHERE id = %d", $dependency_data->dependency_id);
        $query = db_select('lab_migration_dependency_files');
        $query->fields('lab_migration_dependency_files');
        $query->condition('id', $dependency_data->dependency_id);
        $dependency_files_q = $query->execute();
        $dependency_files_data = $dependency_files_q->fetchObject();
        $solution_file_type = 'Dependency file';
        $solution_files_html .= l($dependency_files_data->filename, 'lab-migration/download/dependency/' . $dependency_files_data->id) . ' (' . 'Dependency' . ')' . '<br/>';
    }
    $form['solution_files'] = array(
        '#type' => 'item',
        '#markup' => $solution_files_html,
        '#title' => t('Solution')
    );
    $form['approved'] = array(
        '#type' => 'radios',
        '#options' => array(
            '0' => 'Pending',
            '1' => 'Approved',
            '2' => 'Dis-approved (Solution will be deleted)'
        ),
        '#title' => t('Approval'),
        '#default_value' => $solution_data->approval_status
    );
    $form['message'] = array(
        '#type' => 'textarea',
        '#title' => t('Reason for dis-approval'),
        '#attributes' => array(
            'placeholder' => t('Enter reason for dis-approval'),
            'cols' => 50,
            'rows' => 4
        ),
        '#states' => array(
            'visible' => array(
                ':input[name="approved"]' => array(
                    'value' => "2"
                )
            )
        )
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit')
    );
    $form['cancel'] = array(
        '#type' => 'markup',
        '#markup' => l(t('Cancel'), 'lab-migration/code-approval')
    );
    return $form;
}
function lab_migration_code_approval_form_validate($form, &$form_state)
{
    if ($form_state['values']['approved'] == 2) {
        if ($form_state['values']['message'] == "") {
            form_set_error('message', t('Reason for dis-approval could not be empty'));
        }
    }
}
function lab_migration_code_approval_form_submit($form, &$form_state)
{
    global $user;
    $solution_id = (int) arg(3);
    /* get solution details */
    //$solution_q = db_query("SELECT * FROM {lab_migration_solution} WHERE id = %d", $solution_id);
    $query = db_select('lab_migration_solution');
    $query->fields('lab_migration_solution');
    $query->condition('id', $solution_id);
    $solution_q = $query->execute();
    $solution_data = $solution_q->fetchObject();
    if (!$solution_data) {
        drupal_set_message(t('Invalid solution selected.'), 'status');
        drupal_goto('lab-migration/code-approval');
    }
    /* get experiment data */
    //$experiment_q = db_query("SELECT * FROM {lab_migration_experiment} WHERE id = %d", $solution_data->experiment_id);
    $query = db_select('lab_migration_experiment');
    $query->fields('lab_migration_experiment');
    $query->condition('id', $solution_data->experiment_id);
    $experiment_q = $query->execute();
    $experiment_data = $experiment_q->fetchObject();
    /* get proposal data */
    //$proposal_q = db_query("SELECT * FROM {lab_migration_proposal} WHERE id = %d", $experiment_data->proposal_id);
    $query = db_select('lab_migration_proposal');
    $query->fields('lab_migration_proposal');
    $query->condition('id', $experiment_data->proposal_id);
    $proposal_q = $query->execute();
    $proposal_data = $proposal_q->fetchObject();
    $user_data = user_load($proposal_data->uid);
    $solution_prove_user_data = user_load($proposal_data->solution_provider_uid);
    // **** TODO **** : del_lab_pdf($proposal_data->id);
    if ($form_state['values']['approved'] == "0") {
        $query = "UPDATE {lab_migration_solution} SET approval_status = 0, approver_uid = :approver_uid, approval_date = :approval_date WHERE id = :solution_id";
        $args = array(
            ":approver_uid" => $user->uid,
            ":approval_date" => time(),
            ":solution_id" => $solution_id
        );
        db_query($query, $args);
        /* sending email */
        $email_to = $user_data->mail . ', ' . variable_get('lab_migration_emails', '');
        $param['solution_pending']['solution_id'] = $solution_id;
        $param['solution_pending']['user_id'] = $user_data->uid;
        if (!drupal_mail('lab_migration', 'solution_pending', $email_to, language_default(), $param, variable_get('lab_migration_from_email', NULL), TRUE))
            drupal_set_message('Error sending email message.', 'error');
    } else if ($form_state['values']['approved'] == "1") {
        $query = "UPDATE {lab_migration_solution} SET approval_status = 1, approver_uid = :approver_uid, approval_date = :approval_date WHERE id = :solution_id";
        $args = array(
            ":approver_uid" => $user->uid,
            ":approval_date" => time(),
            ":solution_id" => $solution_id
        );
        db_query($query, $args);
        /* sending email */
        $email_to = $user_data->mail . ', ' . variable_get('lab_migration_emails', '');
        $param['solution_approved']['solution_id'] = $solution_id;
        $param['solution_approved']['user_id'] = $user_data->uid;
        if (!drupal_mail('lab_migration', 'solution_approved', $email_to, language_default(), $param, variable_get('lab_migration_from_email', NULL), TRUE))
            drupal_set_message('Error sending email message.', 'error');
    } else if ($form_state['values']['approved'] == "2") {
        if (lab_migration_delete_solution($solution_id)) {
            /* sending email */
            $email_to = $user_data->mail . ', ' . variable_get('lab_migration_emails', '');
            $param['solution_disapproved']['experiment_number'] = $experiment_data->number;
            $param['solution_disapproved']['experiment_title'] = $experiment_data->title;
            $param['solution_disapproved']['solution_number'] = $solution_data->code_number;
            $param['solution_disapproved']['solution_caption'] = $solution_data->caption;
            $param['solution_disapproved']['user_id'] = $user_data->uid;
            $param['solution_disapproved']['message'] = $form_state['values']['message'];
            if (!drupal_mail('lab_migration', 'solution_disapproved', $email_to, language_default(), $param, variable_get('lab_migration_from_email', NULL), TRUE))
                drupal_set_message('Error sending email message.', 'error');
        } else {
            drupal_set_message('Error disapproving and deleting solution. Please contact administrator.', 'error');
        }
    }
    drupal_set_message('Updated successfully.', 'status');
    drupal_goto('lab-migration/code-approval');
}