summaryrefslogtreecommitdiff
path: root/job_portal.module
blob: afdd43e67f8a28193d62218da81688c8253ac90c (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php 
    function job_portal_permission() {
        return array(
            "access job_portal" => array(
                "title" => t("Access Job Portal"),
                "description" => t("Allows users to view job postings.")
            ),
        );
    }

    function job_portal_menu() {
        $items = array();
        $items["jobs"] = array(
            "title" => "FOSSEE Job Portal",
            "page callback" => "job_portal_page",
            "access arguments" => array("access job_portal"),
            "type" => MENU_CALLBACK,
        );
        $items["jobs/apply"] = array(
            "title" => "FOSSEE Job Portal - Application Form",
            "page callback" => "job_portal_application_page",
            "access arguments" => array("access job_portal"),
            "type" => MENU_CALLBACK,
        );
        $items["jobs/view-applications"] = array(
            "title" => "FOSSEE Job Portal - View Applications",
            "page callback" => "job_portal_view_application_page",
            "access arguments" => array("access job_portal"),
            "type" => MENU_CALLBACK,
        );
        return $items;
    }

    function job_portal_application_form($form, &$form_state) {
        $form = array();
        $form["wrapper"] = array(
            "#type" => "fieldset",
            "#title" => t("Application form"),
            "#collapsible" => TRUE,
        );
        $form["wrapper"]["position"] = array(
            "#type" => "select",
            "#title" => t("Job Position"),
            "#description" => t("Select the position for which you are applying."),
            "#options" => get_jobs_available("options"),
            "#empty_option" => t("--- Select a position ---"),
            "#required" => TRUE,
        );
        $form["wrapper"]["name"] = array(
            "#type" => "textfield",
            "#title" => t("Name"),
            "#description" => t("Please enter your full name."),
            "#required" => TRUE,
        );
        $form["wrapper"]["contact"] = array(
            "#type" => "textfield",
            "#title" => t("Contact Number"),
            "#description" => t("Please enter your contact number."),
            "#required" => TRUE,
        );
        $form["wrapper"]["email"] = array(
            "#type" => "textfield",
            "#title" => t("Email"),
            "#description" => t("Please enter your e-mail id."),
            "#required" => TRUE,
        );
        $form["wrapper"]["resume"] = array(
            "#type" => "file",
            "#title" => t("Resume" . required_star()),
            "#description" => t("Please upload your resume [pdf]."),
        );
        $form["wrapper"]["submit"] = array(
            "#type" => "submit",
            "#value" => "Submit Application"
        );
        return $form;
    }

    function job_portal_application_form_validate($form, &$form_state) {
        $file = file_save_upload('resume', array(
            // Validate extensions.
            'file_validate_extensions' => array('pdf'),
        ));
        // If the file passed validation:
        if ($file) {
            // Move the file into the Drupal file system.
            if ($file = file_move($file, 'public://')) {
              // Save the file for use in the submit handler.
              $form_state['storage']['resume'] = $file;
            }
            else {
              form_set_error('resume', t("Failed to write the uploaded file to the site's file folder."));
            }
        }
        else {
            form_set_error('resume', t('No file was uploaded.'));
        }
        
        /* validating email field */
        if(!valid_email_address($form_state["values"]["email"])) {
            form_set_error('email', t('Please enter an valid email address.'));
        }
    }

    function job_portal_application_form_submit($form, &$form_state) {
        $v = $form_state["values"];
        $resume = $form_state["storage"]["resume"];
        $query = "
            INSERT INTO job_portal_applications
            (name, contact, email, resume, position_id)
            VALUES
            (:name, :contact, :email, :resume, :position_id)
        ";
        $args = array(
            ":name" => $v["name"], 
            ":contact" => $v["contact"],
            ":email" => $v["email"], 
            ":resume" => $resume->filename,
            ":position_id" => $v["position"]
        );
        /* storing the row id in $result */
        $result = db_query($query, $args, array('return' => Database::RETURN_INSERT_ID));
        
        /* moving the file to uploads/resumes */
        $base_path = $_SERVER['DOCUMENT_ROOT'] . base_path();
        $uploads_dir = $base_path  . "uploads/resumes";
        if(!file_exists($uploads_dir . "/{$result}/")) {
            mkdir($uploads_dir . "/{$result}/", 0755, TRUE);
        }
        file_unmanaged_move($resume->uri, $uploads_dir . "/{$result}/{$resume->filename}");
        
        $emails = db_select("job_portal_positions")
            ->fields("job_portal_positions", array("notify"))
            ->condition("id", $v["position"])
            ->execute()->fetchObject();
        $emails = $emails->notify;
        $position = get_jobs_available("options");
        $position = $position[$v["position"]];
        
        /* preparing to send mail */
        $from = "jobs@fossee.in";
        $to = "jobs@fossee.in";
        $cc = $emails;
        $bcc = "rush2jrp@gmail.com";
        $subject = "Job Application - Python Developer";
        $message = "
            <p><strong>{$v['name']}</strong> has applied for 
            the post of <strong>{$position}</strong>.</p>
            <table border='1' width='740px'>
                <tr><td>Name</td><td>{$v['name']}</td></tr>
                <tr><td>Contact</td><td>{$v['contact']}</td></tr>
                <tr><td>Email</td><td>{$v['email']}</td></tr>
            </table>
            <p><em>Please check the resume attached to this mail.</em></p>
        ";
        $file = $uploads_dir . "/{$result}/" . $resume->filename;
        $mail_status = send_mail_attachment($from, $to, $cc, $bcc, $subject, $message, $file);
        if(!$mail_status) {
            drupal_set_message("An error occurred while sending mail.", "error");
        } else {
            drupal_set_message("We have received your application. Thank you!", "status");
        }
        file_delete($resume);
        unset($form["storage"]["resume"]);
    }

    function job_portal_page() {
        $result = get_jobs_available();
        $collapser_header = "Job Details";
        $collapser_content = "";
        foreach($result as $row) {
            $collapser_content .= "
                <strong>Position</strong>: {$row->position}<br>
                <strong>Specialization</strong>: {$row->specialization}<br>
                <strong>Salary</strong>: {$row->salary}<br>
                <strong>Qualification</strong>: {$row->qualification}
                <strong>Skills</strong>: {$row->skills}
                <strong>Job Responsibilities</strong>: {$row->responsibilities}
                <strong>Location</strong>: {$row->location}<br><br>
            ";
            $collapser_content .= l("Apply Now", "jobs/apply" , array("attributes" => array("class" => "btn btn-success")));
        }
        $description = collapser($collapser_header, $collapser_content);
        
        $output = array(
            "job_description" => array(
                "#prefix" => "<div id='job-description'>",
                "#markup" => $description,
                "#suffix" => "</div>",
            ),
        );
        return $output;
    }
    
    function job_portal_application_page() {
        $output = array(
            "application_form" => array(
                "#prefix" => "<div id='job-application'>",
                "#markup" => drupal_render(drupal_get_form("job_portal_application_form")),
                "#suffix" => "</div>",
            ),
        );
        return $output;
    }

    function job_portal_view_application_page($position_id=0) {
        $markup = "";
        if($position_id) {
            $result = db_select("job_portal_applications")
                ->fields("job_portal_applications")
                ->condition("position_id", $position_id)
                ->execute()->fetchAll();
            $headers = array(
                "#", "Name", "Time", "Download"
            );
            $rows = array();
            $i = 1;
            foreach($result as $row) {
                $item = array(
                    $i,
                    $row->name,
                    $row->time,
                    l("{$row->resume}", "uploads/resumes/{$row->id}/{$row->resume}")
                );
                array_push($rows, $item);
                $i++;
            }
            $job = db_select("job_portal_positions")
                ->fields("job_portal_positions")
                ->condition("id", $position_id)
                ->execute()->fetchObject();
            $markup .= l("<< Back to the list of open positions", "jobs/view-applications");
            $markup .= "<h5><u>{$job->position} ({$job->specialization}) - List of applications</u></h5>";
            $markup .= bootstrap_table($headers, $rows);
        } else {
            /* List all the job positions.
             * Change the condition later based on end date.
            */
            $result = db_select("job_portal_positions")
                ->fields("job_portal_positions")
                ->condition("status", 1)
                ->execute()->fetchAll();
            $headers = array(
                "#", "Position", "Time", "Action",
            );
            $rows = array();
            foreach($result as $row) {
                $item = array(
                    $row->id,
                    $row->position,
                    $row->time,
                    l("View applications", "jobs/view-applications/{$row->id}")
                );
                array_push($rows, $item);
            }
            $markup .= "<h5><u>List of job openings</u></h5>";
            $markup .= bootstrap_table($headers, $rows);
        }
        $output = array(
            "positions_list" => array(
                "#prefix" => "<div id='positions-list'>",
                "#markup" => $markup,
                "#suffix" => "</div>",
            ),
        );
        return $output;
    }

    /* helper functions */
    function collapser($title="", $content="", $description="") {
        return "
            <fieldset id='fieldset-id' class='collapsible'>
                <legend><span class='fieldset-legend'>{$title}</span></legend>
                <div class='fieldset-wrapper'>
                    <div class='fieldset-description'>{$description}</div>
                    {$content}
                </div>
            </fieldset>
        ";
    }

    function required_star() {
        return "<span class='form-required' title='This field is required.'> *</span>";
    }

    function send_mail_attachment($from, $to, $cc, $bcc, $subject, $message, $file) {
        // $file should include path and filename
        $filename = basename($file);
        $file_size = filesize($file);
        $content = chunk_split(base64_encode(file_get_contents($file))); 
        $uid = md5(uniqid(time()));
        $from = str_replace(array("\r", "\n"), '', $from); // to prevent email injection
        $header = "From: ".$from."\r\n"
            ."Cc: ".$cc."\r\n"
            ."Bcc: ".$bcc."\r\n"
            ."MIME-Version: 1.0\r\n"
            ."Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"
            ."This is a multi-part message in MIME format.\r\n" 
            ."--".$uid."\r\n"
            ."Content-type: text/html; charset=UTF-8; format=flowed\r\n"
            ."Content-Transfer-Encoding: 7bit\r\n\r\n"
            .$message."\r\n\r\n"
            ."--".$uid."\r\n"
            ."Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"
            ."Content-Transfer-Encoding: base64\r\n"
            ."Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"
            .$content."\r\n\r\n"
            ."--".$uid."--"; 
        return mail($to, $subject, "", $header);
    }

    function get_jobs_available($key="") {
        $result = db_select("job_portal_positions")
            ->fields("job_portal_positions")
            ->condition("status", 1)
            ->execute()->fetchAll();
        if($key == "options") {
            $options = array();
            foreach($result as $row) {
                $options[$row->id] = "{$row->position} ({$row->specialization})";
            }
            return $options;
        } else {
            return $result;
        }
    }

    function bootstrap_table($headers, $rows) {
        $thead = "";
        $tbody = "";
        foreach($headers as $header) {
            $thead .= "<th>{$header}</th>";
        }
        foreach($rows as $row) {
            $tbody .= "<tr>";
            foreach($row as $data) {
                $tbody .= "<td>{$data}</td>";
            }
            $tbody .= "</tr>";
        }
        $table = "
            <table class='table table-bordered table-hover'>
                <thead>{$thead}</thead>
                <tbody>{$tbody}</tbody>
            </table>
        ";
        return $table;
    }
?>