permissions_functions.inc.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. <?php
  2. /**
  3. * This files contains the common functions for the permissions
  4. *
  5. * A list of all the functions (in no particular order)
  6. * ----------------------------------------------------
  7. * store_permissions($content,$id)
  8. * get_permissions($content,$id)
  9. * limited_or_full($current_permissions)
  10. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  11. * @package chamilo.permissions
  12. */
  13. /**
  14. * This function stores the permissions in the correct table.
  15. * Since Checkboxes are used we do not know which ones are unchecked.
  16. * That's why we first delete them all (for the given user/group/role
  17. * and afterwards we store the checked ones only.
  18. * @param $content are we storing rights for a user, a group or a role (the database depends on it)
  19. * @param $id the id of the user, group or role
  20. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  21. * @version 1.0
  22. */
  23. function store_permissions($content, $id) {
  24. $course_id = api_get_course_int_id();
  25. // Which database are we using (depending on the $content parameter)
  26. if ($content=='user')
  27. {
  28. $table=Database::get_course_table(TABLE_PERMISSION_USER);
  29. $id_field = user_id;
  30. }
  31. if ($content=='group')
  32. {
  33. $table=Database::get_course_table(TABLE_PERMISSION_GROUP);
  34. $id_field = group_id;
  35. }
  36. if ($content=='role')
  37. {
  38. $table=Database::get_course_table(TABLE_ROLE_PERMISSION);
  39. $id_field = role_id;
  40. }
  41. // We first delete all the existing permissions for that user/group/role
  42. $sql="DELETE FROM $table WHERE c_id = $course_id AND $id_field = '".Database::escape_string($id)."'";
  43. $result=Database::query($sql);
  44. // looping through the post values to find the permission (containing the string permission* )
  45. foreach ($_POST as $key => $value)
  46. {
  47. if (strstr($key,"permission*"))
  48. {
  49. list($brol,$tool,$action)=explode("*",$key);
  50. $sql="INSERT INTO $table (c_id, $id_field,tool,action) VALUES ($course_id, '".Database::escape_string($id)."','".Database::escape_string($tool)."','".Database::escape_string($action)."')";
  51. $result=Database::query($sql);
  52. }
  53. }
  54. return get_lang('PermissionsStored');
  55. }
  56. /**
  57. * This function stores one permission in the correct table.
  58. * @param $content are we storing rights for a user, a group or a role (the database depends on it)
  59. * @param $action are we granting or revoking a permission?
  60. * @param $id the id of the user, group or role
  61. * @param $tool the tool
  62. * @param $permission the permission the user, group or role has been granted or revoked
  63. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  64. * @version 1.0
  65. */
  66. function store_one_permission($content, $action, $id, $tool,$permission) {
  67. global $rights_full;
  68. $course_id = api_get_course_int_id();
  69. // for some reason I don't know, he can't get to the $rights_full array, so commented the following lines out.
  70. // check
  71. //if(!in_array($permission, $rights_full))
  72. //{
  73. // return get_lang('Error');
  74. //}
  75. // Which database are we using (depending on the $content parameter)
  76. if ($content=='user') {
  77. $table=Database::get_course_table(TABLE_PERMISSION_USER);
  78. $id_field = user_id;
  79. }
  80. if ($content=='group')
  81. {
  82. $table=Database::get_course_table(TABLE_PERMISSION_GROUP);
  83. $id_field = group_id;
  84. }
  85. if ($content=='role')
  86. {
  87. $table=Database::get_course_table(TABLE_ROLE_PERMISSION);
  88. $id_field = role_id;
  89. }
  90. // grating a right
  91. if ($action=='grant') {
  92. $sql="INSERT INTO $table (c_id, $id_field,tool,action) VALUES ($course_id, '".Database::escape_string($id)."','".Database::escape_string($tool)."','".Database::escape_string($permission)."')";
  93. $result=Database::query($sql);
  94. if($result) {
  95. $result_message=get_lang('PermissionGranted');
  96. }
  97. }
  98. if ($action=='revoke')
  99. {
  100. $sql="DELETE FROM $table WHERE c_id = $course_id AND $id_field = '".Database::escape_string($id)."' AND tool='".Database::escape_string($tool)."' AND action='".Database::escape_string($permission)."'";
  101. $result=Database::query($sql);
  102. if($result) {
  103. $result_message=get_lang('PermissionRevoked');
  104. }
  105. }
  106. return $result_message;
  107. }
  108. /**
  109. * This function retrieves the existing permissions of a user, group or role.
  110. * @param $content are we retrieving the rights of a user, a group or a role (the database depends on it)
  111. * @param $id the id of the user, group or role
  112. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  113. * @version 1.0
  114. */
  115. function get_permissions($content, $id) {
  116. $course_id = api_get_course_int_id();
  117. $currentpermissions=array();
  118. // Which database are we using (depending on the $content parameter)
  119. $course_id_condition = " c_id = $course_id AND ";
  120. if ($content == 'user')
  121. {
  122. $table=Database::get_course_table(TABLE_PERMISSION_USER);
  123. $id_field = 'user_id';
  124. }
  125. elseif ($content == 'group')
  126. {
  127. $table=Database::get_course_table(TABLE_PERMISSION_GROUP);
  128. $id_field = 'group_id';
  129. }
  130. elseif ($content == 'role')
  131. {
  132. $table=Database::get_course_table(TABLE_ROLE_PERMISSION);
  133. $id_field = 'role_id';
  134. }
  135. elseif ($content == 'platform_role')
  136. {
  137. $table=Database::get_main_table(TABLE_ROLE_PERMISSION);
  138. $id_field = 'role_id';
  139. $course_id_condition = '';
  140. }
  141. elseif ($content == 'task')
  142. {
  143. $table=Database::get_course_table(TABLE_BLOGS_TASKS_PERMISSIONS);
  144. $id_field = 'task_id';
  145. }
  146. // finding all the permissions. We store this in a multidimensional array
  147. // where the first dimension is the tool.
  148. $sql="
  149. SELECT * FROM " . $table . "
  150. WHERE $course_id_condition " . $id_field . "='" . Database::escape_string($id) . "'";
  151. $result = Database::query($sql);
  152. while($row = Database::fetch_array($result))
  153. $currentpermissions[$row['tool']][] = $row['action'];
  154. return $currentpermissions;
  155. }
  156. /**
  157. * the array that contains the current permission a user, group or role has will now be changed depending on
  158. * the Dokeos Config Setting for the permissions (limited [add, edit, delete] or full [view, add, edit, delete, move, visibility]
  159. * @param $content are we retrieving the rights of a user, a group or a role (the database depends on it)
  160. * @param $id the id of the user, group or role
  161. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  162. * @version 1.0
  163. * @todo currently there is a setting user_permissions and group_permissions. We should merge this in one config setting.
  164. */
  165. function limited_or_full($current_permissions)
  166. {
  167. if (api_get_setting('permissions')=='limited')
  168. {
  169. foreach ($current_permissions as $tool=>$tool_rights)
  170. {
  171. // we loop through the possible permissions of a tool and unset the entry if it is view
  172. // if it is visibility or move we have to grant the edit right
  173. foreach ($tool_rights as $key=>$value)
  174. {
  175. if ($value=='View')
  176. {
  177. unset($current_permissions[$tool][$key]);
  178. }
  179. if ($value=='Visibility' OR $value=='Move')
  180. {
  181. if (!in_array('Edit',$current_permissions[$tool]))
  182. {
  183. $current_permissions[$tool][]='Edit';
  184. }
  185. unset($current_permissions[$tool][$key]);
  186. }
  187. //else
  188. //{
  189. // $current_permissions[$tool][]=$value;
  190. //}
  191. }
  192. }
  193. return $current_permissions;
  194. }
  195. if (api_get_setting('permissions')=='full')
  196. {
  197. return $current_permissions;
  198. }
  199. }
  200. /**
  201. * This function displays a checked or unchecked checkbox. The checkbox will be checked if the
  202. * user, group or role has the permission for the given tool, unchecked if the user, group or role
  203. * does not have the right
  204. * @param $permission_array the array that contains all the permissions of the user, group, role
  205. * @param $tool the tool we want to check a permission for
  206. * @param $permission the permission we want to check for
  207. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  208. * @version 1.0
  209. */
  210. function display_checkbox_matrix($permission_array, $tool, $permission, $inherited_permissions=array())
  211. {
  212. $checked="";
  213. if (is_array($permission_array[$tool]) AND in_array($permission,$permission_array[$tool]))
  214. {
  215. $checked="checked";
  216. }
  217. echo "\t\t\t<input type=\"checkbox\" name=\"permission*$tool*$permission\" $checked>\n";
  218. }
  219. /**
  220. * This function displays a checked or unchecked image. The image will be checked if the
  221. * user, group or role has the permission for the given tool, unchecked if the user, group or role
  222. * does not have the right
  223. * @param $permission_array the array that contains all the permissions of the user, group, role
  224. * @param $tool the tool we want to check a permission for
  225. * @param $permission the permission we want to check for
  226. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  227. * @version 1.0
  228. */
  229. function display_image_matrix($permission_array, $tool, $permission,$inherited_permissions=array(), $course_admin=false, $editable=true)
  230. {
  231. if ($course_admin) {
  232. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  233. } else {
  234. if (in_array($permission,$inherited_permissions[$tool])) {
  235. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  236. } else {
  237. if (is_array($permission_array[$tool]) AND in_array($permission,$permission_array[$tool])) {
  238. if ($editable) {
  239. $url=api_get_self();
  240. $urlparameters = '';
  241. foreach($_GET as $key=>$value) {
  242. $parameter[$key]=$value;
  243. }
  244. $parameter['action']='revoke';
  245. $parameter['permission']=$permission;
  246. $parameter['tool']=$tool;
  247. foreach ($parameter as $key=>$value) {
  248. $urlparameters.=$key.'='.$value.'&amp;';
  249. }
  250. $url=$url.'?'.$urlparameters;
  251. echo "\t\t\t <a href=\"".$url."\">";
  252. }
  253. echo "<img src=\"../img/checkbox_on2.gif\" border=\"0\"/>";
  254. if ($editable) {
  255. echo "</a>";
  256. }
  257. } else {
  258. if ($editable)
  259. {
  260. $url=api_get_self();
  261. $urlparameters = '';
  262. foreach ($_GET as $key=>$value)
  263. {
  264. $parameter[$key]=$value;
  265. }
  266. $parameter['action']='grant';
  267. $parameter['permission']=$permission;
  268. $parameter['tool']=$tool;
  269. foreach ($parameter as $key=>$value)
  270. {
  271. $urlparameters.=$key.'='.$value.'&amp;';
  272. }
  273. $url=$url.'?'.$urlparameters;
  274. //echo "\t\t\t <a href=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."&amp;action=grant&amp;permission=$permission&amp;tool=$tool\">";
  275. echo "\t\t\t <a href=\"".$url."\">";
  276. }
  277. echo "<img src=\"../img/wrong.gif\" border=\"0\"/>";
  278. if ($editable)
  279. {
  280. echo "</a>";
  281. }
  282. }
  283. }
  284. }
  285. }
  286. /**
  287. * Slightly modified: Toon Keppens
  288. * This function displays a checked or unchecked image. The image will be checked if the
  289. * user, group or role has the permission for the given tool, unchecked if the user, group or role
  290. * does not have the right
  291. * @param $permission_array the array that contains all the permissions of the user, group, role
  292. * @param $tool the tool we want to check a permission for
  293. * @param $permission the permission we want to check for
  294. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  295. * @version 1.0
  296. */
  297. function display_image_matrix_for_blogs($permission_array, $user_id, $tool, $permission,$inherited_permissions=array(), $course_admin=false, $editable=true)
  298. {
  299. if ($course_admin)
  300. {
  301. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  302. }
  303. else
  304. {
  305. if (!empty($inherited_permissions) and in_array($permission,$inherited_permissions[$tool]))
  306. {
  307. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  308. }
  309. else
  310. {
  311. if (is_array($permission_array[$tool]) AND in_array($permission,$permission_array[$tool]))
  312. {
  313. if ($editable)
  314. {
  315. $url = api_get_self();
  316. $urlparameters = '';
  317. foreach($_GET as $key => $value)
  318. {
  319. $parameter[$key] = $value;
  320. }
  321. $parameter['action']='manage_rights';
  322. $parameter['do']='revoke';
  323. $parameter['permission']=$permission;
  324. $parameter['tool']=$tool;
  325. $parameter['user_id']=$user_id;
  326. foreach ($parameter as $key=>$value)
  327. {
  328. $urlparameters .= $key . '=' . $value . '&amp;';
  329. }
  330. $url = $url . '?' . $urlparameters;
  331. echo "\t\t\t <a href=\"".$url."\">";
  332. }
  333. echo "<img src=\"../img/checkbox_on2.gif\" border=\"0\"/ title=\"".get_lang('UserHasPermission')."\">";
  334. if ($editable) {
  335. echo "</a>";
  336. }
  337. } else {
  338. if ($editable) {
  339. $url = api_get_self();
  340. $urlparameters = '';
  341. foreach ($_GET as $key=>$value) {
  342. $parameter[$key]=$value;
  343. }
  344. $parameter['action']='manage_rights';
  345. $parameter['do']='grant';
  346. $parameter['permission']=$permission;
  347. $parameter['tool']=$tool;
  348. $parameter['user_id']=$user_id;
  349. foreach ($parameter as $key=>$value) {
  350. $urlparameters .= $key . '=' . $value . '&amp;';
  351. }
  352. $url=$url.'?'.$urlparameters;
  353. //echo "\t\t\t <a href=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."&amp;action=grant&amp;permission=$permission&amp;tool=$tool\">";
  354. echo "\t\t\t <a href=\"".$url."\">";
  355. }
  356. echo "<img src=\"../img/wrong.gif\" border=\"0\"/ title=\"".get_lang('UserHasPermissionNot')."\">";
  357. if ($editable) {
  358. echo "</a>";
  359. }
  360. }
  361. }
  362. }
  363. }
  364. /**
  365. * This function displays a list off all the roles of the course (and those defined by the platform admin)
  366. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  367. * @version 1.0
  368. */
  369. function display_role_list($current_course_roles, $current_platform_roles)
  370. {
  371. global $setting_visualisation;
  372. $course_id = api_get_course_int_id();
  373. $coures_roles_table=Database::get_course_table(TABLE_ROLE);
  374. $platform_roles_table=Database::get_main_table(TABLE_ROLE);
  375. /*
  376. // platform roles
  377. $sql="SELECT * FROM $platform_roles_table";
  378. $result=Database::query($sql);
  379. while ($row=Database::fetch_array($result))
  380. {
  381. if(in_array($row['role_id'], $current_platform_roles))
  382. {
  383. $checked='checked';
  384. $image='checkbox_on2.gif';
  385. $action='revoke';
  386. }
  387. else
  388. {
  389. $checked='';
  390. $image='wrong.gif';
  391. $action='grant';
  392. }
  393. if($setting_visualisation=='checkbox')
  394. {
  395. echo "<input type=\"checkbox\" name=\"role*platform*".$row['role_id']."\" $checked>";
  396. }
  397. if($setting_visualisation=='image')
  398. {
  399. echo "<a href=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."&amp;action=$action&amp;role=".$row['role_id']."&amp;scope=platform\"><img src=\"../img/".$image."\" border=\"0\"/></a>";
  400. }
  401. echo $row['role_name']."<br />\n";
  402. echo $row['role_comment']."<br />\n";
  403. }
  404. */
  405. // course roles
  406. $sql="SELECT * FROM $coures_roles_table WHERE c_id = $course_id ";
  407. $result=Database::query($sql);
  408. while ($row=Database::fetch_array($result))
  409. {
  410. if (in_array($row['role_id'], $current_course_roles))
  411. {
  412. $checked='checked';
  413. $image='checkbox_on2.gif';
  414. $action='revoke';
  415. }
  416. else
  417. {
  418. $checked='';
  419. $image='wrong.gif';
  420. $action='grant';
  421. }
  422. if ($setting_visualisation=='checkbox')
  423. {
  424. echo "<input type=\"checkbox\" name=\"role*course*".$row['role_id']."\" $checked>";
  425. }
  426. if ($setting_visualisation=='image')
  427. {
  428. echo "<a href=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."&amp;action=$action&amp;role=".$row['role_id']."&amp;scope=course\"><img src=\"../img/".$image."\" border=\"0\"/></a>";
  429. }
  430. echo $row['role_name']." <a href=\"../blog/permissions/roles.php?role_id=".$row['role_id']."&amp;scope=course\"><img src=\"../img/edit.gif\" /></a><br />\n";
  431. echo $row['role_comment']."<br />\n";
  432. }
  433. }
  434. /**
  435. * This function gets all the current roles of the user or group
  436. * @param $content are we finding the roles for a user or a group (the database depends on it)
  437. * @param $id the id of the user or group
  438. * @return array that contains the name of the roles the user has
  439. * @todo consider having a separate table that contains only an id and a name of the role.
  440. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  441. * @version 1.0
  442. */
  443. function get_roles($content,$id, $scope='course') {
  444. $course_id = api_get_course_int_id();
  445. if ($content=='user') {
  446. $table=Database::get_course_table(TABLE_ROLE_USER);
  447. $id_field = user_id;
  448. }
  449. if ($content=='group') {
  450. $table=Database::get_course_table(TABLE_ROLE_GROUP);
  451. $id_field = 'group_id';
  452. }
  453. $table_role=Database::get_course_table(TABLE_ROLE);
  454. $current_roles=array();
  455. //$sql="SELECT role.role_id FROM $table role_group_user, $table_role role WHERE role_group_user.$id_field = '$id' AND role_group_user.role_id=role.role_id AND role_group_user.scope='".$scope."'";$sql="SELECT role.role_id FROM $table role_group_user, $table_role role WHERE role_group_user.$id_field = '$id' AND role_group_user.role_id=role.role_id AND role_group_user.scope='".$scope."'";
  456. $sql="SELECT role_id FROM $table WHERE c_id = $course_id AND $id_field = '$id' AND scope='".$scope."'";
  457. $result=Database::query($sql);
  458. while ($row=Database::fetch_array($result)) {
  459. $current_roles[]=$row['role_id'];
  460. }
  461. return $current_roles;
  462. }
  463. /**
  464. * This function gets all the current roles of the user or group
  465. * @return array that contains the name of the roles the user has
  466. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  467. * @version 1.0
  468. */
  469. function get_all_roles($content='course') {
  470. $course_id = api_get_course_int_id();
  471. $course_id_condition = " WHERE c_id = $course_id ";
  472. if ($content=='course')
  473. {
  474. $table_role=Database::get_course_table(TABLE_ROLE);
  475. }
  476. if ($content=='platform')
  477. {
  478. $table_role=Database::get_main_table(TABLE_ROLE);
  479. $course_id_condition = '';
  480. }
  481. $current_roles=array();
  482. $sql="SELECT * FROM $table_role $course_id_condition ";
  483. $result=Database::query($sql);
  484. while ($row=Database::fetch_array($result))
  485. {
  486. $roles[]=$row;
  487. }
  488. return $roles;
  489. }
  490. /**
  491. * This function gets all the roles that are defined
  492. * @param $content are we finding the roles for a user or a group (the database depends on it)
  493. * @param $id the id of the user or group
  494. * @param string Deprecated parameter allowing use of 'platform' scope - the corresponding tables don't exist anymore so the scope is always set to 'course'
  495. * @return array that contains the name of the roles the user has
  496. * @todo consider having a separate table that contains only an id and a name of the role.
  497. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  498. * @version 1.0
  499. */
  500. function get_roles_permissions($content,$id, $scope='course') {
  501. $course_id = api_get_course_int_id();
  502. if ($content == 'user') {
  503. $table=Database::get_course_table(TABLE_ROLE_USER);
  504. $id_field = 'user_id';
  505. }
  506. if ($content == 'group') {
  507. $table = Database::get_course_table(TABLE_ROLE_GROUP);
  508. $id_field = 'group_id';
  509. }
  510. // course roles or platform roles
  511. $scope = 'course';
  512. if ($scope == 'course') {
  513. $table_role = Database::get_course_table(TABLE_ROLE);
  514. $table_role_permissions = Database::get_course_table(TABLE_ROLE_PERMISSION);
  515. $role_condition = " role.c_id = $course_id AND role_permissions.c_id = $course_id AND ";
  516. }
  517. if ($scope == 'platform') {
  518. $table_role = Database::get_main_table(TABLE_ROLE);
  519. $table_role_permissions = Database::get_main_table(TABLE_ROLE_PERMISSION);
  520. $role_condition = '';
  521. }
  522. $current_roles = array();
  523. $sql = "
  524. SELECT *
  525. FROM
  526. " . $table . " role_group_user,
  527. " . $table_role . " role,
  528. " . $table_role_permissions . " role_permissions
  529. WHERE
  530. role_group_user.c_id = $course_id AND
  531. $role_condition
  532. role_group_user.scope = '" . $scope . "' AND
  533. role_group_user." . $id_field . " = '" . $id . "' AND
  534. role_group_user.role_id = role.role_id AND
  535. role.role_id = role_permissions.role_id";
  536. $result = Database::query($sql);
  537. $current_role_permissions = array();
  538. while ($row=Database::fetch_array($result)) {
  539. $current_role_permissions[$row['tool']][]=$row['action'];
  540. }
  541. return $current_role_permissions;
  542. }
  543. /**
  544. * This function is called when we assign a role to a user or a group
  545. * @param $content are we assigning a role to a group or a user
  546. * @param $action we can grant a role to a group or user or revoke it
  547. * @param $id the user_id of the user or the group_id of the group
  548. * @param $role_id the id of the role we are giving to a user or a group.
  549. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  550. */
  551. function assign_role($content, $action, $id, $role_id, $scope='course') {
  552. $course_id = api_get_course_int_id();
  553. // Which database are we using (depending on the $content parameter)
  554. if ($content=='user') {
  555. $table=Database::get_course_table(TABLE_ROLE_USER);
  556. $id_field = 'user_id';
  557. } elseif($content=='group') {
  558. $table=Database::get_course_table(TABLE_ROLE_GROUP);
  559. $id_field = 'group_id';
  560. } else {
  561. return get_lang('Error');
  562. }
  563. // grating a right
  564. if ($action=='grant') {
  565. $sql="INSERT INTO $table (c_id, role_id, scope, $id_field) VALUES ($course_id, '".Database::escape_string($role_id)."','".Database::escape_string($scope)."','".Database::escape_string($id)."')";
  566. $result=Database::query($sql);
  567. if ($result) {
  568. $result_message=get_lang('RoleGranted');
  569. }
  570. }
  571. if ($action=='revoke') {
  572. $sql="DELETE FROM $table WHERE c_id = $course_id AND $id_field = '".Database::escape_string($id)."' AND role_id='".Database::escape_string($role_id)."'";
  573. $result=Database::query($sql);
  574. if ($result) {
  575. $result_message=get_lang('RoleRevoked');
  576. }
  577. }
  578. return $result_message;
  579. }
  580. /**
  581. * This function merges permission arrays. Each permission array has the following structure
  582. * a permission array has a tool contanst as a key and an array as a value. This value array consists of all the permissions that are granted in that tool.
  583. */
  584. function permission_array_merge($array1, $array2)
  585. {
  586. foreach ($array2 as $tool=>$permissions)
  587. {
  588. foreach ($permissions as $permissionkey=>$permissionvalue)
  589. {
  590. $array1[$tool][]=$permissionvalue;
  591. }
  592. }
  593. return $array1;
  594. }
  595. function my_print_r($array)
  596. {
  597. echo '<pre>';
  598. print_r($array);
  599. echo '</pre>';
  600. }
  601. ?>