lots of cleanup on user submitted transcripts

This commit is contained in:
John Bintz 2009-09-28 21:54:02 -04:00
parent 387d1136cb
commit 11f152a2ec
9 changed files with 225 additions and 49 deletions

View File

@ -77,6 +77,26 @@ class WDTSTranscriptManager {
return false; return false;
} }
function update_transcript($info) {
if (isset($info['key'])) {
if (($transcripts = $this->_get_transcripts_metadata()) !== false) {
$new_transcripts = array();
foreach ($transcripts as $transcript) {
if ($transcript['key'] == $info['key']) {
foreach (array('transcript') as $field) {
if (isset($info[$field])) {
$transcript[$field] = $info[$field];
}
}
}
$new_transcripts[] = $transcript;
}
return update_post_meta($this->post_id, $this->key, $new_transcripts);
}
}
}
function _update_search_field($transcripts) { function _update_search_field($transcripts) {
if (!empty($this->search_key)) { if (!empty($this->search_key)) {
$search_lines = array(); $search_lines = array();
@ -136,6 +156,13 @@ class WDTSTranscriptManager {
return $user_transcripts; return $user_transcripts;
} }
function get_transcript_by_key($key) {
foreach ($this->get_transcripts() as $transcript) {
if ($transcript['key'] == $key) { return $transcript; }
}
return false;
}
function get_languages() { function get_languages() {
$languages = array(); $languages = array();
if (($transcripts = $this->_get_transcripts_metadata()) !== false) { if (($transcripts = $this->_get_transcripts_metadata()) !== false) {

View File

@ -72,7 +72,18 @@ class WhatDidTheySayAdmin {
if ($options['automatic_embedding']) { if ($options['automatic_embedding']) {
add_filter('the_content', array(&$this, 'the_content_automatic_embedding'), 15); add_filter('the_content', array(&$this, 'the_content_automatic_embedding'), 15);
} }
foreach (get_class_methods($this) as $method) {
if (strpos($method, "shortcode_") === 0) {
$shortcode_name = str_replace("_", "-", str_replace("shortcode_", "", $method));
add_shortcode($shortcode_name, array(&$this, $method));
}
}
add_filter('filter_shortcode_dialog', array(&$this, 'filter_shortcode_dialog'), 10, 4);
add_filter('filter_shortcode_scene_action', array(&$this, 'filter_shortcode_scene_action'), 10, 2);
add_filter('filter_shortcode_scene_heading', array(&$this, 'filter_shortcode_scene_heading'), 10, 2);
if (isset($_REQUEST['wdts'])) { if (isset($_REQUEST['wdts'])) {
if (isset($_REQUEST['wdts']['_nonce'])) { if (isset($_REQUEST['wdts']['_nonce'])) {
if (wp_verify_nonce($_REQUEST['wdts']['_nonce'], 'what-did-they-say')) { if (wp_verify_nonce($_REQUEST['wdts']['_nonce'], 'what-did-they-say')) {
@ -137,19 +148,9 @@ class WhatDidTheySayAdmin {
if (current_user_can('submit_transcriptions')) { if (current_user_can('submit_transcriptions')) {
wp_enqueue_script('scriptaculous-effects'); wp_enqueue_script('scriptaculous-effects');
wp_enqueue_script('edit-transcript', plugin_dir_url(dirname(__FILE__)) . 'js/edit-transcripts.js', array('scriptaculous-effects'), false, true); wp_enqueue_script('edit-transcript', plugin_dir_url(dirname(__FILE__)) . 'js/edit-transcripts.js', array('scriptaculous-effects'), false, true);
wp_enqueue_style('wdts-admin', plugin_dir_url(dirname(__FILE__)) . 'css/wdts-admin.css');
} }
foreach (get_class_methods($this) as $method) {
if (strpos($method, "shortcode_") === 0) {
$shortcode_name = str_replace("_", "-", str_replace("shortcode_", "", $method));
add_shortcode($shortcode_name, array(&$this, $method));
}
}
add_filter('filter_shortcode_dialog', array(&$this, 'filter_shortcode_dialog'), 10, 4);
add_filter('filter_shortcode_scene_action', array(&$this, 'filter_shortcode_scene_action'), 10, 2);
add_filter('filter_shortcode_scene_heading', array(&$this, 'filter_shortcode_scene_heading'), 10, 2);
$options = get_option('what-did-they-say-options'); $options = get_option('what-did-they-say-options');
if (!empty($options['load_default_styles'])) { if (!empty($options['load_default_styles'])) {
wp_enqueue_style('wdts-defaults', plugin_dir_url(dirname(__FILE__)) . 'css/wdts-defaults.css'); wp_enqueue_style('wdts-defaults', plugin_dir_url(dirname(__FILE__)) . 'css/wdts-defaults.css');
@ -353,6 +354,27 @@ class WhatDidTheySayAdmin {
return $updated; return $updated;
} }
function handle_update_update_queued_transcript($info) {
$this->is_ajax = true;
if ($this->current_user_can_edit_this_transcript($info['post_id'], $info['key'])) {
$queued_transcript_manager = new WDTSQueuedTranscript($info['post_id']);
$queued_transcript_manager->update_transcript($info);
$post = get_post($info['post_id']);
$language_options = new WDTSLanguageOptions();
$transcript_options = new WDTSTranscriptOptions($post->ID);
$transcript = $queued_transcript_manager->get_transcript_by_key($info['key']);
$user = wp_get_current_user();
include(dirname(__FILE__) . '/partials/_queued-transcript-contents.inc');
return;
}
header('HTTP/1.1 401 Unauthorized');
}
/** /**
* Handle updates to post transcripts. * Handle updates to post transcripts.
* @param array $info The part of the $_POST array for What Did They Say?!? * @param array $info The part of the $_POST array for What Did They Say?!?
@ -412,13 +434,33 @@ class WhatDidTheySayAdmin {
header('HTTP/1.1 401 Unauthorized'); header('HTTP/1.1 401 Unauthorized');
} }
function current_user_can_edit_this_transcript($post_id, $transcript_id) {
if (current_user_can('approve_transcriptions')) {
$ok = true;
} else {
$ok = false;
if (current_user_can('submit_transcriptions')) {
$queued_transcript_manager = new WDTSQueuedTranscript($post_id);
$user = wp_get_current_user();
$transcripts = $queued_transcript_manager->get_transcripts_for_user($user->ID);
foreach ($transcripts as $transcript) {
if ($transcript['key'] == $transcript_id) {
$ok = true; break;
}
}
}
}
return $ok;
}
/** /**
* Handle transcript deletion. * Handle transcript deletion.
*/ */
function handle_update_delete_transcript($info) { function handle_update_delete_transcript($info) {
$this->is_ajax = true; $this->is_ajax = true;
if (current_user_can('approve_transcriptions')) { if ($this->current_user_can_edit_this_transcript($info['post_id'], $info['key'])) {
$queued_transcript_manager = new WDTSQueuedTranscript($info['post_id']); $queued_transcript_manager = new WDTSQueuedTranscript($info['post_id']);
if (($transcript = $queued_transcript_manager->delete_transcript_by_key($info['key'])) !== false) { if (($transcript = $queued_transcript_manager->delete_transcript_by_key($info['key'])) !== false) {

View File

@ -0,0 +1,3 @@
<div class="queued-transcription-holder">
<?php include(dirname(__FILE__) . '/_queued-transcript-contents.inc') ?>
</div>

View File

@ -0,0 +1,19 @@
<input type="hidden" name="wdts[language]" value="<?php echo $transcript['language'] ?>" />
<input type="hidden" name="wdts[post_id]" value="<?php echo $post->ID ?>" />
<input type="hidden" name="wdts[key]" value="<?php echo $transcript['key'] ?>" />
<p><?php
printf(
__('From <strong>%s</strong> in <strong>%s</strong>:', 'what-did-they-say'),
$user->display_name,
$language_options->get_language_name($transcript['language'])
)
?></p>
<div class="queued-transcription transcript"><?php echo do_shortcode($transcript['transcript']) ?></div>
<div class="queued-transcription-raw" style="display:none"><?php echo $transcript['transcript'] ?></div>
<div class="transcript-action-links">
<?php if (current_user_can('approve_transcriptions')) { ?>
<a href="#" class="approve-transcript button">Approve</a> |
<?php } ?>
<a href="#" class="delete-transcript button">Delete</a> |
<a href="#" class="edit-transcript button">Edit</a>
</div>

View File

@ -9,29 +9,12 @@
</p> </p>
<?php if (is_array($queued_transcripts) && !empty($queued_transcripts)) { ?> <?php if (is_array($queued_transcripts) && !empty($queued_transcripts)) { ?>
<p><strong><?php _e('Manage queued transcripts:', 'what-did-they-say') ?></strong></p> <p><strong><?php _e('Manage queued transcripts:', 'what-did-they-say') ?></strong></p>
<?php foreach ($queued_transcripts as $transcript) { <?php
$user = get_userdata($transcript['user_id']); foreach ($queued_transcripts as $transcript) {
if (!empty($user)) { ?> $user = get_userdata($transcript['user_id']);
if (!empty($user)) { include('_display-queued-transcript.inc'); }
<div class="queued-transcription-holder"> }
<input type="hidden" name="wdts[language]" value="<?php echo $transcript['language'] ?>" /> ?>
<input type="hidden" name="wdts[key]" value="<?php echo $transcript['key'] ?>" />
<p><?php
printf(
__('From <strong>%s</strong> in <strong>%s</strong>:', 'what-did-they-say'),
$user->display_name,
$language_options->get_language_name($transcript['language'])
)
?></p>
<div class="queued-transcription transcript"><?php echo do_shortcode($transcript['transcript']) ?></div>
<div class="queued-transcription-raw" style="display:none"><?php echo $transcript['transcript'] ?></div>
<div style="padding: 10px 0">
<a href="#" class="approve-transcript button">Approve</a>
<a href="#" class="delete-transcript button">Delete</a>
</div>
</div>
<?php } ?>
<?php } ?>
<?php } ?> <?php } ?>
<p> <p>
<label> <label>
@ -44,9 +27,9 @@
</label> </label>
<div id="wdts-shorttags"> <div id="wdts-shorttags">
<button class="wdts-create" id="wdts-scene-heading">Scene Heading</button> <button class="wdts-create" id="wdts-scene-heading" onclick="return false">Scene Heading</button>
<button class="wdts-create" id="wdts-scene-action">Scene Action</button> <button class="wdts-create" id="wdts-scene-action" onclick="return false">Scene Action</button>
<button class="wdts-create" id="wdts-dialog">Dialog</button> <button class="wdts-create" id="wdts-dialog" onclick="return false">Dialog</button>
<?php foreach (array_keys($options['languages']) as $code) { <?php foreach (array_keys($options['languages']) as $code) {
$approved_transcript_text = ''; $approved_transcript_text = '';

View File

@ -64,4 +64,8 @@
#wdts-page-introduction label { #wdts-page-introduction label {
display: inline display: inline
}
.transcript-action-links {
padding-top: 0.4em
} }

View File

@ -103,7 +103,7 @@ Event.observe(window, 'load', function() {
}); });
}); });
$$('.approve-transcript').each(function(b) { function wdts_setup_approve_transcript_clicker(b) {
b.observe('click', function(e) { b.observe('click', function(e) {
Event.stop(e); Event.stop(e);
var lang = b.parentNode.parentNode.select("input[name*=[language]]").shift(); var lang = b.parentNode.parentNode.select("input[name*=[language]]").shift();
@ -124,7 +124,7 @@ $$('.approve-transcript').each(function(b) {
if (ok) { if (ok) {
editor.value = raw_transcript.innerHTML; editor.value = raw_transcript.innerHTML;
var p = b.parentNode.parentNode; var p = b.parentNode.parentNode;
new Ajax.Request( new Ajax.Request(
ajax_url, { ajax_url, {
'method': 'post', 'method': 'post',
@ -138,12 +138,12 @@ $$('.approve-transcript').each(function(b) {
p.update(messages.approved); p.update(messages.approved);
new Effect.Highlight(p); new Effect.Highlight(p);
var i,il; var i,il;
for (i = 0, il = language_selector.options.length; i < il; ++i) { for (i = 0, il = language_selector.options.length; i < il; ++i) {
if (language_selector.options[i].value == lang) { if (language_selector.options[i].value == lang) {
language_selector.selectedIndex = i; language_selector.selectedIndex = i;
switch_transcript(); switch_transcript();
break; break;
} }
} }
} }
@ -153,10 +153,10 @@ $$('.approve-transcript').each(function(b) {
} }
} }
}); });
}); };
$$('.delete-transcript').each(function(b) { function wdts_setup_delete_transcript_clicker(b) {
Event.observe(b, 'click', function(e) { b.observe('click', function(e) {
Event.stop(e); Event.stop(e);
if (confirm(messages.delete)) { if (confirm(messages.delete)) {
@ -166,7 +166,7 @@ $$('.delete-transcript').each(function(b) {
post_id = post_id.value; post_id = post_id.value;
key = key.value; key = key.value;
var p = b.parentNode.parentNode; var p = b.parentNode.parentNode;
new Ajax.Request( new Ajax.Request(
ajax_url, { ajax_url, {
'method': 'post', 'method': 'post',
@ -181,11 +181,67 @@ $$('.delete-transcript').each(function(b) {
new Effect.Highlight(p); new Effect.Highlight(p);
} }
} }
); );
} }
} }
}); });
}); }
function wdts_setup_edit_transcript_clicker(b) {
b.observe('click', function(e) {
Event.stop(e);
var target = b.parentNode.parentNode;
var transcript = target.select('.transcript').pop();
var textnode = new Element('textarea', { style: 'height: 200px; width: 90%' });
var action_links = target.select('.transcript-action-links').pop();
textnode.value = target.select('.queued-transcription-raw').pop().innerHTML;
b.parentNode.parentNode.insertBefore(textnode, transcript);
transcript.hide();
var post_id = target.select("input[name*=[post_id]]").shift();
var key = target.select("input[name*=[key]]").shift();
var submitter = new Element('button').update('Update Transcript');
submitter.observe('click', function(e) {
top.console.log(post_id);
top.console.log(key);
if (post_id && key) {
post_id = post_id.value;
key = key.value;
new Ajax.Updater(target, ajax_url, {
'method': 'post',
'parameters': {
'wdts[_nonce]': nonce,
'wdts[module]': 'update-queued-transcript',
'wdts[key]': key,
'wdts[post_id]': post_id,
'wdts[transcript]': textnode.value
},
'onComplete': function() {
new Effect.Highlight(target);
wdts_add_clickers(target);
}
});
}
});
b.parentNode.parentNode.appendChild(submitter);
action_links.parentNode.removeChild(action_links);
});
}
function wdts_add_clickers(p) {
top.console.log(p);
p.select('.edit-transcript').each(function(b) { top.console.log(b); wdts_setup_edit_transcript_clicker(b); });
p.select('.approve-transcript').each(function(b) { wdts_setup_approve_transcript_clicker(b); });
p.select('.delete-transcript').each(function(b) { wdts_setup_delete_transcript_clicker(b); });
}
wdts_add_clickers($$('body')[0]);
if (language_selector) { if (language_selector) {
switch_transcript(); switch_transcript();

View File

@ -191,6 +191,40 @@ class WDTSTranscriptTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(array('en', 'fr'), $this->w->get_languages()); $this->assertEquals(array('en', 'fr'), $this->w->get_languages());
} }
function testUpdateTranscript() {
update_post_meta(1, $this->w->key,
array(
array(
'language' => 'en',
'transcript' => 'this is a transcript',
'user_id' => 1,
'key' => 0
)
)
);
$info = array(
'key' => 0,
'user_id' => 1,
'transcript' => 'hiss hiss hiss',
'language' => 'fr'
);
$this->w->update_transcript($info);
$this->assertEquals(
array(
array(
'language' => 'en',
'transcript' => 'hiss hiss hiss',
'user_id' => 1,
'key' => 0
)
),
get_post_meta(1, $this->w->key, true)
);
}
} }
?> ?>

View File

@ -221,6 +221,14 @@ function the_media_transcript_queue_editor() {
<input type="submit" value="Modify Transcript" /> <input type="submit" value="Modify Transcript" />
</form> </form>
<?php } ?> <?php } ?>
<?php if ((count($queued_transcripts_for_user) > 0) && !current_user_can('approve_transcriptions')) { ?>
<h3 style="margin-top: 0.5em"><?php _e('Your Submitted Transcripts:', 'what-did-they-say') ?></h3>
<?php
foreach ($queued_transcripts_for_user as $transcript) {
include(dirname(__FILE__) . '/classes/partials/_display-queued-transcript.inc');
}
?>
<?php } ?>
<?php if (current_user_can('submit_transcriptions')) { ?> <?php if (current_user_can('submit_transcriptions')) { ?>
<?php if ($transcript_options->are_new_transcripts_allowed()) { ?> <?php if ($transcript_options->are_new_transcripts_allowed()) { ?>
<h3 style="margin-top: 0.5em"><?php _e('Submit a new transcript:', 'what-did-they-say') ?></h3> <h3 style="margin-top: 0.5em"><?php _e('Submit a new transcript:', 'what-did-they-say') ?></h3>