Skip to content
Snippets Groups Projects
Select Git revision
  • master default
  • disable-new-requests
  • fix-bulletin-view-missing-notes-error
  • add-missing-queue-managers
  • projects-task-53
  • projects-task-51
  • projects-task-43
  • projects-task-24
  • projects-task-31
  • projects-task-32
  • projects-task-8
  • project-setup-docs
  • projects-task-28
  • projects-task-27
  • projects-task-9
  • projects-task-7
  • mass-update-course-codes-in-sections
  • wdn-four
  • learning-outcomes
  • additional-bulletin-pages
  • svn-redesign
  • svn-popups
  • svn-trunk
  • svn-performance
  • svn-tim
25 results

AdminController.php

Blame
  • AdminController.php 21.40 KiB
    <?php
    
    class Bulletin_AdminController extends Creq_Controller_Action
    {
        public function preDispatch()
        {
            $user = Auth_UserModel::findCurrentUser();
            $roles = Auth_GroupModel::findByUser($user);
            if (!in_array(1, $roles->getId())) {
                throw new Exception('You must be logged in to view this page.');
            }
        }
    
        public function changeMajorNameAction()
        {
            $this->view->messages = $this->_helper->getHelper('FlashMessenger')->getMessages();
    
            $colleges = array();
            $collegeMajors = Bulletin_UtilityModel::getMajors();
            foreach ($collegeMajors as $college => $majors) {
                $colleges[$college] = $college;
                asort($collegeMajors[$college]);
                $collegeMajors[$college] = array('_college' => 'College Information') + $collegeMajors[$college];
            }
            $this->view->colleges = $colleges;
            $this->view->collegeMajors = $collegeMajors;
        }
    
        public function editMajorFileContentsAction()
        {
            // get post data and validate
            $in = $this->getAllParams();
            if ($in['college'] == '_null') {
                $in['college'] = NULL;
            }
            if ($in['major'] == '_college') {
                $in['major'] = NULL;
            }
    
            if (!$in['college']) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('You must select a college/major.');
                $this->redirect('/bulletin/admin/change-major-name');
            }
    
            if (!$in['major']) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('You must select a college/major.');
                $this->redirect('/bulletin/admin/change-major-name');
            }
    
            $activeRequests = Bulletin_SectionModel::findActiveRequestByCollegeAndMajorId($in['college'], $in['major']);
            foreach ($activeRequests as $activeRequest) {
                $href = $this->view->baseUrl('/requests/view/index/id/' . $activeRequest->getId());
                $this->_helper->getHelper('FlashMessenger')->addMessage(
                        "There is an <a href=\"$href\">active request</a> for that college/major. Complete the current request before attempting to change the major name."
                );
                $this->redirect('/bulletin/admin/change-major-name');
            }
    
            if (!$in['new-major']) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('You must enter a new major name.');
                $this->redirect('/bulletin/admin/change-major-name');
            }
    
            // prepare a branch for major name update
            Bulletin_RepositoryModel::getInstance()->pull();
            $branch = "major-" . $in['major'] . "-update-" . date_format(date_create(), 'Ymd');
            $user = Auth_UserModel::findCurrentUser();
    
            // get current editing year
            $currentYear = $this->getCurrentEditingYear();
    
            // get major name
            $db = Zend_Registry::get('db');
            $select = new Zend_Db_Select($db);
            $select->from('creqMajors');
            $select->where('majorId = ?', $in['major']);
            $record = $select->query()->fetch();
            $major = $record['name'];
    
            // get file path and name for major
            $filePath = 'data' .DIRECTORY_SEPARATOR. Bulletin_RepositoryModel::getInstance()->getFilePathForCollegeMajorYear($in['college'], $major, $currentYear);
            if (!is_file($filePath)) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('Unable to find an existing file for that college/major.');
                $this->redirect('/bulletin/admin/change-major-name');
            }
    
            // update file contents to new major name
            $fileContents = Bulletin_RepositoryModel::getInstance()->getFileContents($filePath, $branch);
    
            // show view
            $this->view->file_contents = $fileContents;
            $this->view->current_college = $in['college'];
            $this->view->current_major = $major;
            $this->view->current_major_id = $in['major'];
            $this->view->new_major = $in['new-major'];
        }
    
        public function changeMajorNamePostAction()
        {
            // get post data and validate
            $in = $this->getAllParams();
    
            if (!$in['college']) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('You must select a college/major.');
                $this->redirect('/bulletin/admin/change-major-name');
            }
    
            if (!$in['major']) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('You must select a college/major.');
                $this->redirect('/bulletin/admin/change-major-name');
            }
    
            $activeRequests = Bulletin_SectionModel::findActiveRequestByCollegeAndMajorId($in['college'], $in['major']);
            foreach ($activeRequests as $activeRequest) {
                $href = $this->view->baseUrl('/requests/view/index/id/' . $activeRequest->getId());
                $this->_helper->getHelper('FlashMessenger')->addMessage(
                        "There is an <a href=\"$href\">active request</a> for that college/major. Complete the current request before attempting to change the major name."
                );
                $this->redirect('/bulletin/admin/change-major-name');
            }
    
            if (!$in['new-major']) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('You must enter a new major name.');
                $this->redirect('/bulletin/admin/change-major-name');
            }
    
            if (!$in['file-contents']) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('You must supply new file contents for the major.');
                $this->redirect('/bulletin/admin/change-major-name');
            }
    
            // prepare a branch for major name update
            Bulletin_RepositoryModel::getInstance()->pull();
            $branch = "major-" . $in['major'] . "-update-" . date_format(date_create(), 'Ymd');
            $user = Auth_UserModel::findCurrentUser();
    
            // get current editing year
            $currentYear = $this->getCurrentEditingYear();
    
            // get major name
            $db = Zend_Registry::get('db');
            $select = new Zend_Db_Select($db);
            $select->from('creqMajors');
            $select->where('majorId = ?', $in['major']);
            $record = $select->query()->fetch();
            $major = $record['name'];
    
            // get file path and name for major
            $filePath = 'data' .DIRECTORY_SEPARATOR. Bulletin_RepositoryModel::getInstance()->getFilePathForCollegeMajorYear($in['college'], $major, $currentYear);
            if (!is_file($filePath)) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('Unable to find an existing file for that college/major.');
                $this->redirect('/bulletin/admin/change-major-name');
            }
    
            // get file content edits
            $fileContents = $in['file-contents'];
            $fileContents = $this->cleanupHtml($fileContents);
    
            /* NOT USED
             // modify page title
            $fileContents = str_replace('<title>'.htmlentities($major),'<title>'.htmlentities($in['new-major']),$fileContents);
            // modify major name section at top of page
            $oldDivId = preg_replace('/[^a-zA-Z0-9]+/', '-', strtolower($major));
            $newDivId = preg_replace('/[^a-zA-Z0-9]+/', '-', strtolower($in['new-major']));
            $fileContents = str_replace('<div id="'.$oldDivId,'<div id="'.$newDivId,$fileContents);
            $fileContents = str_replace('<p class="major-name">'.htmlentities($major).'</p>','<p class="major-name">'.htmlentities($in['new-major']).'</p>',$fileContents);
            // modify major name in quickpoints
            $fileContents = str_replace('<p class="quick-points"><span class="quick-point-bold">MAJOR:</span> '.htmlentities($major).'</p>','<p class="quick-points"><span class="quick-point-bold">MAJOR:</span> '.htmlentities($in['new-major']).'</p>',$fileContents);
            */
    
            // save file contents
            Bulletin_RepositoryModel::getInstance()->saveFileContents($filePath, $branch, $user, $fileContents);
    
            // rename file
            $fpParts = pathinfo($filePath);
            Bulletin_RepositoryModel::getInstance()->moveFile($filePath, $fpParts['dirname'].DIRECTORY_SEPARATOR.$in['new-major'].'.'.$fpParts['extension'], $branch, $user);
    
            // update major name in database
            $db = Zend_Registry::get('db');
            $update_data = array('name' => $in['new-major']);
            $db->update('creqMajors', $update_data, 'majorId = '.$in['major']);
    
            // merge branch into master and push to github
            Bulletin_RepositoryModel::getInstance()->mergeBranchToMaster($branch);
    
            // submist pull request to github
            Bulletin_RepositoryModel::getInstance()->pullRequest("master","Updating Major Name","Updating major '" . $major . "' to '" . $in['new-major'] . "'.");
    
            // success
            $this->_helper->getHelper('FlashMessenger')->addMessage("Major '" . $major . "' updated to '" . $in['new-major'] . "' successfully.");
            $this->redirect('/bulletin/admin/change-major-name');
        }
    
        public function addGitNoteAction()
        {
            $this->view->messages = $this->_helper->getHelper('FlashMessenger')->getMessages();
        }
    
        public function addGitNotePostAction()
        {
            // get post data and validate
            $in = $this->getAllParams();
    
            if (!$in['request_id']) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('You must enter a request id.');
                $this->redirect('/bulletin/admin/add-git-note');
            }
            if (!$in['git-note']) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('You must enter a git note.');
                $this->redirect('/bulletin/admin/add-git-note');
            }
    
            $id = $in['request_id'];
            $notes = $in['git-note'];
    
            // take request id and lookup information
            $request = Requests_RequestModel::find($id);
            $section = Bulletin_SectionModel::findWithRequest($request);
            $section = clone $section;
            $path = 'data/' . Bulletin_RepositoryModel::getInstance()->getFilePathForSection($section);
            $branch = 'request-' . $request->getId();
    
            // store git note
            Bulletin_RepositoryModel::getInstance()->saveFileNotes($path, $branch, $notes);
    
            // success
            $this->_helper->getHelper('FlashMessenger')->addMessage('Git note successfully added to request ' . $id);
            $this->redirect('/bulletin/admin/add-git-note');
        }
    
        public function findFilesWithoutGitnotesAction()
        {
            // could take a very long time
            set_time_limit(600);
    
            // get post data
            // https://creq.unl.edu/bulletin/admin/find-files-without-gitnotes/major/113
            $in = $this->getAllParams();
            $post_majorId = $in['major'];
            //$post_majorId = 113;
            if(!$post_majorId) {
                exit;
            }
    
            // initialize
            $repoPath = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'bulletin.git';
            $found_majors = array();
            $missing_files = array();
    
            // get majors list
            $collegeMajors = Bulletin_UtilityModel::getMajors();
    
            // get current editing year
            $currentYear = $this->getCurrentEditingYear();
    
            $log = array();
            $i = 0;
            $j = 0;
            $log[$i] = "";
    
            // change dir to repo path
            $return = NULL;
            $output = array();
            exec('cd ' . escapeshellarg($repoPath), $output, $return);
            $output = implode(PHP_EOL, $output);
            //$log[$i] .= "<br />".'cd ' . escapeshellarg($repoPath);
            //$log[$i] .= "<br />".$return;
            //$log[$i] .= "<br />".$output;
    
            // git status
            /* $return = NULL;
            $output = array();
            exec('cd ' . escapeshellarg($repoPath) . ' && git status', $output, $return);
            $output = implode(PHP_EOL, $output);
            $log[$i] .= "<br />".$return;
            $log[$i] .= "<br />".$output; */
    
            // handle windows cmd
            if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
                $return = NULL;
                $output = array();
                //exec('set TERM=msys', $output, $return);
            }
    
            // change directory pointer to repo root
            $cwd = getcwd();
            chdir($repoPath);
    
            // loop through colleges
            foreach ($collegeMajors as $college => $majors) {
                // loop through majors
                foreach (array_keys($majors) as $majorId) {
                    if ($majorId == $post_majorId) {
                        $major = $majors[$majorId];
    
                        // loop through all requests of the major
                        $requests = Bulletin_SectionModel::findAllRequestsByCollegeAndMajorId($college, $majorId);
                        $requestSections = Bulletin_SectionModel::findWithRequest($requests);
                        foreach ($requests as $request) {
                            $j += 1;
                            if ($j > 500) {
                                //break;
                            }
    
                            $section = $requestSections[$request->getId()];
                            $parentCommit = Bulletin_RepositoryModel::getInstance()->getParentCommitForSection($section);
                            //throw new Exception($parentCommit.";".$request->getId().";".$repoPath.DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR.Bulletin_RepositoryModel::getInstance()->getFilePathForSection($section));
    
                            $path = $repoPath.DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR . Bulletin_RepositoryModel::getInstance()->getFilePathForSection($section);
                            if (!file_exists($path)) {
                                $missing_files[$college][$major] = $path;
                            } else {
                                $branch = $parentCommit;
                                $result = $this->getGitNote($path, $branch, $log);
                                if ($result === false) {
                                    $found_majors[$college][$major][$branch] = $branch;
                                }
    
                                $branch = 'request-' . $request->getId();
                                $result = $this->getGitNote($path, $branch, $log);
                                if ($result === false) {
                                    $found_majors[$college][$major][$branch] = $branch;
                                }
                            }
                        }
                    }
    
                    //break;
                }
                //break;
            }
    
            // change directory pointer back
            chdir($cwd);
    
            $this->view->log = $log;
            $this->view->missingFiles = $missing_files;
            $this->view->collegeMajors = $found_majors;
        }
    
        protected function getGitNote($filePath, $branch, &$log)
        {
            $note = "";
            $i = $branch;
    
            if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
                $filePath = str_replace("/","\\",$filePath);
            }
    
            // checkout branch
            $log[$i] .= "<br />".'git checkout ' . escapeshellarg($branch);
            $return = NULL;
            $output = array();
            exec('git checkout ' . escapeshellarg($branch), $output, $return);
            $output = implode(PHP_EOL, $output);
            $log[$i] .= "<br />".$return;
            //$log[$i] .= "<br />".$output;
            if ($return == 0) {
                // hash object for file
                $log[$i] .= "<br />".'git hash-object ' . escapeshellarg($filePath);
                $return = NULL;
                $output = array();
                exec('git hash-object ' . escapeshellarg($filePath), $output, $return);
                $output = implode(PHP_EOL, $output);
                $log[$i] .= "<br />".$return;
                //$log[$i] .= "<br />".$output;
                if ($return == 0) {
                    // get git note using hash of file
                    $hash = $output;
                    $log[$i] .= "<br />".'git notes --ref=creq show ' . escapeshellarg($hash);
                    $return = NULL;
                    $output = array();
                    exec('git notes --ref=creq show ' . escapeshellarg($hash), $output, $return);
                    $output = implode(PHP_EOL, $output);
                    $log[$i] .= "<br />".$return;
                    //$log[$i] .= "<br />".$output;
                    if ($return == 0) {
                        //$log[$i] .= "<br />".$output;
                        $note = $output;
                    }
                }
            }
    
            // checkout master before ending
            $log[$i] .= "<br />".'git checkout master';
            $return = NULL;
            $output = array();
            exec('git checkout master', $output, $return);
            $output = implode(PHP_EOL, $output);
            $log[$i] .= "<br />".$return;
            //$log[$i] .= "<br />".$output;
    
            $log[$i] .= "<br />"."<br />";
    
            // is missing git note
            if ($note == "") {
                return false;
            } else {
                return true;
            }
        }
    
        public function removeMajorAction()
        {
            $this->view->messages = $this->_helper->getHelper('FlashMessenger')->getMessages();
    
            $colleges = array();
            $collegeMajors = Bulletin_UtilityModel::getMajors();
            foreach ($collegeMajors as $college => $majors) {
                $colleges[$college] = $college;
                asort($collegeMajors[$college]);
                $collegeMajors[$college] = array('_college' => 'College Information') + $collegeMajors[$college];
            }
            $this->view->colleges = $colleges;
            $this->view->collegeMajors = $collegeMajors;
        }
    
        public function removeMajorPostAction()
        {
            // get post data and validate
            $in = $this->getAllParams();
            if ($in['college'] == '_null') {
                $in['college'] = NULL;
            }
            if ($in['major'] == '_college') {
                $in['major'] = NULL;
            }
    
            if (!$in['college']) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('You must select a college/major.');
                $this->redirect('/bulletin/admin/remove-major');
            }
    
            if (!$in['major']) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('You must select a college/major.');
                $this->redirect('/bulletin/admin/remove-major');
            }
    
            $activeRequests = Bulletin_SectionModel::findActiveRequestByCollegeAndMajorId($in['college'], $in['major']);
            foreach ($activeRequests as $activeRequest) {
                $href = $this->view->baseUrl('/requests/view/index/id/' . $activeRequest->getId());
                $this->_helper->getHelper('FlashMessenger')->addMessage(
                        "There is an <a href=\"$href\">active request</a> for that college/major. Complete the current request before attempting to remove the major."
                );
                $this->redirect('/bulletin/admin/remove-major');
            }
    
            if (!$in['justification']) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('You must enter a justification.');
                $this->redirect('/bulletin/admin/remove-major');
            }
    
            // prepare a branch for major removal
            Bulletin_RepositoryModel::getInstance()->pull();
            $branch = "major-" . $in['major'] . "-remove-" . date_format(date_create(), 'Ymd');
            $user = Auth_UserModel::findCurrentUser();
    
            // get current editing year
            $currentYear = $this->getCurrentEditingYear();
    
            // get major name
            $db = Zend_Registry::get('db');
            $select = new Zend_Db_Select($db);
            $select->from('creqMajors');
            $select->where('majorId = ?', $in['major']);
            $record = $select->query()->fetch();
            $major = $record['name'];
    
            // get file path and name for major
            $filePath = 'data' .DIRECTORY_SEPARATOR. Bulletin_RepositoryModel::getInstance()->getFilePathForCollegeMajorYear($in['college'], $major, $currentYear);
            if (!is_file($filePath)) {
                $this->_helper->getHelper('FlashMessenger')->addMessage('Unable to find an existing file for that college/major.');
                $this->redirect('/bulletin/admin/remove-major');
            }
    
            // remove file
            Bulletin_RepositoryModel::getInstance()->deleteFile($filePath, $branch, $user, $in['justification']);
    
            // update major removed status in database
            $db = Zend_Registry::get('db');
            $update_data = array('removed' => 'yes');
            $db->update('creqMajors', $update_data, 'majorId = '.$in['major']);
    
            // merge branch into master
            Bulletin_RepositoryModel::getInstance()->mergeBranchToMaster($branch);
    
            // submist pull request to github
            Bulletin_RepositoryModel::getInstance()->pullRequest("master","Removing Major",$in['justification']);
    
            // success
            $this->_helper->getHelper('FlashMessenger')->addMessage("Major '" . $major . "' removed successfully.");
            $this->redirect('/bulletin/admin/remove-major');
        }
    
        public function appPhpInfoAction()
        {
            $user = Auth_UserModel::findCurrentUser();
            $roles = Auth_GroupModel::findByUser($user);
            if (!in_array(1, $roles->getId())) {
                throw new Exception('You must be logged in to view this page.');
            }
    
            $this->_helper->layout->disableLayout();
            $this->view->message = "";
        }
    
        protected function getCurrentEditingYear()
        {
            $front = Zend_Controller_Front::getInstance();
            $options = $front->getParam('bootstrap')->getOptions();
            $month = $options['bulletin']['setting']['switchOverMonth'];
            $currentYear = Zend_Date::now()->get(Zend_Date::YEAR);
            if (Zend_Date::now()->get(Zend_Date::MONTH) >= $month) {
                $currentYear++;
            }
    
            return $currentYear;
        }
    
        protected function cleanupHtml($html)
        {
            // replace ' & ' with '&amp;'
            $html = str_replace(' & ', ' &amp; ', $html);
    
            // replace '&AMP;' with '&amp;'
            $html = str_replace('&AMP;', '&amp;', $html);
    
            // replace CRLF with LF
            $html = str_replace((chr(13).chr(10)), chr(10), $html);
    
            return $html;
        }
    }