From 47cfd33174cbed962eeb6a763734c4e2f6862304 Mon Sep 17 00:00:00 2001 From: mjollnir_ Date: Mon, 18 Aug 2008 10:16:56 +0000 Subject: [PATCH] MDL-15758 - chat module portfolio export --- lang/en_utf8/chat.php | 2 + mod/chat/db/access.php | 30 ++++++++++++++- mod/chat/lib.php | 87 ++++++++++++++++++++++++++++++++++++++++++ mod/chat/report.php | 31 +++++++++++++++ mod/chat/version.php | 2 +- 5 files changed, 150 insertions(+), 2 deletions(-) diff --git a/lang/en_utf8/chat.php b/lang/en_utf8/chat.php index cb1ac679d16..b6545016f0e 100644 --- a/lang/en_utf8/chat.php +++ b/lang/en_utf8/chat.php @@ -8,6 +8,8 @@ $string['cantlogin'] = 'Could not log in to chat room!!'; $string['cantinsert'] = 'Could not insert a chat message!'; $string['chat:chat'] = 'Talk in a chat'; $string['chat:deletelog'] = 'Delete chat logs'; +$string['chat:exportsession'] = 'Export chat session'; +$string['chat:exportparticipatedsession'] = 'Export participated-in chat session'; $string['chat:readlog'] = 'Read chat logs'; $string['chatintro'] = 'Introduction text'; $string['chatname'] = 'Name of this chat room'; diff --git a/mod/chat/db/access.php b/mod/chat/db/access.php index e3d25887c02..f34243f41ec 100644 --- a/mod/chat/db/access.php +++ b/mod/chat/db/access.php @@ -68,7 +68,35 @@ $mod_chat_capabilities = array( 'editingteacher' => CAP_ALLOW, 'admin' => CAP_ALLOW ) - ) + ), + + 'mod/chat:exportparticipatedsession' => array( + + 'riskbitmask' => RISK_PERSONAL, + + 'captype' => 'read', + 'contextlevel' => CONTEXT_MODULE, + 'legacy' => array( + 'teacher' => CAP_ALLOW, + 'editingteacher' => CAP_ALLOW, + 'admin' => CAP_ALLOW, + // not student - nervous about allowing this by default + ), + + ), + + 'mod/chat:exportsession' => array( + + 'riskbitmask' => RISK_PERSONAL, + + 'captype' => 'read', + 'contextlevel' => CONTEXT_MODULE, + 'legacy' => array( + 'teacher' => CAP_ALLOW, + 'editingteacher' => CAP_ALLOW, + 'admin' => CAP_ALLOW, + ), + ), ); diff --git a/mod/chat/lib.php b/mod/chat/lib.php index b0d0eb5ec28..f794995da39 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -819,4 +819,91 @@ function chat_get_extra_capabilities() { return array('moodle/site:accessallgroups', 'moodle/site:viewfullnames'); } +require_once($CFG->libdir . '/portfoliolib.php'); +class chat_portfolio_caller extends portfolio_module_caller_base { + + private $chat; + private $start; + private $end; + + public function __construct($callbackargs) { + global $DB, $USER; + if (!$this->cm = get_coursemodule_from_id('chat', $callbackargs['id'])) { + portfolio_exporter::raise_error('invalidid', 'chat'); + } + $this->chat = $DB->get_record('chat', array('id' => $this->cm->instance)); + $select = 'chatid = ?'; + $params = array($this->chat->id); + if (array_key_exists('start', $callbackargs) && array_key_exists('end', $callbackargs) + && !empty($callbackargs['start']) && !empty($callbackargs['end'])) { + $select .= ' AND timestamp >= ? AND timestamp <= ?'; + $params[] = $callbackargs['start']; + $params[] = $callbackargs['end']; + $this->start = $callbackargs['start']; + $this->end = $callbackargs['end']; + } + $this->messages = $DB->get_records_select( + 'chat_messages', + $select, + $params, + 'timestamp ASC' + ); + $select .= ' AND userid = ?'; + $params[] = $USER->id; + $this->participated = $DB->record_exists_select( + 'chat_messages', + $select, + $params + ); + } + + public function expected_time() { + return PORTFOLIO_TIME_LOW; + } + + public function get_sha1() { + $str = ''; + ksort($this->messages); + foreach ($this->messages as $m) { + $str .= implode('', (array)$m); + } + return sha1($str); + } + + public function check_permissions() { + $context = get_context_instance(CONTEXT_MODULE, $this->cm->id); + return has_capability('mod/chat:exportsession', $context) + || ($this->participated + && has_capability('mod/chat:exportparticipatedsession', $context)); + } + + public function prepare_package() { + $content = ''; + foreach ($this->messages as $message) { // We are walking FORWARDS through messages + $m = clone $message; // grrrrrr + $formatmessage = chat_format_message($m, null, $this->user); + if (!isset($formatmessage->html)) { + continue; + } + $content .= $formatmessage->html; + } + $content = preg_replace('/\]*\>/', '', $content); + + return $this->exporter->write_new_file($content, clean_filename($this->cm->name . '-session.html')); + } + + public static function display_name() { + return get_string('modulename', 'chat'); + } + + public function get_return_url() { + global $CFG; + + return $CFG->wwwroot . '/mod/chat/report.php?id=' + . $this->cm->id . ((isset($this->start)) + ? '&start=' . $this->start . '&end=' . $this->end + : ''); + } +} + ?> diff --git a/mod/chat/report.php b/mod/chat/report.php index 2d6de6f1380..6f08cd8aba9 100644 --- a/mod/chat/report.php +++ b/mod/chat/report.php @@ -75,6 +75,17 @@ echo $formatmessage->html; } } + if (has_capability('mod/chat:exportsession', $context) + || (array_key_exists($USER->id, $sessionusers) + && has_capability('mod/chat:exportparticipatedsession', $context))) { + require_once($CFG->libdir . '/portfoliolib.php'); + $p = array( + 'id' => $cm->id, + 'start' => $start, + 'end' => $end, + ); + echo '
' . portfolio_add_button('chat_portfolio_caller', $p, '/mod/chat/lib.php', PORTFOLIO_ADD_FULL_FORM, null, true); + } print_simple_box_end(); } @@ -180,6 +191,17 @@ echo '

'; echo "id&start=$sessionstart&end=$sessionend\">$strseesession"; + if (has_capability('mod/chat:exportsession', $context) + || (array_key_exists($USER->id, $sessionusers) + && has_capability('mod/chat:exportparticipatedsession', $context))) { + require_once($CFG->libdir . '/portfoliolib.php'); + $p = array( + 'id' => $cm->id, + 'start' => $sessionstart, + 'end' => $sessionend, + ); + echo '
' . portfolio_add_button('chat_portfolio_caller', $p, '/mod/chat/lib.php', PORTFOLIO_ADD_TEXT_LINK, null, true); + } if (has_capability('mod/chat:deletelog', $context)) { echo "
id&start=$sessionstart&end=$sessionend&deletesession=1\">$strdeletesession"; } @@ -194,6 +216,15 @@ $lasttime = $message->timestamp; } + if (has_capability('mod/chat:exportsession', $context)) { + require_once($CFG->libdir . '/portfoliolib.php'); + $p = array( + 'id' => $cm->id, + ); + echo '
' . portfolio_add_button('chat_portfolio_caller', $p, '/mod/chat/lib.php', PORTFOLIO_ADD_FULL_FORM, get_string('addalltoportfolio', 'portfolio'), true); + } + + /// Finish the page print_footer($course); diff --git a/mod/chat/version.php b/mod/chat/version.php index c933033c04a..8873b80f918 100644 --- a/mod/chat/version.php +++ b/mod/chat/version.php @@ -5,7 +5,7 @@ /// This fragment is called by moodle_needs_upgrading() and /admin/index.php ///////////////////////////////////////////////////////////////////////////////// -$module->version = 2008072400; // The (date) version of this module +$module->version = 2008081400; // The (date) version of this module $module->requires = 2007101509; // Requires this Moodle version $module->cron = 300; // How often should cron check this module (seconds)?