forked from mudrd8mz/moodle-editor_marklar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
158 lines (137 loc) · 5.2 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
<?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/>.
/**
* Text editor interface implementation.
*
* @package editor_marklar
* @copyright 2016 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Defines the Marklar editor behaviour in terms of Moodle text editor interface
*
* @copyright 2016 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class marklar_texteditor extends texteditor {
/**
* Is editor supported in current browser?
*
* @return bool
*/
public function supported_by_browser() {
return true;
}
/**
* Returns list of supported text formats.
*
* Marklar natively supports Markdown texts. As it is basically just a
* textarea, we also declare it should be used for Moodle auto-formatted
* and plain texts. For HTML, another rich text editor would be then used
* (such as Atto). However, users can set this behaviour via their
* preferences.
*
* @return array
*/
public function get_supported_formats() {
// Marklar is always supported.
$supported = [FORMAT_MARKDOWN => FORMAT_MARKDOWN];
// Other formats can be supported via user preferences.
$formats = json_decode(get_user_preferences('editor_marklar/formats'));
if (is_object($formats)) {
if (!empty($formats->{'format'.FORMAT_MOODLE})) {
$supported[FORMAT_MOODLE] = FORMAT_MOODLE;
}
if (!empty($formats->{'format'.FORMAT_HTML})) {
$supported[FORMAT_HTML] = FORMAT_HTML;
}
if (!empty($formats->{'format'.FORMAT_PLAIN})) {
$supported[FORMAT_PLAIN] = FORMAT_PLAIN;
}
} else {
$supported[FORMAT_PLAIN] = FORMAT_PLAIN;
$supported[FORMAT_MOODLE] = FORMAT_MOODLE;
}
return $supported;
}
/**
* Returns main preferred text format.
*
* @return int text format
*/
public function get_preferred_format() {
return FORMAT_MARKDOWN;
}
/**
* Supports file picker and repos?
*
* @todo
* @return bool
*/
public function supports_repositories() {
return false;
}
/**
* Add required JS needed for editor
*
* @param string $elementid id of text area to be converted to editor
* @param array $options
* @param object $fpoptions file picker options
* @return void
*/
public function use_editor($elementid, array $options=null, $fpoptions = null) {
global $PAGE;
$initparams = [
'elementid' => $elementid,
'contextid' => empty($options['context']) ? $PAGE->context->id : $options['context']->id,
];
$PAGE->requires->js_call_amd('editor_marklar/editor', 'init', [$initparams]);
// If we passed the $fpoptions via init's parameters, debugging warning
// about the parameter size would be thrown. This is a really nasty
// hack to work around that. See MDL-53423 for details.
$PAGE->requires->js_init_code('M.editor_marklar = M.editor_marklar || {}');
$PAGE->requires->js_init_code('M.editor_marklar.fpoptions = M.editor_marklar.fpoptions || {}');
$PAGE->requires->js_init_code(js_writer::set_variable('M.editor_marklar.fpoptions.'.$elementid, convert_to_array($fpoptions)));
}
}
/**
* Extends the user preferences page
*
* @param navigation_node $usersetting
* @param stdClass $user
* @param context_user $usercontext
* @param stdClass $course
* @param context_course $coursecontext
*/
function editor_marklar_extend_navigation_user_settings(navigation_node $usersetting, $user, context_user $usercontext,
$course, context_course $coursecontext) {
global $CFG;
// Check if the user's preferred editor is Marklar.
$preference = get_user_preferences('htmleditor', null, $user);
// If the user's preferred editor is "default", check if it is Marklar.
if (empty($preference) and !empty($CFG->texteditors)) {
$editors = explode(',', $CFG->texteditors);
if (reset($editors) === 'marklar') {
$preference = 'marklar';
}
}
// If the user's preferred editor is Marklar, show a link to Marklar preferences page.
if ($preference === 'marklar') {
$prefurl = new moodle_url('/lib/editor/marklar/preferences.php', ['userid' => $user->id]);
$usersetting->add(get_string('preferences', 'editor_marklar'), $prefurl);
}
}