Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - https://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <https://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Prints an instance of mod_stickynotes.
19
 *
20
 * @package     mod_stickynotes
21
 * @copyright   2021 Olivier VALENTIN
22
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
require(__DIR__.'/../../config.php');
26
require_once(__DIR__.'/lib.php');
27
require_once(__DIR__.'/../../lib/outputcomponents.php');
28
global $DB, $USER;
29
 
30
// Course module id.
31
$id = optional_param('id', 0, PARAM_INT);
32
 
33
// Activity instance id.
34
$s = optional_param('s', 0, PARAM_INT);
35
 
36
$vote = optional_param('vote', 0, PARAM_INT);
37
$note = optional_param('note', 0, PARAM_INT);
38
$action = optional_param('action', 0, PARAM_RAW);
39
$lock = optional_param('lock', 0, PARAM_RAW);
40
$lockvalue = optional_param('lockvalue', 0, PARAM_RAW);
41
 
42
if ($id) {
43
    $cm = get_coursemodule_from_id('stickynotes', $id, 0, false, MUST_EXIST);
44
    $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
45
    $moduleinstance = $DB->get_record('stickynotes', array('id' => $cm->instance), '*', MUST_EXIST);
46
} else if ($s) {
47
    $moduleinstance = $DB->get_record('stickynotes', array('id' => $n), '*', MUST_EXIST);
48
    $course = $DB->get_record('course', array('id' => $moduleinstance->course), '*', MUST_EXIST);
49
    $cm = get_coursemodule_from_instance('stickynotes', $moduleinstance->id, $course->id, false, MUST_EXIST);
50
} else {
51
    throw new moodle_exception(get_string('missingidandcmid', 'mod_stickynotes'));
52
}
53
 
54
require_login($course, true, $cm);
55
$modulecontext = context_module::instance($cm->id);
56
 
57
require_capability('mod/stickynotes:view', $modulecontext);
58
 
59
$event = \mod_stickynotes\event\course_module_viewed::create(array(
60
    'objectid' => $moduleinstance->id,
61
    'context' => $modulecontext
62
));
63
$event->add_record_snapshot('course', $course);
64
$event->add_record_snapshot('stickynotes', $moduleinstance);
65
$event->trigger();
66
 
67
$PAGE->set_url('/mod/stickynotes/view.php', array('id' => $cm->id));
68
$PAGE->set_title(format_string($moduleinstance->name));
69
$PAGE->set_heading(format_string($course->fullname));
70
$PAGE->set_context($modulecontext);
71
 
72
$config = ['paths' => ['sortablejs' => $CFG->wwwroot .'/mod/stickynotes/js/sortable.min']];
73
$requirejs = 'require.config(' . json_encode($config) . ')';
74
$PAGE->requires->js_amd_inline($requirejs);
75
 
76
$PAGE->requires->js_call_amd('mod_stickynotes/dragndrop', 'init');
77
 
78
// Define some capabilities.
79
$capabilitycreatenote = false;
80
$capabilityvote = false;
81
$capabilityupdatenote = false;
82
$capabilitydeleteanynote = false;
83
$capabilitymoveallnotes = false;
84
$locknotes = false;
85
$lockvotes = false;
86
 
87
// Check capability for notes creation and votes. Two possibilities.
88
// 1) user is admin. He can always create or vote.
89
if ((has_capability('mod/stickynotes:updateanynote', $modulecontext))
90
    && (has_capability('mod/stickynotes:deleteanynote', $modulecontext))) {
91
    $capabilitycreatenote = true;
92
    $capabilityvote = true;
93
    $is_admin = 1;
94
} else if ((!is_guest($modulecontext, $USER) && isloggedin())
95
        && has_capability('mod/stickynotes:vote', $modulecontext)
96
        && has_capability('mod/stickynotes:createnote', $modulecontext)) {
97
    // 2) user is a logged student. Check if note creation and votes are not locked before giving capability.
98
    // Check notes lock.
99
    if ($moduleinstance->locknotes == 1) {
100
        $locknotes = true;
101
        $capabilitycreatenote = false;
102
    } else  {
103
        $locknotes = false;
104
        $capabilitycreatenote = true;
105
    }
106
    // Check votes lock.
107
    if ($moduleinstance->lockvotes == 1) {
108
        $capabilityvote = false;
109
        $lockvotes = true;
110
    }  else  {
111
        $capabilityvote = true;
112
        $lockvotes = false;
113
    }
114
}
115
 
116
// If user has just voted, first check capability.
117
if ($vote && !$capabilityvote) {
118
    throw new moodle_exception('cannotvote', 'stickynotes');
119
} else if ($vote && $capabilityvote) {
120
    // Check if vote is locked.
121
    if ($moduleinstance->lockvotes == 1) {
122
        throw new moodle_exception('activelockvotes', 'stickynotes');
123
    }
124
    // If vote limitation, first check if user is at max.
125
    if ($moduleinstance->limitvotes == 1) {
126
        $check = $DB->count_records('stickynotes_vote', array ('userid' => $USER->id, 'stickyid' => $cm->instance));
127
        if ($check >= $moduleinstance->maxlimitvotes && $action == 'add') {
128
            throw new moodle_exception('cannotvotelimitreached', 'stickynotes');
129
        }
130
    }
131
    // Call vote function for the Like vote type.
132
    if ($moduleinstance->votes == 1) {
133
        $dovote = stickynote_do_vote_like($USER->id, $note, $action, $cm->instance);
134
    }
135
    redirect("view.php?id=".$cm->id);
136
}
137
 
138
// If admin locks votes or notes, first check capability.
139
if ($lock && $is_admin == 0) {
140
    throw new moodle_exception('activelock', 'stickynotes');
141
} else if ($lock && $is_admin == 1) {
142
    $updatelock = update_lock($cm->instance, $lock, $lockvalue);
143
    redirect("view.php?id=".$cm->id);
144
}
145
 
146
$completion = new completion_info($course);
147
$completion->set_module_viewed($cm);
148
 
149
echo $OUTPUT->header();
150
 
151
// Start to retrieve all columns for this instance.
152
$cols = $DB->get_records('stickynotes_column', array('stickyid' => $moduleinstance->id), '', '*');
153
$allcols = array();
154
 
155
// For each columns, retrieve all notes.
156
foreach ($cols as $col) {
157
    // If user has supercapabilities, we show all notes.
158
    if ((has_capability('mod/stickynotes:updateanynote', $modulecontext))
159
            && (has_capability('mod/stickynotes:deleteanynote', $modulecontext))) {
160
                $notes = $DB->get_records('stickynotes_note', array('stickyid' => $moduleinstance->id, 'stickycolid' => $col->id),
161
                'ordernote', '*');
162
    } else {
163
        // If user hasn't capabilities, check if he can see all notes through activity parameters.
164
        if ($moduleinstance->seeallnotes == 1) {
165
            $notes = $DB->get_records('stickynotes_note', array('stickyid' => $moduleinstance->id, 'stickycolid' => $col->id),
166
            'ordernote', '*');
167
        } else {
168
            $notes = $DB->get_records('stickynotes_note', array('stickyid' => $moduleinstance->id, 'stickycolid' => $col->id, 'userid' => $USER->id),
169
            'ordernote', '*');
170
        }
171
    }
172
 
173
    $allnotes = new StdClass;
174
    $allnotes = array();
175
 
176
    // For each note, retrieve and define all necessary information.
177
    foreach ($notes as $note) {
178
        // Retrieve author of the note.
179
        $getname = $DB->get_record('user', array('id' => $note->userid));
180
        $note->fullname = $getname->lastname." ".$getname->firstname;
181
        // Count number of votes for this note.
182
        $note->totalvotes = stickynote_count_votes($note->id);
183
 
184
        // If vote is enabled, check if user has voted for this note.
185
        if ($moduleinstance->votes == '1') {
186
            $getvote = stickynote_get_vote_like($USER->id, $note->id, $moduleinstance->limitvotes,
187
                $moduleinstance->maxlimitvotes, $cm->instance);
188
            $note->myvote = $getvote['myvote'];
189
            // If max number of votes per user is reached, limit vote.
190
            if ($moduleinstance->limitvotes == 1) {
191
                $note->limitedvote = $getvote['limitedvote'];
192
            }
193
            $note->action = $getvote['action'];
194
            $note->voteurl = $CFG->wwwroot.'/mod/stickynotes/view.php?id='.$cm->id.'&vote=1&action='.$note->action.'
195
&note='.$note->id;
196
        }
197
 
198
        // Defines elements for CSS.
199
        $note->elementid = 'element'.$note->id;
200
 
201
        // Check if rotate is enabled to apply the style with or without notes random rotation.
202
        if ($moduleinstance->rotate == 1) {
203
            $note->elementidstickycontent = 'element'.$note->id.'stickycontent';
204
        } else {
205
            $note->elementidstickycontent = 'element'.$note->id.'stickynormal';
206
        }
207
        // Get background color from plugin settings.
208
        $getcolor = get_config('mod_stickynotes', $note->color);
209
        $note->backgroundcolor = $getcolor;
210
        $note->textcolor = getcontrastcolor($getcolor);
211
 
212
        // Define capabilities for edit and delete note.
213
        // If user can't update and delete everything, it's not an admin. Must check capacities for each note.
214
        if ((!has_capability('mod/stickynotes:updateanynote', $modulecontext))
215
            && (!has_capability('mod/stickynotes:deleteanynote', $modulecontext))) {
216
            // First, check if setting to move all notes is enabled, to give or not this capability.
217
            if ($moduleinstance->moveallnotes == 1) {
218
                $note->capabilitymoveallnotes = true;
219
            } else {
220
                $note->capabilitymoveallnotes = false;
221
            }
222
            // Now, check if this note belongs to user to set update and delete capabilities.
223
            if ($note->userid == $USER->id) {
224
                if (has_capability('mod/stickynotes:updateownnote', $modulecontext)) {
225
                    $note->capabilityupdatenote = true;
226
                } else {
227
                    $note->capabilityupdatenote = false;
228
                }
229
 
230
                if (has_capability('mod/stickynotes:deleteownnote', $modulecontext)) {
231
                    $note->capability_deletenote = true;
232
                } else {
233
                    $note->capability_deletenote = false;
234
                }
235
            }
236
        } else {
237
            // If admin : all edit capabilities are ok.
238
            $note->capabilityupdatenote = true;
239
            $note->capability_deletenote = true;
240
        }
241
 
242
        // Define URLs for edit and delete.
243
        $note->editnoteurl = $CFG->wwwroot . '/mod/stickynotes/note.php?id='.$id.'&edit=1&note=' . $note->id;
244
        $note->deletenoteurl = $CFG->wwwroot . '/mod/stickynotes/note.php?id='.$id.'&delete=1&note=' . $note->id;
245
 
246
        $allnotes[] = (array)$note;
247
    }
248
    $notescol = new StdClass;
249
    $notescol->columnid = $col->id;
250
    $notescol->allnotes = $allnotes;
251
    // Create URL to create note.
252
    $notescol->createnoteurl = $CFG->wwwroot . '/mod/stickynotes/note.php?id='.$id.'&create=1&col=' . $col->id;
253
    $notescol->title = $col->title;
254
 
255
    // Create urls to manage columns.
256
    $notescol->editcolumnurl = $CFG->wwwroot . '/mod/stickynotes/column.php?id='.$id.'&edit=1&col=' . $col->id;
257
    $notescol->deletecolumnurl = $CFG->wwwroot . '/mod/stickynotes/column.php?id='.$id.'&delete=1&col=' . $col->id;
258
    $allcols[] = (array)$notescol;
259
}
260
 
261
// All informations are set.
262
$all = new StdClass;
263
$all->allcols = $allcols;
264
 
265
// Check capability for manage columns and define URL to create column.
266
if (has_capability('mod/stickynotes:managecolumn', $modulecontext)) {
267
    $all->capability_managecolumn = true;
268
    $all->createcolumnurl = $CFG->wwwroot . '/mod/stickynotes/column.php?id='.$id.'&create=1';
269
}
270
$all->capabilitycreatenote = $capabilitycreatenote;
271
$all->capabilityvote = $capabilityvote;
272
$all->locknotes = $locknotes;
273
$all->lockvotes = $lockvotes;
274
 
275
// Check capability for view author and if option is enabled.
276
if (has_capability('mod/stickynotes:viewauthor', $modulecontext) && $moduleinstance->viewauthor) {
277
    $all->capability_viewauthor = true;
278
}
279
 
280
// Set if vote is enabled.
281
if ($moduleinstance->votes == '0') {
282
    $all->voteenabled = 0;
283
} else {
284
    $all->voteenabled = 1;
285
}
286
 
287
// If max limit of notes per user is enabled, check if user has reached maximum.
288
if ($moduleinstance->limitstickynotes == 1) {
289
    // If user can view author, it's a teacher ! No creation limit for teachers.
290
    if (has_capability('mod/stickynotes:viewauthor', $modulecontext)) {
291
        $all->maxnotesreached = 0;
292
    } else {
293
        $notescreated = stickynote_count_notes($USER->id, $cm->instance);
294
        if ($moduleinstance->maxstickynotes == $notescreated ) {
295
            $all->maxnotesreached = 1;
296
        }
297
    }
298
}
299
 
300
echo "<div id='descandcapt' style='margin-bottom: 1em'>";
301
 
302
// If enabled, display button to show legend.
303
if ($moduleinstance->displaystickycaption == '1') {
304
    echo '<button class="btn btn-primary" id="buttondesc" data-toggle="collapse" data-target="#displaycapt">
305
    '.get_string('buttondisplaystickycaption', 'mod_stickynotes').'</button>';
306
}
307
if ((has_capability('mod/stickynotes:updateanynote', $modulecontext))
308
    && (has_capability('mod/stickynotes:deleteanynote', $modulecontext))){
309
    echo "<div style='float: right; margin-bottom: 1em'>";
310
    if ($moduleinstance->locknotes == 0) {
311
        $url = $CFG->wwwroot.'/mod/stickynotes/view.php?id='.$cm->id.'&lock=locknotes&lockvalue=1';
312
        echo "<a href=".$url."><button class='btn btn-primary'><i class='fa fa-lock'></i> ".get_string('buttonlocknotes', 'mod_stickynotes')."</button></a>&nbsp;";
313
    } else {
314
        $url = $CFG->wwwroot.'/mod/stickynotes/view.php?id='.$cm->id.'&lock=locknotes&lockvalue=0';
315
        echo "<a href=".$url."><button class='btn btn-primary'><i class='fa fa-unlock'></i> ".get_string('buttonunlocknotes', 'mod_stickynotes')."</button>&nbsp;";
316
    }
317
    if ($moduleinstance->lockvotes == 0) {
318
        $url = $CFG->wwwroot.'/mod/stickynotes/view.php?id='.$cm->id.'&lock=lockvotes&lockvalue=1';
319
        echo "<a href=".$url."><button class='btn btn-primary'><i class='fa fa-lock'></i> ".get_string('buttonlockvotes', 'mod_stickynotes')."</button></a>";
320
    } else {
321
        $url = $CFG->wwwroot.'/mod/stickynotes/view.php?id='.$cm->id.'&lock=lockvotes&lockvalue=0';
322
        echo "<a href=".$url."><button class='btn btn-primary'><i class='fa fa-unlock'></i> ".get_string('buttonunlockvotes', 'mod_stickynotes')."</button></a>";
323
    }
324
 
325
    echo "</div>";
326
}
327
echo "</div>";
328
 
329
// This next div is for displaying isntructions and caption if necessary.
330
echo '<div style="margin-bottom: 3em;">';
331
 
332
// If enabled, display button to show legend.
333
if ($moduleinstance->displaystickycaption == '1') {
334
    // First, list the 6 colors.
335
    $configcolor = array (
336
        'color1',
337
        'color2',
338
        'color3',
339
        'color4',
340
        'color5',
341
        'color6'
342
    );
343
    // Second, retrieve colors settings for this instance.
344
    $retrievecolors = $DB->get_record('stickynotes', array('id' => $moduleinstance->id), '*', MUST_EXIST);
345
 
346
    $colorarray = array();
347
    echo '<div id="displaycapt" class="collapse">';
348
    echo '<h3>'.get_string('titledisplaystickycaption', 'mod_stickynotes').'</h3>';
349
    foreach ($configcolor as $color) {
350
        if ($retrievecolors->$color == 1) {
351
            // If a color is used in instance, design a colored square and add meaning if define.
352
            $thiscolor = "<div><div style=\"display: inline-block;width:50px;margin-bottom:5px;margin-right:10px;background-color:"
353
            .get_config('mod_stickynotes', $color)
354
            ."\">&nbsp;</div>&nbsp;";
355
            $thiscolor .= "<div style=\"display: inline-block\">".$DB->get_field('stickynotes', $color.'_meaning',
356
            array('id' => $moduleinstance->id));
357
            $thiscolor .= "<br /></div></div>";
358
            echo $thiscolor;
359
        }
360
    }
361
    echo '</div>';
362
}
363
 
364
$output = $PAGE->get_renderer('mod_stickynotes');
365
 
366
echo $output->render_notes_list($all);
367
 
368
echo $OUTPUT->footer();