summaryrefslogtreecommitdiff
path: root/scilab_fixer.module
blob: 33235e1a7a689a3530b61b5a7ae7885f4c2f8256 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php 
    function scilab_fixer_menu() {
        $items = array();
        $items["fix/caption"] = array(
            "title"  => "Fix TBC captions",
            "page callback" => "scilab_fixer_caption_all",
            "access arguments" => array("fix scilab"),
            "type" => MENU_NORMAL_ITEM
        );
        $items["fix/aicte"] = array(
            "title"  => "Add AICTE books",
            "page callback" => "scilab_fixer_aicte_all",
            "access arguments" => array("fix scilab"),
            "weight" => 30,
            "type" => MENU_NORMAL_ITEM
        );
        $items["fix/aicte/new"] = array(
            "title"  => "Add AICTE books",
            "page callback" => "scilab_fixer_aicte_all",
            "access arguments" => array("fix scilab"),
            "weight" => 1,
            "type" => MENU_DEFAULT_LOCAL_TASK
        );
        $items["fix/aicte/edit"] = array(
            "title"  => "Edit AICTE books",
            "page callback" => "scilab_fixer_aicte_edit_all",
            "access arguments" => array("fix scilab"),
            "weight" => 2,
            "type" => MENU_LOCAL_TASK
        );       
        $items["fix/aicte/in"] = array(
            "title"  => "Mark Indian edition books",
            "page callback" => "scilab_fixer_aicte_in_all",
            "access arguments" => array("fix scilab"),
            "type" => MENU_CALLBACK
        );
        $items["fix/code"] = array(
            "title"  => "Edit TBC code",
            "page callback" => "scilab_fixer_code_all",            
            "access arguments" => array("fix scilab"),
            "type" => MENU_CALLBACK
        );
        $items["fix/ajax"] = array(
            "page callback" => "scilab_fixer_ajax",
            "access callback" => TRUE,
            "type" => MENU_CALLBACK
        );
        $items["fix/aicte/book/ajax"] = array(
            "page callback" => "scilab_fixer_aicte_ajax",
            "access callback" => TRUE,
            "type" => MENU_CALLBACK
        );        
        return $items;
    }

    function scilab_fixer_perm() {
        return array(
            "fix scilab",
        );
    }

    function scilab_fixer_caption_form($form_state) {
        $form = array();
        $form["wrapper"] = array(
            "#type" => "fieldset",
            "#title"=> "Caption change form",
            "#prefix" => "<div id='fix-tbc-form'>",
            "#suffix" => "</div>",
        );
        $form["wrapper"]["category"] = array(
            "#type" => "select",
            "#title" => t("Please select the category"),
            '#options' => array(
                0 => 'Please select a category',
                1 => 'Fluid Mechanics',
                2 => 'Control Theory & Control Systems',
                3 => 'Chemical Engineering',
                4 => 'Thermodynamics',
                5 => 'Mechanical Engineering',
                6 => 'Signal Processing',
                7 => 'Digital Communications',
                8 => 'Electrical Technology',
                9 => 'Mathematics & Pure Science',
               10 => 'Analog Electronics',
               11 => 'Digital Electronics',
               12 => 'Computer Programming',
               13 => 'Others'
            ),
        );
        $form["wrapper"]["book"] = array(
            "#type" => "select",
            "#title" => t("Please select the book."),
            "#options" => array(
                0 => "Please select a book"
            )
        );
        $form["wrapper"]["chapter"] = array(
            "#type" => "select",
            "#title" => t("Please select the chapter"),
            "#options" => array(
                0 => "Please select a chapter"
            )
        );
        $form["wrapper"]["example"] = array(
            "#type" => "select",
            "#title" => t("Please select the example"),
            "#options" => array(
                0 => "Please select a example"
            )
        );
        $form["wrapper"]["caption"] = array(
            "#type" => "textfield",
            "#title" => t("Enter new caption"),
        );
        $form["wrapper"]["submit"] = array(
            "#type" => "submit",
            "#value" => "Update"
        );
        $form["wrapper"]["code_wrapper"] = array(
            "#type" => "fieldset",
            "#description" => t("No code to display"),
            "#prefix" => "<div class='well'><pre id='edit-code' class='fix-caption-code'>",
            "#suffix" => "</pre></div>",
        );
        return $form;
    }

    function scilab_fixer_caption_all() {
        $page_content = "";
        $page_content .= "<div id='fix-tbc-page'>";
            $page_content .= "<center><span id='updating'>Updating...</span></center>";
            $page_content .= "<span id='done'>Done.</span>";
            $page_content .= drupal_get_form("scilab_fixer_caption_form");
        $page_content .= "</div>";
        $page_content .= "<small>* Selecting text from above code-area with mouse will add it to the caption textbox.</small>";
        return $page_content;
    }

    function scilab_fixer_ajax($item, $key) {
        $data = "";
        if($item == "category" && $key) {
            $query = "
                SELECT pre.id AS id, pre.book, pre.author FROM textbook_companion_preference pre
                LEFT JOIN textbook_companion_proposal pro ON pro.id = pre.proposal_id
                WHERE pro.proposal_status = 3 AND pre.approval_status = 1 AND pre.category = %d
                ORDER BY pre.book ASC
            ";
            $result = db_query($query, $key);
            
            $data .= "<option value='0'>Please select the book.</option>";
            while($row = db_fetch_object($result)) {
                $data .= "<option value='{$row->id}'>{$row->book} ({$row->author})</option>";
            }
        } else if($item == "book" && $key) {
            $query = "SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d ORDER BY number";
            $result = db_query($query, $key);
            
            $data .= "<option value='0'>Please select the chapter.</option>";
            while($row = db_fetch_object($result)) {
                $data .= "<option value='{$row->id}'>{$row->number} {$row->name}</option>";
            }
        } else if($item == "chapter" && $key) {
            $query = "SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d ORDER BY number";
            $result = db_query($query, $key);
            
            $data .= "<option value='0'>Please select the example.</option>";
            while($row = db_fetch_object($result)) {
                $data .= "<option value='{$row->id}'>{$row->number} {$row->caption}</option>";
            }
        } else if($item == "example" && $key) {
            $query = "
                SELECT * FROM textbook_companion_example_files fil
                LEFT JOIN textbook_companion_example exa ON exa.id = fil.example_id
                WHERE example_id = %d
            ";
            $result = db_query($query, $key);
            $row = db_fetch_object($result);
            /* fetching example file data */
            $uploads_dir = $_SERVER['DOCUMENT_ROOT'] . base_path() . "uploads/";
            $example_path = $uploads_dir . $row->filepath;
            $example = file_get_contents($example_path);
            $data .= "<div id='caption'>{$row->caption}</div>";
            $data .= "<div id='code'>{$example}</div>";
        } else if($item == "update") {
            $example_id = $_POST["example_id"];
            $caption = $_POST["caption"];
            $query = "
                UPDATE textbook_companion_example
                SET caption = '%s'
                WHERE id = %d
            ";
            $result = db_query($query, $caption, $example_id);
            $data .= "Updated";
        } else if($item == "code" && $key) {
            $code = $_POST["code"];
            $query = "
                SELECT * FROM textbook_companion_example_files
                WHERE example_id = %d AND filetype = 'S'
            ";
            $result = db_query($query, $key);
            $row = db_fetch_object($result);
            $uploads_dir = $_SERVER['DOCUMENT_ROOT'] . base_path() . "uploads/";
            $example_path = $uploads_dir . $row->filepath;
            file_put_contents($example_path, $code);
            $data .= "updated";
        } else if($item == "ind-ed" && $key) {
            $query = "
                UPDATE textbook_companion_aicte
                SET ind = !ind
                WHERE id = %d
            ";
            db_query($query, $key);
            $data .= "updated";
        }else {
            $data = "Nothing to display.";
        }
        echo $data;
        exit();
    }
    function scilab_fixer_aicte_ajax($item="", $key="") {
        $data = "";
        if($item == "selected") {
            $query = "
                UPDATE textbook_companion_aicte
                SET selected = !selected
                WHERE id =  {$key}
            ";
            db_query($query);
            $data = "updated";
        }
        echo $data;
        exit();
    }


    function scilab_fixer_aicte_form($form_state, $aicte_id) {
        $query = "
            SELECT * FROM textbook_companion_aicte
            WHERE id = {$aicte_id}
        ";
        $result = db_query($query);
        $row = db_fetch_object($result);
        
        $form = array();
        $form["book"] = array(
            "#type" => "textfield",
            "#title" => "Book Name",
            "#default_value" => $row->book,
            "#required" => TRUE
        );
        $form["author"] = array(
            "#type" => "textfield",
            "#title" => "Author",
            "#default_value" => $row->author,
            "#required" => TRUE
        );
        $form["category"] = array(
            "#type" => "select",
            "#title" => "Book Category",
            '#options' => array(
                0 => 'Please select a category',
                1 => 'Fluid Mechanics',
                2 => 'Control Theory & Control Systems',
                3 => 'Chemical Engineering',
                4 => 'Thermodynamics',
                5 => 'Mechanical Engineering',
                6 => 'Signal Processing',
                7 => 'Digital Communications',
                8 => 'Electrical Technology',
                9 => 'Mathematics & Pure Science',
               10 => 'Analog Electronics',
               11 => 'Digital Electronics',
               12 => 'Computer Programming',
               13 => 'Others'
            ),
            "#default_value" => $row->category,
            "#required" => TRUE
        );
        $form["isbn"] = array(
            "#type" => "textfield",
            "#title" => "ISBN",
            "#default_value" => $row->isbn,
            "#required" => FALSE
        );
        $form["publisher"] = array(
            "#type" => "textfield",
            "#title" => "Publisher",
            "#default_value" => $row->publisher,
            "#required" => TRUE
        );
        $form["edition"] = array(
            "#type" => "textfield",
            "#title" => "Edition",
            "#default_value" => $row->edition,
            "#required" => TRUE
        );
        $form["year"] = array(
            "#type" => "textfield",
            "#title" => "Year of publication",
            "#default_value" => $row->year,
            "#required" => TRUE
        );
        $form["aicte_id"] = array(
            "#type" => "hidden",
            "#value" => $row->id
        );
        $form["submit"] = array(
            "#type" => "submit",
            "#value" => "Submit"
        );
        return $form;
    }

    function scilab_fixer_aicte_form_validate($form, &$form_state) {
        if(!$form_state["values"]["category"]) {
            form_set_error("category", "Please select a category.");
        }
        if(!is_numeric($form_state["values"]["edition"])) {
            form_set_error("edition", "Only digits are allowed.");
        }
        if(!is_numeric($form_state["values"]["year"]) && strlen($form["values"]["year"]) != 4) {
            form_set_error("year", "Please enter a valid year. eg: 2011.");
        }
    }

    function scilab_fixer_aicte_form_submit($form, &$form_state) {
        $v = $form_state["values"];
        if($v["aicte_id"]) {
            $query = "
                UPDATE textbook_companion_aicte
                SET book = '%s', author = '%s', category = %d,
                isbn = '%s', publisher = '%s', edition = %d,
                year = %d 
                WHERE id = %d
            ";
            $result = db_query($query, 
                $v["book"], $v["author"], $v["category"], $v["isbn"],
                $v["publisher"], $v["edition"], $v["year"], $v["aicte_id"]
            );
            drupal_set_message("Book updated successfully", "status");
        } else {
            $query = "
                INSERT INTO textbook_companion_aicte
                (book, author, category, isbn, publisher, edition, year)
                VALUES
                ('%s', '%s', %d, '%s', '%s', %d, %d)
                ";
            $result = db_query($query, 
                $v["book"], $v["author"], $v["category"], $v["isbn"],
                $v["publisher"], $v["edition"], $v["year"]
            );
            drupal_set_message("Book added successfully", "status");
        }
    }

    function scilab_fixer_aicte_all() {
        $page_content = "";
        $page_content .= drupal_get_form("scilab_fixer_aicte_form");
        return $page_content;
    }

    function scilab_fixer_aicte_edit_all($aicte_id=0) {
        $page_content = "";         
        if($aicte_id) {
            $page_content .= drupal_get_form("scilab_fixer_aicte_form", $aicte_id);
        } else {
            $query = "
                SELECT * FROM textbook_companion_aicte
                ORDER BY time DESC
            ";
            $result = db_query($query);
            $headers = array(
                "Book", "Author",
                "Edition", "Action",
            );
            $rows = array();
            
            while($row = db_fetch_object($result)) {
                $item = array(                    
                    "{$row->book}",
                    "{$row->author}",
                    "{$row->edition}",
                    l(t("Edit"), "fix/aicte/edit/{$row->id}")                    
                );
                if($row->selected) {
                    $check = "<input class='selected' type='checkbox' data-bid='{$row->id}' checked>";
                } else {
                    $check = "<input class='selected' type='checkbox' data-bid='{$row->id}'>";
                }                
                array_push($item, $check);
                array_push($rows, $item);
                }
            $page_content .= theme("table", $headers, $rows);
        }
        return $page_content;
    }

    function scilab_fixer_aicte_in_all(){
        $page_content = "";        
        $query = "
            SELECT * FROM textbook_companion_aicte
        ";
        $result = db_query($query);
        $headers = array(
            "Book", "Publisher", "Action"
        );
        $rows = array();
        while($row = db_fetch_object($result)) {
            $item = array(
                "data" => array(
                    "{$row->book}<br>by{$row->author}",
                    $row->publisher,
                ),
            );
            $ind_options = array(
                /* # linking in drupal l() */
                "fragment" => " ",
                "external" => TRUE,
                "attributes" => array(
                    "class" => "ind-ed",
                    "data-aicte" => "{$row->id}",
                )
            );
            /* ind-ed link */
            if($row->ind) {
                array_push($item["data"], l("Unmark", "", $ind_options));
            } else {
                array_push($item["data"], l("Mark", "", $ind_options));
            }
            if($row->ind) {
                $item["class"] .= " orange";
            }
            array_push($rows, $item);
        }
        $page_content .= theme("table", $headers, $rows);
        return $page_content;
    }

    function scilab_fixer_code_form($form_state) {
        $form = array();
        $form["wrapper"] = array(
            "#type" => "fieldset",
            "#title"=> "Code edit form",
            "#prefix" => "<div id='fix-tbc-form'>",
            "#suffix" => "</div>",
        );
        $form["wrapper"]["category"] = array(
            "#type" => "select",
            "#title" => t("Please select the category"),
            '#options' => array(
                0 => 'Please select a category',
                1 => 'Fluid Mechanics',
                2 => 'Control Theory & Control Systems',
                3 => 'Chemical Engineering',
                4 => 'Thermodynamics',
                5 => 'Mechanical Engineering',
                6 => 'Signal Processing',
                7 => 'Digital Communications',
                8 => 'Electrical Technology',
                9 => 'Mathematics & Pure Science',
               10 => 'Analog Electronics',
               11 => 'Digital Electronics',
               12 => 'Computer Programming',
               13 => 'Others'
            ),
        );
        $form["wrapper"]["book"] = array(
            "#type" => "select",
            "#title" => t("Please select the book."),
            "#options" => array(
                0 => "Please select a book"
            )
        );
        $form["wrapper"]["chapter"] = array(
            "#type" => "select",
            "#title" => t("Please select the chapter"),
            "#options" => array(
                0 => "Please select a chapter"
            )
        );
        $form["wrapper"]["example"] = array(
            "#type" => "select",
            "#title" => t("Please select the example"),
            "#options" => array(
                0 => "Please select a example"
            )
        );
        $form["wrapper"]["code"] = array(
            "#type" => "textarea",
            "#title" => t("Code Editor"),
        );
        $form["wrapper"]["submit"] = array(
            "#type" => "submit",
            "#value" => "Update"
        );
        return $form;
    }

    function scilab_fixer_code_all() {
        $page_content = "";
        $page_content .= "<div id='fix-tbc-page'>";
            $page_content .= "<center><span id='updating'>Updating...</span></center>";
            $page_content .= "<span id='done'>Done.</span>";
            $page_content .= drupal_get_form("scilab_fixer_code_form");
        $page_content .= "</div>";
        return $page_content;
    }

    function scilab_fixer_init() {
        drupal_add_css(drupal_get_path("module", "scilab_fixer") . "/css/scilab_fixer.css");
        drupal_add_js(drupal_get_path("module", "scilab_fixer") . "/js/scilab_fixer.js");
        drupal_add_js(drupal_get_path("module", "scilab_fixer") . "/js/selection.js");
    }
?>