Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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/>.
17
 
18
/**
19
 * Library of functions and constants for module wiki
20
 *
21
 * It contains the great majority of functions defined by Moodle
22
 * that are mandatory to develop a module.
23
 *
24
 * @package mod_wiki
25
 * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
26
 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
27
 *
28
 * @author Jordi Piguillem
29
 * @author Marc Alier
30
 * @author David Jimenez
31
 * @author Josep Arus
32
 * @author Kenneth Riba
33
 *
34
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
 
37
defined('MOODLE_INTERNAL') || die();
38
 
39
/**
40
 * Given an object containing all the necessary data,
41
 * (defined by the form in mod.html) this function
42
 * will create a new instance and return the id number
43
 * of the new instance.
44
 *
45
 * @param object $instance An object from the form in mod.html
46
 * @return int The id of the newly inserted wiki record
47
 **/
48
function wiki_add_instance($wiki) {
49
    global $DB;
50
 
51
    $wiki->timemodified = time();
52
    # May have to add extra stuff in here #
53
    if (empty($wiki->forceformat)) {
54
        $wiki->forceformat = 0;
55
    }
56
 
57
    $id = $DB->insert_record('wiki', $wiki);
58
 
59
    $completiontimeexpected = !empty($wiki->completionexpected) ? $wiki->completionexpected : null;
60
    \core_completion\api::update_completion_date_event($wiki->coursemodule, 'wiki', $id, $completiontimeexpected);
61
 
62
    return $id;
63
}
64
 
65
/**
66
 * Given an object containing all the necessary data,
67
 * (defined by the form in mod.html) this function
68
 * will update an existing instance with new data.
69
 *
70
 * @param object $instance An object from the form in mod.html
71
 * @return boolean Success/Fail
72
 **/
73
function wiki_update_instance($wiki) {
74
    global $DB;
75
 
76
    $wiki->timemodified = time();
77
    $wiki->id = $wiki->instance;
78
    if (empty($wiki->forceformat)) {
79
        $wiki->forceformat = 0;
80
    }
81
 
82
    $completiontimeexpected = !empty($wiki->completionexpected) ? $wiki->completionexpected : null;
83
    \core_completion\api::update_completion_date_event($wiki->coursemodule, 'wiki', $wiki->id, $completiontimeexpected);
84
 
85
    # May have to add extra stuff in here #
86
 
87
    return $DB->update_record('wiki', $wiki);
88
}
89
 
90
/**
91
 * Given an ID of an instance of this module,
92
 * this function will permanently delete the instance
93
 * and any data that depends on it.
94
 *
95
 * @param int $id Id of the module instance
96
 * @return boolean Success/Failure
97
 **/
98
function wiki_delete_instance($id) {
99
    global $DB;
100
 
101
    if (!$wiki = $DB->get_record('wiki', array('id' => $id))) {
102
        return false;
103
    }
104
 
105
    $result = true;
106
 
107
    # Get subwiki information #
108
    $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id));
109
 
110
    foreach ($subwikis as $subwiki) {
111
        # Get existing links, and delete them #
112
        if (!$DB->delete_records('wiki_links', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
113
            $result = false;
114
        }
115
 
116
        # Get existing pages #
117
        if ($pages = $DB->get_records('wiki_pages', array('subwikiid' => $subwiki->id))) {
118
            foreach ($pages as $page) {
119
                # Get locks, and delete them #
120
                if (!$DB->delete_records('wiki_locks', array('pageid' => $page->id), IGNORE_MISSING)) {
121
                    $result = false;
122
                }
123
 
124
                # Get versions, and delete them #
125
                if (!$DB->delete_records('wiki_versions', array('pageid' => $page->id), IGNORE_MISSING)) {
126
                    $result = false;
127
                }
128
            }
129
 
130
            # Delete pages #
131
            if (!$DB->delete_records('wiki_pages', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
132
                $result = false;
133
            }
134
        }
135
 
136
        # Get existing synonyms, and delete them #
137
        if (!$DB->delete_records('wiki_synonyms', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
138
            $result = false;
139
        }
140
 
141
        # Delete any subwikis #
142
        if (!$DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING)) {
143
            $result = false;
144
        }
145
    }
146
 
147
    $cm = get_coursemodule_from_instance('wiki', $id);
148
    \core_completion\api::update_completion_date_event($cm->id, 'wiki', $wiki->id, null);
149
 
150
    # Delete any dependent records here #
151
    if (!$DB->delete_records('wiki', array('id' => $wiki->id))) {
152
        $result = false;
153
    }
154
 
155
    return $result;
156
}
157
 
158
/**
159
 * Implements callback to reset course
160
 *
161
 * @param stdClass $data
162
 * @return boolean|array
163
 */
164
function wiki_reset_userdata($data) {
1441 ariadna 165
    global $CFG, $DB;
1 efrain 166
    require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
167
    require_once($CFG->dirroot . "/mod/wiki/locallib.php");
168
 
169
    $componentstr = get_string('modulenameplural', 'wiki');
1441 ariadna 170
    $status = [];
1 efrain 171
 
1441 ariadna 172
    // Get the wiki(s) in this course.
173
    if (!$wikis = $DB->get_records('wiki', ['course' => $data->courseid])) {
1 efrain 174
        return false;
175
    }
176
    if (empty($data->reset_wiki_comments) && empty($data->reset_wiki_tags) && empty($data->reset_wiki_pages)) {
177
        return $status;
178
    }
179
 
180
    foreach ($wikis as $wiki) {
181
        if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $data->courseid)) {
182
            continue;
183
        }
184
        $context = context_module::instance($cm->id);
185
 
186
        // Remove tags or all pages.
187
        if (!empty($data->reset_wiki_pages) || !empty($data->reset_wiki_tags)) {
188
 
189
            // Get subwiki information.
190
            $subwikis = wiki_get_subwikis($wiki->id);
191
 
192
            foreach ($subwikis as $subwiki) {
193
                // Get existing pages.
194
                if ($pages = wiki_get_page_list($subwiki->id)) {
195
                    // If the wiki page isn't selected then we are only removing tags.
196
                    if (empty($data->reset_wiki_pages)) {
197
                        // Go through each page and delete the tags.
198
                        foreach ($pages as $page) {
199
                            core_tag_tag::remove_all_item_tags('mod_wiki', 'wiki_pages', $page->id);
200
                        }
201
                    } else {
202
                        // Otherwise we are removing pages and tags.
203
                        wiki_delete_pages($context, $pages, $subwiki->id);
204
                    }
205
                }
206
                if (!empty($data->reset_wiki_pages)) {
207
                    // Delete any subwikis.
1441 ariadna 208
                    $DB->delete_records('wiki_subwikis', ['id' => $subwiki->id]);
1 efrain 209
 
210
                    // Delete any attached files.
211
                    $fs = get_file_storage();
212
                    $fs->delete_area_files($context->id, 'mod_wiki', 'attachments');
213
                }
214
            }
215
 
216
            if (!empty($data->reset_wiki_pages)) {
1441 ariadna 217
                $status[] = [
218
                    'component' => $componentstr,
219
                    'item' => get_string('deleteallpages', 'wiki'),
220
                    'error' => false,
221
                ];
1 efrain 222
            }
223
            if (!empty($data->reset_wiki_tags)) {
1441 ariadna 224
                $status[] = [
225
                    'component' => $componentstr,
226
                    'item' => get_string('removeallwikitags', 'wiki'),
227
                    'error' => false,
228
                ];
1 efrain 229
            }
230
        }
231
 
232
        // Remove all comments.
233
        if (!empty($data->reset_wiki_comments) || !empty($data->reset_wiki_pages)) {
1441 ariadna 234
            $DB->delete_records_select('comments', "contextid = ? AND commentarea='wiki_page'", [$context->id]);
1 efrain 235
            if (!empty($data->reset_wiki_comments)) {
1441 ariadna 236
                $status[] = [
237
                    'component' => $componentstr,
238
                    'item' => get_string('deleteallcomments'),
239
                    'error' => false,
240
                ];
1 efrain 241
            }
242
        }
243
    }
244
 
245
    // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
246
    // See MDL-9367.
1441 ariadna 247
    shift_course_mod_dates('wiki', ['editbegin', 'editend'], $data->timeshift, $data->courseid);
248
    $status[] = [
249
        'component' => $componentstr,
250
        'item' => get_string('date'),
251
        'error' => false,
252
    ];
1 efrain 253
 
254
    return $status;
255
}
256
 
257
 
258
function wiki_reset_course_form_definition(&$mform) {
259
    $mform->addElement('header', 'wikiheader', get_string('modulenameplural', 'wiki'));
1441 ariadna 260
    $mform->addElement('static', 'wikidelete', get_string('delete'));
1 efrain 261
    $mform->addElement('advcheckbox', 'reset_wiki_pages', get_string('deleteallpages', 'wiki'));
262
    $mform->addElement('advcheckbox', 'reset_wiki_tags', get_string('removeallwikitags', 'wiki'));
263
    $mform->addElement('advcheckbox', 'reset_wiki_comments', get_string('deleteallcomments'));
264
}
265
 
266
/**
267
 * Indicates API features that the wiki supports.
268
 *
269
 * @uses FEATURE_GROUPS
270
 * @uses FEATURE_GROUPINGS
271
 * @uses FEATURE_MOD_INTRO
272
 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
273
 * @uses FEATURE_COMPLETION_HAS_RULES
274
 * @uses FEATURE_GRADE_HAS_GRADE
275
 * @uses FEATURE_GRADE_OUTCOMES
276
 * @param string $feature
277
 * @return mixed True if module supports feature, false if not, null if doesn't know or string for the module purpose.
278
 */
279
function wiki_supports($feature) {
280
    switch ($feature) {
281
    case FEATURE_GROUPS:
282
        return true;
283
    case FEATURE_GROUPINGS:
284
        return true;
285
    case FEATURE_MOD_INTRO:
286
        return true;
287
    case FEATURE_COMPLETION_TRACKS_VIEWS:
288
        return true;
289
    case FEATURE_GRADE_HAS_GRADE:
290
        return false;
291
    case FEATURE_GRADE_OUTCOMES:
292
        return false;
293
    case FEATURE_RATE:
294
        return false;
295
    case FEATURE_BACKUP_MOODLE2:
296
        return true;
297
    case FEATURE_SHOW_DESCRIPTION:
298
        return true;
299
    case FEATURE_COMMENT:
300
        return true;
301
    case FEATURE_MOD_PURPOSE:
302
        return MOD_PURPOSE_COLLABORATION;
303
 
304
    default:
305
        return null;
306
    }
307
}
308
 
309
/**
310
 * Given a course and a time, this module should find recent activity
311
 * that has occurred in wiki activities and print it out.
312
 * Return true if there was output, or false is there was none.
313
 *
314
 * @global $CFG
315
 * @global $DB
316
 * @uses CONTEXT_MODULE
317
 * @uses VISIBLEGROUPS
318
 * @param object $course
319
 * @param bool $viewfullnames capability
320
 * @param int $timestart
321
 * @return boolean
322
 **/
323
function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
324
    global $CFG, $DB, $OUTPUT;
325
 
326
    $sql = "SELECT p.id, p.timemodified, p.subwikiid, sw.wikiid, w.wikimode, sw.userid, sw.groupid
327
            FROM {wiki_pages} p
328
                JOIN {wiki_subwikis} sw ON sw.id = p.subwikiid
329
                JOIN {wiki} w ON w.id = sw.wikiid
330
            WHERE p.timemodified > ? AND w.course = ?
331
            ORDER BY p.timemodified ASC";
332
    if (!$pages = $DB->get_records_sql($sql, array($timestart, $course->id))) {
333
        return false;
334
    }
335
    require_once($CFG->dirroot . "/mod/wiki/locallib.php");
336
 
337
    $wikis = array();
338
 
339
    $modinfo = get_fast_modinfo($course);
340
 
341
    $subwikivisible = array();
342
    foreach ($pages as $page) {
343
        if (!isset($subwikivisible[$page->subwikiid])) {
344
            $subwiki = (object)array('id' => $page->subwikiid, 'wikiid' => $page->wikiid,
345
                'groupid' => $page->groupid, 'userid' => $page->userid);
346
            $wiki = (object)array('id' => $page->wikiid, 'course' => $course->id, 'wikimode' => $page->wikimode);
347
            $subwikivisible[$page->subwikiid] = wiki_user_can_view($subwiki, $wiki);
348
        }
349
        if ($subwikivisible[$page->subwikiid]) {
350
            $wikis[] = $page;
351
        }
352
    }
353
    unset($subwikivisible);
354
    unset($pages);
355
 
356
    if (!$wikis) {
357
        return false;
358
    }
359
    echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 6);
360
    foreach ($wikis as $wiki) {
361
        $cm = $modinfo->instances['wiki'][$wiki->wikiid];
362
        $link = $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $wiki->id;
363
        print_recent_activity_note($wiki->timemodified, $wiki, $cm->name, $link, false, $viewfullnames);
364
    }
365
 
366
    return true; //  True if anything was printed, otherwise false
367
}
368
 
369
/**
370
 * Must return an array of grades for a given instance of this module,
371
 * indexed by user.  It also returns a maximum allowed grade.
372
 *
373
 * Example:
374
 *    $return->grades = array of grades;
375
 *    $return->maxgrade = maximum allowed grade;
376
 *
377
 *    return $return;
378
 *
379
 * @param int $wikiid ID of an instance of this module
380
 * @return mixed Null or object with an array of grades and with the maximum grade
381
 **/
382
function wiki_grades($wikiid) {
383
    return null;
384
}
385
 
386
/**
387
 * Checks if scale is being used by any instance of wiki.
388
 * This function was added in 1.9
389
 *
390
 * This is used to find out if scale used anywhere
391
 * @param $scaleid int
392
 * @return boolean True if the scale is used by any wiki
393
 */
394
function wiki_scale_used_anywhere($scaleid) {
395
    global $DB;
396
 
397
    //if ($scaleid and $DB->record_exists('wiki', array('grade' => -$scaleid))) {
398
    //    return true;
399
    //} else {
400
    //    return false;
401
    //}
402
 
403
    return false;
404
}
405
 
406
/**
407
 * file serving callback
408
 *
409
 * @copyright Josep Arus
410
 * @package  mod_wiki
411
 * @category files
412
 * @param stdClass $course course object
413
 * @param stdClass $cm course module object
414
 * @param stdClass $context context object
415
 * @param string $filearea file area
416
 * @param array $args extra arguments
417
 * @param bool $forcedownload whether or not force download
418
 * @param array $options additional options affecting the file serving
419
 * @return bool false if the file was not found, just send the file otherwise and do not return anything
420
 */
421
function wiki_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
422
    global $CFG;
423
 
424
    if ($context->contextlevel != CONTEXT_MODULE) {
425
        return false;
426
    }
427
 
428
    require_login($course, true, $cm);
429
 
430
    require_once($CFG->dirroot . "/mod/wiki/locallib.php");
431
 
432
    if ($filearea == 'attachments') {
433
        $swid = (int) array_shift($args);
434
 
435
        if (!$subwiki = wiki_get_subwiki($swid)) {
436
            return false;
437
        }
438
 
439
        require_capability('mod/wiki:viewpage', $context);
440
 
441
        $relativepath = implode('/', $args);
442
 
443
        $fullpath = "/$context->id/mod_wiki/attachments/$swid/$relativepath";
444
 
445
        $fs = get_file_storage();
446
        if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
447
            return false;
448
        }
449
 
450
        send_stored_file($file, null, 0, $options);
451
    }
452
}
453
 
454
/**
455
 * Search for wiki
456
 *
457
 * @param stdClass $cm course module object
458
 * @param string $search searchword.
459
 * @param stdClass $subwiki Optional Subwiki.
460
 * @return Search wiki input form
461
 */
462
function wiki_search_form($cm, $search = '', $subwiki = null) {
463
    global $OUTPUT;
464
 
465
    $hiddenfields = [
466
        (object) ['type' => 'hidden', 'name' => 'courseid', 'value' => $cm->course],
467
        (object) ['type' => 'hidden', 'name' => 'cmid', 'value' => $cm->id],
468
        (object) ['type' => 'hidden', 'name' => 'searchwikicontent', 'value' => 1],
469
    ];
470
    if (!empty($subwiki->id)) {
471
        $hiddenfields[] = (object) ['type' => 'hidden', 'name' => 'subwikiid', 'value' => $subwiki->id];
472
    }
473
    $data = [
474
        'action' => new moodle_url('/mod/wiki/search.php'),
475
        'hiddenfields' => $hiddenfields,
476
        'inputname' => 'searchstring',
477
        'query' => s($search, true),
478
        'searchstring' => get_string('searchwikis', 'wiki'),
479
        'extraclasses' => 'mt-2'
480
    ];
481
    return $OUTPUT->render_from_template('core/search_input', $data);
482
}
483
 
484
/**
485
 * Extends the global navigation tree by adding wiki nodes if there is a relevant content
486
 *
487
 * This can be called by an AJAX request so do not rely on $PAGE as it might not be set up properly.
488
 *
489
 * @param navigation_node $navref An object representing the navigation tree node of the workshop module instance
490
 * @param stdClass $course the course object
491
 * @param stdClass $instance the activity record object
492
 * @param cm_info $cm the course module object
493
 */
494
function wiki_extend_navigation(navigation_node $navref, stdClass $course, stdClass $instance, cm_info $cm) {
495
    global $CFG, $USER;
496
 
497
    require_once($CFG->dirroot . '/mod/wiki/locallib.php');
498
 
499
    $context = context_module::instance($cm->id);
500
    $userid = ($instance->wikimode == 'individual') ? $USER->id : 0;
501
    $gid = groups_get_activity_group($cm) ?: 0;
502
 
503
    if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)) {
504
        return;
505
    }
506
 
507
    $pageid = optional_param('pageid', null, PARAM_INT);
508
    if (empty($pageid)) {
509
        // wiki main page
510
        $page = wiki_get_page_by_title($subwiki->id, $instance->firstpagetitle);
511
        $pageid = $page->id;
512
    }
513
 
514
    if (wiki_can_create_pages($context)) {
515
        $link = new moodle_url('/mod/wiki/create.php', ['action' => 'new', 'swid' => $subwiki->id]);
516
        $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING);
517
    }
518
 
519
    if (empty($pageid)) {
520
        return;
521
    }
522
 
523
    $canviewpage = has_capability('mod/wiki:viewpage', $context);
524
 
525
    if ($canviewpage) {
526
        $link = new moodle_url('/mod/wiki/view.php', ['pageid' => $pageid]);
527
        $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING);
528
    }
529
 
530
    if (wiki_user_can_edit($subwiki)) {
531
        $link = new moodle_url('/mod/wiki/edit.php', ['pageid' => $pageid]);
532
        $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING);
533
    }
534
 
535
    if (has_capability('mod/wiki:viewcomment', $context)) {
536
        $link = new moodle_url('/mod/wiki/comments.php', ['pageid' => $pageid]);
537
        $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING);
538
    }
539
 
540
    if ($canviewpage) {
541
        $link = new moodle_url('/mod/wiki/history.php', ['pageid' => $pageid]);
542
        $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING);
543
 
544
        $link = new moodle_url('/mod/wiki/map.php', ['pageid' => $pageid]);
545
        $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING);
546
 
547
        $link = new moodle_url('/mod/wiki/files.php', ['pageid' => $pageid]);
548
        $navref->add(get_string('files', 'wiki'), $link, navigation_node::TYPE_SETTING);
549
    }
550
 
551
    if (has_capability('mod/wiki:managewiki', $context)) {
552
        $link = new moodle_url('/mod/wiki/admin.php', ['pageid' => $pageid]);
553
        $navref->add(get_string('admin', 'wiki'), $link, navigation_node::TYPE_SETTING);
554
    }
555
}
556
/**
557
 * Returns all other caps used in wiki module
558
 *
559
 * @return array
560
 */
561
function wiki_get_extra_capabilities() {
562
    return array('moodle/comment:view', 'moodle/comment:post', 'moodle/comment:delete');
563
}
564
 
565
/**
566
 * Running addtional permission check on plugin, for example, plugins
567
 * may have switch to turn on/off comments option, this callback will
568
 * affect UI display, not like pluginname_comment_validate only throw
569
 * exceptions.
570
 * Capability check has been done in comment->check_permissions(), we
571
 * don't need to do it again here.
572
 *
573
 * @package  mod_wiki
574
 * @category comment
575
 *
576
 * @param stdClass $comment_param {
577
 *              context  => context the context object
578
 *              courseid => int course id
579
 *              cm       => stdClass course module object
580
 *              commentarea => string comment area
581
 *              itemid      => int itemid
582
 * }
583
 * @return array
584
 */
585
function wiki_comment_permissions($comment_param) {
586
    return array('post'=>true, 'view'=>true);
587
}
588
 
589
/**
590
 * Validate comment parameter before perform other comments actions
591
 *
592
 * @param stdClass $comment_param {
593
 *              context  => context the context object
594
 *              courseid => int course id
595
 *              cm       => stdClass course module object
596
 *              commentarea => string comment area
597
 *              itemid      => int itemid
598
 * }
599
 *
600
 * @package  mod_wiki
601
 * @category comment
602
 *
603
 * @return boolean
604
 */
605
function wiki_comment_validate($comment_param) {
606
    global $DB, $CFG;
607
    require_once($CFG->dirroot . '/mod/wiki/locallib.php');
608
    // validate comment area
609
    if ($comment_param->commentarea != 'wiki_page') {
610
        throw new comment_exception('invalidcommentarea');
611
    }
612
    // validate itemid
613
    if (!$record = $DB->get_record('wiki_pages', array('id'=>$comment_param->itemid))) {
614
        throw new comment_exception('invalidcommentitemid');
615
    }
616
    if (!$subwiki = wiki_get_subwiki($record->subwikiid)) {
617
        throw new comment_exception('invalidsubwikiid');
618
    }
619
    if (!$wiki = wiki_get_wiki_from_pageid($comment_param->itemid)) {
620
        throw new comment_exception('invalidid', 'data');
621
    }
622
    if (!$course = $DB->get_record('course', array('id'=>$wiki->course))) {
623
        throw new comment_exception('coursemisconf');
624
    }
625
    if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $course->id)) {
626
        throw new comment_exception('invalidcoursemodule');
627
    }
628
    $context = context_module::instance($cm->id);
629
    // group access
630
    if ($subwiki->groupid) {
631
        $groupmode = groups_get_activity_groupmode($cm, $course);
632
        if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
633
            if (!groups_is_member($subwiki->groupid)) {
634
                throw new comment_exception('notmemberofgroup');
635
            }
636
        }
637
    }
638
    // validate context id
639
    if ($context->id != $comment_param->context->id) {
640
        throw new comment_exception('invalidcontext');
641
    }
642
    // validation for comment deletion
643
    if (!empty($comment_param->commentid)) {
644
        if ($comment = $DB->get_record('comments', array('id'=>$comment_param->commentid))) {
645
            if ($comment->commentarea != 'wiki_page') {
646
                throw new comment_exception('invalidcommentarea');
647
            }
648
            if ($comment->contextid != $context->id) {
649
                throw new comment_exception('invalidcontext');
650
            }
651
            if ($comment->itemid != $comment_param->itemid) {
652
                throw new comment_exception('invalidcommentitemid');
653
            }
654
        } else {
655
            throw new comment_exception('invalidcommentid');
656
        }
657
    }
658
    return true;
659
}
660
 
661
/**
662
 * Return a list of page types
663
 * @param string $pagetype current page type
664
 * @param stdClass $parentcontext Block's parent context
665
 * @param stdClass $currentcontext Current context of block
666
 */
667
function wiki_page_type_list($pagetype, $parentcontext, $currentcontext) {
668
    $module_pagetype = array(
669
        'mod-wiki-*'=>get_string('page-mod-wiki-x', 'wiki'),
670
        'mod-wiki-view'=>get_string('page-mod-wiki-view', 'wiki'),
671
        'mod-wiki-comments'=>get_string('page-mod-wiki-comments', 'wiki'),
672
        'mod-wiki-history'=>get_string('page-mod-wiki-history', 'wiki'),
673
        'mod-wiki-map'=>get_string('page-mod-wiki-map', 'wiki')
674
    );
675
    return $module_pagetype;
676
}
677
 
678
/**
679
 * Mark the activity completed (if required) and trigger the course_module_viewed event.
680
 *
681
 * @param  stdClass $wiki       Wiki object.
682
 * @param  stdClass $course     Course object.
683
 * @param  stdClass $cm         Course module object.
684
 * @param  stdClass $context    Context object.
685
 * @since Moodle 3.1
686
 */
687
function wiki_view($wiki, $course, $cm, $context) {
688
    // Trigger course_module_viewed event.
689
    $params = array(
690
        'context' => $context,
691
        'objectid' => $wiki->id
692
    );
693
    $event = \mod_wiki\event\course_module_viewed::create($params);
694
    $event->add_record_snapshot('course_modules', $cm);
695
    $event->add_record_snapshot('course', $course);
696
    $event->add_record_snapshot('wiki', $wiki);
697
    $event->trigger();
698
 
699
    // Completion.
700
    $completion = new completion_info($course);
701
    $completion->set_module_viewed($cm);
702
}
703
 
704
/**
705
 * Mark the activity completed (if required) and trigger the page_viewed event.
706
 *
707
 * @param  stdClass $wiki       Wiki object.
708
 * @param  stdClass $page       Page object.
709
 * @param  stdClass $course     Course object.
710
 * @param  stdClass $cm         Course module object.
711
 * @param  stdClass $context    Context object.
712
 * @param  int $uid             Optional User ID.
713
 * @param  array $other         Optional Other params: title, wiki ID, group ID, groupanduser, prettyview.
714
 * @param  stdClass $subwiki    Optional Subwiki.
715
 * @since Moodle 3.1
716
 */
717
function wiki_page_view($wiki, $page, $course, $cm, $context, $uid = null, $other = null, $subwiki = null) {
718
 
719
    // Trigger course_module_viewed event.
720
    $params = array(
721
        'context' => $context,
722
        'objectid' => $page->id
723
    );
724
    if ($uid != null) {
725
        $params['relateduserid'] = $uid;
726
    }
727
    if ($other != null) {
728
        $params['other'] = $other;
729
    }
730
 
731
    $event = \mod_wiki\event\page_viewed::create($params);
732
 
733
    $event->add_record_snapshot('wiki_pages', $page);
734
    $event->add_record_snapshot('course_modules', $cm);
735
    $event->add_record_snapshot('course', $course);
736
    $event->add_record_snapshot('wiki', $wiki);
737
    if ($subwiki != null) {
738
        $event->add_record_snapshot('wiki_subwikis', $subwiki);
739
    }
740
    $event->trigger();
741
 
742
    // Completion.
743
    $completion = new completion_info($course);
744
    $completion->set_module_viewed($cm);
745
}
746
 
747
/**
748
 * Check if the module has any update that affects the current user since a given time.
749
 *
750
 * @param  cm_info $cm course module data
751
 * @param  int $from the time to check updates from
752
 * @param  array $filter  if we need to check only specific updates
753
 * @return stdClass an object with the different type of areas indicating if they were updated or not
754
 * @since Moodle 3.2
755
 */
756
function wiki_check_updates_since(cm_info $cm, $from, $filter = array()) {
757
    global $DB, $CFG;
758
    require_once($CFG->dirroot . '/mod/wiki/locallib.php');
759
 
760
    $updates = new stdClass();
761
    if (!has_capability('mod/wiki:viewpage', $cm->context)) {
762
        return $updates;
763
    }
764
    $updates = course_check_module_updates_since($cm, $from, array('attachments'), $filter);
765
 
766
    // Check only pages updated in subwikis the user can access.
767
    $updates->pages = (object) array('updated' => false);
768
    $wiki = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST);
769
    if ($subwikis = wiki_get_visible_subwikis($wiki, $cm, $cm->context)) {
770
        $subwikisids = array();
771
        foreach ($subwikis as $subwiki) {
772
            $subwikisids[] = $subwiki->id;
773
        }
774
        list($subwikissql, $params) = $DB->get_in_or_equal($subwikisids, SQL_PARAMS_NAMED);
775
        $select = 'subwikiid ' . $subwikissql . ' AND (timemodified > :since1 OR timecreated > :since2)';
776
        $params['since1'] = $from;
777
        $params['since2'] = $from;
778
        $pages = $DB->get_records_select('wiki_pages', $select, $params, '', 'id');
779
        if (!empty($pages)) {
780
            $updates->pages->updated = true;
781
            $updates->pages->itemids = array_keys($pages);
782
        }
783
    }
784
    return $updates;
785
}
786
 
787
/**
788
 * Get icon mapping for font-awesome.
789
 */
790
function mod_wiki_get_fontawesome_icon_map() {
791
    return [
792
        'mod_wiki:attachment' => 'fa-paperclip',
793
    ];
794
}
795
 
796
/**
797
 * This function receives a calendar event and returns the action associated with it, or null if there is none.
798
 *
799
 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
800
 * is not displayed on the block.
801
 *
802
 * @param calendar_event $event
803
 * @param \core_calendar\action_factory $factory
804
 * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
805
 * @return \core_calendar\local\event\entities\action_interface|null
806
 */
807
function mod_wiki_core_calendar_provide_event_action(calendar_event $event,
808
                                                    \core_calendar\action_factory $factory,
809
                                                    int $userid = 0) {
810
    global $USER;
811
 
812
    if (!$userid) {
813
        $userid = $USER->id;
814
    }
815
 
816
    $cm = get_fast_modinfo($event->courseid, $userid)->instances['wiki'][$event->instance];
817
 
818
    if (!$cm->uservisible) {
819
        // The module is not visible to the user for any reason.
820
        return null;
821
    }
822
 
823
    $completion = new \completion_info($cm->get_course());
824
 
825
    $completiondata = $completion->get_data($cm, false, $userid);
826
 
827
    if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
828
        return null;
829
    }
830
 
831
    return $factory->create_instance(
832
        get_string('view'),
833
        new \moodle_url('/mod/wiki/view.php', ['id' => $cm->id]),
834
        1,
835
        true
836
    );
837
}
838
 
839
/**
840
 * Sets dynamic information about a course module
841
 *
842
 * This callback is called from cm_info when checking module availability (incl. $cm->uservisible)
843
 *
844
 * Main viewing capability in mod_wiki is 'mod/wiki:viewpage' instead of the expected standardised 'mod/wiki:view'.
845
 * The method cm_info::is_user_access_restricted_by_capability() does not work for wiki, we need to implement
846
 * this callback.
847
 *
848
 * @param cm_info $cm
849
 */
850
function wiki_cm_info_dynamic(cm_info $cm) {
851
    if (!has_capability('mod/wiki:viewpage', $cm->context, $cm->get_modinfo()->get_user_id())) {
852
        $cm->set_available(false);
853
    }
854
}