Skip to content

Commit

Permalink
Merge branch 'MDL-46346_blog' of https://github.com/andyjdavis/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Jul 14, 2014
2 parents d1d9e83 + 2b6e53e commit 2c3d6f2
Show file tree
Hide file tree
Showing 13 changed files with 341 additions and 276 deletions.
56 changes: 36 additions & 20 deletions blog/edit.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
Expand All @@ -25,8 +24,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(dirname(__FILE__)).'/config.php');
include_once('lib.php');
include_once('locallib.php');
require_once('lib.php');
require_once('locallib.php');

$action = required_param('action', PARAM_ALPHA);
$id = optional_param('entryid', 0, PARAM_INT);
Expand All @@ -38,9 +37,13 @@
$id = required_param('entryid', PARAM_INT);
}

$PAGE->set_url('/blog/edit.php', array('action' => $action, 'entryid' => $id, 'confirm' => $confirm, 'modid' => $modid, 'courseid' => $courseid));
$PAGE->set_url('/blog/edit.php', array('action' => $action,
'entryid' => $id,
'confirm' => $confirm,
'modid' => $modid,
'courseid' => $courseid));

// If action is add, we ignore $id to avoid any further problems
// If action is add, we ignore $id to avoid any further problems.
if (!empty($id) && $action == 'add') {
$id = null;
}
Expand All @@ -65,7 +68,7 @@
$returnurl->param('courseid', $courseid);
}

// If a modid is given, guess courseid
// If a modid is given, guess courseid.
if (!empty($modid)) {
$returnurl->param('modid', $modid);
$courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
Expand All @@ -78,7 +81,7 @@
print_error('cannoteditentryorblog');
}

// Make sure that the person trying to edit has access right
// Make sure that the person trying to edit has access right.
if ($id) {
if (!$entry = new blog_entry($id)) {
print_error('wrongentryid', 'blog');
Expand All @@ -93,7 +96,7 @@

} else {
if (!has_capability('moodle/blog:create', $sitecontext)) {
print_error('noentry', 'blog'); // manageentries is not enough for adding
print_error('noentry', 'blog'); // The capability "manageentries" is not enough for adding.
}
$entry = new stdClass();
$entry->id = null;
Expand All @@ -104,14 +107,14 @@
// Blog renderer.
$output = $PAGE->get_renderer('blog');

$strblogs = get_string('blogs','blog');
$strblogs = get_string('blogs', 'blog');

if ($action === 'delete'){
if ($action === 'delete') {
if (empty($entry->id)) {
print_error('wrongentryid', 'blog');
}
if (data_submitted() && $confirm && confirm_sesskey()) {
// Make sure the current user is the author of the blog entry, or has some deleteanyentry capability
// Make sure the current user is the author of the blog entry, or has some deleteanyentry capability.
if (!blog_user_can_edit_entry($entry)) {
print_error('nopermissionstodeleteentry', 'blog');
} else {
Expand All @@ -131,7 +134,9 @@
echo $output->render($entry);

echo '<br />';
echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'),
new moodle_url('edit.php', $optionsyes),
new moodle_url('index.php', $optionsno));
echo $OUTPUT->footer();
die;
}
Expand Down Expand Up @@ -166,24 +171,35 @@
'subdirs'=>file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id));
$attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);

$blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid'));
$blogeditform = new blog_edit_form(null, compact('entry',
'summaryoptions',
'attachmentoptions',
'sitecontext',
'courseid',
'modid'));

$entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
$entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id);
$entry = file_prepare_standard_filemanager($entry,
'attachment',
$attachmentoptions,
$sitecontext,
'blog',
'attachment',
$entry->id);

if (!empty($CFG->usetags) && !empty($entry->id)) {
include_once($CFG->dirroot.'/tag/lib.php');
$entry->tags = tag_get_tags_array('post', $entry->id);
}

$entry->action = $action;
// set defaults
// Set defaults.
$blogeditform->set_data($entry);

if ($blogeditform->is_cancelled()) {
redirect($returnurl);

} else if ($data = $blogeditform->get_data()){
} else if ($data = $blogeditform->get_data()) {

switch ($action) {
case 'add':
Expand All @@ -208,23 +224,23 @@
}


// gui setup
// GUI setup.
switch ($action) {
case 'add':
// prepare new empty form
// Prepare new empty form.
$entry->publishstate = 'site';
$strformheading = get_string('addnewentry', 'blog');
$entry->action = $action;

if ($CFG->useblogassociations) {

//pre-select the course for associations
// Pre-select the course for associations.
if ($courseid) {
$context = context_course::instance($courseid);
$entry->courseassoc = $context->id;
}

//pre-select the mod for associations
// Pre-select the mod for associations.
if ($modid) {
$context = context_module::instance($modid);
$entry->modassoc = $context->id;
Expand Down
42 changes: 30 additions & 12 deletions blog/edit_form.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
Expand All @@ -16,15 +15,18 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
}

require_once($CFG->libdir.'/formslib.php');

class blog_edit_form extends moodleform {
public $modnames = array();

function definition() {
/**
* Blog form definition.
*/
public function definition() {
global $CFG, $DB;

$mform =& $this->_form;
Expand All @@ -50,12 +52,12 @@ function definition() {

$mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'forum'), null, $attachmentoptions);

//disable publishstate options that are not allowed
// Disable publishstate options that are not allowed.
$publishstates = array();
$i = 0;

foreach (blog_entry::get_applicable_publish_states() as $state => $desc) {
$publishstates[$state] = $desc; //no maximum was set
$publishstates[$state] = $desc; // No maximum was set.
$i++;
}

Expand Down Expand Up @@ -87,7 +89,12 @@ function definition() {
}

$mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
$mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid));
$mform->addElement('advcheckbox',
'courseassoc',
get_string('associatewithcourse', 'blog', $a),
null,
null,
array(0, $contextid));
$mform->setDefault('courseassoc', $contextid);

} else if ((!empty($entry->modassoc) || !empty($modid))) {
Expand All @@ -107,7 +114,12 @@ function definition() {
}

$mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
$mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id));
$mform->addElement('advcheckbox',
'modassoc',
get_string('associatewithmodule', 'blog', $a),
null,
null,
array(0, $context->id));
$mform->setDefault('modassoc', $context->id);
}
}
Expand All @@ -130,12 +142,18 @@ function definition() {
$mform->setDefault('courseid', $courseid);
}

function validation($data, $files) {
/**
* Validate the blog form data.
* @param array $data Data to be validated
* @param array $files unused
* @return array|bool
*/
public function validation($data, $files) {
global $CFG, $DB, $USER;

$errors = array();

// validate course association
// Validate course association.
if (!empty($data['courseassoc'])) {
$coursecontext = context::instance_by_id($data['courseassoc']);

Expand All @@ -144,16 +162,16 @@ function validation($data, $files) {
}
}

// validate mod association
// Validate mod association.
if (!empty($data['modassoc'])) {
$modcontextid = $data['modassoc'];
$modcontext = context::instance_by_id($modcontextid);

if ($modcontext->contextlevel == CONTEXT_MODULE) {
// get context of the mod's course
// Get context of the mod's course.
$coursecontext = $modcontext->get_course_context(true);

// ensure only one course is associated
// Ensure only one course is associated.
if (!empty($data['courseassoc'])) {
if ($data['courseassoc'] != $coursecontext->id) {
$errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
Expand Down
17 changes: 9 additions & 8 deletions blog/external_blog_edit.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -35,7 +34,9 @@
$context = context_system::instance();
require_capability('moodle/blog:manageexternal', $context);

// TODO redirect if $CFG->useexternalblogs is off, $CFG->maxexternalblogsperuser == 0, or if user doesn't have caps to manage external blogs
// TODO redirect if $CFG->useexternalblogs is off,
// $CFG->maxexternalblogsperuser == 0,
// or if user doesn't have caps to manage external blogs.

$id = optional_param('id', null, PARAM_INT);
$url = new moodle_url('/blog/external_blog_edit.php');
Expand All @@ -52,24 +53,24 @@

$external = new stdClass();

// Check that this id exists
// Check that this id exists.
if (!empty($id) && !$DB->record_exists('blog_external', array('id' => $id))) {
print_error('wrongexternalid', 'blog');
} elseif (!empty($id)) {
} else if (!empty($id)) {
$external = $DB->get_record('blog_external', array('id' => $id));
}

$strformheading = ($action == 'edit') ? get_string('editexternalblog', 'blog') : get_string('addnewexternalblog', 'blog');
$strexternalblogs = get_string('externalblogs','blog');
$strblogs = get_string('blogs','blog');
$strexternalblogs = get_string('externalblogs', 'blog');
$strblogs = get_string('blogs', 'blog');

$externalblogform = new blog_edit_external_form();

if ($externalblogform->is_cancelled()){
if ($externalblogform->is_cancelled()) {
redirect($returnurl);

} else if ($data = $externalblogform->get_data()) {
//save stuff in db
// Save stuff in db.
switch ($action) {
case 'add':
$rss = new moodle_simplepie($data->url);
Expand Down
5 changes: 2 additions & 3 deletions blog/external_blog_edit_form.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
Expand All @@ -26,7 +25,7 @@
*/

if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
}

require_once($CFG->libdir.'/formslib.php');
Expand Down Expand Up @@ -122,7 +121,7 @@ public function definition_after_data() {
$mform->freeze('filtertags');
}
// TODO change the filtertags element to a multiple select, using the tags of the external blog
// Use $rss->get_channel_tags()
// Use $rss->get_channel_tags().
}
}
}
Loading

0 comments on commit 2c3d6f2

Please sign in to comment.