-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.php
83 lines (76 loc) · 2.85 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
<?php
// This file is part of the Twitter Card local plugin for Moodle
//
// 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/>.
/**
* Library of interface functions and constants for module twittercard
*
* All the core Moodle functions, needed to allow the module to work
* integrated in Moodle should be placed here.
*
* All the twittercard specific functions, needed to implement all the module
* logic, should go to locallib.php. This will help to save some memory when
* Moodle is performing actions across all modules.
*
* @package local_twittercard
* @copyright 2017 Matteo Scaramuccia <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Add extra HTML meta elements according to the Twitter summary card specification.
*
* Give plugins an opportunity to add any head elements.
* The callback must always return a string containing valid html head content.
*
* Implemented in MDL-53978 (Moodle 3.3).
*
* @return string A string containing the Twitter card; otherwise, an empty string.
*/
function local_twittercard_before_standard_html_head() {
global $PAGE;
// Trap any catchable error.
try {
$enabled = (bool)get_config('local_twittercard', 'enabled');
if (!$enabled) {
return '';
}
[$context, $course, $cm] = get_context_info_array($PAGE->context->id);
// Sanity checks.
// 1. Do not emit the card if we're not looking at a course.
if (empty($course)) {
return '';
}
// 2. Do not emit the card if we're looking at e.g. an activity in a course.
if (!empty($cm)) {
return '';
}
// 3. Do not emit the card if we're editing the course.
if ($PAGE->user_is_editing()) {
return '';
}
// 4. Do not emit the card if we're editing the course settings.
$courseediturlpath = 'course/edit.php';
if (substr($PAGE->url->get_path(), -strlen($courseediturlpath)) === $courseediturlpath) {
return '';
}
$card = \local_twittercard\helper::create_card($context, $course);
if (!empty($card)) {
return $card;
}
} catch (Exception $e) {
// Do nothing here.
return '';
}
return '';
}