/**
     * Creates necessary fields in the messaging config form.
     *
     * @param array $preferences An array of user preferences
     */
    public function config_form($preferences) {
        global $CFG, $OUTPUT, $USER, $PAGE;

        $systemcontext = context_system::instance();
        if (!has_capability('message/airnotifier:managedevice', $systemcontext)) {
            return get_string('nopermissiontomanagedevices', 'message_airnotifier');
        }

        if (!$this->is_system_configured()) {
            return get_string('notconfigured', 'message_airnotifier');
        } else {

            $airnotifiermanager = new message_airnotifier_manager();
            $devicetokens = $airnotifiermanager->get_user_devices($CFG->airnotifiermobileappname, $USER->id);

            if (!empty($devicetokens)) {
                $output = '';

                foreach ($devicetokens as $devicetoken) {

                    if ($devicetoken->enable) {
                        $hideshowiconname = 't/hide';
                        $dimmed = '';
                    } else {
                        $hideshowiconname = 't/show';
                        $dimmed = 'dimmed_text';
                    }

                    $hideshowicon = $OUTPUT->pix_icon($hideshowiconname, get_string('showhide', 'message_airnotifier'));
                    $name = "{$devicetoken->name} {$devicetoken->model} {$devicetoken->platform} {$devicetoken->version}";

                    $output .= html_writer::start_tag('li', array('id' => $devicetoken->id,
                                                                    'class' => 'airnotifierdevice ' . $dimmed)) . "\n";
                    $output .= html_writer::label($name, 'deviceid-' . $devicetoken->id, array('class' => 'devicelabel ')) . ' ' .
                        html_writer::link('#', $hideshowicon, array('class' => 'hidedevice', 'alt' => 'show/hide')) . "\n";
                    $output .= html_writer::end_tag('li') . "\n";
                }

                // Include the AJAX script to automatically trigger the action.
                $airnotifiermanager->include_device_ajax();

                $output = html_writer::tag('ul', $output, array('class' => 'list-unstyled unstyled',
                    'id' => 'airnotifierdevices'));
            } else {
                $output = get_string('nodevices', 'message_airnotifier');
            }
            return $output;
        }
    }
Esempio n. 2
0
 /**
  * Return the list of mobile devices that are registered in Moodle for the given user.
  *
  * @param  string  $appid  app unique id (usually a reversed domain)
  * @param  integer $userid the user id, 0 for current user
  * @return array warnings and devices
  * @throws moodle_exception
  * @since Moodle 3.2
  */
 public static function get_user_devices($appid, $userid = 0)
 {
     global $USER;
     $params = self::validate_parameters(self::get_user_devices_parameters(), array('appid' => $appid, 'userid' => $userid));
     $context = context_system::instance();
     self::validate_context($context);
     if (empty($params['userid'])) {
         $user = $USER;
     } else {
         $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
         core_user::require_active_user($user);
         // Allow only admins to retrieve other users devices.
         if ($user->id != $USER->id) {
             require_capability('moodle/site:config', $context);
         }
     }
     $warnings = array();
     $devices = array();
     // Check if mobile notifications are enabled.
     if (!self::is_system_configured()) {
         $warnings[] = array('item' => 'user', 'itemid' => $user->id, 'warningcode' => 'systemnotconfigured', 'message' => 'Mobile notifications are not configured');
     } else {
         // We catch exceptions here because get_user_devices may try to connect to Airnotifier.
         try {
             $manager = new message_airnotifier_manager();
             $devices = $manager->get_user_devices($appid, $user->id);
         } catch (Exception $e) {
             $warnings[] = array('item' => 'user', 'itemid' => $user->id, 'warningcode' => 'errorgettingdevices', 'message' => $e->getMessage());
         }
     }
     return array('devices' => $devices, 'warnings' => $warnings);
 }