permissions_functions.inc.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. {
  233. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  234. }
  235. else
  236. {
  237. if(in_array($permission,$inherited_permissions[$tool]))
  238. {
  239. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  240. }
  241. else
  242. {
  243. if(is_array($permission_array[$tool]) AND in_array($permission,$permission_array[$tool]))
  244. {
  245. if($editable)
  246. {
  247. $url=api_get_self();
  248. foreach($_GET as $key=>$value)
  249. {
  250. $parameter[$key]=$value;
  251. }
  252. $parameter['action']='revoke';
  253. $parameter['permission']=$permission;
  254. $parameter['tool']=$tool;
  255. foreach ($parameter as $key=>$value)
  256. {
  257. $urlparameters.=$key.'='.$value.'&amp;';
  258. }
  259. $url=$url.'?'.$urlparameters;
  260. echo "\t\t\t <a href=\"".$url."\">";
  261. }
  262. echo "<img src=\"../img/checkbox_on2.gif\" border=\"0\"/>";
  263. if($editable)
  264. {
  265. echo "</a>";
  266. }
  267. }
  268. else
  269. {
  270. if($editable)
  271. {
  272. $url=api_get_self();
  273. foreach($_GET as $key=>$value)
  274. {
  275. $parameter[$key]=$value;
  276. }
  277. $parameter['action']='grant';
  278. $parameter['permission']=$permission;
  279. $parameter['tool']=$tool;
  280. foreach ($parameter as $key=>$value)
  281. {
  282. $urlparameters.=$key.'='.$value.'&amp;';
  283. }
  284. $url=$url.'?'.$urlparameters;
  285. //echo "\t\t\t <a href=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."&amp;action=grant&amp;permission=$permission&amp;tool=$tool\">";
  286. echo "\t\t\t <a href=\"".$url."\">";
  287. }
  288. echo "<img src=\"../img/wrong.gif\" border=\"0\"/>";
  289. if($editable)
  290. {
  291. echo "</a>";
  292. }
  293. }
  294. }
  295. }
  296. }
  297. /**
  298. * Slightly modified: Toon Keppens
  299. * This function displays a checked or unchecked image. The image will be checked if the
  300. * user, group or role has the permission for the given tool, unchecked if the user, group or role
  301. * does not have the right
  302. * @param $permission_array the array that contains all the permissions of the user, group, role
  303. * @param $tool the tool we want to check a permission for
  304. * @param $permission the permission we want to check for
  305. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  306. * @version 1.0
  307. */
  308. function display_image_matrix_for_blogs($permission_array, $user_id, $tool, $permission,$inherited_permissions=array(), $course_admin=false, $editable=true)
  309. {
  310. if ($course_admin)
  311. {
  312. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  313. }
  314. else
  315. {
  316. if(!empty($inherited_permissions) and in_array($permission,$inherited_permissions[$tool]))
  317. {
  318. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  319. }
  320. else
  321. {
  322. if(is_array($permission_array[$tool]) AND in_array($permission,$permission_array[$tool]))
  323. {
  324. if($editable)
  325. {
  326. $url=api_get_self();
  327. foreach($_GET as $key=>$value)
  328. {
  329. $parameter[$key]=$value;
  330. }
  331. $parameter['action']='manage_rights';
  332. $parameter['do']='revoke';
  333. $parameter['permission']=$permission;
  334. $parameter['tool']=$tool;
  335. $parameter['user_id']=$user_id;
  336. foreach ($parameter as $key=>$value)
  337. {
  338. $urlparameters.=$key.'='.$value.'&amp;';
  339. }
  340. $url=$url.'?'.$urlparameters;
  341. echo "\t\t\t <a href=\"".$url."\">";
  342. }
  343. echo "<img src=\"../img/checkbox_on2.gif\" border=\"0\"/ title=\"".get_lang('UserHasPermission')."\">";
  344. if($editable)
  345. {
  346. echo "</a>";
  347. }
  348. }
  349. else
  350. {
  351. if($editable)
  352. {
  353. $url=api_get_self();
  354. foreach($_GET as $key=>$value)
  355. {
  356. $parameter[$key]=$value;
  357. }
  358. $parameter['action']='manage_rights';
  359. $parameter['do']='grant';
  360. $parameter['permission']=$permission;
  361. $parameter['tool']=$tool;
  362. $parameter['user_id']=$user_id;
  363. foreach ($parameter as $key=>$value)
  364. {
  365. $urlparameters.=$key.'='.$value.'&amp;';
  366. }
  367. $url=$url.'?'.$urlparameters;
  368. //echo "\t\t\t <a href=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."&amp;action=grant&amp;permission=$permission&amp;tool=$tool\">";
  369. echo "\t\t\t <a href=\"".$url."\">";
  370. }
  371. echo "<img src=\"../img/wrong.gif\" border=\"0\"/ title=\"".get_lang('UserHasPermissionNot')."\">";
  372. if($editable)
  373. {
  374. echo "</a>";
  375. }
  376. }
  377. }
  378. }
  379. }
  380. /**
  381. * This function displays a list off all the roles of the course (and those defined by the platform admin)
  382. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  383. * @version 1.0
  384. */
  385. function display_role_list($current_course_roles, $current_platform_roles)
  386. {
  387. global $setting_visualisation;
  388. $course_id = api_get_course_int_id();
  389. $coures_roles_table=Database::get_course_table(TABLE_ROLE);
  390. $platform_roles_table=Database::get_main_table(TABLE_ROLE);
  391. /*
  392. // platform roles
  393. $sql="SELECT * FROM $platform_roles_table";
  394. $result=Database::query($sql);
  395. while ($row=Database::fetch_array($result))
  396. {
  397. if(in_array($row['role_id'], $current_platform_roles))
  398. {
  399. $checked='checked';
  400. $image='checkbox_on2.gif';
  401. $action='revoke';
  402. }
  403. else
  404. {
  405. $checked='';
  406. $image='wrong.gif';
  407. $action='grant';
  408. }
  409. if($setting_visualisation=='checkbox')
  410. {
  411. echo "<input type=\"checkbox\" name=\"role*platform*".$row['role_id']."\" $checked>";
  412. }
  413. if($setting_visualisation=='image')
  414. {
  415. 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>";
  416. }
  417. echo $row['role_name']."<br />\n";
  418. echo $row['role_comment']."<br />\n";
  419. }
  420. */
  421. // course roles
  422. $sql="SELECT * FROM $coures_roles_table WHERE c_id = $course_id ";
  423. $result=Database::query($sql);
  424. while ($row=Database::fetch_array($result))
  425. {
  426. if(in_array($row['role_id'], $current_course_roles))
  427. {
  428. $checked='checked';
  429. $image='checkbox_on2.gif';
  430. $action='revoke';
  431. }
  432. else
  433. {
  434. $checked='';
  435. $image='wrong.gif';
  436. $action='grant';
  437. }
  438. if($setting_visualisation=='checkbox')
  439. {
  440. echo "<input type=\"checkbox\" name=\"role*course*".$row['role_id']."\" $checked>";
  441. }
  442. if($setting_visualisation=='image')
  443. {
  444. 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>";
  445. }
  446. echo $row['role_name']." <a href=\"../permissions/roles.php?role_id=".$row['role_id']."&amp;scope=course\"><img src=\"../img/edit.gif\" /></a><br />\n";
  447. echo $row['role_comment']."<br />\n";
  448. }
  449. }
  450. /**
  451. * This function gets all the current roles of the user or group
  452. * @param $content are we finding the roles for a user or a group (the database depends on it)
  453. * @param $id the id of the user or group
  454. * @return array that contains the name of the roles the user has
  455. * @todo consider having a separate table that contains only an id and a name of the role.
  456. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  457. * @version 1.0
  458. */
  459. function get_roles($content,$id, $scope='course') {
  460. $course_id = api_get_course_int_id();
  461. if($content=='user') {
  462. $table=Database::get_course_table(TABLE_ROLE_USER);
  463. $id_field = user_id;
  464. }
  465. if($content=='group') {
  466. $table=Database::get_course_table(TABLE_ROLE_GROUP);
  467. $id_field = 'group_id';
  468. }
  469. $table_role=Database::get_course_table(TABLE_ROLE);
  470. $current_roles=array();
  471. //$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."'";
  472. $sql="SELECT role_id FROM $table WHERE c_id = $course_id AND $id_field = '$id' AND scope='".$scope."'";
  473. $result=Database::query($sql);
  474. while ($row=Database::fetch_array($result)) {
  475. $current_roles[]=$row['role_id'];
  476. }
  477. return $current_roles;
  478. }
  479. /**
  480. * This function gets all the current roles of the user or group
  481. * @return array that contains the name of the roles the user has
  482. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  483. * @version 1.0
  484. */
  485. function get_all_roles($content='course') {
  486. $course_id = api_get_course_int_id();
  487. $course_id_condition = " WHERE c_id = $course_id ";
  488. if($content=='course')
  489. {
  490. $table_role=Database::get_course_table(TABLE_ROLE);
  491. }
  492. if($content=='platform')
  493. {
  494. $table_role=Database::get_main_table(TABLE_ROLE);
  495. $course_id_condition = '';
  496. }
  497. $current_roles=array();
  498. $sql="SELECT * FROM $table_role $course_id_condition ";
  499. $result=Database::query($sql);
  500. while ($row=Database::fetch_array($result))
  501. {
  502. $roles[]=$row;
  503. }
  504. return $roles;
  505. }
  506. /**
  507. * This function gets all the roles that are defined
  508. * @param $content are we finding the roles for a user or a group (the database depends on it)
  509. * @param $id the id of the user or group
  510. * @param string Deprecated parameter allowing use of 'platform' scope - the corresponding tables don't exist anymore so the scope is always set to 'course'
  511. * @return array that contains the name of the roles the user has
  512. * @todo consider having a separate table that contains only an id and a name of the role.
  513. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  514. * @version 1.0
  515. */
  516. function get_roles_permissions($content,$id, $scope='course') {
  517. $course_id = api_get_course_int_id();
  518. if($content == 'user') {
  519. $table=Database::get_course_table(TABLE_ROLE_USER);
  520. $id_field = 'user_id';
  521. }
  522. if($content == 'group') {
  523. $table = Database::get_course_table(TABLE_ROLE_GROUP);
  524. $id_field = 'group_id';
  525. }
  526. // course roles or platform roles
  527. $scope = 'course';
  528. if($scope == 'course') {
  529. $table_role = Database::get_course_table(TABLE_ROLE);
  530. $table_role_permissions = Database::get_course_table(TABLE_ROLE_PERMISSION);
  531. $role_condition = " role.c_id = $course_id AND role_permissions.c_id = $course_id AND ";
  532. }
  533. if ($scope == 'platform') {
  534. $table_role = Database::get_main_table(TABLE_ROLE);
  535. $table_role_permissions = Database::get_main_table(TABLE_ROLE_PERMISSION);
  536. $role_condition = '';
  537. }
  538. $current_roles = array();
  539. $sql = "
  540. SELECT *
  541. FROM
  542. " . $table . " role_group_user,
  543. " . $table_role . " role,
  544. " . $table_role_permissions . " role_permissions
  545. WHERE
  546. role_group_user.c_id = $course_id AND
  547. $role_condition
  548. role_group_user.scope = '" . $scope . "' AND
  549. role_group_user." . $id_field . " = '" . $id . "' AND
  550. role_group_user.role_id = role.role_id AND
  551. role.role_id = role_permissions.role_id";
  552. $result = Database::query($sql);
  553. $current_role_permissions = array();
  554. while($row=Database::fetch_array($result)) {
  555. $current_role_permissions[$row['tool']][]=$row['action'];
  556. }
  557. return $current_role_permissions;
  558. }
  559. /**
  560. * This function is called when we assign a role to a user or a group
  561. * @param $content are we assigning a role to a group or a user
  562. * @param $action we can grant a role to a group or user or revoke it
  563. * @param $id the user_id of the user or the group_id of the group
  564. * @param $role_id the id of the role we are giving to a user or a group.
  565. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  566. */
  567. function assign_role($content, $action, $id, $role_id, $scope='course') {
  568. $course_id = api_get_course_int_id();
  569. // Which database are we using (depending on the $content parameter)
  570. if($content=='user') {
  571. $table=Database::get_course_table(TABLE_ROLE_USER);
  572. $id_field = 'user_id';
  573. } elseif($content=='group') {
  574. $table=Database::get_course_table(TABLE_ROLE_GROUP);
  575. $id_field = 'group_id';
  576. } else {
  577. return get_lang('Error');
  578. }
  579. // grating a right
  580. if($action=='grant') {
  581. $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)."')";
  582. $result=Database::query($sql);
  583. if ($result) {
  584. $result_message=get_lang('RoleGranted');
  585. }
  586. }
  587. if($action=='revoke') {
  588. $sql="DELETE FROM $table WHERE c_id = $course_id AND $id_field = '".Database::escape_string($id)."' AND role_id='".Database::escape_string($role_id)."'";
  589. $result=Database::query($sql);
  590. if ($result) {
  591. $result_message=get_lang('RoleRevoked');
  592. }
  593. }
  594. return $result_message;
  595. }
  596. /**
  597. * This function merges permission arrays. Each permission array has the following structure
  598. * 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.
  599. */
  600. function permission_array_merge($array1, $array2)
  601. {
  602. foreach ($array2 as $tool=>$permissions)
  603. {
  604. foreach ($permissions as $permissionkey=>$permissionvalue)
  605. {
  606. $array1[$tool][]=$permissionvalue;
  607. }
  608. }
  609. return $array1;
  610. }
  611. function my_print_r($array)
  612. {
  613. echo '<pre>';
  614. print_r($array);
  615. echo '</pre>';
  616. }
  617. ?>