From c351150fca6d2fc13bb9f78b078ca4370afbe5cc Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Wed, 5 Aug 2009 02:03:50 +0000 Subject: [PATCH] MDL-19756 MDL-19825 MDL-19823 MDL-19794 Deprecated update_tag_button, update_categories_search_button and print_user, and updated a few pages that depended on the now deprecated functions --- course/search.php | 24 ++++- lib/deprecatedlib.php | 52 ++++++++-- lib/outputlib.php | 179 +++++++++++++++++++++++++++----- lib/weblib.php | 232 ------------------------------------------ tag/index.php | 13 ++- user/index.php | 103 ++++++++++++++++++- 6 files changed, 335 insertions(+), 268 deletions(-) diff --git a/course/search.php b/course/search.php index 15d401ed90..cbad211e53 100644 --- a/course/search.php +++ b/course/search.php @@ -176,7 +176,29 @@ $searchform = print_course_search($search, true, "navbar"); if (!empty($courses) && has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) { - $searchform .= update_categories_search_button($search,$page,$perpage); + $searchform = ''; + // not sure if this capability is the best here + if (has_capability('moodle/category:manage', get_context_instance(CONTEXT_SYSTEM))) { + if ($PAGE->user_is_editing()) { + $string = get_string("turneditingoff"); + $edit = "off"; + $perpage = 30; + } else { + $string = get_string("turneditingon"); + $edit = "on"; + } + + $form = new html_form(); + $form->url = new moodle_url("$CFG->wwwroot/course/search.php", array( + 'edit' => $edit, + 'sesskey' => sesskey(), + 'search' => s($search, true), + 'page' => $page, + 'perpage' => $perpage)); + $form->method = 'get'; + $form->button->text = s($string); + $searchform = $OUTPUT->button($form); + } } $navlinks = array(); diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index f30f5c1b5f..198f9fd086 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -3529,7 +3529,7 @@ function print_heading_with_help($text, $helppage, $module='moodle', $icon=false $helpicon->page = $helppage; $helpicon->text = $text; $helpicon->module = $module; - + // Extract the src from $icon if it exists if (preg_match('/src="([^"]*)"/', $icon, $matches)) { $icon = $matches[1]; @@ -3547,14 +3547,52 @@ function print_heading_with_help($text, $helppage, $module='moodle', $icon=false /** * Returns a turn edit on/off button for course in a self contained form. * Used to be an icon, but it's now a simple form button - * * @deprecated since Moodle 2.0 - * - * @global object - * @global object - * @param int $courseid The course to update by id as found in 'course' table - * @return string */ function update_mymoodle_icon() { throw new coding_exception('update_mymoodle_icon() has been completely deprecated.'); } + +/** + * Returns a turn edit on/off button for tag in a self contained form. + * @deprecated since Moodle 2.0 + */ +function update_tag_button() { + throw new coding_exception('update_tag_button() has been completely deprecated.'); +} + + +/** + * Prints the 'update this xxx' button that appears on module pages. + * + * @deprecated since Moodle 2.0 + * + * @param string $cmid the course_module id. + * @param string $ignored not used any more. (Used to be courseid.) + * @param string $string the module name - get_string('modulename', 'xxx') + * @return string the HTML for the button, if this user has permission to edit it, else an empty string. + */ +function update_module_button($cmid, $ignored, $string) { + global $OUTPUT; + + // debugging('update_module_button() has been deprecated. Please change your code to use $OUTPUT->update_module_button().'); + + return $OUTPUT->update_module_button($cmid, $string); +} + +/** + * Prints the editing button on search results listing + * For bulk move courses to another category + * @deprecated since Moodle 2.0 + */ +function update_categories_search_button($search,$page,$perpage) { + throw new coding_exception('update_tag_button() has been completely deprecated.'); +} + +/** + * Prints a summary of a user in a nice little box. + * @deprecated since Moodle 2.0 + */ +function print_user($user, $course, $messageselect=false, $return=false) { + throw new coding_exception('print_user() has been completely deprecated. See user/index.php for new usage.'); +} diff --git a/lib/outputlib.php b/lib/outputlib.php index 0ffa1c2fb9..a524cbd7df 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -2776,6 +2776,27 @@ class moodle_core_renderer extends moodle_renderer_base { return $output; } + /** + * Prints the 'update this xxx' button that appears on module pages. + * + * @param string $cmid the course_module id. + * @param string $modulename the module name - get_string('modulename', 'xxx') + * @return string the HTML for the button, if this user has permission to edit it, else an empty string. + */ + public function update_module_button($cmid, $modulename) { + global $CFG; + if (has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_MODULE, $cmid))) { + $string = get_string('updatethis', '', $modulename); + + $form = new html_form(); + $form->url = new moodle_url("$CFG->wwwroot/course/mod.php", array('update' => $cmid, 'return' => true, 'sesskey' => sesskey())); + $form->button->text = $modulename; + return $this->button($form); + } else { + return ''; + } + } + /** * Outputs a HTML nested list * @@ -3278,43 +3299,62 @@ class moodle_core_renderer extends moodle_renderer_base { $keys = array_keys($table->data); $lastrowkey = end($keys); $output .= $this->output_start_tag('tbody', array()) . "\n"; + foreach ($table->data as $key => $row) { - $oddeven = $oddeven ? 0 : 1; - if (isset($table->rowclasses[$key])) { - $classes = array_unique(moodle_html_component::clean_classes($table->rowclasses[$key])); - } else { - $classes = array(); - } - $classes[] = 'r' . $oddeven; - if ($key == $lastrowkey) { - $classes[] = 'lastrow'; - } - $output .= $this->output_start_tag('tr', array('class' => moodle_renderer_base::prepare_classes($classes))) . "\n"; if (($row === 'hr') && ($countcols)) { $output .= $this->output_tag('td', array('colspan' => $countcols), $this->output_tag('div', array('class' => 'tabledivider'), '')) . "\n"; - } else { /// it's a normal row of data - $keys2 = array_keys($row); + } else { + // Convert array rows to html_table_rows and cell strings to html_table_cell objects + if (!($row instanceof html_table_row)) { + $newrow = new html_table_row(); + + foreach ($row as $key => $item) { + $cell = new html_table_cell(); + $cell->text = $item; + $newrow->cells[] = $cell; + } + $row = $newrow; + } + + $oddeven = $oddeven ? 0 : 1; + if (isset($table->rowclasses[$key])) { + $row->add_classes(array_unique(moodle_html_component::clean_classes($table->rowclasses[$key]))); + } + + $row->add_class('r' . $oddeven); + if ($key == $lastrowkey) { + $row->add_class('lastrow'); + } + + $output .= $this->output_start_tag('tr', array('class' => $row->get_classes_string(), 'style' => $row->style, 'id' => $row->id)) . "\n"; + $keys2 = array_keys($row->cells); $lastkey = end($keys2); - foreach ($row as $key => $item) { + + foreach ($row->cells as $key => $cell) { if (isset($table->colclasses[$key])) { - $classes = array_unique(moodle_html_component::clean_classes($table->colclasses[$key])); - } else { - $classes = array(); + $cell->add_classes(array_unique(moodle_html_component::clean_classes($table->colclasses[$key]))); } - $classes[] = 'cell'; - $classes[] = 'c' . $key; + + $cell->add_classes('cell'); + $cell->add_classes('c' . $key); if ($key == $lastkey) { - $classes[] = 'lastcol'; + $cell->add_classes('lastcol'); } $tdstyle = ''; $tdstyle .= isset($table->align[$key]) ? $table->align[$key] : ''; $tdstyle .= isset($table->size[$key]) ? $table->size[$key] : ''; $tdstyle .= isset($table->wrap[$key]) ? $table->wrap[$key] : ''; - $output .= $this->output_tag('td', - array('style' => $tdstyle, - 'class' => moodle_renderer_base::prepare_classes($classes)), - $item) . "\n"; + $tdattributes = array( + 'style' => $tdstyle . $cell->style, + 'colspan' => $cell->colspan, + 'rowspan' => $cell->rowspan, + 'id' => $cell->id, + 'class' => $cell->get_classes_string(), + 'abbr' => $cell->abbr, + 'scope' => $cell->scope); + + $output .= $this->output_tag('td', $tdattributes, $cell->text) . "\n"; } } $output .= $this->output_end_tag('tr') . "\n"; @@ -4371,14 +4411,28 @@ class html_table extends moodle_html_component { */ public $wrap; /** - * @var array of arrays containing the data. Alternatively, if you have + * @var array of arrays or html_table_row objects containing the data. Alternatively, if you have * $head specified, the string 'hr' (for horizontal ruler) can be used * instead of an array of cells data resulting in a divider rendered. * - * Example if usage: + * Example of usage with array of arrays: * $row1 = array('Harry Potter', '76 %'); * $row2 = array('Hermione Granger', '100 %'); * $t->data = array($row1, $row2); + * + * Example with array of html_table_row objects: (used for more fine-grained control) + * $cell1 = new html_table_cell(); + * $cell1->text = 'Harry Potter'; + * $cell1->colspan = 2; + * $row1 = new html_table_row(); + * $row1->cells[] = $cell1; + * $cell2 = new html_table_cell(); + * $cell2->text = 'Hermione Granger'; + * $cell3 = new html_table_cell(); + * $cell3->text = '100 %'; + * $row2 = new html_table_row(); + * $row2->cells = array($cell2, $cell3); + * $t->data = array($row1, $row2); */ public $data; /** @@ -4506,6 +4560,66 @@ class html_table extends moodle_html_component { } } +/** + * Component representing a table row. + * + * @copyright 2009 Nicolas Connault + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 2.0 + */ +class html_table_row extends moodle_html_component { + /** + * @var array $cells Array of html_table_cell objects + */ + public $cells = array(); + + /** + * @see lib/moodle_html_component#prepare() + * @return void + */ + public function prepare() { + parent::prepare(); + } +} + +/** + * Component representing a table cell. + * + * @copyright 2009 Nicolas Connault + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 2.0 + */ +class html_table_cell extends moodle_html_component { + /** + * @var string $text The contents of the cell + */ + public $text; + /** + * @var string $abbr Abbreviated version of the contents of the cell + */ + public $abbr = ''; + /** + * @var int $colspan Number of columns this cell should span + */ + public $colspan = ''; + /** + * @var int $rowspan Number of rows this cell should span + */ + public $rowspan = ''; + /** + * @var string $scope Defines a way to associate header cells and data cells in a table + */ + public $scope = ''; + + /** + * @see lib/moodle_html_component#prepare() + * @return void + */ + public function prepare() { + parent::prepare(); + } +} + /** * Component representing a XHTML link. * @@ -4537,6 +4651,19 @@ class html_link extends moodle_html_component { parent::prepare(); } + + /** + * Shortcut for creating a link component. + * @param mixed $url String or moodle_url + * @param string $text The text of the link + * @return html_link The link component + */ + public function make($url, $text) { + $link = new html_link(); + $link->url = $url; + $link->text = $text; + return $link; + } } /** diff --git a/lib/weblib.php b/lib/weblib.php index 6b970717ed..cb39dfa851 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2450,146 +2450,6 @@ function _print_custom_corners_end($idbase) { } -/** - * Prints a summary of a user in a nice little box. - * - * @global object - * @global object - * @staticvar object $string - * @staticvar object $datestring - * @staticvar array $countries - * @uses CONTEXT_COURSE - * @uses CONTEXT_USER - * @uses SITEID - * @param object $user A {@link $USER} object representing a user - * @param object $course A {@link $COURSE} object representing a course - * @param bool $messageselect - * @param bool $return If set to true then the HTML is returned rather than echo'd - * @return string|void Depending on the setting of $return - */ -function print_user($user, $course, $messageselect=false, $return=false) { - - global $CFG, $USER; - - $output = ''; - - static $string; - static $datestring; - static $countries; - - $context = get_context_instance(CONTEXT_COURSE, $course->id); - if (isset($user->context->id)) { - $usercontext = $user->context; - } else { - $usercontext = get_context_instance(CONTEXT_USER, $user->id); - } - - if (empty($string)) { // Cache all the strings for the rest of the page - - $string->email = get_string('email'); - $string->city = get_string('city'); - $string->lastaccess = get_string('lastaccess'); - $string->activity = get_string('activity'); - $string->unenrol = get_string('unenrol'); - $string->loginas = get_string('loginas'); - $string->fullprofile = get_string('fullprofile'); - $string->role = get_string('role'); - $string->name = get_string('name'); - $string->never = get_string('never'); - - $datestring->day = get_string('day'); - $datestring->days = get_string('days'); - $datestring->hour = get_string('hour'); - $datestring->hours = get_string('hours'); - $datestring->min = get_string('min'); - $datestring->mins = get_string('mins'); - $datestring->sec = get_string('sec'); - $datestring->secs = get_string('secs'); - $datestring->year = get_string('year'); - $datestring->years = get_string('years'); - - $countries = get_list_of_countries(); - } - -/// Get the hidden field list - if (has_capability('moodle/course:viewhiddenuserfields', $context)) { - $hiddenfields = array(); - } else { - $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields)); - } - - $output .= ''; - $output .= ''; - $output .= ''; - $output .= '
'; - $output .= print_user_picture($user, $course->id, $user->picture, true, true); - $output .= ''; - $output .= '
'.fullname($user, has_capability('moodle/site:viewfullnames', $context)).'
'; - $output .= '
'; - if (!empty($user->role)) { - $output .= $string->role .': '. $user->role .'
'; - } - if ($user->maildisplay == 1 or ($user->maildisplay == 2 and ($course->id != SITEID) and !isguest()) or -has_capability('moodle/course:viewhiddenuserfields', $context)) { - $output .= $string->email .': '. $user->email .'
'; - } - if (($user->city or $user->country) and (!isset($hiddenfields['city']) or !isset($hiddenfields['country']))) { - $output .= $string->city .': '; - if ($user->city && !isset($hiddenfields['city'])) { - $output .= $user->city; - } - if (!empty($countries[$user->country]) && !isset($hiddenfields['country'])) { - if ($user->city && !isset($hiddenfields['city'])) { - $output .= ', '; - } - $output .= $countries[$user->country]; - } - $output .= '
'; - } - - if (!isset($hiddenfields['lastaccess'])) { - if ($user->lastaccess) { - $output .= $string->lastaccess .': '. userdate($user->lastaccess); - $output .= '  ('. format_time(time() - $user->lastaccess, $datestring) .')'; - } else { - $output .= $string->lastaccess .': '. $string->never; - } - } - $output .= '
'; - - if ($return) { - return $output; - } else { - echo $output; - } -} - /** * Print a specified group's avatar. * @@ -2763,98 +2623,6 @@ function switchroles_form($courseid) { return ''; } - -/** - * Returns a turn edit on/off button for tag in a self contained form. - * - * @global object - * @global object - * @param string $tagid The ID attribute - * @return string - */ -function update_tag_button($tagid) { - - global $CFG, $USER; - - if (!empty($USER->editing)) { - $string = get_string('turneditingoff'); - $edit = '0'; - } else { - $string = get_string('turneditingon'); - $edit = '1'; - } - - return "
frametarget method=\"get\" action=\"$CFG->wwwroot/tag/index.php\">". - "
". - "". - "". - "
"; -} - -/** - * Prints the 'update this xxx' button that appears on module pages. - * - * @global object - * @global object - * @uses CONTEXT_MODULE - * @param string $cmid the course_module id. - * @param string $ignored not used any more. (Used to be courseid.) - * @param string $string the module name - get_string('modulename', 'xxx') - * @return string the HTML for the button, if this user has permission to edit it, else an empty string. - */ -function update_module_button($cmid, $ignored, $string) { - global $CFG, $USER; - - if (has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_MODULE, $cmid))) { - $string = get_string('updatethis', '', $string); - - return "
frametarget method=\"get\" action=\"$CFG->wwwroot/course/mod.php\" onsubmit=\"this.target='{$CFG->framename}'; return true\">".//hack to allow edit on framed resources - "
". - "". - "". - "". - "
"; - } else { - return ''; - } -} - -/** - * Prints the editing button on search results listing - * For bulk move courses to another category - * - * @global object - * @global object - * @param string $search The search string - * @param string $page - * @param string $perpage - * @return string HTML form element - */ -function update_categories_search_button($search,$page,$perpage) { - global $CFG, $PAGE; - - // not sure if this capability is the best here - if (has_capability('moodle/category:manage', get_context_instance(CONTEXT_SYSTEM))) { - if ($PAGE->user_is_editing()) { - $string = get_string("turneditingoff"); - $edit = "off"; - $perpage = 30; - } else { - $string = get_string("turneditingon"); - $edit = "on"; - } - - return "
frametarget method=\"get\" action=\"$CFG->wwwroot/course/search.php\">". - '
'. - "". - "". - "". - "". - "". - "
"; - } -} - /** * Returns a small popup menu of course activity modules * diff --git a/tag/index.php b/tag/index.php index 80ee643580..d93f58e25b 100644 --- a/tag/index.php +++ b/tag/index.php @@ -51,7 +51,18 @@ $title = get_string('tag', 'tag') .' - '. $tagname; $button = ''; if ($PAGE->user_allowed_editing() ) { - $button = update_tag_button($tag->id); + if (!empty($USER->editing)) { + $string = get_string('turneditingoff'); + $edit = '0'; + } else { + $string = get_string('turneditingon'); + $edit = '1'; + } + + $form = new html_form(); + $form->url = new moodle_url("$CFG->wwwroot/tag/index.php", array('edit' => $edit, 'id' => $tagid)); + $form->button->text = $string; + $button = $OUTPUT->button($form); } print_header_simple($title, '', $navigation, '', '', '', $button); diff --git a/user/index.php b/user/index.php index cd3c290653..e925d6cabb 100644 --- a/user/index.php +++ b/user/index.php @@ -705,7 +705,108 @@ $usersprinted[] = $user->id; /// Add new user to the array of users printed $user = make_context_subobj($user); - print_user($user, $course, $bulkoperations); + + $context = get_context_instance(CONTEXT_COURSE, $course->id); + if (isset($user->context->id)) { + $usercontext = $user->context; + } else { + $usercontext = get_context_instance(CONTEXT_USER, $user->id); + } + + $countries = get_list_of_countries(); + + /// Get the hidden field list + if (has_capability('moodle/course:viewhiddenuserfields', $context)) { + $hiddenfields = array(); + } else { + $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields)); + } + $table = new html_table(); + $table->add_class('userinfobox'); + + $row = new html_table_row(); + $row->cells[0] = new html_table_cell(); + $row->cells[0]->add_class('left side'); + $row->cells[0]->text = print_user_picture($user, $course->id, $user->picture, true, true); + $row->cells[1] = new html_table_cell(); + $row->cells[1]->add_class('content'); + + $row->cells[1]->text = $OUTPUT->container(fullname($user, has_capability('moodle/site:viewfullnames', $context)), 'username'); + $row->cells[1]->text .= $OUTPUT->container_start('info'); + + if (!empty($user->role)) { + $row->cells[1]->text .= get_string('role') .': '. $user->role .'
'; + } + if ($user->maildisplay == 1 or ($user->maildisplay == 2 and ($course->id != SITEID) and !isguest()) or + has_capability('moodle/course:viewhiddenuserfields', $context)) { + $link = new html_link(); + $link->url = "mailto:$user->email"; + $link->text = $user->email; + $row->cells[1]->text .= get_string('email') .': ' . $OUTPUT->link($link) . '
'; + } + if (($user->city or $user->country) and (!isset($hiddenfields['city']) or !isset($hiddenfields['country']))) { + $row->cells[1]->text .= get_string('city') .': '; + if ($user->city && !isset($hiddenfields['city'])) { + $row->cells[1]->text .= $user->city; + } + if (!empty($countries[$user->country]) && !isset($hiddenfields['country'])) { + if ($user->city && !isset($hiddenfields['city'])) { + $row->cells[1]->text .= ', '; + } + $row->cells[1]->text .= $countries[$user->country]; + } + $row->cells[1]->text .= '
'; + } + + if (!isset($hiddenfields['lastaccess'])) { + if ($user->lastaccess) { + $row->cells[1]->text .= get_string('lastaccess') .': '. userdate($user->lastaccess); + $row->cells[1]->text .= '  ('. format_time(time() - $user->lastaccess, $datestring) .')'; + } else { + $row->cells[1]->text .= get_string('lastaccess') .': '. get_string('never'); + } + } + + $row->cells[1]->text .= $OUTPUT->container_end(); + + $row->cells[2] = new html_table_cell(); + $row->cells[2]->add_class('links'); + $row->cells[2]->text = ''; + + $links = array(); + + if ($CFG->bloglevel > 0) { + $links[] = html_link::make(new moodle_url($CFG->wwwroot.'/blog/index.php?userid='.$user->id), get_string('blogs','blog')); + } + + if (!empty($CFG->enablenotes) and (has_capability('moodle/notes:manage', $context) || has_capability('moodle/notes:view', $context))) { + $links[] = html_link::make(new moodle_url($CFG->wwwroot.'/notes/index.php?course=' . $course->id. '&user='.$user->id), get_string('notes','notes')); + } + + if (has_capability('moodle/site:viewreports', $context) or has_capability('moodle/user:viewuseractivitiesreport', $usercontext)) { + $links[] = html_link::make(new moodle_url($CFG->wwwroot.'/course/user.php?id='. $course->id .'&user='. $user->id), get_string('activity')); + } + + if (has_capability('moodle/role:assign', $context) and get_user_roles($context, $user->id, false)) { // I can unassign and user has some role + $links[] = html_link::make(new moodle_url($CFG->wwwroot.'/course/unenrol.php?id='. $course->id .'&user='. $user->id), get_string('unenrol')); + } + + if ($USER->id != $user->id && !session_is_loggedinas() && has_capability('moodle/user:loginas', $context) && + ! has_capability('moodle/site:doanything', $context, $user->id, false)) { + $links[] = html_link::make(new moodle_url($CFG->wwwroot.'/course/loginas.php?id='. $course->id .'&user='. $user->id .'&sesskey='. sesskey()), get_string('loginas')); + } + + $links[] = html_link::make(new moodle_url($CFG->wwwroot.'/user/view.php?id='. $user->id .'&course='. $course->id), get_string('fullprofile') . '...'); + + foreach ($links as $link) { + $row->cells[2]->text .= $OUTPUT->link($link); + } + + if (!empty($messageselect)) { + $row->cells[2]->text .= '
'; + } + $table->data = array($row); + echo $OUTPUT->print_table($table); } } else { -- 2.39.5