. /** * Bulk course registration functions * * @package tool * @subpackage uploadcoursecategory * @copyright 2004 onwards Martin Dougiamas (http://dougiamas.com) * @copyright 2012 Piers Harding * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); define('CC_COURSE_ADDNEW', 0); define('CC_COURSE_ADDINC', 1); define('CC_COURSE_ADD_UPDATE', 2); define('CC_COURSE_UPDATE', 3); define('CC_UPDATE_NOCHANGES', 0); define('CC_UPDATE_FILEOVERRIDE', 1); define('CC_UPDATE_ALLOVERRIDE', 2); define('CC_UPDATE_MISSING', 3); define('CC_BULK_NONE', 0); define('CC_BULK_NEW', 1); define('CC_BULK_UPDATED', 2); define('CC_BULK_ALL', 3); define('CC_PWRESET_NONE', 0); define('CC_PWRESET_WEAK', 1); define('CC_PWRESET_ALL', 2); /** * Tracking of processed courses. * * This class prints course information into a html table. * * @package core * @subpackage admin * @copyright 2007 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class cc_progress_tracker { private $_row; public $columns = array('status', 'line', 'id', 'name', 'idnumber', 'description', 'oldname', 'deleted'); /** * Print table header. * @return void */ public function start() { $ci = 0; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; $this->_row = null; } /** * 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] = array('normal'=>'', 'info'=>'', 'warning'=>'', 'error'=>''); } return; } $ci = 0; $ri = 1; echo ''; foreach ($this->_row as $key=>$field) { foreach ($field as $type=>$content) { if ($field[$type] !== '') { $field[$type] = ''.$field[$type].''; } else { unset($field[$type]); } } echo ''; } echo ''; foreach ($this->columns as $col) { $this->_row[$col] = array('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(); //init arrays } if (!in_array($col, $this->columns)) { debugging('Incorrect column:'.$col); return; } if ($merge) { if ($this->_row[$col][$level] != '') { $this->_row[$col][$level] .='
'; } $this->_row[$col][$level] .= $msg; } else { $this->_row[$col][$level] = $msg; } } /** * Print the table end * @return void */ public function close() { $this->flush(); echo '
'.get_string('status').''.get_string('cccsvline', 'tool_uploadcoursecategory').'ID'.get_string('name').''.get_string('idnumber').''.get_string('description').''.get_string('oldnamecoursecategory', 'tool_uploadcoursecategory').''.get_string('delete').'
'; if (!empty($field)) { echo implode('
', $field); } else { echo ' '; } echo '
'; } } /** * Validation callback function - verified the column line of csv file. * Converts standard column names to lowercase. * @param csv_import_reader $cir * @param array $stdfields standard coursecategory fields * @param moodle_url $returnurl return url in case of any error * @return array list of fields */ function cc_validate_coursecategory_upload_columns(csv_import_reader $cir, $stdfields, moodle_url $returnurl) { $columns = $cir->get_columns(); if (empty($columns)) { $cir->close(); $cir->cleanup(); print_error('cannotreadtmpfile', 'error', $returnurl); } if (count($columns) < 2) { $cir->close(); $cir->cleanup(); print_error('csvfewcolumns', 'error', $returnurl); } // test columns $processed = array(); foreach ($columns as $key=>$unused) { $field = $columns[$key]; $lcfield = core_text::strtolower($field); if (in_array($field, $stdfields) or in_array($lcfield, $stdfields)) { // standard fields are only lowercase $newfield = $lcfield; } else { $cir->close(); $cir->cleanup(); print_error('invalidfieldname', 'error', $returnurl, $field); } if (in_array($newfield, $processed)) { $cir->close(); $cir->cleanup(); print_error('duplicatefieldname', 'error', $returnurl, $newfield); } $processed[$key] = $newfield; } return $processed; } /** * Increments name - increments trailing number or adds it if not present. * Varifies that the new name does not exist yet * @param string $name * @return incremented name which does not exist yet */ function cc_increment_name($name) { global $DB, $CFG; // make sure we get just the leaf of the parent/category tree $categories = explode('/', $name); $name = array_pop($categories); if (!preg_match_all('/(.*?)([0-9]+)$/', $name, $matches)) { $name = $name.'2'; } else { $name = $matches[1][0].($matches[2][0]+1); } if ($DB->record_exists('course_categories', array('name'=>$name))) { return cc_increment_name($name); } else { return $name; } } /** * Increments idnumber - increments trailing number or adds it if not present. * Varifies that the new idnumber does not exist yet * @param string $idnumber * @return incremented idnumber which does not exist yet */ function cc_increment_idnumber($idnumber) { global $DB, $CFG; if (!preg_match_all('/(.*?)([0-9]+)$/', $idnumber, $matches)) { $idnumber = $idnumber.'2'; } else { $idnumber = $matches[1][0].($matches[2][0]+1); } if ($DB->record_exists('course_categories', array('idnumber'=>$idnumber))) { return cc_increment_idnumber($idnumber); } else { return $idnumber; } } /** * Check if default field contains templates and apply them. * @param string template - potential tempalte string * @param object course object- we need coursename, firstname and lastname * @return string field value */ function cc_process_template($template, $course) { if (is_array($template)) { // hack for for support of text editors with format $t = $template['text']; } else { $t = $template; } if (strpos($t, '%') === false) { return $template; } $name = isset($course->name) ? $course->name : ''; $description = isset($course->description) ? $course->description : ''; $idnumber = isset($course->idnumber) ? $course->idnumber : ''; $callback = partial('cc_process_template_callback', $name, $description, $idnumber); $result = preg_replace_callback('/(?