Skip to content

Commit

Permalink
MDL-65451 tool_uploaduser: move code away from index.php, add CLI script
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Oct 6, 2020
1 parent 87afa4d commit 7a9c8cb
Show file tree
Hide file tree
Showing 14 changed files with 2,533 additions and 1,187 deletions.
402 changes: 402 additions & 0 deletions admin/tool/uploaduser/classes/cli_helper.php

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions admin/tool/uploaduser/classes/local/cli_progress_tracker.php
@@ -0,0 +1,43 @@
<?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/>.

/**
* Class cli_progress_tracker
*
* @package tool_uploaduser
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_uploaduser\local;

/**
* Tracks the progress of the user upload and outputs it in CLI script (writes to STDOUT)
*
* @package tool_uploaduser
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cli_progress_tracker extends text_progress_tracker {

/**
* Output one line (followed by newline)
* @param string $line
*/
protected function output_line(string $line): void {
cli_writeln($line);
}
}
124 changes: 124 additions & 0 deletions admin/tool/uploaduser/classes/local/text_progress_tracker.php
@@ -0,0 +1,124 @@
<?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/>.

/**
* Class text_progress_tracker
*
* @package tool_uploaduser
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_uploaduser\local;

/**
* Tracks the progress of the user upload and echos it in a text format
*
* @package tool_uploaduser
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class text_progress_tracker extends \uu_progress_tracker {

/**
* Print table header.
* @return void
*/
public function start() {
$this->_row = null;
}

/**
* Output one line (followed by newline)
* @param string $line
*/
protected function output_line(string $line): void {
echo $line . PHP_EOL;
}

/**
* Flush previous line and start a new one.
* @return void
*/
public function flush() {
if (empty($this->_row) or empty($this->_row['line']['normal'])) {
// Nothing to print - each line has to have at least number.
$this->_row = array();
foreach ($this->columns as $col) {
$this->_row[$col] = ['normal' => '', 'info' => '', 'warning' => '', 'error' => ''];
}
return;
}
$this->output_line(get_string('linex', 'tool_uploaduser', $this->_row['line']['normal']));
$prefix = [
'normal' => '',
'info' => '',
'warning' => get_string('warningprefix', 'tool_uploaduser') . ' ',
'error' => get_string('errorprefix', 'tool_uploaduser') . ' ',
];
foreach ($this->_row['status'] as $type => $content) {
if (strlen($content)) {
$this->output_line(' '.$prefix[$type].$content);
}
}

foreach ($this->_row as $key => $field) {
foreach ($field as $type => $content) {
if ($key !== 'status' && $type !== 'normal' && strlen($content)) {
$this->output_line(' ' . $prefix[$type] . $this->headers[$key] . ': ' .
str_replace("\n", "\n".str_repeat(" ", strlen($prefix[$type] . $this->headers[$key]) + 4), $content));
}
}
}
foreach ($this->columns as $col) {
$this->_row[$col] = ['normal' => '', 'info' => '', 'warning' => '', 'error' => ''];
}
}

/**
* Add tracking info
* @param string $col name of column
* @param string $msg message
* @param string $level 'normal', 'warning' or 'error'
* @param bool $merge true means add as new line, false means override all previous text of the same type
* @return void
*/
public function track($col, $msg, $level = 'normal', $merge = true) {
if (empty($this->_row)) {
$this->flush();
}
if (!in_array($col, $this->columns)) {
return;
}
if ($merge) {
if ($this->_row[$col][$level] != '') {
$this->_row[$col][$level] .= "\n";
}
$this->_row[$col][$level] .= $msg;
} else {
$this->_row[$col][$level] = $msg;
}
}

/**
* Print the table end
* @return void
*/
public function close() {
$this->flush();
$this->output_line(str_repeat('-', 79));
}
}
158 changes: 158 additions & 0 deletions admin/tool/uploaduser/classes/preview.php
@@ -0,0 +1,158 @@
<?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/>.

/**
* Class preview
*
* @package tool_uploaduser
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_uploaduser;

defined('MOODLE_INTERNAL') || die();

use tool_uploaduser\local\field_value_validators;

require_once($CFG->libdir.'/csvlib.class.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/uploaduser/locallib.php');

/**
* Display the preview of a CSV file
*
* @package tool_uploaduser
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class preview extends \html_table {

/** @var \csv_import_reader */
protected $cir;
/** @var array */
protected $filecolumns;
/** @var int */
protected $previewrows;
/** @var bool */
protected $noerror = true; // Keep status of any error.

/**
* preview constructor.
*
* @param \csv_import_reader $cir
* @param array $filecolumns
* @param int $previewrows
* @throws \coding_exception
*/
public function __construct(\csv_import_reader $cir, array $filecolumns, int $previewrows) {
parent::__construct();
$this->cir = $cir;
$this->filecolumns = $filecolumns;
$this->previewrows = $previewrows;

$this->id = "uupreview";
$this->attributes['class'] = 'generaltable';
$this->tablealign = 'center';
$this->summary = get_string('uploaduserspreview', 'tool_uploaduser');
$this->head = array();
$this->data = $this->read_data();

$this->head[] = get_string('uucsvline', 'tool_uploaduser');
foreach ($filecolumns as $column) {
$this->head[] = $column;
}
$this->head[] = get_string('status');

}

/**
* Read data
*
* @return array
* @throws \coding_exception
* @throws \dml_exception
* @throws \moodle_exception
*/
protected function read_data() {
global $DB, $CFG;

$data = array();
$this->cir->init();
$linenum = 1; // Column header is first line.
while ($linenum <= $this->previewrows and $fields = $this->cir->next()) {
$linenum++;
$rowcols = array();
$rowcols['line'] = $linenum;
foreach ($fields as $key => $field) {
$rowcols[$this->filecolumns[$key]] = s(trim($field));
}
$rowcols['status'] = array();

if (isset($rowcols['username'])) {
$stdusername = \core_user::clean_field($rowcols['username'], 'username');
if ($rowcols['username'] !== $stdusername) {
$rowcols['status'][] = get_string('invalidusernameupload');
}
if ($userid = $DB->get_field('user', 'id',
['username' => $stdusername, 'mnethostid' => $CFG->mnet_localhost_id])) {
$rowcols['username'] = \html_writer::link(
new \moodle_url('/user/profile.php', ['id' => $userid]), $rowcols['username']);
}
} else {
$rowcols['status'][] = get_string('missingusername');
}

if (isset($rowcols['email'])) {
if (!validate_email($rowcols['email'])) {
$rowcols['status'][] = get_string('invalidemail');
}

$select = $DB->sql_like('email', ':email', false, true, false, '|');
$params = array('email' => $DB->sql_like_escape($rowcols['email'], '|'));
if ($DB->record_exists_select('user', $select , $params)) {
$rowcols['status'][] = get_string('useremailduplicate', 'error');
}
}

if (isset($rowcols['theme'])) {
list($status, $message) = field_value_validators::validate_theme($rowcols['theme']);
if ($status !== 'normal' && !empty($message)) {
$rowcols['status'][] = $message;
}
}

// Check if rowcols have custom profile field with correct data and update error state.
$this->noerror = uu_check_custom_profile_data($rowcols) && $this->noerror;
$rowcols['status'] = implode('<br />', $rowcols['status']);
$data[] = $rowcols;
}
if ($fields = $this->cir->next()) {
$data[] = array_fill(0, count($fields) + 2, '...');
}
$this->cir->close();

return $data;
}

/**
* Getter for noerror
*
* @return bool
*/
public function get_no_error() {
return $this->noerror;
}
}

0 comments on commit 7a9c8cb

Please sign in to comment.