| 1 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
           // This file is part of Moodle - http://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 | 
            * This file contains the csv exporter for a competency framework.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package   tool_lpimportcsv
  | 
        
        
            | 
            | 
           21 | 
            * @copyright 2015 Damyon Wiese
  | 
        
        
            | 
            | 
           22 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           23 | 
            */
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           namespace tool_lpimportcsv;
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           use core_competency\api;
  | 
        
        
            | 
            | 
           30 | 
           use stdClass;
  | 
        
        
            | 
            | 
           31 | 
           use csv_export_writer;
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
           /**
  | 
        
        
            | 
            | 
           34 | 
            * Export Competency framework.
  | 
        
        
            | 
            | 
           35 | 
            *
  | 
        
        
            | 
            | 
           36 | 
            * @package   tool_lpimportcsv
  | 
        
        
            | 
            | 
           37 | 
            * @copyright 2015 Damyon Wiese
  | 
        
        
            | 
            | 
           38 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           39 | 
            */
  | 
        
        
            | 
            | 
           40 | 
           class framework_exporter {
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
               /** @var $framework \core_competency\competency_framework */
  | 
        
        
            | 
            | 
           43 | 
               protected $framework = null;
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
               /** @var $error string */
  | 
        
        
            | 
            | 
           46 | 
               protected $error = '';
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
               /**
  | 
        
        
            | 
            | 
           49 | 
                * Constructor
  | 
        
        
            | 
            | 
           50 | 
                * @param int $frameworkid The framework id
  | 
        
        
            | 
            | 
           51 | 
                */
  | 
        
        
            | 
            | 
           52 | 
               public function __construct($frameworkid) {
  | 
        
        
            | 
            | 
           53 | 
                   $this->framework = api::read_framework($frameworkid);
  | 
        
        
            | 
            | 
           54 | 
               }
  | 
        
        
            | 
            | 
           55 | 
              | 
        
        
            | 
            | 
           56 | 
               /**
  | 
        
        
            | 
            | 
           57 | 
                * Export all the competencies from this framework to a csv file.
  | 
        
        
            | 
            | 
           58 | 
                */
  | 
        
        
            | 
            | 
           59 | 
               public function export() {
  | 
        
        
            | 
            | 
           60 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           61 | 
                   require_once($CFG->libdir . '/csvlib.class.php');
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
                   $writer = new csv_export_writer();
  | 
        
        
            | 
            | 
           64 | 
                   $filename = clean_param($this->framework->get('shortname') . '-' . $this->framework->get('idnumber'), PARAM_FILE);
  | 
        
        
            | 
            | 
           65 | 
                   $writer->set_filename($filename);
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
                   $headers = framework_importer::list_required_headers();
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
                   $writer->add_data($headers);
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
                   // Order and number of columns must match framework_importer::list_required_headers().
  | 
        
        
            | 
            | 
           72 | 
                   $row = array(
  | 
        
        
            | 
            | 
           73 | 
                       '',
  | 
        
        
            | 
            | 
           74 | 
                       $this->framework->get('idnumber'),
  | 
        
        
            | 
            | 
           75 | 
                       $this->framework->get('shortname'),
  | 
        
        
            | 
            | 
           76 | 
                       $this->framework->get('description'),
  | 
        
        
            | 
            | 
           77 | 
                       $this->framework->get('descriptionformat'),
  | 
        
        
            | 
            | 
           78 | 
                       $this->framework->get_scale()->compact_items(),
  | 
        
        
            | 
            | 
           79 | 
                       $this->framework->get('scaleconfiguration'),
  | 
        
        
            | 
            | 
           80 | 
                       '',
  | 
        
        
            | 
            | 
           81 | 
                       '',
  | 
        
        
            | 
            | 
           82 | 
                       '',
  | 
        
        
            | 
            | 
           83 | 
                       '',
  | 
        
        
            | 
            | 
           84 | 
                       '',
  | 
        
        
            | 
            | 
           85 | 
                       true,
  | 
        
        
            | 
            | 
           86 | 
                       implode(',', $this->framework->get('taxonomies'))
  | 
        
        
            | 
            | 
           87 | 
                   );
  | 
        
        
            | 
            | 
           88 | 
                   $writer->add_data($row);
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
                   $filters = array('competencyframeworkid' => $this->framework->get('id'));
  | 
        
        
            | 
            | 
           91 | 
                   $competencies = api::list_competencies($filters);
  | 
        
        
            | 
            | 
           92 | 
                   // Index by id so we can lookup parents.
  | 
        
        
            | 
            | 
           93 | 
                   $indexed = array();
  | 
        
        
            | 
            | 
           94 | 
                   foreach ($competencies as $competency) {
  | 
        
        
            | 
            | 
           95 | 
                       $indexed[$competency->get('id')] = $competency;
  | 
        
        
            | 
            | 
           96 | 
                   }
  | 
        
        
            | 
            | 
           97 | 
                   foreach ($competencies as $competency) {
  | 
        
        
            | 
            | 
           98 | 
                       $parentidnumber = '';
  | 
        
        
            | 
            | 
           99 | 
                       if ($competency->get('parentid') > 0) {
  | 
        
        
            | 
            | 
           100 | 
                           $parent = $indexed[$competency->get('parentid')];
  | 
        
        
            | 
            | 
           101 | 
                           $parentidnumber = $parent->get('idnumber');
  | 
        
        
            | 
            | 
           102 | 
                       }
  | 
        
        
            | 
            | 
           103 | 
              | 
        
        
            | 
            | 
           104 | 
                       $scalevalues = '';
  | 
        
        
            | 
            | 
           105 | 
                       $scaleconfig = '';
  | 
        
        
            | 
            | 
           106 | 
                       if ($competency->get('scaleid') !== null) {
  | 
        
        
            | 
            | 
           107 | 
                           $scalevalues = $competency->get_scale()->compact_items();
  | 
        
        
            | 
            | 
           108 | 
                           $scaleconfig = $competency->get('scaleconfiguration');
  | 
        
        
            | 
            | 
           109 | 
                       }
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
                       $ruleconfig = $competency->get('ruleconfig');
  | 
        
        
            | 
            | 
           112 | 
                       if ($ruleconfig === null) {
  | 
        
        
            | 
            | 
           113 | 
                           $ruleconfig = "null";
  | 
        
        
            | 
            | 
           114 | 
                       }
  | 
        
        
            | 
            | 
           115 | 
              | 
        
        
            | 
            | 
           116 | 
                       $allrelated = $competency->get_related_competencies();
  | 
        
        
            | 
            | 
           117 | 
              | 
        
        
            | 
            | 
           118 | 
                       $relatedidnumbers = array();
  | 
        
        
            | 
            | 
           119 | 
                       foreach ($allrelated as $onerelated) {
  | 
        
        
            | 
            | 
           120 | 
                           $relatedidnumbers[] = str_replace(',', '%2C', $onerelated->get('idnumber'));
  | 
        
        
            | 
            | 
           121 | 
                       }
  | 
        
        
            | 
            | 
           122 | 
                       $relatedidnumbers = implode(',', $relatedidnumbers);
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
                       // Order and number of columns must match framework_importer::list_required_headers().
  | 
        
        
            | 
            | 
           125 | 
                       $row = array(
  | 
        
        
            | 
            | 
           126 | 
                           $parentidnumber,
  | 
        
        
            | 
            | 
           127 | 
                           $competency->get('idnumber'),
  | 
        
        
            | 
            | 
           128 | 
                           $competency->get('shortname'),
  | 
        
        
            | 
            | 
           129 | 
                           $competency->get('description'),
  | 
        
        
            | 
            | 
           130 | 
                           $competency->get('descriptionformat'),
  | 
        
        
            | 
            | 
           131 | 
                           $scalevalues,
  | 
        
        
            | 
            | 
           132 | 
                           $scaleconfig,
  | 
        
        
            | 
            | 
           133 | 
                           $competency->get('ruletype'),
  | 
        
        
            | 
            | 
           134 | 
                           $competency->get('ruleoutcome'),
  | 
        
        
            | 
            | 
           135 | 
                           $ruleconfig,
  | 
        
        
            | 
            | 
           136 | 
                           $relatedidnumbers,
  | 
        
        
            | 
            | 
           137 | 
                           $competency->get('id'),
  | 
        
        
            | 
            | 
           138 | 
                           false,
  | 
        
        
            | 
            | 
           139 | 
                           ''
  | 
        
        
            | 
            | 
           140 | 
                       );
  | 
        
        
            | 
            | 
           141 | 
              | 
        
        
            | 
            | 
           142 | 
                       $writer->add_data($row);
  | 
        
        
            | 
            | 
           143 | 
                   }
  | 
        
        
            | 
            | 
           144 | 
              | 
        
        
            | 
            | 
           145 | 
                   $writer->download_file();
  | 
        
        
            | 
            | 
           146 | 
               }
  | 
        
        
            | 
            | 
           147 | 
           }
  |