weekly release 2.8dev
[moodle.git] / notes / index.php
CommitLineData
2f4a2f27 1<?php
83e70cc2 2
2f4a2f27 3/**
4 * file index.php
5 * index page to view notes.
6 * if a course id is specified then the entries from that course are shown
7 * if a user id is specified only notes related to that user are shown
8 */
9require_once('../config.php');
10require_once('lib.php');
83e70cc2 11
12/// retrieve parameters
2f4a2f27 13$courseid = optional_param('course', SITEID, PARAM_INT);
14$userid = optional_param('user', 0, PARAM_INT);
15$filtertype = optional_param('filtertype', '', PARAM_ALPHA);
16$filterselect = optional_param('filterselect', 0, PARAM_INT);
17
9dbf62d2
MG
18if (empty($CFG->enablenotes)) {
19 print_error('notesdisabled', 'notes');
20}
21
a6855934 22$url = new moodle_url('/notes/index.php');
7b2e259c 23if ($courseid != SITEID) {
2f4a2f27 24 $url->param('course', $courseid);
25}
26if ($userid !== 0) {
27 $url->param('user', $userid);
28}
2f4a2f27 29$PAGE->set_url($url);
83e70cc2 30
31/// tabs compatibility
2f4a2f27 32switch($filtertype) {
33 case 'course':
34 $courseid = $filterselect;
35 break;
36 case 'site':
37 $courseid = SITEID;
38 break;
39}
83e70cc2 40
7b5f1cd8
SH
41if (empty($courseid)) {
42 $courseid = SITEID;
43}
44
83e70cc2 45/// locate course information
74df2951 46$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
83e70cc2 47
48/// locate user information
2f4a2f27 49if ($userid) {
3406acde 50 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
2f4a2f27 51 $filtertype = 'user';
52 $filterselect = $user->id;
53
54 if ($user->deleted) {
55 echo $OUTPUT->header();
56 echo $OUTPUT->heading(get_string('userdeleted'));
57 echo $OUTPUT->footer();
58 die;
83e70cc2 59 }
60
2f4a2f27 61} else {
62 $filtertype = 'course';
63 $filterselect = $course->id;
64}
65
83e70cc2 66/// require login to access notes
2f4a2f27 67require_login($course);
83e70cc2 68/// output HTML
2f4a2f27 69if ($course->id == SITEID) {
bf0f06b1 70 $coursecontext = context_system::instance(); // SYSTEM context
2f4a2f27 71} else {
bf0f06b1 72 $coursecontext = context_course::instance($course->id); // Course context
2f4a2f27 73}
9dbf62d2 74require_capability('moodle/notes:view', $coursecontext);
bf0f06b1 75$systemcontext = context_system::instance(); // SYSTEM context
2f4a2f27 76
c4f9401c
AA
77// Trigger event.
78$event = \core\event\notes_viewed::create(array(
c4f9401c 79 'relateduserid' => $userid,
e1b16f97 80 'context' => $coursecontext
c4f9401c
AA
81));
82$event->trigger();
83
2f4a2f27 84$strnotes = get_string('notes', 'notes');
2f4a2f27 85if ($userid) {
bf0f06b1 86 $PAGE->set_context(context_user::instance($user->id));
7a7e209d
SH
87 $PAGE->navigation->extend_for_user($user);
88} else {
3406acde
SH
89 $link = null;
90 if (has_capability('moodle/course:viewparticipants', $coursecontext) || has_capability('moodle/site:viewparticipants', $systemcontext)) {
91 $link = new moodle_url('/user/index.php',array('id'=>$course->id));
92 }
2f4a2f27 93}
3406acde
SH
94
95$PAGE->set_pagelayout('course');
2f4a2f27 96$PAGE->set_title($course->shortname . ': ' . $strnotes);
97$PAGE->set_heading($course->fullname);
98
99echo $OUTPUT->header();
03d9401e
MD
100if ($userid) {
101 echo $OUTPUT->heading(fullname($user).': '.$strnotes);
102} else {
8ebbb06a 103 echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $coursecontext)).': '.$strnotes);
03d9401e 104}
2f4a2f27 105
106$strsitenotes = get_string('sitenotes', 'notes');
107$strcoursenotes = get_string('coursenotes', 'notes');
108$strpersonalnotes = get_string('personalnotes', 'notes');
109$straddnewnote = get_string('addnewnote', 'notes');
110
111echo $OUTPUT->box_start();
112
113if ($courseid != SITEID) {
114 //echo '<a href="#sitenotes">' . $strsitenotes . '</a> | <a href="#coursenotes">' . $strcoursenotes . '</a> | <a href="#personalnotes">' . $strpersonalnotes . '</a>';
bf0f06b1 115 $context = context_course::instance($courseid);
2f4a2f27 116 $addid = has_capability('moodle/notes:manage', $context) ? $courseid : 0;
117 $view = has_capability('moodle/notes:view', $context);
91d284c1 118 $fullname = format_string($course->fullname, true, array('context' => $context));
2f4a2f27 119 note_print_notes('<a name="sitenotes"></a>' . $strsitenotes, $addid, $view, 0, $userid, NOTES_STATE_SITE, 0);
91d284c1 120 note_print_notes('<a name="coursenotes"></a>' . $strcoursenotes. ' ('.$fullname.')', $addid, $view, $courseid, $userid, NOTES_STATE_PUBLIC, 0);
2f4a2f27 121 note_print_notes('<a name="personalnotes"></a>' . $strpersonalnotes, $addid, $view, $courseid, $userid, NOTES_STATE_DRAFT, $USER->id);
122
123} else { // Normal course
124 //echo '<a href="#sitenotes">' . $strsitenotes . '</a> | <a href="#coursenotes">' . $strcoursenotes . '</a>';
bf0f06b1 125 $view = has_capability('moodle/notes:view', context_system::instance());
2f4a2f27 126 note_print_notes('<a name="sitenotes"></a>' . $strsitenotes, 0, $view, 0, $userid, NOTES_STATE_SITE, 0);
127 echo '<a name="coursenotes"></a>';
128
129 if (!empty($userid)) {
df997f84 130 $courses = enrol_get_users_courses($userid);
2f4a2f27 131 foreach($courses as $c) {
bf0f06b1 132 $ccontext = context_course::instance($c->id);
91d284c1
SH
133 $cfullname = format_string($c->fullname, true, array('context' => $ccontext));
134 $header = '<a href="' . $CFG->wwwroot . '/course/view.php?id=' . $c->id . '">' . $cfullname . '</a>';
bf0f06b1 135 if (has_capability('moodle/notes:manage', context_course::instance($c->id))) {
2f4a2f27 136 $addid = $c->id;
137 } else {
138 $addid = 0;
83e70cc2 139 }
2f4a2f27 140 note_print_notes($header, $addid, $view, $c->id, $userid, NOTES_STATE_PUBLIC, 0);
83e70cc2 141 }
2f6c662f 142 }
2f4a2f27 143}
83e70cc2 144
2f4a2f27 145echo $OUTPUT->box_end();
83e70cc2 146
1d422980 147echo $OUTPUT->footer();