-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
178 lines (153 loc) · 5.04 KB
/
locallib.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?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/>.
/**
* Informations block locallib
*
* @package block_informations
* @copyright 2024 Fondation UNIT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Create a new licence record.
*
* @param object $licence
* @return resource|moodle_exception The block's configure URL
*/
function block_informations_add_licence($licence) {
global $DB;
if ($licence->licencename && $licence->licenceurl && $licence->licenceimage) {
$id = $DB->insert_record('block_informations_licences', $licence);
if ($id) {
$url = new moodle_url('/blocks/informations/configure.php');
redirect($url);
}
}
throw new moodle_exception('errorinsertingrecord', 'block_informations');
}
/**
* Get all the configured licences.
*
* @return array of licences
*/
function block_informations_get_all_licences() {
global $DB;
$licences = $DB->get_records('block_informations_licences');
$items = array();
foreach ($licences as $licence) {
$licence->categoryname = null;
if ($licence->categoryid > 0) {
$category = \core_course_category::get($licence->categoryid);
$licence->categoryname = $category->name;
}
array_push($items, (array) $licence);
}
return $items;
}
/**
* Delete a licence.
*
* @param int $itemid
* @return resource|moodle_exception The block's configure URL
*/
function block_informations_delete_licence($itemid) {
global $DB;
$params = ['id' => $itemid];
if ($DB->delete_records('block_informations_licences', $params)) {
$redirecto = new moodle_url('/blocks/informations/configure.php');
redirect($redirecto);
}
throw new moodle_exception('errordeletingrecord', 'block_informations');
}
/**
* Get the names of the available licences.
*
* @return array of licences names
*/
function block_informations_get_licences_names() {
global $DB;
$licences = $DB->get_records('block_informations_licences');
$items = [];
foreach ($licences as $licence) {
$items[$licence->id] = $licence->licencename;
}
return $items;
}
/**
* Get the licence infos.
*
* @param integer $id
* @return object of block_informations_licences
*/
function block_informations_get_licence($id) {
global $DB;
return $DB->get_record('block_informations_licences', array('id' => $id));
}
/**
* Get a list of the categories that aren't associated with a licence.
*
* @return array of categories id and name
*/
function block_informations_get_available_categories() {
global $DB;
$categories = core_course_category::make_categories_list();
$sql = 'SELECT DISTINCT(categoryid) as id FROM {block_informations_licences} WHERE categoryid IS NOT NULL';
$usedcategoriesids = $DB->get_records_sql($sql);
foreach ($usedcategoriesids as $key => $val) {
unset($categories[$key]);
}
$availablecategories = array();
foreach ($categories as $key => $val) {
$availablecategories[] = (object) array('id' => $key, 'name' => $val);
}
return $availablecategories;
}
/**
* Retrieve a licence for the given category id.
*
* @param int $categoryid
* @return object|null $licence A record of block_informations_licences or null
*/
function block_informations_category_licence($categoryid) {
global $DB;
$sql = 'SELECT * FROM {block_informations_licences} WHERE categoryid = :categoryid';
$licence = $DB->get_record_sql($sql, array('categoryid' => $categoryid));
if (!$licence) {
$category = $DB->get_record_sql('SELECT * FROM {course_categories} WHERE id = :id', array('id' => $categoryid));
$children = $DB->get_records('course_categories', array('parent' => $categoryid), 'sortorder ASC');
if ($category->parent) {
// Search a licence for the parent category.
return block_informations_category_licence($category->parent);
}
}
return $licence;
}
/**
* Get the course category id.
*
* @param context $coursecontext
* @return int $categoryid
*/
function block_informations_get_course_category($coursecontext) {
global $DB;
$categoryid = null;
$courseid = $coursecontext->instanceid;
$course = $DB->get_record('course', array('id' => $courseid), 'id, category');
if ($course) {
$categoryid = $course->category;
}
return $categoryid;
}