<commit_before><?php

////////////////////////////////////////////////////////////////////////////
/// Blackboard 6.x Format
///
/// This Moodle class provides all functions necessary to import and export
///
///
////////////////////////////////////////////////////////////////////////////

// Based on default.php, included by ../import.php
/**
 * @package questionbank
 * @subpackage importexport
 */
require_once ("$CFG->libdir/xmlize.php");

class qformat_blackboard_six extends qformat_default {
    function provide_import() {
        return true;
    }
    
    
    //Function to check and create the needed dir to unzip file to
    function check_and_create_import_dir($unique_code) {

        global $CFG; 

        $status = $this->check_dir_exists($CFG->dataroot."/temp",true);
        if ($status) {
            $status = $this->check_dir_exists($CFG->dataroot."/temp/bbquiz_import",true);
        }
        if ($status) {
            $status = $this->check_dir_exists($CFG->dataroot."/temp/bbquiz_import/".$unique_code,true);
        }
        
        return $status;
    }
    
    function clean_temp_dir($dir='') {
        // for now we will just say everything happened okay note 
        // that a mess may be piling up in $CFG->dataroot/temp/bbquiz_import
        return true;
        
        if ($dir == '') {
            $dir = $this->temp_dir;   
        }
        $slash = "/";

        // Create arrays to store files and directories
        $dir_files      = array();
        $dir_subdirs    = array();

        // Make sure we can delete it
        chmod($dir, 0777);

        if ((($handle = opendir($dir))) == FALSE) {
            // The directory could not be opened
            return false;
        }

        // Loop through all directory entries, and construct two temporary arrays containing files and sub directories
        while(false !== ($entry = readdir($handle))) {
            if (is_dir($dir. $slash .$entry) && $entry != ".." && $entry != ".") {
                $dir_subdirs[] = $dir. $slash .$entry;
            }
            else if ($entry != ".." && $entry != ".") {
                $dir_files[] = $dir. $slash .$entry;
            }
        }

        // Delete all files in the curent directory return false and halt if a file cannot be removed
        for($i=0; $i<count($dir_files); $i++) {
            chmod($dir_files[$i], 0777);
            if (((unlink($dir_files[$i]))) == FALSE) {
                return false;
            }
        }

        // Empty sub directories and then remove the directory
        for($i=0; $i<count($dir_subdirs); $i++) {
            chmod($dir_subdirs[$i], 0777);
            if ($this->clean_temp_dir($dir_subdirs[$i]) == FALSE) {
                return false;
            }
            else {
                if (rmdir($dir_subdirs[$i]) == FALSE) {
                return false;
                }
            }
        }

        // Close directory
        closedir($handle);
        if (rmdir($this->temp_dir) == FALSE) {
            return false;    
        }
        // Success, every thing is gone return true
        return true;
    }
    
    //Function to check if a directory exists and, optionally, create it
    function check_dir_exists($dir,$create=false) {

        global $CFG; 

        $status = true;
        if(!is_dir($dir)) {
            if (!$create) {
                $status = false;
            } else {
                umask(0000);
                $status = mkdir ($dir,$CFG->directorypermissions);
            }
        }
        return $status;
    }

    function importpostprocess() {
    /// Does any post-processing that may be desired
    /// Argument is a simple array of question ids that 
    /// have just been added.
    
        // need to clean up temporary directory
        return $this->clean_temp_dir();
    }

    function copy_file_to_course($filename) {
        global $CFG, $COURSE;
        $filename = str_replace('\\','/',$filename);
        $fullpath = $this->temp_dir.'/res00001/'.$filename;
        $basename = basename($filename);
    
        $copy_to = $CFG->dataroot.'/'.$COURSE->id.'/bb_import';
        
        if ($this->check_dir_exists($copy_to,true)) {
            if(is_readable($fullpath)) {
                $copy_to.= '/'.$basename;
                if (!copy($fullpath, $copy_to)) {
                    return false;
                }
                else {
                    return $copy_to;
                }
            }
        }
        else {
            return false;   
        }
    }

    function readdata($filename) {
    /// Returns complete file with an array, one item per line
        global $CFG;
  
        // if the extension is .dat we just return that,
        // if .zip we unzip the file and get the data
        $ext = substr($this->realfilename, strpos($this->realfilename,'.'), strlen($this->realfilename)-1);      
        if ($ext=='.dat') {
            if (!is_readable($filename)) {
                print_error('filenotreadable', 'error');	
            }		
            return file($filename);
        }	  
        
        $unique_code = time();
        $temp_dir = $CFG->dataroot."/temp/bbquiz_import/".$unique_code;
        $this->temp_dir = $temp_dir;
        if ($this->check_and_create_import_dir($unique_code)) {
            if(is_readable($filename)) {
                if (!copy($filename, "$temp_dir/bboard.zip")) {
                    print_error('cannotcopybackup', 'question');
                }
                if(unzip_file("$temp_dir/bboard.zip", '', false)) {
                    // assuming that the information is in res0001.dat
                    // after looking at 6 examples this was always the case
                    $q_file = "$temp_dir/res00001.dat";
                    if (is_file($q_file)) {
                        if (is_readable($q_file)) {
                            $filearray = file($q_file);
                            /// Check for Macintosh OS line returns (ie file on one line), and fix
                            if (ereg("\r", $filearray[0]) AND !ereg("\n", $filearray[0])) {
                                return explode("\r", $filearray[0]);
                            } else {
                                return $filearray;
                            }
                            return false;        
                        }
                    }
                    else {
                        print_error('cannotfindquestionfile', 'questioni');   
                    }
                }
                else {<commit_msg>MDL-18644 Bugs found by syntax checker, merged from MOODLE_19_STABLE<commit_after><?php

////////////////////////////////////////////////////////////////////////////
/// Blackboard 6.x Format
///
/// This Moodle class provides all functions necessary to import and export
///
///
////////////////////////////////////////////////////////////////////////////

// Based on default.php, included by ../import.php
/**
 * @package questionbank
 * @subpackage importexport
 */
require_once ("$CFG->libdir/xmlize.php");

class qformat_blackboard_six extends qformat_default {
    function provide_import() {
        return true;
    }
    
    
    //Function to check and create the needed dir to unzip file to
    function check_and_create_import_dir($unique_code) {

        global $CFG; 

        $status = $this->check_dir_exists($CFG->dataroot."/temp",true);
        if ($status) {
            $status = $this->check_dir_exists($CFG->dataroot."/temp/bbquiz_import",true);
        }
        if ($status) {
            $status = $this->check_dir_exists($CFG->dataroot."/temp/bbquiz_import/".$unique_code,true);
        }
        
        return $status;
    }
    
    function clean_temp_dir($dir='') {
        // for now we will just say everything happened okay note 
        // that a mess may be piling up in $CFG->dataroot/temp/bbquiz_import
        // TODO return true at top of the function renders all the following code useless
        return true;
        
        if ($dir == '') {
            $dir = $this->temp_dir;   
        }
        $slash = "/";

        // Create arrays to store files and directories
        $dir_files      = array();
        $dir_subdirs    = array();

        // Make sure we can delete it
        chmod($dir, 0777);

        if ((($handle = opendir($dir))) == FALSE) {
            // The directory could not be opened
            return false;
        }

        // Loop through all directory entries, and construct two temporary arrays containing files and sub directories
        while(false !== ($entry = readdir($handle))) {
            if (is_dir($dir. $slash .$entry) && $entry != ".." && $entry != ".") {
                $dir_subdirs[] = $dir. $slash .$entry;
            }
            else if ($entry != ".." && $entry != ".") {
                $dir_files[] = $dir. $slash .$entry;
            }
        }

        // Delete all files in the curent directory return false and halt if a file cannot be removed
        for($i=0; $i<count($dir_files); $i++) {
            chmod($dir_files[$i], 0777);
            if (((unlink($dir_files[$i]))) == FALSE) {
                return false;
            }
        }

        // Empty sub directories and then remove the directory
        for($i=0; $i<count($dir_subdirs); $i++) {
            chmod($dir_subdirs[$i], 0777);
            if ($this->clean_temp_dir($dir_subdirs[$i]) == FALSE) {
                return false;
            }
            else {
                if (rmdir($dir_subdirs[$i]) == FALSE) {
                return false;
                }
            }
        }

        // Close directory
        closedir($handle);
        if (rmdir($this->temp_dir) == FALSE) {
            return false;    
        }
        // Success, every thing is gone return true
        return true;
    }
    
    //Function to check if a directory exists and, optionally, create it
    function check_dir_exists($dir,$create=false) {

        global $CFG; 

        $status = true;
        if(!is_dir($dir)) {
            if (!$create) {
                $status = false;
            } else {
                umask(0000);
                $status = mkdir ($dir,$CFG->directorypermissions);
            }
        }
        return $status;
    }

    function importpostprocess() {
    /// Does any post-processing that may be desired
    /// Argument is a simple array of question ids that 
    /// have just been added.
    
        // need to clean up temporary directory
        return $this->clean_temp_dir();
    }

    function copy_file_to_course($filename) {
        global $CFG, $COURSE;
        $filename = str_replace('\\','/',$filename);
        $fullpath = $this->temp_dir.'/res00001/'.$filename;
        $basename = basename($filename);
    
        $copy_to = $CFG->dataroot.'/'.$COURSE->id.'/bb_import';
        
        if ($this->check_dir_exists($copy_to,true)) {
            if(is_readable($fullpath)) {
                $copy_to.= '/'.$basename;
                if (!copy($fullpath, $copy_to)) {
                    return false;
                }
                else {
                    return $copy_to;
                }
            }
        }
        else {
            return false;   
        }
    }

    function readdata($filename) {
    /// Returns complete file with an array, one item per line
        global $CFG;
  
        // if the extension is .dat we just return that,
        // if .zip we unzip the file and get the data
        $ext = substr($this->realfilename, strpos($this->realfilename,'.'), strlen($this->realfilename)-1);      
        if ($ext=='.dat') {
            if (!is_readable($filename)) {
                print_error('filenotreadable', 'error');    
            }       
            return file($filename);
        }     
        
        $unique_code = time();
        $temp_dir = $CFG->dataroot."/temp/bbquiz_import/".$unique_code;
        $this->temp_dir = $temp_dir;
        if ($this->check_and_create_import_dir($unique_code)) {
            if(is_readable($filename)) {
                if (!copy($filename, "$temp_dir/bboard.zip")) {
                    print_error('cannotcopybackup', 'question');
                }
                if(unzip_file("$temp_dir/bboard.zip", '', false)) {
                    // assuming that the information is in res0001.dat
                    // after looking at 6 examples this was always the case
                    $q_file = "$temp_dir/res00001.dat";
                    if (is_file($q_file)) {
                        if (is_readable($q_file)) {
                            $filearray = file($q_file);
                            /// Check for Macintosh OS line returns (ie file on one line), and fix
                            if (ereg("\r", $filearray[0]) AND !ereg("\n", $filearray[0])) {
                                return explode("\r", $filearray[0]);
                            } else {
                                return $filearray;
                            }
                        }
                    }
                    else {
                        print_error('cannotfindquestionfile', 'questioni');   
                    }
                }
                else {
{'fraction_non_alphanumeric': 0.14787723399441974, 'fraction_numerical': 0.006258954829952492, 'mean_word_length': 1.3813970192135032, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1874432', 'n_tokens_mistral': 3702, 'n_tokens_neox': 3523, 'n_words': 884}