MDL-21249 improved php docs package+subpackage info in mod/lesson
[moodle.git] / mod / label / lib.php
CommitLineData
44a87f63 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/>.
2c5c3e62 17
44a87f63 18/**
19 * Library of functions and constants for module label
20 *
21 * @package mod-label
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
2c5c3e62 25
44a87f63 26/** LABEL_MAX_NAME_LENGTH = 50 */
2c5c3e62 27define("LABEL_MAX_NAME_LENGTH", 50);
28
44a87f63 29/**
30 * @uses LABEL_MAX_NAME_LENGTH
31 * @param object $label
32 * @return string
33 */
376ca347 34function get_label_name($label) {
35 $textlib = textlib_get_instance();
36
43b44d5e 37 $name = strip_tags(format_string($label->intro,true));
a38f1292 38 if ($textlib->strlen($name) > LABEL_MAX_NAME_LENGTH) {
39 $name = $textlib->substr($name, 0, LABEL_MAX_NAME_LENGTH)."...";
376ca347 40 }
41
42 if (empty($name)) {
43 // arbitrary name
c0a578c0 44 $name = get_string('modulename','label');
376ca347 45 }
46
47 return $name;
48}
44a87f63 49/**
50 * Given an object containing all the necessary data,
51 * (defined by the form in mod_form.php) this function
52 * will create a new instance and return the id number
53 * of the new instance.
54 *
55 * @global object
56 * @param object $label
57 * @return bool|int
58 */
2c5c3e62 59function label_add_instance($label) {
c18269c7 60 global $DB;
2c5c3e62 61
376ca347 62 $label->name = get_label_name($label);
2c5c3e62 63 $label->timemodified = time();
64
c18269c7 65 return $DB->insert_record("label", $label);
2c5c3e62 66}
67
44a87f63 68/**
69 * Given an object containing all the necessary data,
70 * (defined by the form in mod_form.php) this function
71 * will update an existing instance with new data.
72 *
73 * @global object
74 * @param object $label
75 * @return bool
76 */
2c5c3e62 77function label_update_instance($label) {
c18269c7 78 global $DB;
2bf5b5d4 79
376ca347 80 $label->name = get_label_name($label);
2c5c3e62 81 $label->timemodified = time();
82 $label->id = $label->instance;
83
c18269c7 84 return $DB->update_record("label", $label);
2c5c3e62 85}
86
44a87f63 87/**
88 * Given an ID of an instance of this module,
89 * this function will permanently delete the instance
90 * and any data that depends on it.
91 *
92 * @global object
93 * @param int $id
94 * @return bool
95 */
2c5c3e62 96function label_delete_instance($id) {
c18269c7 97 global $DB;
2c5c3e62 98
c18269c7 99 if (! $label = $DB->get_record("label", array("id"=>$id))) {
2c5c3e62 100 return false;
101 }
102
103 $result = true;
104
c18269c7 105 if (! $DB->delete_records("label", array("id"=>$label->id))) {
2c5c3e62 106 $result = false;
107 }
108
109 return $result;
110}
111
44a87f63 112/**
113 * Returns the users with data in one resource
114 * (NONE, but must exist on EVERY mod !!)
115 *
116 * @param int $labelid
117 */
2b9f0625 118function label_get_participants($labelid) {
2b9f0625 119
120 return false;
121}
122
f36cbf1d 123/**
124 * Given a course_module object, this function returns any
125 * "extra" information that may be needed when printing
126 * this activity in a course listing.
127 * See get_array_of_activities() in course/lib.php
44a87f63 128 *
129 * @global object
130 * @param object $coursemodule
131 * @return object|null
f36cbf1d 132 */
8dddba42 133function label_get_coursemodule_info($coursemodule) {
1b81b0a4 134 global $DB;
135
43b44d5e 136 if ($label = $DB->get_record('label', array('id'=>$coursemodule->instance), 'id, name, intro, introformat')) {
d02f1934 137 if (empty($label->name)) {
138 // label name missing, fix it
139 $label->name = "label{$label->id}";
140 $DB->set_field('label', 'name', $label->name, array('id'=>$label->id));
141 }
dd97c328 142 $info = new object();
43b44d5e 143 // no filtering hre because this info is cached and filtered later
9a9012dc
PS
144 $info->extra = format_module_intro('label', $label, $coursemodule->id, false);
145 $info->name = $label->name;
dd97c328 146 return $info;
147 } else {
148 return null;
149 }
8dddba42 150}
151
44a87f63 152/**
153 * @return array
154 */
f3221af9 155function label_get_view_actions() {
156 return array();
157}
158
44a87f63 159/**
160 * @return array
161 */
f3221af9 162function label_get_post_actions() {
163 return array();
164}
165
0b5a80a1 166/**
167 * This function is used by the reset_course_userdata function in moodlelib.
86342d63 168 *
44a87f63 169 * @param object $data the data submitted from the reset course.
0b5a80a1 170 * @return array status array
171 */
172function label_reset_userdata($data) {
173 return array();
174}
175
f432bebf 176/**
177 * Returns all other caps used in module
44a87f63 178 *
179 * @return array
f432bebf 180 */
181function lable_get_extra_capabilities() {
182 return array('moodle/site:accessallgroups');
183}
184
42f103be 185/**
44a87f63 186 * @uses FEATURE_IDNUMBER
187 * @uses FEATURE_GROUPS
188 * @uses FEATURE_GROUPINGS
189 * @uses FEATURE_GROUPMEMBERSONLY
190 * @uses FEATURE_MOD_INTRO
191 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
192 * @uses FEATURE_GRADE_HAS_GRADE
193 * @uses FEATURE_GRADE_OUTCOMES
42f103be 194 * @param string $feature FEATURE_xx constant for requested feature
44a87f63 195 * @return bool|null True if module supports feature, false if not, null if doesn't know
42f103be 196 */
197function label_supports($feature) {
198 switch($feature) {
199 case FEATURE_IDNUMBER: return false;
200 case FEATURE_GROUPS: return false;
201 case FEATURE_GROUPINGS: return false;
202 case FEATURE_GROUPMEMBERSONLY: return true;
43b44d5e 203 case FEATURE_MOD_INTRO: return true;
42f103be 204 case FEATURE_COMPLETION_TRACKS_VIEWS: return false;
205 case FEATURE_GRADE_HAS_GRADE: return false;
206 case FEATURE_GRADE_OUTCOMES: return false;
31dadc6f 207 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
f58a9e37 208 case FEATURE_BACKUP_MOODLE2: return true;
42f103be 209
210 default: return null;
211 }
212}
213