Skip to content

Commit

Permalink
OUBlog: Improving import functionality #116078
Browse files Browse the repository at this point in the history
  • Loading branch information
An Pham Van authored and sammarshallou committed Nov 17, 2016
1 parent 87fbc59 commit 05c3a26
Show file tree
Hide file tree
Showing 11 changed files with 1,826 additions and 38 deletions.
7 changes: 6 additions & 1 deletion externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@ public static function get_blog_posts($blogid, $bcontextid, $selected, $inccomme
return array();
}
$selected = explode(',', $params['selected']);
$return = oublog_import_getposts($params['blogid'], $params['bcontextid'],
if ($selected[0] == 0) {
$return = oublog_import_getposts($params['blogid'], $params['bcontextid'],
$selected, $params['inccomments'], $user, true);
} else {
$return = oublog_import_getposts($params['blogid'], $params['bcontextid'],
$selected, $params['inccomments'], $user);
}
// Convert file objects into a custom known object to send.
foreach ($return as &$post) {
foreach ($post->images as &$file) {
Expand Down
99 changes: 78 additions & 21 deletions import.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,17 @@
try {
if ($remoteblogs = oublog_import_remote_call('mod_oublog_get_user_blogs',
array('username' => $USER->username))) {
$blogs = array_merge($blogs, $remoteblogs);
$blogs = array_merge($remoteblogs, $blogs);
}
} catch (moodle_exception $e) {
// Ignore fail when contacting external server, keep message for debugging.
debugging($e->getMessage());
}
// Sort coursename in alphabetical order.
usort($blogs, function($a, $b)
{
return strcasecmp($a->coursename, $b->coursename);
});
$personalblogout = '';
$blogout = '';
foreach ($blogs as $bloginfo) {
Expand All @@ -118,14 +123,20 @@
'alt' => ''));
$bloglink = '';
if ($bloginfo->numposts) {
$url = new moodle_url('/mod/oublog/import.php', $params +
array('step' => 1, 'bid' => $bloginfo->cmid));
$url = new moodle_url('/mod/oublog/import.php', $params + array('step' => 1, 'bid' => $bloginfo->cmid));
$urlimportblog = new moodle_url('/mod/oublog/import.php',
$params + array('step' => 2, 'bid' => $bloginfo->cmid, 'importall' => 'true', 'sesskey' => sesskey()));
if (isset($bloginfo->remote)) {
$url->param('remote', true);
$urlimportblog->param('remote', true);
}
$link = html_writer::link($url, $bloginfo->name);
$bloglink = html_writer::tag('li', $img . ' ' . $link . ' ' .
get_string('import_step0_numposts', 'oublog', $bloginfo->numposts));
$linkimportselectedposts = html_writer::link($url, get_string('import_step0_selected_posts', 'oublog'),
array('class' => 'oublog_link_import_selectedposts'));
$linkimportblog = html_writer::link($urlimportblog, get_string('import_step0_blog', 'oublog'),
array('class' => 'oublog_link_import_blog'));
$bloglink = html_writer::tag('li', $img . ' ' . $bloginfo->name . ' ' .
get_string('import_step0_numposts', 'oublog', $bloginfo->numposts) . ' ' .
$linkimportselectedposts . ' ' . $linkimportblog);
} else {
$bloglink = html_writer::tag('li', $img . ' ' . $bloginfo->name . ' ' .
get_string('import_step0_numposts', 'oublog', 0));
Expand Down Expand Up @@ -280,10 +291,14 @@
echo html_writer::end_tag('form');
}
} else if ($step == 2) {
// Div used to hide the 'progress' once the page gets onto 'finished'.
echo html_writer::start_div('', array('id' => 'oublog_import_progress_container'));

// Do the import, show feedback. First check access.
echo html_writer::tag('p', get_string('import_step2_inst', 'oublog'));
flush();
$bid = required_param('bid', PARAM_INT);
$importall = optional_param('importall', false, PARAM_BOOL);
if ($remote = optional_param('remote', false, PARAM_BOOL)) {
// Blog on remote server, use WS to get info.
if (!$result = oublog_import_remote_call('mod_oublog_get_blog_info',
Expand All @@ -299,30 +314,45 @@
}
require_sesskey();
// Get selected and pre-selected posts.
$preselected = explode(',', optional_param('preselected', '', PARAM_SEQUENCE));
$selected = array();
foreach ($_POST as $name => $val) {
if (strpos($name, 'post_') === 0) {
$selected[] = $val;
$preselected = explode(',', optional_param('preselected', '', PARAM_SEQUENCE));
if ($_POST) {
foreach ($_POST as $name => $val) {
if (strpos($name, 'post_') === 0) {
$selected[] = $val;
}
}
}
$selected = array_filter(array_unique(array_merge($selected, $preselected), SORT_NUMERIC));
$stepinfo = array('step' => 2, 'bid' => $bid, 'preselected' => implode(',', $selected), 'remote' => $remote);
if (empty($selected)) {
echo html_writer::tag('p', get_string('import_step2_none', 'oublog'));
echo $OUTPUT->continue_button(new moodle_url('/mod/oublog/import.php',
if ($_POST) {
if (empty($selected)) {
echo html_writer::tag('p', get_string('import_step2_none', 'oublog'));
echo $OUTPUT->continue_button(new moodle_url('/mod/oublog/import.php',
array_merge($params, $stepinfo, array('step' => 1))));
echo $OUTPUT->footer();
exit;
echo $OUTPUT->footer();
exit;
}
}

if ($remote) {
$posts = oublog_import_remote_call('mod_oublog_get_blog_posts',
if ($importall) {
$posts = oublog_import_remote_call('mod_oublog_get_blog_posts',
array('username' => $USER->username, 'blogid' => $boublogid, 'selected' => '0',
'inccomments' => $oublog->allowcomments != OUBLOG_COMMENTS_PREVENT, 'bcontextid' => $bcontextid));
} else {
$posts = oublog_import_remote_call('mod_oublog_get_blog_posts',
array('username' => $USER->username, 'blogid' => $boublogid, 'selected' => implode(',', $selected),
'inccomments' => $oublog->allowcomments != OUBLOG_COMMENTS_PREVENT, 'bcontextid' => $bcontextid));
'inccomments' => $oublog->allowcomments != OUBLOG_COMMENTS_PREVENT, 'bcontextid' => $bcontextid));
}

} else {
$posts = oublog_import_getposts($boublogid, $bcontextid, $selected,
if ($importall) {
$posts = oublog_import_getposts($boublogid, $bcontextid, $selected,
$oublog->allowcomments != OUBLOG_COMMENTS_PREVENT, $USER->id, $importall);
} else {
$posts = oublog_import_getposts($boublogid, $bcontextid, $selected,
$oublog->allowcomments != OUBLOG_COMMENTS_PREVENT, $USER->id);
}
}

if (empty($posts)) {
Expand Down Expand Up @@ -451,6 +481,10 @@
$trans->allow_commit();
$bar->update($cur, count($posts), get_string('import_step2_prog', 'oublog'));
}

// End of div 'oublog_import_progress_container'.
echo html_writer::end_div();

if (count($conflicts) != count($posts)) {
// Inform completion system, if available.
$completion = new completion_info($course);
Expand All @@ -470,8 +504,27 @@
$event = \mod_oublog\event\post_imported::create($params);
$event->trigger();

echo html_writer::tag('p', get_string('import_step2_total', 'oublog',
(count($posts) - count($conflicts))));
// Div used to show the 'result' once the page gets onto 'finished'.
echo html_writer::start_div('', array('id' => 'oublog_import_result_container'));

// When 'importing posts' in progress, hide 'result_container'.
// When it've finished, hide 'progress_container', and show 'result_container'.
$jsstep2 = 'var resultContainer = document.getElementById("oublog_import_result_container");
var progressContainer = document.getElementById("oublog_import_progress_container");
var progressBar = document.getElementsByClassName("bar")[0];
var checkProgress = setInterval(function(){ toggleProgressResult() }, 1000);
function toggleProgressResult() {
if (progressBar.getAttribute("aria-valuenow") == progressBar.getAttribute("aria-valuemax")) {
clearInterval(checkProgress);
progressContainer.style.display = "none";
resultContainer.style.display = "block";
}
}
';
$PAGE->requires->js_init_code($jsstep2, true);

echo html_writer::tag('h3', get_string('import_step2_total', 'oublog',
(count($posts) - count($conflicts))));
$continueurl = '/mod/oublog/view.php?id=' . $cm->id;
if ($oublog->global) {
$continueurl = '/mod/oublog/view.php?user=' . $USER->id;
Expand All @@ -480,13 +533,17 @@
// Enable conflicts to be ignored by resending only these.
$stepinfo['ignoreconflicts'] = true;
$stepinfo['preselected'] = implode(',', $conflicts);
$stepinfo['importall'] = false;
$url = new moodle_url('/mod/oublog/import.php', array_merge($params, $stepinfo));
$conflictimport = new single_button($url, get_string('import_step2_conflicts_submit', 'oublog'));
echo $OUTPUT->confirm(get_string('import_step2_conflicts', 'oublog', count($conflicts)),
$conflictimport, $continueurl);
} else {
echo $OUTPUT->continue_button($continueurl);
}

// End of div 'oublog_import_result_container'.
echo html_writer::end_div();
}

echo html_writer::end_div();
Expand Down
12 changes: 7 additions & 5 deletions lang/en/oublog.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,11 +534,13 @@
$string['allowimport'] = 'Enable post import';
$string['allowimport_help'] = 'Allow any user to import pages from other blog activities they have access to.';
$string['allowimport_invalid'] = 'Posts can only be imported when activity is set to individual mode.';
$string['import'] = 'Import posts';
$string['import'] = 'Import';
$string['import_notallowed'] = 'Importing posts is disabled for this {$a}.';
$string['import_step0_nonefound'] = 'You do not have access to any activities where posts can be imported from.';
$string['import_step0_inst'] = 'Select an activity to import posts from:';
$string['import_step0_inst'] = 'From the list of blogs below, you may either import the entire blog or import selected posts.';
$string['import_step0_numposts'] = '({$a} posts)';
$string['import_step0_blog'] = 'Import blog';
$string['import_step0_selected_posts'] = 'Import selected posts';
$string['import_step1_inst'] = 'Select posts to import:';
$string['import_step1_from'] = 'Import from:';
$string['import_step1_table_title'] = 'Title';
Expand All @@ -548,14 +550,14 @@
$string['import_step1_addtag'] = 'Filter by tag - {$a}';
$string['import_step1_removetag'] = 'Remove tag filter - {$a}';
$string['import_step1_include_label'] = 'Import post - {$a}';
$string['import_step1_submit'] = 'Import posts';
$string['import_step1_submit'] = 'Import';
$string['import_step1_all'] = 'Select all';
$string['import_step1_none'] = 'Select none';
$string['import_step2_inst'] = 'Importing posts:';
$string['import_step2_none'] = 'No posts selected for import.';
$string['import_step2_prog'] = 'Importing in progress';
$string['import_step2_total'] = 'Imported {$a} posts.';
$string['import_step2_conflicts'] = '{$a} posts to import were identified as conflicts with existing posts.';
$string['import_step2_total'] = '{$a} post(s) imported successfully';
$string['import_step2_conflicts'] = '{$a} post(s) to import were identified as conflicts with existing posts.';
$string['import_step2_conflicts_submit'] = 'Import conflicting posts';

// My Participation.
Expand Down
20 changes: 12 additions & 8 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5138,24 +5138,28 @@ function oublog_import_getallposts($blogid, $sort, $userid = 0, $page = 0, $tags
* @param array $selected - array of selected post ids
* @param bool $inccomments - include comments?
* @param int $userid - user id (ensures user is post author)
* @param bool $importall - indicate whether or not get all posts
* @return array posts
*/
function oublog_import_getposts($blogid, $bcontextid, $selected, $inccomments = false, $userid = 0) {
function oublog_import_getposts($blogid, $bcontextid, $selected, $inccomments = false, $userid = 0, $importall = false) {
global $DB, $USER;
if ($userid == 0) {
$userid = $USER->id;
}
list($inwhere, $sqlparams) = $DB->get_in_or_equal($selected);
$sqlwhere = "bi.userid = ? AND bi.oublogid = ? AND p.deletedby IS NULL";
$sqlparams = array();
if ($importall) {
$sqlparams = array($userid, $blogid);
} else {
list($inwhere, $params) = $DB->get_in_or_equal($selected);
$sqlwhere .= " AND p.id $inwhere";
$sqlparams = array_merge(array($userid, $blogid), $params);
}
$sql = "SELECT p.*
FROM {oublog_posts} p
INNER JOIN {oublog_instances} bi on bi.id = p.oubloginstancesid
WHERE bi.userid = ?
AND bi.oublogid = ?
AND p.deletedby IS NULL
AND p.id $inwhere
WHERE $sqlwhere
ORDER BY p.id ASC";

$sqlparams = array_merge(array($userid, $blogid), $sqlparams);
if (!$posts = $DB->get_records_sql($sql, $sqlparams)) {
return array();
}
Expand Down
17 changes: 14 additions & 3 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,6 @@ ul.oublog-accordion li .oublog_statsview_content {
margin-right: 10px;
}
/** Import Posts **/
.oublog_importpostbutton {
margin-left: .5em;
}
.oublog_import_step UL {
list-style: none;
}
Expand Down Expand Up @@ -716,3 +713,17 @@ div.tagselector_result .tagselector_result_info {
#oublog-arrowback {
margin: 1em 0;
}

.oublog_link_import_selectedposts {
padding-left: 10px;
}
.oublog_link_import_blog {
padding-left: 10px;
}

#oublog_import_result_container {
display: none;
}
#oublog_import_result_container h3 {
text-align: center;
}
Loading

0 comments on commit 05c3a26

Please sign in to comment.