Skip to content

Commit

Permalink
MDL-47494 ddmarker: NOBUG further work on adminpage
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiepratt committed Mar 22, 2012
1 parent 6bdb732 commit 5e37b1f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 17 deletions.
88 changes: 73 additions & 15 deletions question/type/ddmarker/imagetargetconverter.php
Expand Up @@ -42,12 +42,14 @@ abstract class qtype_ddmarker_list_item {

public function add_child($child) {
$this->children[] = $child;
//array_unique relies on __toString() returning a unique string to determine if objects in array
//are the same or not
$this->children = array_unique($this->children);
}

abstract protected function parent_node ();

abstract public function render ();
abstract public function render($stringidentifier, $link);

public function leaf_to_root($qcount) {
$this->qcount += $qcount;
Expand All @@ -58,10 +60,10 @@ public function leaf_to_root($qcount) {
}
}

protected function render_children() {
protected function render_children($stringidentifier, $link) {
$children = array();
foreach ($this->children as $child) {
$children[] = $child->render();
$children[] = $child->render($stringidentifier, $link);
}
return html_writer::alist($children);
}
Expand All @@ -87,11 +89,18 @@ public function parent_node() {
}
}

public function render () {
public function render ($stringidentifier, $link) {
global $PAGE;
$a = new stdClass();
$a->qcount = $this->qcount;
$a->name = $this->record->name;
return get_string('categorylistitem', 'qtype_ddmarker', $a).$this->render_children();
$thisitem = get_string($stringidentifier.'category', 'qtype_ddmarker', $a);
if ($link) {
$actionurl = new moodle_url($PAGE->url, array('categoryid'=> $this->record->id));
$thisitem = html_writer::tag('a', $thisitem, array('href' => $actionurl));
}

return $thisitem.$this->render_children($stringidentifier, $link);
}
}
class qtype_ddmarker_context_list_item extends qtype_ddmarker_list_item {
Expand All @@ -113,11 +122,17 @@ public function parent_node() {
}
}

public function render () {
public function render ($stringidentifier, $link) {
global $PAGE;
$a = new stdClass();
$a->qcount = $this->qcount;
$a->name = $this->record->get_context_name();
return get_string('contextlistitem', 'qtype_ddmarker', $a).$this->render_children();
$thisitem = get_string($stringidentifier.'context', 'qtype_ddmarker', $a);
if ($link) {
$actionurl = new moodle_url($PAGE->url, array('contextid'=> $this->record->id));
$thisitem = html_writer::tag('a', $thisitem,array('href' => $actionurl));
}
return $thisitem.$this->render_children($stringidentifier, $link);
}
}
/**
Expand Down Expand Up @@ -162,8 +177,11 @@ public function __construct($contextids) {
}
parent::make_list_item_instances_from_records ($contextids);
}
public function render() {
$rootitem = html_writer::tag('li', $this->root_node()->render());
public function render($stringidentifier, $link, $roottorender = null) {
if ($roottorender === null) {
$roottorender = $this->root_node();
}
$rootitem = html_writer::tag('li', $roottorender->render($stringidentifier, $link));
return html_writer::tag('ul', $rootitem);
}
public function root_node () {
Expand All @@ -187,6 +205,10 @@ public function __construct($contextids, $contextlist) {
parent::make_list_item_instances_from_records ($contextids);
}
}

$categoryid = optional_param('categoryid', 0, PARAM_INT);
$qcontextid = optional_param('contextid', 0, PARAM_INT);
$confirm = optional_param('confirm', 0, PARAM_INT);
// Check the user is logged in.
require_login();
$context = get_context_instance(CONTEXT_SYSTEM);
Expand All @@ -198,10 +220,29 @@ public function __construct($contextids, $contextlist) {
echo $OUTPUT->header();
echo $OUTPUT->heading_with_help(get_string('imagetargetconverter', 'qtype_ddmarker'), '', 'qtype_ddmarker');

$catswithquestions = $DB->get_records_sql('SELECT cat.id, cat.contextid, COUNT(1) AS qcount '.
'FROM {question_categories} cat, {question} q '.
'WHERE q.category = cat.id '.
'GROUP BY cat.id, cat.contextid');

$params = array();
$from = 'FROM {question_categories} cat, {question} q';
$where = ' WHERE q.category = cat.id ';

if ($qcontextid) {
$qcontext = get_context_instance_by_id($qcontextid, MUST_EXIST);
$from .= ', {context} context';
$where .= 'AND cat.contextid = context.id AND (context.path LIKE :path OR context.id = :id) ';
$params['path'] = $qcontext->path.'/%';
$params['id'] = $qcontext->id;
} else if ($categoryid) {
$from .= ', {context} context, {question_categories} cat2';
$where .= 'AND cat.contextid = cat2.contextid AND cat2.id = :categoryid ';
$params['categoryid'] = $categoryid;
}
$sql = 'SELECT cat.id, cat.contextid, COUNT(1) AS qcount '.
$from.
$where.
'GROUP BY cat.id, cat.contextid';


$catswithquestions = $DB->get_records_sql($sql, $params);
//print_object($catswithquestions);

$contextids = array();
Expand All @@ -216,8 +257,25 @@ public function __construct($contextids, $contextlist) {
$categories->leaf_node($catwithquestions->id, $catwithquestions->qcount);
}
//print_object($contexts->root_node());

echo $contexts->render();
if ($categoryid || $qcontextid) {
if (!$confirm) {
$cofirmedurl = new moodle_url($PAGE->url, compact('categoryid', 'contextid')+array('confirm'=>1));
$cancelurl = new moodle_url($PAGE->url);
if ($categoryid) {
$torender = $categories->get_instance($categoryid);
} else if ($qcontextid) {
$torender = $contexts->get_instance($qcontextid);
} else {
$torender = $contexts->root_node();
}
echo $contexts->render('listitem', false, $torender);
echo $OUTPUT->confirm(get_string('confirmimagetargetconversion', 'qtype_ddmarker'), $cofirmedurl, $cancelurl);
} else if (confirm_sesskey()) {

}
} else {
echo $contexts->render('listitemaction', true, $contexts->root_node());
}


// Footer.
Expand Down
7 changes: 5 additions & 2 deletions question/type/ddmarker/lang/en/qtype_ddmarker.php
Expand Up @@ -27,8 +27,7 @@
$string['alttext'] = 'Alt text';
$string['answer'] = 'Answer';
$string['bgimage'] = 'Background image';
$string['categorylistitem'] = 'Category "{$a->name}" (contains {$a->qcount} questions)';
$string['contextlistitem'] = 'Context "{$a->name}" (contains {$a->qcount} questions)';
$string['confirmimagetargetconversion'] = 'You are about to convert the above image target questions to the drag and drop markers question type.';
$string['coords'] = 'Coords';
$string['correctansweris'] = 'The correct answer is: {$a}';
$string['ddmarker'] = 'Drag and drop markers';
Expand Down Expand Up @@ -57,6 +56,10 @@
$string['formerror_unrecognisedxypart'] = 'We do not recognise the x,y coordinates you have specified. Your coordinates for a {$a->shape} should be expressed as - {$a->coordsstring}.';
$string['imagetargetconverter'] = 'Convert image target questions to drag and drop marker';
$string['infinite'] = 'Infinite';
$string['listitemactioncategory'] = 'Convert all imagetarget questions in category "{$a->name}" (contains {$a->qcount} imagetarget questions)';
$string['listitemactioncontext'] = 'Convert all imagetarget questions in context "{$a->name}" (contains {$a->qcount} imagetarget questions)';
$string['listitemcategory'] = 'Questions in category "{$a->name}" (contains {$a->qcount} imagetarget questions)';
$string['listitemcontext'] = 'Questions in context "{$a->name}" (contains {$a->qcount} imagetarget questions)';
$string['marker'] = 'Marker';
$string['marker_n'] = 'Marker {no}';
$string['markers'] = 'Markers';
Expand Down

0 comments on commit 5e37b1f

Please sign in to comment.