Skip to content

Commit 56d465b

Browse files
Andrew Robert Nicolsandrewnicols
authored andcommitted
MDL 38508 JavaScript: Split out AJAX and non-AJAX help
We need to keep these two separate as scripts which define AJAX_SCRIPT before loading config.php will use a different default renderer and will return appropriate exceptions which can be parsed by M.core.exception and M.core.ajaxException correctly. This also addresses an issue whereby a missing heading could break the tooltip.
1 parent 422f68f commit 56d465b

File tree

8 files changed

+307
-188
lines changed

8 files changed

+307
-188
lines changed

help.php

Lines changed: 11 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -32,73 +32,22 @@
3232

3333
$identifier = required_param('identifier', PARAM_STRINGID);
3434
$component = required_param('component', PARAM_COMPONENT);
35-
$lang = required_param('lang', PARAM_LANG); // TODO: maybe split into separate scripts
36-
$ajax = optional_param('ajax', 0, PARAM_BOOL);
35+
$lang = optional_param('lang', 'en', PARAM_LANG);
3736

38-
if (!$lang) {
39-
$lang = 'en';
40-
}
41-
$SESSION->lang = $lang; // does not actually modify session because we do not use cookies here
42-
43-
$sm = get_string_manager();
37+
// We don't actually modify the session here as we have NO_MOODLE_COOKIES set.
38+
$SESSION->lang = $lang;
4439

4540
$PAGE->set_url('/help.php');
4641
$PAGE->set_pagelayout('popup');
4742
$PAGE->set_context(context_system::instance());
4843

49-
if ($ajax) {
50-
@header('Content-Type: text/plain; charset=utf-8');
51-
}
52-
53-
if (!$sm->string_exists($identifier.'_help', $component)) {
54-
// strings on disk-cache may be dirty - try to rebuild it and check again
55-
$sm->load_component_strings($component, current_language(), true);
56-
}
57-
58-
$data = new stdClass();
59-
60-
if ($sm->string_exists($identifier.'_help', $component)) {
61-
$options = new stdClass();
62-
$options->trusted = false;
63-
$options->noclean = false;
64-
$options->smiley = false;
65-
$options->filter = false;
66-
$options->para = true;
67-
$options->newlines = false;
68-
$options->overflowdiv = !$ajax;
69-
70-
$data->heading = format_string(get_string($identifier, $component));
71-
// Should be simple wiki only MDL-21695
72-
$data->text = format_text(get_string($identifier.'_help', $component), FORMAT_MARKDOWN, $options);
73-
74-
$helplink = $identifier . '_link';
75-
if ($sm->string_exists($helplink, $component)) { // Link to further info in Moodle docs
76-
$link = get_string($helplink, $component);
77-
$linktext = get_string('morehelp');
78-
79-
$data->doclink = new stdClass();
80-
$url = new moodle_url(get_docs_url($link));
81-
$data->doclink->link = $url->out();
82-
$data->doclink->linktext = $linktext;
83-
$data->doclink->class = ($CFG->doctonewwindow) ? 'helplinkpopup' : '';
84-
85-
$completedoclink = html_writer::tag('div', $OUTPUT->doc_link($link, $linktext), array('class' => 'helpdoclink'));
86-
}
87-
} else {
88-
$data->text = html_writer::tag('p',
89-
html_writer::tag('strong', 'TODO') . ": missing help string [{$identifier}_help, {$component}]");
44+
$data = get_formatted_help_string($identifier, $component, false);
45+
echo $OUTPUT->header();
46+
if (!empty($data->heading)) {
47+
echo $OUTPUT->heading($data->heading, 1, 'helpheading');
9048
}
91-
92-
if ($ajax) {
93-
echo json_encode($data);
94-
} else {
95-
echo $OUTPUT->header();
96-
if (isset($data->heading)) {
97-
echo $OUTPUT->heading($data->heading, 1, 'helpheading');
98-
}
99-
echo $data->text;
100-
if (isset($completedoclink)) {
101-
echo $completedoclink;
102-
}
103-
echo $OUTPUT->footer();
49+
echo $data->text;
50+
if (isset($data->completedoclink)) {
51+
echo $data->completedoclink;
10452
}
53+
echo $OUTPUT->footer();

help_ajax.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
// This file is part of Moodle - http://moodle.org/
4+
//
5+
// Moodle is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Moodle is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17+
18+
/**
19+
* Displays help via AJAX call
20+
*
21+
* @copyright 2013 onwards Andrew Nicols
22+
* @package core
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
define('NO_MOODLE_COOKIES', true);
27+
define('AJAX_SCRIPT', true);
28+
require_once(__DIR__ . '/config.php');
29+
30+
$identifier = required_param('identifier', PARAM_STRINGID);
31+
$component = required_param('component', PARAM_COMPONENT);
32+
$lang = optional_param('lang', 'en', PARAM_LANG);
33+
34+
// We don't actually modify the session here as we have NO_MOODLE_COOKIES set.
35+
$SESSION->lang = $lang;
36+
$PAGE->set_url('/help_ajax.php');
37+
$PAGE->set_context(context_system::instance());
38+
39+
$data = get_formatted_help_string($identifier, $component, true);
40+
echo json_encode($data);

lib/weblib.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3463,3 +3463,72 @@ function print_password_policy() {
34633463
}
34643464
return $message;
34653465
}
3466+
3467+
/**
3468+
* Get the value of a help string fully prepared for display in the current language.
3469+
*
3470+
* @param string $identifier The identifier of the string to search for.
3471+
* @param string $component The module the string is associated with.
3472+
* @param boolean $ajax Whether this help is called from an AJAX script.
3473+
* This is used to influence text formatting and determines
3474+
* which format to output the doclink in.
3475+
* @return Object An object containing:
3476+
* - heading: Any heading that there may be for this help string.
3477+
* - text: The wiki-formatted help string.
3478+
* - doclink: An object containing a link, the linktext, and any additional
3479+
* CSS classes to apply to that link. Only present if $ajax = false.
3480+
* - completedoclink: A text representation of the doclink. Only present if $ajax = true.
3481+
*/
3482+
function get_formatted_help_string($identifier, $component, $ajax = false) {
3483+
global $CFG, $OUTPUT;
3484+
$sm = get_string_manager();
3485+
3486+
if (!$sm->string_exists($identifier, $component) ||
3487+
!$sm->string_exists($identifier . '_help', $component)) {
3488+
// Strings in the on-disk cache may be dirty - try to rebuild it and check again.
3489+
$sm->load_component_strings($component, current_language(), true);
3490+
}
3491+
3492+
$data = new stdClass();
3493+
3494+
if ($sm->string_exists($identifier, $component)) {
3495+
$data->heading = format_string(get_string($identifier, $component));
3496+
} else {
3497+
// Gracefully fall back to an empty string.
3498+
$data->heading = '';
3499+
}
3500+
3501+
if ($sm->string_exists($identifier . '_help', $component)) {
3502+
$options = new stdClass();
3503+
$options->trusted = false;
3504+
$options->noclean = false;
3505+
$options->smiley = false;
3506+
$options->filter = false;
3507+
$options->para = true;
3508+
$options->newlines = false;
3509+
$options->overflowdiv = !$ajax;
3510+
3511+
// Should be simple wiki only MDL-21695.
3512+
$data->text = format_text(get_string($identifier.'_help', $component), FORMAT_MARKDOWN, $options);
3513+
3514+
$helplink = $identifier . '_link';
3515+
if ($sm->string_exists($helplink, $component)) { // Link to further info in Moodle docs
3516+
$link = get_string($helplink, $component);
3517+
$linktext = get_string('morehelp');
3518+
3519+
$data->doclink = new stdClass();
3520+
$url = new moodle_url(get_docs_url($link));
3521+
if ($ajax) {
3522+
$data->doclink->link = $url->out();
3523+
$data->doclink->linktext = $linktext;
3524+
$data->doclink->class = ($CFG->doctonewwindow) ? 'helplinkpopup' : '';
3525+
} else {
3526+
$data->completedoclink = html_writer::tag('div', $OUTPUT->doc_link($link, $linktext), array('class' => 'helpdoclink'));
3527+
}
3528+
}
3529+
} else {
3530+
$data->text = html_writer::tag('p',
3531+
html_writer::tag('strong', 'TODO') . ": missing help string [{$identifier}_help, {$component}]");
3532+
}
3533+
return $data;
3534+
}

0 commit comments

Comments
 (0)