Skip to content

Commit

Permalink
Initial commit of message plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian Ringer committed Aug 19, 2020
0 parents commit f739df4
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Local message

## Functionality
- Form for admins to add new notification
- Store read messages for users, do not display twice.
- Store notifications in database.
50 changes: 50 additions & 0 deletions classes/form/edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
// This file is part of Moodle Course Rollover Plugin
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.


/**
* @package local_message
* @author Kristian
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

class edit extends moodleform {
//Add elements to form
public function definition() {
global $CFG;
$mform = $this->_form; // Don't forget the underscore!

$mform->addElement('text', 'messagetext', 'Message text'); // Add elements to your form
$mform->setType('messagetext', PARAM_NOTAGS); //Set type of element
$mform->setDefault('messagetext', 'Please enter a message'); //Default value

$choices = array();
$choices['0'] = \core\output\notification::NOTIFY_WARNING;
$choices['1'] = \core\output\notification::NOTIFY_SUCCESS;
$choices['2'] = \core\output\notification::NOTIFY_ERROR;
$choices['3'] = \core\output\notification::NOTIFY_INFO;
$mform->addElement('select', 'messagetype', 'Message type', $choices);
$mform->setDefault('messagetype', '3');

$this->add_action_buttons();
}
//Custom validation should be added here
function validation($data, $files) {
return array();
}
}
29 changes: 29 additions & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="local/message/db" VERSION="20130407" COMMENT="XMLDB file for local message plugin"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="local_message" COMMENT="each record is a message">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="messagetext" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="messagetype" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="local_message_read" COMMENT="each record is a user record of reading a message">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="messageid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timeread" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
60 changes: 60 additions & 0 deletions edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
// This file is part of Moodle Course Rollover Plugin
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package local_message
* @author Kristian
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/


require_once(__DIR__ . '/../../config.php');
require_once($CFG->dirroot . '/local/message/classes/form/edit.php');

global $DB;

$PAGE->set_url(new moodle_url('/local/message/edit.php'));
$PAGE->set_context(\context_system::instance());
$PAGE->set_title('Edit');


// We want to display our form.
$mform = new edit();



if ($mform->is_cancelled()) {
// Go back to manage.php page
redirect($CFG->wwwroot . '/local/message/manage.php', 'You cancelled the message form');


} else if ($fromform = $mform->get_data()) {

// Insert the data into our database table.
$recordtoinsert = new stdClass();
$recordtoinsert->messagetext = $fromform->messagetext;
$recordtoinsert->messagetype = $fromform->messagetype;

$DB->insert_record('local_message', $recordtoinsert);

// Go back to manage.php page
redirect($CFG->wwwroot . '/local/message/manage.php', 'You created a message with title ' . $fromform->messagetext);
}

echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();

53 changes: 53 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
// This file is part of Moodle Course Rollover Plugin
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package local_message
* @author Kristian
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

function local_message_before_footer() {
global $DB, $USER;
$sql = "SELECT lm.id, lm.messagetext, lm.messagetype
FROM {local_message} lm
left outer join {local_message_read} lmr ON lm.id = lmr.messageid
WHERE lmr.userid <> :userid
OR lmr.userid IS NULL";
$params = [
'userid' => $USER->id,
];
$messages = $DB->get_records_sql($sql, $params);
foreach ($messages as $message) {
$type = \core\output\notification::NOTIFY_INFO;
if ($message->messagetype === '0') {
$type = \core\output\notification::NOTIFY_WARNING;
}
if ($message->messagetype === '1') {
$type = \core\output\notification::NOTIFY_SUCCESS;
}
if ($message->messagetype === '2') {
$type = \core\output\notification::NOTIFY_ERROR;
}
\core\notification::add($message->messagetext, $type);

$readrecord = new stdClass();
$readrecord->messageid = $message->id;
$readrecord->userid = $USER->id;
$readrecord->timeread = time();
$DB->insert_record('local_message_read', $readrecord);
}
}
41 changes: 41 additions & 0 deletions manage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
// This file is part of Moodle Course Rollover Plugin
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package local_message
* @author Kristian
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once(__DIR__ . '/../../config.php');

global $DB;

$PAGE->set_url(new moodle_url('/local/message/manage.php'));
$PAGE->set_context(\context_system::instance());
$PAGE->set_title('Manage messages');

$messages = $DB->get_records('local_message');

echo $OUTPUT->header();
$templatecontext = (object)[
'messages' => array_values($messages),
'editurl' => new moodle_url('/local/message/edit.php'),
];

echo $OUTPUT->render_from_template('local_message/manage', $templatecontext);

echo $OUTPUT->footer();
32 changes: 32 additions & 0 deletions templates/manage.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template local_message/manage
Example context (json):
{
}
}}
<h1>List of messages</h1>
{{#messages}}
<p>{{messagetext}}</p>
{{/messages}}

<hr>
<input type="button" class="btn btn-primary" value="Create message" onclick="location.href='{{editurl}}'">
28 changes: 28 additions & 0 deletions version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
// This file is part of Moodle Course Rollover Plugin
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package local_message
* @author Kristian
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @var stdClass $plugin
*/

defined('MOODLE_INTERNAL') || die();

$plugin->component = 'local_message';
$plugin->version = 2020071900;
$plugin->requires = 2016052300; // Moodle version

0 comments on commit f739df4

Please sign in to comment.