permissions_functions.inc.php 21 KB

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