forked from jed15/socialwiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socialwikitree.php~
executable file
·128 lines (113 loc) · 3.24 KB
/
socialwikitree.php~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
class socialwiki_node{
//the page id
public $id;
//page title and authors name
public $content;
//boolean true if the node isn't a leaf
public $hidden;
//the column the node is in
public $column;
//an array of children ids
public $children=array();
//the parents id
public $parent;
//whether the mode has been added to the tree
public $added;
//the level of the tree the node is on
public $level;
function __construct($page){
$this->id='l'.$page->id;
if($page->parent==NULL){
$this->parent=-1;
}else{
$this->parent='l'.$page->parent;
}
$this->column=-1;
$this->added=false;
$this->hidden = true;
$this->set_content($page);
}
private function set_content($page){
Global $PAGE,$CFG;
$user = socialwiki_get_user_info($page->userid);
$userlink = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $PAGE->cm->course));
$this->content=html_writer::link($CFG->wwwroot.'/mod/socialwiki/view.php?pageid='.$page->id,$page->title,array("class"=>"colourtext")).'<br/>'.html_writer::link($userlink->out(false),fullname($user),array("class"=>"colourtext"));
}
function add_child($child){
$this->children[]=$child;
}
function display(){
Global $OUTPUT;
echo $OUTPUT->box($this->content,'socialwiki_treebox');
}
}
class socialwiki_tree{
//an array of socialwiki_nodes
public $nodes=array();
function add_node($page){
$this->nodes['l'.$page->id]=new socialwiki_node($page);
//if the array has a parent add it to the parents child array
if($page->parent!=NULL){
$parent=$this->find_node('l'.$page->parent);
if($parent){
$parent->add_child($page->id);
}else{
print_error('nonode','socialwiki');
}
}
}
//finds a node given an id returns that node if found. -1 if the node doesn't exist
function find_node($nodeid){
foreach($this->nodes as $node){
if($node->id==$nodeid){
return $node;
}
}
return NULL;
}
//sort the array with the children of a node apearing directly after it in the array
function sort(){
$sorted=array();
$parents=array();
foreach($this->nodes as $node){
if($node->parent==-1){
$parents[]=$node;
}
}
//make a tree array for each parent so the sorted array has one complete tree followed by another tree
foreach ($parents as $parent){
//add the new array to the end of the sorted array
$sorted=$this->add_children($parent->id,$sorted,1);
}
//set nodes to the sorted array
$this->nodes=$sorted;
}
//recursively add chid nodes to an array
function add_children($nodeid,$ar,$level){
$node=$this->find_node($nodeid);
$node->level=$level;
//add the child to the array
$ar[$nodeid]=$node;
//increase level
$level++;
if(count($node->children)>0){
foreach($node->children as $childid){
$ar=$this->add_children($childid,$ar,$level);
}
}
return $ar;
}
function display(){
Global $OUTPUT;
foreach($this->nodes as $node){
if($node->parent==-1){
echo'<br/><br/><br/>';
}
echo $OUTPUT->container_start();
echo str_repeat('        ',$node->level-1);
$node->display();
echo $OUTPUT->container_end();
}
}
}