-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.php
executable file
·2678 lines (2229 loc) · 93.4 KB
/
lib.php
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?PHP // $Id: lib.php,v 1.19 2005/08/27 16:20:31 skodak Exp $
define('NUM_NONE', '0');
define('NUM_NUMBERS', '1');
define('NUM_BULLETS', '2');
define('NUM_INDENTED', '3');
require_once($CFG->libdir.'/filelib.php');
// $db->debug = true;
$NUMBERING_TYPE = array (NUM_NONE => get_string('numbering0', 'hiperbook'),
NUM_NUMBERS => get_string('numbering1', 'hiperbook'),
NUM_BULLETS => get_string('numbering2', 'hiperbook'),
NUM_INDENTED => get_string('numbering3', 'hiperbook') );
if (!isset($CFG->hiperbook_tocwidth)) {
set_config("hiperbook_tocwidth", 180); // default toc width
}
/// Library of functions and constants for module 'book'
function hiperbook_add_instance($book) {
/// Given an object containing all the necessary data,
/// (defined by the form in mod.html) this function
/// will create a new instance and return the id number
/// of the new instance.
global $CFG;
$book->timecreated = time();
$book->timemodified = $book->timecreated;
$book->template_hw = '';
$book->template_tips = '';
$book->template_suggs = '';
$book->img_hotwords_top = 'title_hotwords.png';
$book->img_tips_top = 'title_tips.png';
$book->img_suggestions_top= 'title_suggestions.png';
$book->img_links_top= 'title_links.png';
$book->img_hotwords_icon = "icone_glossario.png";
$book->img_tips_icon = "icone_dicas.png";
$book->img_suggestions_icon = "icone_sugestoes_de_estudo.png";
$book->img_links_icon = "icone_links.png";
$book->img_top_popup = 'top.png';
$book->img_page_next = 'next.png';
$book->img_page_prev = 'back.png';
$book->img_separador_toc = 'separador_toc.png';
$book->img_navpath_active_start = 'navpath_current_start.png';
$book->img_navpath_active_middle = 'navpath_current.png';
$book->img_navpath_active_end = 'navpath_current_end.png';
$book->img_navpath_inactive_start = 'navpath_past_start.png';
$book->img_navpath_inactive_middle= 'navpath_past.png';
$book->img_navpath_inactive_end = 'navpath_past_end.png';
$bookid= insert_record('hiperbook', $book);
$book->id = $bookid;
$book->template_css = "
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
#mod-hiperbook-hotword #page{
margin-top:0px;
padding-top:0px;
background-color:white;
}
#mod-hiperbook-popup #page{
margin-top:0px;
padding-top:0px;
background-color:white;
}
#mod-hiperbook-tips #page #header,
#mod-hiperbook-suggestions #page #header,
#mod-hiperbook-hotwords #page #header {
display:none;
}
#mod-hiperbook-popup #bookname{
position:absolute;
top:25px;
left:5px;
font-weight:bold;
font-style:italic;
font-size:14px;
text-align:left;
}
.mod-hiperbook #navpaths{
margin-top:10px;
margin-left:10px;
display:block;
}
.mod-hiperbook #navpaths #current{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:13px;
color:white;
height:22px;
}
.mod-hiperbook #navpaths #current #main{
background-color:#233E54;
display:inline;
text-align:center;
padding:2px;
padding-bottom:3px;
font-weight:bold;
}
.mod-hiperbook #navpaths #current #start{
background-position:center;
background-repeat:no-repeat;
display:inline;
padding:2px;
padding-bottom:3px;
}
.mod-hiperbook #navpaths #current #end{
background-position:center;
background-repeat:no-repeat;
display:inline;
padding:2px;
padding-bottom:3px;
}
.mod-hiperbook #navpaths #past{
}
.mod-hiperbook #navpaths #past #main{
background-position:center;
background-repeat:repeat-x;
display:inline;
text-align:center;
padding:4px;
}
.mod-hiperbook #navpaths #past #main a{
font-family:Verdana, Arial, Helvetica, sans-serif;
text-decoration:none;
font-size:13px;
color:#8FB4DF;
background-position:center;
}
.mod-hiperbook #navpaths #past #start{
background-position:center;
background-repeat:no-repeat;
display:inline;
padding:4px;
padding-right:0px
}
.mod-hiperbook #navpaths #past #end{
background-position:center;
background-repeat:no-repeat;
display:inline;
padding:2px;
padding-left:0px
}
.mod-hiperbook #breadcrumbs{
height:18px;
margin-left:10px;
}
.mod-hiperbook #breadcrumbs #current{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-weight:bold;
font-size:12px;
}
.mod-hiperbook #breadcrumbs #current #main{
display:inline;
text-align:center;
}
.mod-hiperbook #breadcrumbs #current #start{
display:inline;
font-weight:normal;
}
.mod-hiperbook #breadcrumbs #current #end{
display:inline;
}
.mod-hiperbook #breadcrumbs #past{
}
.mod-hiperbook #breadcrumbs #past a{
font-family:Verdana, Arial, Helvetica, sans-serif;
text-decoration:none;
font-size:12px;
}
.mod-hiperbook #breadcrumbs #past #main{
display:inline;
text-align:center;
background-position:center;
}
.mod-hiperbook #breadcrumbs #past #start{
font-family:Verdana, Arial, Helvetica, sans-serif;
text-decoration:none;
font-size:12px;
display:inline;
}
.mod-hiperbook #breadcrumbs #past #end{
display:inline;
}
.mod-hiperbook #page_navigation{
text-align:center;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
}
.mod-hiperbook #page_navigation img{
text-align:center;
color:black;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-weight:bold;
vertical-align:middle;
}
.mod-hiperbook #toc{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-weight:bold;
font-size:12px;
padding-left:18px;
color:#00477F;
width:150px;
}
.mod-hiperbook #toc #item a{
text-align:left;
text-decoration:none;
font-weight:normal;
}
.mod-hiperbook #toc #item a:visited{
color:#8FB4DF;
}
.mod-hiperbook #toc #item{
}
.mod-hiperbook .hiperbook_content{
width:500px;
}
.mod-hiperbook .hiperbook_content #template_top_left{
background-image:url(".$CFG->wwwroot."/file.php/".$book->course."/template_hiperbook".$book->id."/top_conteudo_left.gif);
background-repeat:no-repeat;
}
.mod-hiperbook .hiperbook_content #template_top_right{
background-image:url(".$CFG->wwwroot."/file.php/".$book->course."/template_hiperbook".$book->id."/top_conteudo_right.gif);
background-repeat:no-repeat;
background-position:right;
}
.mod-hiperbook .hiperbook_content #template_main{
background-color:#efefef;
}
.mod-hiperbook .hiperbook_content #template_bottom_left{
background-image:url(".$CFG->wwwroot."/file.php/".$book->course."/template_hiperbook".$book->id."/bottom_conteudo_left.gif);
background-repeat:no-repeat;
}
.mod-hiperbook .hiperbook_content #template_bottom_right{
background-image:url(".$CFG->wwwroot."/file.php/".$book->course."/template_hiperbook".$book->id."/bottom_conteudo_right.gif);
background-repeat:no-repeat;
background-position:right;
}
.hiperbook_content .hotwordslist{
width:100%;
border:0px;
color:#003366;
}
.hiperbook_content .hotwordslist .title a{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
font-weight:bold;
padding-left:46px;
}
.hiperbook_content .hotwordslist .title {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
font-weight:bold;
padding-top:23px;
padding-bottom:23px;
}
.hiperbook_content .hotwordslist .content{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
padding-left:92px;
padding-right:46px;
}
.mod-hiperbook #botoesLateral{
width:250px;
margin-left:170px;
margin-top:10px;
}
#mod-hiperbook-tips #page {
padding-top:0px;
margin-top:0px;
}
.book_chapter_title {
font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif;
text-align: left;
font-size: large;
font-weight: bold;
margin-left: 0px;
margin-bottom: 0px;
}
.book_content {
text-align: left;
}
.book_toc_none ul {
margin-left: 0px;
padding-left: 0px;
}
.book_toc_none ul ul {
margin-left: 0px;
padding-left: 0px;
}
.book_toc_none li {
margin-top: 0px;
list-style: none;
}
.book_toc_none li li {
margin-top: 0px;
list-style: none;
}
.mod-hiperbook #bookname{
font-weight:bold;
font-style:italic;
font-size:14px;
margin-bottom:15px;
}
.mod-hiperbook, a{
color:#00477F;
font-family:Verdana, Arial, Helvetica, sans-serif;
border-collapse:collapse;
border:0px;
}
.mod-hiperbook a:link, .mod-hiperbook a:visited, .mod-hiperbook a{
font-family:Verdana, Arial, Helvetica, sans-serif;
text-decoration:underline;
}
table {
border-collapse:collapse;
border-spacing:0;
}
";
$book->template_main = '<table width="540" height="268" border="0" background="'. $CFG->wwwroot."/file.php/".$book->course."/template_hiperbook".$book->id.'/template_hiperlivro.gif"> <tbody>
<tr height="20">
<td height="20" id="template_top_left">
</td>
<td width="240" height="20" id="template_main">
</td>
<td width="20" height="20" id="template_main">
</td>
<td width="240" height="20" id="template_main">
</td>
<td height="20" id="template_top_right">
</td>
</tr>
<tr id="template_main">
<td width="20" height="195">
</td>
<td valign="top" colspan="3">
<div align="center"></div>
<div align="center"></div><br />
</td>
<td width="20">
</td>
</tr>
<tr id="template_main">
<td id="template_main">
</td>
<td id="template_main"><br />
</td>
<td id="template_main">
</td>
<td id="template_main">
</td>
<td id="template_main">
</td>
</tr>
<tr>
<td height="20" id="template_bottom_left">
</td>
<td height="20" id="template_main">
</td>
<td height="20" id="template_main">
</td>
<td height="20" id="template_main">
</td>
<td height="20" id="template_bottom_right">
</td>
</tr> </tbody>
</table>';
update_record('hiperbook', $book);
mkdir($CFG->dataroot.'/'.$book->course.'/template_hiperbook'.$bookid);
hiperbook_COPY_RECURSIVE_DIRS($CFG->dirroot."/mod/hiperbook/template_avaad/", $CFG->dataroot.'/'.$book->course.'/template_hiperbook'.$bookid);
echo $CFG->dataroot.'/'.$book->course.'/template_hiperbook'.$bookid;
//die();
return $bookid;
}
function hiperbook_COPY_RECURSIVE_DIRS($dirsource, $dirdest)
{ // recursive function to copy
// all subdirectories and contents:
if(is_dir($dirsource))$dir_handle=opendir($dirsource);
mkdir($dirdest."/".$dirsource, 0750);
while($file=readdir($dir_handle))
{
if($file!="." && $file!="..")
{
if(!is_dir($dirsource."/".$file)){
copy ($dirsource."/".$file, $dirdest."/".$file);
} else{
hiperbook_COPY_RECURSIVE_DIRS($dirsource."/".$file, $dirdest);
}
}
}
closedir($dir_handle);
return true;
}
function hiperbook_update_instance($book) {
/// Given an object containing all the necessary data,
/// (defined by the form in mod.html) this function
/// will update an existing instance with new data.
$book->timemodified = time();
$book->id = $book->instance;
# May have to add extra stuff in here #
return update_record('hiperbook', $book);
}
function hiperbook_delete_instance($id) {
/// Given an ID of an instance of this module,
/// this function will permanently delete the instance
/// and any data that depends on it.
if (!$book = get_record('hiperbook', 'id', $id)) {
return false;
}
$module = get_record('modules', 'name', 'hiperbook');
$cm = get_record('course_modules', 'instance', $id,'module' ,$module->id);
$result = true;
$chapters = get_records_select('hiperbook_chapters',"bookid = $book->id");
foreach($chapters as $chapter){
hiperbook_remove_chapter($chapter->id, $cm);
}
if (!delete_records('hiperbook_navigationpath', 'bookid', $book->id)) {
$result = false;
}
if (!delete_records('hiperbook', 'id', $book->id)) {
$result = false;
}
//echo 'funcao nao disponivel';
return $result;
}
function hiperbook_user_outline($course, $user, $mod, $book) {
/// Return a small object with summary information about what a
/// user has done with a given particular instance of this module
/// Used for user activity reports.
/// $return->time = the time they did it
/// $return->info = a short text description
$return = null;
return $return;
}
function hiperbook_user_complete($course, $user, $mod, $book) {
/// Print a detailed representation of what a user has done with
/// a given particular instance of this module, for user activity reports.
return true;
}
function hiperbook_print_recent_activity($course, $isteacher, $timestart) {
/// Given a course and a time, this module should find recent activity
/// that has occurred in book activities and print it out.
/// Return true if there was output, or false is there was none.
global $CFG;
return false; // True if anything was printed, otherwise false
}
function hiperbook_cron () {
/// Function to be run periodically according to the moodle cron
/// This function searches for things that need to be done, such
/// as sending out mail, toggling flags etc ...
global $CFG;
return true;
}
function hiperbook_grades($bookid) {
/// Must return an array of grades for a given instance of this module,
/// indexed by user. It also returns a maximum allowed grade.
return NULL;
}
function hiperbook_get_participants($bookid) {
//Must return an array of user records (all data) who are participants
//for a given instance of book. Must include every user involved
//in the instance, independient of his role (student, teacher, admin...)
//See other modules as example.
return false;
}
function hiperbook_scale_used ($bookid,$scaleid) {
//This function returns if a scale is being used by one book
//it it has support for grading and scales. Commented code should be
//modified if necessary. See forum, glossary or journal modules
//as reference.
$return = false;
//$rec = get_record('book','id',$bookid,'scale',"-$scaleid");
//
//if (!empty($rec) && !empty($scaleid)) {
// $return = true;
//}
return $return;
}
//////////////////////////////////////////////////////////////////////////////////////
/// Any other book functions go here. Each of them must have a name that
/// starts with hiperbook_
//check chapter ordering and
//make sure subchapter is not first in book
//hidden chapter must have all subchapters hidden too
function hiperbook_check_structure($bookid) {
}
/// prepare button to turn chapter editing on - connected with course editing
function hiperbook_edit_button($id, $courseid, $chapterid, $navigationnum, $pagenum, $target_navigation_chapter) {
global $CFG, $USER;
if (isteacheredit($courseid)) {
if (!empty($USER->editing)) {
$string = get_string("turneditingoff");
$edit = '0';
} else {
$string = get_string("turneditingon");
$edit = '1';
}
return '<form target="'.$CFG->framename.'" method="get" action="'.$CFG->wwwroot.'/mod/hiperbook/view.php">'.
'<input type="hidden" name="id" value="'.$id.'" />'.
'<input type="hidden" name="chapterid" value="'.$chapterid.'" />'.
'<input type="hidden" name="navigationnum" value="'.$navigationnum.'" />'.
'<input type="hidden" name="pagenum" value="'.$pagenum.'" />'.
'<input type="hidden" name="target_navigation_chapter" value="'.$target_navigation_chapter.'" />'.
'<input type="hidden" name="edit" value="'.$edit.'" />'.
'<input type="submit" value="'.$string.'" /></form>';
} else {
return '';
}
}
/// general function for logging to table
function hiperbook_log($str1, $str2, $level = 0) {
switch ($level) {
case 1:
echo '<tr><td><span class="dimmed_text">'.$str1.'</span></td><td><span class="dimmed_text">'.$str2.'</span></td></tr>';
break;
case 2:
echo '<tr><td><span style="color: rgb(255, 0, 0);">'.$str1.'</span></td><td><span style="color: rgb(255, 0, 0);">'.$str2.'</span></td></tr>';
break;
default:
echo '<tr><td>'.$str1.'</class></td><td>'.$str2.'</td></tr>';
break;
}
}
//=================================================
// import functions
//=================================================
/// normalize relative links (= remove ..)
function hiperbook_prepare_link($ref) {
if ($ref == '') {
return '';
}
$ref = str_replace('\\','/',$ref); //anti MS hack
$cnt = substr_count($ref, '..');
for($i=0; $i<$cnt; $i++) {
$ref = ereg_replace('[^/]+/\.\./', '', $ref);
}
//still any '..' left?? == error! error!
if (substr_count($ref, '..') > 0) {
return '';
}
if (ereg('[\|\`]', $ref)) { // check for other bad characters
return '';
}
return $ref;
}
/// read chapter content from file
function hiperbook_read_chapter($base, $ref) {
$file = $base.'/'.$ref;
if (filesize($file) <= 0 or !is_readable($file)) {
hiperbook_log($ref, get_string('error'), 2);
return;
}
//first read data
$handle = fopen($file, "rb");
$contents = fread($handle, filesize($file));
fclose($handle);
//extract title
if (preg_match('/<title>([^<]+)<\/title>/i', $contents, $matches)) {
$chapter->title = $matches[1];
} else {
$chapter->title = $ref;
}
//extract page body
if (preg_match('/<body[^>]*>(.+)<\/body>/is', $contents, $matches)) {
$chapter->content = $matches[1];
} else {
hiperbook_log($ref, get_string('error'), 2);
return;
}
hiperbook_log($ref, get_string('ok'));
$chapter->importsrc = $ref;
//extract page head
if (preg_match('/<head[^>]*>(.+)<\/head>/is', $contents, $matches)) {
$head = $matches[1];
if (preg_match('/charset=([^"]+)/is', $head, $matches)) {
$enc = $matches[1];
$chapter->content = hiperbook_conv($enc, $chapter->content);
$chapter->title = hiperbook_conv($enc, $chapter->title);
}
if (preg_match_all('/<link[^>]+rel="stylesheet"[^>]*>/i', $head, $matches)) { //dlnsk extract links to css
for($i=0; $i<count($matches[0]); $i++){
$chapter->content = $matches[0][$i]."\n".$chapter->content;
}
}
}
return $chapter;
}
///relink images and relative links
function hiperbook_relink($id, $bookid, $courseid) {
global $CFG;
if ($CFG->slasharguments) {
$coursebase = $CFG->wwwroot.'/file.php/'.$courseid;
} else {
$coursebase = $CFG->wwwroot.'/file.php?file=/'.$courseid;
}
$chapters = get_records('hiperbook_chapters', 'bookid', $bookid, 'pagenum', 'id, pagenum, title, content, importsrc');
$originals = array();
foreach($chapters as $ch) {
$originals[$ch->importsrc] = $ch;
}
foreach($chapters as $ch) {
$rel = substr($ch->importsrc, 0, strrpos($ch->importsrc, '/')+1);
$base = $coursebase.strtr(urlencode($rel), array("%2F" => "/")); //for better internationalization (dlnsk)
$modified = false;
//image relinking
if ($ch->importsrc && preg_match_all('/(<img[^>]+src=")([^"]+)("[^>]*>)/i', $ch->content, $images)) {
for($i = 0; $i<count($images[0]); $i++) {
if (!preg_match('/[a-z]+:/i', $images[2][$i])) { // not absolute link
$link = hiperbook_prepare_link($base.$images[2][$i]);
if ($link == '') {
continue;
}
$origtag = $images[0][$i];
$newtag = $images[1][$i].$link.$images[3][$i];
$ch->content = str_replace($origtag, $newtag, $ch->content);
$modified = true;
hiperbook_log($ch->title, $images[2][$i].' --> '.$link);
}
}
}
//css relinking (dlnsk)
if ($ch->importsrc && preg_match_all('/(<link[^>]+href=")([^"]+)("[^>]*>)/i', $ch->content, $csslinks)) {
for($i = 0; $i<count($csslinks[0]); $i++) {
if (!preg_match('/[a-z]+:/i', $csslinks[2][$i])) { // not absolute link
$link = hiperbook_prepare_link($base.$csslinks[2][$i]);
if ($link == '') {
continue;
}
$origtag = $csslinks[0][$i];
$newtag = $csslinks[1][$i].$link.$csslinks[3][$i];
$ch->content = str_replace($origtag, $newtag, $ch->content);
$modified = true;
hiperbook_log($ch->title, $csslinks[2][$i].' --> '.$link);
}
}
}
//general embed relinking - flash and others??
if ($ch->importsrc && preg_match_all('/(<embed[^>]+src=")([^"]+)("[^>]*>)/i', $ch->content, $embeds)) {
for($i = 0; $i<count($embeds[0]); $i++) {
if (!preg_match('/[a-z]+:/i', $embeds[2][$i])) { // not absolute link
$link = hiperbook_prepare_link($base.$embeds[2][$i]);
if ($link == '') {
continue;
}
$origtag = $embeds[0][$i];
$newtag = $embeds[1][$i].$link.$embeds[3][$i];
$ch->content = str_replace($origtag, $newtag, $ch->content);
$modified = true;
hiperbook_log($ch->title, $embeds[2][$i].' --> '.$link);
}
}
}
//flash in IE <param name=movie value="something" - I do hate IE!
if ($ch->importsrc && preg_match_all('/<param[^>]+name\s*=\s*"?movie"?[^>]*>/i', $ch->content, $params)) {
for($i = 0; $i<count($params[0]); $i++) {
if (preg_match('/(value=\s*")([^"]+)(")/i', $params[0][$i], $values)) {
if (!preg_match('/[a-z]+:/i', $values[2])) { // not absolute link
$link = hiperbook_prepare_link($base.$values[2]);
if ($link == '') {
continue;
}
$newvalue = $values[1].$link.$values[3];
$newparam = str_replace($values[0], $newvalue, $params[0][$i]);
$ch->content = str_replace($params[0][$i], $newparam, $ch->content);
$modified = true;
hiperbook_log($ch->title, $values[2].' --> '.$link);
}
}
}
}
//java applet - add code bases if not present!!!!
if ($ch->importsrc && preg_match_all('/<applet[^>]*>/i', $ch->content, $applets)) {
for($i = 0; $i<count($applets[0]); $i++) {
if (!stripos($applets[0][$i], 'codebase')) {
$newapplet = str_ireplace('<applet', '<applet codebase="."', $applets[0][$i]);
$ch->content = str_replace($applets[0][$i], $newapplet, $ch->content);
$modified = true;
}
}
}
//relink java applet code bases
if ($ch->importsrc && preg_match_all('/(<applet[^>]+codebase=")([^"]+)("[^>]*>)/i', $ch->content, $codebases)) {
for($i = 0; $i<count($codebases[0]); $i++) {
if (!preg_match('/[a-z]+:/i', $codebases[2][$i])) { // not absolute link
$link = hiperbook_prepare_link($base.$codebases[2][$i]);
if ($link == '') {
continue;
}
$origtag = $codebases[0][$i];
$newtag = $codebases[1][$i].$link.$codebases[3][$i];
$ch->content = str_replace($origtag, $newtag, $ch->content);
$modified = true;
hiperbook_log($ch->title, $codebases[2][$i].' --> '.$link);
}
}
}
//relative link conversion
if ($ch->importsrc && preg_match_all('/(<a[^>]+href=")([^"^#]*)(#[^"]*)?("[^>]*>)/i', $ch->content, $links)) {
for($i = 0; $i<count($links[0]); $i++) {
if ($links[2][$i] != '' //check for inner anchor links
&& !preg_match('/[a-z]+:/i', $links[2][$i])) { //not absolute link
$origtag = $links[0][$i];
$target = hiperbook_prepare_link($rel.$links[2][$i]); //target chapter
if ($target != '' && array_key_exists($target, $originals)) {
$o = $originals[$target];
$newtag = $links[1][$i].$CFG->wwwroot.'/mod/hiperbook/view.php?id='.$id.'&chapterid='.$o->id.$links[3][$i].$links[4][$i];
$newtag = preg_replace('/target=[^\s>]/i','', $newtag);
$ch->content = str_replace($origtag, $newtag, $ch->content);
$modified = true;
hiperbook_log($ch->title, $links[2][$i].$links[3][$i].' --> '.$CFG->wwwroot.'/mod/hiperbook/view.php?id='.$id.'&chapterid='.$o->id.$links[3][$i]);
} else if ($target!='' && (!preg_match('/\.html$|\.htm$/i', $links[2][$i]))) { // other relative non html links converted to download links
$target = hiperbook_prepare_link($base.$links[2][$i]);
$origtag = $links[0][$i];
$newtag = $links[1][$i].$target.$links[4][$i];
$ch->content = str_replace($origtag, $newtag, $ch->content);
$modified = true;
hiperbook_log($ch->title, $links[2][$i].' --> '.$target);
}
}
}
}
if ($modified) {
$ch->title = addslashes($ch->title);
$ch->content = addslashes($ch->content);
$ch->importsrc = addslashes($ch->importsrc);
if (!update_record('hiperbook_chapters', $ch)) {
error('Could not update your hiperbook');
}
}
}
}
//=================================================
// encoding conversion functions
//=================================================
function hiperbook_conv($in, $text) {
$in = strtolower($in);
if (!empty($CFG->unicode)) {
$out = 'utf-8';
} else {
$out = get_string('thischarset');
}
$out = strtolower($out);
if ($in === "windows-1250" && $out === "iso-8859-2") {
$conv = array (130=>"‚", 132=>"„", 133=>"…", 134=>"†", 135=>"‡", 137=>"‰", 138=>"\xa9", 139=>"‹", 140=>"\xa6", 141=>"\xab", 142=>"\xae", 143=>"\xac", 145=>"‘", 146=>"’", 147=>"“", 148=>"”", 149=>"•", 150=>"–", 151=>"—", 153=>"™", 154=>"\xb9", 155=>"›", 156=>"\xb6", 157=>"\xbb", 158=>"\xbe", 159=>"\xbc", 161=>"\xb7", 165=>"\xa1", 166=>"¦", 169=>"©", 171=>"«", 172=>"¬", 174=>"®", 177=>"±", 181=>"µ", 182=>"¶", 183=>"·", 185=>"\xb1", 187=>"»", 188=>"\xa5", 190=>"\xb5");
$len = strlen($text);
$res = '';
for($i=0 ; $i<$len; $i++) {
$num = ord($text{$i});
if (array_key_exists($num, $conv)) {
$res .= $conv[$num];
} else {
$res .= $text{$i};
}
}
return $res;
} else {
return $text;
}
}
//a dicionada por ronnie para leitura dos capitulos superiores para geracao dos breadcrumbs
function hiperbook_link_parent_chapters($navigation_chapter_id, $cm, $groupid=0, $popup= false){
// busca a navegacao atual
$navigation_chapter = get_record('hiperbook_navigation_chapters', 'id',$navigation_chapter_id);
$chapter = get_record('hiperbook_chapters', 'id', $navigation_chapter->chapterid);
$navpath = get_record('hiperbook_navigationpath', 'id',$navigation_chapter->navigationid);
//se tem pai
if($navigation_chapter->parentnavchapterid != 0){
$parentnavchapter = get_record('hiperbook_navigation_chapters', 'id',$navigation_chapter->parentnavchapterid);
$parentchapters_links .= hiperbook_link_parent_chapters($parentnavchapter->id, $cm, $groupid, $popup);
}
$parentchapters_links .= '<td id="past"><div id="start"> > </div><div id="main"><a title="'.htmlspecialchars($parentchapter->title).'" href="';
if($popup) { $parentchapters_links .= 'popup.php'; } else { $parentchapters_links .= 'view.php'; }
$parentchapters_links .= '?id='. $cm->id.'&target_navigation_chapter='.$navigation_chapter->id.'&groupid='.$groupid.'">'.$chapter->title.'</a> </div><div id="end"> </div></td>';
return $parentchapters_links;
}
function hiperbook_link_parent_chapters_html($navigation_chapter_id, $cm, $groupid=0 ){
// busca a navegacao atual
$navigation_chapter = get_record('hiperbook_navigation_chapters', 'id',$navigation_chapter_id);
$chapter = get_record('hiperbook_chapters', 'id', $navigation_chapter->chapterid);
$navpath = get_record('hiperbook_navigationpath', 'id',$navigation_chapter->navigationid);
//se tem pai
if($navigation_chapter->parentnavchapterid != 0){
$parentnavchapter = get_record('hiperbook_navigation_chapters', 'id',$navigation_chapter->parentnavchapterid);
$parentchapters_links .= hiperbook_link_parent_chapters_html($parentnavchapter->id, $cm, $groupid);
}
$parentchapters_links .= '<td id="past"><div id="start"> > </div><div id="main"><a title="'.htmlspecialchars($parentchapter->title).'" href="../../scos/cap'.$chapter->id.'/1.html">'.$chapter->title.'</a> </div><div id="end"> </div></td>';
return $parentchapters_links;
}
// remove um capitulo
function hiperbook_remove_chapter($chapterid, $cm ){
global $CFG;
hiperbook_remove_chapter_pages($chapterid, $cm );
hiperbook_remove_chapter_tips($chapterid, $cm );
hiperbook_remove_chapter_links($chapterid, $cm );
hiperbook_remove_chapter_suggestions($chapterid, $cm );
hiperbook_remove_chapter_exercises($chapterid, $cm );
hiperbook_remove_chapter_hotword($chapterid, $cm );
hiperbook_remove_navigation_chapter($chapterid,$cm);
//busca o cap a ser apagado
$chapter = get_record('hiperbook_chapters', 'id', $chapterid,'bookid',$cm->instance);
if (!delete_records('hiperbook_chapters', 'id', $chapterid,'bookid',$cm->instance)) {
error('Could not update your book chapters');
}else{
$chapternum = $chapter->chapternum;
$parentchapterid = $chapter->parentchapterid;
//move os superiores para baixo
//$sup = get_records_sql("select * from ". $CFG->prefix."hiperbook_chapters where parentchapterid='$parentchapterid' and bookid = '$cm->instance' and chapternum > '$chapternum'");
foreach($sup as $subchapter){
// como se estivesse movendo um cap p/ cima (e baixando os demais) em todos os navigation paths
//hiperbook_move_chapter($cm,$subchapter->id,1,-1);
}
}
//die();
}
// remocve as paginas no capitulo
function hiperbook_remove_chapter_pages($chapterid, $cm ){
echo 'hiperbook_chapters_pages'. ' chapterid='. $chapterid;
if (!delete_records('hiperbook_chapters_pages', 'chapterid', $chapterid)) {
error('Could not update your book pages');
}
}
// remocve as tips no capitulo
function hiperbook_remove_chapter_tips($chapterid, $cm ){
echo 'hiperbook_remove_chapter_tips'. ' chapterid='. $chapterid;
if (!delete_records('hiperbook_chapters_tips', 'chapterid', $chapterid)) {
error('Could not update your book tips');
}
}
function hiperbook_remove_chapter_suggestions($chapterid, $cm ){
echo 'hiperbook_remove_chapter_tips'. ' chapterid='. $chapterid;
if (!delete_records('hiperbook_chapters_suggestions', 'chapterid', $chapterid)) {
error('Could not update your book suggestions');
}
}
function hiperbook_remove_chapter_hotword($chapterid, $cm ){
echo 'hiperbook_remove_chapter_tips'. ' chapterid='. $chapterid;
if (!delete_records('hiperbook_chapters_hotword', 'chapterid', $chapterid)) {
error('Could not update your book hotwords');
}
}
function hiperbook_remove_chapter_links($chapterid, $cm ){
echo 'hiperbook_remove_chapter_tips'. ' chapterid='. $chapterid;