Test Setup Failed
Push — master ( ec638a...cb9435 )
by Julito
51:10
created

NotebookManager::get_note_information()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * 	This class provides methods for the notebook management.
6
 * 	Include/require it in your code to use its features.
7
 * 	@author Carlos Vargas <[email protected]>, move code of main/notebook up here
8
 * 	@package chamilo.library
9
 */
10
class NotebookManager
11
{
12
    /**
13
     * Constructor
14
     */
15
    public function __construct()
16
    {
17
    }
18
19
    /**
20
     * a little bit of javascript to display a prettier warning when deleting a note
21
     *
22
     * @return string
23
     *
24
     * @author Patrick Cool <[email protected]>, Ghent University, Belgium
25
     * @version januari 2009, dokeos 1.8.6
26
     */
27
    public static function javascript_notebook()
28
    {
29
        return "<script>
30
				function confirmation (name)
31
				{
32
					if (confirm(\" " . get_lang("NoteConfirmDelete") . " \"+ name + \" ?\"))
33
						{return true;}
34
					else
35
						{return false;}
36
				}
37
				</script>";
38
    }
39
40
    /**
41
     * This functions stores the note in the database
42
     *
43
     * @param array $values
44
     * @param int $userId Optional. The user ID
45
     * @param int $courseId Optional. The course ID
46
     * @param int $sessionId Optional. The session ID
47
     * @return bool
48
     * @author Christian Fasanando <[email protected]>
49
     * @author Patrick Cool <[email protected]>, Ghent University, Belgium
50
     * @version januari 2009, dokeos 1.8.6
51
     */
52
    public static function save_note($values, $userId = 0, $courseId = 0, $sessionId = 0)
53
    {
54
        if (!is_array($values) || empty($values['note_title'])) {
55
            return false;
56
        }
57
58
        // Database table definition
59
        $table = Database::get_course_table(TABLE_NOTEBOOK);
60
        $userId = $userId ?: api_get_user_id();
61
        $courseId = $courseId ?: api_get_course_int_id();
62
        $courseInfo = api_get_course_info_by_id($courseId);
63
        $courseCode = $courseInfo['code'];
64
        $sessionId = $sessionId ?: api_get_session_id();
65
        $now = api_get_utc_datetime();
66
        $params = [
67
            'notebook_id' => 0,
68
            'c_id' => $courseId,
69
            'user_id' => $userId,
70
            'course' => $courseCode,
71
            'session_id' => $sessionId,
72
            'title' => $values['note_title'],
73
            'description' => $values['note_comment'],
74
            'creation_date' => $now,
75
            'update_date' => $now,
76
            'status' => 0
77
        ];
78
        $id = Database::insert($table, $params);
79
80 View Code Duplication
        if ($id > 0) {
81
            $sql = "UPDATE $table SET notebook_id = $id WHERE iid = $id";
82
            Database::query($sql);
83
84
            //insert into item_property
85
            api_item_property_update(
86
                $courseInfo,
87
                TOOL_NOTEBOOK,
88
                $id,
89
                'NotebookAdded',
90
                $userId
91
            );
92
93
            return $id;
94
        }
95
    }
96
97
    /**
98
     * @param int $notebook_id
99
     * @return array|mixed
100
     */
101
    public static function get_note_information($notebook_id)
102
    {
103
        if (empty($notebook_id)) {
104
            return array();
105
        }
106
107
        // Database table definition
108
        $t_notebook = Database::get_course_table(TABLE_NOTEBOOK);
109
        $course_id = api_get_course_int_id();
110
111
        $sql = "SELECT
112
                notebook_id 		AS notebook_id,
113
                title				AS note_title,
114
                description 		AS note_comment,
115
                session_id			AS session_id
116
               FROM $t_notebook
117
               WHERE c_id = $course_id AND notebook_id = '" . intval($notebook_id) . "' ";
118
        $result = Database::query($sql);
119
        if (Database::num_rows($result) != 1) {
120
            return array();
121
        }
122
123
        return Database::fetch_array($result);
124
    }
125
126
    /**
127
     * This functions updates the note in the database
128
     *
129
     * @param array $values
130
     *
131
     * @author Christian Fasanando <[email protected]>
132
     * @author Patrick Cool <[email protected]>, Ghent University, Belgium
133
     * @version januari 2009, dokeos 1.8.6
134
     */
135
    public static function update_note($values)
136
    {
137
        if (!is_array($values) or empty($values['note_title'])) {
138
            return false;
139
        }
140
        // Database table definition
141
        $table = Database::get_course_table(TABLE_NOTEBOOK);
142
143
        $course_id = api_get_course_int_id();
144
        $sessionId = api_get_session_id();
145
146
        $params = [
147
            'user_id' => api_get_user_id(),
148
            'course' => api_get_course_id(),
149
            'session_id' => $sessionId,
150
            'title' => $values['note_title'],
151
            'description' => $values['note_comment'],
152
            'update_date' => api_get_utc_datetime()
153
        ];
154
155
        Database::update(
156
            $table,
157
            $params,
158
            [
159
                'c_id = ? AND notebook_id = ?' => [
160
                    $course_id,
161
                    $values['notebook_id']
162
                ],
163
            ]
164
        );
165
166
        // update item_property (update)
167
        api_item_property_update(
168
            api_get_course_info(),
169
            TOOL_NOTEBOOK,
170
            $values['notebook_id'],
171
            'NotebookUpdated',
172
            api_get_user_id()
173
        );
174
175
        return true;
176
    }
177
178
    /**
179
     * @param int $notebook_id
180
     * @return bool
181
     */
182
    public static function delete_note($notebook_id)
183
    {
184
        if (empty($notebook_id) || $notebook_id != strval(intval($notebook_id))) {
185
            return false;
186
        }
187
188
        // Database table definition
189
        $t_notebook = Database::get_course_table(TABLE_NOTEBOOK);
190
191
        $course_id = api_get_course_int_id();
192
193
        $sql = "DELETE FROM $t_notebook
194
                WHERE
195
                    c_id = $course_id AND
196
                    notebook_id='" . intval($notebook_id) . "' AND
197
                    user_id = '" . api_get_user_id() . "'";
198
        $result = Database::query($sql);
199
        $affected_rows = Database::affected_rows($result);
200
        if ($affected_rows != 1) {
201
            return false;
202
        }
203
204
        //update item_property (delete)
205
        api_item_property_update(
206
            api_get_course_info(),
207
            TOOL_NOTEBOOK,
208
            intval($notebook_id),
209
            'delete',
210
            api_get_user_id()
211
        );
212
        return true;
213
    }
214
215
    /**
216
     * Display notes
217
     */
218
    public static function display_notes()
219
    {
220
        $_user = api_get_user_info();
221
        if (!isset($_GET['direction'])) {
222
            $sort_direction = 'ASC';
223
            $link_sort_direction = 'DESC';
224
        } elseif ($_GET['direction'] == 'ASC') {
225
            $sort_direction = 'ASC';
226
            $link_sort_direction = 'DESC';
227
        } else {
228
            $sort_direction = 'DESC';
229
            $link_sort_direction = 'ASC';
230
        }
231
232
        // action links
233
        echo '<div class="actions">';
234
        if (!api_is_anonymous()) {
235 View Code Duplication
            if (api_get_session_id() == 0) {
236
                echo '<a href="index.php?'.api_get_cidreq().'&action=addnote">'.
237
                    Display::return_icon(
238
                        'new_note.png',
239
                        get_lang('NoteAddNew'),
240
                        '',
241
                        '32'
242
                    ).'</a>';
243
            } elseif (api_is_allowed_to_session_edit(false, true)) {
244
                echo '<a href="index.php?' . api_get_cidreq() . '&action=addnote">' .
245
                    Display::return_icon('new_note.png', get_lang('NoteAddNew'), '', '32') . '</a>';
246
            }
247
        } else {
248
            echo '<a href="javascript:void(0)">' .
249
                Display::return_icon('new_note.png', get_lang('NoteAddNew'), '', '32') . '</a>';
250
        }
251
252
        echo '<a href="index.php?' . api_get_cidreq() . '&action=changeview&view=creation_date&direction=' . $link_sort_direction . '">' .
253
            Display::return_icon('notes_order_by_date_new.png', get_lang('OrderByCreationDate'), '', '32') . '</a>';
254
        echo '<a href="index.php?' . api_get_cidreq() . '&action=changeview&view=update_date&direction=' . $link_sort_direction . '">' .
255
            Display::return_icon('notes_order_by_date_mod.png', get_lang('OrderByModificationDate'), '', '32') . '</a>';
256
        echo '<a href="index.php?' . api_get_cidreq() . '&action=changeview&view=title&direction=' . $link_sort_direction . '">' .
257
            Display::return_icon('notes_order_by_title.png', get_lang('OrderByTitle'), '', '32') . '</a>';
258
        echo '</div>';
259
260
        if (!isset($_SESSION['notebook_view']) ||
261
            !in_array($_SESSION['notebook_view'], array('creation_date', 'update_date', 'title'))
262
        ) {
263
            $_SESSION['notebook_view'] = 'creation_date';
264
        }
265
266
        // Database table definition
267
        $t_notebook = Database::get_course_table(TABLE_NOTEBOOK);
268
        if ($_SESSION['notebook_view'] == 'creation_date' || $_SESSION['notebook_view'] == 'update_date') {
269
            $order_by = " ORDER BY " . $_SESSION['notebook_view'] . " $sort_direction ";
270
        } else {
271
            $order_by = " ORDER BY " . $_SESSION['notebook_view'] . " $sort_direction ";
272
        }
273
274
        //condition for the session
275
        $session_id = api_get_session_id();
276
        $condition_session = api_get_session_condition($session_id);
277
278
        $cond_extra = ($_SESSION['notebook_view'] == 'update_date') ? " AND update_date <> ''" : " ";
279
        $course_id = api_get_course_int_id();
280
281
        $sql = "SELECT * FROM $t_notebook
282
                WHERE
283
                    c_id = $course_id AND
284
                    user_id = '" . api_get_user_id() . "'
285
                    $condition_session
286
                    $cond_extra $order_by
287
                ";
288
        $result = Database::query($sql);
289
        while ($row = Database::fetch_array($result)) {
290
            // Validation when belongs to a session
291
            $session_img = api_get_session_image($row['session_id'], $_user['status']);
292
            $updateValue = '';
293
            if ($row['update_date'] <> $row['creation_date']) {
294
                $updateValue = ', ' . get_lang('UpdateDate') . ': ' . Display::dateToStringAgoAndLongDate($row['update_date']);
295
            }
296
297
            $actions = '<a href="' . api_get_self() . '?action=editnote&notebook_id=' . $row['notebook_id'] . '">' .
298
                Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL) . '</a>';
299
            $actions .= '<a href="' . api_get_self() . '?action=deletenote&notebook_id=' . $row['notebook_id'] . '" onclick="return confirmation(\'' . $row['title'] . '\');">' .
300
                Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL) . '</a>';
301
302
            echo Display::panel(
303
                $row['description'],
304
                $row['title'] . $session_img.' <div class="pull-right">'.$actions.'</div>',
305
                get_lang('CreationDate') . ': ' . Display::dateToStringAgoAndLongDate($row['creation_date']). $updateValue
306
            );
307
        }
308
    }
309
}
310