From 93cfb7e250fe4d65e03710155a5ce1d0285755e2 Mon Sep 17 00:00:00 2001 From: Sesostris Vieira Date: Mon, 8 Sep 2014 15:18:19 -0300 Subject: [PATCH] First commit --- README.md | 11 +++ db/access.php | 43 ++++++++++++ db/install.php | 32 +++++++++ index.php | 87 +++++++++++++++++++++++ lang/en/report_messages.php | 36 ++++++++++ lang/pt_br/report_messages.php | 34 +++++++++ lib.php | 105 ++++++++++++++++++++++++++++ locallib.php | 55 +++++++++++++++ styles.css | 8 +++ user.php | 124 +++++++++++++++++++++++++++++++++ version.php | 30 ++++++++ 11 files changed, 565 insertions(+) create mode 100644 db/access.php create mode 100644 db/install.php create mode 100644 index.php create mode 100644 lang/en/report_messages.php create mode 100644 lang/pt_br/report_messages.php create mode 100644 lib.php create mode 100644 locallib.php create mode 100644 styles.css create mode 100644 user.php create mode 100644 version.php diff --git a/README.md b/README.md index a9a77b7..dc2de70 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,14 @@ moodle_report_messages ====================== A simple report to view message exchanges between user from a course + +To install, clone this plugin in $MOODLE/report/messages folder: + $ cd $MOODLE/report + $ git clone https://github.com/interlegis/moodle_report_messages.git messages + +Enter in your moodle site as admin user and install the plugin + +Usage: + In a course, click in Administration > Report > Messages report to see a + synthesis report for all enrolled users. Click in {view details} link to + see all messages sended from/to that user. diff --git a/db/access.php b/db/access.php new file mode 100644 index 0000000..5619fe7 --- /dev/null +++ b/db/access.php @@ -0,0 +1,43 @@ +. + +/** + * Capabilities + * + * @package report_outline + * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$capabilities = array( + + 'report/messages:view' => array( + 'riskbitmask' => RISK_PERSONAL, + 'captype' => 'read', + 'contextlevel' => CONTEXT_COURSE, + 'archetypes' => array( + 'teacher' => CAP_ALLOW, + 'editingteacher' => CAP_ALLOW, + 'manager' => CAP_ALLOW + ), + + 'clonepermissionsfrom' => 'coursereport/messages:view', + ) +); + + diff --git a/db/install.php b/db/install.php new file mode 100644 index 0000000..7eee477 --- /dev/null +++ b/db/install.php @@ -0,0 +1,32 @@ +. + +/** + * Post installation and migration code. + * + * @package report + * @subpackage messages + * @copyright 2011 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +function xmldb_report_messages_install() { + global $DB; + +} + diff --git a/index.php b/index.php new file mode 100644 index 0000000..cff4abb --- /dev/null +++ b/index.php @@ -0,0 +1,87 @@ +. + +/** + * Display user messages reports for a course (totals) + * + * @package report + * @subpackage messages + * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require('../../config.php'); +require_once($CFG->dirroot.'/report/messages/locallib.php'); + +$id = required_param('id',PARAM_INT); // course id + +$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST); + +$PAGE->set_url('/report/messages/index.php', array('id'=>$id)); +$PAGE->set_pagelayout('report'); + +require_login($course); +$context = context_course::instance($course->id); +require_capability('report/messages:view', $context); + +add_to_log($course->id, 'course', 'report messages', "report/messages/index.php?id=$course->id", $course->id); + +$strviewdetail = get_string('viewdetail', 'report_messages'); +$stractivityreport = get_string('pluginname', 'report_messages'); +$stractivity = get_string('activity'); +$strlast = get_string('lastaccess'); +$strreports = get_string('reports'); +$strviews = get_string('views'); +$strrelatedblogentries = get_string('relatedblogentries', 'blog'); + +$PAGE->set_title($course->shortname .': '. $stractivityreport); +$PAGE->set_heading($course->fullname); + +echo $OUTPUT->header(); +echo $OUTPUT->heading(format_string($course->fullname)); + +$messagestable = new html_table(); +$messagestable->attributes['class'] = 'generaltable boxaligncenter'; +$messagestable->cellpadding = 5; +$messagestable->id = 'messagestable'; +$messagestable->head = array(get_string('fullname'), get_string('totalsended', 'report_messages'), + get_string('totalreceived','report_messages'), $strviewdetail); +$messagestable->align = array('left', 'right', 'right', 'center'); +$messagestable->data = array(); + +$userlist = get_enrolled_users($context, '', 0, 'u.id'); +list($in_user, $param_users) = $DB->get_in_or_equal(array_keys($userlist), SQL_PARAMS_QM); +$fullname = $DB->sql_fullname('u.firstname', 'u.lastname'); + +$sql = " +select u.id, $fullname as fullname, u.picture, u.firstname, u.lastname, u.imagealt, u.email, + (select count(*) from mdl_message where useridfrom=u.id and useridto<>u.id and useridto $in_user) as totalsend, + (select count(*) from mdl_message where useridto=u.id and useridfrom<>u.id and useridfrom $in_user) as totalreceive +from mdl_user u +where u.id $in_user +"; + +$data = $DB->get_records_sql($sql, array_merge($param_users,$param_users,$param_users)); + +foreach ($data as $user) { + $upic = $OUTPUT->user_picture($user); + $ulink = "wwwroot}/user/view.php?id={$user->id}&course={$course->id}\">{$upic}"; + $link = "wwwroot/report/messages/user.php?id=$user->id&course=$course->id\">$strviewdetail"; + $messagestable->data[] = array($ulink.' '.$user->fullname, $user->totalsend, $user->totalreceive, $link); +} + +echo html_writer::table($messagestable); +echo $OUTPUT->footer(); diff --git a/lang/en/report_messages.php b/lang/en/report_messages.php new file mode 100644 index 0000000..76c436c --- /dev/null +++ b/lang/en/report_messages.php @@ -0,0 +1,36 @@ +. + +/** + * Strings + * + * @package report + * @subpackage messages + * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$string['messages:view'] = 'View activity report'; +$string['page-report-messages-x'] = 'Any messages report'; +$string['page-report-messages-index'] = 'Course messages report'; +$string['page-report-messages-user'] = 'User course messages report'; +$string['pluginname'] = 'Messages report'; +$string['reporttitle'] = 'Message report to user {$a->fullname}'; +$string['exchangedmessages'] = '{$a->userlink} {$a->total_messages} messages exchanged with {$a->fullname}'; +$string['message'] = 'Message body'; +$string['viewdetail'] = 'View details'; +$string['totalsended'] = 'Total sended messages'; +$string['totalreceived'] = 'Total received messages'; diff --git a/lang/pt_br/report_messages.php b/lang/pt_br/report_messages.php new file mode 100644 index 0000000..1e3355c --- /dev/null +++ b/lang/pt_br/report_messages.php @@ -0,0 +1,34 @@ +. + +/** + * Strings + * + * @package report + * @subpackage messages + * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$string['messages:view'] = 'Ver relatório de mensagens'; +$string['page-report-messages-x'] = 'Um relatório de mensagens'; +$string['page-report-messages-index'] = 'Relatório de mensagens do curso'; +$string['page-report-messages-user'] = 'Relatório de mensagens do usuário no curso'; +$string['pluginname'] = 'Relatório de Mensagens'; +$string['reporttitle'] = 'Relatório de mensagens do usuário {$a->fullname}'; +$string['exchangedmessages'] = '{$a->userlink} {$a->total_messages} mensagens trocadas com {$a->fullname}'; +$string['message'] = 'Texto da mensagem'; +$string['viewdetail'] = 'Ver detalhes'; diff --git a/lib.php b/lib.php new file mode 100644 index 0000000..e3a5e4e --- /dev/null +++ b/lib.php @@ -0,0 +1,105 @@ +. + +/** + * This file contains public API of messages report + * + * @package report + * @subpackage messages + * @copyright 2009 Sam Hemelryk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +/** + * This function extends the course navigation with the report items + * + * @param navigation_node $navigation The navigation node to extend + * @param stdClass $course The course to object for the report + * @param stdClass $context The context of the course + */ +function report_messages_extend_navigation_course($navigation, $course, $context) { + if (has_capability('report/messages:view', $context)) { + $url = new moodle_url('/report/messages/index.php', array('id'=>$course->id)); + $navigation->add(get_string('pluginname', 'report_messages'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', '')); + } +} + +/** + * This function extends the course navigation with the report items + * + * @param navigation_node $navigation The navigation node to extend + * @param stdClass $user + * @param stdClass $course The course to object for the report + */ +function report_messages_extend_navigation_user($navigation, $user, $course) { + if (report_messages_can_access_user_report($user, $course)) { + $url = new moodle_url('/report/messages/user.php', array('id'=>$user->id, 'course'=>$course->id)); + $navigation->add(get_string('pluginname', 'report_messages'), $url); + } +} + +/** + * Is current user allowed to access this report + * + * @private defined in lib.php for performance reasons + * + * @param stdClass $user + * @param stdClass $course + * @return bool + */ +function report_messages_can_access_user_report($user, $course) { + global $USER; + + $coursecontext = context_course::instance($course->id); + $personalcontext = context_user::instance($user->id); + + if (has_capability('report/messages:view', $coursecontext)) { + return true; + } + + if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)) { + if ($course->showreports and (is_viewing($coursecontext, $user) or is_enrolled($coursecontext, $user))) { + return true; + } + + } else if ($user->id == $USER->id) { + if ($course->showreports and (is_viewing($coursecontext, $USER) or is_enrolled($coursecontext, $USER))) { + return true; + } + } + + return false; +} + +/** + * Return a list of page types + * @param string $pagetype current page type + * @param stdClass $parentcontext Block's parent context + * @param stdClass $currentcontext Current context of block + * @return array + */ +function report_messages_page_type_list($pagetype, $parentcontext, $currentcontext) { + $array = array( + '*' => get_string('page-x', 'pagetype'), + 'report-*' => get_string('page-report-x', 'pagetype'), + 'report-messages-*' => get_string('page-report-messages-x', 'report_messages'), + 'report-messages-index' => get_string('page-report-messages-index', 'report_messages'), + 'report-messages-user' => get_string('page-report-messages-user', 'report_messages') + ); + return $array; +} diff --git a/locallib.php b/locallib.php new file mode 100644 index 0000000..ba4761c --- /dev/null +++ b/locallib.php @@ -0,0 +1,55 @@ +. + +/** + * This file contains functions used by the outline reports + * + * @package report + * @subpackage outline + * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +require_once(dirname(__FILE__).'/lib.php'); +require_once($CFG->dirroot.'/course/lib.php'); + +function report_outline_print_row($mod, $instance, $result) { + global $OUTPUT, $CFG; + + $image = "pix_url('icon', $mod->modname) . "\" class=\"icon\" alt=\"$mod->modfullname\" />"; + + echo ""; + echo "$image"; + echo ""; + echo " modfullname\""; + echo " href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".format_string($instance->name,true).""; + echo "   "; + echo ""; + if (isset($result->info)) { + echo "$result->info"; + } else { + echo "

-

"; + } + echo ""; + echo "   "; + if (!empty($result->time)) { + $timeago = format_time(time() - $result->time); + echo "".userdate($result->time)." ($timeago)"; + } + echo ""; +} diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..aff09a7 --- /dev/null +++ b/styles.css @@ -0,0 +1,8 @@ +#page-report-outline-index td.numviews {text-align:right;} +#page-report-outline-index tr.section {text-align: center;} +#page-report-outline-index td.lastaccess {font-size: 0.8em;} + +#page-report-outline-user .section .content {margin-left: 30px;margin-right: 30px;} +#page-report-outline-user .section h2 {margin-top: 0;} +#page-report-outline-user .section {margin-left: 30px;margin-right: 30px;margin-bottom: 20px;} +#page-report-outline-user .section {border-width:1px;border-style:solid;padding:10px;} diff --git a/user.php b/user.php new file mode 100644 index 0000000..5bc34b7 --- /dev/null +++ b/user.php @@ -0,0 +1,124 @@ +. + +/** + * Display user activity reports for a course (totals) + * + * @package report + * @subpackage outline + * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require('../../config.php'); +require_once('locallib.php'); + +$userid = required_param('id', PARAM_INT); +$courseid = required_param('course', PARAM_INT); + +$user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST); +$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST); + +$coursecontext = context_course::instance($course->id); +$personalcontext = context_user::instance($user->id); + +if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) + and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) { + //TODO: do not require parents to be enrolled in courses - this is a hack! + require_login(); + $PAGE->set_course($course); +} else { + require_login($course); +} + +if (!report_messages_can_access_user_report($user, $course, true)) { + require_capability('report/messages:view', $coursecontext); +} + +add_to_log($course->id, 'course', 'report messages', "report/messages/user.php?id=$user->id&course=$course->id", $course->id); + +$PAGE->set_pagelayout('admin'); +$PAGE->set_url('/report/messages/user.php', array('id'=>$user->id, 'course'=>$course->id)); +$PAGE->navigation->extend_for_user($user); +$PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed. +$PAGE->set_title($course->shortname.': '.get_string('pluginname', 'report_messages')); +$PAGE->set_heading($course->fullname); +echo $OUTPUT->header(); +echo '

'.get_string('reporttitle','report_messages',array('fullname'=>fullname($user))).'

'; + +$from = $DB->get_records_sql('select distinct m.useridfrom as id from {message} m where m.useridto = ? and m.useridfrom <> ?', array($user->id, $user->id)); +$to = $DB->get_records_sql('select distinct m.useridto as id from {message} m where m.useridfrom = ? and m.useridto <> ?', array($user->id, $user->id)); +$contacts = array(); +$sqlfullname = $DB->sql_fullname('u.firstname','u.lastname'); +$sql = " + select u.id, $sqlfullname as fullname, count(*) as total_messages, u.picture, u.firstname, u.lastname, u.imagealt, u.email + from {user} u, {message} m + where u.id = ? and + ((m.useridfrom = ? and m.useridto = ?) or + (m.useridfrom = ? and m.useridto = ?)) + group by u.id, u.username, u.firstname, u.lastname, u.email +"; + +foreach ($from as $u) { + if (is_enrolled($coursecontext, $u)) { + $contacts[$u->id] = $DB->get_record_sql($sql, array($u->id, $u->id, $user->id, $user->id, $u->id)); + } +} + + +foreach ($to as $u) { + if (is_enrolled($coursecontext, $u)) { + $contacts[$u->id] = $DB->get_record_sql($sql, array($u->id, $u->id, $user->id, $user->id, $u->id)); + } +} + +if (empty($contacts)) { + echo $OUTPUT->notification(get_string('nothingtodisplay')); +} else { + $sqlfromname = $DB->sql_fullname('fu.firstname','fu.lastname'); + $sqltoname = $DB->sql_fullname('tu.firstname','tu.lastname'); + $sql = " + select m.timecreated, $sqlfromname as fromuser, $sqltoname as touser, m.fullmessage, m.fullmessageformat + from {message} m + inner join {user} fu on fu.id = m.useridfrom + inner join {user} tu on tu.id = m.useridto + where (m.useridfrom = ? and m.useridto = ?) or + (m.useridfrom = ? and m.useridto = ?) + order by m.timecreated desc + "; + + foreach ($contacts as $contact) { + echo $OUTPUT->box_start(); + $upic = $OUTPUT->user_picture($contact); + $ulink = "wwwroot}/user/view.php?id={$contact->id}&course={$course->id}\">{$upic}"; + $a = array('userlink'=>$ulink, 'total_messages'=>$contact->total_messages, 'fullname'=>$contact->fullname); + $head = get_string('exchangedmessages', 'report_messages', $a); + echo "

$head

"; + + $table = new html_table(); + $tabl->attributes['class'] = 'generaltable boxaligncenter'; + $table->head = array(get_string('date'), get_string('from'), get_string('to'), get_string('message', 'report_messages')); + $messages = $DB->get_records_sql($sql, array($user->id, $contact->id, $contact->id, $user->id)); + $table->data = array(); + foreach ($messages as $message) { + $table->data[] = array(userdate($message->timecreated), $message->fromuser, $message->touser, format_text($message->fullmessage, $message->fullmessageformat)); + } + echo html_writer::table($table); + echo $OUTPUT->box_end(); + } +} + +echo $OUTPUT->footer(); diff --git a/version.php b/version.php new file mode 100644 index 0000000..647a66f --- /dev/null +++ b/version.php @@ -0,0 +1,30 @@ +. + +/** + * Version info + * + * @package report + * @subpackage messages + * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +$plugin->version = 2013050100; // The current plugin version (Date: YYYYMMDDXX) +$plugin->requires = 2013050100; // Requires this Moodle version +$plugin->component = 'report_messages'; // Full name of the plugin (used for diagnostics)