PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/edit.php

https://bitbucket.org/moodle/moodle
PHP | 296 lines | 208 code | 52 blank | 36 comment | 52 complexity | 5781f2085e0c8194bd011bec455a2200 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
  16. /**
  17. * Blog entry edit page
  18. *
  19. * @package moodlecore
  20. * @subpackage blog
  21. * @copyright 2009 Nicolas Connault
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. require_once(__DIR__ . '/../config.php');
  25. require_once($CFG->dirroot . '/blog/lib.php');
  26. require_once($CFG->dirroot . '/blog/locallib.php');
  27. require_once($CFG->dirroot . '/comment/lib.php');
  28. require_once($CFG->dirroot . '/blog/edit_form.php');
  29. $action = required_param('action', PARAM_ALPHA);
  30. $id = optional_param('entryid', 0, PARAM_INT);
  31. $confirm = optional_param('confirm', 0, PARAM_BOOL);
  32. $modid = optional_param('modid', 0, PARAM_INT); // To associate the entry with a module instance.
  33. $courseid = optional_param('courseid', 0, PARAM_INT); // To associate the entry with a course.
  34. if ($action == 'edit') {
  35. $id = required_param('entryid', PARAM_INT);
  36. }
  37. $PAGE->set_url('/blog/edit.php', array('action' => $action,
  38. 'entryid' => $id,
  39. 'confirm' => $confirm,
  40. 'modid' => $modid,
  41. 'courseid' => $courseid));
  42. // If action is add, we ignore $id to avoid any further problems.
  43. if (!empty($id) && $action == 'add') {
  44. $id = null;
  45. }
  46. $entry = new stdClass();
  47. $entry->id = null;
  48. if ($id) {
  49. if (!$entry = new blog_entry($id)) {
  50. print_error('wrongentryid', 'blog');
  51. }
  52. $userid = $entry->userid;
  53. } else {
  54. $userid = $USER->id;
  55. }
  56. $sitecontext = context_system::instance();
  57. $usercontext = context_user::instance($userid);
  58. if ($modid) {
  59. $PAGE->set_context($sitecontext);
  60. } else {
  61. $PAGE->set_context($usercontext);
  62. $blognode = $PAGE->settingsnav->find('blogadd', null);
  63. $blognode->make_active();
  64. }
  65. require_login($courseid);
  66. if (empty($CFG->enableblogs)) {
  67. print_error('blogdisable', 'blog');
  68. }
  69. if (isguestuser()) {
  70. print_error('noguest');
  71. }
  72. $returnurl = new moodle_url('/blog/index.php');
  73. if (!empty($courseid) && empty($modid)) {
  74. $returnurl->param('courseid', $courseid);
  75. }
  76. // If a modid is given, guess courseid.
  77. if (!empty($modid)) {
  78. $returnurl->param('modid', $modid);
  79. $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
  80. $returnurl->param('courseid', $courseid);
  81. }
  82. $blogheaders = blog_get_headers();
  83. if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
  84. print_error('cannoteditentryorblog');
  85. }
  86. // Make sure that the person trying to edit has access right.
  87. if ($id) {
  88. if (!blog_user_can_edit_entry($entry)) {
  89. print_error('notallowedtoedit', 'blog');
  90. }
  91. $entry->subject = clean_text($entry->subject);
  92. $entry->summary = clean_text($entry->summary, $entry->format);
  93. } else {
  94. if (!has_capability('moodle/blog:create', $sitecontext)) {
  95. print_error('noentry', 'blog'); // The capability "manageentries" is not enough for adding.
  96. }
  97. }
  98. $returnurl->param('userid', $userid);
  99. // Blog renderer.
  100. $output = $PAGE->get_renderer('blog');
  101. $strblogs = get_string('blogs', 'blog');
  102. if ($action === 'delete') {
  103. // Init comment JS strings.
  104. comment::init();
  105. if (empty($entry->id)) {
  106. print_error('wrongentryid', 'blog');
  107. }
  108. if (data_submitted() && $confirm && confirm_sesskey()) {
  109. // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability.
  110. if (!blog_user_can_edit_entry($entry)) {
  111. print_error('nopermissionstodeleteentry', 'blog');
  112. } else {
  113. $entry->delete();
  114. blog_rss_delete_file($userid);
  115. redirect($returnurl);
  116. }
  117. } else if (blog_user_can_edit_entry($entry)) {
  118. $optionsyes = array('entryid' => $id,
  119. 'action' => 'delete',
  120. 'confirm' => 1,
  121. 'sesskey' => sesskey(),
  122. 'courseid' => $courseid);
  123. $optionsno = array('userid' => $entry->userid, 'courseid' => $courseid);
  124. $PAGE->set_title("$SITE->shortname: $strblogs");
  125. $PAGE->set_heading($SITE->fullname);
  126. echo $OUTPUT->header();
  127. // Output edit mode title.
  128. echo $OUTPUT->heading($strblogs . ': ' . get_string('deleteentry', 'blog'), 2);
  129. echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog', format_string($entry->subject)),
  130. new moodle_url('edit.php', $optionsyes),
  131. new moodle_url('index.php', $optionsno));
  132. echo '<br />';
  133. // Output the entry.
  134. $entry->prepare_render();
  135. echo $output->render($entry);
  136. echo $OUTPUT->footer();
  137. die;
  138. }
  139. } else if ($action == 'add') {
  140. $editmodetitle = $strblogs . ': ' . get_string('addnewentry', 'blog');
  141. $PAGE->set_title("$SITE->shortname: $editmodetitle");
  142. $PAGE->set_heading(fullname($USER));
  143. } else if ($action == 'edit') {
  144. $editmodetitle = $strblogs . ': ' . get_string('editentry', 'blog');
  145. $PAGE->set_title("$SITE->shortname: $editmodetitle");
  146. $PAGE->set_heading(fullname($USER));
  147. }
  148. if (!empty($entry->id)) {
  149. if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
  150. foreach ($blogassociations as $assocrec) {
  151. $context = context::instance_by_id($assocrec->contextid);
  152. switch ($context->contextlevel) {
  153. case CONTEXT_COURSE:
  154. $entry->courseassoc = $assocrec->contextid;
  155. break;
  156. case CONTEXT_MODULE:
  157. $entry->modassoc = $assocrec->contextid;
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. $summaryoptions = array('maxfiles' => 99, 'maxbytes' => $CFG->maxbytes, 'trusttext' => true, 'context' => $sitecontext,
  164. 'subdirs' => file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id));
  165. $attachmentoptions = array('subdirs' => false, 'maxfiles' => 99, 'maxbytes' => $CFG->maxbytes);
  166. $blogeditform = new blog_edit_form(null, compact('entry',
  167. 'summaryoptions',
  168. 'attachmentoptions',
  169. 'sitecontext',
  170. 'courseid',
  171. 'modid'));
  172. $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
  173. $entry = file_prepare_standard_filemanager($entry,
  174. 'attachment',
  175. $attachmentoptions,
  176. $sitecontext,
  177. 'blog',
  178. 'attachment',
  179. $entry->id);
  180. if (!empty($entry->id)) {
  181. $entry->tags = core_tag_tag::get_item_tags_array('core', 'post', $entry->id);
  182. }
  183. $entry->action = $action;
  184. // Set defaults.
  185. $blogeditform->set_data($entry);
  186. if ($blogeditform->is_cancelled()) {
  187. redirect($returnurl);
  188. } else if ($data = $blogeditform->get_data()) {
  189. switch ($action) {
  190. case 'add':
  191. $blogentry = new blog_entry(null, $data, $blogeditform);
  192. $blogentry->add();
  193. $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
  194. break;
  195. case 'edit':
  196. if (empty($entry->id)) {
  197. print_error('wrongentryid', 'blog');
  198. }
  199. $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
  200. break;
  201. default :
  202. print_error('invalidaction');
  203. }
  204. redirect($returnurl);
  205. }
  206. // GUI setup.
  207. switch ($action) {
  208. case 'add':
  209. // Prepare new empty form.
  210. $entry->publishstate = 'site';
  211. $strformheading = get_string('addnewentry', 'blog');
  212. $entry->action = $action;
  213. if ($CFG->useblogassociations) {
  214. // Pre-select the course for associations.
  215. if ($courseid) {
  216. $context = context_course::instance($courseid);
  217. $entry->courseassoc = $context->id;
  218. }
  219. // Pre-select the mod for associations.
  220. if ($modid) {
  221. $context = context_module::instance($modid);
  222. $entry->modassoc = $context->id;
  223. }
  224. }
  225. break;
  226. case 'edit':
  227. if (empty($entry->id)) {
  228. print_error('wrongentryid', 'blog');
  229. }
  230. $strformheading = get_string('updateentrywithid', 'blog');
  231. break;
  232. default :
  233. print_error('unknowaction');
  234. }
  235. $entry->modid = $modid;
  236. $entry->courseid = $courseid;
  237. echo $OUTPUT->header();
  238. // Output title for editing mode.
  239. if (isset($editmodetitle)) {
  240. echo $OUTPUT->heading($editmodetitle, 2);
  241. }
  242. $blogeditform->display();
  243. echo $OUTPUT->footer();
  244. die;