| 1 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
           // This file is part of Moodle - https://moodle.org/
  | 
        
        
            | 
            | 
           3 | 
           //
  | 
        
        
            | 
            | 
           4 | 
           // Moodle is free software: you can redistribute it and/or modify
  | 
        
        
            | 
            | 
           5 | 
           // it under the terms of the GNU General Public License as published by
  | 
        
        
            | 
            | 
           6 | 
           // the Free Software Foundation, either version 3 of the License, or
  | 
        
        
            | 
            | 
           7 | 
           // (at your option) any later version.
  | 
        
        
            | 
            | 
           8 | 
           //
  | 
        
        
            | 
            | 
           9 | 
           // Moodle is distributed in the hope that it will be useful,
  | 
        
        
            | 
            | 
           10 | 
           // but WITHOUT ANY WARRANTY; without even the implied warranty of
  | 
        
        
            | 
            | 
           11 | 
           // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  | 
        
        
            | 
            | 
           12 | 
           // GNU General Public License for more details.
  | 
        
        
            | 
            | 
           13 | 
           //
  | 
        
        
            | 
            | 
           14 | 
           // You should have received a copy of the GNU General Public License
  | 
        
        
            | 
            | 
           15 | 
           // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           /**
  | 
        
        
            | 
            | 
           18 | 
            * Provides {@link \tool_analytics\output\restorable_models} class.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package     tool_analytics
  | 
        
        
            | 
            | 
           21 | 
            * @category    output
  | 
        
        
            | 
            | 
           22 | 
            * @copyright   2019 David Mudrák <david@moodle.com>
  | 
        
        
            | 
            | 
           23 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           24 | 
            */
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           namespace tool_analytics\output;
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
            | 
            | 
           28 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           29 | 
              | 
        
        
            | 
            | 
           30 | 
           /**
  | 
        
        
            | 
            | 
           31 | 
            * Represents the list of default models that can be eventually restored.
  | 
        
        
            | 
            | 
           32 | 
            *
  | 
        
        
            | 
            | 
           33 | 
            * @copyright 2019 David Mudrák <david@moodle.com>
  | 
        
        
            | 
            | 
           34 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           35 | 
            */
  | 
        
        
            | 
            | 
           36 | 
           class restorable_models implements \renderable, \templatable {
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
               /** @var array */
  | 
        
        
            | 
            | 
           39 | 
               protected $models;
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
               /**
  | 
        
        
            | 
            | 
           42 | 
                * Instantiate an object of this class.
  | 
        
        
            | 
            | 
           43 | 
                *
  | 
        
        
            | 
            | 
           44 | 
                * @param array $models List of models as returned by {@link \core_analytics\manager::load_default_models_for_all_components()}
  | 
        
        
            | 
            | 
           45 | 
                */
  | 
        
        
            | 
            | 
           46 | 
               public function __construct(array $models) {
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
                   $this->models = $models;
  | 
        
        
            | 
            | 
           49 | 
               }
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
               /**
  | 
        
        
            | 
            | 
           52 | 
                * Export the list of models to be rendered.
  | 
        
        
            | 
            | 
           53 | 
                *
  | 
        
        
            | 
            | 
           54 | 
                * @param \renderer_base $output
  | 
        
        
            | 
            | 
           55 | 
                * @return string
  | 
        
        
            | 
            | 
           56 | 
                */
  | 
        
        
            | 
            | 
           57 | 
               public function export_for_template(\renderer_base $output) {
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
                   $components = [];
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
            | 
            | 
           61 | 
                   foreach ($this->models as $componentname => $modelslist) {
  | 
        
        
            | 
            | 
           62 | 
                       $component = [
  | 
        
        
            | 
            | 
           63 | 
                           'name' => $this->component_name($componentname),
  | 
        
        
            | 
            | 
           64 | 
                           'component' => $componentname,
  | 
        
        
            | 
            | 
           65 | 
                           'models' => [],
  | 
        
        
            | 
            | 
           66 | 
                       ];
  | 
        
        
            | 
            | 
           67 | 
              | 
        
        
            | 
            | 
           68 | 
                       foreach ($modelslist as $definition) {
  | 
        
        
            | 
            | 
           69 | 
                           list($target, $indicators) = \core_analytics\manager::get_declared_target_and_indicators_instances($definition);
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
                           if (\core_analytics\model::exists($target, $indicators)) {
  | 
        
        
            | 
            | 
           72 | 
                               continue;
  | 
        
        
            | 
            | 
           73 | 
                           }
  | 
        
        
            | 
            | 
           74 | 
              | 
        
        
            | 
            | 
           75 | 
                           $targetnamelangstring = $target->get_name();
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
                           $model = [
  | 
        
        
            | 
            | 
           78 | 
                               'defid' => \core_analytics\manager::model_declaration_identifier($definition),
  | 
        
        
            | 
            | 
           79 | 
                               'targetname' => $targetnamelangstring,
  | 
        
        
            | 
            | 
           80 | 
                               'targetclass' => $definition['target'],
  | 
        
        
            | 
            | 
           81 | 
                               'indicatorsnum' => count($definition['indicators']),
  | 
        
        
            | 
            | 
           82 | 
                               'indicators' => [],
  | 
        
        
            | 
            | 
           83 | 
                           ];
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
                           if (get_string_manager()->string_exists($targetnamelangstring->get_identifier().'_help',
  | 
        
        
            | 
            | 
           86 | 
                                   $targetnamelangstring->get_component())) {
  | 
        
        
            | 
            | 
           87 | 
                               $helpicon = new \help_icon($targetnamelangstring->get_identifier(), $targetnamelangstring->get_component());
  | 
        
        
            | 
            | 
           88 | 
                               $model['targethelp'] = $helpicon->export_for_template($output);
  | 
        
        
            | 
            | 
           89 | 
                           }
  | 
        
        
            | 
            | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
                           foreach ($indicators as $indicator) {
  | 
        
        
            | 
            | 
           92 | 
                               $indicatornamelangstring = $indicator->get_name();
  | 
        
        
            | 
            | 
           93 | 
                               $indicatordata = [
  | 
        
        
            | 
            | 
           94 | 
                                   'name' => $indicatornamelangstring,
  | 
        
        
            | 
            | 
           95 | 
                                   'classname' => $indicator->get_id(),
  | 
        
        
            | 
            | 
           96 | 
                               ];
  | 
        
        
            | 
            | 
           97 | 
              | 
        
        
            | 
            | 
           98 | 
                               if (get_string_manager()->string_exists($indicatornamelangstring->get_identifier().'_help',
  | 
        
        
            | 
            | 
           99 | 
                                       $indicatornamelangstring->get_component())) {
  | 
        
        
            | 
            | 
           100 | 
                                   $helpicon = new \help_icon($indicatornamelangstring->get_identifier(),
  | 
        
        
            | 
            | 
           101 | 
                                       $indicatornamelangstring->get_component());
  | 
        
        
            | 
            | 
           102 | 
                                   $indicatordata['indicatorhelp'] = $helpicon->export_for_template($output);
  | 
        
        
            | 
            | 
           103 | 
                               }
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
                               $model['indicators'][] = $indicatordata;
  | 
        
        
            | 
            | 
           106 | 
                           }
  | 
        
        
            | 
            | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
                           $component['models'][] = $model;
  | 
        
        
            | 
            | 
           109 | 
                       }
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
                       if (!empty($component['models'])) {
  | 
        
        
            | 
            | 
           112 | 
                           $components[] = $component;
  | 
        
        
            | 
            | 
           113 | 
                       }
  | 
        
        
            | 
            | 
           114 | 
                   }
  | 
        
        
            | 
            | 
           115 | 
              | 
        
        
            | 
            | 
           116 | 
                   $result = [
  | 
        
        
            | 
            | 
           117 | 
                       'hasdata' => !empty($components),
  | 
        
        
            | 
            | 
           118 | 
                       'components' => array_values($components),
  | 
        
        
            | 
            | 
           119 | 
                       'submiturl' => new \moodle_url('/admin/tool/analytics/restoredefault.php'),
  | 
        
        
            | 
            | 
           120 | 
                       'backurl' => new \moodle_url('/admin/tool/analytics/index.php'),
  | 
        
        
            | 
            | 
           121 | 
                       'sesskey' => sesskey(),
  | 
        
        
            | 
            | 
           122 | 
                   ];
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
                   return $result;
  | 
        
        
            | 
            | 
           125 | 
               }
  | 
        
        
            | 
            | 
           126 | 
              | 
        
        
            | 
            | 
           127 | 
               /**
  | 
        
        
            | 
            | 
           128 | 
                * Return a human readable name for the given frankenstyle component.
  | 
        
        
            | 
            | 
           129 | 
                *
  | 
        
        
            | 
            | 
           130 | 
                * @param string $component Frankenstyle component such as 'core', 'core_analytics' or 'mod_workshop'
  | 
        
        
            | 
            | 
           131 | 
                * @return string Human readable name of the component
  | 
        
        
            | 
            | 
           132 | 
                */
  | 
        
        
            | 
            | 
           133 | 
               protected function component_name(string $component): string {
  | 
        
        
            | 
            | 
           134 | 
              | 
        
        
            | 
            | 
           135 | 
                   if ($component === 'core' || strpos($component, 'core_')) {
  | 
        
        
            | 
            | 
           136 | 
                       return get_string('componentcore', 'tool_analytics');
  | 
        
        
            | 
            | 
           137 | 
              | 
        
        
            | 
            | 
           138 | 
                   } else {
  | 
        
        
            | 
            | 
           139 | 
                       return get_string('pluginname', $component);
  | 
        
        
            | 
            | 
           140 | 
                   }
  | 
        
        
            | 
            | 
           141 | 
               }
  | 
        
        
            | 
            | 
           142 | 
           }
  |