Skip to content

Audiences #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions classes/audience.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
// 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/>.

/**
* Class for audience rules.
*
* @package block_advnotifications
* @copyright 2016 onwards LearningWorks Ltd {@link https://learningworks.co.nz/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Zander Potgieter <[email protected]>
*/

namespace block_advnotifications;

defined('MOODLE_INTERNAL') || die;

class audience {

public static function get_cohorts_for_autocomplete($notificationid) {
global $DB;

$cohortssql =
'SELECT c.id, c.name, nc.id as inuse
FROM {cohort} c
LEFT JOIN {block_advnotifications_coh} nc
ON c.id = nc.cohortid AND nc.notificationid = ?';

$cohorts = $DB->get_records_sql($cohortssql, [$notificationid]);

$options = [];
$values = [];

foreach ($cohorts as $c) {
$options[$c->id] = $c->name;
if (!is_null($c->inuse)) {
$values[] = $c->id;
}
}
return [$options, $values];
}

public static function get_roles_for_autocomplete($notificationid) {
global $DB;

$roles = role_get_names();
$selectedroles = $DB->get_fieldset_select(
'block_advnotifications_roles',
'roleid',
'notificationid = ?',
[$notificationid]
);
$options = [];
$values = [];
foreach ($roles as $r) {
$options[$r->id] = $r->localname;
if (in_array($r->id, $selectedroles)) {
$values[] = $r->id;
}
}
return [$options, $values];
}

public static function meets_profile_requirements($notificationid, $userid) {
global $DB, $USER;
if (!$rules = $DB->get_records('block_advnotifications_field', ['notificationid' => $notificationid])) {
return true; // There is no field restriction.
}
foreach ($rules as $r) {
if (strpos($r->userfield, 'profile_field_') === false) {
$currentvalue = $USER->{$r->userfield};
} else {
$field = substr($r->userfield, 14);
$currentvalue = $USER->profile[$field];
}
switch ($r->operator) {
case 'equals':
if ($currentvalue !== $r->fieldvalue) {
return false;
}
break;
case 'contains':
if (strpos($currentvalue, $r->fieldvalue) === false) {
return false;
}
break;
case 'beginwith':
if (strpos($currentvalue, $r->fieldvalue) !== 0) {
return false;
}
break;
}
}
return true;
}

public static function meets_cohorts_requirements($notificationid, $userid) {
global $DB;
if (!$DB->record_exists('block_advnotifications_coh', ['notificationid' => $notificationid])) {
return true; // There is no cohort restriction.
}
$sql =
'SELECT 1
FROM {block_advnotifications_coh} anc
JOIN {cohort_members} cm
ON cm.cohortid = anc.cohortid AND
anc.notificationid = ? AND
cm.userid = ?';
return $DB->record_exists_sql($sql, [$notificationid, $userid]);
}

public static function meets_roles_requirements($notificationid, $userid, $blockid) {
global $DB;
if (!$roles = $DB->get_records('block_advnotifications_roles', ['notificationid' => $notificationid])) {
return true; // There is no role restriction.
}
if ($blockid) {
$context = \context_block::instance($blockid);
} else {
$context = \context_system::instance();
}
foreach ($roles as $r) {
if (!user_has_role_assignment($userid, $r->roleid, $context->id)) {
return false;
}
}
return true;
}
}
5 changes: 3 additions & 2 deletions classes/notifications_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public function col_actions($values) {
<input type="hidden" class="delete_notification_blockid" name="blockid" value="' . $values->blockid . '">
<button type="submit" class="delete_notification_delete icon fa fa-trash-o fa-fw" name="delete"
title="' . get_string('advnotifications_delete_label', 'block_advnotifications') . '"></button>
</form>';
</form>
<a href="'. (new moodle_url('/blocks/advnotifications/pages/audience.php', ['id' => $values->id])).'"><i class="icon fa fa-users"/></a>';
}
}
}
}
128 changes: 128 additions & 0 deletions classes/output/form/audience.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
// 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/>.

/**
* Reports form.
*
* @package block_advnotifications
* @copyright 2020 Daniel Neis Araujo <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace block_advnotifications\output\form;
defined('MOODLE_INTERNAL') || die();

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

use moodleform;

/**
* Audience form class.
*
* @package block_advnotifications
* @copyright 2020 Daniel Neis Araujo <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class audience extends moodleform {

public function definition() {

$mform = $this->_form;

$notificationid = $this->_customdata['notificationid'];

list($options, $values) = \block_advnotifications\audience::get_cohorts_for_autocomplete($notificationid);

$autocomplete = $mform->addElement(
'autocomplete',
'cohorts',
get_string('cohorts', 'cohort' ),
$options,
['multiple' => true]
);
$autocomplete->setSelected($values);

list($options, $values) = \block_advnotifications\audience::get_roles_for_autocomplete($notificationid);
$autocomplete = $mform->addElement(
'autocomplete',
'roles',
get_string('roles'),
$options,
['multiple' => true]
);
$autocomplete->setSelected($values);

$elements = [
$mform->createElement('select', 'userfield', '', $this->filter_options()),
$mform->createElement('select', 'operator', '', $this->operator_options()),
$mform->createElement('text', 'fieldvalue', '', ['size' => 12]),
];
$filters = $mform->createElement('group', 'userfieldfilters', get_string('filter_userfield', 'block_advnotifications'), $elements);
$deletebutton = $mform->createElement('submit', 'deletefieldrule', 'X', [], false);

$rules = [
'userfieldfilters[userfield]' => ['type' => PARAM_TEXT],
'userfieldfilters[operator]' => ['type' => PARAM_TEXT],
'userfieldfilters[fieldvalue]' => ['type' => PARAM_TEXT]
];

$this->repeat_elements([$filters, $deletebutton], $this->_customdata['filterscount'], $rules,
'filterscount', 'adduserfieldfilter', 1, get_string('adduserfieldfilter', 'block_advnotifications'),
true, 'deletefieldrule');

$this->add_action_buttons();
}

protected function filter_options() {
global $DB;

$filters = [
'' => get_string('choosedots'),
'id' => 'id',
'username' => get_string('username'),
'idnumber' => get_string('idnumber'),
'firstname' => get_string('firstname'),
'lastname' => get_string('lastname'),
'fullname' => get_string('fullnameuser'),
'email' => get_string('email'),
'phone1' => get_string('phone1'),
'phone2' => get_string('phone2'),
'institution' => get_string('institution'),
'department' => get_string('department'),
'address' => get_string('address'),
'city' => get_string('city'),
'timezone' => get_string('timezone'),
'url' => get_string('webpage'),
];

if ($profilefields = $DB->get_records('user_info_field', [], 'sortorder ASC')) {
foreach ($profilefields as $f) {
$filters['profile_field_' . $f->shortname] = format_string($f->name);
}
}

return $filters;
}

protected function operator_options() {
return [
'' => get_string('choosedots'),
'beginwith' => get_string('operator_beginwith', 'block_advnotifications'),
'contains' => get_string('operator_contains', 'block_advnotifications'),
'equals' => get_string('operator_equals', 'block_advnotifications')
];
}
}
44 changes: 42 additions & 2 deletions db/install.xml
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" PATH="blocks/advnotifications/db" VERSION="20160704" COMMENT="XMLDB file for Moodle blocks/advnotifications" xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd">
<XMLDB PATH="blocks/advnotifications/db" VERSION="20210929" COMMENT="XMLDB file for Moodle blocks/advnotifications"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="block_advnotifications" COMMENT="A record of all advanced notifications">
<FIELDS>
Expand Down Expand Up @@ -36,5 +39,42 @@
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="block_advnotifications_coh" COMMENT="Default comment for the table, please edit me">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="notificationid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="cohortid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="notificationid" TYPE="foreign" FIELDS="notificationid" REFTABLE="block_advnotifications" REFFIELDS="id"/>
<KEY NAME="cohortid" TYPE="foreign" FIELDS="cohortid" REFTABLE="cohort" REFFIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="block_advnotifications_field" COMMENT="Default comment for the table, please edit me">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="notificationid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="userfield" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="operator" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="fieldvalue" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="notificationid" TYPE="foreign" FIELDS="notificationid" REFTABLE="block_advnotifications" REFFIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="block_advnotifications_roles" COMMENT="Default comment for the table, please edit me">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="notificationid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="roleid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="notificationid" TYPE="foreign" FIELDS="notificationid" REFTABLE="block_advnotifications" REFFIELDS="id"/>
<KEY NAME="roleid" TYPE="foreign" FIELDS="roleid" REFTABLE="role" REFFIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>
Loading