From 44e505d6516b481b4cf5d06aabbec36d23e140d8 Mon Sep 17 00:00:00 2001
From: Matheus Garcia <garcia.figueiredo@gmail.com>
Date: Tue, 8 Jan 2019 17:04:43 -0200
Subject: [PATCH] =?UTF-8?q?Vers=C3=A3o=20inicial=20do=20m=C3=A9todo=20de?=
 =?UTF-8?q?=20inscri=C3=A7=C3=A3o,=20criado=20a=20partir=20do=20m=C3=A9tod?=
 =?UTF-8?q?o=20do=20PagSeguro?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 db/access.php         |  62 ++++++++++++++
 db/install.xml        |  32 +++++++
 db/messages.php       |  29 +++++++
 db/upgrade.php        |  44 ++++++++++
 edit.php              | 101 ++++++++++++++++++++++
 edit_form.php         | 101 ++++++++++++++++++++++
 lang/en/enrol_evl.php |  67 +++++++++++++++
 lib.php               | 195 ++++++++++++++++++++++++++++++++++++++++++
 pix/icon.gif          | Bin 0 -> 381 bytes
 process.php           |  48 +++++++++++
 return.php            |  79 +++++++++++++++++
 settings.php          |  31 +++++++
 unenrolself.php       |  61 +++++++++++++
 version.php           |  30 +++++++
 14 files changed, 880 insertions(+)
 create mode 100644 db/access.php
 create mode 100644 db/install.xml
 create mode 100644 db/messages.php
 create mode 100644 db/upgrade.php
 create mode 100644 edit.php
 create mode 100644 edit_form.php
 create mode 100644 lang/en/enrol_evl.php
 create mode 100644 lib.php
 create mode 100644 pix/icon.gif
 create mode 100644 process.php
 create mode 100644 return.php
 create mode 100644 settings.php
 create mode 100644 unenrolself.php
 create mode 100644 version.php

diff --git a/db/access.php b/db/access.php
new file mode 100644
index 0000000..a2503e6
--- /dev/null
+++ b/db/access.php
@@ -0,0 +1,62 @@
+<?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/>.
+
+/**
+ * Capabilities for evl enrolment plugin.
+ *
+ * @package    enrol_evl
+ * @author     Interlegis
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+$capabilities = array(
+
+    'enrol/evl:config' => array(
+        'captype' => 'write',
+        'contextlevel' => CONTEXT_COURSE,
+        'archetypes' => array(
+            'manager' => CAP_ALLOW,
+        )
+    ),
+
+    'enrol/evl:manage' => array(
+        'captype' => 'write',
+        'contextlevel' => CONTEXT_COURSE,
+        'archetypes' => array(
+            'manager' => CAP_ALLOW,
+            'editingteacher' => CAP_ALLOW,
+        )
+    ),
+
+    'enrol/evl:unenrol' => array(
+        'captype' => 'write',
+        'contextlevel' => CONTEXT_COURSE,
+        'archetypes' => array(
+            'manager' => CAP_ALLOW,
+        )
+    ),
+
+    'enrol/evl:unenrolself' => array(
+        'captype' => 'write',
+        'contextlevel' => CONTEXT_COURSE,
+        'archetypes' => array(
+        )
+    ),
+
+);
+
diff --git a/db/install.xml b/db/install.xml
new file mode 100644
index 0000000..769e6f0
--- /dev/null
+++ b/db/install.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<XMLDB PATH="enrol/evl/db" VERSION="20120122" COMMENT="XMLDB file for Moodle enrol/evl"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
+>
+  <TABLES>
+    <TABLE NAME="enrol_evl" COMMENT="Holds all known information about evl transactions">
+      <FIELDS>
+        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="business"/>
+        <FIELD NAME="business" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="id" NEXT="receiver_email"/>
+        <FIELD NAME="receiver_email" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="business" NEXT="courseid"/>
+        <FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="receiver_email" NEXT="userid"/>
+        <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="courseid" NEXT="instanceid"/>
+        <FIELD NAME="instanceid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="userid" NEXT="date"/>
+        <FIELD NAME="date" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="instanceid" NEXT="code"/>
+        <FIELD NAME="code" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="date" NEXT="reference"/>
+        <FIELD NAME="reference" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="code" NEXT="type"/>
+        <FIELD NAME="type" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="reference" NEXT="status"/>
+        <FIELD NAME="status" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="type" NEXT="paymentmethod_type"/>
+        <FIELD NAME="paymentmethod_type" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="status" NEXT="paymentmethod_code"/>
+        <FIELD NAME="paymentmethod_code" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="paymentmethod_type" NEXT="grossamount"/>
+        <FIELD NAME="grossamount" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="paymentmethod_code" NEXT="timeupdated"/>
+        <FIELD NAME="timeupdated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="grossamount" NEXT="payment_status"/>
+        <FIELD NAME="payment_status" TYPE="char" LENGTH="20" NOTNULL="true" SEQUENCE="false" PREVIOUS="timeupdated" NEXT="xmlstring"/>
+        <FIELD NAME="xmlstring" TYPE="text" LENGTH="small" NOTNULL="false" SEQUENCE="false" COMMENT="Notification XML" PREVIOUS="payment_status" /> 
+      </FIELDS>
+      <KEYS>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
+      </KEYS>
+    </TABLE>
+  </TABLES>
+</XMLDB>
\ No newline at end of file
diff --git a/db/messages.php b/db/messages.php
new file mode 100644
index 0000000..f8a4237
--- /dev/null
+++ b/db/messages.php
@@ -0,0 +1,29 @@
+<?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/>.
+
+/**
+ * Defines message providers (types of message sent) for the evl enrolment plugin.
+ *
+ * @package    enrol_evl
+ * @author     Interlegis
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+$messageproviders = array(
+    'evl_enrolment' => array(),
+);
diff --git a/db/upgrade.php b/db/upgrade.php
new file mode 100644
index 0000000..46806d2
--- /dev/null
+++ b/db/upgrade.php
@@ -0,0 +1,44 @@
+<?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/>.
+
+/**
+ * This file keeps track of upgrades to the evl enrolment plugin
+ *
+ * @package    enrol_evl
+ * @author     Interlegis
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+// Sometimes, changes between versions involve
+// alterations to database structures and other
+// major things that may break installations.
+//
+// The upgrade function in this file will attempt
+// to perform all the necessary actions to upgrade
+// your older installation to the current version.
+//
+// If there's something it cannot do itself, it
+// will tell you what you need to do.
+//
+// The commands in here will all be database-neutral,
+// using the methods of database_manager class
+//
+// Please do not forget to use upgrade_set_timeout()
+// before any action that may take longer time to finish.
+
+function xmldb_enrol_evl_upgrade($oldversion) {
+    return true;
+}
diff --git a/edit.php b/edit.php
new file mode 100644
index 0000000..604d764
--- /dev/null
+++ b/edit.php
@@ -0,0 +1,101 @@
+<?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/>.
+
+/**
+ * Adds new instance of enrol_evl to specified course
+ * or edits current instance.
+ *
+ * @package    enrol_evl
+ * @author     Interlegis
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require('../../config.php');
+require_once('edit_form.php');
+
+$courseid   = required_param('courseid', PARAM_INT);
+$instanceid = optional_param('id', 0, PARAM_INT); // instanceid
+
+$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
+$context = context_course::instance($course->id);
+
+require_login($course);
+require_capability('enrol/evl:config', $context);
+
+$PAGE->set_url('/enrol/evl/edit.php', array('courseid' => $course->id, 'id' => $instanceid));
+$PAGE->set_pagelayout('admin');
+
+$return = new moodle_url('/enrol/instances.php', array('id' => $course->id));
+if (!enrol_is_enabled('evl')) {
+    redirect($return);
+}
+
+$plugin = enrol_get_plugin('evl');
+
+if ($instanceid) {
+    $instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'evl', 'id' => $instanceid), '*', MUST_EXIST);
+} else {
+    require_capability('moodle/course:enrolconfig', $context);
+    // no instance yet, we have to add new instance
+    navigation_node::override_active_url(new moodle_url('/enrol/instances.php', array('id' => $course->id)));
+    $instance = new stdClass();
+    $instance->id       = null;
+    $instance->courseid = $course->id;
+}
+
+$mform = new enrol_evl_edit_form(null, array($instance, $plugin, $context));
+
+if ($mform->is_cancelled()) {
+    redirect($return);
+
+} else if ($data = $mform->get_data()) {
+
+    if ($instance->id) {
+        $reset = ($instance->status != $data->status);
+
+        $instance->status         = $data->status;
+        $instance->name           = $data->name;
+        $instance->cost           = $data->cost;
+        $instance->currency       = $data->currency;
+        $instance->roleid         = $data->roleid;
+        $instance->enrolperiod    = $data->enrolperiod;
+        $instance->enrolstartdate = $data->enrolstartdate;
+        $instance->enrolenddate   = $data->enrolenddate;
+        $instance->timemodified   = time();
+        $DB->update_record('enrol', $instance);
+
+        if ($reset) {
+            $context->mark_dirty();
+        }
+
+    } else {
+        $fields = array('status' => $data->status, 'name' => $data->name, 'cost' => $data->cost,
+                        'currency' => $data->currency, 'roleid' => $data->roleid,
+                        'enrolperiod' => $data->enrolperiod, 'enrolstartdate' => $data->enrolstartdate,
+                        'enrolenddate' => $data->enrolenddate);
+        $plugin->add_instance($course, $fields);
+    }
+
+    redirect($return);
+}
+
+$PAGE->set_heading($course->fullname);
+$PAGE->set_title(get_string('pluginname', 'enrol_evl'));
+
+echo $OUTPUT->header();
+echo $OUTPUT->heading(get_string('pluginname', 'enrol_evl'));
+$mform->display();
+echo $OUTPUT->footer();
diff --git a/edit_form.php b/edit_form.php
new file mode 100644
index 0000000..73338ce
--- /dev/null
+++ b/edit_form.php
@@ -0,0 +1,101 @@
+<?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/>.
+
+/**
+ * Adds new instance of enrol_evl to specified course
+ * or edits current instance.
+ *
+ * @package    enrol_evl
+ * @author     Interlegis
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+require_once($CFG->libdir.'/formslib.php');
+
+class enrol_evl_edit_form extends moodleform {
+
+    public function definition() {
+        $mform = $this->_form;
+
+        list($instance, $plugin, $context) = $this->_customdata;
+
+        $mform->addElement('header', 'header', get_string('pluginname', 'enrol_evl'));
+
+        $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'));
+        $mform->setType('name', PARAM_TEXT);
+
+        $options = array(ENROL_INSTANCE_ENABLED  => get_string('yes'),
+                         ENROL_INSTANCE_DISABLED => get_string('no'));
+        $mform->addElement('select', 'status', get_string('status', 'enrol_evl'), $options);
+        $mform->setDefault('status', $plugin->get_config('status'));
+
+        $mform->addElement('text', 'cost', get_string('cost', 'enrol_evl'), array('size' => 4));
+        $mform->setType('cost', PARAM_RAW);
+        $mform->setDefault('cost', $plugin->get_config('cost'));
+
+        $mform->addElement('select', 'currency', get_string('currency', 'enrol_evl'),
+                           \get_string_manager()->get_list_of_currencies());
+        $mform->setDefault('currency', $plugin->get_config('currency'));
+
+        if ($instance->id) {
+            $roles = get_default_enrol_roles($context, $instance->roleid);
+        } else {
+            $roles = get_default_enrol_roles($context, $plugin->get_config('roleid'));
+        }
+        $mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_evl'), $roles);
+        $mform->setDefault('roleid', $plugin->get_config('roleid'));
+
+        $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_evl'), array('optional' => true, 'defaultunit' => 86400));
+        $mform->setDefault('enrolperiod', $plugin->get_config('enrolperiod'));
+        $mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_evl');
+
+        $mform->addElement('date_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_evl'), array('optional' => true));
+        $mform->setDefault('enrolstartdate', 0);
+        $mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_evl');
+
+        $mform->addElement('date_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_evl'), array('optional' => true));
+        $mform->setDefault('enrolenddate', 0);
+        $mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_evl');
+
+        $mform->addElement('hidden', 'id');
+        $mform->setType('id', PARAM_INT);
+        $mform->addElement('hidden', 'courseid');
+        $mform->setType('courseid', PARAM_INT);
+
+        $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol')));
+
+        $this->set_data($instance);
+    }
+
+    public function validation($data, $files) {
+        $errors = parent::validation($data, $files);
+
+        if ($data['status'] == ENROL_INSTANCE_ENABLED) {
+            if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) {
+                $errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_evl');
+            }
+
+            if (!is_numeric($data['cost'])) {
+                $errors['cost'] = get_string('costerror', 'enrol_evl');
+
+            }
+        }
+
+        return $errors;
+    }
+}
diff --git a/lang/en/enrol_evl.php b/lang/en/enrol_evl.php
new file mode 100644
index 0000000..46e873e
--- /dev/null
+++ b/lang/en/enrol_evl.php
@@ -0,0 +1,67 @@
+<?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/>.
+
+/**
+ * Strings for component 'enrol_evl', language 'en', branch 'MOODLE_20_STABLE'
+ *
+ * @package    enrol_evl
+ * @author     Interlegis
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+*/
+
+$string['enrol_success'] = 'Matriculado com sucesso no curso "{$a}"';
+$string['enrol_error'] = 'Erro ao realizar a matrícula na EVL';
+$string['pluginname'] = 'enrol_EVL';
+$string['pluginname_desc'] = 'Permite realizar matrículas em cursos disponíveis na Escola Virtual do Legislativo (EVL)';
+
+
+$string['assignrole'] = 'Assign role';
+$string['businessemail'] = 'EVL business email';
+$string['businessemail_desc'] = 'The email address of your business EVL account';
+$string['businesstoken'] = 'EVL business token';
+$string['businesstoken_desc'] = 'The token of your business EVL account';
+$string['cost'] = 'Enrol cost';
+$string['costerror'] = 'The enrolment cost is not numeric';
+$string['costorkey'] = 'Please choose one of the following methods of enrolment.';
+$string['currency'] = 'Currency';
+$string['currency_desc'] = 'Brazil currency : Brazilian Real';
+$string['defaultrole'] = 'Default role assignment';
+$string['defaultrole_desc'] = 'Select role which should be assigned to users during EVL enrolments';
+$string['enrolenddate'] = 'End date';
+$string['enrolenddate_help'] = 'If enabled, users can be enrolled until this date only.';
+$string['enrolenddaterror'] = 'Enrolment end date cannot be earlier than start date';
+$string['enrolperiod'] = 'Enrolment duration';
+$string['enrolperiod_desc'] = 'Default length of time that the enrolment is valid (in seconds). If set to zero, the enrolment duration will be unlimited by default.';
+$string['enrolperiod_help'] = 'Length of time that the enrolment is valid, starting with the moment the user is enrolled. If disabled, the enrolment duration will be unlimited.';
+$string['enrolstartdate'] = 'Start date';
+$string['enrolstartdate_help'] = 'If enabled, users can be enrolled from this date onward only.';
+$string['error:unauthorized'] = 'This host is not authorized to use EVL API.';
+$string['mailadmins'] = 'Notify admin';
+$string['mailstudents'] = 'Notify students';
+$string['mailteachers'] = 'Notify teachers';
+$string['messageprovider:EVL_enrolment'] = 'EVL enrolment messages';
+$string['needsignuporlogin'] = 'You need to sign up or log in before make a payment.';
+$string['nocost'] = 'There is no cost associated with enrolling in this course!';
+$string['EVL:config'] = 'Configure EVL enrol instances';
+$string['EVL:manage'] = 'Manage enrolled users';
+$string['EVL:unenrol'] = 'Unenrol users from course';
+$string['EVL:unenrolself'] = 'Unenrol self from the course';
+$string['EVLaccepted'] = 'EVL payments accepted';
+$string['paymentrequired'] = 'You must make a payment of {$a->currency} {$a->cost} via EVL to access this course.';
+$string['sendpaymentbutton'] = 'Send payment via EVL';
+$string['status'] = 'Allow EVL enrolments';
+$string['status_desc'] = 'Allow users to use EVL to enrol into a course by default.';
+$string['unenrolselfconfirm'] = 'Do you really want to unenrol yourself from course "{$a}"?';
diff --git a/lib.php b/lib.php
new file mode 100644
index 0000000..d1978d0
--- /dev/null
+++ b/lib.php
@@ -0,0 +1,195 @@
+<?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/>.
+
+/**
+ * EVL enrolment plugin.
+ *
+ * This plugin allows you to set up paid courses.
+ *
+ * @package    enrol_evl
+ * @author     Interlegis
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+// FIXME revisar
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * EVL enrolment plugin implementation.
+ * @author  Eugene Venter - based on code by Martin Dou giamas and others
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class enrol_evl_plugin extends enrol_plugin {
+
+    /**
+     * Returns optional enrolment information icons.
+     *
+     * This is used in course list for quick overview of enrolment options.
+     *
+     * We are not using single instance parameter because sometimes
+     * we might want to prevent icon repetition when multiple instances
+     * of one type exist. One instance may also produce several icons.
+     *
+     * @param array $instances all enrol instances of this type in one course
+     * @return array of pix_icon
+     */
+    public function get_info_icons(array $instances) {
+        return array(new pix_icon('icon', get_string('pluginname', 'enrol_evl'), 'enrol_evl'));
+    }
+
+    public function roles_protected() {
+        return false;
+    }
+
+    public function allow_unenrol(stdClass $instance) {
+        return true;
+    }
+
+    public function allow_manage(stdClass $instance) {
+        return true;
+    }
+
+    public function show_enrolme_link(stdClass $instance) {
+        return ($instance->status == ENROL_INSTANCE_ENABLED);
+    }
+
+    /**
+     * Sets up navigation entries.
+     *
+     * @param object $instance
+     * @return void
+     */
+    public function add_course_navigation($instancesnode, stdClass $instance) {
+        if ($instance->enrol !== 'evl') {
+             throw new coding_exception('Invalid enrol instance type!');
+        }
+
+        $context = context_course::instance($instance->courseid);
+        if (has_capability('enrol/evl:config', $context)) {
+            $managelink = new moodle_url('/enrol/evl/edit.php', array('courseid' => $instance->courseid, 'id' => $instance->id));
+            $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
+        }
+    }
+
+    /**
+     * Returns edit icons for the page with list of instances
+     * @param stdClass $instance
+     * @return array
+     */
+    public function get_action_icons(stdClass $instance) {
+        global $OUTPUT;
+
+        if ($instance->enrol !== 'evl') {
+            throw new coding_exception('invalid enrol instance!');
+        }
+        $context = context_course::instance($instance->courseid);
+
+        $icons = array();
+
+        if (has_capability('enrol/evl:config', $context)) {
+            $editlink = new moodle_url("/enrol/evl/edit.php", array('courseid' => $instance->courseid, 'id' => $instance->id));
+            $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit'), 'core', array('class' => 'icon')));
+        }
+
+        return $icons;
+    }
+
+    /**
+     * Returns link to page which may be used to add new instance of enrolment plugin in course.
+     * @param int $courseid
+     * @return moodle_url page url
+     */
+    public function get_newinstance_link($courseid) {
+        $context = context_course::instance($courseid);
+
+        if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/evl:config', $context)) {
+            return null;
+        }
+
+        return new moodle_url('/enrol/evl/edit.php', array('courseid' => $courseid));
+    }
+
+    /**
+     * Creates course enrol form, checks if form submitted
+     * and enrols user if necessary. It can also redirect.
+     *
+     * @param stdClass $instance
+     * @return string html text, usually a form in a text box
+     */
+    public function enrol_page_hook(stdClass $instance) {
+        global $CFG, $USER, $OUTPUT, $PAGE, $DB;
+
+        ob_start();
+
+        // Assegura que ainda existe não existe matrícula para esse usuário
+        if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
+            return ob_get_clean();
+        }
+
+        // Assegura que o período de inscrição está aberto
+        if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) {
+            return ob_get_clean();
+        }
+        if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) {
+            return ob_get_clean();
+        }
+
+        // Recupera dados do curso
+        $course = $DB->get_record('course', array('id' => $instance->courseid));
+        $context = context_course::instance($instance->courseid);
+
+        $shortname = format_string($course->shortname, true, array('context' => $context));
+        $strloginto = get_string("loginto", "", $shortname);
+        $strcourses = get_string("courses");
+
+        // Pass $view=true to filter hidden caps if the user cannot see them.
+        if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
+                                             '', '', '', '', false, true)) {
+            $users = sort_by_roleassignment_authority($users, $context);
+            $teacher = array_shift($users);
+        } else {
+            $teacher = false;
+        }
+
+        // Assegura que usuário está logado
+        require_login();
+        
+        $urlLogin = new moodle_url('/enrol/evl/process.php', array('instanceid' => $instance->id));
+        redirect($urlLogin);
+    }
+
+    /**
+     * Não é possível remover instância deste método de inscrição
+     * @param stdClass $instance
+     * @return bool
+     */
+    public function can_delete_instance($instance) {
+        
+        return false; 
+    }
+
+    /**
+     * Não é possível ocultar instância deste método de inscrição
+     *
+     * @param stdClass $instance
+     * @return bool
+     */
+    public function can_hide_show_instance($instance) {
+        return false;
+    }
+
+}
diff --git a/pix/icon.gif b/pix/icon.gif
new file mode 100644
index 0000000000000000000000000000000000000000..86fc60aeaad0a0925e29410b5034d1a942888401
GIT binary patch
literal 381
zcmZ?wbhEHb6krfwSZc!n1WXJJd~7U&f`W>YTq?4Hii(QzD#A)iN_z6TMoO}lYIcsQ
zw#LTBKBhvxrYcUh>R}c-E-o%!?%GaX8tJZbp`oG0p{7OQ>Qzzp35lWUX`yLpY1873
zCMWB4XPM5(w3(4(TV7tizR+|*ZBj=^$F{bh-JR}7JG_px<;|Qq^ZMMd#cN7#Zjar)
zd-t`yiB}Fx`m!(R*P)0rXU=>&o&Eo8{N1~Ef4|!K|3laRcU#`Qd-wn6{`vFg!I9!m
z7Dg@xdj=hlPe6WRV4HPd{)gq8c>6sT9+~QM%ZsDW%Unmnn}Ov}lf;>p>1QrXy{dbX
zBk{x(fh6huPk9Uk$^y8}83LmIrN!GTDDya3F;_FlG&;#^b2(edbW{s9^2xDySn+aH
zXG-X*i!!+gX%*^4Fi7hgnHcgHG6=1-ua+{`G%!_RlwnX>nZjnTV`*(6XrIOqBgD)b
W&n_Y?;BFis<Kyd5ch}yL!5RSH@rASi

literal 0
HcmV?d00001

diff --git a/process.php b/process.php
new file mode 100644
index 0000000..17d6180
--- /dev/null
+++ b/process.php
@@ -0,0 +1,48 @@
+<?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/>.
+
+/**
+ * Listens for Enrol Confirmation from EVL and after that setup user enrolment in Moodle
+ *
+ * @package    enrol_evl
+ * @author     Interlegis
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require('../../config.php');
+require_once("lib.php");
+require_once($CFG->libdir.'/eventslib.php');
+require_once($CFG->libdir.'/enrollib.php');
+
+// Obtem ID da instância do método de inscrição
+$instanceid = optional_param('instanceid', 0, PARAM_INT);
+
+// Obtém informações sobre o plugin de enrol e respectivo curso
+$plugin = enrol_get_plugin('evl');
+$plugin_instance = $DB->get_record("enrol", array("id" => $instanceid, "status" => 0));
+$courseid = $plugin_instance->courseid;
+
+// Monta url para redirecionamento após matrícula
+$urlRedirect = new moodle_url('/enrol/evl/return.php', array('id' => $courseid, 'instanceid' => $instanceid));
+// Monta url para matrícula
+$urlEnrol = new moodle_url('https://escolamodelows.interlegis.leg.br/cursos/registro', 
+        array(
+            'school' => 'ILB', // FIXME utilizar código da escola
+            'school_course' => $courseid, 
+            'redirect' => $urlRedirect
+        )
+    );
+redirect($urlEnrol);
\ No newline at end of file
diff --git a/return.php b/return.php
new file mode 100644
index 0000000..a499122
--- /dev/null
+++ b/return.php
@@ -0,0 +1,79 @@
+<?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/>.
+
+/**
+ * Script utilitário para matrículas via EVL.
+ *
+ * @package    enrol_evl
+ * @author     Interlegis
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require("../../config.php");
+require_once("$CFG->dirroot/enrol/evl/lib.php");
+
+// Obtém parâmetros opcionais da URL
+$id = optional_param('id', 0, PARAM_INT); // id do curso
+$instanceid = optional_param('instanceid', 0, PARAM_INT); // id da instancia do enrol
+
+// Assegura que existe o curso em que usuário tentou se matricular
+if (!$course = $DB->get_record("course", array("id" => $id))) {
+    redirect($CFG->wwwroot);
+}
+
+// Assegura que usuário está logado
+require_login();
+
+// Obtem url de redirecionamento, para acesso ao curso
+if (isset($SESSION->wantsurl)) {
+    $destination = $SESSION->wantsurl;
+    unset($SESSION->wantsurl);
+} else {
+    $destination = "{$CFG->wwwroot}/course/view.php?id={$course->id}";
+}
+
+// Obtem contexto do curso
+$context = context_course::instance($course->id);
+// Obtem nome do curso
+$fullname = format_string($course->fullname, true, array('context' => $context));
+
+// TODO: verificar que matrícula foi bem sucedida, por meio de chamada ao web service
+if( true /* matriculadoNaEvl() */) {
+    // Matricula usuário na Escola Modelo
+    $roleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
+    $enrol_plugin = enrol_get_plugin('evl');
+    $enrol_instance = $DB->get_record('enrol', array('id' => $instanceid));
+    //$DB->get_record("enrol", array("id" => $instanceid, "status" => 0));
+    $enrol_plugin->enrol_user($enrol_instance, $USER->id, $roleid);
+
+    // Assegura que foi matriculado
+    if (is_enrolled($context, null, '', true) || true) { 
+        // Redireciona usuário para página do curso
+        redirect($destination, get_string('enrol_success', 'enrol_evl', $fullname));
+    } else {
+        $PAGE->set_context($context);
+        $PAGE->set_url($destination);
+        echo $OUTPUT->header();
+        notice(get_string('enrol_error', 'enrol_evl'), $destination);
+        echo $OUTPUT->footer();
+    } 
+} else {
+    $PAGE->set_context($context);
+    $PAGE->set_url($destination);
+    echo $OUTPUT->header();
+    notice(get_string('enrol_error', 'enrol_evl'), $destination);
+    echo $OUTPUT->footer();
+}
diff --git a/settings.php b/settings.php
new file mode 100644
index 0000000..af10297
--- /dev/null
+++ b/settings.php
@@ -0,0 +1,31 @@
+<?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/>.
+
+/**
+ * EVL enrolments plugin settings and presets.
+ *
+ * @package    enrol_evl
+ * @author     Interlegis
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ *
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+if ($ADMIN->fulltree) {
+    // TODO: verificar a necessidade de criar eventuais configurações aqui
+}
diff --git a/unenrolself.php b/unenrolself.php
new file mode 100644
index 0000000..49e9c9b
--- /dev/null
+++ b/unenrolself.php
@@ -0,0 +1,61 @@
+<?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/>.
+
+/**
+ * EVL enrolment plugin - support for user self unenrolment.
+ *
+ * @package    enrol_evl
+ * @author     Interlegis
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require('../../config.php');
+
+$enrolid = required_param('enrolid', PARAM_INT);
+$confirm = optional_param('confirm', 0, PARAM_BOOL);
+
+$instance = $DB->get_record('enrol', array('id' => $enrolid, 'enrol' => 'evl'), '*', MUST_EXIST);
+$course = $DB->get_record('course', array('id' => $instance->courseid), '*', MUST_EXIST);
+$context = context_course::instance($course->id);
+
+require_login();
+if (!is_enrolled($context)) {
+    redirect(new moodle_url('/'));
+}
+require_login($course);
+
+$plugin = enrol_get_plugin('evl');
+
+// security defined inside following function
+if (!$plugin->get_unenrolself_link($instance)) {
+    redirect(new moodle_url('/course/view.php', array('id' => $course->id)));
+}
+
+$PAGE->set_url('/enrol/evl/unenrolself.php', array('enrolid' => $instance->id));
+$PAGE->set_title($plugin->get_instance_name($instance));
+
+if ($confirm and confirm_sesskey()) {
+    $plugin->unenrol_user($instance, $USER->id);
+    // TODO: trigger event.
+    redirect(new moodle_url('/index.php'));
+}
+
+echo $OUTPUT->header();
+$yesurl = new moodle_url($PAGE->url, array('confirm' => 1, 'sesskey' => sesskey()));
+$nourl = new moodle_url('/course/view.php', array('id' => $course->id));
+$message = get_string('unenrolselfconfirm', 'enrol_evl', format_string($course->fullname));
+echo $OUTPUT->confirm($message, $yesurl, $nourl);
+echo $OUTPUT->footer();
diff --git a/version.php b/version.php
new file mode 100644
index 0000000..c93ad8a
--- /dev/null
+++ b/version.php
@@ -0,0 +1,30 @@
+<?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/>.
+
+/**
+ * EVL enrolment plugin version specification.
+ *
+ * @package    enrol_evl
+ * @author     Interlegis
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+$plugin->version   = 20190108;
+$plugin->requires  = 2013051401;
+$plugin->release   = '1';
+$plugin->component = 'enrol_evl';