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
 
31
// Declare optional parameters.
32
$edit = optional_param('edit', 0, PARAM_INT);
33
$create = optional_param('create', 0, PARAM_INT);
34
$delete = optional_param('delete', 0, PARAM_INT);
35
$col = optional_param('col', 0, PARAM_INT);
36
$confirm = optional_param('confirm', 0, PARAM_INT);
37
 
38
// These params will be passed as hidden variables later in the form.
39
$pageparams = array('edit' => $edit, 'create' => $create);
40
 
41
// Course module id.
42
$id = optional_param('id', 0, PARAM_INT);
43
 
44
// Activity instance id.
45
$s = optional_param('s', 0, PARAM_INT);
46
 
47
// Get the system context instance.
48
$systemcontext = context_system::instance();
49
 
50
if ($id) {
51
    $cm = get_coursemodule_from_id('stickynotes', $id, 0, false, MUST_EXIST);
52
    $moduleinstance = $DB->get_record('stickynotes', array('id' => $cm->instance), '*', MUST_EXIST);
53
    $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
54
}
55
 
56
$PAGE->set_url('/mod/stickynnotes/column.php', array(
57
    'edit'           => $edit,
58
    'create'         => $create,
59
));
60
 
61
require_login(0, false);
62
 
63
if (!empty($create)) {
64
    // Case 1 : manager wants to create a new column.
65
    // Retrieve the related coursemodule.
66
    if (!$cm = get_coursemodule_from_instance('stickynotes', $cm->instance)) {
67
        throw new moodle_exception('invalidcoursemodule');
68
    }
69
    // Check if the instance is part of a course.
70
    if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
71
        throw new moodle_exception('invalidcourseid');
72
    }
73
 
74
    $post = new stdClass();
75
    $post->title = '';
76
    $stickyid = $cm->instance;
77
} else if ($edit) {
78
    // Case 2 : manager wants updates column.
79
    // Retrieve the related coursemodule.
80
    if (!$cm = get_coursemodule_from_instance('stickynotes', $cm->instance)) {
81
        throw new moodle_exception('invalidcoursemodule');
82
    }
83
 
84
    // Check if the instance is part of a course.
85
    if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
86
        throw new moodle_exception('invalidcourseid');
87
    }
88
 
89
    if (!$post = $DB->get_record('stickynotes_column', array('id' => $col))) {
90
        throw new moodle_exception('cannotgetcolumn', 'stickynotes');
91
    }
92
 
93
    // Require a login and retrieve the modulecontext.
94
    require_login($course, false, $cm);
95
    $modulecontext = context_module::instance($cm->id);
96
 
97
    // Check capability.
98
    if (!has_capability('mod/stickynotes:managecolumn', $modulecontext)) {
99
        throw new moodle_exception('cannotmanagecolumn', 'stickynotes');
100
    }
101
 
102
    $post->edit = $edit;
103
    $post->course = $course->id;
104
    $post->title = $post->title;
105
    $post->col = $col;
106
} else if ($delete) {
107
    // Case 3 : manager wants to delete column.
108
 
109
    // Retrieve the related coursemodule.
110
    if (!$cm = get_coursemodule_from_instance('stickynotes', $cm->instance)) {
111
        throw new moodle_exception('invalidcoursemodule');
112
    }
113
 
114
    // Check if the instance is part of a course.
115
    if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
116
        throw new moodle_exception('invalidcourseid');
117
    }
118
 
119
    // Check if column exists.
120
    if (!$post = $DB->get_record('stickynotes_column', array('id' => $col))) {
121
        throw new moodle_exception('cannotgetcolumn', 'stickynotes');
122
    }
123
    // Require a login and retrieve the modulecontext.
124
    require_login($course, false, $cm);
125
    $modulecontext = context_module::instance($cm->id);
126
 
127
    // Check capability.
128
    if (!has_capability('mod/stickynotes:managecolumn', $modulecontext)) {
129
        throw new moodle_exception('cannotmanagecolumn', 'stickynotes');
130
    }
131
 
132
    // User has confirmed deletion : column is deleted.
133
    if (!empty($confirm) AND confirm_sesskey()) {
134
        // First, retrieve all notes in selected column.
135
        $notestodelete = $DB->get_records('stickynotes_note', array('stickyid' => $cm->instance, 'stickycolid' => $col), '', '*');
136
 
137
        // Then, get infos about column to be deleted.
138
        $coltodelete = $DB->get_record('stickynotes_column', array('id' => $col));
139
 
140
        // Third, retrieve all columns for which order rank is superior at selected column.
141
        $sql = "SELECT *
142
                FROM {stickynotes_column} snc
143
                WHERE stickyid = :instanceid AND column_order > :delcolorder";
144
        $params          = array('instanceid' => $cm->instance,
145
                                 'delcolorder' => $coltodelete->column_order);
146
        $colstochange = $DB->get_records_sql($sql, $params);
147
 
148
        // Update all superior columns : decrease rank of each superior columns by 1.
149
        foreach ($colstochange as $coltochange) {
150
            $coltochange->col = $coltochange->id;
151
            $oldorder = $coltochange->column_order;
152
            $coltochange->column_order = $oldorder - 1;
153
            update_column($coltochange);
154
        }
155
 
156
        // Then, every note in selected column is deleted.
157
        foreach ($notestodelete as $notetodelete) {
158
            $deletenote = delete_stickynote($notetodelete->id, $modulecontext, $moduleinstance, $course, $cm, $notetodelete->userid);
159
        }
160
        // Finally, delete column.
161
        $deletecolumn = delete_column($col, $modulecontext);
162
 
163
        $returnurl = "view.php?id=".$cm->id;
164
        redirect($returnurl);
165
    } else {
166
        // Shows form to confirm before delete.
167
        $modulecontext = context_module::instance($cm->id);
168
        $coursecontext = context_course::instance($course->id);
169
        $PAGE->navbar->add(get_string('deletecolumn', 'stickynotes'));
170
        $PAGE->set_title($course->shortname);
171
        $PAGE->set_heading($course->fullname);
172
 
173
        echo $OUTPUT->header();
174
        echo $OUTPUT->heading(format_string($cm->name), 2);
175
        echo $OUTPUT->confirm(get_string("deletecolumnsure", "stickynotes"),
176
                "column.php?delete=$delete&confirm=$delete&id=".$cm->id."&col=".$col,
177
                $CFG->wwwroot . '/mod/stickynotes/view.php?id=' . $cm->id);
178
    }
179
 
180
    echo $OUTPUT->footer();
181
    exit;
182
}
183
 
184
// Prepare the form.
185
// Second step: The user must be logged on properly. Must be enrolled to the course as well.
186
require_login($course, false, $cm);
187
 
188
$modulecontext = context_module::instance($cm->id);
189
$coursecontext = context_course::instance($course->id);
190
 
191
// Get column infos.
192
$postcol = empty($post->col) ? null : $post->col;
193
$posttitle = empty($post->title) ? null : $post->title;
194
 
195
$formarray = array(
196
    'id'             => $cm->id,
197
    'course'         => $course,
198
    'cm'             => $cm,
199
    'modulecontext'  => $modulecontext,
200
    'edit'           => $edit,
201
    'create'         => $create,
202
    'title'          => $posttitle,
203
    'col'            => $postcol,
204
    'stickyid'       => $cm->instance,
205
);
206
 
207
$mformcol = new form_column('column.php', $formarray, 'post');
208
 
209
$mformcol->set_data(array(
210
        'stickycolid' => $postcol,
211
        'id' => $id,
212
        'col' => $postcol,
213
    ) + $pageparams + $formarray);
214
 
215
// Is it canceled?
216
if ($mformcol->is_cancelled()) {
217
    $returnurl = "view.php?id=".$cm->id;
218
    redirect($returnurl);
219
}
220
 
221
// Is it submitted?
222
if ($fromform = $mformcol->get_data()) {
223
    // Redirect url in case of occuring errors.
224
    if (empty($SESSION->fromurl)) {
225
        $errordestination = "$CFG->wwwroot/mod/stickynotes/view.php?id=$cm->id";
226
    } else {
227
        $errordestination = $SESSION->fromurl;
228
    }
229
 
230
    // If we are updating column.
231
    if ($fromform->edit) {
232
        $fromform->userid = $USER->id;
233
        $fromform->instance = $fromform->id;
234
        $returnurl = "view.php?id=".$fromform->instance;
235
        update_column($fromform);
236
        redirect($returnurl);
237
 
238
    } else if ($fromform->create) {
239
        // Add a new column.
240
        $fromform->userid = $USER->id;
241
        $returnurl = "view.php?id=".$fromform->id;
242
        insert_column($fromform);
243
        redirect($returnurl);
244
        exit();
245
    }
246
}
247
 
248
// Initiate the page.
249
$PAGE->set_title(format_string($cm->name));
250
$PAGE->set_heading(format_string($course->fullname));
251
 
252
echo $OUTPUT->header();
253
echo $OUTPUT->heading($cm->name);
254
 
255
$mformcol->display();
256
 
257
echo $OUTPUT->footer();