-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathorganisation_model.php
111 lines (85 loc) · 3.67 KB
/
organisation_model.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
<?php
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
// - create organisation
// - add member to organisation
// - get organisations that a user is a member of
// - get members for an organisation that user is a member of
// An assessment belongs to a user. But could be shared with another user or with an organisation.
// - two tables:
// - organisation_assessments
// -
// In the UI you have organisation view, user view:
// organisation assessments, assessments shared with organisation
// assessment model has userid as ownership
// to share an assessment
// assessment id -> userid -> write/read
// get assessment list:
// : select from assessment where userid =
// or from ownership table?
// select from assessment_ownership where userid = ...
//
class Organisation
{
private $mysqli;
public function __construct($mysqli)
{
$this->mysqli = $mysqli;
}
public function create($orgname,$userid)
{
$userid = (int) $userid;
$orgname = preg_replace('/[^\w\s]/', '', $orgname);
$result = $this->mysqli->query("SELECT * FROM organisations WHERE `name`='$orgname'");
if ($result->num_rows==1) return false; // entry already exists
$this->mysqli->query("INSERT INTO organisations (`name`) VALUES ('$orgname')");
$orgid = $this->mysqli->insert_id;
$this->add_member($orgid,$userid);
return $orgid;
}
public function add_member($orgid,$userid)
{
$orgid = (int) $orgid;
$userid = (int) $userid;
$result = $this->mysqli->query("SELECT * FROM organisation_membership WHERE `orgid`='$orgid' AND `userid`='$userid'");
if ($result->num_rows==1) return false; // entry already exists
$this->mysqli->query("INSERT INTO organisation_membership (`orgid`,`userid`) VALUES ('$orgid','$userid')");
return true;
}
// Return a list of organisations that a user is a member of
public function get_organisations($userid)
{
$userid = (int) $userid;
$result = $this->mysqli->query("SELECT orgid FROM organisation_membership WHERE `userid`='$userid'");
$organisations = array();
while($row = $result->fetch_object())
{
$orgid = "".$row->orgid;
$orgresult = $this->mysqli->query("SELECT * FROM organisations WHERE `id`='$orgid'");
$orgrow = $orgresult->fetch_object();
// return full organisation details here
$resnumassessments = $this->mysqli->query("SELECT * FROM assessment_access WHERE `orgid`='$orgid'");
$numassessments = $resnumassessments->num_rows;
$organisations[$orgid] = array(
"orgid"=>$orgid,
"name"=>$orgrow->name,
"assessments"=>$numassessments,
"members"=>array()
);
$members = array();
$member_result = $this->mysqli->query("SELECT * FROM organisation_membership WHERE `orgid`='$orgid'");
while($member_row = $member_result->fetch_object())
{
global $user;
$username = $user->get_username($member_row->userid);
$members[] = array("userid"=>$member_row->userid, "name"=>$username, "lastactive"=>"?");
}
$organisations[$orgid]['members'] = $members;
}
return $organisations;
}
// to share an assessment with an organisation we need another table that records
// organisation id, assessment id.
// what about shared with users?
// and how to share libraries.
}