Passed
Push — master ( c87549...c35fdc )
by Julito
09:10
created

NotebookManager::updateNote()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 3
nop 1
dl 0
loc 22
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Framework\Container;
5
use Chamilo\CourseBundle\Entity\CNotebook;
6
use ChamiloSession as Session;
7
8
/**
9
 * This class provides methods for the notebook management.
10
 * Include/require it in your code to use its features.
11
 *
12
 * @author Carlos Vargas <[email protected]>, move code of main/notebook up here
13
 */
14
class NotebookManager
15
{
16
    /**
17
     * Constructor.
18
     */
19
    public function __construct()
20
    {
21
    }
22
23
    /**
24
     * a little bit of javascript to display a prettier warning when deleting a note.
25
     *
26
     * @return string
27
     *
28
     * @author Patrick Cool <[email protected]>, Ghent University, Belgium
29
     *
30
     * @version januari 2009, dokeos 1.8.6
31
     */
32
    public static function javascript_notebook()
33
    {
34
        return "<script>
35
				function confirmation (name)
36
				{
37
					if (confirm(\" ".get_lang("Are you sure you want to delete this note")." \"+ name + \" ?\"))
38
						{return true;}
39
					else
40
						{return false;}
41
				}
42
				</script>";
43
    }
44
45
    public static function saveNote(array $values, $userId = 0, $courseId = 0, $sessionId = 0)
46
    {
47
        if (!is_array($values) || empty($values['note_title'])) {
48
            return false;
49
        }
50
51
        $userId = $userId ?: api_get_user_id();
52
        $courseId = $courseId ?: api_get_course_int_id();
53
        $course = api_get_course_entity($courseId);
54
        $sessionId = $sessionId ?: api_get_session_id();
55
        $session = api_get_session_entity($sessionId);
56
57
        $notebook = new CNotebook();
58
        $notebook
59
            ->setTitle($values['note_title'])
60
            ->setDescription($values['note_comment'])
61
            ->setUserId($userId)
62
            ->addCourseLink($course, $session)
63
        ;
64
65
        $repo = Container::getNotebookRepository();
66
        $repo->getEntityManager()->persist($notebook);
67
        $repo->getEntityManager()->flush();
68
69
        return $notebook->getIid();
70
    }
71
72
    /**
73
     * @param int $notebook_id
74
     *
75
     * @return array
76
     */
77
    public static function get_note_information($notebook_id)
78
    {
79
        if (empty($notebook_id)) {
80
            return [];
81
        }
82
83
        // Database table definition
84
        $table = Database::get_course_table(TABLE_NOTEBOOK);
85
        $course_id = api_get_course_int_id();
86
        $notebook_id = (int) $notebook_id;
87
88
        $sql = "SELECT
89
                iid 		AS notebook_id,
90
                title				AS note_title,
91
                description 		AS note_comment,
92
                session_id			AS session_id
93
                FROM $table
94
                WHERE iid = '".$notebook_id."' ";
95
        $result = Database::query($sql);
96
        if (1 != Database::num_rows($result)) {
97
            return [];
98
        }
99
100
        return Database::fetch_array($result);
101
    }
102
103
    /**
104
     * @param array $values
105
     */
106
    public static function updateNote($values)
107
    {
108
        if (!is_array($values) || empty($values['note_title'])) {
109
            return false;
110
        }
111
112
        $repo = Container::getNotebookRepository();
113
        $notebook = $repo->find($values['notebook_id']);
114
115
        if (!$notebook) {
0 ignored issues
show
introduced by
$notebook is of type Chamilo\CoreBundle\Entity\ResourceInterface, thus it always evaluated to true.
Loading history...
116
            return false;
117
        }
118
119
        $notebook
120
            ->setTitle($values['note_title'])
0 ignored issues
show
Bug introduced by
The method setTitle() does not exist on Chamilo\CoreBundle\Entity\ResourceInterface. It seems like you code against a sub-type of Chamilo\CoreBundle\Entity\ResourceInterface such as Chamilo\CoreBundle\Entity\Course or Chamilo\CourseBundle\Entity\CLink or Chamilo\CourseBundle\Entity\CNotebook or Chamilo\CourseBundle\Entity\CAnnouncement or Chamilo\CourseBundle\Entity\CThematicPlan or Chamilo\CourseBundle\Entity\CQuiz or Chamilo\CourseBundle\Entity\CCourseDescription or Chamilo\CourseBundle\Entity\CGroupCategory or Chamilo\CourseBundle\Entity\CDocument or Chamilo\CourseBundle\Entity\CThematic or Chamilo\CourseBundle\Entity\CQuizQuestionCategory or Chamilo\CourseBundle\Entity\CStudentPublication or Chamilo\CourseBundle\Entity\CCalendarEvent. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

120
            ->/** @scrutinizer ignore-call */ 
121
              setTitle($values['note_title'])
Loading history...
121
            ->setDescription($values['note_comment'])
122
        ;
123
124
        $repo->getEntityManager()->persist($notebook);
125
        $repo->getEntityManager()->flush();
126
127
        return true;
128
    }
129
130
    /**
131
     * @param int $notebook_id
132
     *
133
     * @return bool
134
     */
135
    public static function delete_note($notebook_id)
136
    {
137
        $notebook_id = (int) $notebook_id;
138
139
        if (empty($notebook_id)) {
140
            return false;
141
        }
142
143
        // Database table definition
144
        $table = Database::get_course_table(TABLE_NOTEBOOK);
145
        $course_id = api_get_course_int_id();
146
147
        $sql = "DELETE FROM $table
148
                WHERE
149
                    iid='".$notebook_id."' AND
150
                    user_id = '".api_get_user_id()."'";
151
        $result = Database::query($sql);
152
        $affected_rows = Database::affected_rows($result);
153
154
        if (1 != $affected_rows) {
155
            return false;
156
        }
157
158
        // Update item_property (delete)
159
        /*api_item_property_update(
160
            api_get_course_info(),
161
            TOOL_NOTEBOOK,
162
            $notebook_id,
163
            'delete',
164
            api_get_user_id()
165
        );*/
166
167
        return true;
168
    }
169
170
    /**
171
     * Display notes.
172
     */
173
    public static function display_notes()
174
    {
175
        $sessionId = api_get_session_id();
176
        $_user = api_get_user_info();
177
        if (!isset($_GET['direction'])) {
178
            $sort_direction = 'ASC';
179
            $link_sort_direction = 'DESC';
180
        } elseif ('ASC' == $_GET['direction']) {
181
            $sort_direction = 'ASC';
182
            $link_sort_direction = 'DESC';
183
        } else {
184
            $sort_direction = 'DESC';
185
            $link_sort_direction = 'ASC';
186
        }
187
188
        // action links
189
        echo '<div class="actions">';
190
        if (!api_is_anonymous()) {
191
            if (0 == $sessionId || api_is_allowed_to_session_edit(false, true)) {
192
                echo '<a href="index.php?'.api_get_cidreq().'&action=addnote">'.
193
                    Display::return_icon('new_note.png', get_lang('Add new note in my personal notebook'), '', '32').
194
                    '</a>';
195
            }
196
        }
197
198
        echo '<a
199
            href="index.php?'.api_get_cidreq().'&action=changeview&view=creation_date&direction='.$link_sort_direction.'">'.
200
            Display::return_icon('notes_order_by_date_new.png', get_lang('Sort by date created'), '', '32').
201
            '</a>';
202
        echo '<a
203
            href="index.php?'.api_get_cidreq().'&action=changeview&view=update_date&direction='.$link_sort_direction.'">'.
204
            Display::return_icon('notes_order_by_date_mod.png', get_lang('Sort by date last modified'), '', '32').
205
            '</a>';
206
        echo '<a href="index.php?'.api_get_cidreq().'&action=changeview&view=title&direction='.$link_sort_direction.'">'.
207
            Display::return_icon('notes_order_by_title.png', get_lang('Sort by title'), '', '32').'</a>';
208
        echo '</div>';
209
210
        $notebookView = Session::read('notebook_view');
211
        if (empty($notebookView)) {
212
            $notebookView = 'creation_date';
213
        }
214
215
        if (!in_array($notebookView, ['creation_date', 'update_date', 'title'])) {
216
            Session::write('notebook_view', 'creation_date');
217
        }
218
219
        // Database table definition
220
        $table = Database::get_course_table(TABLE_NOTEBOOK);
221
        $order_by = ' ORDER BY '.$notebookView." $sort_direction ";
222
223
        // Condition for the session
224
        $condition_session = api_get_session_condition($sessionId);
225
226
        $cond_extra = 'update_date' === $notebookView ? " AND update_date <> ''" : ' ';
227
        $course_id = api_get_course_int_id();
228
229
        $sql = "SELECT * FROM $table
230
                WHERE
231
                    c_id = $course_id AND
232
                    user_id = '".api_get_user_id()."'
233
                    $condition_session
234
                    $cond_extra $order_by
235
                ";
236
        $result = Database::query($sql);
237
        while ($row = Database::fetch_array($result)) {
238
            // Validation when belongs to a session
239
            $session_img = api_get_session_image($row['session_id'], $_user['status']);
240
            $updateValue = '';
241
            if ($row['update_date'] != $row['creation_date']) {
242
                $updateValue = ', '.get_lang('Updated').': '.Display::dateToStringAgoAndLongDate($row['update_date']);
243
            }
244
245
            $actions = '<a href="'.api_get_self().'?action=editnote&notebook_id='.$row['notebook_id'].'">'.
246
                Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>';
247
            $actions .= '<a
248
                href="'.api_get_self().'?action=deletenote&notebook_id='.$row['notebook_id'].'"
249
                onclick="return confirmation(\''.$row['title'].'\');">'.
250
                Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
251
252
            echo Display::panel(
253
                $row['description'],
254
                $row['title'].$session_img.' <div class="pull-right">'.$actions.'</div>',
255
                get_lang('Creation date').': '.Display::dateToStringAgoAndLongDate($row['creation_date']).$updateValue
256
            );
257
        }
258
    }
259
}
260