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
|
<?php
require_once('r_tbc_fixer_email.inc');
include('r_tbc_fixer_edit_book_category.inc');
function r_tbc_fixer_menu()
{
$items = array();
$items["r_tbc_fixer/caption"] = array(
"title" => "Edit TBC captions",
"page callback" => "R_fixer_caption_all",
"access arguments" => array(
"fix R_code_caption"
),
"type" => MENU_NORMAL_ITEM
);
$items["r_tbc_fixer/code"] = array(
"title" => "Edit TBC code",
"page callback" => "R_fixer_code_all",
"access arguments" => array(
"fix R"
),
"type" => MENU_NORMAL_ITEM
);
$items["r_tbc_fixer/ajax"] = array(
"page callback" => "R_fixer_ajax",
"access callback" => TRUE,
"type" => MENU_CALLBACK
);
$items["r_tbc_fixer/ajax/edit-book-category"] = array(
"page callback" => "r_tbc_fixer_edit_book_category_ajax",
"access callback" => TRUE,
"type" => MENU_CALLBACK,
"file" => "r_tbc_fixer_edit_book_category.inc"
);
/* for admin */
$items['admin/settings/r_tbc_fixer_settings'] = array(
'title' => 'textbook companion fixer Settings',
'description' => 'Textbook Companion Fixer Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'r_tbc_fixer_settings_form'
),
'access arguments' => array(
'administer textbook companion fixer settings'
),
'type' => MENU_NORMAL_ITEM,
'file' => 'r_tbc_fixer_settings.inc'
);
// edit book categoery
$items["r_tbc_fixer/edit_book_category"] = array(
"title" => "Edit Books Category",
"page callback" => "r_tbc_fixer_edit_book_proposal_all",
"access arguments" => array(
"fix R_textbook_category"
),
"type" => MENU_NORMAL_ITEM,
'file' => 'r_tbc_fixer_edit_book_category.inc'
);
$items['r_tbc_fixer/category_edit'] = array(
'title' => 'Categorize',
'description' => 'Edit Completed Books Category',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'r_tbc_fixer_category_edit_form'
),
'access arguments' => array(
'fix R_textbook_category'
),
'type' => MENU_NORMAL_ITEM,
'file' => 'r_tbc_fixer_edit_book_category.inc'
);
return $items;
}
function r_tbc_fixer_permission()
{
return array(
"fix R" => array(
"title" => t("fix R code"),
'restrict access' => TRUE
),
"fix R_code_caption" => array(
"title" => t("fix R code caption"),
'restrict access' => TRUE
),
"administer textbook companion fixer settings" => array(
"title" => t("administer textbook companion fixer settings"),
'restrict access' => TRUE
),
"fix R_textbook_category" => array(
"title" => t("fix R textbook category"),
'restrict access' => TRUE
)
);
}
function R_fixer_caption_form($form, &$form_state)
{
$form = array();
$form["wrapper"] = array(
"#type" => "fieldset",
"#title" => "Caption change form",
"#prefix" => "<div id='fix-tbc-form'>",
"#suffix" => "</div>"
);
$form["wrapper"]["book"] = array(
"#type" => "select",
"#title" => t("Please select the book."),
"#options" => r_get_tbc_books(),
);
$form["wrapper"]["chapter"] = array(
"#type" => "select",
"#title" => t("Please select the chapter"),
"#options" => array(
0 => "Please select a chapter"
),
"#prefix" => "<div class='select-chapter'>",
"#suffix" => "</div>"
);
$form["wrapper"]["chapter_name"] = array(
"#type" => "textfield",
"#title" => t("Enter new chapter name"),
"#size" => 255,
"#maxlength" => 255,
"#attributes" => array(
"Style" => "width:100%"
),
"#prefix" => "<div class='enter-chapter-name'>",
"#suffix" => "</div>"
);
$form["wrapper"]["example"] = array(
"#type" => "select",
"#title" => t("Please select the example"),
"#description" => t("<span style='color:red'>*Double click on example caption you want to edit</span>"),
"#options" => array(
0 => "Please select a example"
),
"#prefix" => "<div class='select-example'>",
"#suffix" => "</div>"
);
$form["wrapper"]["caption"] = array(
"#type" => "textfield",
"#title" => t("Enter new caption"),
"#attributes" => array(
"Style" => "width:100%"
),
"#size" => 255,
"#maxlength" => 255,
"#prefix" => "<div class='enter-caption'>",
"#suffix" => "</div>"
);
$form["wrapper"]["chapter_example"] = array(
"#markup" => "<input type='checkbox' name='chapterchk' value='chapter' class='chapter-caption-chk'>
Update Chapter caption</input><br>
<input type='checkbox' name='examplechk' value='example' class='example-caption-chk'>
Update Example caption</input>",
"#prefix" => "<div class='chapter-example-chk'>",
"#suffix" => "</div>"
);
$form["wrapper"]["submit"] = array(
"#type" => "submit",
"#value" => "Update",
"#prefix" => "<div class='update-button'>",
"#suffix" => "</div>"
);
$form["wrapper"]["code_wrapper"] = array(
"#type" => "fieldset",
"#description" => t("No code to display"),
"#attributes" => array(
"onclick" => "return check();"
),
"#prefix" => "<div class='well'><pre id='edit-code' class='fix-caption-code'>",
"#suffix" => "</pre></div>"
);
return $form;
}
function R_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>";
$R_fixer_caption_form = drupal_get_form("R_fixer_caption_form");
$page_content .= drupal_render($R_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 R_fixer_ajax($item, $key)
{
$data = "";
global $user;
if ($item == "book" && $key) {
/*$query = "SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d ORDER BY number";
$result = db_query($query, $key);*/
$query = db_select('textbook_companion_chapter');
$query->fields('textbook_companion_chapter');
$query->condition('preference_id', $key);
$query->orderBy('number', 'ASC');
$result = $query->execute();
$data .= "<option value='0'>Please select the chapter.</option>";
while ($row = $result->fetchObject()) {
$data .= "<option value='{$row->id}' data-chapterid='{$row->id}' data-chaptername='{$row->name}'>{$row->number}) {$row->name}</option>";
} //$row = $result->fetchObject()
} //$item == "book" && $key
else if ($item == "chapter" && $key) {
/*$query = "SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d ORDER BY number";
$result = db_query($query, $key);*/
$query = db_select('textbook_companion_example');
$query->fields('textbook_companion_example');
$query->condition('chapter_id', $key);
$query->orderBy('number', 'ASC');
$result = $query->execute();
$data .= "<option value='0'>Please select the example.</option>";
while ($row = $result->fetchObject()) {
$data .= "<option value='{$row->id}' data-exampleid='{$row->caption}'>{$row->number} {$row->caption}</option>";
} //$row = $result->fetchObject()
} //$item == "chapter" && $key
else if ($item == "example" && $key) {
$result = db_query("select tcp.directory_name, tce.caption, tcef.filepath from textbook_companion_preference tcp join textbook_companion_chapter tcc on tcp.id=tcc.preference_id join textbook_companion_example tce ON tcc.id=tce.chapter_id join textbook_companion_example_files tcef on tce.id=tcef.example_id where tcef.filetype= 'S' AND tcef.example_id= :example_id", array(
':example_id' => $key
));
$row = $result->fetchObject();
/* fetching example file data */
$uploads_dir = $_SERVER['DOCUMENT_ROOT'] . base_path() . "r_uploads/tbc_uploads/";
$example_path = $uploads_dir. $row->directory_name. '/'. $row->filepath;
$example = file_get_contents($example_path);
$data .= "<div id='caption'>{$row->caption}</div>";
$data .= "<div id='code'>{$example}</div>";
} //$item == "example" && $key
else if ($item == "update-example") {
$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);*/
$query = db_update('textbook_companion_example');
$query->fields(array(
'caption' => $caption
));
$query->condition('id', $example_id);
$result = $query->execute();
$data .= "Updated";
/* sending email */
$email_to = $user->mail;
$from = variable_get('textbook_companion_from_email', '');
$bcc = variable_get('r_tbc_fixer_bcc_emails', '');
$cc = variable_get('r_tbc_fixer_cc_emails', '');
$params['example_updated']['example_id'] = $example_id;
$params['example_updated']['user_id'] = $user->uid;
$params['example_updated']['headers'] = array(
'From' => $from,
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal',
'Cc' => $cc,
'Bcc' => $bcc
);
if (!drupal_mail('r_tbc_fixer', 'example_updated', $email_to, language_default(), $params, $from, TRUE)) {
$data .= 'Error sending email message.';
} //!drupal_mail('r_tbc_fixer', 'example_updated', $email_to, language_default(), $params, $from, TRUE)
} //$item == "update-example"
else if ($item == "update-chapter") {
$chapter_id = $_POST["chapter_id"];
$chapter_caption = $_POST["chapter_caption"];
$query_chapter = db_update('textbook_companion_chapter');
$query_chapter->fields(array(
'name' => $chapter_caption
));
$query_chapter->condition('id', $chapter_id);
$result_chapter = $query_chapter->execute();
$data .= "Updated";
/* sending email */
$email_to = $user->mail;
$from = variable_get('textbook_companion_from_email', '');
$bcc = variable_get('r_tbc_fixer_bcc_emails', '');
$cc = variable_get('r_tbc_fixer_cc_emails', '');
$params['chapter_updated']['chapter_id'] = $chapter_id;
$params['chapter_updated']['user_id'] = $user->uid;
$params['chapter_updated']['headers'] = array(
'From' => $from,
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal',
'Cc' => $cc,
'Bcc' => $bcc
);
if (!drupal_mail('r_tbc_fixer', 'chapter_updated', $email_to, language_default(), $params, $from, TRUE)) {
$data .= 'Error sending email message.';
} //!drupal_mail('r_tbc_fixer', 'chapter_updated', $email_to, language_default(), $params, $from, TRUE)
} //$item == "update-chapter"
else if ($item == "update-both") {
$example_id = $_POST["example_id"];
$caption = $_POST["caption"];
$chapter_id = $_POST["chapter_id"];
$chapter_caption = $_POST["chapter_caption"];
$query_exmaple = db_update('textbook_companion_example');
$query_exmaple->fields(array(
'caption' => $caption
));
$query_exmaple->condition('id', $example_id);
$result_example = $query_exmaple->execute();
$query_chapter = db_update('textbook_companion_chapter');
$query_chapter->fields(array(
'name' => $chapter_caption
));
$query_chapter->condition('id', $chapter_id);
$result_chapter = $query_chapter->execute();
$data .= "Updated";
/* sending email */
$email_to = $user->mail;
$from = variable_get('textbook_companion_from_email', '');
$bcc = variable_get('r_tbc_fixer_bcc_emails', '');
$cc = variable_get('r_tbc_fixer_cc_emails', '');
$params['example_updated']['example_id'] = $example_id;
$params['example_updated']['user_id'] = $user->uid;
$params['example_updated']['headers'] = array(
'From' => $from,
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal',
'Cc' => $cc,
'Bcc' => $bcc
);
if (!drupal_mail('r_tbc_fixer', 'chapter_example_updated', $email_to, language_default(), $params, $from, TRUE)) {
$data .= 'Error sending email message.';
} //!drupal_mail('r_tbc_fixer', 'chapter_example_updated', $email_to, language_default(), $params, $from, TRUE)
} //$item == "update"
else if ($item == "code" && $key) {
$code = $_POST["code"];
$result = db_query("select * from textbook_companion_preference tcp join textbook_companion_chapter tcc on tcp.id=tcc.preference_id join textbook_companion_example tce ON tcc.id=tce.chapter_id join textbook_companion_example_files tcef on tce.id=tcef.example_id where tcef.filetype= 'S' AND tcef.example_id= :example_id", array(
':example_id' => $key
));
$row = $result->fetchObject();
$uploads_dir = $_SERVER['DOCUMENT_ROOT'] . base_path() . "r_uploads/tbc_uploads/";
$example_path = $uploads_dir. $row->directory_name. '/'. $row->filepath;
file_put_contents($example_path, $code);
$data .= "updated";
} //$item == "code" && $key
else {
$data = "<option value='0'>Please select a book</option>";
}
echo $data;
exit();
}
function R_fixer_code_form($form, &$form_state)
{
$form = array();
$form["code_edit"] = array(
"#type" => "fieldset",
"#title" => "Code edit form",
"#prefix" => "<div id='fix-tbc-form'>",
"#suffix" => "</div>"
);
$form["code_edit"]["book"] = array(
"#type" => "select",
"#title" => t("Please select the book."),
"#options" => r_get_tbc_books(),
);
$form["code_edit"]["chapter"] = array(
"#type" => "select",
"#title" => t("Please select the chapter"),
"#options" => array(
0 => "Please select a chapter"
),
"#prefix" => "<div class='select-chapter'>",
"#suffix" => "</div>"
);
$form["code_edit"]["example"] = array(
"#type" => "select",
"#title" => t("Please select the example"),
"#options" => array(
0 => "Please select a example"
),
"#prefix" => "<div class='select-example'>",
"#suffix" => "</div>"
);
$form["code_edit"]["code"] = array(
"#type" => "textarea",
"#title" => t("Code Editor"),
'#resizable' => FALSE,
"#prefix" => "<div class='example-code-edit'>",
"#suffix" => "</div>"
);
$form["code_edit"]["submit"] = array(
"#type" => "submit",
"#value" => "Update",
"#prefix" => "<div class='update-button'>",
"#suffix" => "</div>"
);
return $form;
}
function R_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>";
$R_fixer_code_form = drupal_get_form("R_fixer_code_form");
$page_content .= drupal_render($R_fixer_code_form);
$page_content .= "</div>";
return $page_content;
}
function r_get_tbc_books(){
$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
ORDER BY pre.book ASC
";
$book_list = db_query($query);
$i = 1;
$book[0] = "Select any book";
while ($book_list_data = $book_list->fetchObject())
{
if (!isset($book[$book_list_data->id])) {
$book[$book_list_data->id] = '';
}
$book[$book_list_data->id] = $book[$book_list_data->id] . $i . ") ". $book_list_data->book . " (by " . $book_list_data->author . ")";
$i++;
}
return $book;
}
function r_tbc_fixer_init()
{
drupal_add_css(drupal_get_path("module", "r_tbc_fixer") . "/css/r_tbc_fixer.css");
drupal_add_js(drupal_get_path("module", "r_tbc_fixer") . "/js/r_tbc_fixer.js");
drupal_add_js(drupal_get_path("module", "r_tbc_fixer") . "/js/r_tbc_fixer_edit_category.js");
drupal_add_js(drupal_get_path("module", "r_tbc_fixer") . "/js/selection.js");
}
|