Improve password-pop-up handling.
authorDan Marsden <dan@danmarsden.com>
Thu, 22 Jun 2017 03:16:21 +0000 (15:16 +1200)
committerDan Marsden <dan@danmarsden.com>
Thu, 22 Jun 2017 22:47:10 +0000 (10:47 +1200)
password.php [new file with mode: 0644]
password_ajax.php [new file with mode: 0644]
renderables.php
renderer.php
templates/attendance_password_icon.mustache [new file with mode: 0644]
templates/attendance_password_icon_boost.mustache [new file with mode: 0644]

diff --git a/password.php b/password.php
new file mode 100644 (file)
index 0000000..11f2724
--- /dev/null
@@ -0,0 +1,50 @@
+<?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/>.
+
+/**
+ * Displays help via AJAX call or in a new page
+ *
+ * Use {@link core_renderer::help_icon()} or {@link addHelpButton()} to display
+ * the help icon.
+ *
+ * @copyright 2002 onwards Martin Dougiamas
+ * @package   core
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require_once(dirname(__FILE__).'/../../config.php');
+
+$session = required_param('session', PARAM_INT);
+$session = $DB->get_record('attendance_sessions', array('id' => $session), '*', MUST_EXIST);
+
+$cm = get_coursemodule_from_instance('attendance', $session->attendanceid);
+$context = context_module::instance($cm->id);
+$capabilities = array('mod/attendance:manageattendances', 'mod/attendance:takeattendances','mod/attendance:changeattendances');
+if (!has_any_capability($capabilities, $context)) {
+    exit;
+}
+
+$PAGE->set_url('/mod/attendance/password.php');
+$PAGE->set_pagelayout('popup');
+
+$PAGE->set_context(context_system::instance());
+
+$PAGE->set_title(get_string('password', 'attendance'));
+
+echo $OUTPUT->header();
+echo html_writer::span($session->studentpassword, 'student-password');
+echo $OUTPUT->footer();
diff --git a/password_ajax.php b/password_ajax.php
new file mode 100644 (file)
index 0000000..47bb82a
--- /dev/null
@@ -0,0 +1,49 @@
+<?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/>.
+
+/**
+ * Displays help via AJAX call or in a new page
+ *
+ * Use {@link core_renderer::help_icon()} or {@link addHelpButton()} to display
+ * the help icon.
+ *
+ * @copyright 2002 onwards Martin Dougiamas
+ * @package   core
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+define('AJAX_SCRIPT', true);
+require_once(dirname(__FILE__).'/../../config.php');
+
+$session = required_param('session', PARAM_INT);
+$session = $DB->get_record('attendance_sessions', array('id' => $session), '*', MUST_EXIST);
+
+$cm = get_coursemodule_from_instance('attendance', $session->attendanceid);
+$context = context_module::instance($cm->id);
+$capabilities = array('mod/attendance:manageattendances', 'mod/attendance:takeattendances','mod/attendance:changeattendances');
+if (!has_any_capability($capabilities, $context)) {
+    exit;
+}
+
+$PAGE->set_url('/mod/attendance/password.php');
+$PAGE->set_pagelayout('popup');
+
+$PAGE->set_context(context_system::instance());
+
+$data->heading = '';
+$data->text = html_writer::span($session->studentpassword, 'student-password');
+
+echo json_encode($data);
index 0f2d483..dc3ced6 100644 (file)
@@ -809,3 +809,61 @@ class url_helpers {
         return $att->url_view($params);
     }
 }
+
+/**
+ * Data structure representing an attendance password icon.
+ * copied from help_icon class
+ *
+ * @copyright 2017 Dan Marsden
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class attendance_password_icon implements renderable, templatable {
+
+    /**
+     * @var string text to show
+     */
+    public $text;
+
+    /**
+     * @var string Extra descriptive text next to the icon
+     */
+    public $linktext = null;
+
+    /**
+     * Constructor
+     *
+     * @param string $identifier string for help page title,
+     *  string with _help suffix is used for the actual help text.
+     *  string with _link suffix is used to create a link to further info (if it exists)
+     * @param string $component
+     */
+    public function __construct($text, $sessionid) {
+        $this->text  = $text;
+        $this->sessionid = $sessionid;
+    }
+
+    /**
+     * Export this data so it can be used as the context for a mustache template.
+     *
+     * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
+     * @return array
+     */
+    public function export_for_template(renderer_base $output) {
+
+        $title = get_string('password', 'attendance');
+
+        $data = new stdClass();
+        $data->heading = '';
+        $data->text = $this->text;
+
+        $data->alt = $title;
+        $data->icon = (new pix_icon('key', '', 'attendance'))->export_for_template($output);
+        $data->linktext = '';
+        $data->title = $title;
+        $data->url = (new moodle_url('/mod/attendance/password.php', [
+            'session' => $this->sessionid]))->out(false);
+
+        $data->ltr = !right_to_left();
+        return $data;
+    }
+}
index 6223fcf..a37bb9c 100644 (file)
@@ -293,6 +293,15 @@ class mod_attendance_renderer extends plugin_renderer_base {
     }
 
     /**
+     * Implementation of user image rendering.
+     *
+     * @param help_icon $helpicon A help icon instance
+     * @return string HTML fragment
+     */
+    protected function render_attendance_password_icon(attendance_password_icon $helpicon) {
+        return $this->render_from_template('attendance/attendance_password_icon', $helpicon->export_for_template($this));
+    }
+    /**
      * Construct date time actions.
      *
      * @param attendance_manage_data $sessdata
@@ -306,12 +315,8 @@ class mod_attendance_renderer extends plugin_renderer_base {
             has_capability('mod/attendance:takeattendances', $sessdata->att->context) ||
             has_capability('mod/attendance:changeattendances', $sessdata->att->context))) {
 
-            $icon = new pix_icon('key', '', 'attendance');
-            $attributes = array("class" => "btn-link p-a-0", "role" => "button",
-                                "data-toggle" => "popover", "data-placement" => "left", "data-html" => "true",
-                                "tabindex" => "0", "data-trigger" => "manual");
-            $attributes['data-content'] = html_writer::span($sess->studentpassword, 'student-pass');
-            $actions .= html_writer::tag('a', $this->output->render($icon), $attributes);
+            $icon = new attendance_password_icon($sess->studentpassword, $sess->id);
+            $actions .= $this->render($icon);
         }
 
         $date = userdate($sess->sessdate, get_string('strftimedmyw', 'attendance'));
diff --git a/templates/attendance_password_icon.mustache b/templates/attendance_password_icon.mustache
new file mode 100644 (file)
index 0000000..2d1a928
--- /dev/null
@@ -0,0 +1,22 @@
+{{!
+    @template attendance/attendance_password_icon
+
+    attendance_password  icon.
+
+    Example context (json):
+    {
+        "title": "Help with something",
+        "url": "http://example.org/help",
+        "linktext": "",
+        "icon":{
+            "attributes": [
+                {"name": "class", "value": "iconhelp"},
+                {"name": "src", "value": "../../../pix/help.svg"},
+                {"name": "alt", "value": "Help icon"}
+            ]
+        }
+    }
+}}
+<span class="helptooltip">
+    <a href="{{url}}" title={{#quote}}{{title}}{{/quote}} aria-haspopup="true" target="_blank">{{#icon}}{{>core/pix_icon}}{{/icon}}{{#linktext}}{{.}}{{/linktext}}</a>
+</span>
\ No newline at end of file
diff --git a/templates/attendance_password_icon_boost.mustache b/templates/attendance_password_icon_boost.mustache
new file mode 100644 (file)
index 0000000..319b897
--- /dev/null
@@ -0,0 +1,28 @@
+{{!
+    @template attendance/attendance_password_icon Boost Example.
+    This is an example of a template you could copy into a boost based theme to use proper popover.
+    At the moment we cannot specify different templates to use in plugin so we use
+    a cross-compatible link based pop-up for the password.
+
+    attendance_password  icon.
+
+    Example context (json):
+    {
+        "title": "Help with something",
+        "url": "http://example.org/help",
+        "linktext": "",
+        "icon":{
+            "attributes": [
+                {"name": "class", "value": "iconhelp"},
+                {"name": "src", "value": "../../../pix/help.svg"},
+                {"name": "alt", "value": "Help icon"}
+            ]
+        }
+    }
+}}
+<a class="btn btn-link p-a-0" role="button"
+    data-container="body" data-toggle="popover"
+    data-placement="{{#ltr}}left{{/ltr}}{{^ltr}}right{{/ltr}}" data-content="<span class='student-pass'>{{text}}</span> {{completedoclink}}"
+    data-html="true" tabindex="0" data-trigger="focus">
+    {{#pix}}key, attendance, {{alt}}{{/pix}}
+</a>