filter.php 9.85 KB
Newer Older
Thomas Fradet's avatar
Thomas Fradet committed
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
<?php

// $id$
//////////////////////////////////////////////////////////////
// 
//  This filter allows making calls to moodle.
//
//////////////////////////////////////////////////////////////
/// This is the filtering function itself.  It accepts the 
/// courseid and the text to be filtered (in HTML form).
/**
 * The iena filter plugin transforms the moodle resource links
 * into a button that opens the resource in a modal
 *
 * @package    filter_iena
 * @category   filter
 * @copyright  2018 Softia/Université lorraine
 * @author     vrignaud camille
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */


/**
 * filter_iena
 *
 *
 * @package    filter_iena
 * @copyright  2018 Softia/Université lorraine
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class filter_iena extends moodle_text_filter
{

Thomas Fradet's avatar
Thomas Fradet committed
34
	/* @var string This variable is used for delemiter */
Thomas Fradet's avatar
Thomas Fradet committed
35
	public $start = "[IENA]";
Thomas Fradet's avatar
Thomas Fradet committed
36
	/* @var string This variable is used for delemiter */
Thomas Fradet's avatar
Thomas Fradet committed
37
	public $end = "[/IENA]";
Thomas Fradet's avatar
Thomas Fradet committed
38
39
40
41
42
43
44
45
46
47
48
49
	/* @var string Contain button name, the button name is automatically the name of resource */
	public $btn_name;
	/** @var string Contain the type of button */
	public $btn_type;
	/** @var string Hex color of button is defined in settings ($CFG->color_btn) */
	public $color_btn = "#009085";
	/** @var string Hex color of button text is defined in settings ($CFG->color_btn_txt) */
	public $color_btn_txt = "#ffffff";
	/** @var string set empty, if the id does not exist then turn to "disabled" */
	public $disabled = "";
	/** @var string hide iframe nav and footer, admin setting */
	public $iena_filter_iframe = true;
Thomas Fradet's avatar
Thomas Fradet committed
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

/**
 * Get name and type of resource with the id
 * Set the $btn_name and $btn_type
 * Set $disabled (if $disabled is empty the button is active)
 * @param array $id_btn
 *
 * @return void
 */
public function filter_iena_get_info($id_btn)
{

	global $DB;
	$this->btn_name = get_string('erreur_ressource', 'filter_iena');
	$this->disabled = "";
	$id_course_modules = $id_btn[0][0];
	if ($id_course_modules) {
		$requete = $DB->get_record_sql('SELECT * FROM {course_modules} WHERE id = ?', array($id_course_modules));
		$id_instance = $requete->instance;
		$id_module = $requete->module;
		if ($id_module) {
			$modules = $DB->get_record_sql('SELECT * FROM {modules} WHERE id = ?', array($id_module));
		}
		if ($modules->name) {
			$instance = $DB->get_record_sql('SELECT * FROM {' . $modules->name . '} WHERE id = ?', array($id_instance));
		}
		if ($instance->name) {
			$this->btn_name = $instance->name;
		} else {
			$this->disabled = "disabled";
		}

		$this->btn_type = $modules->name;
	}
}

/**
 * add $start and $end on each ressource link
 * @param array $pieces
 *
 * @return array $pieces
 */
public function filter_iena_add_delimiters($pieces)
{

	global $CFG;

	for ($i = 1; $i < count($pieces); $i++) {
		// If the link is in iframe then it is not tranformer
		if ((strpos($pieces[$i], '</iframe>') !== false)) {
			$pieces[$i] = $CFG->wwwroot . '/mod' . $pieces[$i];
			continue;
		}
		if ((strpos($pieces[$i], '</a>') !== false)) {
			$pieces[$i] = $CFG->wwwroot . '/mod' . $pieces[$i];
			continue;
		}
		$pieces[$i] = $this->start . $CFG->wwwroot . '/mod' . $pieces[$i];
		$temp = strpos($pieces[$i], "id=") + 3;
		$nb_temp = '';
		while (is_numeric($pieces[$i][$temp])) {
			$nb_temp = $nb_temp . $pieces[$i][$temp];
			$temp++;
		}
		$pieces[$i] = substr_replace($pieces[$i], $nb_temp . $this->end, strpos($pieces[$i], "id=") + 3, strlen($nb_temp));
		$pieces[$i] = str_replace('&amp;iframe=true', '', $pieces[$i]);
		$pieces[$i] = str_replace('&amp;iframe=false', '', $pieces[$i]);
		$pieces[$i] = str_replace('&iframe=true', '', $pieces[$i]);
		$pieces[$i] = str_replace('&iframe=false', '', $pieces[$i]);
	}
	return $pieces;
}

/**
 * @param $text
 * @param array $options
 * @return string
 */
function filter($text, array $options = array())
{

	global $CFG;
	global $PAGE;

	if ($CFG->color_btn) {
		$this->color_btn = $CFG->color_btn;
	}
	if ($CFG->color_btn_txt) {
		$this->color_btn_txt = $CFG->color_btn_txt;
	}

Thomas Fradet's avatar
Thomas Fradet committed
141
	$PAGE->requires->js('/filter/iena/js/jquery-3.3.1.min.js');
Thomas Fradet's avatar
Thomas Fradet committed
142
	$PAGE->requires->js('/filter/iena/js/iena.js');
Thomas Fradet's avatar
Thomas Fradet committed
143
	//We hide the menus and block if the iframe parameter is a true
Thomas Fradet's avatar
Thomas Fradet committed
144
145
146
147
148
149
150
151
	if (isset($CFG->iena_filter_iframe)) {
		$this->iena_filter_iframe = $CFG->iena_filter_iframe;
	}
	if (isset($_GET['iframe']) && $this->iena_filter_iframe == "1") {
		if ($_GET['iframe'] == 'true') {
			$PAGE->requires->js('/filter/iena/js/iframe_true.js');
		}
	}
Thomas Fradet's avatar
Thomas Fradet committed
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

	preg_match_all('/<a href="(.*?)">(.*?)<\/a>/s', $text, $matches);

	for ($i = 0; $i < count($matches[0]); $i++) {
		if (strcmp($matches[1][$i], $matches[2][$i]) == 0) {
			$text = str_replace($matches[0][$i], $matches[1][$i], $text);
		}
	}

	$pieces = explode($CFG->wwwroot . '/mod', $text);
	$pieces = $this->filter_iena_add_delimiters($pieces);

	for ($i = 0; $i < count($pieces); $i++) {
		$pieces[$i] = ' ' . $pieces[$i];
		$ini = strpos($pieces[$i], $this->start);
		$ini += strlen($this->start);
		if (strlen($pieces[$i]) <= $ini) {
			continue;
		}
		$len = strpos($pieces[$i], $this->end, $ini) - $ini;
		$parsed = substr($pieces[$i], $ini, $len);
		preg_match_all('/id=[\d]*/', $parsed, $matches);
		if ($matches[0]) {
			preg_match_all('/[\d]+/', $matches[0][0], $id_btn);
		}

		if (isset($id_btn[0])) {
			$this->filter_iena_get_info($id_btn);
		}

		// $PAGE->requires->js('/filter/iena/js/iena-filter-accessibility.js');

		// $pieces[$i] = preg_replace("/(\\S+)\\[\/IENA\\]/", "<a class=\"sr-only\" target=\"_blank\" href=\"$parsed\">Ouvrir dans un nouvel onglet au lieu d'utiliser la modale : $this->btn_name.</a><button id=\"iena-modal-btn-" . $i . "\" type=\"button\" " . $this->disabled . " "
		// 	. "class=\"btn iena-filter-modal-btn \" data-toggle=\"modal\" data-target=\"#iena-modal-" . $i . "\" style=\"background-color : " . $this->color_btn . "; "
		// 	. "color : $this->color_btn_txt;border-radius:0.15rem;\">"
		// 	. "<img class=\"icon icon\" alt=\"\" src=\"" . $CFG->wwwroot . "/theme/image.php/boost/" . $this->btn_type . "/1/icon\">"
		// 	. "" . $this->btn_name . " </button>"
		// 	. "<div class=\"modal fade iena-filter-modal\" id=\"iena-modal-" . $i . "\" tabindex=\"-1\" "
		// 	. "role=\"dialog\" aria-labelledby=\"#iena-modal-btn-" . $i . "\" aria-hidden=\"true\">"
		// 	. "<div class=\"modal-dialog\" role=\"document\">"
		// 	. "<button type=\"button\" class=\"close\" data-dismiss=\"modal\" onclick=\"$('#iena-modal-" . $i . "').modal('hide');\"><span class=\"sr-only\">Fermer la modale</span><span aria-hidden=\"true\">&times;</span></button>"
		// 	. "<iframe src=\"" . $parsed . "&iframe=true\" frameborder=\"0\" allowfullscreen></iframe>"
		// 	. "</div>"
		// 	. "</div>"
		// 	, $pieces[$i]);

		$pieces[$i] = preg_replace("/(\\S+)\\[\/IENA\\]/", 
			"<div>"
Thomas Fradet's avatar
Thomas Fradet committed
200
			. "<a class=\"sr-only\" target=\"_blank\" href=\"$parsed\">Ouvrir dans un nouvel onglet au lieu d'utiliser la modale : $this->btn_name.</a><button data-iframe=\"" .$parsed. "&iframe=true\" type=\"button\" " . $this->disabled . " "
Thomas Fradet's avatar
Thomas Fradet committed
201
202
203
204
205
206
			. "class=\"btn iena-filter-modal-btn \" data-toggle=\"modal\" data-target=\"#iena-modal-" . $i . "\" style=\"background-color : " . $this->color_btn . "; "
			. "color : $this->color_btn_txt;border-radius:0.15rem;\">"
			. "<img class=\"icon icon\" alt=\"\" src=\"" . $CFG->wwwroot . "/theme/image.php/boost/" . $this->btn_type . "/1/icon\">"
			. "" . $this->btn_name . " </button>"

			. "<div class=\"modal fade iena-filter-modal\" id=\"iena-modal-" . $i . "\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"#iena-modal-btn-" . $i . "\" aria-hidden=\"true\">"
Thomas Fradet's avatar
Thomas Fradet committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
			. "<div class=\"modal-dialog\" role=\"document\">"
			. '<div class="modal-content">'
			. '<div class="modal-header">'
			. '<h5 class="modal-title" id="iena-modal-btn-'.$i.'">'.$this->btn_name.'</h5>'
			. '<button type="button" class="close" data-dismiss="modal" aria-label="Close">
			<span aria-hidden="true">&times;</span>
			</button>'
			. "</div>"
			. '<div class="modal-body">'
			// . "<iframe src=\"" . $parsed . "&iframe=true\" frameborder=\"0\" allowfullscreen></iframe>"
			. "<iframe src=\"\" frameborder=\"0\" allowfullscreen></iframe>"
			. "</div>"
			. "</div>"
			. "</div>"
Thomas Fradet's avatar
Thomas Fradet committed
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
			. "</div>"
			
			. "</div>"

			, $pieces[$i]);

	}
	return implode($pieces);
}

}


// $pieces[$i] = preg_replace("/(\\S+)\\[\/IENA\\]/", "<button id=\"iena-modal-btn-" . $i . "\" type=\"button\" " . $this->disabled . " "
// 					. "class=\"btn \" data-toggle=\"modal\" data-target=\"#iena-modal-" . $i . "\" style=\"background-color : " . $this->color_btn . "; "
// 					. "color : $this->color_btn_txt;border-radius:0.15rem;\">"
// 					. "<img class=\"icon icon\" alt=\"\" src=\"" . $CFG->wwwroot . "/theme/image.php/boost/" . $this->btn_type . "/1/icon\">"
// 					. "" . $this->btn_name . " </button>"
// 					. "<div class=\"modal fade iena-filter-modal\" id=\"iena-modal-" . $i . "\" tabindex=\"-1\" "
// 					. "role=\"dialog\" aria-labelledby=\"#iena-modal-btn-" . $i . "\" aria-hidden=\"true\">"
// 					. "<div role=\"document\" class=\"modal-dialog modal-lg\">"
// 					. "<div class=\"modal-content\" id=\"iena-modal-content\" >"
// 					. "<div class=\"modal-header\">"
// 					. "<button type=\"button\" class=\"close\" data-dismiss=\"modal\">&times;</button>"
// 					. "<h4 class=\"modal-title\">"
// 					. "<img class=\"icon icon\" alt=\"\" src=\"" . $CFG->wwwroot . "/theme/image.php/boost/" . $this->btn_type . "/1/icon\">" . $this->btn_name . "</h4>"
// 					. "</div>"
// 					. "<div class=\"modal-body\" id=\"iena-modal-body\">"
// 					. "<iframe width=\"100%\" height=\"85vh\" src=\"" . $parsed . "&iframe=true\" frameborder=\"0\" allowfullscreen></iframe>"
// 					. "</div>"
// 					. "</div>"
// 					. "</div>"
// 					. "</div>"
// 					, $pieces[$i]);