Merge branch 'MDL-33121' of git://github.com/rwijaya/moodle
[moodle.git] / mod / book / locallib.php
index b0a23af..26d0e40 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// This file is part of Book module for Moodle - http://moodle.org/
+// This file is part of Moodle - http://moodle.org/
 //
 // Moodle is free software: you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -75,11 +75,6 @@ function book_preload_chapters($book) {
             $first = false;
         }
         if (!$ch->subchapter) {
-            $ch->prev = $prev;
-            $ch->next = null;
-            if ($prev) {
-                $chapters[$prev]->next = $ch->id;
-            }
             if ($ch->hidden) {
                 if ($book->numbering == BOOK_NUM_NUMBERS) {
                     $ch->number = 'x';
@@ -97,11 +92,6 @@ function book_preload_chapters($book) {
             $ch->parent = null;
             $ch->subchapters = array();
         } else {
-            $ch->prev = $prevsub;
-            $ch->next = null;
-            if ($prevsub) {
-                $chapters[$prevsub]->next = $ch->id;
-            }
             $ch->parent = $parent;
             $ch->subchapters = null;
             $chapters[$parent]->subchapters[$ch->id] = $ch->id;
@@ -120,6 +110,7 @@ function book_preload_chapters($book) {
                 $ch->number = $j;
             }
         }
+
         if ($oldch->subchapter != $ch->subchapter or $oldch->pagenum != $ch->pagenum or $oldch->hidden != $ch->hidden) {
             // update only if something changed
             $DB->update_record('book_chapters', $ch);
@@ -159,27 +150,6 @@ function book_get_chapter_title($chid, $chapters, $book, $context) {
     return $title;
 }
 
-/**
- * General logging to table
- * @param string $str1
- * @param string $str2
- * @param int $level
- * @return void
- */
-function book_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;
-    }
-}
-
 /**
  * Add the book TOC sticky block to the 1st region available
  *
@@ -217,7 +187,7 @@ function book_add_fake_block($chapters, $chapter, $book, $cm, $edit) {
 function book_get_toc($chapters, $chapter, $book, $cm, $edit) {
     global $USER, $OUTPUT;
 
-    $toc = '';  // Representation of toc (HTML)
+    $toc = '';
     $nch = 0;   // Chapter number
     $ns = 0;    // Subchapter number
     $first = 1;
@@ -226,27 +196,35 @@ function book_get_toc($chapters, $chapter, $book, $cm, $edit) {
 
     switch ($book->numbering) {
         case BOOK_NUM_NONE:
-            $toc .= '<div class="book_toc_none">';
+            $toc .= html_writer::start_tag('div', array('class' => 'book_toc_none'));
             break;
         case BOOK_NUM_NUMBERS:
-            $toc .= '<div class="book_toc_numbered">';
+            $toc .= html_writer::start_tag('div', array('class' => 'book_toc_numbered'));
             break;
         case BOOK_NUM_BULLETS:
-            $toc .= '<div class="book_toc_bullets">';
+            $toc .= html_writer::start_tag('div', array('class' => 'book_toc_bullets'));
             break;
         case BOOK_NUM_INDENTED:
-            $toc .= '<div class="book_toc_indented">';
+            $toc .= html_writer::start_tag('div', array('class' => 'book_toc_indented'));
             break;
     }
 
     if ($edit) { // Teacher's TOC
-        $toc .= '<ul>';
+        $toc .= html_writer::start_tag('ul');
         $i = 0;
         foreach ($chapters as $ch) {
             $i++;
             $title = trim(format_string($ch->title, true, array('context'=>$context)));
             if (!$ch->subchapter) {
-                $toc .= ($first) ? '<li>' : '</ul></li><li>';
+
+                if ($first) {
+                    $toc .= html_writer::start_tag('li');
+                } else {
+                    $toc .= html_writer::end_tag('ul');
+                    $toc .= html_writer::end_tag('li');
+                    $toc .= html_writer::start_tag('li');
+                }
+
                 if (!$ch->hidden) {
                     $nch++;
                     $ns = 0;
@@ -257,10 +235,18 @@ function book_get_toc($chapters, $chapter, $book, $cm, $edit) {
                     if ($book->numbering == BOOK_NUM_NUMBERS) {
                         $title = "x $title";
                     }
-                    $title = '<span class="dimmed_text">'.$title.'</span>';
+                    $title = html_writer::tag('span', $title, array('class' => 'dimmed_text'));
                 }
             } else {
-                $toc .= ($first) ? '<li><ul><li>' : '<li>';
+
+                if ($first) {
+                    $toc .= html_writer::start_tag('li');
+                    $toc .= html_writer::start_tag('ul');
+                    $toc .= html_writer::start_tag('li');
+                } else {
+                    $toc .= html_writer::start_tag('li');
+                }
+
                 if (!$ch->hidden) {
                     $ns++;
                     if ($book->numbering == BOOK_NUM_NUMBERS) {
@@ -274,75 +260,109 @@ function book_get_toc($chapters, $chapter, $book, $cm, $edit) {
                             $title = "x.x $title";
                         }
                     }
-                    $title = '<span class="dimmed_text">'.$title.'</span>';
+                    $title = html_writer::tag('span', $title, array('class' => 'dimmed_text'));
                 }
             }
 
             if ($ch->id == $chapter->id) {
-                $toc .= '<strong>'.$title.'</strong>';
+                $toc .= html_writer::tag('strong', $title);
             } else {
-                $toc .= '<a title="'.s($title).'" href="view.php?id='.$cm->id.'&amp;chapterid='.$ch->id.'">'.$title.'</a>';
+                $toc .= html_writer::link(new moodle_url('view.php', array('id' => $cm->id, 'chapterid' => $ch->id)), $title, array('title' => s($title)));
             }
             $toc .=  '&nbsp;&nbsp;';
             if ($i != 1) {
-                $toc .=  ' <a title="'.get_string('up').'" href="move.php?id='.$cm->id.'&amp;chapterid='.$ch->id.
-                        '&amp;up=1&amp;sesskey='.$USER->sesskey.'"><img src="'.$OUTPUT->pix_url('t/up').'" class="iconsmall" alt="'.get_string('up').'" /></a>';
+                $toc .= html_writer::link(new moodle_url('move.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'up' => '1', 'sesskey' => $USER->sesskey)),
+                                            $OUTPUT->pix_icon('t/up', get_string('up')), array('title' => get_string('up')));
             }
             if ($i != count($chapters)) {
-                $toc .=  ' <a title="'.get_string('down').'" href="move.php?id='.$cm->id.'&amp;chapterid='.$ch->id.
-                        '&amp;up=0&amp;sesskey='.$USER->sesskey.'"><img src="'.$OUTPUT->pix_url('t/down').'" class="iconsmall" alt="'.get_string('down').'" /></a>';
+                $toc .= html_writer::link(new moodle_url('move.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'up' => '0', 'sesskey' => $USER->sesskey)),
+                                            $OUTPUT->pix_icon('t/down', get_string('down')), array('title' => get_string('down')));
             }
-            $toc .=  ' <a title="'.get_string('edit').'" href="edit.php?cmid='.$cm->id.'&amp;id='.$ch->id.'"><img src="'.
-                    $OUTPUT->pix_url('t/edit').'" class="iconsmall" alt="'.get_string('edit').'" /></a>';
-            $toc .=  ' <a title="'.get_string('delete').'" href="delete.php?id='.$cm->id.'&amp;chapterid='.$ch->id.
-                    '&amp;sesskey='.$USER->sesskey.'"><img src="'.$OUTPUT->pix_url('t/delete').'" class="iconsmall" alt="'.get_string('delete').'" /></a>';
+            $toc .= html_writer::link(new moodle_url('edit.php', array('cmid' => $cm->id, 'id' => $ch->id)),
+                                        $OUTPUT->pix_icon('t/edit', get_string('edit')), array('title' => get_string('edit')));
+            $toc .= html_writer::link(new moodle_url('delete.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)),
+                                        $OUTPUT->pix_icon('t/delete', get_string('delete')), array('title' => get_string('delete')));
             if ($ch->hidden) {
-                $toc .= ' <a title="'.get_string('show').'" href="show.php?id='.$cm->id.'&amp;chapterid='.$ch->id.
-                        '&amp;sesskey='.$USER->sesskey.'"><img src="'.$OUTPUT->pix_url('t/show').'" class="iconsmall" alt="'.get_string('show').'" /></a>';
+                $toc .= html_writer::link(new moodle_url('show.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)),
+                                            $OUTPUT->pix_icon('t/show', get_string('show')), array('title' => get_string('show')));
             } else {
-                $toc .= ' <a title="'.get_string('hide').'" href="show.php?id='.$cm->id.'&amp;chapterid='.$ch->id.
-                        '&amp;sesskey='.$USER->sesskey.'"><img src="'.$OUTPUT->pix_url('t/hide').'" class="iconsmall" alt="'.get_string('hide').'" /></a>';
+                $toc .= html_writer::link(new moodle_url('show.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)),
+                                            $OUTPUT->pix_icon('t/hide', get_string('hide')), array('title' => get_string('hide')));
             }
-            $toc .= ' <a title="'.get_string('addafter', 'mod_book').'" href="edit.php?cmid='.$cm->id.
-                    '&amp;pagenum='.$ch->pagenum.'&amp;subchapter='.$ch->subchapter.'"><img src="'.
-                    $OUTPUT->pix_url('add', 'mod_book').'" class="iconsmall" alt="'.get_string('addafter', 'mod_book').'" /></a>';
+            $toc .= html_writer::link(new moodle_url('edit.php', array('cmid' => $cm->id, 'pagenum' => $ch->pagenum, 'subchapter' => $ch->subchapter)),
+                                            $OUTPUT->pix_icon('add', get_string('addafter', 'mod_book'), 'mod_book'), array('title' => get_string('addafter', 'mod_book')));
 
-            $toc .= (!$ch->subchapter) ? '<ul>' : '</li>';
+
+            if (!$ch->subchapter) {
+                $toc .= html_writer::start_tag('ul');
+            } else {
+                $toc .= html_writer::end_tag('li');
+            }
             $first = 0;
         }
-        $toc .= '</ul></li></ul>';
+
+        $toc .= html_writer::end_tag('ul');
+        $toc .= html_writer::end_tag('li');
+        $toc .= html_writer::end_tag('ul');
+
     } else { // Normal students view
-        $toc .= '<ul>';
+        $toc .= html_writer::start_tag('ul');
         foreach ($chapters as $ch) {
             $title = trim(format_string($ch->title, true, array('context'=>$context)));
             if (!$ch->hidden) {
                 if (!$ch->subchapter) {
                     $nch++;
                     $ns = 0;
-                    $toc .= ($first) ? '<li>' : '</ul></li><li>';
+
+                    if ($first) {
+                        $toc .= html_writer::start_tag('li');
+                    } else {
+                        $toc .= html_writer::end_tag('ul');
+                        $toc .= html_writer::end_tag('li');
+                        $toc .= html_writer::start_tag('li');
+                    }
+
                     if ($book->numbering == BOOK_NUM_NUMBERS) {
                           $title = "$nch $title";
                     }
                 } else {
                     $ns++;
-                    $toc .= ($first) ? '<li><ul><li>' : '<li>';
+
+                    if ($first) {
+                        $toc .= html_writer::start_tag('li');
+                        $toc .= html_writer::start_tag('ul');
+                        $toc .= html_writer::start_tag('li');
+                    } else {
+                        $toc .= html_writer::start_tag('li');
+                    }
+
                     if ($book->numbering == BOOK_NUM_NUMBERS) {
                           $title = "$nch.$ns $title";
                     }
                 }
                 if ($ch->id == $chapter->id) {
-                    $toc .= '<strong>'.$title.'</strong>';
+                    $toc .= html_writer::tag('strong', $title);
                 } else {
-                    $toc .= '<a title="'.s($title).'" href="view.php?id='.$cm->id.'&amp;chapterid='.$ch->id.'">'.$title.'</a>';
+                    $toc .= html_writer::link(new moodle_url('view.php', array('id' => $cm->id, 'chapterid' => $ch->id)), $title, array('title' => s($title)));
                 }
-                $toc .= (!$ch->subchapter) ? '<ul>' : '</li>';
+
+                if (!$ch->subchapter) {
+                    $toc .= html_writer::start_tag('ul');
+                } else {
+                    $toc .= html_writer::end_tag('li');
+                }
+
                 $first = 0;
             }
         }
-        $toc .= '</ul></li></ul>';
+
+        $toc .= html_writer::end_tag('ul');
+        $toc .= html_writer::end_tag('li');
+        $toc .= html_writer::end_tag('ul');
+
     }
 
-    $toc .= '</div>';
+    $toc .= html_writer::end_tag('div');
 
     $toc = str_replace('<ul></ul>', '', $toc); // Cleanup of invalid structures.