Skip to content

Commit

Permalink
merging MOODLE_19_QUESTIONS with HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiesensei committed Aug 9, 2007
1 parent 271e6de commit 515ed4c
Show file tree
Hide file tree
Showing 8 changed files with 881 additions and 0 deletions.
39 changes: 39 additions & 0 deletions question/category_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php //$Id$

require_once($CFG->libdir.'/formslib.php');

class question_category_edit_form extends moodleform {

function definition() {
global $CFG;
$mform =& $this->_form;

$contexts = $this->_customdata['contexts'];
$currentcat = $this->_customdata['currentcat'];
//--------------------------------------------------------------------------------
$mform->addElement('header', 'categoryheader', get_string('addcategory', 'quiz'));
$mform->setHelpButton('categoryheader', array('categories_edit', get_string('addcategory', 'quiz'), 'quiz'));

$questioncategoryel = $mform->addElement('questioncategory', 'parent', get_string('parent', 'quiz'),
array('contexts'=>$contexts, 'top'=>true, 'currentcat'=>$currentcat, 'nochildrenof'=>$currentcat));
$mform->setType('parent', PARAM_SEQUENCE);
if (1 == count_records_sql("SELECT count(*) FROM {$CFG->prefix}question_categories as c1, {$CFG->prefix}question_categories as c2 WHERE c2.id = $currentcat AND c1.contextid = c2.contextid")){
$mform->hardFreeze('parent');
}

$mform->addElement('text','name', get_string('name'),'maxlength="254" size="50"');
$mform->setDefault('name', '');
$mform->addRule('name', get_string('categorynamecantbeblank', 'quiz'), 'required', null, 'client');
$mform->setType('name', PARAM_MULTILANG);

$mform->addElement('textarea', 'info', get_string('categoryinfo', 'quiz'), array('rows'=> '10', 'cols'=>'45'));
$mform->setDefault('info', '');
$mform->setType('info', PARAM_MULTILANG);
//--------------------------------------------------------------------------------
$this->add_action_buttons(false, get_string('addcategory', 'quiz'));
//--------------------------------------------------------------------------------
$mform->addElement('hidden', 'id', 0);
$mform->setType('id', PARAM_INT);
}
}
?>
249 changes: 249 additions & 0 deletions question/contextmove.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
<?php // $Id$
/**
* Allows someone with appropriate permissions to move a category and associated
* files to another context.
*
* @author Jamie Pratt
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package questionbank
*/

require_once("../config.php");
require_once($CFG->dirroot."/question/editlib.php");
require_once($CFG->dirroot."/question/contextmove_form.php");

list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) = question_edit_setup('categories');

// get values from form for actions on this page
$toparent = required_param('toparent', PARAM_SEQUENCE);
$cattomove = required_param('cattomove', PARAM_INT);
$totop = optional_param('totop', 0, PARAM_INT); // optional param moves category to top of peers. Default is
//to add it to the bottom.



$onerrorurl = $CFG->wwwroot.'/question/category.php?'.$thispageurl->get_query_string();
list($toparent, $contextto) = explode(',', $toparent);
if (!empty($toparent)){//not top level category, make it a child of $toparent
if (!$toparent = get_record('question_categories', 'id', $toparent)){
error('Invalid category id for parent!', $onerrorurl);
}
$contextto = $toparent->contextid;
} else {
$toparent = new object();
$toparent->id = 0;
$toparent->contextid = $contextto;
}
if (!$cattomove = get_record('question_categories', 'id', $cattomove)){
error('Invalid category id to move!', $onerrorurl);
}
if ($cattomove->contextid == $contextto){
error("You shouldn't have got here if you're not moving a category to another context.", $onerrorurl);
}
$cattomove->categorylist = question_categorylist($cattomove->id);

$thispageurl->params(array('cattomove'=>$cattomove->id,
'toparent'=>"{$toparent->id},{$toparent->contextid}",
'totop'=>$totop));

$contextfrom = get_context_instance_by_id($cattomove->contextid);
$contextto = get_context_instance_by_id($contextto);
$contexttostring = print_context_name($contextto);

require_capability('moodle/question:managecategory', $contextfrom);
require_capability('moodle/question:managecategory', $contextto);


$fromcoursefilesid = get_filesdir_from_context($contextfrom);//siteid or courseid
$tocoursefilesid = get_filesdir_from_context($contextto);//siteid or courseid
if ($fromcoursefilesid != $tocoursefilesid){
$questions = get_records_select('question', "category IN ({$cattomove->categorylist})");
$urls = array();
if ($questions){
foreach ($questions as $id => $question){
$QTYPES[$questions[$id]->qtype]->get_question_options(&$questions[$id]);
$urls = array_merge_recursive($urls, $QTYPES[$questions[$id]->qtype]->find_file_links($questions[$id], $fromcoursefilesid));
}
}
ksort($urls);
} else {
$urls = array();
}
$brokenurls = array();
foreach (array_keys($urls) as $url){
if (!file_exists($CFG->dataroot."/$fromcoursefilesid/".$url)){
$brokenurls[] = $url;
}
}
if ($fromcoursefilesid == SITEID){
$fromareaname = get_string('filesareasite', 'question');
} else {
$fromareaname = get_string('filesareacourse', 'question');
}
if ($tocoursefilesid == SITEID){
$toareaname = get_string('filesareasite', 'question');
} else {
$toareaname = get_string('filesareacourse', 'question');
}
$contextmoveform = new question_context_move_form($thispageurl,
compact('urls', 'fromareaname', 'toareaname', 'brokenurls',
'fromcoursefilesid', 'tocoursefilesid'));
if ($contextmoveform->is_cancelled()){
$thispageurl->remove_params('cattomove', 'toparent', 'totop');
redirect($CFG->wwwroot."/question/category.php?".$thispageurl->get_query_string());
}elseif ($moveformdata = $contextmoveform->get_data()) {
if (is_array($moveformdata->urls)){
check_dir_exists($CFG->dataroot."/$tocoursefilesid/", true);
$flipurls = array_keys($urls);
foreach ($moveformdata->urls as $key => $urlaction){
$source = $CFG->dataroot."/$fromcoursefilesid/".$flipurls[$key];
$destination = $flipurls[$key];
if (($urlaction != QUESTION_FILEDONOTHING) && ($urlaction != QUESTION_FILEMOVELINKSONLY)){
while (file_exists($CFG->dataroot."/$tocoursefilesid/".$destination)){
$matches = array();
//check for '_'. copyno after filename, before extension.
if (preg_match('!\_([0-9]+)(\.[^\.\\/]+)?$!', $destination, $matches)){
$copyno = $matches[1]+1;
} else {
$copyno = 1;
}
//replace old copy no with incremented one.
$destination = preg_replace('!(\_[0-9]+)?(\.[^\.\\/]+)?$!', '_'.$copyno.'\\2', $destination, 1);
}
}
switch ($urlaction){
case QUESTION_FILECOPY :
if (!copy($source, $CFG->dataroot."/$tocoursefilesid/".$destination)){
print_error('errorfilecannotbecopied', 'question', $onerrorurl, $source);
}
break;
case QUESTION_FILEMOVE :
if (!rename($source, $CFG->dataroot."/$tocoursefilesid/".$destination)){
print_error('errorfilecannotbemoved', 'question', $onerrorurl, $source);
}
break;
case QUESTION_FILEDONOTHING :
case QUESTION_FILEMOVELINKSONLY :
break;
default :
error('Invalid action selected!', $onerrorurl);
}
switch ($urlaction){
//now search and replace urls in questions.
case QUESTION_FILECOPY :
case QUESTION_FILEMOVE :
case QUESTION_FILEMOVELINKSONLY :
$url = $flipurls[$key];
$questionids = array_unique($urls[$url]);
foreach ($questionids as $questionid){
$question = $questions[$questionid];
$QTYPES[$question->qtype]->replace_file_links($question, $fromcoursefilesid, $tocoursefilesid, $url, $destination);
}
break;
case QUESTION_FILEDONOTHING :
default :
break;
}


}
}

//adjust sortorder before we make the cat a peer of it's new peers
$peers = get_records_select_menu('question_categories', "contextid = {$toparent->contextid} AND ".
"parent = {$toparent->id}", "sortorder ASC",
"id, id");
$peers = array_keys($peers);
if ($totop){
array_unshift($peers, $cattomove->id);
} else {
$peers[] = $cattomove->id;
}
$sortorder = 0;
foreach ($peers as $peer) {
if (! set_field('question_categories', "sortorder", $sortorder, "id", $peer)) {
print_error('listupdatefail', '', $onerrorurl);
}
$sortorder++;
}
//now move category
$cat = new object();
$cat->id = $cattomove->id;
$cat->parent = $toparent->id;
//set context of category we are moving and all children also!
if (!execute_sql("UPDATE {$CFG->prefix}question_categories SET contextid = {$contextto->id} WHERE id IN ({$cattomove->categorylist})", false)){
error("Could not move the category '$newname' to ".$contexttostring, $onerrorurl);
}
//finally set the new parent id
if (!update_record("question_categories", $cat)) {
error("Could not update the category '$updatename'", $onerrorurl);
}
$thispageurl->remove_params('cattomove', 'toparent', 'totop');
redirect($CFG->wwwroot."/question/category.php?".$thispageurl->get_query_string(array('cat'=>"{$cattomove->id},{$contextto->id}")));
}

$streditingcategories = get_string('editcategories', 'quiz');
$crumbs = array();
if ($cm!==null) {
// Page header
$strupdatemodule = has_capability('moodle/course:manageactivities', $contexts->lowest())
? update_module_button($cm->id, $COURSE->id, get_string('modulename', $cm->modname))
: "";
$crumbs[] = array('name' => get_string('modulenameplural', $cm->modname),
'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$COURSE->id",
'type' => 'activity');
$crumbs[] = array('name' => format_string($module->name),
'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}",
'type' => 'title');
} else {
// Print basic page layout.
$strupdatemodule = '';
}


$crumbs[] = array('name' => $streditingcategories, 'link' => $thispageurl->out(), 'type' => 'title');
$crumbs[] = array('name' => get_string('movingcategory', 'question'), 'link' => '', 'type' => 'title');

$navigation = build_navigation($crumbs);
print_header_simple($streditingcategories, '', $navigation, "", "", true, $strupdatemodule);

// print tabs
if ($cm!==null) {
$currenttab = 'edit';
$mode = 'categories';
${$cm->modname} = $module;
include($CFG->dirroot."/mod/{$cm->modname}/tabs.php");
} else {
$currenttab = 'categories';
$context = $contexts->lowest();
include('tabs.php');
}
//parameter for get_string
$cattomove->contextto = $contexttostring;
if (count($urls)){
$defaults = array();
for ($default_key =0; $default_key < count($urls); $default_key++){
switch ($tocoursefilesid){
case SITEID:
$defaults['urls'][$default_key] = QUESTION_FILEMOVE;
break;
default :
$defaults['urls'][$default_key] = QUESTION_FILECOPY;
break;

}
}
$contextmoveform->set_data($defaults);
//some parameters for get_string
$cattomove->urlcount = count($urls);
$cattomove->toareaname = $toareaname;
$cattomove->fromareaname = $fromareaname;

print_box(get_string('movingcategoryandfiles', 'question', $cattomove), 'boxwidthnarrow boxaligncenter generalbox');
} else {
print_box(get_string('movingcategorynofiles', 'question', $cattomove), 'boxwidthnarrow boxaligncenter generalbox');
}
$contextmoveform->display();
print_object($brokenurls);
print_footer($COURSE);
?>
104 changes: 104 additions & 0 deletions question/contextmove_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php //$Id$

require_once($CFG->libdir.'/formslib.php');

class question_context_move_form extends moodleform {

function definition() {
global $CFG;
$mform =& $this->_form;

//--------------------------------------------------------------------------------
$urls = $this->_customdata['urls'];
$fromareaname = $this->_customdata['fromareaname'];
$toareaname = $this->_customdata['toareaname'];
$fileoptions = array(QUESTION_FILEDONOTHING=>get_string('donothing', 'question'),
QUESTION_FILECOPY=>get_string('copy', 'question', $fromareaname),
QUESTION_FILEMOVE=>get_string('move', 'question', $fromareaname),
QUESTION_FILEMOVELINKSONLY=>get_string('movelinksonly', 'question', $fromareaname));
$brokenfileoptions = array(QUESTION_FILEDONOTHING=>get_string('donothing', 'question'),
QUESTION_FILEMOVELINKSONLY=>get_string('movelinksonly', 'question', $fromareaname));
$brokenurls = $this->_customdata['brokenurls'];
if (count($urls)){

$mform->addElement('header','general', get_string('filestomove', 'question', $toareaname));

$i = 0;
foreach (array_keys($urls) as $url){
$iconname = mimeinfo('icon', $url);
$icontype = mimeinfo('type', $url);
$img = "<img src=\"$CFG->pixpath/f/$iconname\" class=\"icon\" alt=\"$icontype\" />";
if (in_array($url, $brokenurls)){
$mform->addElement('select', "urls[$i]", $img.$url, $brokenfileoptions);
} else {
$mform->addElement('select', "urls[$i]", $img.$url, $fileoptions);
}
$i++;
}

}
if (count($brokenurls)){
$mform->addElement('advcheckbox','ignorebroken', get_string('ignorebroken', 'question'));
}
//--------------------------------------------------------------------------------
$this->add_action_buttons(true, get_string('movecategory', 'question'));

}

function validation($data){
$errors = array();
$tocoursefilesid = $this->_customdata['tocoursefilesid'];
$fromcoursefilesid = $this->_customdata['fromcoursefilesid'];
if (isset($data['urls']) && (count($data['urls']))){
foreach ($data['urls'] as $key => $urlaction){
switch ($urlaction){
case QUESTION_FILEMOVE :
if (!has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $fromcoursefilesid))){
$errors["urls[$key]"] = get_string('filecantmovefrom', 'question');
}
//no break; COPY check is also applied to MOVE action
case QUESTION_FILECOPY :
if (!has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $tocoursefilesid))){
$errors["urls[$key]"] = get_string('filecantmoveto', 'question');
}
break;
case QUESTION_FILEMOVELINKSONLY :
case QUESTION_FILEDONOTHING :
break;
}
}
}
//check that there hasn't been any changes in files between time form was displayed
//and now when it has been submitted.
if (isset($data['urls']) &&
(count($data['urls'])
!= count($this->_customdata['urls']))){
$errors['urls[0]'] = get_string('errorfileschanged', 'question');
}
return $errors;
}
/*
* We want these errors to show up on first loading the form which is not the default for
* validation method which is not run until submission.
*/
function definition_after_data(){
$mform = $this->_form;
$brokenurls = $this->_customdata['brokenurls'];
if (count($brokenurls)){
$ignoreval = $mform->getElementValue('ignorebroken');
if (!$ignoreval){
$urls = $this->_customdata['urls'];
$i = 0;
foreach (array_keys($urls) as $url){
if (in_array($url, $brokenurls)){
$mform->setElementError("urls[$i]", get_string('broken', 'question'));
} else {
$mform->setElementError("urls[$i]", '');
}
$i++;
}
}
}
}
}
?>
Loading

0 comments on commit 515ed4c

Please sign in to comment.