Commit 0c5e1bff authored by Adrian Greeve's avatar Adrian Greeve
Browse files

MDL-71912 mod_book: Update book to implement the action menu.


Co-authored-by: Sujith Haridasan's avatarSujith Haridasan <sujith@moodle.com>
parent d135a120
<?php
// 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Output the action menu for this activity.
*
* @package mod_book
* @copyright 2021 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_book\output;
use templatable;
use renderable;
use moodle_url;
use stdClass;
/**
* Output the action menu for the book activity.
*
* @package mod_book
* @copyright 2021 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class main_action_menu implements templatable, renderable {
/** @var int The course module ID. */
protected $cmid;
/** @var stdClass[] Chapters of the book. */
protected $chapters;
/** @var stdClass Current chapter of the book. */
protected $chapter;
/**
* Constructor for this class.
*
* @param int $cmid The course module ID.
* @param array $chapters Chapters of this book.
* @param stdClass $chapter The current chapter.
*/
public function __construct(int $cmid, array $chapters, stdClass $chapter) {
$this->cmid = $cmid;
$this->chapters = $chapters;
$this->chapter = $chapter;
}
/**
* Get the next chapter in the book.
*
* @return ?stdClass The next chapter of the book.
*/
protected function get_next_chapter(): ?stdClass {
$nextpageid = $this->chapter->pagenum + 1;
// Early return if the current chapter is also the last chapter.
if ($nextpageid > count($this->chapters)) {
return null;
}
while ((!$nextchapter = $this->get_chapter($nextpageid))) {
// Break the loop if this is the last chapter.
if ($nextpageid === count($this->chapters)) {
break;
}
$nextpageid++;
}
return $nextchapter;
}
/**
* Get the previous chapter in the book.
*
* @return ?stdClass The previous chapter of the book.
*/
protected function get_previous_chapter(): ?stdClass {
$prevpageid = $this->chapter->pagenum - 1;
// Early return if the current chapter is also the first chapter.
if ($prevpageid < 1) {
return null;
}
while ((!$prevchapter = $this->get_chapter($prevpageid))) {
// Break the loop if this is the first chapter.
if ($prevpageid === 1) {
break;
}
$prevpageid--;
}
return $prevchapter;
}
/**
* Get the specific chapter of the book.
*
* @param int $id The chapter id to retrieve.
* @return ?stdClass The requested chapter.
*/
protected function get_chapter(int $id): ?stdClass {
$context = \context_module::instance($this->cmid);
$viewhidden = has_capability('mod/book:viewhiddenchapters', $context);
foreach ($this->chapters as $chapter) {
// Also make sure that the chapter is not hidden or the user can view hidden chapters before returning
// the chapter object.
if (($chapter->pagenum == $id) && (!$chapter->hidden || $viewhidden)) {
return $chapter;
}
}
return null;
}
/**
* Exports the navigation buttons around the book.
*
* @param \renderer_base $output renderer base output.
* @return array Data to render.
*/
public function export_for_template(\renderer_base $output): array {
$next = $this->get_next_chapter();
$previous = $this->get_previous_chapter();
$context = \context_module::instance($this->cmid);
$data = [];
if ($next) {
$nextdata = [
'title' => get_string('navnext', 'mod_book'),
'url' => (new moodle_url('/mod/book/view.php', ['id' => $this->cmid, 'chapterid' => $next->id]))->out(false)
];
$data['next'] = $nextdata;
}
if ($previous) {
$previousdata = [
'title' => get_string('navprev', 'mod_book'),
'url' => (new moodle_url('/mod/book/view.php', ['id' => $this->cmid, 'chapterid' => $previous->id]))->out(false)
];
$data['previous'] = $previousdata;
}
return $data;
}
}
<?php
// 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Moodle renderer used to display special elements of the book module
*
* @package mod_book
* @copyright 2021 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_book\output;
/**
* Moodle renderer used to display special elements of the book module
*
* @package mod_book
* @copyright 2021 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends \plugin_renderer_base {
/**
* Renderers the main action menu.
*
* @param main_action_menu $actionmenu Main action menu object.
* @return string The rendered html.
*/
public function render_main_action_menu(main_action_menu $actionmenu): string {
$context = $actionmenu->export_for_template($this);
return $this->render_from_template('mod_book/main_action_menu', $context);
}
}
......@@ -43,9 +43,12 @@ function book_get_numbering_types() {
/**
* Returns list of available navigation link types.
*
* @deprecated since Moodle 4.0. MDL-72376.
* @return array
*/
function book_get_nav_types() {
debugging("book_get_nav_types() is deprecated. There is no replacement. Navigation is now only next and previous.");
require_once(__DIR__.'/locallib.php');
return array (
......@@ -332,25 +335,6 @@ function book_extend_settings_navigation(settings_navigation $settingsnav, navig
$firstkey = null;
}
$params = $PAGE->url->params();
if ($PAGE->cm->modname === 'book' and !empty($params['id']) and !empty($params['chapterid'])
and has_capability('mod/book:edit', $PAGE->cm->context)) {
if (!empty($USER->editing)) {
$string = get_string("turneditingoff");
$edit = '0';
} else {
$string = get_string("turneditingon");
$edit = '1';
}
$url = new moodle_url('/mod/book/view.php', array('id'=>$params['id'], 'chapterid'=>$params['chapterid'], 'edit'=>$edit, 'sesskey'=>sesskey()));
$editnode = navigation_node::create($string, $url, navigation_node::TYPE_SETTING);
$booknode->add_node($editnode, $firstkey);
if (!$PAGE->theme->haseditswitch) {
$PAGE->set_button($OUTPUT->single_button($url, $string));
}
}
$plugins = core_component::get_plugin_list('booktool');
foreach ($plugins as $plugin => $dir) {
if (file_exists("$dir/lib.php")) {
......
......@@ -70,25 +70,7 @@ class mod_book_mod_form extends moodleform_mod {
$mform->addHelpButton('numbering', 'numbering', 'mod_book');
$mform->setDefault('numbering', $config->numbering);
$alloptions = book_get_nav_types();
$allowed = explode(',', $config->navoptions);
$options = array();
foreach ($allowed as $type) {
if (isset($alloptions[$type])) {
$options[$type] = $alloptions[$type];
}
}
if ($this->current->instance) {
if (!isset($options[$this->current->navstyle])) {
if (isset($alloptions[$this->current->navstyle])) {
$options[$this->current->navstyle] = $alloptions[$this->current->navstyle];
}
}
}
$mform->addElement('select', 'navstyle', get_string('navstyle', 'book'), $options);
$mform->addHelpButton('navstyle', 'navstyle', 'mod_book');
$mform->setDefault('navstyle', $config->navstyle);
$mform->addElement('static', 'customtitlestext', get_string('customtitles', 'mod_book'));
$mform->addElement('checkbox', 'customtitles', get_string('customtitles', 'book'));
$mform->addHelpButton('customtitles', 'customtitles', 'mod_book');
$mform->setDefault('customtitles', 0);
......
......@@ -35,11 +35,6 @@ if ($ADMIN->fulltree) {
get_string('numberingoptions', 'mod_book'), get_string('numberingoptions_desc', 'mod_book'),
array_keys($options), $options));
$navoptions = book_get_nav_types();
$settings->add(new admin_setting_configmultiselect('book/navoptions',
get_string('navoptions', 'mod_book'), get_string('navoptions_desc', 'mod_book'),
array_keys($navoptions), $navoptions));
// Modedit defaults.
$settings->add(new admin_setting_heading('bookmodeditdefaults',
......@@ -48,7 +43,4 @@ if ($ADMIN->fulltree) {
$settings->add(new admin_setting_configselect('book/numbering',
get_string('numbering', 'mod_book'), '', BOOK_NUM_NUMBERS, $options));
$settings->add(new admin_setting_configselect('book/navstyle',
get_string('navstyle', 'mod_book'), '', BOOK_LINK_IMAGE, $navoptions));
}
{{!
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template mod_book/main_action_menu
Previous and next buttons for the book.
Classes required for JS:
* none
Context variables required for this template:
* see mod/book/classes/output/main_actionbar.php
Example context (json):
{
"previous": {
"url": "https://moodle.org",
"title": "Chapter 1: First of many"
},
"next": {
"url": "https://moodle.org",
"title": "Chapter 3: Last of many"
}
}
}}
<div class="container-fluid">
<div class="row">
<div class="col-xs-6">
{{#previous}}
<a class="btn btn-secondary mr-1" href="{{url}}">{{title}}</a>
{{/previous}}
</div>
<div class="col-xs-6">
{{#next}}
<a class="btn btn-secondary" href="{{url}}">{{title}}</a>
{{/next}}
</div>
</div>
</div>
......@@ -49,7 +49,7 @@ Feature: In a book, create chapters and sub chapters
And I should see "2. Dummy second chapter" in the ".book_content" "css_element"
And I should see "2. Dummy second chapter" in the "strong" "css_element"
And I should not see "Next" in the ".book_content" "css_element"
And I click on "Exit book" "link"
And I am on "Course 1" course homepage
And I should see "Test book" in the "Topic 1" "section"
And I click on "Test book" "link" in the "Topic 1" "section"
And I should not see "Previous" in the ".book_content" "css_element"
......
......@@ -16,6 +16,7 @@ Feature: In a book, change the navigation options
| teacher1 | C1 | editingteacher |
And I log in as "teacher1"
# The option to "Style of navigation" is removed from the settings.
Scenario: Change navigation options
Given the following "activities" exist:
| activity | name | intro | course | idnumber | section | navstyle |
......@@ -37,18 +38,6 @@ Feature: In a book, change the navigation options
And I should see "Test book"
And I should see "2. Test chapter 2"
And I click on "1. Test chapter 1" "link" in the "Table of contents" "block"
And "Next" "link" should not exist
And I click on "2. Test chapter 2" "link" in the "Table of contents" "block"
And "Previous" "link" should not exist
And I navigate to "Settings" in current page administration
And I set the field "Style of navigation" to "Images"
And I press "Save and display"
And "Next:" "icon" should exist
And I click on "2. Test chapter 2" "link" in the "Table of contents" "block"
And "Previous:" "icon" should exist
When I navigate to "Settings" in current page administration
And I set the field "Style of navigation" to "Text"
And I press "Save and display"
Then "Next" "link" should exist
And "Next" "link" should exist
And I click on "2. Test chapter 2" "link" in the "Table of contents" "block"
And "Previous" "link" should exist
......@@ -33,7 +33,7 @@ Feature: In a book, verify log entries
And I click on "Next" "link"
And I click on "Previous" "link"
And I navigate to "Print book" in current page administration
And I navigate to "Download" in current page administration
And I navigate to "Download IMS CP" in current page administration
And I navigate to "Logs" in current page administration
Then I should see "Book exported"
And I should see "Book printed"
......
......@@ -25,7 +25,9 @@ Feature: Book activity chapter visibility management
| Chapter title | First chapter |
| Content | First chapter |
And I press "Save changes"
And I am on "Course 1" course homepage
And I turn editing mode on
And I click on "Test book" "link" in the "region-main" "region"
And I click on "Add new chapter after \"First chapter\"" "link"
And I set the following fields to these values:
| Chapter title | Second chapter |
......@@ -54,26 +56,25 @@ Feature: Book activity chapter visibility management
When I follow "Hide chapter \"2. Second chapter\""
And I follow "Hide chapter \"2. Third chapter\""
And I am on the "Test book" "book activity" page
And I am on "Course 1" course homepage
And I turn editing mode off
And I click on "Test book" "link" in the "region-main" "region"
Then the "class" attribute of "a[title='Second chapter']" "css_element" should contain "dimmed_text"
And the "class" attribute of "a[title='Third chapter']" "css_element" should contain "dimmed_text"
And I am on "Course 1" course homepage
And I turn editing mode on
And I click on "Test book" "link" in the "region-main" "region"
And I follow "Next"
And I should see "Second chapter" in the ".book_content" "css_element"
And I should not see "Exit book"
And I follow "Next"
And I should see "Sub chapter" in the ".book_content" "css_element"
And I follow "Next"
And I should see "Third chapter" in the ".book_content" "css_element"
And I follow "Next"
And I should see "Fourth chapter" in the ".book_content" "css_element"
And I follow "Exit book"
And I log out
And I am on the "Test book" "book activity" page logged in as student1
And I should not see "Second chapter" in the "Table of contents" "block"
And I should not see "Third chapter" in the "Table of contents" "block"
And I follow "Next"
And I should see "Fourth chapter" in the ".book_content" "css_element"
And I follow "Exit book"
And I am on the "Test book" "book activity" page
And I should see "First chapter" in the ".book_content" "css_element"
......@@ -26,9 +26,7 @@ defined('MOODLE_INTERNAL') || die;
$string['eventbookexported'] = 'Book exported';
$string['exportimscp:export'] = 'Export book as IMS content package';
$string['generateimscp'] = 'Download IMS CP';
$string['nochapters'] = 'No book chapters found, so unable to export to IMS CP.';
$string['pluginname'] = 'Book IMS CP export';
$string['privacy:metadata'] = 'The Book IMS CP export plugin does not store any personal data.';
// Deprecated since Moodle 4.0.
$string['generateimscp'] = 'Generate IMS CP';
generateimscp,booktool_exportimscp
\ No newline at end of file
......@@ -36,6 +36,8 @@ function booktool_exportimscp_extend_settings_navigation(settings_navigation $se
if (has_capability('booktool/exportimscp:export', $PAGE->cm->context)) {
$url = new moodle_url('/mod/book/tool/exportimscp/index.php', array('id'=>$PAGE->cm->id));
$icon = new pix_icon('generate', '', 'booktool_exportimscp', array('class'=>'icon'));
$node->add(get_string('download'), $url, navigation_node::TYPE_SETTING, null, null, $icon);
$umscpnode = $node->add(get_string('generateimscp', 'booktool_exportimscp'), $url,
navigation_node::TYPE_SETTING, null, null, $icon);
$umscpnode->set_force_into_more_menu(true);
}
}
......@@ -38,7 +38,7 @@ require_login($course, false, $cm);
$context = context_module::instance($cm->id);
require_capability('booktool/importhtml:import', $context);
$PAGE->set_url('/mod/book/tool/importhtml/index.php', array('id'=>$id, 'chapterid'=>$chapterid));
$PAGE->set_url('/mod/book/tool/importhtml/index.php', array('id' => $id));
if ($chapterid) {
if (!$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id))) {
......
......@@ -35,6 +35,6 @@ function booktool_importhtml_extend_settings_navigation(settings_navigation $set
if (has_capability('booktool/importhtml:import', $PAGE->cm->context)) {
$url = new moodle_url('/mod/book/tool/importhtml/index.php', array('id'=>$PAGE->cm->id));
$node->add(get_string('import', 'booktool_importhtml'), $url, navigation_node::TYPE_SETTING, null, null, null);
$node->add(get_string('import', 'booktool_importhtml'), $url, navigation_node::TYPE_SETTING, null, 'importchapter', null);
}
}
......@@ -42,11 +42,13 @@ function booktool_print_extend_settings_navigation(settings_navigation $settings
$url1 = new moodle_url('/mod/book/tool/print/index.php', array('id'=>$params['id']));
$url2 = new moodle_url('/mod/book/tool/print/index.php', array('id'=>$params['id'], 'chapterid'=>$params['chapterid']));
$action = new action_link($url1, get_string('printbook', 'booktool_print'), new popup_action('click', $url1));
$node->add(get_string('printbook', 'booktool_print'), $action, navigation_node::TYPE_SETTING, null, null,
new pix_icon('book', '', 'booktool_print', array('class'=>'icon')));
$booknode = $node->add(get_string('printbook', 'booktool_print'), $action, navigation_node::TYPE_SETTING, null, 'printbook',
new pix_icon('book', '', 'booktool_print', array('class' => 'icon')));
$booknode->set_force_into_more_menu(true);
$action = new action_link($url2, get_string('printchapter', 'booktool_print'), new popup_action('click', $url2));
$node->add(get_string('printchapter', 'booktool_print'), $action, navigation_node::TYPE_SETTING, null, null,
new pix_icon('chapter', '', 'booktool_print', array('class'=>'icon')));
$chapternode = $node->add(get_string('printchapter', 'booktool_print'), $action, navigation_node::TYPE_SETTING, null,
'printchapter', new pix_icon('chapter', '', 'booktool_print', array('class' => 'icon')));
$chapternode->set_force_into_more_menu(true);
}
}
......
This files describes API changes in the book code.
=== 4.0 ===
* book_get_nav_types() has been deprecated. Related settings have been removed. The navigation is now set to only "next" and
"previous".
=== 3.11 ===
* Final deprecation - booktool_print_get_toc(). Please use render_print_book_toc() instead.
......
......@@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die;
$plugin->component = 'mod_book'; // Full name of the plugin (used for diagnostics)
$plugin->version = 2021052501; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2021052504; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2021052500; // Requires this Moodle version.
$plugin->cron = 0; // Period for cron to check this module (secs)
......@@ -123,81 +123,12 @@ $PAGE->set_heading($course->fullname);
book_add_fake_block($chapters, $chapter, $book, $cm, $edit);
// prepare chapter navigation icons
$previd = null;
$prevtitle = null;
$navprevtitle = null;
$nextid = null;
$nexttitle = null;
$navnexttitle = null;
$last = null;
foreach ($chapters as $ch) {
if (!$edit and ($ch->hidden && !$viewhidden)) {
continue;
}
if ($last == $chapter->id) {
$nextid = $ch->id;
$nexttitle = book_get_chapter_title($ch->id, $chapters, $book, $context);
$navnexttitle = get_string('navnexttitle', 'mod_book', $nexttitle);
break;
}
if ($ch->id != $chapter->id) {
$previd = $ch->id;
$prevtitle = book_get_chapter_title($ch->id, $chapters, $book, $context);
$navprevtitle = get_string('navprevtitle', 'mod_book', $prevtitle);
}
$last = $ch->id;
}
if ($book->navstyle) {
$navprevicon = right_to_left() ? 'nav_next' : 'nav_prev';
$navnexticon = right_to_left() ? 'nav_prev' : 'nav_next';
$chnavigation = '';
if ($previd) {
$navprev = get_string('navprev', 'book');
if ($book->navstyle == 1) {
$chnavigation .= '<a title="' . $navprevtitle . '" class="bookprev" href="view.php?id=' .
$cm->id . '&amp;chapterid=' . $previd . '">' .
$OUTPUT->pix_icon($navprevicon, $navprevtitle, 'mod_book') . '</a>';
} else {
$chnavigation .= '<a title="' . $navprev . '" class="bookprev" href="view.php?id=' .
$cm->id . '&amp;chapterid=' . $previd . '">' .
'<span class="chaptername"><span class="arrow">' . $OUTPUT->larrow() . '&nbsp;</span></span>' .
$navprev . ':&nbsp;<span class="chaptername">' . $prevtitle . '</span></a>';
}
}
if ($nextid) {
$navnext = get_string('navnext', 'book');
if ($book->navstyle == 1) {
$chnavigation .= '<a title="' . $navnexttitle . '" class="booknext" href="view.php?id=' .
$cm->id . '&amp;chapterid='.$nextid.'">' .
$OUTPUT->pix_icon($navnexticon, $navnexttitle, 'mod_book') . '</a>';
} else {
$chnavigation .= ' <a title="' . $navnext . '" class="booknext" href="view.php?id=' .
$cm->id . '&amp;chapterid='.$nextid.'">' .
$navnext . ':<span class="chaptername">&nbsp;' . $nexttitle.
'<span class="arrow">&nbsp;' . $OUTPUT->rarrow() . '</span></span></a>';
}
} else {
$navexit = get_string('navexit', 'book');
$sec = $DB->get_field('course_sections', 'section', array('id' => $cm->section));
$returnurl = course_get_url($course, $sec);
if ($book->navstyle == 1) {
$chnavigation .= '<a title="' . $navexit . '" class="bookexit" href="'.$returnurl.'">' .
$OUTPUT->pix_icon('nav_exit', $navexit, 'mod_book') . '</a>';
} else {
$chnavigation .= ' <a title="' . $navexit . '" class="bookexit" href="'.$returnurl.'">' .
'<span class="chaptername">' . $navexit . '&nbsp;' . $OUTPUT->uarrow() . '</span></a>';
}
}
}
$renderer = $PAGE->get_renderer('mod_book');
$actionmenu = new \mod_book\output\main_action_menu($cm->id, $chapters, $chapter, $book);
$renderedmenu = $renderer->render($actionmenu);
// We need to discover if this is the last chapter to mark activity as completed.
$islastchapter = false;
if (!$nextid) {
$islastchapter = true;
}
$islastchapter = $chapter->pagenum + 1 > count($chapters);